Просмотр исходного кода

修复 Word 导入图片被放大

Word 转换结果保留了图片显示宽高,但 CKEditor 会丢弃 img 的宽高属性,且编辑器 CSS 强制图片宽度 100%,导致小图被拉满正文。
导入 Word 时读取原始显示宽度,插入编辑器后写入 CKEditor 图片宽度属性;样式改为宽度自动、最大宽度 100%。

Validation:
- 实际 DOCX 4 张图片导入后宽度为 252/255/261/256px,与 Word 原稿一致
- 图片全部加载并居中
- node --check 通过
- 内网、外网 JS/CSS SHA-256 一致
- 测试文章未保存,测试账号已退出
非繁 1 неделя назад
Родитель
Сommit
d50cd8d902

+ 4 - 3
WebRoot/apps/space/css/space.editor.css

@@ -241,7 +241,8 @@
 
 .space-editor-wrap .ck-content .image {
 	display: block;
-	width: 100%;
+	width: auto;
+	max-width: 100%;
 	margin: 18px 0;
 	text-align: left;
 }
@@ -661,7 +662,7 @@ html.article-info-layer-page body {
 .article-body .ck-content .image,
 .article-body .ck-content figure.image {
 	display: block !important;
-	width: 100% !important;
+	width: auto;
 	max-width: 100% !important;
 	margin: 18px 0 !important;
 	padding: 0 !important;
@@ -696,7 +697,7 @@ html.article-info-layer-page body {
 .article-body figure.image,
 .article-body figure:has(img) {
 	display: block !important;
-	width: 100% !important;
+	width: auto;
 	max-width: 100% !important;
 	margin: 18px 0;
 	text-align: left !important;

+ 80 - 0
WebRoot/apps/space/js/space.vue.subassembly.js

@@ -401,6 +401,7 @@ Vue.component('spaceEditor', {
 			defaultEditorFontFamily: "'仿宋_GB2312', 'FangSong_GB2312', FangSong, serif",
 			defaultEditorFontSize: "20pt",
 			defaultEditorLineHeight: "1.75",
+			pendingImportedWordImageSizes: [],
 		}
 	},
 	name: "spaceEditor",
@@ -2737,8 +2738,18 @@ Vue.component('spaceEditor', {
 					inlineNode.parentNode.removeChild(inlineNode);
 				}
 				var importedImages = wrapper.querySelectorAll('img');
+				var importedImageSizes = [];
+				var imageReferenceWidth = 900;
+				if (this.editor && this.editor.ui && this.editor.ui.view && this.editor.ui.view.editable) {
+					imageReferenceWidth = this.editor.ui.view.editable.element.clientWidth || imageReferenceWidth;
+				}
 				for (var importedImageIndex = 0; importedImageIndex < importedImages.length; importedImageIndex++) {
 					var importedImage = importedImages[importedImageIndex];
+					var importedWidth = this.parseImportedImageLength(importedImage.getAttribute('width')
+						|| (importedImage.style && importedImage.style.width), imageReferenceWidth);
+					var importedHeight = this.parseImportedImageLength(importedImage.getAttribute('height')
+						|| (importedImage.style && importedImage.style.height), imageReferenceWidth);
+					importedImageSizes.push({ width: importedWidth, height: importedHeight });
 					var importedSrc = this.normalizeEditorMediaUrl(importedImage.getAttribute('data-original')
 						|| importedImage.getAttribute('data-src')
 						|| importedImage.getAttribute('data-lazy-src')
@@ -2751,15 +2762,84 @@ Vue.component('spaceEditor', {
 						importedImage.removeAttribute('data-lazy-src');
 					}
 				}
+				this.pendingImportedWordImageSizes = importedImageSizes;
 				var normalizedHtml = wrapper.innerHTML;
 				document.body.removeChild(wrapper);
 				return normalizedHtml;
 			},
 			insertFormattedWordHtml: function (editor, htmlContent) {
 				this.insertHtml(htmlContent);
+				this.applyImportedWordImageSizes(editor);
 				this.$emit('input', this.getEditorSaveHtml());
 				this.deferSyncEditorPlayableVideos();
 			},
+			parseImportedImageLength: function (value, referenceWidth) {
+				if (value === null || value === undefined || value === '') {
+					return 0;
+				}
+				var text = String(value).trim().toLowerCase();
+				var match = text.match(/^(-?\d+(?:\.\d+)?)\s*(px|pt|in|cm|mm|%)?$/);
+				if (!match) {
+					return 0;
+				}
+				var number = parseFloat(match[1]);
+				if (!isFinite(number) || number <= 0) {
+					return 0;
+				}
+				var unit = match[2] || 'px';
+				if (unit === 'pt') {
+					return number * 96 / 72;
+				}
+				if (unit === 'in') {
+					return number * 96;
+				}
+				if (unit === 'cm') {
+					return number * 96 / 2.54;
+				}
+				if (unit === 'mm') {
+					return number * 96 / 25.4;
+				}
+				if (unit === '%') {
+					return referenceWidth * number / 100;
+				}
+				return number;
+			},
+			applyImportedWordImageSizes: function (editor) {
+				var sizes = this.pendingImportedWordImageSizes || [];
+				this.pendingImportedWordImageSizes = [];
+				if (!sizes.length || !editor || !editor.model || !editor.model.document) {
+					return;
+				}
+				var root = editor.model.document.getRoot();
+				var range = editor.model.createRangeIn(root);
+				var walker = range.getWalker({ ignoreElementEnd: true });
+				var images = [];
+				for (var step = walker.next(); !step.done; step = walker.next()) {
+					var item = step.value.item;
+					if (item && item.is && item.is('element') && (item.name === 'image' || item.name === 'imageBlock' || item.name === 'imageInline')) {
+						images.push(item);
+					}
+				}
+				if (!images.length) {
+					return;
+				}
+				var contentWidth = 900;
+				if (editor.ui && editor.ui.view && editor.ui.view.editable) {
+					contentWidth = editor.ui.view.editable.element.clientWidth || contentWidth;
+				}
+				editor.model.change(function (writer) {
+					for (var i = 0; i < images.length && i < sizes.length; i++) {
+						var width = sizes[i].width;
+						if (!width) {
+							continue;
+						}
+						width = Math.max(1, Math.min(Math.round(width), contentWidth));
+						var widthValue = width + 'px';
+						writer.setAttribute('resizedWidth', widthValue, images[i]);
+						writer.setAttribute('width', widthValue, images[i]);
+					}
+				});
+			},
 			applyDefaultEditorTypography: function (editor) {
 				if (!editor || !editor.model || !editor.model.document) {
 					return;

+ 88 - 0
local-run/reports/word-image-import-size-20261116/README.md

@@ -0,0 +1,88 @@
+# Word 导入图片被放大修复
+
+日期:2026-11-16
+
+## 根因
+
+LibreOffice 将示例 DOCX 转成 HTML 时,已经正确保留了 Word 中图片的显示尺寸:
+
+- `width="252" height="215"`
+- `width="255" height="216"`
+- `width="261" height="214"`
+- `width="256" height="213"`
+
+但 CKEditor 5 的数据模型默认会丢弃 `<img>` 上的 `width`、`height` 和内联尺寸样式。随后 `space.editor.css` 又对所有编辑器图片强制:
+
+```css
+width: 100% !important;
+```
+
+因此原始图片虽然自然宽度约 578–597px,Word 中只显示为 252–261px,导入编辑器后却会被拉满正文区域。
+
+## 修复
+
+### 导入链路
+
+`space.vue.subassembly.js` 在 Word HTML 插入前读取每张图片的 Word 显示宽度,支持 `px/pt/in/cm/mm/%`,插入 CKEditor 后写入其原生图片尺寸属性:
+
+- `resizedWidth`
+- `width`
+
+宽度同时受编辑器内容区最大宽度限制。
+
+### CSS
+
+`space.editor.css` 不再强制图片容器 `width: 100% !important`,改为:
+
+```css
+width: auto;
+max-width: 100%;
+```
+
+原始图片宽度仍由 CKEditor 的图片尺寸属性控制;无明确尺寸的上传图片仍可正常自适应,过宽图片不会溢出正文。
+
+## 实际 Word 验证
+
+使用文件:
+
+`/Volumes/FFKJ/SS/项目/广州华夏职业学院/华夏学院升级V2/文章/深耕课堂展风采,以研促教共成长 —— 信息工程学院李志财老师开展《数据库技术与应用》公开课教研活动.docx`
+
+后台“新增文章”实测,不保存文章:
+
+| 图片 | 原图自然宽度 | 编辑器显示宽度 | 高度 |
+| --- | ---: | ---: | ---: |
+| 1 | 578px | 252px | 215px |
+| 2 | 584px | 255px | 216px |
+| 3 | 597px | 261px | 214px |
+| 4 | 587px | 256px | 213px |
+
+结果:
+
+- 4/4 图片宽度与 Word 原稿一致
+- 4/4 图片正常加载
+- 4/4 图片保持居中
+- 没有保存或发布测试文章
+- 测试账号已退出
+
+## 发布与校验
+
+修改文件:
+
+- `WebRoot/apps/space/js/space.vue.subassembly.js`
+- `WebRoot/apps/space/css/space.editor.css`
+
+发布后 SHA-256:
+
+- JS:`d36bbf1fef3b28c3079a44397cfd41dfc30abedcd9fe1d833b9b62f3c585d924`
+- CSS:`70578677977ecf80875c281d3154af53f0aefe90b3622d4db9fcf7ba47609e11`
+
+内网 `10.0.42.6`、外网 `10.0.42.5` 文件哈希一致。
+
+备份:
+
+- `/home/hxxy/zq/backups/word-import-image-size-20261116/internal/`
+- `/home/hxxy/zq/backups/word-import-image-size-20261116/external/`
+
+语法检查:
+
+- `node --check /tmp/staged-space-vue.js`:通过