Explorar o código

perf: remove editor esc-save

Tienson Qin hai 1 ano
pai
achega
c752e2964b

+ 39 - 10
src/main/frontend/components/editor.cljs

@@ -715,6 +715,26 @@
       :else
       nil)))
 
+(defn- editor-on-blur
+  [_e]
+  (cond
+    (contains?
+     #{:commands :block-commands
+       :page-search :page-search-hashtag :block-search :template-search
+       :property-search :property-value-search
+       :datepicker}
+     (state/get-editor-action))
+    (state/clear-editor-action!) ;; FIXME: This should probably be handled as a keydown handler in editor, but this handler intercepts Esc first
+
+         ;; editor/input component handles Escape directly, so just prevent handling it here
+    (= :input (state/get-editor-action))
+    nil
+
+    :else
+    (let [{:keys [on-hide value]} (editor-handler/get-state)]
+      (when on-hide
+        (on-hide value nil)))))
+
 (rum/defcs box < rum/reactive
   {:init (fn [state]
            (assoc state ::id (str (random-uuid))))
@@ -724,19 +744,28 @@
   (mixins/event-mixin setup-key-listener!)
   (shortcut/mixin :shortcut.handler/block-editing-only)
   lifecycle/lifecycle
-  [state {:keys [format block parent-block]} id config]
+  [state {:keys [format block parent-block on-hide]} id config]
   (let [content (state/sub-edit-content (:block/uuid block))
         heading-class (get-editor-style-class block content format)
         opts (cond->
-                 {:id                id
-                  :cacheMeasurements (editor-row-height-unchanged?) ;; check when content updated (as the content variable is binded)
-                  :default-value     (or content "")
-                  :minRows           (if (state/enable-grammarly?) 2 1)
-                  :on-click          (editor-handler/editor-on-click! id)
-                  :on-change         (editor-handler/editor-on-change! block id search-timeout)
-                  :on-paste          (paste-handler/editor-on-paste! id)
-                  :auto-focus        false
-                  :class             heading-class}
+              {:id                id
+               :cacheMeasurements (editor-row-height-unchanged?) ;; check when content updated (as the content variable is binded)
+               :default-value     (or content "")
+               :minRows           (if (state/enable-grammarly?) 2 1)
+               :on-click          (editor-handler/editor-on-click! id)
+               :on-change         (editor-handler/editor-on-change! block id search-timeout)
+               :on-paste          (paste-handler/editor-on-paste! id)
+               :on-blur           (fn [e]
+                                    (if-let [on-blur (:on-blur config)]
+                                      (on-blur e)
+                                      (editor-on-blur e)))
+               :on-key-down       (fn [e]
+                                    (if-let [on-key-down (:on-key-down config)]
+                                      (on-key-down e)
+                                      (when (and (= (util/ekey e) "Escape") on-hide)
+                                        (on-hide content :esc))))
+               :auto-focus        true
+               :class             heading-class}
                (some? parent-block)
                (assoc :parentblockid (str (:block/uuid parent-block)))
 

+ 0 - 38
src/main/frontend/handler/editor/keyboards.cljs

@@ -1,38 +0,0 @@
-(ns ^:no-doc frontend.handler.editor.keyboards
-  (:require [frontend.handler.editor :as editor-handler]
-            [frontend.mixins :as mixins]
-            [frontend.state :as state]
-            [goog.dom :as gdom]))
-
-;; TODO: don't depend on handler.editor
-
-(defn esc-save! [state]
-  (let [id (nth (:rum/args state) 1)]
-    (mixins/hide-when-esc-or-outside
-     state
-     :on-hide
-     (fn [_state e event]
-       (cond
-         (contains?
-          #{:commands :block-commands
-            :page-search :page-search-hashtag :block-search :template-search
-            :property-search :property-value-search
-            :datepicker}
-          (state/get-editor-action))
-         (state/clear-editor-action!) ;; FIXME: This should probably be handled as a keydown handler in editor, but this handler intercepts Esc first
-
-         ;; editor/input component handles Escape directly, so just prevent handling it here
-         (= :input (state/get-editor-action))
-         nil
-
-         (some-> (.-target e)
-                 (.closest ".ls-keep-editing-when-outside-click"))
-         nil
-
-         :else
-         (let [{:keys [on-hide value]} (editor-handler/get-state)]
-           (when on-hide
-             (on-hide value event)))))
-     :node (gdom/getElement id)
-    ;; :visibilitychange? true
-     )))

+ 1 - 5
src/main/frontend/handler/editor/lifecycle.cljs

@@ -1,6 +1,5 @@
 (ns ^:no-doc frontend.handler.editor.lifecycle
   (:require [frontend.handler.editor :as editor-handler]
-            [frontend.handler.editor.keyboards :as keyboards-handler]
             [frontend.state :as state]
             [frontend.util :as util]
             [frontend.util.cursor :as cursor]
@@ -24,10 +23,8 @@
       (when content
         (editor-handler/restore-cursor-pos! id content)))
 
-    (keyboards-handler/esc-save! state)
-
     (when-let [element (gdom/getElement id)]
-      (.focus element)
+      ;; TODO: check whether editor is visible, do less work
       (js/setTimeout #(util/scroll-editor-cursor element) 50)))
   state)
 
@@ -40,7 +37,6 @@
       (when-let [input (state/get-input)]
         (util/set-change-value input
                                (block-handler/sanity-block-content repo (get new-block :block/format :markdown) (:block/content new-block))))))
-  (keyboards-handler/esc-save! state)
   state)
 
 (defn will-unmount