优化代码

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