Przeglądaj źródła

fix: code block e2e test

Tienson Qin 1 rok temu
rodzic
commit
732b87a63f

+ 11 - 9
src/main/frontend/components/block.cljs

@@ -2226,9 +2226,11 @@
                               {:ref ref
                                :move-cursor? false}))]
                    ;; wait a while for the value of the caret range
-                    (if (util/ios?)
-                      (f)
-                      (js/setTimeout f 5))
+                    (p/do!
+                     (state/pub-event! [:editor/save-code-editor])
+                     (if (util/ios?)
+                       (f)
+                       (js/setTimeout f 5)))
 
                     (when block-id (state/set-selection-start-block! block-id))))))))))))
 
@@ -2492,12 +2494,12 @@
                                       :block-parent-id block-id
                                       :format format
                                       :on-hide (fn [value event]
-                                                 (when (= event :esc)
-                                                   (p/let [_ (editor-handler/save-block! (editor-handler/get-state) value)]
-                                                     (let [select? (not (string/includes? value "```"))]
-                                                       (editor-handler/escape-editing select?)
-                                                       (when (contains? #{:esc :visibilitychange :click} event)
-                                                         (state/clear-edit!))))))}
+                                                 (p/let [_ (editor-handler/save-block! (editor-handler/get-state) value)]
+                                                   (let [select? (and (= event :esc)
+                                                                      (not (string/includes? value "```")))]
+                                                     (editor-handler/escape-editing select?)
+                                                     (when (contains? #{:esc :visibilitychange :click} event)
+                                                       (state/clear-edit!)))))}
                                      edit-input-id
                                      config))]
           (if (and named? (seq (:block/tags block)) db-based?)

+ 14 - 8
src/main/frontend/extensions/code.cljs

@@ -140,7 +140,8 @@
             [frontend.schema.handler.common-config :refer [Config-edn]]
             [malli.util :as mu]
             [malli.core :as m]
-            [rum.core :as rum]))
+            [rum.core :as rum]
+            [promesa.core :as p]))
 
 ;; codemirror
 
@@ -373,6 +374,14 @@
          (.-mime cm-mode)
          mode)))))
 
