From 663fcc7cbeb948e4f5fe6bb5a27cefaf6f9d4f7b Mon Sep 17 00:00:00 2001 From: qiang Date: Thu, 16 Apr 2020 18:21:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20EditorContext.insertImage=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20width=E3=80=81height=E3=80=81extClass=E3=80=81data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../view/components/editor/formats/image.js | 28 +++++++++++++++++++ src/core/view/components/editor/index.vue | 10 +++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/core/view/components/editor/formats/image.js b/src/core/view/components/editor/formats/image.js index 92adcde21..e332fe54b 100644 --- a/src/core/view/components/editor/formats/image.js +++ b/src/core/view/components/editor/formats/image.js @@ -1,4 +1,32 @@ export default function (Quill) { const Image = Quill.import('formats/image') + const ATTRIBUTES = [ + 'alt', + 'height', + 'width', + 'data-custom', + 'class', + 'data-local' + ] Image.sanitize = url => url + Image.formats = function formats (domNode) { + return ATTRIBUTES.reduce(function (formats, attribute) { + if (domNode.hasAttribute(attribute)) { + formats[attribute] = domNode.getAttribute(attribute) + } + return formats + }, {}) + } + const format = Image.prototype.format + Image.prototype.format = function (name, value) { + if (ATTRIBUTES.indexOf(name) > -1) { + if (value) { + this.domNode.setAttribute(name, value) + } else { + this.domNode.removeAttribute(name) + } + } else { + format.call(this, name, value) + } + } } diff --git a/src/core/view/components/editor/index.vue b/src/core/view/components/editor/index.vue index 8888b4ed6..1ab538925 100644 --- a/src/core/view/components/editor/index.vue +++ b/src/core/view/components/editor/index.vue @@ -142,9 +142,15 @@ export default { case 'insertImage': { range = quill.getSelection(true) - const { src = '', alt = '', data = {} } = options - quill.insertEmbed(range.index, 'image', this.$getRealPath(src), Quill.sources.USER) + const { src = '', alt = '', width = '', height = '', extClass = '', data = {} } = options + const path = this.$getRealPath(src) + quill.insertEmbed(range.index, 'image', path, Quill.sources.USER) + const local = /^(file|blob):/.test(path) ? path : false + quill.formatText(range.index, 1, 'data-local', local) quill.formatText(range.index, 1, 'alt', alt) + quill.formatText(range.index, 1, 'width', width) + quill.formatText(range.index, 1, 'height', height) + quill.formatText(range.index, 1, 'class', extClass) quill.formatText(range.index, 1, 'data-custom', Object.keys(data).map(key => `${key}=${data[key]}`).join('&')) quill.setSelection(range.index + 1, Quill.sources.SILENT) } -- GitLab