From 2d3f152454f4a17d60863d49fd638ed27487b053 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Tue, 11 Jun 2024 20:23:57 +0800 Subject: [PATCH] fix: simplify page delete and fix rtc issues --- deps/db/src/logseq/db.cljs | 12 -- src/main/frontend/worker/handler/page.cljs | 104 +++++------------- .../frontend/worker/rtc/remote_update.cljs | 9 +- 3 files changed, 30 insertions(+), 95 deletions(-) diff --git a/deps/db/src/logseq/db.cljs b/deps/db/src/logseq/db.cljs index b3eca052b..d64c470dd 100644 --- a/deps/db/src/logseq/db.cljs +++ b/deps/db/src/logseq/db.cljs @@ -394,18 +394,6 @@ [db property-id] (:class/_schema.properties (d/entity db property-id))) -(defn get-block-property-values - "Get blocks which have this property." - [db property-uuid] - (d/q - '[:find ?b ?v - :in $ ?property-uuid - :where - [?b :block/properties ?p] - [(get ?p ?property-uuid) ?v] - [(some? ?v)]] - db - property-uuid)) (defn get-alias-source-page "return the source page (page-name) of an alias" diff --git a/src/main/frontend/worker/handler/page.cljs b/src/main/frontend/worker/handler/page.cljs index 10c619f41..b22e2c707 100644 --- a/src/main/frontend/worker/handler/page.cljs +++ b/src/main/frontend/worker/handler/page.cljs @@ -11,7 +11,6 @@ [logseq.common.util :as common-util] [logseq.common.config :as common-config] [logseq.db.frontend.content :as db-content] - [medley.core :as medley] [frontend.worker.date :as date] [logseq.db.frontend.order :as db-order] [logseq.db.frontend.property.util :as db-property-util] @@ -175,108 +174,57 @@ (let [refs (:block/_refs page-entity) id-ref->page #(db-content/special-id-ref->page % [page-entity])] (when (seq refs) - (let [tx-data (mapcat (fn [{:block/keys [raw-content properties] :as ref}] - ;; block content or properties + (let [tx-data (mapcat (fn [{:block/keys [raw-content] :as ref}] + ;; block content (let [content' (id-ref->page raw-content) content-tx (when (not= raw-content content') {:db/id (:db/id ref) :block/content content'}) - page-uuid (:block/uuid page-entity) - properties' (-> (medley/map-vals (fn [v] - (cond - (and (coll? v) (uuid? (first v))) - (vec (remove #{page-uuid} v)) - - (and (uuid? v) (= v page-uuid)) - nil - - (and (coll? v) (string? (first v))) - (mapv id-ref->page v) - - (string? v) - (id-ref->page v) - - :else - v)) properties) - (common-util/remove-nils-non-nested)) - tx (merge - content-tx - (when (not= (seq properties) (seq properties')) - {:db/id (:db/id ref) - ;; FIXME: properties - :block/properties properties'}))] + tx content-tx] (concat [[:db/retract (:db/id ref) :block/refs (:db/id page-entity)]] (when tx [tx])))) refs)] tx-data))))) -(defn- page-unable-to-delete - "If a page is unable to delete, returns a map with more information. Otherwise returns nil" - [conn page] - (try - (cond - ;; TODO: allow users to delete not built-in properties - (contains? (:block/type page) "property") - (cond (seq (ldb/get-classes-with-property @conn (:block/uuid page))) - {:msg "Page content deleted but unable to delete this page because classes use this property"} - (seq (ldb/get-block-property-values @conn (:block/uuid page))) - {:msg "Page content deleted but unable to delete this page because blocks use this property"}) - - (contains? (:block/type page) "hidden") - {:msg "Page content deleted but unable to delete this page because there are still references to it"}) - - (catch :default e - (js/console.error e) - {:msg (str "An unexpected failure while deleting: " e)}))) - (defn delete! "Deletes a page. Returns true if able to delete page. If unable to delete, calls error-handler fn and returns false" [repo conn page-uuid & {:keys [persist-op? rename? error-handler] :or {persist-op? true error-handler (fn [{:keys [msg]}] (js/console.error msg))}}] + (assert (uuid? page-uuid) (str "frontend.worker.handler.page/delete! srong page-uuid: " (if page-uuid page-uuid "nil"))) (when (and repo page-uuid) (when-let [page (d/entity @conn [:block/uuid page-uuid])] - (let [property? (contains? (:block/type page) "property") - page-name (:block/name page) + (let [page-name (:block/name page) blocks (:block/_page page) truncate-blocks-tx-data (mapv (fn [block] [:db.fn/retractEntity [:block/uuid (:block/uuid block)]]) blocks) db-based? (sqlite-util/db-based-graph? repo)] - (if (ldb/built-in? page) + ;; TODO: maybe we should add $$$favorites to built-in pages? + (if (or (ldb/built-in? page) (contains? (:block/type page) "hidden")) (do (error-handler {:msg "Built-in page cannot be deleted"}) false) - (if-let [msg (and db-based? (page-unable-to-delete conn page))] - (do - (ldb/transact! conn truncate-blocks-tx-data - {:outliner-op :truncate-page-blocks :persist-op? persist-op?}) - (error-handler msg) - false) - (let [db @conn - file (when-not db-based? (gp-db/get-page-file db page-name)) - file-path (:file/path file) - delete-file-tx (when file - [[:db.fn/retractEntity [:file/path file-path]]]) - delete-page-tx (concat (db-refs->page repo page) - [[:db.fn/retractEntity (:db/id page)]]) + (let [db @conn + file (when-not db-based? (gp-db/get-page-file db page-name)) + file-path (:file/path file) + delete-file-tx (when file + [[:db.fn/retractEntity [:file/path file-path]]]) + delete-page-tx (concat (db-refs->page repo page) + [[:db.fn/retractEntity (:db/id page)]]) - ;; TODO: is this still needed? - delete-property-pairs-tx (when property? - (map (fn [d] [:db.fn/retractEntity (:e d)]) (d/datoms db :avet (:db/ident page)))) - tx-data (concat truncate-blocks-tx-data - delete-page-tx - delete-file-tx - delete-property-pairs-tx)] + tx-data (concat truncate-blocks-tx-data + delete-page-tx + delete-file-tx)] - (ldb/transact! conn tx-data - (cond-> {:outliner-op :delete-page - :deleted-page (str (:block/uuid page)) - :persist-op? persist-op?} - rename? - (assoc :real-outliner-op :rename-page) - file-path - (assoc :file-path file-path))) - true))))))) + (ldb/transact! conn tx-data + (cond-> {:outliner-op :delete-page + :deleted-page (str (:block/uuid page)) + :persist-op? persist-op?} + rename? + (assoc :real-outliner-op :rename-page) + file-path + (assoc :file-path file-path))) + true)))))) diff --git a/src/main/frontend/worker/rtc/remote_update.cljs b/src/main/frontend/worker/rtc/remote_update.cljs index 6b315a5fe..5b80b4b12 100644 --- a/src/main/frontend/worker/rtc/remote_update.cljs +++ b/src/main/frontend/worker/rtc/remote_update.cljs @@ -227,8 +227,7 @@ (defn- apply-remote-remove-page-ops [repo conn remove-page-ops] (doseq [op remove-page-ops] - (when-let [page-name (:block/name (d/entity @conn [:block/uuid (:block-uuid op)]))] - (worker-page/delete! repo conn page-name {:persist-op? false})))) + (worker-page/delete! repo conn (:block-uuid op) {:persist-op? false}))) (defn- filter-remote-data-by-local-unpushed-ops "when remote-data request client to move/update/remove/... blocks, @@ -299,14 +298,14 @@ (let [db @conn first-remote-parent (first parents)] (when-let [local-parent (d/entity db [:block/uuid first-remote-parent])] - (let [page-name (:block/name local-parent) + (let [page-id (:db/id local-parent) properties* (ldb/read-transit-str properties) shape-property-id (db-property-util/get-pid repo :logseq.property.tldraw/shape) shape (and (map? properties*) (get properties* shape-property-id))] - (assert (some? page-name) local-parent) + (assert (some? page-id) local-parent) (assert (some? shape) properties*) - (transact-db! :upsert-whiteboard-block conn [(gp-whiteboard/shape->block repo shape page-name)]))))) + (transact-db! :upsert-whiteboard-block conn [(gp-whiteboard/shape->block repo shape page-id)]))))) (def ^:private update-op-watched-attrs #{:block/content