Browse Source

fix: Error + Data Loss with message block with id already exists.

close #5414

This also adds the support to insert a block as the first child.

Example:
logseq.api.insert_block("628ce7a2-02db-4aa1-a99a-2e6398297780", "foo", {sibling: false, before: true})
Tienson Qin 3 years ago
parent
commit
877a5d8042
2 changed files with 38 additions and 19 deletions
  1. 19 16
      src/main/frontend/handler/editor.cljs
  2. 19 3
      src/main/logseq/api.cljs

+ 19 - 16
src/main/frontend/handler/editor.cljs

@@ -590,6 +590,7 @@
                  edit-block? true}}]
   (when (or page block-uuid)
     (let [before? (if page false before?)
+          sibling? (boolean sibling?)
           sibling? (if before? true (if page false sibling?))
           block (if page
                   (db/entity [:block/name (util/page-name-sanity-lc page)])
@@ -1290,21 +1291,23 @@
                  db-content (:block/content db-block)
                  db-content-without-heading (and db-content
                                                  (gp-util/safe-subs db-content (:block/level db-block)))
-                 value (or (:block/content current-block)
-                           (and elem (gobj/get elem "value")))]
-             (cond
-               force?
-               (save-block-aux! db-block value opts)
-
-               (and skip-properties?
-                    (db-model/top-block? block)
-                    (when elem (thingatpt/properties-at-point elem)))
-               nil
-
-               (and block value db-content-without-heading
-                    (not= (string/trim db-content-without-heading)
-                          (string/trim value)))
-               (save-block-aux! db-block value opts)))
+                 value (if (= (:block/uuid current-block) (:block/uuid block))
+                         (:block/content current-block)
+                         (and elem (gobj/get elem "value")))]
+             (when value
+               (cond
+                 force?
+                 (save-block-aux! db-block value opts)
+
+                 (and skip-properties?
+                      (db-model/top-block? block)
+                      (when elem (thingatpt/properties-at-point elem)))
+                 nil
+
+                 (and block value db-content-without-heading
+                      (not= (string/trim db-content-without-heading)
+                            (string/trim value)))
+                 (save-block-aux! db-block value opts))))
            (catch js/Error error
              (log/error :save-block-failed error))))))))
 
@@ -2906,7 +2909,7 @@
       (and (gp-util/url? text)
            (not (string/blank? (util/get-selected-text))))
       (html-link-format! text)
-      
+
       (and (text/block-ref? text)
            (wrapped-by? input "((" "))"))
       (commands/simple-insert! (state/get-edit-input-id) (text/get-block-ref text) nil)

+ 19 - 3
src/main/logseq/api.cljs

@@ -454,11 +454,27 @@
     (let [{:keys [before sibling isPageBlock properties]} (bean/->clj opts)
           page-name (and isPageBlock block-uuid-or-page-name)
           block-uuid (if isPageBlock nil (uuid block-uuid-or-page-name))
+          block-uuid' (if (and (not sibling) before block-uuid)
+                        (let [block (db/entity [:block/uuid block-uuid])
+                              first-child (db-model/get-by-parent-&-left (db/get-db)
+                                                                         (:db/id block)
+                                                                         (:db/id block))]
+                          (if first-child
+                            (:block/uuid first-child)
+                            block-uuid))
+                        block-uuid)
+          insert-at-first-child? (not= block-uuid' block-uuid)
+          [sibling? before?] (if insert-at-first-child?
+                               [true true]
+                               [sibling before])
+          before? (if (and (false? sibling?) before? (not insert-at-first-child?))
+                    false
+                    before?)
           new-block (editor-handler/api-insert-new-block!
                       content
-                      {:block-uuid block-uuid
-                       :sibling?   sibling
-                       :before?    before
+                      {:block-uuid block-uuid'
+                       :sibling?   sibling?
+                       :before?    before?
                        :page       page-name
                        :properties properties})]
       (bean/->js (normalize-keyword-for-json new-block)))))