+(defn- save-editor!
+  [config]
+  (p/do!
+   (code-handler/save-code-editor!)
+   (when-let [block-id (:block/uuid config)]
+     (let [block (db/pull [:block/uuid block-id])]
+       (editor-handler/edit-block! block :max block-id)))))
+
 (defn render!
   [state]
   (let [[config id attr _code theme user-options] (:rum/args state)
@@ -388,7 +397,7 @@
         config-edit? (and (:file? config) (string/ends-with? (:file-path config) "config.edn"))
         textarea (gdom/getElement id)
         radix-color (state/sub :ui/radix-color)
-        default-cm-options {:theme (if radix-color 
+        default-cm-options {:theme (if radix-color
                                      (str "lsradix " theme)
                                      (str "solarized " theme))
                             :autoCloseBrackets true
@@ -400,12 +409,9 @@
                           {:mode mode
                            :tabIndex -1 ;; do not accept TAB-in, since TAB is bind globally
                            :extraKeys (merge {"Esc" (fn [cm]
-                                                   ;; Avoid reentrancy
+                                                      ;; Avoid reentrancy
                                                       (gobj/set cm "escPressed" true)
-                                                      (code-handler/save-code-editor!)
-                                                      (when-let [block-id (:block/uuid config)]
-                                                        (let [block (db/pull [:block/uuid block-id])]
-                                                          (editor-handler/edit-block! block :max block-id))))}
+                                                      (save-editor! config))}
                                              (when config-edit?
                                                {"':'" complete-after
                                                 "Ctrl-Space" "autocomplete"}))}
@@ -493,7 +499,7 @@
                  (let [next-theme (get-theme!)
                        last-theme @(:last-theme state)
                        editor (some-> state :editor-atom deref)]
-                   (when (and editor (not= next-theme last-theme)) 
+                   (when (and editor (not= next-theme last-theme))
                      (reset! (:last-theme state) next-theme)
                      (.setOption editor "theme" next-theme)))
                  (reset! (:code-options state) (last (:rum/args state)))

+ 34 - 31
src/main/frontend/handler/block.cljs

@@ -18,7 +18,8 @@
    [frontend.handler.file-based.property.util :as property-util]
    [frontend.handler.property.util :as pu]
    [dommy.core :as dom]
-   [goog.object :as gobj]))
+   [goog.object :as gobj]
+   [promesa.core :as p]))
 
 ;;  Fns
 
@@ -228,36 +229,38 @@
                            :or {tail-len 0}
                            :as opts}]
   (when-not config/publishing?
-    (when-let [block-id (:block/uuid block)]
-      (let [repo (state/get-current-repo)
-            block-node (cond
-                         (uuid? block-node)
-                         nil
-                         (string? block-node)
-                         (gdom/getElement (string/replace block-node "edit-block" "ls-block"))
-                         :else
-                         block-node)
-            db-graph? (config/db-based-graph? repo)
-            block (or (db/entity [:block/uuid block-id]) block)
-            content (if (and db-graph? (:block/name block))
-                      (:block/original-name block)
-                      (or custom-content (:block/content block) ""))
-            content-length (count content)
-            text-range (cond
-                         (vector? pos)
-                         (text-range-by-lst-fst-line content pos)
-
-                         (and (> tail-len 0) (>= (count content) tail-len))
-                         (subs content 0 (- (count content) tail-len))
-
-                         (or (= :max pos) (<= content-length pos))
-                         content
-
-                         :else
-                         (subs content 0 pos))
-            content (sanity-block-content repo (:block/format block) content)]
-        (state/clear-selection!)
-        (edit-block-aux repo block content block-node text-range opts)))))
+    (p/do!
+     (state/pub-event! [:editor/save-code-editor])
+     (when-let [block-id (:block/uuid block)]
+       (let [repo (state/get-current-repo)
+             block-node (cond
+                          (uuid? block-node)
+                          nil
+                          (string? block-node)
+                          (gdom/getElement (string/replace block-node "edit-block" "ls-block"))
+                          :else
+                          block-node)
+             db-graph? (config/db-based-graph? repo)
+             block (or (db/entity [:block/uuid block-id]) block)
+             content (if (and db-graph? (:block/name block))
+                       (:block/original-name block)
+                       (or custom-content (:block/content block) ""))
+             content-length (count content)
+             text-range (cond
+                          (vector? pos)
+                          (text-range-by-lst-fst-line content pos)
+
+                          (and (> tail-len 0) (>= (count content) tail-len))
+                          (subs content 0 (- (count content) tail-len))
+
+                          (or (= :max pos) (<= content-length pos))
+                          content
+
+                          :else
+                          (subs content 0 pos))
+             content (sanity-block-content repo (:block/format block) content)]
+         (state/clear-selection!)
+         (edit-block-aux repo block content block-node text-range opts))))))
 
 (defn- get-original-block-by-dom
   [node]

+ 4 - 0
src/main/frontend/handler/events.cljs

@@ -56,6 +56,7 @@
             [frontend.handler.property :as property-handler]
             [frontend.handler.whiteboard :as whiteboard-handler]
             [frontend.handler.web.nfs :as nfs-handler]
+            [frontend.handler.code :as code-handler]
             [frontend.mobile.core :as mobile]
             [frontend.mobile.graph-picker :as graph-picker]
             [frontend.mobile.util :as mobile-util]
@@ -915,6 +916,9 @@
   (when (some-> block (editor-handler/own-order-number-list?))
     (editor-handler/remove-block-own-order-list-type! block)))
 
+(defmethod handle :editor/save-code-editor [_]
+  (code-handler/save-code-editor!))
+
 (defmethod handle :editor/toggle-children-number-list [[_ block]]
   (when-let [blocks (and block (db-model/get-block-immediate-children (state/get-current-repo) (:block/uuid block)))]
     (editor-handler/toggle-blocks-as-own-order-list! blocks)))