From 0cfa21d4e6a8446213790bc6b46b3c6f3f8c8399 Mon Sep 17 00:00:00 2001 From: wanglin2 <1013335014@qq.com> Date: Fri, 29 Dec 2023 22:02:15 +0800 Subject: [PATCH] =?UTF-8?q?Feat:=E5=A2=9E=E5=8A=A0=E5=AF=B9=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E9=9D=9E=E5=AF=8C=E6=96=87=E6=9C=AC=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E7=B2=98=E8=B4=B4=E7=9A=84=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/core/render/TextEdit.js | 15 ++++++++++++++- simple-mind-map/src/utils/index.js | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/simple-mind-map/src/core/render/TextEdit.js b/simple-mind-map/src/core/render/TextEdit.js index 620ba85a..6d96b1ea 100644 --- a/simple-mind-map/src/core/render/TextEdit.js +++ b/simple-mind-map/src/core/render/TextEdit.js @@ -3,7 +3,10 @@ import { checkNodeOuter, focusInput, selectAllInput, - htmlEscape + htmlEscape, + handleInputPasteText, + checkSmmFormatData, + getTextFromHtml } from '../../utils' import { ERROR_TYPES, CONSTANTS } from '../../constants/constant' @@ -226,6 +229,16 @@ export default class TextEdit { e.stopPropagation() } }) + this.textEditNode.addEventListener('paste', e => { + const text = e.clipboardData.getData('text') + const { isSmm, data } = checkSmmFormatData(text) + if (isSmm && data[0] && data[0].data) { + // 只取第一个节点的纯文本 + handleInputPasteText(e, getTextFromHtml(data[0].data.text)) + } else { + handleInputPasteText(e) + } + }) const targetNode = this.mindMap.opt.customInnerElsAppendTo || document.body targetNode.appendChild(this.textEditNode) diff --git a/simple-mind-map/src/utils/index.js b/simple-mind-map/src/utils/index.js index 6417e79d..6fdf0230 100644 --- a/simple-mind-map/src/utils/index.js +++ b/simple-mind-map/src/utils/index.js @@ -1058,3 +1058,19 @@ export const checkSmmFormatData = data => { data: isSmm ? smmData : String(data) } } + +// 处理输入框的粘贴事件,会去除文本的html格式、换行 +export const handleInputPasteText = (e, text) => { + e.preventDefault() + const selection = window.getSelection() + if (!selection.rangeCount) return + selection.deleteFromDocument() + text = text || e.clipboardData.getData('text') + // 去除格式 + text = getTextFromHtml(text) + // 去除换行 + text = text.replaceAll(/\n/g, '') + const node = document.createTextNode(text) + selection.getRangeAt(0).insertNode(node) + selection.collapseToEnd() +}