Merge branch 'feat/db' into refactor/boolean-not-ref-type

pull/11451/head
Tienson Qin 2024-08-03 15:10:53 +08:00
commit a85fcdb842
9 changed files with 95 additions and 78 deletions

View File

@ -264,8 +264,7 @@
(def property-common-schema-attrs
"Property :schema attributes common to all properties"
[[:hide? {:optional true} :boolean]
[:description {:optional true} :string]])
[[:hide? {:optional true} :boolean]])
(def internal-property
(vec
@ -371,10 +370,7 @@
[:db/ident {:optional true} logseq-property-ident]
[:block/title {:optional true} :string]
[:property.value/content {:optional true} [:or :string :double]]
[:block/closed-value-property {:optional true} [:set :int]]
[:block/schema {:optional true}
[:map
[:description {:optional true} :string]]]]
[:block/closed-value-property {:optional true} [:set :int]] ]
(remove #(#{:block/title} (first %)) block-attrs)
page-or-block-attrs)))

View File

@ -150,6 +150,10 @@
:hide? true
:view-context :page
:public? true}}
:logseq.property/description {:schema
{:type :default
:public? true}}
:logseq.property.table/sorting {:schema
{:type :coll
:hide? true

View File

@ -21,7 +21,7 @@
(defn build-closed-value-block
"Builds a closed value block to be transacted"
[block-uuid block-value property {:keys [db-ident icon description]}]
[block-uuid block-value property {:keys [db-ident icon]}]
(assert block-uuid (uuid? block-uuid))
(cond->
(closed-value-new-block block-uuid block-value property)
@ -31,9 +31,6 @@
icon
(assoc :logseq.property/icon icon)
description
(update :block/schema assoc :description description)
true
sqlite-util/block-with-timestamps))
@ -46,13 +43,13 @@
:ref-type? true})
property-attributes)]
(into [property-tx]
(map (fn [{:keys [db-ident value icon description uuid]}]
(map (fn [{:keys [db-ident value icon uuid]}]
(cond->
(build-closed-value-block
uuid
value
property
{:db-ident db-ident :icon icon :description description})
{:db-ident db-ident :icon icon})
true
(assoc :block/order (db-order/gen-key))))
(:closed-values property)))))

View File

