Fix: failed to load when refreshing with invalid block refs

This commit is contained in:
Tienson Qin
2021-08-19 12:27:52 +08:00
parent 65d7ae5ae7
commit bfd614e1fe
3 changed files with 31 additions and 12 deletions

View File

@@ -15,6 +15,18 @@
[frontend.handler.editor :as editor]
[frontend.handler.extract :as extract]))
(defn- set-missing-block-ids!
[content]
(when (string? content)
(doseq [block-id (extract/extract-all-block-refs content)]
(when-let [block (try
(model/get-block-by-uuid block-id)
(catch js/Error _e
nil))]
(let [id-property (:id (:block/properties block))]
(when-not (= (str id-property) (str block-id))
(editor/set-block-property! block-id "id" block-id)))))))
(defn handle-changed!
[type {:keys [dir path content stat] :as payload}]
(when dir
@@ -26,9 +38,7 @@
(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)))
(set-missing-block-ids! content)
(db/set-file-last-modified-at! repo path mtime)
;; return nil, otherwise the entire db will be transfered by ipc
nil))
@@ -42,9 +52,7 @@
(> 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)))
(set-missing-block-ids! content)
(db/set-file-last-modified-at! repo path mtime)
nil)