Browse Source

fix: double [[ not trigger auto-complete on Windows with microsoft pinyin

close #3251
Tienson Qin 4 years ago
parent
commit
efbcb79299
2 changed files with 111 additions and 99 deletions
  1. 108 99
      src/main/frontend/handler/editor.cljs
  2. 3 0
      src/main/frontend/util/keycode.cljs

+ 108 - 99
src/main/frontend/handler/editor.cljs

@@ -2762,7 +2762,8 @@
           metaKey (gobj/get e "metaKey")
           is-composing? (gobj/getValueByKeys e "event_" "isComposing")
           pos (cursor/pos input)
-          shift? (.-shiftKey e)]
+          shift? (.-shiftKey e)
+          code (gobj/getValueByKeys e "event_" "code")]
       (cond
         (or is-composing? (= key-code 229))
         nil
@@ -2789,14 +2790,16 @@
         nil
 
         (and (not (string/blank? (util/get-selected-text)))
-             (= key-code keycode/left-square-bracket)
+             (or (= key-code keycode/left-square-bracket)
+                 (= keycode/left-square-bracket-code code))
              (not shift?))
         (do
-          (util/stop e)
-          (autopair input-id "[" format nil))
+          (autopair input-id "[" format nil)
+          (util/stop e))
 
         (and (not (string/blank? (util/get-selected-text)))
-             (= key-code keycode/left-paren)
+             (or (= key-code keycode/left-paren)
+                 (= keycode/left-paren-code code))
              shift?)
         (do
           (util/stop e)
@@ -2843,102 +2846,108 @@
 (defn keyup-handler
   [state input input-id search-timeout]
   (fn [e key-code]
-    (let [k (gobj/get e "key")
-          format (:format (get-state))
-          current-pos (cursor/pos input)
-          value (gobj/get input "value")
-          c (util/nth-safe value (dec current-pos))
-          last-key-code (state/get-last-key-code)
-          blank-selected? (string/blank? (util/get-selected-text))
-          shift? (.-shiftKey e)]
-      (when-not (state/get-editor-show-input)
-        (cond
-          (and (not (contains? #{"ArrowDown" "ArrowLeft" "ArrowRight" "ArrowUp"} k))
-               (not (:editor/show-page-search? @state/state))
-               (not (:editor/show-page-search-hashtag? @state/state))
-               (wrapped-by? input "[[" "]]"))
-          (let [orig-pos (cursor/get-caret-pos input)
-                value (gobj/get input "value")
-                square-pos (string/last-index-of (subs value 0 (:pos orig-pos)) "[[")
-                pos (+ square-pos 2)
-                _ (state/set-last-pos! pos)
-                pos (assoc orig-pos :pos pos)
-                command-step (if (= \# (util/nth-safe value (dec square-pos)))
-                               :editor/search-page-hashtag
-                               :editor/search-page)]
-            (commands/handle-step [command-step])
-            (reset! commands/*slash-caret-pos pos))
-
-          (and blank-selected?
-               (= keycode/left-square-bracket key-code (:key-code last-key-code))
-               (not shift?)
-               (not= k "[")
-               (> current-pos 0))
-          (do
-            (commands/handle-step [:editor/input "[[]]" {:backward-truncate-number 2
-                                                         :backward-pos 2}])
-            (commands/handle-step [:editor/search-page])
-            (reset! commands/*slash-caret-pos (cursor/get-caret-pos input)))
-
-          (and blank-selected?
-               (= keycode/left-paren key-code (:key-code last-key-code))
-               (:shift? last-key-code)
-               shift?
-               (not= k "(")
-               (> current-pos 0))
-          (do
-            (commands/handle-step [:editor/input "(())" {:backward-truncate-number 2
-                                                         :backward-pos 2}])
-            (commands/handle-step [:editor/search-block :reference])
-            (reset! commands/*slash-caret-pos (cursor/get-caret-pos input)))
-
-          (and (= "〈" c)
-               (= "《" (util/nth-safe value (dec (dec current-pos))))
-               (> current-pos 0))
-          (do
-            (commands/handle-step [:editor/input commands/angle-bracket {:last-pattern "《〈"
-                                                                         :backward-pos 0}])
-            (reset! commands/*angle-bracket-caret-pos (cursor/get-caret-pos input))
-            (reset! commands/*show-block-commands true))
-
-          (and (= c " ")
-               (or (= (util/nth-safe value (dec (dec current-pos))) "#")
-                   (not (state/get-editor-show-page-search?))
-                   (and (state/get-editor-show-page-search?)
-                        (not= (util/nth-safe value current-pos) "]"))))
-          (state/set-editor-show-page-search-hashtag! false)
-
-          (and @*show-commands (not= k (state/get-editor-command-trigger)))
-          (let [matched-commands (get-matched-commands input)]
-            (if (seq matched-commands)
+    (let [k (gobj/get e "key")]
+      (when-not (= k "Process")
+        (let [code (gobj/getValueByKeys e "event_" "code")
+              format (:format (get-state))
+              current-pos (cursor/pos input)
+              value (gobj/get input "value")
+              c (util/nth-safe value (dec current-pos))
+              last-key-code (state/get-last-key-code)
+              blank-selected? (string/blank? (util/get-selected-text))
+              shift? (.-shiftKey e)]
+          (when-not (state/get-editor-show-input)
+            (cond
+              (and (not (contains? #{"ArrowDown" "ArrowLeft" "ArrowRight" "ArrowUp"} k))
+                   (not (:editor/show-page-search? @state/state))
+                   (not (:editor/show-page-search-hashtag? @state/state))
+                   (wrapped-by? input "[[" "]]"))
+              (let [orig-pos (cursor/get-caret-pos input)
+                    value (gobj/get input "value")
+                    square-pos (string/last-index-of (subs value 0 (:pos orig-pos)) "[[")
+                    pos (+ square-pos 2)
+                    _ (state/set-last-pos! pos)
+                    pos (assoc orig-pos :pos pos)
+                    command-step (if (= \# (util/nth-safe value (dec square-pos)))
+                                   :editor/search-page-hashtag
+                                   :editor/search-page)]
+                (commands/handle-step [command-step])
+                (reset! commands/*slash-caret-pos pos))
+
+              (and blank-selected?
+                   (or (= keycode/left-square-bracket key-code (:key-code last-key-code))
+                       (= keycode/left-square-bracket-code code (:code last-key-code)))
+                   (not shift?)
+                   (> current-pos 0)
+                   (not (wrapped-by? input "[[" "]]")))
               (do
-                (reset! *show-commands true)
-                (reset! commands/*matched-commands matched-commands))
-              (reset! *show-commands false)))
-
-          (and @*show-block-commands (not= key-code 188)) ; not <
-          (let [matched-block-commands (get-matched-block-commands input)]
-            (if (seq matched-block-commands)
-              (cond
-                (= key-code 9)       ;tab
-                (when @*show-block-commands
-                  (util/stop e)
-                  (insert-command! input-id
-                                   (last (first matched-block-commands))
-                                   format
-                                   {:last-pattern commands/angle-bracket}))
-
-                :else
-                (reset! commands/*matched-block-commands matched-block-commands))
-              (reset! *show-block-commands false)))
-
-          (nil? @search-timeout)
-          (close-autocomplete-if-outside input)
+                (commands/handle-step [:editor/input "[[]]" {:backward-truncate-number 2
+                                                             :backward-pos 2}])
+                (commands/handle-step [:editor/search-page])
+                (reset! commands/*slash-caret-pos (cursor/get-caret-pos input)))
+
+              (and blank-selected?
+                   (or (= keycode/left-paren key-code (:key-code last-key-code))
+                       (= keycode/left-paren-code code (:code last-key-code)))
+                   (:shift? last-key-code)
+                   shift?
+                   (> current-pos 0)
+                   (not (wrapped-by? input "((" "))")))
+              (do
+                (commands/handle-step [:editor/input "(())" {:backward-truncate-number 2
+                                                             :backward-pos 2}])
+                (commands/handle-step [:editor/search-block :reference])
+                (reset! commands/*slash-caret-pos (cursor/get-caret-pos input)))
+
+              (and (= "〈" c)
+                   (= "《" (util/nth-safe value (dec (dec current-pos))))
+                   (> current-pos 0))
+              (do
+                (commands/handle-step [:editor/input commands/angle-bracket {:last-pattern "《〈"
+                                                                             :backward-pos 0}])
+                (reset! commands/*angle-bracket-caret-pos (cursor/get-caret-pos input))
+                (reset! commands/*show-block-commands true))
+
+              (and (= c " ")
+                   (or (= (util/nth-safe value (dec (dec current-pos))) "#")
+                       (not (state/get-editor-show-page-search?))
+                       (and (state/get-editor-show-page-search?)
+                            (not= (util/nth-safe value current-pos) "]"))))
+              (state/set-editor-show-page-search-hashtag! false)
+
+              (and @*show-commands (not= k (state/get-editor-command-trigger)))
+              (let [matched-commands (get-matched-commands input)]
+                (if (seq matched-commands)
+                  (do
+                    (reset! *show-commands true)
+                    (reset! commands/*matched-commands matched-commands))
+                  (reset! *show-commands false)))
+
+              (and @*show-block-commands (not= key-code 188)) ; not <
+              (let [matched-block-commands (get-matched-block-commands input)]
+                (if (seq matched-block-commands)
+                  (cond
+                    (= key-code 9)       ;tab
+                    (when @*show-block-commands
+                      (util/stop e)
+                      (insert-command! input-id
+                                       (last (first matched-block-commands))
+                                       format
+                                       {:last-pattern commands/angle-bracket}))
+
+                    :else
+                    (reset! commands/*matched-block-commands matched-block-commands))
+                  (reset! *show-block-commands false)))
+
+              (nil? @search-timeout)
+              (close-autocomplete-if-outside input)
 
-          :else
-          nil)))
-    (state/set-last-key-code! {:key-code key-code
-                               :shift? (.-shiftKey e)})))
+              :else
+              nil))
+          (when-not (= k "Shift")
+            (state/set-last-key-code! {:key-code key-code
+                                       :code code
+                                       :shift? (.-shiftKey e)})))))))
 
 (defn editor-on-click!
   [id]

+ 3 - 0
src/main/frontend/util/keycode.cljs

@@ -2,3 +2,6 @@
 
 (def left-square-bracket 219)
 (def left-paren 57)
+
+(def left-square-bracket-code "BracketLeft")
+(def left-paren-code "Digit9")