diff --git a/src/main/frontend/db/datascript/entity_plus.cljs b/src/main/frontend/db/datascript/entity_plus.cljs index b6b7c6a0d5..3d3243c46d 100644 --- a/src/main/frontend/db/datascript/entity_plus.cljs +++ b/src/main/frontend/db/datascript/entity_plus.cljs @@ -10,7 +10,11 @@ (defn lookup-kv-then-entity ([e k] (lookup-kv-then-entity e k nil)) ([^Entity e k default-value] - (if (and (= k :block/content) (config/db-based-graph? (state/get-current-repo))) + (cond + (and (= k :block/raw-content) (config/db-based-graph? (state/get-current-repo))) + (lookup-entity e :block/content default-value) + + (and (= k :block/content) (config/db-based-graph? (state/get-current-repo))) (let [result (lookup-entity e k default-value) refs (:block/refs e)] (or @@ -18,6 +22,8 @@ (db-utils/special-id->page result refs) result) default-value)) + + :else (or (get (.-kv e) k) (lookup-entity e k default-value))))) diff --git a/src/main/frontend/db/utils.cljs b/src/main/frontend/db/utils.cljs index 8dc8a2656c..eb0e7f536c 100644 --- a/src/main/frontend/db/utils.cljs +++ b/src/main/frontend/db/utils.cljs @@ -6,7 +6,8 @@ [frontend.db.conn :as conn] [frontend.config :as config] [logseq.graph-parser.util :as gp-util] - [clojure.string :as string])) + [clojure.string :as string] + [logseq.graph-parser.util.page-ref :as page-ref])) ;; transit serialization @@ -72,6 +73,22 @@ content refs)) +(defn special-id-ref->page + "Convert special id ref backs to page name." + [content refs] + (reduce + (fn [content ref] + (if (:block/name ref) + (string/replace content + (str page-ref/left-brackets + config/page-ref-special-chars + (:block/uuid ref) + page-ref/right-brackets) + (:block/original-name ref)) + content)) + content + refs)) + (defn update-block-content "Replace `[[internal-id]]` with `[[page name]]`" [item eid] diff --git a/src/main/frontend/handler/page.cljs b/src/main/frontend/handler/page.cljs index bb0241cfc0..c800d41f25 100644 --- a/src/main/frontend/handler/page.cljs +++ b/src/main/frontend/handler/page.cljs @@ -45,7 +45,8 @@ [logseq.graph-parser.util :as gp-util] [logseq.graph-parser.util.page-ref :as page-ref] [promesa.core :as p] - [logseq.common.path :as path])) + [logseq.common.path :as path] + [medley.core :as medley])) ;; FIXME: add whiteboard (defn- get-directory @@ -368,6 +369,45 @@ (unfavorite-page! page-name) (favorite-page! page-name))))) +(defn db-refs->page + "Replace [[page name]] with page name" + [repo page-entity] + (when (config/db-based-graph? repo) + (let [refs (:block/_refs page-entity) + id-ref->page #(db-utils/special-id-ref->page % [page-entity])] + (when (seq refs) + (let [tx-data (keep (fn [{:block/keys [raw-content properties] :as ref}] + ;; block content or properties + (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) + (util/remove-nils-non-nested)) + tx (merge + content-tx + (when (not= (seq properties) (seq properties')) + {:db/id (:db/id ref) + :block/properties properties'}))] + tx)) refs)] + tx-data))))) + (defn delete! [page-name ok-handler & {:keys [delete-file?] :or {delete-file? true}}] @@ -376,25 +416,34 @@ (when-let [repo (state/get-current-repo)] (let [page-name (util/page-name-sanity-lc page-name) blocks (db/get-page-blocks-no-cache page-name) - tx-data (mapv - (fn [block] - [:db.fn/retractEntity [:block/uuid (:block/uuid block)]]) - blocks) - page (db/entity [:block/name page-name])] - (db/transact! tx-data) + truncate-blocks-tx-data (mapv + (fn [block] + [:db.fn/retractEntity [:block/uuid (:block/uuid block)]]) + blocks) + page (db/entity [:block/name page-name]) + _ (delete-file! repo page-name delete-file?) + ;; if other page alias this pagename, + ;; then just remove some attrs of this entity instead of retractEntity + delete-page-tx (cond + (contains? #{"property" "class"} (:block/type page)) + nil - (delete-file! repo page-name delete-file?) + (not (:block/_namespace page)) + (if (model/get-alias-source-page (state/get-current-repo) page-name) + (when-let [id (:db/id (db/entity [:block/name page-name]))] + (mapv (fn [attribute] + [:db/retract id attribute]) + db-schema/retract-page-attributes)) + (concat (db-refs->page repo page) + [[:db.fn/retractEntity [:block/name page-name]]])) - ;; if other page alias this pagename, - ;; then just remove some attrs of this entity instead of retractEntity - (when-not (:block/_namespace page) - (if (model/get-alias-source-page (state/get-current-repo) page-name) - (when-let [id (:db/id (db/entity [:block/name page-name]))] - (let [txs (mapv (fn [attribute] - [:db/retract id attribute]) - db-schema/retract-page-attributes)] - (db/transact! txs))) - (db/transact! [[:db.fn/retractEntity [:block/name page-name]]]))) + :else + nil) + tx-data (concat truncate-blocks-tx-data delete-page-tx)] + + (util/pprint tx-data) + + (db/transact! repo tx-data {:outliner-op :delete-page}) (unfavorite-page! page-name)