diff --git a/README.md b/README.md index 18d771af..448bfe16 100644 --- a/README.md +++ b/README.md @@ -419,7 +419,7 @@ v0.1.5+ ### 方法 -#### keyCommand(key, fn) +#### addShortcut(key, fn) 添加快捷键 @@ -427,7 +427,7 @@ v0.1.5+ ```js // 单个按键 -mindMap.keyCommand..addShortcut('Enter', () => {}) +mindMap.keyCommand.addShortcut('Enter', () => {}) // 或 mindMap.keyCommand.addShortcut('Del|Backspace', () => {}) // 组合键 @@ -436,6 +436,21 @@ mindMap.keyCommand.addShortcut('Control+Enter', () => {}) `fn`:要执行的方法 +#### removeShortcut(key, fn) + +移除快捷键命令,`fn`不指定则移除该快捷键的所有回调方法 + +#### getShortcutFn(key) + +v0.2.2+。获取指定快捷键的处理函数 + +#### pause() + +v0.2.2+。暂停所有快捷键响应 + +#### recovery() + +v0.2.2+。恢复快捷键响应 ## command实例 diff --git a/index.html b/index.html index b3833881..efe46bab 100644 --- a/index.html +++ b/index.html @@ -1 +1 @@ -一个简单的web思维导图实现
\ No newline at end of file +一个简单的web思维导图实现
\ No newline at end of file diff --git a/simple-mind-map/package.json b/simple-mind-map/package.json index 1716c441..e7d80628 100644 --- a/simple-mind-map/package.json +++ b/simple-mind-map/package.json @@ -1,6 +1,6 @@ { "name": "simple-mind-map", - "version": "0.2.1", + "version": "0.2.2", "description": "一个简单的web在线思维导图", "authors": [ { diff --git a/simple-mind-map/src/KeyCommand.js b/simple-mind-map/src/KeyCommand.js index c1a89b04..6b31ee21 100644 --- a/simple-mind-map/src/KeyCommand.js +++ b/simple-mind-map/src/KeyCommand.js @@ -16,9 +16,28 @@ export default class KeyCommand { this.shortcutMap = { //Enter: [fn] } + this.isPause = false this.bindEvent() } + /** + * @Author: 王林 + * @Date: 2022-08-14 08:57:55 + * @Desc: 暂停快捷键响应 + */ + pause() { + this.isPause = true + } + + /** + * @Author: 王林 + * @Date: 2022-08-14 08:58:43 + * @Desc: 恢复快捷键响应 + */ + recovery() { + this.isPause = false + } + /** * @Author: 王林 * @Date: 2021-04-24 15:23:22 @@ -26,6 +45,9 @@ export default class KeyCommand { */ bindEvent() { window.addEventListener('keydown', (e) => { + if (this.isPause) { + return + } Object.keys(this.shortcutMap).forEach((key) => { if (this.checkKey(e, key)) { e.stopPropagation() @@ -139,4 +161,17 @@ export default class KeyCommand { } }) } + + /** + * @Author: 王林 + * @Date: 2022-08-14 08:49:58 + * @Desc: 获取指定快捷键的处理函数 + */ + getShortcutFn(key) { + let res = [] + key.split(/\s*\|\s*/).forEach((item) => { + res = this.shortcutMap[item] || [] + }) + return res + } } \ No newline at end of file diff --git a/simple-mind-map/src/Render.js b/simple-mind-map/src/Render.js index e68f29ab..18dab112 100644 --- a/simple-mind-map/src/Render.js +++ b/simple-mind-map/src/Render.js @@ -198,14 +198,8 @@ class Render { // 插入概要 this.mindMap.keyCommand.addShortcut('Shift+s', this.addGeneralization) // 展开/收起节点 - this.mindMap.keyCommand.addShortcut('/', () => { - this.activeNodeList.forEach((node) => { - if (node.nodeData.children.length <= 0) { - return - } - this.toggleNodeExpand(node) - }) - }) + this.toggleActiveExpand = this.toggleActiveExpand.bind(this) + this.mindMap.keyCommand.addShortcut('/', this.toggleActiveExpand) // 删除节点 this.removeNodeWrap = () => { this.mindMap.execCommand('REMOVE_NODE') @@ -239,6 +233,7 @@ class Render { */ startTextEdit() { this.mindMap.keyCommand.removeShortcut('Del|Backspace') + this.mindMap.keyCommand.removeShortcut('/') this.mindMap.keyCommand.removeShortcut('Enter', this.insertNodeWrap) } @@ -250,6 +245,7 @@ class Render { */ endTextEdit() { this.mindMap.keyCommand.addShortcut('Del|Backspace', this.removeNodeWrap) + this.mindMap.keyCommand.addShortcut('/', this.toggleActiveExpand) this.mindMap.keyCommand.addShortcut('Enter', this.insertNodeWrap) } @@ -801,6 +797,20 @@ class Render { }, null, true, 0, 0) } + /** + * @Author: 王林 + * @Date: 2022-08-14 09:18:40 + * @Desc: 切换激活节点的展开状态 + */ + toggleActiveExpand() { + this.activeNodeList.forEach((node) => { + if (node.nodeData.children.length <= 0) { + return + } + this.toggleNodeExpand(node) + }) + } + /** * @Author: 王林 * @Date: 2021-07-11 17:15:33 diff --git a/web/src/pages/Edit/components/NodeHyperlink.vue b/web/src/pages/Edit/components/NodeHyperlink.vue index 9b7c6be4..31a5ce96 100644 --- a/web/src/pages/Edit/components/NodeHyperlink.vue +++ b/web/src/pages/Edit/components/NodeHyperlink.vue @@ -53,6 +53,7 @@ export default { } }); this.$bus.$on("showNodeLink", () => { + this.activeNodes[0].mindMap.keyCommand.pause(); this.$bus.$emit('startTextEdit'); this.dialogVisible = true; }); @@ -65,6 +66,7 @@ export default { */ cancel() { this.dialogVisible = false; + this.activeNodes[0].mindMap.keyCommand.recovery(); this.$bus.$emit('endTextEdit'); },