Browse Source

Merge branch 'feat/db' into feat/datascript-storage

Tienson Qin 2 years ago
parent
commit
0a393eb822

+ 40 - 2
src/main/frontend/db/model.cljs

@@ -568,10 +568,10 @@ independent of format as format specific heading characters are stripped"
           '[:db/id :block/collapsed? :block/properties {:block/parent ...}]
           [:block/uuid block-id]))
 
-(defn get-block-last-direct-child
+(defn get-block-last-direct-child-id
   "Notice: if `not-collapsed?` is true, will skip searching for any collapsed block."
   ([db db-id]
-   (get-block-last-direct-child db db-id false))
+   (get-block-last-direct-child-id db db-id false))
   ([db db-id not-collapsed?]
    (when-let [block (db-utils/entity db db-id)]
      (when (if not-collapsed?
@@ -582,6 +582,16 @@ independent of format as format specific heading characters are stripped"
              all-ids (set (map :db/id children))]
          (first (set/difference all-ids all-left)))))))
 
+(defn get-block-deep-last-open-child-id
+  [db db-id]
+  (loop [node (db-utils/entity db db-id)]
+    (if-let [last-child-id (get-block-last-direct-child-id db (:db/id node) true)]
+      (let [e (db-utils/entity db last-child-id)]
+        (if (or (:block/collapsed? e) (empty? (:block/_parent e)))
+          last-child-id
+          (recur e)))
+      nil)))
+
 (defn get-prev-sibling
   [db id]
   (when-let [e (db-utils/entity db id)]
@@ -596,6 +606,34 @@ independent of format as format specific heading characters are stripped"
                           (:db/id (:block/parent block))
                           db-id)))
 
+(defn get-next
+  "Get next block, either its right sibling, or loop to find its next block."
+  [db db-id & {:keys [skip-collapsed? init?]
+               :or {skip-collapsed? true
+                    init? true}
+               :as opts}]
+  (when-let [entity (db-utils/entity db db-id)]
+    (or (when-not (and (:block/collapsed? entity) skip-collapsed? init?)
+          (get-right-sibling db db-id))
+        (let [parent-id (:db/id (:block/parent (db-utils/entity db db-id)))]
+          (get-next db parent-id (assoc opts :init? false))))))
+
+(defn get-prev
+  "Get prev block, either its left sibling if the sibling is collapsed or no children,
+  or get sibling's last deep displayable child (collaspsed parent or non-collapsed child)."
+  [db db-id]
+  (when-let [entity (db-utils/entity db db-id)]
+    (or
+     (when-let [prev-sibling (get-prev-sibling db db-id)]
+       (if (or (:block/collapsed? prev-sibling)
+               (empty? (:block/_parent prev-sibling)))
+         prev-sibling
+         (some->> (get-block-deep-last-open-child-id db (:db/id prev-sibling))
+                  (db-utils/entity db))))
+     (let [parent (:block/parent entity)]
+       (when-not (:block/name parent)
+         parent)))))
+
 (defn last-child-block?
   "The child block could be collapsed."
   [db parent-id child-id]

+ 23 - 9
src/main/frontend/db/rtc/core.cljs

@@ -142,7 +142,7 @@
         (prn :apply-remote-remove-ops (:block-uuid op))))))
 
 (defn- insert-or-move-block
-  [repo block-uuid remote-parents remote-left-uuid move?]
+  [repo block-uuid remote-parents remote-left-uuid move? op-value]
   (when (and (seq remote-parents) remote-left-uuid)
     (let [first-remote-parent (first remote-parents)
           local-parent (db/pull repo '[*] [:block/uuid first-remote-parent])
@@ -179,10 +179,22 @@
                           :block/format :markdown}]
                         local-parent {:sibling? false :keep-uuid? true}))
 
-        ([true true false] [true true true])
         ;; Don't need to insert-whiteboard-block here,
         ;; will do :upsert-whiteboard-block in `update-block-attrs`
-        nil
+        [true true false]
+        (when (nil? (:properties op-value))
+          ;; when :properties is nil, this block should be treat as normal block
+          (if move?
+            (transact-db! :move-blocks [b] local-parent false)
+            (transact-db! :insert-blocks [{:block/uuid block-uuid :block/content "" :block/format :markdown}]
+                          local-parent {:sibling? false :keep-uuid? true})))
+        [true true true]
+        (when (nil? (:properties op-value))
+          (let [sibling? (not= (:block/uuid local-parent) (:block/uuid local-left))]
+            (if move?
+              (transact-db! :move-blocks [b] local-left sibling?)
+              (transact-db! :insert-blocks [{:block/uuid block-uuid :block/content "" :block/format :markdown}]
+                            local-left {:sibling? sibling? :keep-uuid? true}))))
 
         (throw (ex-info "Don't know where to insert" {:block-uuid block-uuid :remote-parents remote-parents
                                                       :remote-left remote-left-uuid}))))))
