Browse Source

perf: sub selected block

Tienson Qin 3 năm trước cách đây
mục cha
commit
ca7b86f082

+ 1 - 3
src/main/frontend/components/block.cljs

@@ -2353,7 +2353,6 @@
     (when (and
            (state/in-selection-mode?)
            (non-dragging? e))
-      (util/stop e)
       (editor-handler/highlight-selection-area! block-id))))
 
 (defn- block-mouse-leave
@@ -2457,8 +2456,7 @@
         edit? (state/sub [:editor/editing? edit-input-id])
         card? (string/includes? data-refs-self "\"card\"")
         review-cards? (:review-cards? config)
-        selected-blocks (set (state/get-selection-block-ids))
-        selected? (contains? selected-blocks uuid)]
+        selected? (state/sub-block-selected? uuid)]
     [:div.ls-block
      (cond->
        {:id block-id

+ 1 - 1
src/main/frontend/components/content.cljs

@@ -367,7 +367,7 @@
                           (and block-id (parse-uuid block-id))
                           (let [block (.closest target ".ls-block")]
                             (when block
-                              (state/conj-selection-block! block :down))
+                              (util/select-highlight! [block]))
                             (common-handler/show-custom-context-menu!
                             e
                             (block-context-menu-content target (uuid block-id))))

+ 2 - 1
src/main/frontend/handler/editor.cljs

@@ -204,7 +204,8 @@
 
 (defn clear-selection!
   []
-  (state/clear-selection!))
+  (state/clear-selection!)
+  (util/select-unhighlight! (dom/by-class "selected")))
 
 (defn- text-range-by-lst-fst-line [content [direction pos]]
   (case direction

+ 15 - 3
src/main/frontend/state.cljs

@@ -691,13 +691,25 @@
   []
   (:selection/blocks @state))
 
-(defn get-selection-block-ids
-  []
-  (->> (sub :selection/blocks)
+(defn- get-selected-block-ids
+  [blocks]
+  (->> blocks
        (keep #(when-let [id (dom/attr % "blockid")]
                 (uuid id)))
        (distinct)))
 
+(defn get-selection-block-ids
+  []
+  (get-selected-block-ids (get-selection-blocks)))
+
+(defn sub-block-selected?
+  [block-uuid]
+  (rum/react
+   (rum/derived-atom [state] [::select-block block-uuid]
+     (fn [state]
+       (contains? (set (get-selected-block-ids (:selection/blocks state)))
+                  block-uuid)))))
+
 (defn get-selection-start-block-or-first
   []
   (or (get-selection-start-block)

+ 12 - 0
src/main/frontend/util.cljc

@@ -1077,6 +1077,18 @@
 
 (defn keyname [key] (str (namespace key) "/" (name key)))
 
+#?(:cljs
+   (defn select-highlight!
+     [blocks]
+     (doseq [block blocks]
+       (d/add-class! block "selected noselect"))))
+
+#?(:cljs
+   (defn select-unhighlight!
+     [blocks]
+     (doseq [block blocks]
+       (d/remove-class! block "selected" "noselect"))))
+
 (defn batch [in max-time handler buf-atom]
   (async/go-loop [buf buf-atom t (async/timeout max-time)]
     (let [[v p] (async/alts! [in t])]