fix: get-block-and-children

pull/1656/head
Tienson Qin 2021-04-05 14:14:44 +08:00
parent da43e43603
commit 10f7f078f7
5 changed files with 94 additions and 174 deletions

View File

@ -2206,7 +2206,9 @@
ref? (:ref? config) ref? (:ref? config)
custom-query? (:custom-query? config) custom-query? (:custom-query? config)
blocks->vec-tree #(if (or custom-query? ref?) % (tree/blocks->vec-tree %)) blocks->vec-tree #(if (or custom-query? ref?) % (tree/blocks->vec-tree %))
blocks (blocks->vec-tree blocks)] ;; FIXME: blocks->vec-tree not working for the block container (zoom view)
blocks' (blocks->vec-tree blocks)
blocks (if (seq blocks') blocks' blocks)]
(when (seq blocks) (when (seq blocks)
[:div.blocks-container.flex-1 [:div.blocks-container.flex-1
{:style {:margin-left (cond {:style {:margin-left (cond
@ -2229,9 +2231,7 @@
config (assoc config :block/uuid (:block/uuid item))] config (assoc config :block/uuid (:block/uuid item))]
(rum/with-key (rum/with-key
(block-container config item) (block-container config item)
(:block/uuid item))))) (:block/uuid item)))))])))
;; (add-button config ref? custom-query? blocks)
])))
;; headers to hiccup ;; headers to hiccup
(defn ->hiccup (defn ->hiccup

View File

@ -40,7 +40,7 @@
add-properties! block-and-children-transform blocks-count blocks-count-cache clean-export! cloned? delete-blocks add-properties! block-and-children-transform blocks-count blocks-count-cache clean-export! cloned? delete-blocks
delete-file! delete-file-blocks! delete-file-pages! delete-file-tx delete-files delete-pages-by-files delete-file! delete-file-blocks! delete-file-pages! delete-file-tx delete-files delete-pages-by-files
filter-only-public-pages-and-blocks get-all-block-contents get-all-tagged-pages filter-only-public-pages-and-blocks get-all-block-contents get-all-tagged-pages
get-all-templates get-block-and-children get-block-and-children-no-cache get-block-by-uuid get-block-children get-all-templates get-block-and-children get-block-by-uuid get-block-children
get-block-children-ids get-block-file get-block-immediate-children get-block-page get-block-children-ids get-block-file get-block-immediate-children get-block-page
get-block-page-end-pos get-block-parent get-block-parents get-block-referenced-blocks get-block-refs-count get-block-page-end-pos get-block-parent get-block-parents get-block-referenced-blocks get-block-refs-count
get-blocks-by-priority get-blocks-contents get-collapsed-blocks get-custom-css get-blocks-by-priority get-blocks-contents get-collapsed-blocks get-custom-css

View File

@ -591,14 +591,9 @@
content)))) content))))
(defn block-and-children-transform (defn block-and-children-transform
[result repo-url block-uuid level] [result repo-url block-uuid]
(some->> result (some->> result
db-utils/seq-flatten db-utils/seq-flatten
(take-while (fn [h]
(or
(= (:block/uuid h)
block-uuid)
(> (:block/level h) level))))
(db-utils/with-repo repo-url) (db-utils/with-repo repo-url)
(with-block-refs-count repo-url))) (with-block-refs-count repo-url)))
@ -665,53 +660,25 @@
(when (seq ids) (when (seq ids)
(db-utils/pull-many repo '[*] ids))))) (db-utils/pull-many repo '[*] ids)))))
;; TODO: replace with get-block-children
(defn get-block-and-children (defn get-block-and-children
([repo block-uuid] ([repo block-uuid]
(get-block-and-children repo block-uuid true)) (get-block-and-children repo block-uuid true))
([repo block-uuid use-cache?] ([repo block-uuid use-cache?]
(let [block (db-utils/entity repo [:block/uuid block-uuid]) (let [block (db-utils/entity repo [:block/uuid block-uuid])]
page (:db/id (:block/page block))
pos (:start-pos (:block/meta block))
level (:block/level block)
pred (fn []
(let [block (db-utils/entity repo [:block/uuid block-uuid])
pos (:start-pos (:block/meta block))]
(fn [data meta]
(>= (:start-pos meta) pos))))]
(some-> (react/q repo [:block/block block-uuid] (some-> (react/q repo [:block/block block-uuid]
{:use-cache? use-cache? {:use-cache? use-cache?
:transform-fn #(block-and-children-transform % repo block-uuid level)
:inputs-fn (fn []
[page (pred)])}
'[:find (pull ?block [*])
:in $ ?page ?pred
:where
[?block :block/page ?page]
[?block :block/meta ?meta]
[(?pred $ ?meta)]])
react))))
;; TODO: performance :transform-fn #(block-and-children-transform % repo block-uuid)}
(defn get-block-and-children-no-cache '[:find (pull ?c [*])
[repo block-uuid] :in $ ?id %
(let [block (db-utils/entity repo [:block/uuid block-uuid])
page (:db/id (:block/page block))
pos (:start-pos (:block/meta block))
level (:block/level block)
pred (fn [data meta]
(>= (:start-pos meta) pos))]
(-> (d/q
'[:find (pull ?block [*])
:in $ ?page ?pred
:where :where
[?block :block/page ?page] [?b :block/uuid ?id]
[?block :block/meta ?meta] (or-join [?b ?c ?id]
[(?pred $ ?meta)]] (parent ?b ?c)
(conn/get-conn repo) [?c :block/uuid ?id])]
page block-uuid
pred) rules)
(block-and-children-transform repo block-uuid level)))) react))))
(defn get-file-page (defn get-file-page
([file-path] ([file-path]

View File

@ -79,16 +79,6 @@
block)] block)]
(apply util/join-newline @contents)))) (apply util/join-newline @contents))))
;; with children content
(defn get-block-full-content
([repo block-id]
(get-block-full-content repo block-id (fn [block] (:block/content block))))
([repo block-id transform-fn]
(let [blocks (db/get-block-and-children-no-cache repo block-id)]
(->> blocks
(map transform-fn)
(apply util/join-newline)))))
(defn get-block-end-pos-rec (defn get-block-end-pos-rec
[repo block] [repo block]
(let [children (:block/children block)] (let [children (:block/children block)]

View File

@ -639,7 +639,6 @@
(remove nil?))))))))) (remove nil?)))))))))
;; TODO: fix for ;; TODO: fix for
;; 1. collapsed parent
;; 2. block as a container instead of a page ;; 2. block as a container instead of a page
(defn build-outliner-relation (defn build-outliner-relation
[current-block new-block] [current-block new-block]
@ -647,7 +646,12 @@
(mapv outliner-core/block [current-block new-block]) (mapv outliner-core/block [current-block new-block])
has-children? (db/has-children? (state/get-current-repo) has-children? (db/has-children? (state/get-current-repo)
(tree/-get-id current-node)) (tree/-get-id current-node))
sibling? (not has-children?)] sibling? (cond
(:block/collapsed? current-block)
true
:else
(not has-children?))]
;; perf: multiple file writes ;; perf: multiple file writes
(outliner-core/save-node current-node) (outliner-core/save-node current-node)
(outliner-core/insert-node new-node current-node sibling?))) (outliner-core/insert-node new-node current-node sibling?)))
@ -657,14 +661,13 @@
db-id :db/id db-id :db/id
:as block} :as block}
value value
{:keys [ok-handler new-level current-page] {:keys [ok-handler]
:as opts}] :as opts}]
(let [block-page? (and current-page (util/uuid-string? current-page)) (let [current-page (state/get-current-page)
block-page? (and current-page (util/uuid-string? current-page))
block-self? (= uuid (and block-page? (medley/uuid current-page))) block-self? (= uuid (and block-page? (medley/uuid current-page)))
input (gdom/getElement (state/get-edit-input-id)) input (gdom/getElement (state/get-edit-input-id))
pos (if new-level pos (util/get-input-pos input)
(dec (count value))
(util/get-input-pos input))
repo (or repo (state/get-current-repo)) repo (or repo (state/get-current-repo))
[fst-block-text snd-block-text] (compute-fst-snd-block-text value pos) [fst-block-text snd-block-text] (compute-fst-snd-block-text value pos)
current-block (-> (assoc block :block/content fst-block-text) current-block (-> (assoc block :block/content fst-block-text)
@ -737,29 +740,17 @@
block-id (:block/uuid block) block-id (:block/uuid block)
block (or (db/pull [:block/uuid block-id]) block (or (db/pull [:block/uuid block-id])
block) block)
collapsed? (:block/collapsed? block)
repo (or (:block/repo block) (state/get-current-repo)) repo (or (:block/repo block) (state/get-current-repo))
last-child (and collapsed? properties (with-timetracking-properties block value)]
(last (db/get-block-and-children-no-cache repo (:block/uuid block))))
last-child (when (not= (:block/uuid last-child)
(:block/uuid block))
last-child)
new-block (or last-child block)
new-value (if last-child (:block/content last-child) value)
properties (with-timetracking-properties new-block new-value)]
;; save the current block and insert a new block ;; save the current block and insert a new block
(insert-new-block-aux! (insert-new-block-aux!
(assoc new-block :block/properties properties) (assoc block :block/properties properties)
new-value value
{:create-new-block? true {:ok-handler
:ok-handler
(fn [last-block] (fn [last-block]
(let [last-id (:block/uuid last-block)] (let [last-id (:block/uuid last-block)]
(edit-block! last-block 0 format id) (edit-block! last-block 0 format id)
(clear-when-saved!))) (clear-when-saved!)))})))))
:new-level (and last-child (:block/level block))
:blocks-container-id (:id config)
:current-page (state/get-current-page)})))))
(defn update-timestamps-content! (defn update-timestamps-content!
[{:block/keys [repeated? marker] :as block} content] [{:block/keys [repeated? marker] :as block} content]
@ -1770,64 +1761,65 @@
(defn move-up-down (defn move-up-down
[e up?] [e up?]
(when-let [block-id (:block/uuid (state/get-edit-block))] ;; (when-let [block-id (:block/uuid (state/get-edit-block))]
(let [block-parent-id (state/get-editing-block-dom-id) ;; (let [block-parent-id (state/get-editing-block-dom-id)
block (db/entity [:block/uuid block-id]) ;; block (db/entity [:block/uuid block-id])
meta (:block/meta block) ;; meta (:block/meta block)
page (:block/page block) ;; page (:block/page block)
block-dom-node (gdom/getElement block-parent-id) ;; block-dom-node (gdom/getElement block-parent-id)
prev-block (get-prev-block-non-collapsed block-dom-node) ;; prev-block (get-prev-block-non-collapsed block-dom-node)
next-block (get-next-block-non-collapsed block-dom-node) ;; next-block (get-next-block-non-collapsed block-dom-node)
repo (state/get-current-repo) ;; repo (state/get-current-repo)
move-upwards-to-parent? (and up? prev-block (< (d/attr prev-block "level") (:block/level block))) ;; move-upwards-to-parent? (and up? prev-block (< (d/attr prev-block "level") (:block/level block)))
move-down-to-higher-level? (and (not up?) next-block (< (d/attr next-block "level") (:block/level block)))] ;; move-down-to-higher-level? (and (not up?) next-block (< (d/attr next-block "level") (:block/level block)))]
(when-let [sibling-block (cond ;; (when-let [sibling-block (cond
move-upwards-to-parent? ;; move-upwards-to-parent?
prev-block ;; prev-block
move-down-to-higher-level? ;; move-down-to-higher-level?
next-block ;; next-block
:else ;; :else
(let [f (if up? util/get-prev-block-with-same-level util/get-next-block-with-same-level)] ;; (let [f (if up? util/get-prev-block-with-same-level util/get-next-block-with-same-level)]
(f block-dom-node)))] ;; (f block-dom-node)))]
(when-let [sibling-block-id (d/attr sibling-block "blockid")] ;; (when-let [sibling-block-id (d/attr sibling-block "blockid")]
(when-let [sibling-block (db/pull-block (medley/uuid sibling-block-id))] ;; (when-let [sibling-block (db/pull-block (medley/uuid sibling-block-id))]
(let [sibling-meta (:block/meta sibling-block) ;; (let [sibling-meta (:block/meta sibling-block)
hc1 (db/get-block-and-children-no-cache repo (:block/uuid block)) ;; hc1 (db/get-block-and-children-no-cache repo (:block/uuid block))
hc2 (if (or move-upwards-to-parent? move-down-to-higher-level?) ;; hc2 (if (or move-upwards-to-parent? move-down-to-higher-level?)
[sibling-block] ;; [sibling-block]
(db/get-block-and-children-no-cache repo (:block/uuid sibling-block)))] ;; (db/get-block-and-children-no-cache repo (:block/uuid sibling-block)))]
;; Same page and next to the other ;; ;; Same page and next to the other
(when (and ;; (when (and
(= (:db/id (:block/page block)) ;; (= (:db/id (:block/page block))
(:db/id (:block/page sibling-block))) ;; (:db/id (:block/page sibling-block)))
(or ;; (or
(and up? (= (:end-pos (:block/meta (last hc2))) (:start-pos (:block/meta (first hc1))))) ;; (and up? (= (:end-pos (:block/meta (last hc2))) (:start-pos (:block/meta (first hc1)))))
(and (not up?) (= (:end-pos (:block/meta (last hc1))) (:start-pos (:block/meta (first hc2))))))) ;; (and (not up?) (= (:end-pos (:block/meta (last hc1))) (:start-pos (:block/meta (first hc2)))))))
(let [hc1-content (block-and-children-content hc1) ;; (let [hc1-content (block-and-children-content hc1)
hc2-content (block-and-children-content hc2) ;; hc2-content (block-and-children-content hc2)
file (db/get-block-file (:block/uuid block)) ;; file (db/get-block-file (:block/uuid block))
file-path (:file/path file) ;; file-path (:file/path file)
old-file-content (db/get-file file-path) ;; old-file-content (db/get-file file-path)
[start-pos end-pos new-content blocks] (if up? ;; [start-pos end-pos new-content blocks] (if up?
[(:start-pos sibling-meta) ;; [(:start-pos sibling-meta)
(get-in (last hc1) [:block/meta :end-pos]) ;; (get-in (last hc1) [:block/meta :end-pos])
(str hc1-content hc2-content) ;; (str hc1-content hc2-content)
(concat hc1 hc2)] ;; (concat hc1 hc2)]
[(:start-pos meta) ;; [(:start-pos meta)
(get-in (last hc2) [:block/meta :end-pos]) ;; (get-in (last hc2) [:block/meta :end-pos])
(str hc2-content hc1-content) ;; (str hc2-content hc1-content)
(concat hc2 hc1)])] ;; (concat hc2 hc1)])]
(when (and start-pos end-pos) ;; (when (and start-pos end-pos)
(let [new-file-content (utf8/insert! old-file-content start-pos end-pos new-content) ;; (let [new-file-content (utf8/insert! old-file-content start-pos end-pos new-content)
blocks-meta (rebuild-blocks-meta start-pos blocks)] ;; blocks-meta (rebuild-blocks-meta start-pos blocks)]
(profile ;; (profile
(str "Move block " (if up? "up: " "down: ")) ;; (str "Move block " (if up? "up: " "down: "))
(repo-handler/transact-react-and-alter-file! ;; (repo-handler/transact-react-and-alter-file!
repo ;; repo
blocks-meta ;; blocks-meta
{:key :block/change ;; {:key :block/change
:data (map (fn [block] (assoc block :block/page page)) blocks)} ;; :data (map (fn [block] (assoc block :block/page page)) blocks)}
[[file-path new-file-content]]))))))))))))) ;; [[file-path new-file-content]]))))))))))))
)
(defn expand! (defn expand!
[] []
@ -2140,40 +2132,11 @@
(state/set-editor-show-block-search! false) (state/set-editor-show-block-search! false)
(util/cursor-move-forward input 2))) (util/cursor-move-forward input 2)))
;; TODO: re-implement
(defn template-on-chosen-handler (defn template-on-chosen-handler
[input id q format edit-block edit-content] [input id q format edit-block edit-content]
(fn [[template db-id] _click?] (fn [[template db-id] _click?]
(if-let [block (db/entity db-id)]
(let [new-level (:block/level edit-block)
properties (:block/properties block)
block-uuid (:block/uuid block)
including-parent? (not= (get properties "including-parent") "false")
template-parent-level (:block/level block)
pattern (config/get-block-pattern format)
content
(block-handler/get-block-full-content
(state/get-current-repo)
(:block/uuid block)
(fn [{:block/keys [uuid level content properties] :as block}]
(let [parent? (= uuid block-uuid)
ignore-parent? (and parent? (not including-parent?))]
(if ignore-parent?
""
(let [new-level (+ new-level
(- level template-parent-level
(if (not including-parent?) 1 0)))
properties' (dissoc (into {} properties) "id" "custom_id" "template" "including-parent")]
(-> content
(string/replace-first (apply str (repeat level pattern))
(apply str (repeat new-level pattern)))
text/remove-properties!
(text/rejoin-properties properties')))))))
content (if (string/includes? (string/trim edit-content) "\n")
content
(text/remove-level-spaces content format))
content (template/resolve-dynamic-template! content)]
(state/set-editor-show-template-search! false)
(insert-command! id content format {})))
(when-let [input (gdom/getElement id)] (when-let [input (gdom/getElement id)]
(.focus input)))) (.focus input))))