优化代码

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

View File

@ -132,11 +132,6 @@ class Render {
} }
} }
// 解绑事件
unBindEvent() {
window.removeEventListener('paste', this.handlePaste)
}
// 绑定事件 // 绑定事件
bindEvent() { bindEvent() {
// 画布点击事件清除当前激活节点列表 // 画布点击事件清除当前激活节点列表
@ -172,7 +167,7 @@ class Render {
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()
}) })
@ -1134,22 +1129,23 @@ 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)
} }
} }