From 2dcef654b4bab0afa55eb8f1087ff4666c01e439 Mon Sep 17 00:00:00 2001 From: dleged <15658866967@163.com> Date: Thu, 7 Mar 2024 15:17:15 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E5=9C=A8=E7=BC=96=E8=BE=91=E5=99=A8?= =?UTF-8?q?=E4=B8=AD=E5=A2=9E=E5=8A=A0=E6=93=8D=E4=BD=9C=E9=87=8D=E7=BD=AE?= =?UTF-8?q?,=E6=8B=B7=E8=B4=9D=E5=92=8C=E8=BF=90=E8=A1=8C=E7=9A=84?= =?UTF-8?q?=E5=BF=AB=E6=8D=B7=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- en/assets/shortcut-key.js | 74 ++++++++++++++++++++++++++++++++++++ en/book.toml | 2 +- zh-CN/assets/shortcut-key.js | 74 ++++++++++++++++++++++++++++++++++++ zh-CN/book.toml | 2 +- 4 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 en/assets/shortcut-key.js create mode 100644 zh-CN/assets/shortcut-key.js diff --git a/en/assets/shortcut-key.js b/en/assets/shortcut-key.js new file mode 100644 index 00000000..381d5835 --- /dev/null +++ b/en/assets/shortcut-key.js @@ -0,0 +1,74 @@ + +// 处理键位和功能绑定关系,执行匹配的快捷键的功能 +class KeyBinding { + constructor(combinationKeys, selectorMap) { + this.combinationKeys = combinationKeys; + this.selectorMap = selectorMap; + } + + matchAction(e) { + return Object.entries(this.combinationKeys).find(([k, val]) => { + const { prefixKeys = [], keys } = val; + const isComposing = !!prefixKeys.find((key) => e[key]); + return isComposing && keys.includes(e.key); + }); + } + + handleAction(e) { + const keyType = this.matchAction(e); + if (keyType) { + const actionButton = document.querySelector(this.selectorMap[keyType[0]]); + if (actionButton) actionButton.click(); + } + } +} + +// 键位绑定器,监听 keydown 事件 +class EventBinder { + constructor(contentId, keyBinding) { + this.contentDom = document.getElementById(contentId); + this.keyBinding = keyBinding; + } + + bindKeydown() { + if (this.contentDom) { + this.contentDom.addEventListener('keydown', (e) => { + const target = e.target; + if (target && target.classList.contains('ace_text-input')) { + this.keyBinding.handleAction(e); + } + }); + } + } +} + +// 封装初始化逻辑 +function initializeKeyBinding() { + const bindCombinationKeysWithAction = { + // 重置用 i 键代替,编辑框中内置了很多快捷键,没有合适的键位设置了 + 'reset': { + prefixKeys: ['ctrlKey', 'metaKey'], + keys: ['i'] + }, + 'clip': { + prefixKeys: ['ctrlKey', 'metaKey'], + keys: ['c'] + }, + 'play': { + prefixKeys: ['ctrlKey', 'metaKey'], + keys: ['Enter'], + } + }; + + const combinationKeysWithSelector = { + 'reset': '.reset-button', + 'clip': '.clip-button', + 'play': '.play-button' + }; + + const keyBinding = new KeyBinding(bindCombinationKeysWithAction, combinationKeysWithSelector); + const eventBinder = new EventBinder('content', keyBinding); + eventBinder.bindKeydown(); +} + +window.addEventListener('load', initializeKeyBinding); diff --git a/en/book.toml b/en/book.toml index abe56952..3b6664fb 100644 --- a/en/book.toml +++ b/en/book.toml @@ -14,7 +14,7 @@ level = 1 [output.html] additional-css = ["theme/style1.css"] -additional-js = ["assets/custom3.js", "assets/lang1.js"] +additional-js = ["assets/custom3.js", "assets/lang1.js","assets/shortcut-key.js"] git-repository-url = "https://github.com/sunface/rust-by-practice" edit-url-template = "https://github.com/sunface/rust-by-practice/edit/master/en/{path}" diff --git a/zh-CN/assets/shortcut-key.js b/zh-CN/assets/shortcut-key.js new file mode 100644 index 00000000..381d5835 --- /dev/null +++ b/zh-CN/assets/shortcut-key.js @@ -0,0 +1,74 @@ + +// 处理键位和功能绑定关系,执行匹配的快捷键的功能 +class KeyBinding { + constructor(combinationKeys, selectorMap) { + this.combinationKeys = combinationKeys; + this.selectorMap = selectorMap; + } + + matchAction(e) { + return Object.entries(this.combinationKeys).find(([k, val]) => { + const { prefixKeys = [], keys } = val; + const isComposing = !!prefixKeys.find((key) => e[key]); + return isComposing && keys.includes(e.key); + }); + } + + handleAction(e) { + const keyType = this.matchAction(e); + if (keyType) { + const actionButton = document.querySelector(this.selectorMap[keyType[0]]); + if (actionButton) actionButton.click(); + } + } +} + +// 键位绑定器,监听 keydown 事件 +class EventBinder { + constructor(contentId, keyBinding) { + this.contentDom = document.getElementById(contentId); + this.keyBinding = keyBinding; + } + + bindKeydown() { + if (this.contentDom) { + this.contentDom.addEventListener('keydown', (e) => { + const target = e.target; + if (target && target.classList.contains('ace_text-input')) { + this.keyBinding.handleAction(e); + } + }); + } + } +} + +// 封装初始化逻辑 +function initializeKeyBinding() { + const bindCombinationKeysWithAction = { + // 重置用 i 键代替,编辑框中内置了很多快捷键,没有合适的键位设置了 + 'reset': { + prefixKeys: ['ctrlKey', 'metaKey'], + keys: ['i'] + }, + 'clip': { + prefixKeys: ['ctrlKey', 'metaKey'], + keys: ['c'] + }, + 'play': { + prefixKeys: ['ctrlKey', 'metaKey'], + keys: ['Enter'], + } + }; + + const combinationKeysWithSelector = { + 'reset': '.reset-button', + 'clip': '.clip-button', + 'play': '.play-button' + }; + + const keyBinding = new KeyBinding(bindCombinationKeysWithAction, combinationKeysWithSelector); + const eventBinder = new EventBinder('content', keyBinding); + eventBinder.bindKeydown(); +} + +window.addEventListener('load', initializeKeyBinding); diff --git a/zh-CN/book.toml b/zh-CN/book.toml index 6ec0e158..1a411c97 100644 --- a/zh-CN/book.toml +++ b/zh-CN/book.toml @@ -14,7 +14,7 @@ level = 1 [output.html] additional-css = ["theme/style1.css"] -additional-js = ["assets/custom3.js", "assets/lang1.js"] +additional-js = ["assets/custom3.js", "assets/lang1.js","assets/shortcut-key.js"] git-repository-url = "https://github.com/sunface/rust-by-practice" edit-url-template = "https://github.com/sunface/rust-by-practice/edit/master/zh-CN/{path}"