Skip to content

Commit

Permalink
Fix: block id loss for references by file altering (logseq#2664)
Browse files Browse the repository at this point in the history
Co-authored-by: Tienson Qin <[email protected]>
  • Loading branch information
gombiuda and tiensonqin authored Aug 19, 2021
1 parent a97b4fe commit 65d7ae5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/frontend/fs/watcher_handler.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
[frontend.db :as db]
[frontend.state :as state]
[clojure.string :as string]
[frontend.encrypt :as encrypt]))
[frontend.encrypt :as encrypt]
[frontend.db.model :as model]
[frontend.handler.editor :as editor]
[frontend.handler.extract :as extract]))

(defn handle-changed!
[type {:keys [dir path content stat] :as payload}]
Expand All @@ -23,6 +26,9 @@
(when-not (db/file-exists? repo path)
(let [_ (file-handler/alter-file repo path content {:re-render-root? true
:from-disk? true})]
(doseq [block-id (extract/extract-all-block-refs content)]
(if (model/get-block-by-uuid block-id)
(editor/set-block-property! block-id "id" block-id)))
(db/set-file-last-modified-at! repo path mtime)
;; return nil, otherwise the entire db will be transfered by ipc
nil))
Expand All @@ -36,6 +42,9 @@
(> mtime last-modified-at)))
(let [_ (file-handler/alter-file repo path content {:re-render-root? true
:from-disk? true})]
(doseq [block-id (extract/extract-all-block-refs content)]
(if (model/get-block-by-uuid block-id)
(editor/set-block-property! block-id "id" block-id)))
(db/set-file-last-modified-at! repo path mtime)
nil)

Expand Down
4 changes: 4 additions & 0 deletions src/main/frontend/handler/extract.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,7 @@
block-ids-set (set (map (fn [{:block/keys [uuid]}] [:block/uuid uuid]) block-ids))
blocks (map #(remove-illegal-refs % block-ids-set refresh?) blocks)]
(apply concat [pages-index pages block-ids blocks])))))))))

(defn extract-all-block-refs
[content]
(map second (re-seq #"\(\(([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})\)\)" content)))

0 comments on commit 65d7ae5

Please sign in to comment.