Browse Source

enhance(shortcut): support intuitional keyboard chars for binding key

charlie 3 years ago
parent
commit
7a7dbd5e68

+ 6 - 0
libs/src/LSPlugin.ts

@@ -308,6 +308,12 @@ export interface IAppProxy {
     action: SimpleCommandCallback
   ) => void
 
+  /**
+   * Supported key names
+   * @link https://gist.github.com/xyhp915/d1a6d151a99f31647a95e59cdfbf4ddc
+   * @param keybinding
+   * @param action
+   */
   registerCommandShortcut: (
     keybinding: SimpleCommandKeybinding,
     action: SimpleCommandCallback

+ 2 - 2
src/main/frontend/modules/shortcut/core.cljs

@@ -66,7 +66,7 @@
          (doseq [k (dh/shortcut-binding id)]
            (try
              (log/debug :shortcut/register-shortcut {:id id :binding k})
-             (.registerShortcut handler (util/keyname id) k)
+             (.registerShortcut handler (util/keyname id) (dh/normalize-user-keyname k))
              (catch js/Object e
                (log/error :shortcut/register-shortcut {:id      id
                                                        :binding k
@@ -81,7 +81,7 @@
   (when-let [handler (get-handler-by-id handler-id)]
     (when-let [ks (dh/shortcut-binding shortcut-id)]
       (doseq [k ks]
-        (.unregisterShortcut ^js handler k)))
+        (.unregisterShortcut ^js handler (dh/normalize-user-keyname k))))
     (shortcut-config/remove-shortcut! handler-id shortcut-id)))
 
 (defn uninstall-shortcut!

+ 11 - 0
src/main/frontend/modules/shortcut/data_helper.cljs

@@ -44,6 +44,17 @@
          shortcut)
        (mapv mod-key)))))
 
+(defn normalize-user-keyname
+  [k]
+  (some-> k
+          (util/safe-lower-case)
+          (str/replace #";+" "semicolon")
+          (str/replace #"=+" "equals")
+          (str/replace #"~+" "dash")
+          (str/replace "[" "open-square-bracket")
+          (str/replace "]" "close-square-bracket")
+          (str/replace "'" "single-quote")))
+
 ;; returns a vector to preserve order
 (defn binding-by-category [name]
   (let [dict (->> (vals @config/config)