Browse Source

fix: can't insert child for the root block

Tienson Qin 2 năm trước cách đây
mục cha
commit
f2db3a3caa
1 tập tin đã thay đổi với 25 bổ sung50 xóa
  1. 25 50
      src/main/frontend/handler/block.cljs

+ 25 - 50
src/main/frontend/handler/block.cljs

@@ -308,41 +308,6 @@
                   :own-order-list-index own-order-list-index
                   :own-order-list-index own-order-list-index
                   :own-order-number-list? (= own-order-list-type "number"))))
                   :own-order-number-list? (= own-order-list-type "number"))))
 
 
-(defn- check-node?
-  [node block-id]
-  (= block-id (dom/attr node "blockid")))
-
-(defn- get-blocks-by-id
-  [node block-id]
-  (->> (dom/by-class node "ls-block")
-       (filter #(check-node? % block-id))
-       (util/sort-by-height)))
-
-(defn get-nearby-block-by-id
-  [block-id direction]
-  (when block-id
-    (let [block-id (str block-id)]
-      (when (util/uuid-string? block-id)
-        (when-let [e (state/get-input)]
-          (when-let [current-block ^js (or (dom/closest e ".ls-block") e)]
-            (let [near-by (if (= direction :down)
-                            (let [next (.-nextSibling current-block)]
-                              (if (and next (check-node? next block-id))
-                                next
-                                (first (get-blocks-by-id current-block block-id))))
-                            (let [prev (or (.-previousSibling current-block)
-                                           (util/rec-get-node (gobj/get current-block "parentNode") "ls-block"))]
-                              (if (and prev (check-node? prev block-id))
-                                prev
-                                (when prev (last (get-blocks-by-id prev block-id))))))]
-              (when (and near-by (check-node? near-by block-id))
-                near-by))))))))
-
-(defn- get-block-node-by-id
-  [block-id direction]
-  (or (get-nearby-block-by-id block-id direction)
-      (util/get-first-block-by-id block-id)))
-
 (defn- text-range-by-lst-fst-line [content [direction pos]]
 (defn- text-range-by-lst-fst-line [content [direction pos]]
   (case direction
   (case direction
     :up
     :up
@@ -365,33 +330,43 @@
   [repo block content block-node text-range {:keys [direction retry-times]
   [repo block content block-node text-range {:keys [direction retry-times]
                                              :or {retry-times 0}
                                              :or {retry-times 0}
                                              :as opts}]
                                              :as opts}]
-  (when (and (<= retry-times 10) block)
+  (when (and (<= retry-times 3) block)
     (let [block-node block-node
     (let [block-node block-node
           block-id (:block/uuid block)
           block-id (:block/uuid block)
           id-class (str "id" block-id)
           id-class (str "id" block-id)
-          next-edit-node (or
-                          (when (and direction block-node)
-                            (let [blocks (dom/by-class "ls-block")
-                                  idx (.indexOf blocks block-node)]
-                              (when idx
-                                (if (= direction :down)
-                                  (util/nth-safe blocks (inc idx))
-                                  (util/nth-safe blocks (dec idx))))))
-                          (when block-node
+          next-edit-node (if block-node
+                           (or
+                            ;; up/down
+                            (when direction
+                              (let [blocks (dom/by-class "ls-block")
+                                    idx (.indexOf blocks block-node)]
+                                (when idx
+                                  (if (= direction :down)
+                                    (util/nth-safe blocks (inc idx))
+                                    (util/nth-safe blocks (dec idx))))))
+
+                            ;; next | next then first child
                             (when-let [next (.-nextSibling block-node)]
                             (when-let [next (.-nextSibling block-node)]
                               (when (dom/has-class? next id-class)
                               (when (dom/has-class? next id-class)
-                                next)))
-                          (when block-node
+                                next))
+
+                            ;; first child
+                            (when-let [first-child (first (dom/sel block-node ".ls-block"))]
+                              (when (dom/has-class? first-child id-class)
+                                first-child))
+
+                            ;; prev
                             (when-let [prev (.-previousSibling block-node)]
                             (when-let [prev (.-previousSibling block-node)]
                               (when (dom/has-class? prev id-class)
                               (when (dom/has-class? prev id-class)
                                 prev)))
                                 prev)))
-                          (when-not block-node
-                            (get-block-node-by-id (:block/uuid block) direction)))]
+
+                           ;; take the first dom node
+                           (gdom/getElement (str "ls-block-" (:block/uuid block))))]
       (if next-edit-node
       (if next-edit-node
         (do
         (do
           (state/set-editing! "" content block text-range {:ref next-edit-node})
           (state/set-editing! "" content block text-range {:ref next-edit-node})
           (mark-last-input-time! repo))
           (mark-last-input-time! repo))
-        (js/setTimeout (fn [] (edit-block-aux repo block content block-node text-range (update opts :retry-times inc))) 5)))))
+        (util/schedule (fn [] (edit-block-aux repo block content block-node text-range (update opts :retry-times inc))))))))
 
 
 (defn edit-block!
 (defn edit-block!
   [block pos block-node & {:keys [custom-content tail-len _direction]
   [block pos block-node & {:keys [custom-content tail-len _direction]