@@ -247,7 +259,7 @@
         (transact-db! :upsert-whiteboard-block repo [(whiteboard-handler/shape->block shape page-name)])))))
 
 (defn- update-block-attrs
-  [repo block-uuid {:keys [parents] :as op-value}]
+  [repo block-uuid {:keys [parents properties content] :as op-value}]
   (let [key-set (set/intersection
                  (conj rtc-const/general-attr-set :content)
                  (set (keys op-value)))]
@@ -255,9 +267,11 @@
       (let [first-remote-parent (first parents)
             local-parent (db/pull repo '[*] [:block/uuid first-remote-parent])
             whiteboard-page-block? (whiteboard-page-block? local-parent)]
-        (if whiteboard-page-block?
+        (cond
+          (and whiteboard-page-block? properties)
           (upsert-whiteboard-block repo op-value)
 
+          :else
           (let [b-ent (db/pull repo '[*] [:block/uuid block-uuid])
                 new-block
                 (cond-> (db/pull repo '[*] (:db/id b-ent))
@@ -296,9 +310,9 @@
     (let [r (check-block-pos repo self parents left)]
       (case r
         :not-exist
-        (insert-or-move-block repo self parents left false)
+        (insert-or-move-block repo self parents left false op-value)
         :wrong-pos
-        (insert-or-move-block repo self parents left true)
+        (insert-or-move-block repo self parents left true op-value)
         nil                             ; do nothing
         nil)
       (update-block-attrs repo self op-value)
@@ -313,9 +327,9 @@
       (let [r (check-block-pos repo self parents left)]
         (case r
           :not-exist
-          (insert-or-move-block repo self parents left false)
+          (insert-or-move-block repo self parents left false op-value)
           :wrong-pos
-          (insert-or-move-block repo self parents left true)
+          (insert-or-move-block repo self parents left true op-value)
           nil)))
     (update-block-attrs repo self op-value)
     (prn :apply-remote-update-ops self)))

+ 14 - 12
src/main/frontend/db/rtc/full_upload_download_graph.cljs

@@ -69,24 +69,26 @@
   [blocks]
   (mapv
    (fn [block]
-     (let [db-id (:db/id block)
-           block-parent (:db/id (:block/parent block))
-           block-left (:db/id (:block/left block))
-           block-alias (map :db/id (:block/alias block))
-           block-tags (map :db/id (:block/tags block))
-           block-type (keep (comp block-type-ident->str :db/ident) (:block/type block))
-           block-schema (some->> (:block/schema block)
-                                 (transit/read transit-r))
+     (let [db-id            (:db/id block)
+           block-parent     (:db/id (:block/parent block))
+           block-left       (:db/id (:block/left block))
+           block-alias      (map :db/id (:block/alias block))
+           block-tags       (map :db/id (:block/tags block))
+           block-type       (keep (comp block-type-ident->str :db/ident) (:block/type block))
+           block-schema     (some->> (:block/schema block)
+                                     (transit/read transit-r))
            block-properties (some->> (:block/properties block)
-                                     (transit/read transit-r))]
+                                     (transit/read transit-r))
+           block-link       (:db/id (:block/link block))]
        (cond-> (assoc block :db/id (str db-id))
-         block-parent (assoc :block/parent (str block-parent))
-         block-left (assoc :block/left (str block-left))
+         block-parent      (assoc :block/parent (str block-parent))
+         block-left        (assoc :block/left (str block-left))
          (seq block-alias) (assoc :block/alias (map str block-alias))
          (seq block-tags)  (assoc :block/tags (map str block-tags))
          (seq block-type)  (assoc :block/type block-type)
          block-schema      (assoc :block/schema block-schema)
-         block-properties  (assoc :block/properties block-properties))))
+         block-properties  (assoc :block/properties block-properties)
+         block-link        (assoc :block/link (str block-link)))))
    blocks))
 
 (def page-of-block

+ 1 - 1
src/main/frontend/handler/db_based/page.cljs

@@ -73,7 +73,7 @@
                                     (outliner-core/block)
                                     (outliner-tree/-get-down)
                                     (outliner-core/get-data))
-          to-last-direct-child-id (model/get-block-last-direct-child (db/get-db) to-id)
+          to-last-direct-child-id (model/get-block-last-direct-child-id (db/get-db) to-id)
           repo (state/get-current-repo)
           conn (conn/get-db repo false)
           datoms (d/datoms @conn :avet :block/page from-id)

+ 2 - 2
src/main/frontend/handler/db_based/property.cljs

@@ -577,7 +577,7 @@
                     :block/content ""
                     :block/page page-id
                     :block/parent page-id
-                    :block/left (or (when page-entity (model/get-block-last-direct-child (db/get-db) (:db/id page-entity)))
+                    :block/left (or (when page-entity (model/get-block-last-direct-child-id (db/get-db) (:db/id page-entity)))
                                     page-id)
                     :block/metadata metadata}
                    sqlite-util/block-with-timestamps)
@@ -631,7 +631,7 @@
                        :block/page page-id
                        :block/metadata metadata
                        :block/parent page-id
-                       :block/left (or (when page-entity (model/get-block-last-direct-child (db/get-db) (:db/id page-entity)))
+                       :block/left (or (when page-entity (model/get-block-last-direct-child-id (db/get-db) (:db/id page-entity)))
                                        page-id)}
                       sqlite-util/block-with-timestamps)]
     {:page page-tx

+ 118 - 94
src/main/frontend/handler/editor.cljs

@@ -257,20 +257,14 @@
                :block/content value}]
     (profile
      "Save block: "
-     (let [original-block (db/entity (:db/id block))
-           original-uuid (:block/uuid original-block)
-           original-props (:block/properties original-block)
-           uuid-changed? (not= (:block/uuid block) original-uuid)
-           block' (-> (wrap-parse-block block)
+      (let [block' (-> (wrap-parse-block block)
                       ;; :block/uuid might be changed when backspace/delete
                       ;; a block that has been refed
                       (assoc :block/uuid (:block/uuid block)))
-           opts' (merge opts (cond-> {:outliner-op :save-block}
-                               uuid-changed?
-                               (assoc :uuid-changed {:from (:block/uuid block)
-                                                     :to original-uuid})))]
-
-       (let [{:keys [tx-data]}
+            opts' (assoc opts :outliner-op :save-block)]
+        (let [original-block (db/entity (:db/id block))
+             original-props (:block/properties original-block)
+             {:keys [tx-data]}
              (outliner-tx/transact!
               opts'
               (outliner-core/save-block! block')
@@ -768,69 +762,97 @@
   (let [{:keys [id block-id block-parent-id value format config]} (get-state)]
     (when block-id
       (when-let [block-e (db/entity [:block/uuid block-id])]
-        (let [has-children? (seq (:block/_parent block-e))
-              block (db/pull (:db/id block-e))
-              left (tree/-get-left (outliner-core/block block))
-              left-has-children? (and left
-                                      (when-let [block-id (:block/uuid (:data left))]
-                                        (let [block (db/entity [:block/uuid block-id])]
-                                          (seq (:block/_parent block)))))
-              delete-block-fn (fn [block]
-                                (delete-block-aux! block delete-children? {:children-check? false}))]
-          (when-not (and has-children? left-has-children?)
-            (when block-parent-id
-              (let [block-parent (gdom/getElement block-parent-id)
-                    sibling-block (if (:embed? config)
-                                    (util/get-prev-block-non-collapsed
-                                     block-parent
-                                     {:container (util/rec-get-blocks-container block-parent)})
-                                    (util/get-prev-block-non-collapsed-non-embed block-parent))
-                    {:keys [prev-block new-content pos]} (move-to-prev-block repo sibling-block format id value)
-                    concat-prev-block? (boolean (and prev-block new-content))
-                    transact-opts (cond->
-                                   {:outliner-op :delete-blocks}
-                                    concat-prev-block?
-                                    (assoc :concat-data
-                                           {:last-edit-block (:block/uuid block)}))
-                    db-based? (config/db-based-graph? repo)]
-                (outliner-tx/transact!
-                 transact-opts
-                 (cond
-                   (and prev-block (:block/name prev-block)
-                        (not= (:db/id prev-block) (:db/id (:block/parent block)))
-                        (db-model/hidden-page? (:block/page block))) ; embed page
-                   nil
-
-                   concat-prev-block?
-                   (let [new-properties (merge (:block/properties (db/entity (:db/id prev-block)))
-                                               (:block/properties (db/entity (:db/id block))))]
-                     (if (seq (:block/_refs (db/entity (:db/id block))))
-                       (do
-                         (delete-block-fn prev-block)
-                         (save-block! repo block new-content {:editor/op :delete})
-                         (outliner-core/save-block! {:db/id (:db/id block)
-                                                     :block/uuid (:block/uuid block)
-                                                     :block/parent (:db/id (:block/parent prev-block))
-                                                     :block/left (or (:db/id (:block/left prev-block))
-                                                                     (:db/id (:block/parent prev-block)))})
-                         (when db-based?
-                           (outliner-core/save-block! {:db/id (:db/id block)
-                                                       :block/properties new-properties}))
-                         (when pos
-                           (util/schedule
-                            (fn []
-                              (when-let [input (state/get-input)]
-                                (cursor/move-cursor-to input pos))))))
-
-                       (do
-                         (delete-block-fn block)
-                         (save-block! repo prev-block new-content {:editor/op :delete})
-                         (when db-based?
-                           (outliner-core/save-block! {:db/id (:db/id prev-block)
-                                                     :block/properties new-properties})))))
+        (let [prev-block (db-model/get-prev (db/get-db) (:db/id block-e))]
+          (cond
+            (nil? prev-block)
+            nil
 
-                   :else
-                   (delete-block-fn block))))))))))
+            :else
+            (let [has-children? (seq (:block/_parent block-e))
+                  block (db/pull (:db/id block-e))
+                  left (tree/-get-left (outliner-core/block block))
+                  left-has-children? (and left
+                                          (when-let [block-id (:block/uuid (:data left))]
+                                            (let [block (db/entity [:block/uuid block-id])]
+                                              (seq (:block/_parent block)))))
+                  delete-block-fn (fn [block]
+                                    (delete-block-aux! block delete-children? {:children-check? false}))]
+              (when-not (and has-children? left-has-children?)
+                (when block-parent-id
+                  (let [block-parent (gdom/getElement block-parent-id)
+                        sibling-block (if (:embed? config)
+                                        (util/get-prev-block-non-collapsed
+                                         block-parent
+                                         {:container (util/rec-get-blocks-container block-parent)})
+                                        (util/get-prev-block-non-collapsed-non-embed block-parent))
+                        {:keys [prev-block new-content pos]} (move-to-prev-block repo sibling-block format id value)
+                        concat-prev-block? (boolean (and prev-block new-content))
+                        transact-opts (cond->
+                                       {:outliner-op :delete-blocks}
+                                        concat-prev-block?
+                                        (assoc :concat-data
+                                               {:last-edit-block (:block/uuid block)}))
+                        db-based? (config/db-based-graph? repo)]
+                    (outliner-tx/transact!
+                     transact-opts
+                     (cond
+                       (and prev-block (:block/name prev-block)
+                            (not= (:db/id prev-block) (:db/id (:block/parent block)))
+                            (db-model/hidden-page? (:block/page block))) ; embed page
+                       nil
+
+                       concat-prev-block?
+                       (let [new-properties (merge (:block/properties (db/entity (:db/id prev-block)))
+                                                   (:block/properties (db/entity (:db/id block))))]
+                         (if (seq (:block/_refs (db/entity (:db/id block))))
+                           (let [block-right (outliner-core/get-right-sibling (:db/id block))]
+                             (delete-block-fn prev-block)
+                             (save-block! repo block new-content {:editor/op :delete})
+                             (outliner-core/save-block! {:db/id (:db/id block)
+                                                         :block/parent (:db/id (:block/parent prev-block))
+                                                         :block/left (or (:db/id (:block/left prev-block))
+                                                                         (:db/id (:block/parent prev-block)))})
+
+                             ;; block->right needs to point its `left` to block->left
+                             (when (and block-right (not= (:db/id (:block/parent prev-block))
+                                                          (:db/id (:block/parent block))))
+                               (outliner-core/save-block! {:db/id (:db/id block-right)
+                                                           :block/left (:db/id (:block/left block))}))
+
+                             ;; update prev-block's children to point to the refed block
+                             (when (or (:block/collapsed? prev-block)
+                                       (= (:db/id prev-block) (:db/id (:block/parent block))))
+                               (let [children (:block/_parent prev-block)]
+                                 (doseq [child children]
+                                   (when-not (= (:db/id child) (:db/id block))
+                                     (outliner-core/save-block! {:db/id (:db/id child)
+                                                                :block/parent (:db/id block)
+                                                                :block/left (:db/id block)})))))
+
+                             ;; parent will be removed
+                             (when (= (:db/id prev-block) (:db/id (:block/parent block)))
+                               (when-let [parent-right (outliner-core/get-right-sibling (:db/id prev-block))]
+                                 (outliner-core/save-block! {:db/id (:db/id parent-right)
+                                                             :block/left (:db/id block)})))
+
+                             (when db-based?
+                               (outliner-core/save-block! {:db/id (:db/id block)
+                                                           :block/properties new-properties}))
+                             (when pos
+                               (util/schedule
+                                (fn []
+                                  (when-let [input (state/get-input)]
+                                    (cursor/move-cursor-to input pos))))))
+
+                           (do
+                             (delete-block-fn block)
+                             (save-block! repo prev-block new-content {:editor/op :delete})
+                             (when db-based?
+                               (outliner-core/save-block! {:db/id (:db/id prev-block)
+                                                           :block/properties new-properties})))))
+
+                       :else
+                       (delete-block-fn block))))))))))))
   (state/set-editor-op! nil))
 
 (defn delete-blocks!
@@ -2665,22 +2687,17 @@
         ^js input (state/get-input)
         current-pos (cursor/pos input)
         value (gobj/get input "value")
-        right (outliner-core/get-right-sibling (:db/id current-block))
-        current-block-has-children? (db/has-children? (:block/uuid current-block))
         collapsed? (util/collapsed? current-block)
-        first-child (:data (tree/-get-down (outliner-core/block current-block)))
-        next-block (if (or collapsed? (not current-block-has-children?))
-                     (when right (db/pull (:db/id right)))
-                     first-child)
+        next-block (when-let [e (db-model/get-next (db/get-db repo) (:db/id current-block))]
+                     (db/pull (:db/id e)))
+        next-block-right (when next-block (outliner-core/get-right-sibling (:db/id next-block)))
         db-based? (config/db-based-graph? repo)]
     (cond
       (nil? next-block)
       nil
 
-      (and collapsed? right (db/has-children? (:block/uuid right)))
-      nil
-
-      (and (not collapsed?) first-child (db/has-children? (:block/uuid first-child)))
+      ;; TODO: merge children from both the current block and the next block
+      (and collapsed? next-block (db/has-children? (:block/uuid next-block)))
       nil
 
       :else
@@ -2693,22 +2710,29 @@
                                     (drawer/remove-logbook)))
                           (str value "" (:block/content next-block)))
             repo (state/get-current-repo)
-            edit-block' (if next-block-has-refs?
-                          (assoc edit-block
-                                 :block/uuid (:block/uuid next-block))
-                          edit-block)
-            new-properties (merge
-                            (:block/properties (db/entity (:db/id next-block)))
-                            (:block/properties (db/entity (:db/id edit-block))))]
+            delete-block (if next-block-has-refs? edit-block next-block)
+            keep-block (if next-block-has-refs? next-block edit-block)]
         (outliner-tx/transact!
          {:outliner-op :delete-blocks
           :concat-data {:last-edit-block (:block/uuid edit-block)
                         :end? true}}
-         (delete-block-aux! next-block false)
-         (save-block! repo edit-block' new-content {:editor/op :delete})
+         (delete-block-aux! delete-block false)
+         (save-block! repo keep-block new-content {:editor/op :delete})
+         (when next-block-has-refs?
+           (outliner-core/save-block! {:db/id (:db/id keep-block)
+                                       :block/left (:db/id (:block/left delete-block))
+                                       :block/parent (:db/id (:block/parent delete-block))})
+           (when (and next-block-right (not= (:db/id (:block/parent next-block))
+                                             (:db/id (:block/parent edit-block))))
+             (outliner-core/save-block! {:db/id (:db/id next-block-right)
+                                         :block/left (:db/id (:block/left next-block))})))
          (when db-based?
-           (outliner-core/save-block! {:db/id (:db/id edit-block)
-                                       :block/properties new-properties})))
+           (let [new-properties (merge
+                                 (:block/properties (db/entity (:db/id edit-block)))
+                                 (:block/properties (db/entity (:db/id next-block))))]
+             (when-not (= new-properties (:block/properties keep-block))
+               (outliner-core/save-block! {:db/id (:db/id keep-block)
+                                           :block/properties new-properties})))))
         (let [block (if next-block-has-refs? next-block edit-block)]
           (edit-block! block current-pos nil))))))
 

