Browse Source

fix(plugins): prepend block

Tienson Qin 2 weeks ago
parent
commit
3fa1c5c9b8
2 changed files with 40 additions and 22 deletions
  1. 14 3
      src/main/frontend/handler/editor.cljs
  2. 26 19
      src/main/logseq/api.cljs

+ 14 - 3
src/main/frontend/handler/editor.cljs

@@ -556,7 +556,9 @@
                  (state/set-state! :editor/async-unsaved-chars nil))))))
                  (state/set-state! :editor/async-unsaved-chars nil))))))
 
 
 (defn api-insert-new-block!
 (defn api-insert-new-block!
-  [content {:keys [page block-uuid sibling? before? properties
+  [content {:keys [page block-uuid
+                   sibling? before? start? end?
+                   properties
                    custom-uuid replace-empty-target? edit-block? ordered-list? other-attrs]
                    custom-uuid replace-empty-target? edit-block? ordered-list? other-attrs]
             :or {sibling? false
             :or {sibling? false
                  before? false
                  before? false
@@ -598,6 +600,7 @@
                             (wrap-parse-block)
                             (wrap-parse-block)
                             (assoc :block/uuid (or custom-uuid (db/new-block-id))))
                             (assoc :block/uuid (or custom-uuid (db/new-block-id))))
               new-block (merge new-block other-attrs)
               new-block (merge new-block other-attrs)
+              block' (db/entity (:db/id block))
               [target-block sibling?] (cond
               [target-block sibling?] (cond
                                         before?
                                         before?
                                         (let [left-or-parent (or (ldb/get-left-sibling block)
                                         (let [left-or-parent (or (ldb/get-left-sibling block)
@@ -607,13 +610,21 @@
                                           [left-or-parent sibling?])
                                           [left-or-parent sibling?])
 
 
                                         sibling?
                                         sibling?
-                                        [(db/entity (:db/id block)) sibling?]
+                                        [block' sibling?]
+
+                                        start?
+                                        [block' false]
+
+                                        end?
+                                        (if last-block
+                                          [block' false]
+                                          [last-block true])
 
 
                                         last-block
                                         last-block
                                         [last-block true]
                                         [last-block true]
 
 
                                         block
                                         block
-                                        [(db/entity (:db/id block)) sibling?]
+                                        [block' sibling?]
 
 
                                         ;; FIXME: assert
                                         ;; FIXME: assert
                                         :else
                                         :else

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

@@ -710,7 +710,7 @@
                    block (<get-block (str block-uuid-or-page-name))]
                    block (<get-block (str block-uuid-or-page-name))]
              (if (and block? (not block))
              (if (and block? (not block))
                (throw (js/Error. "Block not exists"))
                (throw (js/Error. "Block not exists"))
-               (p/let [{:keys [before sibling focus customUUID properties autoOrderedList schema]} (bean/->clj opts)
+               (p/let [{:keys [before start end sibling focus customUUID properties autoOrderedList schema]} (bean/->clj opts)
                        [page-name block-uuid] (if (util/uuid-string? block-uuid-or-page-name)
                        [page-name block-uuid] (if (util/uuid-string? block-uuid-or-page-name)
                                                 [nil (uuid block-uuid-or-page-name)]
                                                 [nil (uuid block-uuid-or-page-name)]
                                                 [block-uuid-or-page-name nil])
                                                 [block-uuid-or-page-name nil])
@@ -742,6 +742,8 @@
                        opts' {:block-uuid block-uuid'
                        opts' {:block-uuid block-uuid'
                               :sibling? sibling?
                               :sibling? sibling?
                               :before? before?
                               :before? before?
+                              :start? start
+                              :end? end
                               :edit-block? edit-block?
                               :edit-block? edit-block?
                               :page page-name
                               :page page-name
                               :custom-uuid custom-uuid
                               :custom-uuid custom-uuid
@@ -1048,27 +1050,26 @@
 
 
 (defn ^:export prepend_block_in_page
 (defn ^:export prepend_block_in_page
   [uuid-or-page-name content ^js opts]
   [uuid-or-page-name content ^js opts]
-  (p/let [block           (<get-block uuid-or-page-name)
+  (p/let [uuid-or-page-name (or
+                             uuid-or-page-name
+                             (state/get-current-page)
+                             (date/today))
+          block           (<get-block uuid-or-page-name)
           new-page        (when (and (not block) (not (util/uuid-string? uuid-or-page-name))) ; page not exists
           new-page        (when (and (not block) (not (util/uuid-string? uuid-or-page-name))) ; page not exists
                             (page-handler/<create! uuid-or-page-name
                             (page-handler/<create! uuid-or-page-name
                                                    {:redirect?           false
                                                    {:redirect?           false
                                                     :format              (state/get-preferred-format)}))]
                                                     :format              (state/get-preferred-format)}))]
-    (let [block (or block new-page)]
-      (when-not block
-        (throw (ex-info (str "There's no page/block for: " uuid-or-page-name) {})))
-      (let [opts (bean/->clj opts)
-            opts' (assoc opts :before false :sibling false)]
-        (insert_block (str (:block/uuid block)) content (bean/->js opts'))))))
+    (let [block (or block new-page)
+          opts (bean/->clj opts)
+          opts' (assoc opts :before false :sibling false :start true)]
+      (insert_block (str (:block/uuid block)) content (bean/->js opts')))))
 
 
 (defn ^:export append_block_in_page
 (defn ^:export append_block_in_page
   [uuid-or-page-name content ^js opts]
   [uuid-or-page-name content ^js opts]
-  (let [current-page? (or (and (nil? content) (nil? opts))
-                          (and (nil? opts) (some->> content (instance? js/Object))))
-        opts (if current-page? content opts)
-        content (if current-page? uuid-or-page-name content)
-        uuid-or-page-name (if current-page?
-                            (or (state/get-current-page) (date/today))
-                            uuid-or-page-name)]
+  (let [uuid-or-page-name (or
+                           uuid-or-page-name
+                           (state/get-current-page)
+                           (date/today))]
     (p/let [_ (<ensure-page-loaded uuid-or-page-name)
     (p/let [_ (<ensure-page-loaded uuid-or-page-name)
             page? (not (util/uuid-string? uuid-or-page-name))
             page? (not (util/uuid-string? uuid-or-page-name))
             page (db-model/get-page uuid-or-page-name)
             page (db-model/get-page uuid-or-page-name)
@@ -1076,10 +1077,16 @@
             new-page (when page-not-exist?
             new-page (when page-not-exist?
                        (page-handler/<create! uuid-or-page-name
                        (page-handler/<create! uuid-or-page-name
                                               {:redirect? false
                                               {:redirect? false
-                                               :format (state/get-preferred-format)}))]
-      (when-let [block (or page new-page)]
-        (let [target (str (:block/uuid block))]
-          (insert_block target content opts))))))
+                                               :format (state/get-preferred-format)}))
+            block (or page new-page)]
+      (let [children (:block/_parent block)
+            [target sibling?] (if (seq children)
+                                [(last (ldb/sort-by-order children)) true]
+                                [block false])
+            target-id (str (:block/uuid target))
+            opts (-> (bean/->clj opts)
+                     (assoc :sibling sibling?))]
+        (insert_block target-id content opts)))))
 
 
 ;; plugins
 ;; plugins
 (defn ^:export validate_external_plugins [urls]
 (defn ^:export validate_external_plugins [urls]