fix: parse-block preserve parent and left

pull/1656/head
Tienson Qin 2021-03-25 22:00:51 +08:00
parent db4d8c8f47
commit 84fb245d8a
3 changed files with 22 additions and 39 deletions

View File

@ -501,55 +501,39 @@
[blocks parents' result]))] [blocks parents' result]))]
(recur blocks parents result))))) (recur blocks parents result)))))
;; TODO: remove meta, pos
(defn parse-block (defn parse-block
([block format] ([block format]
(parse-block block format nil)) (parse-block block format nil))
([{:block/keys [uuid content meta file page pre-block?] :as block} format {:keys [with-id?] ([{:block/keys [uuid content meta file page pre-block? parent left] :as block}
:or {with-id? true}}] format
{:keys [with-id?]
:or {with-id? true}}]
(when-not (string/blank? content) (when-not (string/blank? content)
(let [ast (format/to-edn content format nil) (let [ast (format/to-edn content format nil)
start-pos (:start-pos meta) start-pos (:start-pos meta)
encoded-content (utf8/encode content) encoded-content (utf8/encode content)
content-length (utf8/length encoded-content) content-length (utf8/length encoded-content)
blocks (extract-blocks ast content with-id?) new-block (first (extract-blocks ast content with-id?))
ref-pages-atom (atom []) ref-pages-atom (atom [])
parent-ref-pages (->> (db/get-block-parent (state/get-current-repo) uuid) parent-ref-pages (->> (db/get-block-parent (state/get-current-repo) uuid)
:block/path-refs :block/path-refs
(map :db/id)) (map :db/id))
blocks (doall block (let [{:block/keys [refs]} new-block
(map-indexed ref-pages (filter :block/name refs)
(fn [idx {:block/keys [refs meta] :as block}] path-ref-pages (concat ref-pages parent-ref-pages)
(let [ref-pages (filter :block/name refs) block (merge
path-ref-pages (concat ref-pages parent-ref-pages) block
block (merge new-block
block {:block/path-refs path-ref-pages})]
{:block/meta meta (-> block
:block/marker (get block :block/marker "nil") (assoc-in [:block/meta :start-pos] (+ (:start-pos meta) start-pos))
:block/properties (get block :block/properties {}) (assoc-in [:block/meta :end-pos] (+ (:end-pos meta) start-pos))))
:block/file file _ (doseq [page (:refs block)]
:block/format format (when (:block/name page)
:block/page page (swap! ref-pages-atom conj page)))
:block/content (utf8/substring encoded-content
(:start-pos meta)
(:end-pos meta))
:block/path-refs path-ref-pages}
;; Preserve the original block id
(when (zero? idx)
{:block/uuid uuid})
(when (seq refs)
{:block/refs
(mapv
(fn [page]
(when (:block/name page)
(swap! ref-pages-atom conj page))
page)
refs)}))]
(-> block
(assoc-in [:block/meta :start-pos] (+ (:start-pos meta) start-pos))
(assoc-in [:block/meta :end-pos] (+ (:end-pos meta) start-pos)))))
blocks))
pages (vec (distinct @ref-pages-atom))] pages (vec (distinct @ref-pages-atom))]
{:blocks blocks {:blocks [(dissoc block :db/id)]
:pages pages :pages pages
:start-pos start-pos :start-pos start-pos
:end-pos (+ start-pos content-length)})))) :end-pos (+ start-pos content-length)}))))

View File

@ -66,9 +66,7 @@
[block] [block]
(let [refs (if-let [refs (seq (:block/refs-with-children block))] (let [refs (if-let [refs (seq (:block/refs-with-children block))]
refs refs
(concat (:block/refs block))]
(:block/refs block)
(:block/tags block)))]
(distinct refs))) (distinct refs)))
(defn prepare-blocks (defn prepare-blocks

View File

@ -397,6 +397,7 @@
tx tx
transact-option) transact-option)
;; TODO: remove
(when (seq pages) (when (seq pages)
(let [children-tx (mapcat #(rebuild-page-blocks-children repo %) pages)] (let [children-tx (mapcat #(rebuild-page-blocks-children repo %) pages)]
(when (seq children-tx) (when (seq children-tx)