mirror of
https://github.com/logseq/logseq.git
synced 2026-05-03 02:16:30 +00:00
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.
This commit is contained in:
@@ -600,7 +600,7 @@
|
||||
(state/set-editor-op! nil)))
|
||||
|
||||
(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
|
||||
before? false}}]
|
||||
(when (or page block-uuid)
|
||||
@@ -616,51 +616,60 @@
|
||||
last-block-id (:db/id (last blocks))]
|
||||
(when 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?])
|
||||
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?]
|
||||
sibling?
|
||||
[(db/pull (:db/id block)) sibling?]
|
||||
|
||||
last-block
|
||||
[last-block true]
|
||||
last-block
|
||||
[last-block true]
|
||||
|
||||
block
|
||||
[(db/pull (:db/id block)) sibling?]
|
||||
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))))))
|
||||
;; 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!
|
||||
[page-name]
|
||||
|
||||
Reference in New Issue
Block a user