Browse Source

refactor(editor): fix on-key-down mixin

Add both explicit options:
1. :not-match-handler
2. :all-handler
Tienson Qin 5 years ago
parent
commit
8b165c89a5

+ 78 - 92
src/main/frontend/components/editor.cljs

@@ -270,8 +270,7 @@
                       {:keys [pos]} @*slash-caret-pos
                       command (:command (first input-option))]
                   (on-submit command @input-value pos))
-                (reset! input-value nil))))}
-      nil)))
+                (reset! input-value nil))))})))
   {:did-update
    (fn [state]
      (when-let [show-input (state/get-editor-show-input)]
@@ -355,9 +354,6 @@
         false
         *slash-caret-pos)))])
 
-(defonce evt-passthrough! #(if (instance? goog.events.Event %) (set! (. % -pt) true)))
-(defonce evt-passthrough? #(if (instance? goog.events.Event %) (. % -pt)))
-
 (rum/defcs box < rum/reactive
   (mixins/event-mixin
    (fn [state]
@@ -400,7 +396,6 @@
                                  insert?
                                  (not (editor-handler/in-auto-complete? input)))
                             (util/stop e)
-                            (evt-passthrough! e)
                             (profile
                              "Insert block"
                              (editor-handler/insert-new-block! state))))))))))
@@ -410,7 +405,6 @@
                      (not (gobj/get e "ctrlKey"))
                      (not (gobj/get e "metaKey"))
                      (not (editor-handler/in-auto-complete? input)))
-                (evt-passthrough! e)
                 (editor-handler/on-up-down state e true)))
          ;; down
          40 (fn [state e]
@@ -418,7 +412,6 @@
                      (not (gobj/get e "ctrlKey"))
                      (not (gobj/get e "metaKey"))
                      (not (editor-handler/in-auto-complete? input)))
-                (evt-passthrough! e)
                 (editor-handler/on-up-down state e false)))
          ;; backspace
          8  (fn [state e]
@@ -440,9 +433,7 @@
                        (not (and page
                                  (util/uuid-string? page)
                                  (= (medley/uuid page) block-id))))
-                  (do
-                    (evt-passthrough! e)
-                    (editor-handler/delete-block! state repo e))
+                  (editor-handler/delete-block! state repo e)
 
                   (and (> current-pos 1)
                        (= (util/nth-safe value (dec current-pos)) commands/slash))
@@ -499,84 +490,82 @@
                       (and input pos (js/setTimeout #(when-let [input (gdom/getElement input-id)]
                                                        (util/move-cursor-to input pos))
                                                     0)))))))}
