Explorar o código

enhance: add an `reuse-last-block?` paras key to `api-insert-new-block`

When last block is empty and `reuse-last-block?` is non-nil, don't
create a new block but using last block to insert contents.

Use case 1:
When receiving shared data from other apps and inserting them to today's
empty journal, it's better to insert the data to the first empty block
instead of creating a new block to save it but keeping the first one
empty.
llcc %!s(int64=3) %!d(string=hai) anos
pai
achega
32809dd57f
Modificáronse 1 ficheiros con 55 adicións e 46 borrados
  1. 55 46
      src/main/frontend/handler/editor.cljs

+ 55 - 46
src/main/frontend/handler/editor.cljs

@@ -600,7 +600,7 @@
    (state/set-editor-op! nil)))
    (state/set-editor-op! nil)))
 
 
 (defn api-insert-new-block!
 (defn api-insert-new-block!
-  [content {:keys [page block-uuid sibling? before? properties custom-uuid]
+  [content {:keys [page block-uuid sibling? before? properties custom-uuid reuse-last-block?]
             :or {sibling? false
             :or {sibling? false
                  before? false}}]
                  before? false}}]
   (when (or page block-uuid)
   (when (or page block-uuid)
@@ -616,51 +616,60 @@
                                  last-block-id (:db/id (last blocks))]
                                  last-block-id (:db/id (last blocks))]
                              (when last-block-id
                              (when last-block-id
                                (db/pull last-block-id))))
                                (db/pull last-block-id))))
-              format (or
-                      (:block/format block)
-                      (db/get-page-format (:db/id block))
-                      (state/get-preferred-format))
-              content (if (seq properties)
-                        (property/insert-properties format content properties)
-                        content)
-              new-block (-> (select-keys block [:block/page :block/journal?
-                                                :block/journal-day])
-                            (assoc :block/content content
-                                   :block/format format))
-              new-block (assoc new-block :block/page
-                               (if page
-                                 (:db/id block)
-                                 (:db/id (:block/page new-block))))
-              new-block (-> new-block
-                            (wrap-parse-block)
-                            (assoc :block/uuid (or custom-uuid (db/new-block-id))))
-              [block-m sibling?] (cond
-                                   before?
-                                   (let [first-child? (->> [:block/parent :block/left]
-                                                           (map #(:db/id (get block %)))
-                                                           (apply =))
-                                         block (db/pull (:db/id (:block/left block)))
-                                         sibling? (if (or first-child? ;; insert as first child
-                                                          (:block/name block))
-                                                    false sibling?)]
-                                     [block sibling?])
-
-                                   sibling?
-                                   [(db/pull (:db/id block)) sibling?]
-
-                                   last-block
-                                   [last-block true]
-
-                                   block
-                                   [(db/pull (:db/id block)) sibling?]
-
-                                   ;; FIXME: assert
-                                   :else
-                                   nil)]
-
-          (when block-m
-            (outliner-insert-block! {:skip-save-current-block? true} block-m new-block {:sibling? sibling?})
-            new-block))))))
+              last-block-content (:block/content last-block)]
+          ;; when last block is blank and `reuse-last-block?' nils,
+          ;; dont't insert content to a new block but last block.
+          (if (and last-block
+                   (string/blank? last-block-content)
+                   reuse-last-block?)
+            (let [new-block (assoc last-block :block/content content)]
+              (-> (outliner-core/block new-block)
+                  (outliner-core/save-node))
+              new-block)
+            (let [format (or
+                          (:block/format block)
+                          (db/get-page-format (:db/id block))
+                          (state/get-preferred-format))
+                  content (if (seq properties)
+                            (property/insert-properties format content properties)
+                            content)
+                  new-block (-> (select-keys block [:block/page :block/journal?
+                                                    :block/journal-day])
+                                (assoc :block/content content
+                                       :block/format format))
+                  new-block (assoc new-block :block/page
+                                   (if page
+                                     (:db/id block)
+                                     (:db/id (:block/page new-block))))
+                  new-block (-> new-block
+                                (wrap-parse-block)
+                                (assoc :block/uuid (or custom-uuid (db/new-block-id))))
+                  [block-m sibling?] (cond
+                                       before?
+                                       (let [first-child? (->> [:block/parent :block/left]
+                                                               (map #(:db/id (get block %)))
+                                                               (apply =))
+                                             block (db/pull (:db/id (:block/left block)))
+                                             sibling? (if (or first-child? ;; insert as first child
+                                                              (:block/name block))
+                                                        false sibling?)]
+                                         [block sibling?])
+
+                                       sibling?
+                                       [(db/pull (:db/id block)) sibling?]
+
+                                       last-block
+                                       [last-block true]
+
+                                       block
+                                       [(db/pull (:db/id block)) sibling?]
+
+                                       ;; FIXME: assert
+                                       :else
+                                       nil)]
+              (when block-m
+                (outliner-insert-block! {:skip-save-current-block? true} block-m new-block {:sibling? sibling?})
+                new-block))))))))
 
 
 (defn insert-first-page-block-if-not-exists!
 (defn insert-first-page-block-if-not-exists!
   [page-name]
   [page-name]