From 2c1b2211e74a7f54d8bc909f632e596803385be1 Mon Sep 17 00:00:00 2001 From: qiang Date: Wed, 12 Aug 2020 18:42:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20editor=20=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=8F=AF=E8=83=BD=E9=87=8D=E5=A4=8D=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=20Quill=E3=80=81QuillImageResize=20=E4=BE=9D=E8=B5=96=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/view/components/editor/index.vue | 31 +++---------------- .../view/components/editor/load-script.js | 21 +++++++++++++ 2 files changed, 26 insertions(+), 26 deletions(-) create mode 100644 src/core/view/components/editor/load-script.js diff --git a/src/core/view/components/editor/index.vue b/src/core/view/components/editor/index.vue index 6ee1ef42de..39b15c62f8 100644 --- a/src/core/view/components/editor/index.vue +++ b/src/core/view/components/editor/index.vue @@ -14,6 +14,7 @@ import { } from 'uni-mixins' import HTMLParser from 'uni-helpers/html-parser' import * as formats from './formats' +import loadScript from './load-script' export default { name: 'Editor', @@ -78,9 +79,11 @@ export default { if (this.showImgResize) { imageResizeModules.push('Resize') } - this.loadQuill(() => { + const quillSrc = __PLATFORM__ === 'app-plus' ? './__uniappquill.js' : 'https://unpkg.com/quill@1.3.7/dist/quill.min.js' + loadScript(window.Quill, quillSrc, () => { if (imageResizeModules.length) { - this.loadImageResizeModule(() => { + const imageResizeSrc = __PLATFORM__ === 'app-plus' ? './__uniappquillimageresize.js' : 'https://unpkg.com/quill-image-resize-mp@3.0.1/image-resize.min.js' + loadScript(window.ImageResize, imageResizeSrc, () => { this.initQuill(imageResizeModules) }) } else { @@ -218,30 +221,6 @@ export default { }, this.$page.id) } }, - loadQuill (callback) { - if (typeof window.Quill === 'function') { - if (typeof callback === 'function') { - callback() - } - return - } - const script = document.createElement('script') - script.src = __PLATFORM__ === 'app-plus' ? './__uniappquill.js' : 'https://unpkg.com/quill@1.3.7/dist/quill.min.js' - document.body.appendChild(script) - script.onload = callback - }, - loadImageResizeModule (callback) { - if (typeof window.ImageResize === 'function') { - if (typeof callback === 'function') { - callback() - } - return - } - const script = document.createElement('script') - script.src = __PLATFORM__ === 'app-plus' ? './__uniappquillimageresize.js' : 'https://unpkg.com/quill-image-resize-mp@3.0.1/image-resize.min.js' - document.body.appendChild(script) - script.onload = callback - }, initQuill (imageResizeModules) { const Quill = window.Quill formats.register(Quill) diff --git a/src/core/view/components/editor/load-script.js b/src/core/view/components/editor/load-script.js new file mode 100644 index 0000000000..7d4d8a6826 --- /dev/null +++ b/src/core/view/components/editor/load-script.js @@ -0,0 +1,21 @@ +const scripts = {} + +export default function loadScript (globalName, src, callback) { + const globalObject = typeof globalName === 'string' ? window[globalName] : globalName + if (globalObject) { + callback() + return + } + let callbacks = scripts[src] + if (!callbacks) { + callbacks = scripts[src] = [] + const script = document.createElement('script') + script.src = src + document.body.appendChild(script) + script.onload = function () { + callbacks.forEach(callback => callback()) + delete scripts[src] + } + } + callbacks.push(callback) +} -- GitLab