+ 1 - 1
src/main/frontend/handler/file_based/page.cljs

@@ -322,7 +322,7 @@
                                     (outliner-core/block)
                                     (outliner-tree/-get-down)
                                     (outliner-core/get-data))
-          to-last-direct-child-id (model/get-block-last-direct-child (db/get-db) to-id)
+          to-last-direct-child-id (model/get-block-last-direct-child-id (db/get-db) to-id)
           repo (state/get-current-repo)
           conn (conn/get-db repo false)
           datoms (d/datoms @conn :avet :block/page from-id)

+ 17 - 5
src/main/frontend/modules/outliner/core.cljs

@@ -133,7 +133,7 @@
 
 (defn- get-last-child-or-self
   [block]
-  (let [last-child (some-> (db-model/get-block-last-direct-child (conn/get-db) (:db/id block) true)
+  (let [last-child (some-> (db-model/get-block-last-direct-child-id (conn/get-db) (:db/id block) true)
                            db/entity)
         target (or last-child block)]
     [target (some? last-child)]))
@@ -303,13 +303,25 @@
                 fix-tag-ids)
           repo (state/get-current-repo)
           db-based? (config/db-based-graph? repo)
-          eid (or (:db/id (:data this))
-                  (when-let [block-uuid (:block/uuid (:data this))] [:block/uuid block-uuid]))
+          db-id (:db/id (:data this))
+          block-uuid (:block/uuid (:data this))
+          eid (or db-id (when block-uuid [:block/uuid block-uuid]))
           block-entity (db/entity eid)
           tags-has-class? (and db-based?
                                (some (fn [tag]
                                        (contains? (:block/type (db/entity [:block/uuid (:block/uuid tag)])) "class"))
                                      (:block/tags m)))]
+
+      ;; Ensure block UUID never changes
+      (when (and db-id block-uuid)
+        (let [uuid-not-changed? (= block-uuid (:block/uuid (db/entity db-id)))]
+          (when-not uuid-not-changed?
+            (when config/dev?
+              (state/pub-event! [:notification/show
+                                {:content "Block UUID shouldn't be changed"
+                                 :status :error}])))
+          (assert uuid-not-changed? "Block UUID changed")))
+
       (when eid
         ;; Retract attributes to prepare for tx which rewrites block attributes
         (when (:block/content m)
@@ -1081,7 +1093,7 @@
             opts {:outliner-op :indent-outdent-blocks}]
         (if indent?
           (when (and left (not (page-first-child? first-block)))
-            (let [last-direct-child-id (db-model/get-block-last-direct-child db (:db/id left))
+            (let [last-direct-child-id (db-model/get-block-last-direct-child-id db (:db/id left))
                   blocks' (drop-while (fn [b]
                                         (= (:db/id (:block/parent b))
                                            (:db/id left)))
@@ -1120,7 +1132,7 @@
                         right-siblings (->> (get-right-siblings (block last-top-block))
                                             (map :data))]
                     (if (seq right-siblings)
-                      (let [result2 (if-let [last-direct-child-id (db-model/get-block-last-direct-child db (:db/id last-top-block))]
+                      (let [result2 (if-let [last-direct-child-id (db-model/get-block-last-direct-child-id db (:db/id last-top-block))]
                                       (move-blocks right-siblings (db/entity last-direct-child-id) (merge opts {:sibling? true}))
                                       (move-blocks right-siblings last-top-block (merge opts {:sibling? false})))]
                         (concat-tx-fn result result2))

+ 7 - 40
src/main/frontend/modules/outliner/datascript.cljs

@@ -80,39 +80,10 @@
   [tx-report]
   (get-in tx-report [:tempids :db/current-tx]))
 
-(defn update-block-refs
-  [txs opts]
-  (if-let [changed (:uuid-changed opts)]
-    (let [{:keys [from to]} changed
-          from-e (db/entity [:block/uuid from])
-          to-e (db/entity [:block/uuid to])
-          from-id (:db/id from-e)
-          to-id (:db/id to-e)
-          from-refs (:block/_refs from-e)
-          from-path-refs (:block/_path-refs from-e)
-          to-refs (:block/_refs to-e)
-          from-refs-txs (mapcat (fn [ref]
-                                  (let [id (:db/id ref)]
-                                    [[:db/retract id :block/refs from-id]
-                                     [:db/add id :block/refs to-id]])) from-refs)
-          from-path-refs-txs (mapcat (fn [ref]
-                                       (let [id (:db/id ref)]
-                                         [[:db/retract id :block/path-refs from-id]
-                                          [:db/add id :block/path-refs to-id]])) from-path-refs)
-          to-refs-txs (mapcat (fn [ref]
-                                (let [id (:db/id ref)
-                                      new-content (string/replace (:block/content ref)
-                                                                  (block-ref/->block-ref to)
-                                                                  (block-ref/->block-ref from))]
-                                  [[:db/add id :block/content new-content]])) to-refs)]
-      (concat txs from-refs-txs from-path-refs-txs to-refs-txs))
-    txs))
-
 (defn update-refs-and-macros
   "When a block is deleted, refs are updated and macros associated with the block are deleted"
   [txs repo opts]
-  (if (and (= :delete-blocks (:outliner-op opts))
-           (empty? (:uuid-changed opts)))
+  (if (= :delete-blocks (:outliner-op opts))
     (let [retracted-block-ids (->> (keep (fn [tx]
                                            (when (and (vector? tx)
                                                       (= :db.fn/retractEntity (first tx)))
@@ -146,9 +117,9 @@
           macros-tx (mapcat (fn [b]
                               ;; Only delete if last reference
                               (keep #(when (<= (count (:block/_macros (db/entity repo (:db/id %))))
-                                                   1)
-                                           (vector :db.fn/retractEntity (:db/id %)))
-                                        (:block/macros b)))
+                                               1)
+                                       (vector :db.fn/retractEntity (:db/id %)))
+                                    (:block/macros b)))
                             retracted-blocks)]
       (when (seq retracted-tx')
         (state/set-state! [:editor/last-replace-ref-content-tx (state/get-current-repo)]
@@ -179,16 +150,12 @@
         txs (map (fn [m]
                    (if (map? m)
                      (dissoc m :block/children :block/meta :block/top? :block/bottom? :block/anchor
-                               :block/title :block/body :block/level :block/container :db/other-tx
-                               :block/unordered)
+                             :block/title :block/body :block/level :block/container :db/other-tx
+                             :block/unordered)
                      m)) txs)
         txs (remove-nil-from-transaction txs)
         txs (cond-> txs
-              (:uuid-changed opts)
-              (update-block-refs opts)
-
-              (and (= :delete-blocks (:outliner-op opts))
-                   (empty? (:uuid-changed opts)))
+              (= :delete-blocks (:outliner-op opts))
               (update-refs-and-macros repo opts)
 
               true