fix: setting block properties with raw value

With this fix able to convert 2 built-ins to :default. Also cleaned
up set-block-property! which had outdated assumptions
experiment/tanstack-table
Gabriel Horner 2024-05-15 15:40:10 -04:00
parent 2117245b28
commit e50d3eff21
3 changed files with 31 additions and 45 deletions

View File

@ -41,7 +41,7 @@
:public? true
:view-context :page
:cardinality :many}}
:logseq.property/background-color {:schema {:type :string :hide? true}}
:logseq.property/background-color {:schema {:type :default :hide? true}}
:logseq.property/background-image {:schema
{:type :string
:view-context :block
@ -76,7 +76,7 @@
:hide? true}}
:logseq.property/hl-stamp {:schema {:type :number
:hide? true}}
:logseq.property/hl-color {:schema {:type :string
:logseq.property/hl-color {:schema {:type :default
:hide? true}}
:logseq.property/order-list-type {:name :logseq.order-list-type
:schema {:type :string

View File

@ -247,8 +247,10 @@
raw-value)))
(defn set-block-property!
"Updates a block property's value for an existing property-id. Property value
is translated from input, sanitized and validated. Also handle db attributes as properties"
"Updates a block property's value for an existing property-id and block.
Property value is sanitized and if property is a ref type, automatically
handles a raw property value i.e. you can pass \"value\" instead of the
property value entity. Also handle db attributes as properties"
[conn block-eid property-id v]
(let [block-eid (->eid block-eid)
db @conn
@ -256,53 +258,36 @@
block (d/entity @conn block-eid)
property (d/entity @conn property-id)
_ (assert (some? property) (str "Property " property-id " doesn't exists yet"))
property-schema (:block/schema property)
{:keys [type] :or {type :default}} property-schema
property-type (get-in property [:block/schema :type] :default)
v' (or (resolve-tag! conn v) v)
db-attribute? (contains? db-property/db-attribute-properties property-id)
ref-type? (db-property-type/ref-property-types type)]
ref-type? (db-property-type/ref-property-types property-type)]
(if db-attribute?
(d/transact! conn [{:db/id (:db/id block) property-id v'}]
{:outliner-op :save-block})
(let [v' (cond
(= v' :logseq.property/empty-placeholder)
(if (= type :checkbox) false v')
(let [new-value* (cond
(= v' :logseq.property/empty-placeholder)
(if (= property-type :checkbox) false v')
ref-type?
(if (and (integer? v')
(or (and (= type :number) (= property-id (:db/ident (:logseq.property/created-from-property (d/entity db v')))))
(not= type :number)))
v'
(or (get-property-value-eid db property-id (str v'))
(let [v-uuid (create-property-text-block! conn nil (:db/id property) (str v') {})]
(:db/id (d/entity @conn [:block/uuid v-uuid])))))
:else
v')
v'' (if property v' (or v' ""))]
(when (some? v'')
(let [infer-schema (infer-schema-from-input-string v'')
property-type' (or type infer-schema)
existing-value (when-let [id (:db/ident property)]
(get block id))
new-value* (if (= v'' :logseq.property/empty-placeholder)
v''
(try
(convert-property-input-string property-type' v'')
(catch :default e
(js/console.error e)
(throw (ex-info "Property converted failed"
{:type :notification
:payload {:message (str e)
:type :error}}))
nil)))
;; don't modify maps
new-value (if (or (sequential? new-value*) (set? new-value*))
(if (= :coll property-type')
(vec (remove string/blank? new-value*))
(set (remove string/blank? new-value*)))
new-value*)]
(when-not (= existing-value new-value)
(raw-set-block-property! conn block property property-type' new-value))))))))
ref-type?
(if (and (integer? v')
(or (and (= property-type :number) (= property-id (:db/ident (:logseq.property/created-from-property (d/entity db v')))))
(not= property-type :number)))
v'
;; Get or create a property value by its raw value
(or (get-property-value-eid db property-id (str v'))
(let [v-uuid (create-property-text-block! conn nil (:db/id property) (str v') {})]
(:db/id (d/entity @conn [:block/uuid v-uuid])))))
:else
v')
new-value (if (or (sequential? new-value*) (set? new-value*))
(if (= :coll property-type)
(vec (remove string/blank? new-value*))
(set (remove string/blank? new-value*)))
new-value*)
existing-value (get block property-id)]
(when-not (= existing-value new-value)
(raw-set-block-property! conn block property property-type new-value))))))
(defn batch-set-property!
"Notice that this works only for properties with cardinality equals to `one`."

View File

@ -211,6 +211,7 @@
(assoc :id (str id)))
properties (wrap-props props)]
(when (string? text)
;; FIXME: Handle properties for db graphs
(editor-handler/api-insert-new-block!
text (merge {:page (:block/name ref-page)
:custom-uuid id