-        (fn [e key-code]
-          (when-not (evt-passthrough? e)
-            (let [key (gobj/get e "key")
-                  value (gobj/get input "value")
-                  pos (:pos (util/get-caret-pos input))]
-              (cond
-                (or
-                 (and (= key "#")
-                      (and
-                       (> pos 0)
-                       (= "#" (util/nth-safe value (dec pos)))))
-                 (and (= key " ")
-                      (state/get-editor-show-page-search-hashtag?)))
-                (state/set-editor-show-page-search-hashtag! false)
-
-                (and
-                 (not= key-code 8)                         ;; backspace
-                 (or
-                  (editor-handler/surround-by? input "#" " ")
-                  (editor-handler/surround-by? input "#" :end)
-                  (= key "#")))
-                (do
-                  (commands/handle-step [:editor/search-page-hashtag])
-                  (state/set-last-pos! (:pos (util/get-caret-pos input)))
-                  (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
-
-                (and
-                 (= key " ")
-                 (state/get-editor-show-page-search-hashtag?))
-                (state/set-editor-show-page-search-hashtag! false)
-
-                (and
-                 (contains? (set/difference (set (keys editor-handler/reversed-autopair-map))
-                                            #{"`"})
-                            key)
-                 (= (editor-handler/get-current-input-char input) key))
-                (do
-                  (util/stop e)
-                  (util/cursor-move-forward input 1))
-
-                (contains? (set (keys editor-handler/autopair-map)) key)
-                (do
-                  (util/stop e)
-                  (editor-handler/autopair input-id key format nil)
-                  (cond
-                    (editor-handler/surround-by? input "[[" "]]")
-                    (do
-                      (commands/handle-step [:editor/search-page])
-                      (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
-                    (editor-handler/surround-by? input "((" "))")
-                    (do
-                      (commands/handle-step [:editor/search-block :reference])
-                      (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
-                    :else
-                    nil))
-
-                (let [sym "$"]
-                  (and (= key sym)
-                       (>= (count value) 1)
-                       (> pos 0)
-                       (= (nth value (dec pos)) sym)
-                       (if (> (count value) pos)
-                         (not= (nth value pos) sym)
-                         true)))
-                (commands/simple-insert! input-id "$$" {:backward-pos 2})
-
-                (let [sym "^"]
-                  (and (= key sym)
-                       (>= (count value) 1)
-                       (> pos 0)
-                       (= (nth value (dec pos)) sym)
-                       (if (> (count value) pos)
-                         (not= (nth value pos) sym)
-                         true)))
-                (commands/simple-insert! input-id "^^" {:backward-pos 2})
-
-                :else
-                nil)))))
+        {:not-matched-handler
+         (fn [e key-code]
+           (let [key (gobj/get e "key")
+                 value (gobj/get input "value")
+                 pos (:pos (util/get-caret-pos input))]
+             (cond
+               (or
+                (and (= key "#")
+                     (and
+                      (> pos 0)
+                      (= "#" (util/nth-safe value (dec pos)))))
+                (and (= key " ")
+                     (state/get-editor-show-page-search-hashtag?)))
+               (state/set-editor-show-page-search-hashtag! false)
+
+               (or
+                (editor-handler/surround-by? input "#" " ")
+                (editor-handler/surround-by? input "#" :end)
+                (= key "#"))
+               (do
+                 (commands/handle-step [:editor/search-page-hashtag])
+                 (state/set-last-pos! (:pos (util/get-caret-pos input)))
+                 (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
+
+               (and
+                (= key " ")
+                (state/get-editor-show-page-search-hashtag?))
+               (state/set-editor-show-page-search-hashtag! false)
+
+               (and
+                (contains? (set/difference (set (keys editor-handler/reversed-autopair-map))
+                                           #{"`"})
+                           key)
+                (= (editor-handler/get-current-input-char input) key))
+               (do
+                 (util/stop e)
+                 (util/cursor-move-forward input 1))
+
+               (contains? (set (keys editor-handler/autopair-map)) key)
+               (do
+                 (util/stop e)
+                 (editor-handler/autopair input-id key format nil)
+                 (cond
+                   (editor-handler/surround-by? input "[[" "]]")
+                   (do
+                     (commands/handle-step [:editor/search-page])
+                     (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
+                   (editor-handler/surround-by? input "((" "))")
+                   (do
+                     (commands/handle-step [:editor/search-block :reference])
+                     (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
+                   :else
+                   nil))
+
+               (let [sym "$"]
+                 (and (= key sym)
+                      (>= (count value) 1)
+                      (> pos 0)
+                      (= (nth value (dec pos)) sym)
+                      (if (> (count value) pos)
+                        (not= (nth value pos) sym)
+                        true)))
+               (commands/simple-insert! input-id "$$" {:backward-pos 2})
+
+               (let [sym "^"]
+                 (and (= key sym)
+                      (>= (count value) 1)
+                      (> pos 0)
+                      (= (nth value (dec pos)) sym)
+                      (if (> (count value) pos)
+                        (not= (nth value pos) sym)
+                        true)))
+               (commands/simple-insert! input-id "^^" {:backward-pos 2})
+
+               :else
+               nil)))})
        (mixins/on-key-up
         state
         {}
@@ -706,9 +695,6 @@
                          current-pos (:pos (util/get-caret-pos input))]
                      (state/set-edit-pos! current-pos)
                      (editor-handler/close-autocomplete-if-outside input)))
-       ;:on-key-down (fn [_e]
-       ;               (let [current-pos (:pos (util/get-caret-pos (gdom/getElement id)))]
-       ;                 (state/set-edit-pos! current-pos)))
        :on-change (fn [e]
                     (let [value (util/evalue e)
                           current-pos (:pos (util/get-caret-pos (gdom/getElement id)))]

+ 2 - 4
src/main/frontend/components/sidebar.cljs

@@ -294,9 +294,7 @@
               (when-let [repo-url (state/get-current-repo)]
                 (when-not (state/get-edit-input-id)
                   (util/stop e)
-                  (state/set-modal! commit/add-commit-message)))))}
-      (fn [e key-code]
-        nil))))
+                  (state/set-modal! commit/add-commit-message)))))})))
   {:did-mount (fn [state]
                 (keyboards/bind-shortcuts!)
                 state)}
