From bfd614e1feda4f2c261459840f5f815ec7a55bff Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Thu, 19 Aug 2021 12:27:52 +0800 Subject: [PATCH] Fix: failed to load when refreshing with invalid block refs --- src/main/frontend/db/model.cljs | 6 ++++++ src/main/frontend/fs/watcher_handler.cljs | 20 ++++++++++++++------ src/main/frontend/handler/repo.cljs | 17 +++++++++++------ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/main/frontend/db/model.cljs b/src/main/frontend/db/model.cljs index 53ccec500..5f61fdddc 100644 --- a/src/main/frontend/db/model.cljs +++ b/src/main/frontend/db/model.cljs @@ -1211,6 +1211,12 @@ (reset! blocks-count-cache n) n))))) +(defn get-all-block-uuids + [] + (when-let [conn (conn/get-conn)] + (->> (d/datoms conn :avet :block/uuid) + (map :v)))) + ;; block/uuid and block/content (defn get-all-block-contents [] diff --git a/src/main/frontend/fs/watcher_handler.cljs b/src/main/frontend/fs/watcher_handler.cljs index 278dae4ac..83e1d53fc 100644 --- a/src/main/frontend/fs/watcher_handler.cljs +++ b/src/main/frontend/fs/watcher_handler.cljs @@ -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) diff --git a/src/main/frontend/handler/repo.cljs b/src/main/frontend/handler/repo.cljs index 71d08b307..c1553cc7a 100644 --- a/src/main/frontend/handler/repo.cljs +++ b/src/main/frontend/handler/repo.cljs @@ -5,6 +5,7 @@ [frontend.config :as config] [frontend.date :as date] [frontend.db :as db] + [frontend.db.model :as db-model] [frontend.dicts :as dicts] [frontend.encrypt :as encrypt] [frontend.format :as format] @@ -26,7 +27,8 @@ [frontend.util :as util] [lambdaisland.glogi :as log] [promesa.core :as p] - [shadow.resource :as rc])) + [shadow.resource :as rc] + [clojure.set :as set])) ;; Project settings should be checked in two situations: ;; 1. User changes the config.edn directly in logseq.com (fn: alter-file) @@ -147,10 +149,11 @@ (state/pub-event! [:page/create-today-journal repo-url]))))) (defn- remove-non-exists-refs! - [data] - (let [block-ids (->> (map :block/uuid data) - (remove nil?) - (set)) + [data all-block-ids] + (let [block-ids (->> (->> (map :block/uuid data) + (remove nil?) + (set)) + (set/union (set all-block-ids))) keep-block-ref-f (fn [refs] (filter (fn [ref] (if (and (vector? ref) @@ -169,7 +172,9 @@ (let [files (map #(select-keys % [:file/path :file/last-modified-at]) files) all-data (-> (concat delete-files delete-blocks files blocks-pages) (util/remove-nils)) - all-data (if refresh? all-data (remove-non-exists-refs! all-data))] + all-data (if refresh? + (remove-non-exists-refs! all-data (db-model/get-all-block-uuids)) + (remove-non-exists-refs! all-data nil))] (db/transact! repo-url all-data))) (defn- load-pages-metadata!