From fa7761b5a35dd59223553e0b3a9d4612732f749a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Thu, 7 Nov 2024 09:33:26 +0800 Subject: [PATCH] =?UTF-8?q?Feat=EF=BC=9A1.=E5=8F=AA=E6=9C=89=E5=BD=93?= =?UTF-8?q?=E6=8C=89=E9=94=AE=E4=BA=8B=E4=BB=B6=E7=9A=84=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E7=9B=AE=E6=A0=87=E4=B8=BAbody=E6=88=96=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E7=BC=96=E8=BE=91=E6=A1=86=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E6=97=B6=E6=89=8D=E5=93=8D=E5=BA=94=E5=BF=AB=E6=8D=B7=E9=94=AE?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=EF=BC=9B2.=E6=96=B0=E5=A2=9E=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=93=8D=E5=BA=94=E5=BF=AB=E6=8D=B7=E9=94=AE?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E7=9A=84=E5=AE=9E=E4=BE=8B=E5=8C=96=E9=80=89?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/constants/constant.js | 6 ++++- .../src/constants/defaultOptions.js | 3 +++ .../src/core/command/KeyCommand.js | 23 +++++++++++++++++-- simple-mind-map/src/plugins/RichText.js | 6 +++-- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/simple-mind-map/src/constants/constant.js b/simple-mind-map/src/constants/constant.js index 110c90f4..6fae1173 100644 --- a/simple-mind-map/src/constants/constant.js +++ b/simple-mind-map/src/constants/constant.js @@ -75,6 +75,10 @@ export const CONSTANTS = { TAG_POSITION: { RIGHT: 'right', BOTTOM: 'bottom' + }, + EDIT_NODE_CLASS: { + SMM_NODE_EDIT_WRAP: 'smm-node-edit-wrap', + RICH_TEXT_EDIT_WRAP: 'ql-editor' } } @@ -215,4 +219,4 @@ export const selfCloseTagList = [ ] // 非富文本模式下的节点文本行高 -export const noneRichTextNodeLineHeight = 1.2 \ No newline at end of file +export const noneRichTextNodeLineHeight = 1.2 diff --git a/simple-mind-map/src/constants/defaultOptions.js b/simple-mind-map/src/constants/defaultOptions.js index 91755a1e..028201ed 100644 --- a/simple-mind-map/src/constants/defaultOptions.js +++ b/simple-mind-map/src/constants/defaultOptions.js @@ -84,6 +84,9 @@ export const defaultOpt = { isShowExpandNum: true, // 是否只有当鼠标在画布内才响应快捷键事件 enableShortcutOnlyWhenMouseInSvg: true, + // 自定义判断是否响应快捷键事件,优先级比enableShortcutOnlyWhenMouseInSvg选项高 + // 可以传递一个函数,接收事件对象e为参数,需要返回true或false,返回true代表允许响应快捷键事件,反之不允许,库默认当事件目标为body,或为文本编辑框元素时响应快捷键,其他不响应 + customCheckEnableShortcut: null, // 初始根节点的位置 initRootNodePosition: null, // 节点文本编辑框的z-index diff --git a/simple-mind-map/src/core/command/KeyCommand.js b/simple-mind-map/src/core/command/KeyCommand.js index 13c7783d..f65c9fa3 100644 --- a/simple-mind-map/src/core/command/KeyCommand.js +++ b/simple-mind-map/src/core/command/KeyCommand.js @@ -1,4 +1,5 @@ import { keyMap } from './keyMap' +import { CONSTANTS } from '../../constants/constant' // 快捷按键、命令处理类 export default class KeyCommand { @@ -73,10 +74,28 @@ export default class KeyCommand { window.removeEventListener('keydown', this.onKeydown) } + // 根据事件目标判断是否响应快捷键事件 + defaultEnableCheck(e) { + const target = e.target + return ( + target === document.body || + target.classList.contains(CONSTANTS.EDIT_NODE_CLASS.SMM_NODE_EDIT_WRAP) || + target.classList.contains(CONSTANTS.EDIT_NODE_CLASS.RICH_TEXT_EDIT_WRAP) + ) + } + // 按键事件 onKeydown(e) { - const { enableShortcutOnlyWhenMouseInSvg, beforeShortcutRun } = - this.mindMap.opt + const { + enableShortcutOnlyWhenMouseInSvg, + beforeShortcutRun, + customCheckEnableShortcut + } = this.mindMap.opt + const checkFn = + typeof customCheckEnableShortcut === 'function' + ? customCheckEnableShortcut + : this.defaultEnableCheck + if (!checkFn(e)) return if (this.isPause || (enableShortcutOnlyWhenMouseInSvg && !this.isInSvg)) { return } diff --git a/simple-mind-map/src/plugins/RichText.js b/simple-mind-map/src/plugins/RichText.js index 392c65b4..05eee7bb 100644 --- a/simple-mind-map/src/plugins/RichText.js +++ b/simple-mind-map/src/plugins/RichText.js @@ -101,7 +101,7 @@ class RichText { ` ) let cssText = ` - .ql-editor { + .${CONSTANTS.EDIT_NODE_CLASS.RICH_TEXT_EDIT_WRAP} { overflow: hidden; padding: 0; height: auto; @@ -297,7 +297,9 @@ class RichText { this.textEditNode.innerHTML = this.cacheEditingText || nodeText } this.initQuillEditor() - document.querySelector('.ql-editor').style.minHeight = originHeight + 'px' + document.querySelector( + '.' + CONSTANTS.EDIT_NODE_CLASS.RICH_TEXT_EDIT_WRAP + ).style.minHeight = originHeight + 'px' this.showTextEdit = true // 如果是刚创建的节点,那么默认全选,否则普通激活不全选,除非selectTextOnEnterEditText配置为true // 在selectTextOnEnterEditText时,如果是在keydown事件进入的节点编辑,也不需要全选