From 5313b9b69cc94e40fd328eae941cf227363a0320 Mon Sep 17 00:00:00 2001 From: wanglin2 <1013335014@qq.com> Date: Tue, 21 Mar 2023 09:34:47 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BC=98=E5=8C=96=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E6=95=B0=E6=8D=AE=EF=BC=9B2.=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E8=8A=82=E7=82=B9=E7=BC=96=E8=BE=91=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E5=90=91=E9=94=AE=E5=86=B2=E7=AA=81=EF=BC=9B3.?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8B=96=E6=8B=BD=E8=8A=82=E7=82=B9=E7=9A=84?= =?UTF-8?q?id=E4=B8=A2=E5=A4=B1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/package.json | 2 +- simple-mind-map/src/Command.js | 4 +++ simple-mind-map/src/KeyboardNavigation.js | 34 +++++++++++++---------- simple-mind-map/src/Render.js | 10 +++---- simple-mind-map/src/utils/index.js | 4 +-- 5 files changed, 32 insertions(+), 22 deletions(-) diff --git a/simple-mind-map/package.json b/simple-mind-map/package.json index 5c446feb..5f154591 100644 --- a/simple-mind-map/package.json +++ b/simple-mind-map/package.json @@ -1,6 +1,6 @@ { "name": "simple-mind-map", - "version": "0.4.5", + "version": "0.4.6", "description": "一个简单的web在线思维导图", "authors": [ { diff --git a/simple-mind-map/src/Command.js b/simple-mind-map/src/Command.js index 25ad3f33..71a88be1 100644 --- a/simple-mind-map/src/Command.js +++ b/simple-mind-map/src/Command.js @@ -76,6 +76,10 @@ class Command { return } let data = this.getCopyData() + // 此次数据和上次一样则不重复添加 + if (this.history.length > 0 && JSON.stringify(this.history[this.history.length - 1]) === JSON.stringify(data)) { + return + } this.history.push(simpleDeepClone(data)) this.activeHistoryIndex = this.history.length - 1 this.mindMap.emit('data_change', data) diff --git a/simple-mind-map/src/KeyboardNavigation.js b/simple-mind-map/src/KeyboardNavigation.js index a8c7d1c6..0408dfae 100644 --- a/simple-mind-map/src/KeyboardNavigation.js +++ b/simple-mind-map/src/KeyboardNavigation.js @@ -1,4 +1,3 @@ -import { isKey } from './utils/keyMap' import { bfsWalk } from './utils' // 键盘导航类 @@ -8,22 +7,29 @@ class KeyboardNavigation { this.opt = opt this.mindMap = opt.mindMap this.onKeyup = this.onKeyup.bind(this) - this.mindMap.on('keyup', this.onKeyup) + this.mindMap.keyCommand.addShortcut('Left', () => { + this.onKeyup('Left') + }) + this.mindMap.keyCommand.addShortcut('Up', () => { + this.onKeyup('Up') + }) + this.mindMap.keyCommand.addShortcut('Right', () => { + this.onKeyup('Right') + }) + this.mindMap.keyCommand.addShortcut('Down', () => { + this.onKeyup('Down') + }) } // 处理按键事件 - onKeyup(e) { - ;['Left', 'Up', 'Right', 'Down'].forEach(dir => { - if (isKey(e, dir)) { - if (this.mindMap.renderer.activeNodeList.length > 0) { - this.focus(dir) - } else { - let root = this.mindMap.renderer.root - this.mindMap.renderer.moveNodeToCenter(root) - root.active() - } - } - }) + onKeyup(dir) { + if (this.mindMap.renderer.activeNodeList.length > 0) { + this.focus(dir) + } else { + let root = this.mindMap.renderer.root + this.mindMap.renderer.moveNodeToCenter(root) + root.active() + } } // 聚焦到下一个节点 diff --git a/simple-mind-map/src/Render.js b/simple-mind-map/src/Render.js index 6d3c92e5..4c87a471 100644 --- a/simple-mind-map/src/Render.js +++ b/simple-mind-map/src/Render.js @@ -355,7 +355,7 @@ class Render { // 插入同级节点,多个节点只会操作第一个节点 - insertNode() { + insertNode(openEdit = true) { if (this.activeNodeList.length <= 0) { return } @@ -369,7 +369,7 @@ class Render { } let index = this.getNodeIndex(first) first.parent.nodeData.children.splice(index + 1, 0, { - inserting: true, + inserting: openEdit, data: { text: text, expand: true @@ -382,7 +382,7 @@ class Render { // 插入子节点 - insertChildNode() { + insertChildNode(openEdit = true) { if (this.activeNodeList.length <= 0) { return } @@ -392,7 +392,7 @@ class Render { } let text = node.isRoot ? '二级节点' : '分支主题' node.nodeData.children.push({ - inserting: true, + inserting: openEdit, data: { text: text, expand: true @@ -629,7 +629,7 @@ class Render { if (node.isRoot) { return } - let copyData = copyNodeTree({}, node) + let copyData = copyNodeTree({}, node, false, true) this.removeActiveNode(node) this.removeOneNode(node) this.mindMap.emit('node_active', null, this.activeNodeList) diff --git a/simple-mind-map/src/utils/index.js b/simple-mind-map/src/utils/index.js index 35f8f812..7a825a6e 100644 --- a/simple-mind-map/src/utils/index.js +++ b/simple-mind-map/src/utils/index.js @@ -134,10 +134,10 @@ export const copyRenderTree = (tree, root) => { } // 复制节点树数据 -export const copyNodeTree = (tree, root, removeActiveState = false) => { +export const copyNodeTree = (tree, root, removeActiveState = false, keepId = false) => { tree.data = simpleDeepClone(root.nodeData ? root.nodeData.data : root.data) // 去除节点id,因为节点id不能重复 - if (tree.data.id) delete tree.data.id + if (tree.data.id && !keepId) delete tree.data.id if (removeActiveState) { tree.data.isActive = false }