1.优化重复的历史数据;2.修复节点编辑时的方向键冲突;3.修复拖拽节点的id丢失问题

This commit is contained in:
wanglin2 2023-03-21 09:34:47 +08:00
parent 8dcfdcc44a
commit 5313b9b69c
5 changed files with 32 additions and 22 deletions

View File

@ -1,6 +1,6 @@
{
"name": "simple-mind-map",
"version": "0.4.5",
"version": "0.4.6",
"description": "一个简单的web在线思维导图",
"authors": [
{

View File

@ -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)

View File

@ -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()
}
}
// 聚焦到下一个节点

View File

@ -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)

View File

@ -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
}