@@ -359,4 +357,4 @@
          ;;   :on-click (fn []
          ;;               (state/set-left-sidebar-open! (not (state/get-left-sidebar-open?))))}
          ;;  (if (state/sub :ui/left-sidebar-open?) "<" ">")]
-         )])))
+)])))

+ 13 - 8
src/main/frontend/mixins.cljs

@@ -102,14 +102,19 @@
                 (when all-handler (all-handler e key-code)))))))
 
 (defn on-key-down
-  [state keycode-map all-handler]
-  (let [node (rum/dom-node state)]
-    (listen state js/window "keydown"
-            (fn [e]
-              (let [key-code (.-keyCode e)]
-                (when-let [f (get keycode-map key-code)]
-                  (f state e))
-                (when all-handler (all-handler e key-code)))))))
+  ([state keycode-map]
+   (on-key-down state keycode-map {}))
+  ([state keycode-map {:keys [not-matched-handler all-handler]}]
+   (let [node (rum/dom-node state)]
+     (listen state js/window "keydown"
+             (fn [e]
+               (let [key-code (.-keyCode e)]
+                 (if-let [f (get keycode-map key-code)]
+                   (f state e)
+                   (when (and not-matched-handler (fn? not-matched-handler))
+                     (not-matched-handler e key-code)))
+                 (when (and all-handler (fn? all-handler))
+                   (all-handler e key-code))))))))
 
 (defn event-mixin
   ([attach-listeners]

+ 1 - 2
src/main/frontend/ui.cljs

@@ -336,8 +336,7 @@
                          (> (count matched)
                             @current-idx))
                   (on-chosen (nth matched @current-idx) false)
-                  (and on-enter (on-enter state))))))}
-      nil)))
+                  (and on-enter (on-enter state))))))})))
   [state matched {:keys [on-chosen
                          on-shift-chosen
                          on-enter

+ 17 - 17
src/main/frontend/ui/date_picker.cljs

@@ -183,30 +183,30 @@
    (fn [state]
      (let [{:keys [on-change on-switch deadline-or-schedule?]} (last (:rum/args state))]
        (mixins/on-key-down
-       state
-       {;; enter, current day
-        13 (fn [state e]
-             (when on-change
-               (when-not deadline-or-schedule?
-                 (on-change e @*internal-model))))
+        state
+        {;; enter, current day
+         13 (fn [state e]
+              (when on-change
+                (when-not deadline-or-schedule?
+                  (on-change e @*internal-model))))
 
         ;; left, previous day
-        37 (fn [state e]
-             (swap! *internal-model inc-date -1))
+         37 (fn [state e]
+              (swap! *internal-model inc-date -1))
 
         ;; right, next day
-        39 (fn [state e]
-             (swap! *internal-model inc-date 1))
+         39 (fn [state e]
+              (swap! *internal-model inc-date 1))
 
         ;; up, one week ago
-        38 (fn [state e]
-             (swap! *internal-model inc-week -1))
+         38 (fn [state e]
+              (swap! *internal-model inc-week -1))
         ;; down, next week
-        40 (fn [state e]
-             (swap! *internal-model inc-week 1))}
-       (fn [e key-code]
-         (when (contains? #{13 37 38 39 40} key-code)
-           (util/stop e)))))))
+         40 (fn [state e]
+              (swap! *internal-model inc-week 1))}
+        {:all-handler (fn [e key-code]
+                        (when (contains? #{13 37 38 39 40} key-code)
+                          (util/stop e)))}))))
   {:init (fn [state]
            (reset! *internal-model (first (:rum/args state)))
            state)}