update property-handler

pull/10016/head
rcmerci 2023-06-26 17:52:40 +08:00
parent 62b9819df5
commit 2d4da3c1c5
3 changed files with 50 additions and 25 deletions

View File

@ -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?)

View File

@ -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))}

View File

@ -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]