Răsfoiți Sursa

修复Word图片地址并增加正文附件按钮

Word 导入时统一把内网绝对上传地址和 data-original 图片地址归一化为 /web/upload 相对路径,保存文章时同步清理图片懒加载属性,避免外网加载 ht.gzhxtc.edu.cn 图片失败。\n\n在富文本工具栏“常用语”后增加“附件”按钮,支持选择本地任意文件上传,正文插入蓝色文件名下载链接,上传上限100MB。新增历史正文上传地址批量修复脚本并更新文章编辑器缓存版本。\n\nValidation:\n- node --check WebRoot/apps/space/js/space.vue.subassembly.js\n- bash -n local-run/repairs/2026-11-10-normalize-upload-urls.sh\n- git diff --cached --check
非繁 2 săptămâni în urmă
părinte
comite
551432962a

+ 148 - 8
WebRoot/apps/space/js/space.vue.subassembly.js

@@ -750,16 +750,21 @@ Vue.component('spaceEditor', {
 
 			var missingImageIndex = 0;
 			for (var imageIndex = 0; imageIndex < outputImages.length; imageIndex++) {
-				if (outputImages[imageIndex].getAttribute("src")) {
-					continue;
+				var outputImage = outputImages[imageIndex];
+				var src = getImageSrc(outputImage);
+				if (!src) {
+					src = editorImages[imageIndex]
+						|| modelImages[imageIndex]
+						|| (this.uploadedImageUrls ? this.uploadedImageUrls[missingImageIndex] : "")
+						|| (this.uploadedImageUrls ? this.uploadedImageUrls[imageIndex] : "")
+						|| "";
+					src = this.normalizeEditorMediaUrl(src);
 				}
-				var src = editorImages[imageIndex]
-					|| modelImages[imageIndex]
-					|| (this.uploadedImageUrls ? this.uploadedImageUrls[missingImageIndex] : "")
-					|| (this.uploadedImageUrls ? this.uploadedImageUrls[imageIndex] : "")
-					|| "";
 				if (src) {
-					outputImages[imageIndex].setAttribute("src", src);
+					outputImage.setAttribute("src", src);
+					outputImage.removeAttribute("data-original");
+					outputImage.removeAttribute("data-src");
+					outputImage.removeAttribute("data-lazy-src");
 				}
 				missingImageIndex++;
 			}
@@ -1985,6 +1990,69 @@ Vue.component('spaceEditor', {
 				});
 				this.refreshLineHeightControl(editor);
 			},
+			mountAttachmentToolbarButton: function (editor) {
+				if (!editor || !editor.ui || !editor.ui.view || !editor.ui.view.element) {
+					return;
+				}
+
+				var toolbarItems = editor.ui.view.element.querySelector('.ck-toolbar__items');
+				if (!toolbarItems || toolbarItems.querySelector('[data-space-attachment="true"]')) {
+					return;
+				}
+
+				var phraseButton = null;
+				var buttons = toolbarItems.querySelectorAll('.ck-button');
+				for (var i = 0; i < buttons.length; i++) {
+					var label = (buttons[i].getAttribute('aria-label') || buttons[i].getAttribute('title') || buttons[i].textContent || '').replace(/\s/g, '');
+					if (label.indexOf('常用语') !== -1) {
+						phraseButton = buttons[i];
+						break;
+					}
+				}
+				if (!phraseButton) {
+					return;
+				}
+
+				var button = phraseButton.cloneNode(true);
+				button.removeAttribute('aria-haspopup');
+				button.removeAttribute('aria-controls');
+				button.removeAttribute('aria-expanded');
+				button.removeAttribute('aria-disabled');
+				button.removeAttribute('aria-labelledby');
+				button.removeAttribute('data-cke-tooltip-text');
+				button.classList.remove('ck-dropdown__button', 'ck-disabled', 'ck-on');
+				button.classList.add('ck-off');
+				button.setAttribute('aria-label', '附件');
+				button.setAttribute('title', '附件');
+				button.setAttribute('data-space-attachment', 'true');
+				button.tabIndex = 0;
+
+				var buttonLabel = button.querySelector('.ck-button__label');
+				if (buttonLabel) {
+					buttonLabel.textContent = '附件';
+				}
+				var arrow = button.querySelector('.ck-dropdown__arrow');
+				if (arrow) {
+					arrow.remove();
+				}
+
+				var _this = this;
+				button.addEventListener('click', function (event) {
+					event.preventDefault();
+					event.stopPropagation();
+					_this.openAttachmentPicker();
+				});
+
+				var anchor = phraseButton;
+				while (anchor && anchor.parentNode && anchor.parentNode !== toolbarItems) {
+					anchor = anchor.parentNode;
+				}
+				if (anchor && anchor.parentNode === toolbarItems) {
+					toolbarItems.insertBefore(button, anchor.nextSibling);
+				} else {
+					toolbarItems.appendChild(button);
+				}
+			},
 		alignFullScreenToolbarButton: function () {
 			if (!this.editor || !this.editor.ui || !this.editor.ui.view || !this.editor.ui.view.element) {
 				return;
@@ -2632,6 +2700,21 @@ Vue.component('spaceEditor', {
 					}
 					inlineNode.parentNode.removeChild(inlineNode);
 				}
+				var importedImages = wrapper.querySelectorAll('img');
+				for (var importedImageIndex = 0; importedImageIndex < importedImages.length; importedImageIndex++) {
+					var importedImage = importedImages[importedImageIndex];
+					var importedSrc = this.normalizeEditorMediaUrl(importedImage.getAttribute('data-original')
+						|| importedImage.getAttribute('data-src')
+						|| importedImage.getAttribute('data-lazy-src')
+						|| importedImage.getAttribute('src')
+						|| '');
+					if (importedSrc) {
+						importedImage.setAttribute('src', importedSrc);
+						importedImage.removeAttribute('data-original');
+						importedImage.removeAttribute('data-src');
+						importedImage.removeAttribute('data-lazy-src');
+					}
+				}
 				var normalizedHtml = wrapper.innerHTML;
 				document.body.removeChild(wrapper);
 				return normalizedHtml;
@@ -2793,6 +2876,11 @@ Vue.component('spaceEditor', {
 				this.$refs.pdfInput.click();
 			}
 		},
+		openAttachmentPicker: function () {
+			if (this.$refs.attachmentInput) {
+				this.$refs.attachmentInput.click();
+			}
+		},
 		handleVideoSelect: function (event) {
 			var files = Array.prototype.slice.call(event.target.files || []);
 			event.target.value = "";
@@ -2807,6 +2895,56 @@ Vue.component('spaceEditor', {
 				this.uploadPdfFile(files[0]);
 			}
 		},
+		handleAttachmentSelect: function (event) {
+			var files = Array.prototype.slice.call(event.target.files || []);
+			event.target.value = "";
+			if (files.length > 0) {
+				this.uploadAttachmentFile(files[0]);
+			}
+		},
+		uploadAttachmentFile: function (file) {
+			if (!file) {
+				return;
+			}
+
+			var _this = this;
+			_this.uploading = true;
+			_this.uploadStatus = '正在上传附件';
+			_this.uploadPlainFileByAdapter(file, {
+				imageQuality: 'origin',
+				fileMaxSize: 100 * 1024 * 1024
+			}, function (loaded, total) {
+				if (total > 0) {
+					_this.uploadStatus = '正在上传附件 ' + Math.round(loaded * 100 / total) + '%';
+				}
+			}).then(function (res) {
+				var attachmentUrl = _this.normalizeLocalUploadUrl(res && res.default ? res.default : res);
+				if (!attachmentUrl) {
+					throw new Error('附件上传失败');
+				}
+				_this.insertAttachmentLink(attachmentUrl, file.name);
+				_this.$emit('input', _this.getEditorSaveHtml());
+				space.message.success('附件已插入正文');
+			}).catch(function (msg) {
+				space.message.error(msg || '附件上传失败');
+			}).finally(function () {
+				_this.uploading = false;
+				_this.uploadStatus = '';
+			});
+		},
+		buildAttachmentLink: function (url, fileName) {
+			var safeUrl = this.escapeHtmlAttr(url);
+			var safeName = this.escapeHtmlAttr(fileName || '附件');
+			return '<p data-space-attachment="true"><a href="' + safeUrl + '" download="' + safeName
+				+ '" target="_blank" rel="noopener" title="下载附件:' + safeName
+				+ '" style="color:#1769ff;text-decoration:underline;">' + safeName + '</a></p><p></p>';
+		},
+		insertAttachmentLink: function (url, fileName) {
+			if (!url) {
+				return;
+			}
+			this.insertHtml(this.buildAttachmentLink(url, fileName));
+		},
 		isPdfFile: function (file) {
 			return !!file && ((file.type && file.type.toLowerCase() == "application/pdf") || /\.pdf$/i.test(file.name || ""));
 		},
@@ -3596,6 +3734,7 @@ Vue.component('spaceEditor', {
 				_this.alignFullScreenToolbarButton();
 					_this.mountFirstLineIndentToolbarButton(editor);
 					_this.mountLineHeightToolbar(editor);
+					_this.mountAttachmentToolbarButton(editor);
 					_this.configureWordUploadPlugin(editor);
 				if (_this.type !== 'flow') {
 					_this.mountVideoUploadToolbarButton();
@@ -3716,6 +3855,7 @@ Vue.component('spaceEditor', {
 		</div>
 		<input ref="videoInput" type="file" accept="video/*" style="display:none;" @change="handleVideoSelect">
 		<input ref="pdfInput" type="file" accept="application/pdf,.pdf" style="display:none;" @change="handlePdfSelect">
+		<input ref="attachmentInput" type="file" style="display:none;" @change="handleAttachmentSelect">
 		<div :id="id">
 			<script type="text/plain"></script>
 		</div>

+ 1 - 1
WebRoot/web/subassembly/app/pc/10021/10104/1002110104_run.jsp

@@ -18,7 +18,7 @@
  <script src="/apps/space/plug-in/element-resize-detector/element-resize-detector.min.js"></script> 
  <script src="/apps/space/plug-in/vue/vue.min.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/apps/space/plug-in/element-ui@2.13.0/index.js" type="text/javascript" charset="utf-8"></script> 
- <script src="/js/space.vue.pc.subassembly.js?v=20261110-default-typography-indent-line-height"></script>
+ <script src="/js/space.vue.pc.subassembly.js?v=20261110-word-images-attachment"></script>
  <script src="/apps/space/plug-in/layui/dist/layui.all.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.recycle.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.display.js?v=20261110-title-submit-v2" type="text/javascript" charset="utf-8"></script>

+ 1 - 1
WebRoot/web/subassembly/app/pc/10021/10105/1002110105_run.jsp

@@ -18,7 +18,7 @@
  <script src="/apps/space/plug-in/element-resize-detector/element-resize-detector.min.js"></script> 
  <script src="/apps/space/plug-in/vue/vue.min.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/apps/space/plug-in/element-ui@2.13.0/index.js" type="text/javascript" charset="utf-8"></script> 
- <script src="/js/space.vue.pc.subassembly.js?v=20261110-default-typography-indent-line-height"></script>
+ <script src="/js/space.vue.pc.subassembly.js?v=20261110-word-images-attachment"></script>
  <script src="/apps/space/plug-in/layui/dist/layui.all.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.recycle.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.display.js?v=20261110-title-submit-v2" type="text/javascript" charset="utf-8"></script>

+ 1 - 1
WebRoot/web/subassembly/app/pc/10021/10211/1002110211_run.jsp

@@ -18,7 +18,7 @@
  <script src="/apps/space/plug-in/element-resize-detector/element-resize-detector.min.js"></script> 
  <script src="/apps/space/plug-in/vue/vue.min.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/apps/space/plug-in/element-ui@2.13.0/index.js" type="text/javascript" charset="utf-8"></script> 
- <script src="/js/space.vue.pc.subassembly.js?v=20261110-default-typography-indent-line-height"></script>
+ <script src="/js/space.vue.pc.subassembly.js?v=20261110-word-images-attachment"></script>
  <script src="/apps/space/plug-in/layui/dist/layui.all.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.recycle.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.display.js?v=20261110-title-submit-v2" type="text/javascript" charset="utf-8"></script>

+ 43 - 0
local-run/repairs/2026-11-10-normalize-upload-urls.sh

@@ -0,0 +1,43 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+WEBAPP_ROOT="${1:?用法: $0 <webapp根目录> <备份tar.gz路径>}"
+BACKUP_FILE="${2:?用法: $0 <webapp根目录> <备份tar.gz路径>}"
+UPLOAD_ROOT="${WEBAPP_ROOT%/}/web/upload"
+FILE_LIST="$(mktemp)"
+
+cleanup() {
+  rm -f "${FILE_LIST}"
+}
+trap cleanup EXIT
+
+if [[ ! -d "${UPLOAD_ROOT}" ]]; then
+  printf '上传目录不存在:%s\n' "${UPLOAD_ROOT}" >&2
+  exit 1
+fi
+
+find "${UPLOAD_ROOT}" -type f \( -name '*.html' -o -name '*.htm' \) -print0 \
+  | xargs -0 grep -IlE 'https?://(ht|www)\.gzhxtc\.edu\.cn//?web/upload/' \
+  > "${FILE_LIST}" || true
+
+FILE_COUNT="$(wc -l < "${FILE_LIST}" | tr -d ' ')"
+printf '待修复正文文件:%s\n' "${FILE_COUNT}"
+if [[ "${FILE_COUNT}" == '0' ]]; then
+  exit 0
+fi
+
+mkdir -p "$(dirname "${BACKUP_FILE}")"
+tar -czf "${BACKUP_FILE}" -T "${FILE_LIST}"
+printf '修复前备份:%s\n' "${BACKUP_FILE}"
+
+xargs -r -n 200 sed -i -E \
+  's#https?://(ht|www)\.gzhxtc\.edu\.cn//?web/upload/#/web/upload/#g' \
+  < "${FILE_LIST}"
+
+REMAINING="$(find "${UPLOAD_ROOT}" -type f \( -name '*.html' -o -name '*.htm' \) -print0 \
+  | xargs -0 grep -IlE 'https?://(ht|www)\.gzhxtc\.edu\.cn//?web/upload/' \
+  | wc -l | tr -d ' ')"
+printf '修复后剩余:%s\n' "${REMAINING}"
+if [[ "${REMAINING}" != '0' ]]; then
+  exit 2
+fi