|
|
@@ -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;
|