@ -82,34 +82,32 @@
(defn- property-with-values
[db block]
(when (entity-plus/db-based-graph? db)
(let [block (d/entity db (:db/id block))
block-properties (when (seq (:block/properties block))
(mapcat
(fn [[_property-id property-values]]
(let [values (if (and (coll? property-values)
(map? (first property-values)))
property-values
#{property-values})
value-ids (when (every? map? values)
(->> (map :db/id values)
(filter (fn [id] (or (int? id) (keyword? id))))))
value-blocks (->>
(when (seq value-ids)
(map
(fn [id] (d/pull db '[*] id))
value-ids))
;; FIXME: why d/pull returns {:db/id db-ident} instead of {:db/id number-eid}?
(map (fn [block]
(let [from-property-id (get-in block [:logseq.property/created-from-property :db/id])]
(if (keyword? from-property-id)
(assoc-in block [:logseq.property/created-from-property :db/id] (:db/id (d/entity db from-property-id)))
block)))))
page (when (seq values)
(when-let [page-id (:db/id (:block/page (d/entity db (:db/id (first values)))))]
(d/pull db '[*] page-id)))]
(remove nil? (concat [page] value-blocks))))
(:block/properties block)))]
block-properties)))
(let [block (d/entity db (:db/id block))]
(->> (:block/properties block)
vals
(mapcat
(fn [property-values]
(let [values (->>
(if (and (coll? property-values)
(map? (first property-values)))
property-values
#{property-values})
(remove sqlite-util/page?))
value-ids (when (every? map? values)
(->> (map :db/id values)
(filter (fn [id] (or (int? id) (keyword? id))))))
value-blocks (->>
(when (seq value-ids)
(map
(fn [id] (d/pull db '[*] id))
value-ids))
;; FIXME: why d/pull returns {:db/id db-ident} instead of {:db/id number-eid}?
(keep (fn [block]
(let [from-property-id (get-in block [:logseq.property/created-from-property :db/id])]
(if (keyword? from-property-id)
(assoc-in block [:logseq.property/created-from-property :db/id] (:db/id (d/entity db from-property-id)))
block)))))]
value-blocks)))))))
(defn get-block-children-ids
"Returns children UUIDs"

View File

@ -397,32 +397,24 @@
(seq properties))))
(defn- build-closed-value-tx
[db property resolved-value {:keys [id icon description]
:or {description ""}}]
[db property resolved-value {:keys [id icon]}]
(let [block (when id (d/entity db [:block/uuid id]))
block-id (or id (ldb/new-block-id))
icon (when-not (and (string? icon) (string/blank? icon)) icon)
description (string/trim description)
description (when-not (string/blank? description) description)
tx-data (if block
[(let [schema (:block/schema block)]
(cond->
(outliner-core/block-with-updated-at
(merge
{:block/uuid id
:block/closed-value-property (:db/id property)
:block/schema (if description
(assoc schema :description description)
(dissoc schema :description))}
(if (db-property-type/original-value-ref-property-types (get-in property [:block/schema :type]))
{:property.value/content resolved-value}
{:block/title resolved-value})))
icon
(assoc :logseq.property/icon icon)))]
[(cond->
(outliner-core/block-with-updated-at
(merge
{:block/uuid id
:block/closed-value-property (:db/id property)}
(if (db-property-type/original-value-ref-property-types (get-in property [:block/schema :type]))
{:property.value/content resolved-value}
{:block/title resolved-value})))
icon
(assoc :logseq.property/icon icon))]
(let [max-order (:block/order (last (:property/closed-values property)))
new-block (-> (db-property-build/build-closed-value-block block-id resolved-value
property {:icon icon
:description description})
property {:icon icon})
(assoc :block/order (db-order/gen-key max-order nil)))]
[new-block
(outliner-core/block-with-updated-at
@ -434,7 +426,7 @@
(defn upsert-closed-value!
"id should be a block UUID or nil"
[conn property-id {:keys [id value] :as opts}]
[conn property-id {:keys [id value description] :as opts}]
(assert (or (nil? id) (uuid? id)))
(let [db @conn
property (d/entity db property-id)
@ -472,9 +464,20 @@
nil
:else
(ldb/transact! conn
(build-closed-value-tx @conn property resolved-value opts)
{:outliner-op :save-block}))))))
(let [tx-data (build-closed-value-tx @conn property resolved-value opts)]
(ldb/transact! conn tx-data {:outliner-op :save-block})
(when (seq description)
(if-let [desc-ent (and id (:logseq.property/description (d/entity db [:block/uuid id])))]
(ldb/transact! conn
[(outliner-core/block-with-updated-at {:db/id (:db/id desc-ent)
:block/title description})]
{:outliner-op :save-block})
(set-block-property! conn
;; new closed value is first in tx-data
[:block/uuid (or id (:block/uuid (first tx-data)))]
:logseq.property/description
description)))))))))
(defn add-existing-values-to-closed-values!
"Adds existing values as closed values and returns their new block uuids"

View File

@ -255,7 +255,7 @@
:description "choice 4"})
updated-b (d/entity @conn [:block/uuid (:block/uuid b)])]
(is (= 4 (db-property/closed-value-content updated-b)))
(is (= "choice 4" (:description (:block/schema updated-b))))))))
(is (= "choice 4" (db-property/property-value-content (:logseq.property/description updated-b))))))))
(deftest delete-closed-value!
(let [closed-value-uuid (random-uuid)

View File

@ -65,7 +65,7 @@
(cond-> {:block/title class-name
:build/properties (cond-> {:url url}
(class-m "rdfs:comment")
(assoc :description (get-comment-string (class-m "rdfs:comment") renamed-pages)))}
(assoc :logseq.property/description (get-comment-string (class-m "rdfs:comment") renamed-pages)))}
parent-class'
(assoc :build/class-parent (keyword (strip-schema-prefix parent-class')))
(seq properties)
@ -117,12 +117,12 @@
schema (cond-> {:type schema-type}
;; This cardinality rule should be adjusted as we use schema.org more
(= schema-type :node)
(assoc :cardinality :many)
(property-m "rdfs:comment")
(assoc :description (get-comment-string (property-m "rdfs:comment") renamed-pages)))]
(assoc :cardinality :many))]
{(keyword (strip-schema-prefix (property-m "@id")))
(cond-> {:block/schema schema
:build/properties {:url url}}
:build/properties (cond-> {:url url}
(property-m "rdfs:comment")
(assoc :logseq.property/description (get-comment-string (property-m "rdfs:comment") renamed-pages)))}
(= schema-type :node)
(assoc :build/schema-classes (mapv (comp keyword strip-schema-prefix) range-includes)))}))
@ -256,8 +256,13 @@
(get-schema-type (get-range-includes property-m) class-map)))
frequencies)
"\n"))
(apply merge
(mapv #(->property-page % class-map options) select-properties)))
(assoc
(apply merge
(mapv #(->property-page % class-map options) select-properties))
;; Have to update schema for now as validation doesn't take into account existing properties
:logseq.property/description {:block/schema {:public? true :type :default}
:build/properties {:url "https://schema.org/description"
:logseq.property/description "A description of the item."}}))
(defn- get-all-classes-and-properties
"Get all classes and properties from raw json file"
@ -267,7 +272,8 @@
(if (string? type') [type'] type')))
"rdfs:Class")
schema-data)
all-properties* (get-all-properties schema-data options)
;; Use built-in description
all-properties* (remove #(= "schema:description" (% "@id")) (get-all-properties schema-data options))
property-tuples (map #(vector (% "@id") :property) all-properties*)
class-tuples (map #(vector (% "@id") :class) all-classes*)
page-tuples (map #(vector (str "schema:" %) :node) existing-pages)
@ -276,6 +282,7 @@
renamed-properties (detect-property-conflicts-and-get-renamed-properties
property-tuples page-tuples options)
renamed-pages (merge renamed-classes renamed-properties)
;; Note: schema:description refs don't get renamed but they aren't used
;; Updates keys like @id, @subClassOf
rename-page-ids (fn [m]
(w/postwalk (fn [x]

View File

@ -238,6 +238,7 @@
db-mixins/query
(rum/local nil ::property-name)
(rum/local nil ::property-schema)
(rum/local nil ::property-description)
{:init (fn [state]
(let [*values (atom :loading)]
(p/let [result (db-async/<get-block-property-values (state/get-current-repo)
@ -248,6 +249,7 @@
(let [[property _opts] (:rum/args state)]
(reset! (::property-name state) (:block/title property))
(reset! (::property-schema state) (:block/schema property))
(reset! (::property-description state) (db-property/property-value-content (:logseq.property/description property)))
(state/set-state! :editor/property-configure? true)
state))
:will-unmount (fn [state]
@ -260,6 +262,7 @@
(when-not (= :loading values)
(let [*property-name (::property-name state)
*property-schema (::property-schema state)
*property-description (::property-description state)
property (db/sub-block (:db/id property))
built-in? (ldb/built-in? property)
disabled? (or built-in? config/publishing?)
@ -391,15 +394,24 @@
(swap! *property-schema assoc :hide? (not hide?))
(save-property-fn))})])
(let [description (or (:description @*property-schema) "")]
(let [description (or @*property-description "")]
[:div.grid.grid-cols-5.gap-1.items-start.leading-8
[:label.col-span-2 "Description:"]
[:div.col-span-3
[:div.mt-1
(shui/textarea
{:on-change (fn [e]
(swap! *property-schema assoc :description (util/evalue e)))
:on-blur save-property-fn
(reset! *property-description (util/evalue e)))
:on-blur (fn []
(if-let [ent (:logseq.property/description property)]
(db/transact! (state/get-current-repo)
[(outliner-core/block-with-updated-at
{:db/id (:db/id ent) :block/title @*property-description})]
{:outliner-op :save-block})
(db-property-handler/set-block-property!
(:db/id property)
:logseq.property/description
@*property-description)))
:disabled disabled?
:default-value description})]]])]]))))
@ -716,7 +728,7 @@
[:div.flex.flex-1
(if (and (:class-schema? opts) (:page-configure? opts))
[:div.property-description.text-sm.opacity-70
(inline-text {} :markdown (get-in property [:block/schema :description]))]
(inline-text {} :markdown (db-property/property-value-content (:logseq.property/description property)))]
[:div.property-value.flex.flex-1
(pv/property-value block property v opts)])]]]))))

View File

@ -63,7 +63,7 @@
(let [block (second (:rum/args state))
value (or (str (db-property/closed-value-content block)) "")
icon (:logseq.property/icon block)
description (or (get-in block [:block/schema :description]) "")]
description (or (db-property/property-value-content (:logseq.property/description block)) "")]
(assoc state
::value (atom value)
::icon (atom icon)