From 2d4da3c1c5db3102b7b8eebdd069a48b030268c8 Mon Sep 17 00:00:00 2001 From: rcmerci Date: Mon, 26 Jun 2023 17:52:40 +0800 Subject: [PATCH] update property-handler --- src/main/frontend/components/block.cljs | 5 +- src/main/frontend/components/property.cljs | 4 +- src/main/frontend/handler/property.cljs | 66 ++++++++++++++-------- 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index 9032f88f8d..a397229f3a 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -2855,7 +2855,10 @@ (block-right-menu config block edit?))] (when (config/db-based-graph? repo) - (property-component/properties-area block (:block/properties block) edit-input-id)) + (property-component/properties-area block + (:block/properties block) + (:block/properties-text-values block) + edit-input-id)) (block-children config block children collapsed?) diff --git a/src/main/frontend/components/property.cljs b/src/main/frontend/components/property.cljs index 2634dd8dc3..844212242a 100644 --- a/src/main/frontend/components/property.cljs +++ b/src/main/frontend/components/property.cljs @@ -83,7 +83,7 @@ (ui/icon "circle-plus")]))) (rum/defc properties-area < rum/static - [block properties edit-input-id] + [block properties properties-text-values edit-input-id] (let [repo (state/get-current-repo)] [:div.ls-properties-area.pl-6 (when (seq properties) @@ -96,7 +96,7 @@ [:a.mr-2 {:on-click (fn [] (state/set-modal! #(property-class-config repo (uuid prop-uuid-or-built-in-prop))))} (:block/name property-class)] - [:span v] + [:span (or (get properties-text-values prop-uuid-or-built-in-prop) v)] [:a.ml-8 {:on-click (fn [] (property-handler/remove-property! repo block prop-uuid-or-built-in-prop))} diff --git a/src/main/frontend/handler/property.cljs b/src/main/frontend/handler/property.cljs index 27ea531153..2df7f98399 100644 --- a/src/main/frontend/handler/property.cljs +++ b/src/main/frontend/handler/property.cljs @@ -13,9 +13,23 @@ [logseq.graph-parser.mldoc :as gp-mldoc] [logseq.graph-parser.util :as gp-util] [logseq.graph-parser.util.page-ref :as page-ref] - [malli.core :as m] [malli.util :as mu])) +(def builtin-schema-types + {:string-contains-refs :string ;default + :refs [:sequential :string]}) + +(def ^:private gp-mldoc-config (gp-mldoc/default-config :markdown)) + +(defn extract-page-refs-from-prop-str-value + [str-v] + (let [ast-refs (gp-mldoc/get-references str-v gp-mldoc-config) + refs (map #(gp-block/get-page-reference % #{}) ast-refs) + refs' (->> refs + (remove string/blank?) + distinct)] + refs')) + (defn add-property! [repo block k-name v] (when-let [v* (try (edn/read-string v) @@ -23,27 +37,41 @@ (notification/show! (str e) :error false) nil))] (let [property-class (db/pull repo '[*] [:block/name k-name]) - property-class-uuid (or (:block/uuid property-class) (random-uuid))] - (if-let [msg (some-> (:block/schema property-class) - (malli.util/explain-data v*))] + property-class-uuid (or (:block/uuid property-class) (random-uuid)) + property-schema (:block/schema property-class) + schema* (get builtin-schema-types property-schema property-schema)] + (if-let [msg (some-> schema* (malli.util/explain-data v*))] (notification/show! (str msg) :error false) - (let [tx-data (cond-> [] - (nil? property-class) (conj {:block/schema :any - :block/name k-name - :block/uuid property-class-uuid - :block/type "property"}) - true (conj {:block/uuid (:block/uuid block) - :block/properties (assoc (:block/properties block) (str property-class-uuid) v*)}))] - (db/transact! repo tx-data)))))) + (do (when (nil? property-class) ;if property-class not exists yet + (db/transact! repo [{:block/schema :string-contains-refs + :block/name k-name + :block/uuid property-class-uuid + :block/type "property"}])) + (let [block-properties (assoc (:block/properties block) + (str property-class-uuid) + (if (= property-schema :string-contains-refs) + (set (extract-page-refs-from-prop-str-value v*)) + v*)) + block-properties-text-values (cond-> (:block/properties-text-values block) + (= property-schema :string-contains-refs) + (assoc (str property-class-uuid) v*))] + (outliner-tx/transact! + {:outliner-op :save-block} + (outliner-core/save-block! + {:block/uuid (:block/uuid block) + :block/properties block-properties + :block/properties-text-values block-properties-text-values})))))))) (defn remove-property! [repo block k-uuid-or-builtin-k-name] {:pre (string? k-uuid-or-builtin-k-name)} (let [origin-properties (:block/properties block)] (assert (contains? (set (keys origin-properties)) k-uuid-or-builtin-k-name)) - (db/transact! repo - [{:block/uuid (:block/uuid block) - :block/properties (dissoc origin-properties k-uuid-or-builtin-k-name)}]))) + (db/transact! + repo + [{:block/uuid (:block/uuid block) + :block/properties (dissoc origin-properties k-uuid-or-builtin-k-name) + :block/properties-text-values (dissoc (:block/properties-text-values block) k-uuid-or-builtin-k-name)}]))) (defn update-property-class! @@ -54,13 +82,7 @@ property-schema (assoc :block/schema property-schema))] (db/transact! repo [tx-data]))) -(defn explain-property-value - [repo property-uuid property-value] - {:pre [(uuid? property-uuid)]} - (let [prop-entity (db/entity repo [:block/uuid property-uuid])] - (assert (= "property" (:block/type prop-entity)) prop-entity) - (when-let [schema (:block/schema prop-entity)] - (m/explain schema property-value)))) + (defn- extract-refs [entity properties]