|
@@ -414,7 +414,7 @@ Vue.component('spaceEditor', {
|
|
|
* type:编辑器工具栏类型
|
|
* type:编辑器工具栏类型
|
|
|
* isfullscreen:是否有全屏,
|
|
* isfullscreen:是否有全屏,
|
|
|
*/
|
|
*/
|
|
|
- props: ['htmlContent', 'size', 'width', 'height', 'placeholder', 'type', 'isfullscreen', 'articlepreview', 'previewdata', 'previewurl'],
|
|
|
|
|
|
|
+ props: ['htmlContent', 'size', 'width', 'height', 'placeholder', 'type', 'isfullscreen', 'articlepreview', 'previewdata', 'previewurl', 'wordconverturl'],
|
|
|
computed: {
|
|
computed: {
|
|
|
previewFrameStyle: function () {
|
|
previewFrameStyle: function () {
|
|
|
var widthMap = {
|
|
var widthMap = {
|
|
@@ -523,6 +523,30 @@ Vue.component('spaceEditor', {
|
|
|
}
|
|
}
|
|
|
return this.normalizeLocalUploadUrl(url);
|
|
return this.normalizeLocalUploadUrl(url);
|
|
|
},
|
|
},
|
|
|
|
|
+ getEditorEditableElement: function (editor) {
|
|
|
|
|
+ if (!editor) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (editor.ui && editor.ui.view && editor.ui.view.editable && editor.ui.view.editable.element) {
|
|
|
|
|
+ return editor.ui.view.editable.element;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (editor.editing && editor.editing.view && typeof editor.editing.view.getDomRoot === 'function') {
|
|
|
|
|
+ var domRoot = editor.editing.view.getDomRoot();
|
|
|
|
|
+ if (domRoot) {
|
|
|
|
|
+ return domRoot;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (editor.ui && typeof editor.ui.getEditableElement === 'function') {
|
|
|
|
|
+ var editable = editor.ui.getEditableElement();
|
|
|
|
|
+ if (editable) {
|
|
|
|
|
+ return editable;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (editor.ui && editor.ui.view && editor.ui.view.element) {
|
|
|
|
|
+ return editor.ui.view.element.querySelector('.ck-editor__editable_inline');
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ },
|
|
|
normalizeVideoHtmlForEditor: function (html) {
|
|
normalizeVideoHtmlForEditor: function (html) {
|
|
|
if (!html || html.indexOf("<video") === -1 || html.indexOf("<source") === -1) {
|
|
if (!html || html.indexOf("<video") === -1 || html.indexOf("<source") === -1) {
|
|
|
return html || "";
|
|
return html || "";
|
|
@@ -700,9 +724,7 @@ Vue.component('spaceEditor', {
|
|
|
}.bind(this);
|
|
}.bind(this);
|
|
|
|
|
|
|
|
var editorImages = [];
|
|
var editorImages = [];
|
|
|
- var editable = this.editor.ui && this.editor.ui.getEditableElement
|
|
|
|
|
- ? this.editor.ui.getEditableElement()
|
|
|
|
|
- : null;
|
|
|
|
|
|
|
+ var editable = this.getEditorEditableElement(this.editor);
|
|
|
if (editable) {
|
|
if (editable) {
|
|
|
var domImages = editable.querySelectorAll("img");
|
|
var domImages = editable.querySelectorAll("img");
|
|
|
for (var i = 0; i < domImages.length; i++) {
|
|
for (var i = 0; i < domImages.length; i++) {
|
|
@@ -1008,7 +1030,7 @@ Vue.component('spaceEditor', {
|
|
|
data.preventDefault();
|
|
data.preventDefault();
|
|
|
}
|
|
}
|
|
|
}, { priority: "high" });
|
|
}, { priority: "high" });
|
|
|
- var editable = editor.ui.getEditableElement();
|
|
|
|
|
|
|
+ var editable = _this.getEditorEditableElement(editor);
|
|
|
if (editable) {
|
|
if (editable) {
|
|
|
editable.addEventListener("keydown", function (event) {
|
|
editable.addEventListener("keydown", function (event) {
|
|
|
if (event.keyCode !== 8 && event.keyCode !== 46) {
|
|
if (event.keyCode !== 8 && event.keyCode !== 46) {
|
|
@@ -1674,6 +1696,145 @@ Vue.component('spaceEditor', {
|
|
|
|
|
|
|
|
return toolbar;
|
|
return toolbar;
|
|
|
},
|
|
},
|
|
|
|
|
+ installFirstLineIndentPlugin: function (editor) {
|
|
|
|
|
+ if (!editor.model.schema.checkAttribute('$block', 'textIndent')) {
|
|
|
|
|
+ editor.model.schema.extend('$block', { allowAttributes: ['textIndent'] });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ editor.conversion.attributeToAttribute({
|
|
|
|
|
+ model: { key: 'textIndent', value: '2em' },
|
|
|
|
|
+ view: { key: 'style', value: { 'text-indent': '2em' } }
|
|
|
|
|
+ });
|
|
|
|
|
+ },
|
|
|
|
|
+ getSelectedTextBlocks: function (editor) {
|
|
|
|
|
+ var blocks = [];
|
|
|
|
|
+ var selection = editor.model.document.selection;
|
|
|
|
|
+ if (!selection.isCollapsed && typeof selection.getSelectedBlocks === 'function') {
|
|
|
|
|
+ blocks = Array.prototype.slice.call(selection.getSelectedBlocks());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ var node = selection.getFirstPosition().parent;
|
|
|
|
|
+ while (node && (!node.is || !node.is('element') || !editor.model.schema.isBlock(node.name))) {
|
|
|
|
|
+ node = node.parent;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (node && node.is('element')) {
|
|
|
|
|
+ blocks.push(node);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return blocks.filter(function (block) {
|
|
|
|
|
+ return block && editor.model.schema.checkAttribute(block.name, 'textIndent');
|
|
|
|
|
+ });
|
|
|
|
|
+ },
|
|
|
|
|
+ toggleFirstLineIndent: function (editor) {
|
|
|
|
|
+ var blocks = this.getSelectedTextBlocks(editor);
|
|
|
|
|
+ if (!blocks.length) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var removeIndent = blocks.every(function (block) {
|
|
|
|
|
+ return block.getAttribute('textIndent') === '2em';
|
|
|
|
|
+ });
|
|
|
|
|
+ editor.model.change(function (writer) {
|
|
|
|
|
+ blocks.forEach(function (block) {
|
|
|
|
|
+ if (removeIndent) {
|
|
|
|
|
+ writer.removeAttribute('textIndent', block);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ writer.setAttribute('textIndent', '2em', block);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ editor.editing.view.focus();
|
|
|
|
|
+ },
|
|
|
|
|
+ refreshFirstLineIndentButton: function (editor) {
|
|
|
|
|
+ if (!editor || !editor.ui || !editor.ui.view || !editor.ui.view.element) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var button = editor.ui.view.element.querySelector('[data-space-first-line-indent="true"]');
|
|
|
|
|
+ if (!button) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var blocks = this.getSelectedTextBlocks(editor);
|
|
|
|
|
+ var active = blocks.length > 0 && blocks.every(function (block) {
|
|
|
|
|
+ return block.getAttribute('textIndent') === '2em';
|
|
|
|
|
+ });
|
|
|
|
|
+ button.classList.toggle('ck-on', active);
|
|
|
|
|
+ button.classList.toggle('ck-off', !active);
|
|
|
|
|
+ button.setAttribute('aria-pressed', active ? 'true' : 'false');
|
|
|
|
|
+ },
|
|
|
|
|
+ mountFirstLineIndentToolbarButton: 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-first-line-indent="true"]')) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var buttons = toolbarItems.querySelectorAll('.ck-button');
|
|
|
|
|
+ var templateButton = null;
|
|
|
|
|
+ var outdentButton = null;
|
|
|
|
|
+ for (var i = 0; i < buttons.length; i++) {
|
|
|
|
|
+ var label = buttons[i].getAttribute('aria-label') || buttons[i].textContent || '';
|
|
|
|
|
+ if (!templateButton && buttons[i].querySelector('svg.ck-button__icon')) {
|
|
|
|
|
+ templateButton = buttons[i];
|
|
|
|
|
+ }
|
|
|
|
|
+ if (label.indexOf('减少缩进') !== -1 || label.indexOf('减少縮排') !== -1) {
|
|
|
|
|
+ outdentButton = buttons[i];
|
|
|
|
|
+ if (!templateButton) {
|
|
|
|
|
+ templateButton = buttons[i];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!templateButton) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var button = templateButton.cloneNode(true);
|
|
|
|
|
+ button.removeAttribute('aria-disabled');
|
|
|
|
|
+ button.removeAttribute('aria-labelledby');
|
|
|
|
|
+ button.removeAttribute('aria-haspopup');
|
|
|
|
|
+ button.setAttribute('aria-label', '首行缩进两字符');
|
|
|
|
|
+ button.setAttribute('aria-pressed', 'false');
|
|
|
|
|
+ button.setAttribute('title', '首行缩进两字符');
|
|
|
|
|
+ button.setAttribute('data-space-first-line-indent', 'true');
|
|
|
|
|
+ button.classList.remove('ck-disabled', 'ck-on');
|
|
|
|
|
+ button.classList.add('ck-off');
|
|
|
|
|
+ button.tabIndex = 0;
|
|
|
|
|
+
|
|
|
|
|
+ var tooltipText = button.querySelector('.ck-tooltip__text');
|
|
|
|
|
+ if (tooltipText) {
|
|
|
|
|
+ tooltipText.textContent = '首行缩进两字符';
|
|
|
|
|
+ }
|
|
|
|
|
+ var buttonLabel = button.querySelector('.ck-button__label');
|
|
|
|
|
+ if (buttonLabel) {
|
|
|
|
|
+ buttonLabel.textContent = '首行缩进两字符';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var _this = this;
|
|
|
|
|
+ button.addEventListener('click', function (event) {
|
|
|
|
|
+ event.preventDefault();
|
|
|
|
|
+ event.stopPropagation();
|
|
|
|
|
+ _this.toggleFirstLineIndent(editor);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ var referenceButton = outdentButton || templateButton;
|
|
|
|
|
+ if (referenceButton && referenceButton.parentNode === toolbarItems) {
|
|
|
|
|
+ toolbarItems.insertBefore(button, referenceButton.nextSibling);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ toolbarItems.appendChild(button);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ editor.model.document.selection.on('change', function () {
|
|
|
|
|
+ _this.refreshFirstLineIndentButton(editor);
|
|
|
|
|
+ });
|
|
|
|
|
+ editor.model.document.on('change:data', function () {
|
|
|
|
|
+ _this.refreshFirstLineIndentButton(editor);
|
|
|
|
|
+ });
|
|
|
|
|
+ this.refreshFirstLineIndentButton(editor);
|
|
|
|
|
+ },
|
|
|
alignFullScreenToolbarButton: function () {
|
|
alignFullScreenToolbarButton: function () {
|
|
|
if (!this.editor || !this.editor.ui || !this.editor.ui.view || !this.editor.ui.view.element) {
|
|
if (!this.editor || !this.editor.ui || !this.editor.ui.view || !this.editor.ui.view.element) {
|
|
|
return;
|
|
return;
|
|
@@ -2111,6 +2272,259 @@ Vue.component('spaceEditor', {
|
|
|
this.deferSyncEditorPlayableVideos();
|
|
this.deferSyncEditorPlayableVideos();
|
|
|
space.message.success("正文已完成排版整理");
|
|
space.message.success("正文已完成排版整理");
|
|
|
},
|
|
},
|
|
|
|
|
+ configureWordUploadPlugin: function (editor) {
|
|
|
|
|
+ if (!editor || !editor.plugins) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var wordUi = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ wordUi = editor.plugins.get('WordUploadUi');
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ for (var entry of editor.plugins) {
|
|
|
|
|
+ var plugin = Array.isArray(entry) ? entry[1] : entry;
|
|
|
|
|
+ if (plugin && typeof plugin.importWordFile === 'function') {
|
|
|
|
|
+ wordUi = plugin;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!wordUi || wordUi.spaceFormatOverride) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var _this = this;
|
|
|
|
|
+ wordUi.spaceFormatOverride = true;
|
|
|
|
|
+ wordUi.importWordFile = function (targetEditor, file) {
|
|
|
|
|
+ return _this.importFormattedWordFile(targetEditor, file, wordUi);
|
|
|
|
|
+ };
|
|
|
|
|
+ },
|
|
|
|
|
+ importFormattedWordFile: function (editor, file, wordUi) {
|
|
|
|
|
+ if (!file) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var fileName = (file.name || '').toLowerCase();
|
|
|
|
|
+ if (!/\.(doc|docx)$/.test(fileName)) {
|
|
|
|
|
+ wordUi.showMessage('error', '请选择 .doc 或 .docx 格式的 Word 文件。');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ wordUi.showLoading();
|
|
|
|
|
+ this.uploadStatus = '正在导入 Word 文档…';
|
|
|
|
|
+ return this.convertWordWithServer(file)
|
|
|
|
|
+ .catch(function (error) {
|
|
|
|
|
+ if (!/\.docx$/.test(fileName)) {
|
|
|
|
|
+ throw new Error('旧版 .doc 转换失败,请在 Word 中另存为 .docx 后再导入。');
|
|
|
|
|
+ }
|
|
|
|
|
+ console.warn('LibreOffice Word conversion unavailable, fallback to mammoth:', error);
|
|
|
|
|
+ return this.convertWordWithMammoth(file, wordUi);
|
|
|
|
|
+ }.bind(this))
|
|
|
|
|
+ .then(function (result) {
|
|
|
|
|
+ var htmlContent = this.normalizeImportedWordHtml(wordUi, result.html);
|
|
|
|
|
+ if (!htmlContent) {
|
|
|
|
|
+ throw new Error('没有读取到可导入的正文内容。');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ this.insertFormattedWordHtml(editor, htmlContent);
|
|
|
|
|
+ if (result.messages && result.messages.length) {
|
|
|
|
|
+ wordUi.showMessage('warning', 'Word 已导入,少量复杂格式可能需要人工检查。');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ wordUi.showMessage('success', 'Word 文件已导入正文,并尽量保留原文档格式。');
|
|
|
|
|
+ }
|
|
|
|
|
+ }.bind(this))
|
|
|
|
|
+ .catch(function (error) {
|
|
|
|
|
+ console.error(error);
|
|
|
|
|
+ wordUi.showMessage('error', error && error.message ? error.message : 'Word 文件导入失败,请确认文件未损坏。');
|
|
|
|
|
+ })
|
|
|
|
|
+ .finally(function () {
|
|
|
|
|
+ this.uploadStatus = '';
|
|
|
|
|
+ wordUi.closeLoading();
|
|
|
|
|
+ }.bind(this));
|
|
|
|
|
+ },
|
|
|
|
|
+ convertWordWithMammoth: function (file, wordUi) {
|
|
|
|
|
+ return wordUi.loadMammoth()
|
|
|
|
|
+ .then(function () { return wordUi.readFileAsArrayBuffer(file); })
|
|
|
|
|
+ .then(function (arrayBuffer) {
|
|
|
|
|
+ return window.mammoth.convertToHtml({ arrayBuffer: arrayBuffer }, {
|
|
|
|
|
+ convertImage: window.mammoth.images.imgElement(function (image) {
|
|
|
|
|
+ return image.read('base64').then(function (imageBuffer) {
|
|
|
|
|
+ return { src: 'data:' + image.contentType + ';base64,' + imageBuffer };
|
|
|
|
|
+ });
|
|
|
|
|
+ }),
|
|
|
|
|
+ styleMap: [
|
|
|
|
|
+ "p[style-name='Title'] => h1:fresh",
|
|
|
|
|
+ "p[style-name='Subtitle'] => p.subtitle:fresh",
|
|
|
|
|
+ "p[style-name='标题'] => h2:fresh",
|
|
|
|
|
+ "p[style-name='标题 1'] => h2:fresh",
|
|
|
|
|
+ "p[style-name='标题 2'] => h3:fresh",
|
|
|
|
|
+ "p[style-name='标题 3'] => h4:fresh",
|
|
|
|
|
+ "p[style-name='标题 4'] => h5:fresh",
|
|
|
|
|
+ "p[style-name='Heading 1'] => h2:fresh",
|
|
|
|
|
+ "p[style-name='Heading 2'] => h3:fresh",
|
|
|
|
|
+ "p[style-name='Heading 3'] => h4:fresh",
|
|
|
|
|
+ "p[style-name='Heading 4'] => h5:fresh",
|
|
|
|
|
+ "p[style-name='正文'] => p:fresh",
|
|
|
|
|
+ "p[style-name='Normal'] => p:fresh",
|
|
|
|
|
+ "p[style-name='Body Text'] => p:fresh",
|
|
|
|
|
+ "p[style-name='Quote'] => blockquote:fresh",
|
|
|
|
|
+ "p[style-name='引用'] => blockquote:fresh",
|
|
|
|
|
+ "r[style-name='Strong'] => strong",
|
|
|
|
|
+ "r[style-name='Emphasis'] => em",
|
|
|
|
|
+ "r[style-name='加粗'] => strong",
|
|
|
|
|
+ "r[style-name='倾斜'] => em"
|
|
|
|
|
+ ]
|
|
|
|
|
+ });
|
|
|
|
|
+ })
|
|
|
|
|
+ .then(function (conversion) {
|
|
|
|
|
+ return { html: conversion.value, messages: conversion.messages || [] };
|
|
|
|
|
+ });
|
|
|
|
|
+ },
|
|
|
|
|
+ normalizeImportedWordHtml: function (wordUi, html) {
|
|
|
|
|
+ var normalized = wordUi.normalizeWordHtml(html || '');
|
|
|
|
|
+ if (!normalized) {
|
|
|
|
|
+ return '';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var wrapper = document.createElement('div');
|
|
|
|
|
+ wrapper.innerHTML = normalized;
|
|
|
|
|
+ wrapper.style.position = 'absolute';
|
|
|
|
|
+ wrapper.style.left = '-99999px';
|
|
|
|
|
+ wrapper.style.top = '0';
|
|
|
|
|
+ wrapper.style.visibility = 'hidden';
|
|
|
|
|
+ document.body.appendChild(wrapper);
|
|
|
|
|
+ var nodes = wrapper.querySelectorAll('p, div, blockquote, table, td, th, span, font');
|
|
|
|
|
+ for (var i = 0; i < nodes.length; i++) {
|
|
|
|
|
+ var node = nodes[i];
|
|
|
|
|
+ var styleText = node.getAttribute('style') || '';
|
|
|
|
|
+ var keptStyles = styleText.split(';').filter(function (rule) {
|
|
|
|
|
+ return rule.trim() && !/^\s*mso-/i.test(rule);
|
|
|
|
|
+ }).join(';');
|
|
|
|
|
+ if (keptStyles) {
|
|
|
|
|
+ node.setAttribute('style', keptStyles);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ node.removeAttribute('style');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var textIndent = node.style && node.style.textIndent;
|
|
|
|
|
+ if (textIndent && !/^0(?:px|pt|em|%)?$/i.test(textIndent.trim())) {
|
|
|
|
|
+ node.style.textIndent = '2em';
|
|
|
|
|
+ }
|
|
|
|
|
+ if (node.getAttribute && node.getAttribute('align')) {
|
|
|
|
|
+ node.style.textAlign = node.getAttribute('align');
|
|
|
|
|
+ node.removeAttribute('align');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var walker = document.createTreeWalker(wrapper, NodeFilter.SHOW_TEXT, {
|
|
|
|
|
+ acceptNode: function (textNode) {
|
|
|
|
|
+ return textNode.nodeValue && textNode.nodeValue.trim()
|
|
|
|
|
+ ? NodeFilter.FILTER_ACCEPT
|
|
|
|
|
+ : NodeFilter.FILTER_REJECT;
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ var textNodes = [];
|
|
|
|
|
+ while (walker.nextNode()) {
|
|
|
|
|
+ textNodes.push(walker.currentNode);
|
|
|
|
|
+ }
|
|
|
|
|
+ textNodes.forEach(function (textNode) {
|
|
|
|
|
+ var parent = textNode.parentElement;
|
|
|
|
|
+ if (!parent || !window.getComputedStyle) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ var computed = window.getComputedStyle(parent);
|
|
|
|
|
+ var formattedSpan = document.createElement('span');
|
|
|
|
|
+ formattedSpan.setAttribute('data-word-format', 'true');
|
|
|
|
|
+ formattedSpan.style.fontFamily = computed.fontFamily;
|
|
|
|
|
+ formattedSpan.style.fontSize = computed.fontSize;
|
|
|
|
|
+ formattedSpan.style.color = computed.color;
|
|
|
|
|
+ if (computed.fontWeight && computed.fontWeight !== '400' && computed.fontWeight !== 'normal') {
|
|
|
|
|
+ formattedSpan.style.fontWeight = computed.fontWeight;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (computed.fontStyle && computed.fontStyle !== 'normal') {
|
|
|
|
|
+ formattedSpan.style.fontStyle = computed.fontStyle;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (computed.textDecorationLine && computed.textDecorationLine !== 'none') {
|
|
|
|
|
+ formattedSpan.style.textDecoration = computed.textDecorationLine;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (computed.verticalAlign && computed.verticalAlign !== 'baseline') {
|
|
|
|
|
+ formattedSpan.style.verticalAlign = computed.verticalAlign;
|
|
|
|
|
+ }
|
|
|
|
|
+ formattedSpan.appendChild(textNode.cloneNode(true));
|
|
|
|
|
+ textNode.parentNode.replaceChild(formattedSpan, textNode);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ var inlineNodes = wrapper.querySelectorAll('span, font, b, strong, i, em, u, s, strike, sub, sup');
|
|
|
|
|
+ for (var inlineIndex = inlineNodes.length - 1; inlineIndex >= 0; inlineIndex--) {
|
|
|
|
|
+ var inlineNode = inlineNodes[inlineIndex];
|
|
|
|
|
+ if (inlineNode.getAttribute('data-word-format') === 'true' || !inlineNode.parentNode) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ while (inlineNode.firstChild) {
|
|
|
|
|
+ inlineNode.parentNode.insertBefore(inlineNode.firstChild, inlineNode);
|
|
|
|
|
+ }
|
|
|
|
|
+ inlineNode.parentNode.removeChild(inlineNode);
|
|
|
|
|
+ }
|
|
|
|
|
+ var normalizedHtml = wrapper.innerHTML;
|
|
|
|
|
+ document.body.removeChild(wrapper);
|
|
|
|
|
+ return normalizedHtml;
|
|
|
|
|
+ },
|
|
|
|
|
+ insertFormattedWordHtml: function (editor, htmlContent) {
|
|
|
|
|
+ this.insertHtml(htmlContent);
|
|
|
|
|
+ this.$emit('input', this.getEditorSaveHtml());
|
|
|
|
|
+ this.deferSyncEditorPlayableVideos();
|
|
|
|
|
+ },
|
|
|
|
|
+ convertWordWithServer: function (file) {
|
|
|
|
|
+ var formData = new FormData();
|
|
|
|
|
+ var convertUrl = this.wordconverturl;
|
|
|
|
|
+ formData.append('upload', file);
|
|
|
|
|
+
|
|
|
|
|
+ return fetch('/apps?ark=sys.uploadAjax', {
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ body: formData,
|
|
|
|
|
+ credentials: 'same-origin'
|
|
|
|
|
+ }).then(readJsonResponse).then(function (uploadResult) {
|
|
|
|
|
+ var uploadData = uploadResult && (uploadResult.data || uploadResult);
|
|
|
|
|
+ var path = uploadResult && uploadResult.data && uploadResult.data[0] && uploadResult.data[0].msg;
|
|
|
|
|
+ path = path || (uploadData && (uploadData.msg || uploadData.path));
|
|
|
|
|
+ if (!path) {
|
|
|
|
|
+ throw new Error('Word 文件上传失败。');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return fetch(convertUrl, {
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
|
|
|
|
|
+ body: 'path=' + encodeURIComponent(path),
|
|
|
|
|
+ credentials: 'same-origin'
|
|
|
|
|
+ });
|
|
|
|
|
+ }).then(readJsonResponse).then(function (conversionResult) {
|
|
|
|
|
+ var data = conversionResult && (conversionResult.data || conversionResult);
|
|
|
|
|
+ var html = conversionResult && conversionResult.htmlContent;
|
|
|
|
|
+ html = html || (data && data.htmlContent);
|
|
|
|
|
+ html = html || (conversionResult && conversionResult.data && conversionResult.data[0] && conversionResult.data[0].htmlContent);
|
|
|
|
|
+ if (!html) {
|
|
|
|
|
+ throw new Error('服务器没有返回可导入的 Word 内容。');
|
|
|
|
|
+ }
|
|
|
|
|
+ return { html: html, messages: [] };
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ function readJsonResponse(response) {
|
|
|
|
|
+ if (!response || !response.ok) {
|
|
|
|
|
+ throw new Error('Word 转换服务不可用。');
|
|
|
|
|
+ }
|
|
|
|
|
+ return response.text().then(function (text) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ var json = JSON.parse(text);
|
|
|
|
|
+ if (json && json.code && json.code !== 'success') {
|
|
|
|
|
+ throw new Error(json.msg || 'Word 转换失败。');
|
|
|
|
|
+ }
|
|
|
|
|
+ return json;
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ throw new Error(error && error.message ? error.message : 'Word 转换服务返回异常。');
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
openWordImporter: function () {
|
|
openWordImporter: function () {
|
|
|
if (!this.editor || !this.editor.ui || !this.editor.ui.view || !this.editor.ui.view.element) {
|
|
if (!this.editor || !this.editor.ui || !this.editor.ui.view || !this.editor.ui.view.element) {
|
|
|
space.message.warning("编辑器尚未加载完成");
|
|
space.message.warning("编辑器尚未加载完成");
|
|
@@ -2406,42 +2820,165 @@ Vue.component('spaceEditor', {
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
bindVideoPasteUpload: function () {
|
|
bindVideoPasteUpload: function () {
|
|
|
- if (!this.editor || !this.editor.ui || !this.editor.ui.getEditableElement) {
|
|
|
|
|
|
|
+ if (!this.editor) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var editable = this.editor.ui.getEditableElement();
|
|
|
|
|
|
|
+ var editable = this.getEditorEditableElement(this.editor);
|
|
|
if (!editable || editable.getAttribute("data-space-video-upload") == "true") {
|
|
if (!editable || editable.getAttribute("data-space-video-upload") == "true") {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- editable.setAttribute("data-space-video-upload", "true");
|
|
|
|
|
|
|
+ editable.setAttribute("data-space-video-upload", "true");
|
|
|
var _this = this;
|
|
var _this = this;
|
|
|
var handler = function (event) {
|
|
var handler = function (event) {
|
|
|
var dataTransfer = event.clipboardData || event.dataTransfer;
|
|
var dataTransfer = event.clipboardData || event.dataTransfer;
|
|
|
- if (!dataTransfer || !dataTransfer.files || dataTransfer.files.length == 0) {
|
|
|
|
|
|
|
+ var files = _this.getClipboardFiles(dataTransfer);
|
|
|
|
|
+ if (!files.length) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var files = Array.prototype.slice.call(dataTransfer.files);
|
|
|
|
|
var videoFile = files.find(function (file) {
|
|
var videoFile = files.find(function (file) {
|
|
|
return file && file.type && file.type.indexOf("video/") === 0;
|
|
return file && file.type && file.type.indexOf("video/") === 0;
|
|
|
});
|
|
});
|
|
|
var pdfFile = files.find(function (file) {
|
|
var pdfFile = files.find(function (file) {
|
|
|
return _this.isPdfFile(file);
|
|
return _this.isPdfFile(file);
|
|
|
});
|
|
});
|
|
|
|
|
+ var imageFiles = files.filter(function (file) {
|
|
|
|
|
+ return _this.isImageFile(file);
|
|
|
|
|
+ });
|
|
|
|
|
+ var htmlContent = _this.readClipboardData(dataTransfer, 'text/html');
|
|
|
|
|
+ var plainText = _this.readClipboardData(dataTransfer, 'text/plain');
|
|
|
|
|
+ var hasTextContent = !!(htmlContent.replace(/\s+/g, '') || plainText.trim());
|
|
|
|
|
|
|
|
if (videoFile) {
|
|
if (videoFile) {
|
|
|
event.preventDefault();
|
|
event.preventDefault();
|
|
|
|
|
+ if (event.stopImmediatePropagation) {
|
|
|
|
|
+ event.stopImmediatePropagation();
|
|
|
|
|
+ }
|
|
|
_this.uploadVideoFile(videoFile);
|
|
_this.uploadVideoFile(videoFile);
|
|
|
} else if (pdfFile) {
|
|
} else if (pdfFile) {
|
|
|
event.preventDefault();
|
|
event.preventDefault();
|
|
|
|
|
+ if (event.stopImmediatePropagation) {
|
|
|
|
|
+ event.stopImmediatePropagation();
|
|
|
|
|
+ }
|
|
|
_this.uploadPdfFile(pdfFile);
|
|
_this.uploadPdfFile(pdfFile);
|
|
|
|
|
+ } else if (imageFiles.length && hasTextContent) {
|
|
|
|
|
+ event.preventDefault();
|
|
|
|
|
+ if (event.stopImmediatePropagation) {
|
|
|
|
|
+ event.stopImmediatePropagation();
|
|
|
|
|
+ }
|
|
|
|
|
+ _this.insertMixedClipboardContent(htmlContent, plainText, imageFiles);
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- editable.addEventListener("paste", handler);
|
|
|
|
|
- editable.addEventListener("drop", handler);
|
|
|
|
|
|
|
+ editable.addEventListener("paste", handler, true);
|
|
|
|
|
+ editable.addEventListener("drop", handler, true);
|
|
|
|
|
+ },
|
|
|
|
|
+ getClipboardFiles: function (dataTransfer) {
|
|
|
|
|
+ if (!dataTransfer) {
|
|
|
|
|
+ return [];
|
|
|
|
|
+ }
|
|
|
|
|
+ var files = dataTransfer.files ? Array.prototype.slice.call(dataTransfer.files) : [];
|
|
|
|
|
+ if (files.length || !dataTransfer.items) {
|
|
|
|
|
+ return files;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (var i = 0; i < dataTransfer.items.length; i++) {
|
|
|
|
|
+ var item = dataTransfer.items[i];
|
|
|
|
|
+ if (item && item.kind === 'file' && typeof item.getAsFile === 'function') {
|
|
|
|
|
+ var file = item.getAsFile();
|
|
|
|
|
+ if (file) {
|
|
|
|
|
+ files.push(file);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return files;
|
|
|
|
|
+ },
|
|
|
|
|
+ isImageFile: function (file) {
|
|
|
|
|
+ return !!(file && ((file.type && file.type.indexOf('image/') === 0)
|
|
|
|
|
+ || /\.(jpg|jpeg|png|gif|bmp|webp|svg)$/i.test(file.name || '')));
|
|
|
|
|
+ },
|
|
|
|
|
+ readClipboardData: function (dataTransfer, type) {
|
|
|
|
|
+ if (!dataTransfer || typeof dataTransfer.getData !== 'function') {
|
|
|
|
|
+ return '';
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ return dataTransfer.getData(type) || '';
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ return '';
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ insertMixedClipboardContent: function (htmlContent, plainText, imageFiles) {
|
|
|
|
|
+ if (!this.editor || !imageFiles || !imageFiles.length) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var _this = this;
|
|
|
|
|
+ this.uploadStatus = '正在上传粘贴内容中的图片…';
|
|
|
|
|
+ if (window.space && space.plugins && space.plugins.loading) {
|
|
|
|
|
+ space.plugins.loading.show();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Promise.all(imageFiles.map(function (file) {
|
|
|
|
|
+ return _this.uploadPlainFileByAdapter(file, _this.getUploadOptions()).then(function (result) {
|
|
|
|
|
+ return result && result.default ? result.default : '';
|
|
|
|
|
+ });
|
|
|
|
|
+ })).then(function (imageUrls) {
|
|
|
|
|
+ var html = _this.buildMixedClipboardHtml(htmlContent, plainText, imageUrls, imageFiles);
|
|
|
|
|
+ if (!html) {
|
|
|
|
|
+ throw new Error('粘贴内容为空');
|
|
|
|
|
+ }
|
|
|
|
|
+ _this.insertHtml(html);
|
|
|
|
|
+ _this.$emit('input', _this.getEditorSaveHtml());
|
|
|
|
|
+ _this.deferSyncEditorPlayableVideos();
|
|
|
|
|
+ space.message.success('文字和图片已同时粘贴');
|
|
|
|
|
+ }).catch(function (error) {
|
|
|
|
|
+ console.error(error);
|
|
|
|
|
+ space.message.error(error && error.message ? error.message : '粘贴图片上传失败,请重试');
|
|
|
|
|
+ }).finally(function () {
|
|
|
|
|
+ _this.uploadStatus = '';
|
|
|
|
|
+ if (window.space && space.plugins && space.plugins.loading) {
|
|
|
|
|
+ space.plugins.loading.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ },
|
|
|
|
|
+ buildMixedClipboardHtml: function (htmlContent, plainText, imageUrls, imageFiles) {
|
|
|
|
|
+ var safeUrls = imageUrls || [];
|
|
|
|
|
+ var wrapper = document.createElement('div');
|
|
|
|
|
+ if (htmlContent) {
|
|
|
|
|
+ wrapper.innerHTML = htmlContent;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ var lines = (plainText || '').replace(/\r\n/g, '\n').split('\n');
|
|
|
|
|
+ wrapper.innerHTML = lines.map(function (line) {
|
|
|
|
|
+ return '<p>' + this.escapeClipboardText(line) + '</p>';
|
|
|
|
|
+ }.bind(this)).join('');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var images = wrapper.querySelectorAll('img');
|
|
|
|
|
+ for (var i = 0; i < images.length && i < safeUrls.length; i++) {
|
|
|
|
|
+ if (safeUrls[i]) {
|
|
|
|
|
+ images[i].setAttribute('src', safeUrls[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (var imageIndex = images.length; imageIndex < safeUrls.length; imageIndex++) {
|
|
|
|
|
+ if (!safeUrls[imageIndex]) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ var figure = document.createElement('figure');
|
|
|
|
|
+ figure.className = 'image';
|
|
|
|
|
+ var image = document.createElement('img');
|
|
|
|
|
+ image.setAttribute('src', safeUrls[imageIndex]);
|
|
|
|
|
+ image.setAttribute('alt', imageFiles[imageIndex] && imageFiles[imageIndex].name ? imageFiles[imageIndex].name : '粘贴图片');
|
|
|
|
|
+ figure.appendChild(image);
|
|
|
|
|
+ wrapper.appendChild(figure);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return wrapper.innerHTML;
|
|
|
|
|
+ },
|
|
|
|
|
+ escapeClipboardText: function (text) {
|
|
|
|
|
+ return String(text || '').replace(/&/g, '&').replace(/</g, '<')
|
|
|
|
|
+ .replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
|
|
|
},
|
|
},
|
|
|
isPdfToolbarControl: function (domTarget) {
|
|
isPdfToolbarControl: function (domTarget) {
|
|
|
if (!domTarget || !domTarget.closest) {
|
|
if (!domTarget || !domTarget.closest) {
|
|
@@ -2628,11 +3165,11 @@ Vue.component('spaceEditor', {
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
syncEditorPlayableVideos: function () {
|
|
syncEditorPlayableVideos: function () {
|
|
|
- if (!this.editor || !this.editor.ui || !this.editor.ui.getEditableElement) {
|
|
|
|
|
|
|
+ if (!this.editor) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var editable = this.editor.ui.getEditableElement();
|
|
|
|
|
|
|
+ var editable = this.getEditorEditableElement(this.editor);
|
|
|
if (!editable) {
|
|
if (!editable) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -2654,11 +3191,11 @@ Vue.component('spaceEditor', {
|
|
|
}, 0);
|
|
}, 0);
|
|
|
},
|
|
},
|
|
|
bindEditorVideoPlayback: function () {
|
|
bindEditorVideoPlayback: function () {
|
|
|
- if (!this.editor || !this.editor.ui || !this.editor.ui.getEditableElement) {
|
|
|
|
|
|
|
+ if (!this.editor) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var editable = this.editor.ui.getEditableElement();
|
|
|
|
|
|
|
+ var editable = this.getEditorEditableElement(this.editor);
|
|
|
if (!editable || editable.getAttribute("data-space-playable-media-root") == "true") {
|
|
if (!editable || editable.getAttribute("data-space-playable-media-root") == "true") {
|
|
|
this.syncEditorPlayableVideos();
|
|
this.syncEditorPlayableVideos();
|
|
|
return;
|
|
return;
|
|
@@ -2792,12 +3329,33 @@ Vue.component('spaceEditor', {
|
|
|
ClassicEditor.create(document.querySelector("#" + _this.id), {
|
|
ClassicEditor.create(document.querySelector("#" + _this.id), {
|
|
|
extraPlugins: [function (editor) {
|
|
extraPlugins: [function (editor) {
|
|
|
_this.installUploadAdapter(editor);
|
|
_this.installUploadAdapter(editor);
|
|
|
|
|
+ _this.installFirstLineIndentPlugin(editor);
|
|
|
_this.installVideoViewerPlugin(editor);
|
|
_this.installVideoViewerPlugin(editor);
|
|
|
_this.installPdfViewerPlugin(editor);
|
|
_this.installPdfViewerPlugin(editor);
|
|
|
}], // 添加上传适配器
|
|
}], // 添加上传适配器
|
|
|
toolbar: toolbar || {},
|
|
toolbar: toolbar || {},
|
|
|
language: 'zh-cn',
|
|
language: 'zh-cn',
|
|
|
placeholder: _this.placeholder || '',
|
|
placeholder: _this.placeholder || '',
|
|
|
|
|
+ fontFamily: {
|
|
|
|
|
+ supportAllValues: true,
|
|
|
|
|
+ options: [
|
|
|
|
|
+ 'default',
|
|
|
|
|
+ { title: '宋体', model: 'SimSun, serif', view: { name: 'span', styles: { 'font-family': "'SimSun', serif" }, priority: 7 } },
|
|
|
|
|
+ { title: '仿宋', model: 'FangSong, serif', view: { name: 'span', styles: { 'font-family': "'FangSong', serif" }, priority: 7 } },
|
|
|
|
|
+ { title: '楷体', model: 'KaiTi, serif', view: { name: 'span', styles: { 'font-family': "'KaiTi', serif" }, priority: 7 } },
|
|
|
|
|
+ { title: '黑体', model: 'SimHei, sans-serif', view: { name: 'span', styles: { 'font-family': "'SimHei', sans-serif" }, priority: 7 } },
|
|
|
|
|
+ { title: '微软雅黑', model: 'Microsoft YaHei, sans-serif', view: { name: 'span', styles: { 'font-family': "'Microsoft YaHei', sans-serif" }, priority: 7 } },
|
|
|
|
|
+ { title: '等线', model: 'DengXian, sans-serif', view: { name: 'span', styles: { 'font-family': "'DengXian', sans-serif" }, priority: 7 } },
|
|
|
|
|
+ { title: '思源宋体', model: 'Source Han Serif SC, serif', view: { name: 'span', styles: { 'font-family': "'Source Han Serif SC', serif" }, priority: 7 } },
|
|
|
|
|
+ { title: '思源黑体', model: 'Source Han Sans SC, sans-serif', view: { name: 'span', styles: { 'font-family': "'Source Han Sans SC', sans-serif" }, priority: 7 } },
|
|
|
|
|
+ { title: 'Arial', model: 'Arial, Helvetica, sans-serif', view: { name: 'span', styles: { 'font-family': "'Arial', Helvetica, sans-serif" }, priority: 7 } },
|
|
|
|
|
+ { title: 'Times New Roman', model: 'Times New Roman, Times, serif', view: { name: 'span', styles: { 'font-family': "'Times New Roman', Times, serif" }, priority: 7 } }
|
|
|
|
|
+ ]
|
|
|
|
|
+ },
|
|
|
|
|
+ fontSize: {
|
|
|
|
|
+ supportAllValues: true,
|
|
|
|
|
+ options: ['default', '10.5pt', '12pt', '14pt', '16pt', '18pt', '22pt', '26pt', '36pt']
|
|
|
|
|
+ },
|
|
|
mediaEmbed: {
|
|
mediaEmbed: {
|
|
|
previewsInData: true,
|
|
previewsInData: true,
|
|
|
providers: [{
|
|
providers: [{
|
|
@@ -2844,6 +3402,8 @@ Vue.component('spaceEditor', {
|
|
|
|
|
|
|
|
_this.editor = editor;
|
|
_this.editor = editor;
|
|
|
_this.alignFullScreenToolbarButton();
|
|
_this.alignFullScreenToolbarButton();
|
|
|
|
|
+ _this.mountFirstLineIndentToolbarButton(editor);
|
|
|
|
|
+ _this.configureWordUploadPlugin(editor);
|
|
|
if (_this.type !== 'flow') {
|
|
if (_this.type !== 'flow') {
|
|
|
_this.mountVideoUploadToolbarButton();
|
|
_this.mountVideoUploadToolbarButton();
|
|
|
_this.mountPdfUploadToolbarButton();
|
|
_this.mountPdfUploadToolbarButton();
|