Feat:节点富文本编辑中,如果粘贴的是smm格式的数据,那么取出第一个节点的纯文本进行粘贴

This commit is contained in:
街角小林 2023-12-29 17:46:58 +08:00
parent c0fb1e8db8
commit 1409b07fb3
3 changed files with 60 additions and 22 deletions

View File

@ -26,7 +26,9 @@ import {
getDataFromClipboard, getDataFromClipboard,
htmlEscape, htmlEscape,
parseAddGeneralizationNodeList, parseAddGeneralizationNodeList,
checkNodeListIsEqual checkNodeListIsEqual,
createSmmFormatData,
checkSmmFormatData
} from '../../utils' } from '../../utils'
import { shapeList } from './node/Shape' import { shapeList } from './node/Shape'
import { lineStyleProps } from '../../themes/default' import { lineStyleProps } from '../../themes/default'
@ -876,20 +878,14 @@ class Render {
copy() { copy() {
this.beingCopyData = this.copyNode() this.beingCopyData = this.copyNode()
if (!this.beingCopyData) return if (!this.beingCopyData) return
setDataToClipboard({ setDataToClipboard(createSmmFormatData(this.beingCopyData))
simpleMindMap: true,
data: this.beingCopyData
})
} }
// 剪切节点 // 剪切节点
cut() { cut() {
this.mindMap.execCommand('CUT_NODE', copyData => { this.mindMap.execCommand('CUT_NODE', copyData => {
this.beingCopyData = copyData this.beingCopyData = copyData
setDataToClipboard({ setDataToClipboard(createSmmFormatData(copyData))
simpleMindMap: true,
data: copyData
})
}) })
} }
@ -936,10 +932,11 @@ class Render {
const res = await this.mindMap.opt.customHandleClipboardText(text) const res = await this.mindMap.opt.customHandleClipboardText(text)
if (!isUndef(res)) { if (!isUndef(res)) {
useDefault = false useDefault = false
if (typeof res === 'object' && res.simpleMindMap) { const checkRes = checkSmmFormatData(res)
smmData = res.data if (checkRes.isSmm) {
smmData = checkRes.data
} else { } else {
text = String(res) text = checkRes.data
} }
} }
} catch (error) { } catch (error) {
@ -948,13 +945,11 @@ class Render {
} }
// 默认处理 // 默认处理
if (useDefault) { if (useDefault) {
try { const checkRes = checkSmmFormatData(text)
const parsedData = JSON.parse(text) if (checkRes.isSmm) {
if (parsedData && parsedData.simpleMindMap) { smmData = checkRes.data
smmData = parsedData.data } else {
} text = checkRes.data
} catch (error) {
errorHandler(ERROR_TYPES.PARSE_PASTE_DATA_ERROR, error)
} }
} }
if (smmData) { if (smmData) {

View File

@ -6,7 +6,8 @@ import {
getTextFromHtml, getTextFromHtml,
isWhite, isWhite,
getVisibleColorFromTheme, getVisibleColorFromTheme,
isUndef isUndef,
checkSmmFormatData
} from '../utils' } from '../utils'
import { CONSTANTS } from '../constants/constant' import { CONSTANTS } from '../constants/constant'
@ -397,7 +398,7 @@ class RichText {
// 拦截粘贴,只允许粘贴纯文本 // 拦截粘贴,只允许粘贴纯文本
this.quill.clipboard.addMatcher(Node.TEXT_NODE, node => { this.quill.clipboard.addMatcher(Node.TEXT_NODE, node => {
let style = this.getPasteTextStyle() let style = this.getPasteTextStyle()
return new Delta().insert(node.data, style) return new Delta().insert(this.formatPasteText(node.data), style)
}) })
this.quill.clipboard.addMatcher(Node.ELEMENT_NODE, (node, delta) => { this.quill.clipboard.addMatcher(Node.ELEMENT_NODE, (node, delta) => {
let ops = [] let ops = []
@ -407,7 +408,7 @@ class RichText {
if (op.insert && typeof op.insert === 'string' && op.insert !== '\n') { if (op.insert && typeof op.insert === 'string' && op.insert !== '\n') {
ops.push({ ops.push({
attributes: { ...style }, attributes: { ...style },
insert: op.insert insert: this.formatPasteText(op.insert)
}) })
} }
}) })
@ -428,6 +429,17 @@ class RichText {
return {} return {}
} }
// 处理粘贴的文本内容
formatPasteText(text) {
const { isSmm, data } = checkSmmFormatData(text)
if (isSmm && data[0] && data[0].data) {
// 只取第一个节点的纯文本
return getTextFromHtml(data[0].data.text)
} else {
return text
}
}
// 正则输入中文 // 正则输入中文
onCompositionStart() { onCompositionStart() {
if (!this.showTextEdit) { if (!this.showTextEdit) {

View File

@ -1027,3 +1027,34 @@ export const getChromeVersion = () => {
} }
return '' return ''
} }
// 创建smm粘贴的粘贴数据
export const createSmmFormatData = data => {
return {
simpleMindMap: true,
data
}
}
// 检查是否是smm粘贴格式的数据
export const checkSmmFormatData = data => {
let smmData = null
// 如果是字符串,则尝试解析为对象
if (typeof data === 'string') {
try {
const parsedData = JSON.parse(data)
// 判断是否是对象,且存在属性标志
if (typeof parsedData === 'object' && parsedData.simpleMindMap) {
smmData = parsedData.data
}
} catch (error) {}
} else if (typeof data === 'object' && data.simpleMindMap) {
// 否则如果是对象,则检查属性标志
smmData = data.data
}
const isSmm = !!smmData
return {
isSmm,
data: isSmm ? smmData : String(data)
}
}