From 5ff87047783ba6a1023f52a31ac6b66d1c2d5801 Mon Sep 17 00:00:00 2001 From: wanglin2 <1013335014@qq.com> Date: Fri, 25 Aug 2023 09:20:12 +0800 Subject: [PATCH] =?UTF-8?q?Feat=EF=BC=9A=E6=8B=A6=E6=88=AA=E5=AF=8C?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E7=BC=96=E8=BE=91=E6=97=B6=E7=9A=84=E7=B2=98?= =?UTF-8?q?=E8=B4=B4=E6=93=8D=E4=BD=9C=EF=BC=8C=E5=8E=BB=E6=8E=89=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E5=8F=AA=E5=85=81=E8=AE=B8=E7=B2=98=E8=B4=B4?= =?UTF-8?q?=E7=BA=AF=E6=96=87=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/plugins/RichText.js | 36 ++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/simple-mind-map/src/plugins/RichText.js b/simple-mind-map/src/plugins/RichText.js index 1666387e..8b393d60 100644 --- a/simple-mind-map/src/plugins/RichText.js +++ b/simple-mind-map/src/plugins/RichText.js @@ -1,6 +1,6 @@ import Quill from 'quill' +import Delta from 'quill-delta' import 'quill/dist/quill.snow.css' -// import domtoimage from 'dom-to-image-more' import { walk, getTextFromHtml, @@ -43,6 +43,7 @@ class RichText { this.quill = null this.range = null this.lastRange = null + this.pasteUseRange = null this.node = null this.isInserting = false this.styleEl = null @@ -329,6 +330,7 @@ class RichText { this.lastRange = this.range this.range = null if (range) { + this.pasteUseRange = range let bounds = this.quill.getBounds(range.index, range.length) let rect = this.textEditNode.getBoundingClientRect() let rectInfo = { @@ -368,6 +370,38 @@ class RichText { this.lostStyle = false } }) + // 拦截粘贴,只允许粘贴纯文本 + this.quill.clipboard.addMatcher(Node.TEXT_NODE, (node) => { + let style = this.getPasteTextStyle() + return new Delta().insert(node.data, style) + }) + this.quill.clipboard.addMatcher(Node.ELEMENT_NODE, (node, delta) => { + let ops = [] + let style = this.getPasteTextStyle() + delta.ops.forEach(op => { + // 过滤出文本内容,过滤掉换行 + if (op.insert && typeof op.insert === 'string' && op.insert !== '\n') { + ops.push({ + attributes: { ...style }, + insert: op.insert + }) + } + }) + delta.ops = ops + return delta + }) + } + + // 获取粘贴的文本的样式 + getPasteTextStyle() { + // 粘贴的数据使用当前光标位置处的文本样式 + if (this.pasteUseRange) { + return this.quill.getFormat( + this.pasteUseRange.index, + this.pasteUseRange.length + ) + } + return {} } // 正则输入中文