优化代码

This commit is contained in:
街角小林 2024-09-19 18:14:52 +08:00
parent c36338a794
commit 5d49d985c0

View File

@ -97,7 +97,7 @@ class Render {
this.beingPasteText = '' this.beingPasteText = ''
this.beingPasteImgSize = 0 this.beingPasteImgSize = 0
this.currentBeingPasteType = '' this.currentBeingPasteType = ''
this.pasteData = { text: null, img: null} this.pasteData = { text: null, img: null }
// 节点高亮框 // 节点高亮框
this.highlightBoxNode = null this.highlightBoxNode = null
this.highlightBoxNodeStyle = null this.highlightBoxNodeStyle = null
@ -132,11 +132,6 @@ class Render {
} }
} }
// 解绑事件
unBindEvent() {
window.removeEventListener('paste', this.handlePaste)
}
// 绑定事件 // 绑定事件
bindEvent() { bindEvent() {
// 画布点击事件清除当前激活节点列表 // 画布点击事件清除当前激活节点列表
@ -168,11 +163,11 @@ class Render {
}) })
} }
// 处理非https下的复制黏贴问题 // 处理非https下的复制黏贴问题
if(!navigator.clipboard) { if (!navigator.clipboard) {
this.handlePaste = this.handlePaste.bind(this) this.handlePaste = this.handlePaste.bind(this)
window.addEventListener('paste', this.handlePaste) window.addEventListener('paste', this.handlePaste)
this.mindMap.on('beforeDestroy', () => { this.mindMap.on('beforeDestroy', () => {
this.unBindEvent() window.removeEventListener('paste', this.handlePaste)
}) })
} }
} }
@ -419,7 +414,7 @@ class Render {
this.mindMap.keyCommand.addShortcut('Control+Down', () => { this.mindMap.keyCommand.addShortcut('Control+Down', () => {
this.mindMap.execCommand('DOWN_NODE') this.mindMap.execCommand('DOWN_NODE')
}) })
// 复制节点 // 复制节点
this.mindMap.keyCommand.addShortcut('Control+c', () => { this.mindMap.keyCommand.addShortcut('Control+c', () => {
this.copy() this.copy()
}) })
@ -429,7 +424,7 @@ class Render {
}) })
// 粘贴节点 // 粘贴节点
this.mindMap.keyCommand.addShortcut('Control+v', () => { this.mindMap.keyCommand.addShortcut('Control+v', () => {
if(navigator.clipboard) this.paste() if (navigator.clipboard) this.paste()
}) })
// 根节点居中显示 // 根节点居中显示
this.mindMap.keyCommand.addShortcut('Control+Enter', () => { this.mindMap.keyCommand.addShortcut('Control+Enter', () => {
@ -1133,23 +1128,24 @@ class Render {
} }
// 非https下复制黏贴获取内容方法 // 非https下复制黏贴获取内容方法
handlePaste(event){ handlePaste(event) {
const clipboardData = event.clipboardData || event.originalEvent.clipboardData || window.clipboardData const { disabledClipboard } = this.mindMap.opt
if (disabledClipboard) return
const clipboardData =
event.clipboardData || event.originalEvent.clipboardData
const items = clipboardData.items const items = clipboardData.items
const clipboardType = items && items.length ? items[0].type : '' let img = null
const isImg = clipboardType.indexOf('image') > -1 let text = ''
const isText = clipboardType.indexOf('text') > -1 Array.from(items).forEach(item => {
this.pasteData = { img: null, text: null} if (item.type.indexOf('image') > -1) {
img = item.getAsFile()
// 复制的图片处理逻辑
if (isImg) {
for (let index in items) {
const item = items[index]
if (item.kind === 'file') this.pasteData.img = item.getAsFile()
} }
} if (item.type.indexOf('text') > -1) {
// 复制的文本处理逻辑 text = clipboardData.getData('text')
if (isText) this.pasteData.text = clipboardData.getData('text') }
})
this.pasteData.img = img
this.pasteData.text = text
this.paste() this.paste()
} }
@ -1166,7 +1162,9 @@ class Render {
let img = null let img = null
if (!disabledClipboard) { if (!disabledClipboard) {
try { try {
const res = navigator.clipboard ? await getDataFromClipboard() : this.pasteData const res = navigator.clipboard
? await getDataFromClipboard()
: this.pasteData
text = res.text || '' text = res.text || ''
img = res.img || null img = res.img || null
} catch (error) { } catch (error) {
@ -1600,7 +1598,7 @@ class Render {
this.setNodeDataRender(node, data) this.setNodeDataRender(node, data)
// 更新了连线的样式 // 更新了连线的样式
if (lineStyleProps.includes(prop)) { if (lineStyleProps.includes(prop)) {
(node.parent || node).renderLine(true) ;(node.parent || node).renderLine(true)
} }
} }
@ -1621,7 +1619,7 @@ class Render {
} }
}) })
if (hasLineStyleProps) { if (hasLineStyleProps) {
(node.parent || node).renderLine(true) ;(node.parent || node).renderLine(true)
} }
} }