Skip to content

Commit

Permalink
enhance: add an reuse-last-block? paras key to api-insert-new-block
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
llcc authored and tiensonqin committed Apr 7, 2022
1 parent 5a9155d commit 32809dd
Showing 1 changed file with 55 additions and 46 deletions.
101 changes: 55 additions & 46 deletions src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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?])

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!
[page-name]
Expand Down

0 comments on commit 32809dd

Please sign in to comment.