chore: move db only property util fns to their own ns

by doing this found a couple places where we accidentally called db fns
in file graph contexts
This commit is contained in:
Gabriel Horner
2023-11-17 16:36:09 -05:00
parent 6f9143df4f
commit e95531869a
16 changed files with 103 additions and 81 deletions

View File

@@ -82,6 +82,7 @@
frontend.handler.db-based.editor db-editor-handler
frontend.handler.db-based.page db-page-handler
frontend.handler.db-based.property db-property-handler
frontend.handler.db-based.property.util db-pu
frontend.handler.file-based.page file-page-handler
frontend.handler.file-based.property file-property-handler
frontend.handler.file-based.page-property file-page-property

View File

@@ -52,6 +52,7 @@
[frontend.handler.export.common :as export-common-handler]
[frontend.handler.property :as property-handler]
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]
[frontend.mobile.util :as mobile-util]
[frontend.modules.outliner.tree :as tree]
[frontend.security :as security]
@@ -614,8 +615,7 @@
(let [repo (state/get-current-repo)
block-id (:block/uuid config)
block (when block-id (db/entity [:block/uuid block-id]))
tags-id (pu/get-built-in-property-uuid repo :tags)]
block (when block-id (db/entity [:block/uuid block-id]))]
(when (and block tag? @*hover? (config/db-based-graph? repo)
display-close-button?)
[:a.close.fade-in
@@ -625,8 +625,9 @@
:on-mouse-down
(fn [e]
(util/stop e)
(property-handler/delete-property-value! repo block
tags-id
(property-handler/delete-property-value! repo
block
(db-pu/get-built-in-property-uuid repo :tags)
(:block/uuid page-entity)))}
(ui/icon "x" {:size 15})]))]))
@@ -3188,9 +3189,12 @@
(defn table
[config {:keys [header groups col_groups]}]
(case (get-shui-component-version :table config)
2 (let [v2-config (assoc-in config [:block :properties]
(pu/readable-properties (get-in config [:block :block/properties])))]
2 (let [v2-config (cond-> config
(config/db-based-graph? (state/get-current-repo))
(assoc-in [:block :properties]
(db-pu/readable-properties (get-in config [:block :block/properties]))))]
(shui/table-v2 {:data (concat [[header]] groups)}
(make-shui-context v2-config inline)))
1 (let [tr (fn [elm cols]

View File

@@ -4,6 +4,7 @@
[frontend.extensions.sci :as sci]
[frontend.handler.common :as common-handler]
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]
[frontend.state :as state]
[goog.string :as gstring]
[goog.string.format]
@@ -48,7 +49,7 @@
int? (some integer? vals)
repo (state/get-current-repo)
prop-key (if (config/db-based-graph? repo)
(or (pu/get-user-property-uuid repo f)
(or (db-pu/get-user-property-uuid repo f)
;; Fall back to the keyword for queries that set named properties through :result-transform
f)
f)]

View File

@@ -13,6 +13,7 @@
[frontend.components.property.value :as pv]
[frontend.components.class :as class-component]
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]
[frontend.handler.property :as property-handler]
[frontend.components.svg :as svg]
[frontend.config :as config]
@@ -453,9 +454,9 @@
(reset! *hover? false))}
(when icon
[:div.page-icon {:on-mouse-down util/stop-propagation}
(if (map? icon)
(if (and (map? icon) db-based?)
(property/icon icon {:on-chosen (fn [_e icon]
(let [icon-property-id (pu/get-built-in-property-uuid :icon)]
(let [icon-property-id (db-pu/get-built-in-property-uuid :icon)]
(property-handler/update-property!
repo
(:block/uuid page)

View File

@@ -14,6 +14,7 @@
[frontend.handler.property :as property-handler]
[frontend.handler.page :as page-handler]
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]
[frontend.mixins :as mixins]
[frontend.modules.shortcut.core :as shortcut]
[frontend.search :as search]
@@ -159,7 +160,7 @@
(closed-value/icon icon-value
{:disabled? disabled?
:on-chosen (fn [_e icon]
(let [icon-property-id (pu/get-built-in-property-uuid :icon)]
(let [icon-property-id (db-pu/get-built-in-property-uuid :icon)]
(property-handler/update-property!
(state/get-current-repo)
(:block/uuid property)
@@ -492,7 +493,7 @@
(icon-component/icon-search
{:on-chosen
(fn [_e icon]
(let [icon-property-id (pu/get-built-in-property-uuid :icon)]
(let [icon-property-id (db-pu/get-built-in-property-uuid :icon)]
(when icon
(property-handler/update-property! repo
(:block/uuid property)
@@ -617,7 +618,7 @@
(sort-by first block-properties))
alias (set (map :block/uuid (:block/alias block)))
alias-properties (when (seq alias)
[[(pu/get-built-in-property-uuid :alias) alias]])
[[(db-pu/get-built-in-property-uuid :alias) alias]])
remove-built-in-properties (fn [properties]
(remove (fn [x]
(let [id (if (uuid? x) x (first x))]

View File

@@ -18,7 +18,8 @@
[rum.core :as rum]
[logseq.graph-parser.text :as text]
[logseq.db.frontend.property :as db-property]
[frontend.handler.property.util :as pu]))
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]))
;; Util fns
;; ========
@@ -108,7 +109,7 @@
hidden-properties (if db-graph?
;; TODO: Support additional hidden properties e.g. from user config
;; or gp-property/built-in-extended properties
(set (map #(pu/get-built-in-property-uuid repo %)
(set (map #(db-pu/get-built-in-property-uuid repo %)
db-property/built-in-properties-keys-str))
(conj (file-property-handler/built-in-properties) :template))
prop-keys* (->> (distinct (mapcat keys (map :block/properties result)))
@@ -296,9 +297,11 @@
sort-result (sort-result result (assoc sort-state :page? page?))
table-version (get-shui-component-version :table config)]
(case table-version
2 (let [v2-columns (mapv #(if (uuid? %) (pu/get-property-name %) %) columns)
v2-config (assoc-in config [:block :properties]
(pu/readable-properties (get-in config [:block :block/properties])))
2 (let [v2-columns (mapv #(if (uuid? %) (db-pu/get-property-name %) %) columns)
v2-config (cond-> config
db-graph?
(assoc-in [:block :properties]
(db-pu/readable-properties (get-in config [:block :block/properties]))))
result-as-text (for [row result]
(for [column columns]
(build-column-text row column)))]

View File

@@ -11,7 +11,7 @@
[frontend.db :as db]
[frontend.modules.outliner.tree :as outliner-tree]
[frontend.state :as state]
[frontend.handler.property.util :as pu]))
[frontend.handler.db-based.property.util :as db-pu]))
(defn loaded? []
js/window.Reveal)
@@ -25,7 +25,7 @@
properties
(fn [k]
(-> (str "data-" (if (config/db-based-graph? (state/get-current-repo))
(pu/get-property-name k)
(db-pu/get-property-name k)
(name k)))
(string/replace "data-data-" "data-")))))
m)))

View File

@@ -11,7 +11,7 @@
[logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.property :as gp-property]
[logseq.graph-parser.mldoc :as gp-mldoc]
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]
[lambdaisland.glogi :as log]
[frontend.util :as util]
[datascript.core :as d]
@@ -25,7 +25,7 @@
(let [repo (state/get-current-repo)
update-properties (fn [props]
(update-keys props #(if (contains? db-property/built-in-properties-keys %)
(pu/get-built-in-property-uuid repo %)
(db-pu/get-built-in-property-uuid repo %)
%)))]
(if (config/db-based-graph? repo)
(->> blocks

View File

@@ -6,7 +6,7 @@
[frontend.handler.notification :as notification]
[frontend.ui :as ui]
[frontend.util.page :as page-util]
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]
[frontend.format.mldoc :as mldoc]
[frontend.config :as config]))
@@ -15,8 +15,8 @@
[& pull-args]
(let [result* (apply db/pull pull-args)
result (cond-> result*
(seq (:block/properties result*))
(assoc :block.debug/properties (update-keys (:block/properties result*) pu/get-property-name)))
(and (seq (:block/properties result*)) (config/db-based-graph? (state/get-current-repo)))
(assoc :block.debug/properties (update-keys (:block/properties result*) db-pu/get-property-name)))
pull-data (with-out-str (pprint/pprint result))]
(println pull-data)
(notification/show!

View File

@@ -13,7 +13,7 @@
[malli.error :as me]
[frontend.format.block :as block]
[logseq.graph-parser.util.page-ref :as page-ref]
[frontend.handler.property.util :as pu]))
[frontend.handler.db-based.property.util :as db-pu]))
;; schema -> type, cardinality, object's class
;; min, max -> string length, number range, cardinality size limit
@@ -372,7 +372,7 @@
[repo block-ids key]
(when-let [property-uuid (if (uuid? key)
key
(pu/get-user-property-uuid repo key))]
(db-pu/get-user-property-uuid repo key))]
(let [txs (mapcat
(fn [id]
(when-let [block (db/entity [:block/uuid id])]
@@ -467,7 +467,7 @@
(map (fn [k]
(if (uuid? k)
k
(let [property-id (pu/get-user-property-uuid k)]
(let [property-id (db-pu/get-user-property-uuid k)]
(when-not property-id
(throw (ex-info "Property not exists yet"
{:key k})))

View File

@@ -0,0 +1,52 @@
(ns frontend.handler.db-based.property.util
"DB-graph only utility fns for properties"
(:require [frontend.db :as db]
[frontend.state :as state]
[logseq.db.frontend.property :as db-property]
[logseq.graph-parser.util :as gp-util]
[clojure.set :as set]))
(defn get-property-name
"Get a property's name given its uuid"
[uuid]
(:block/original-name (db/entity [:block/uuid uuid])))
(defn get-built-in-property-uuid
"Get a built-in property's uuid given its name"
([property-name] (get-built-in-property-uuid (state/get-current-repo) property-name))
([repo property-name]
(:block/uuid (db/entity repo [:block/name (name property-name)]))))
(defn get-user-property-uuid
"Get a user property's uuid given its unsanitized name"
([property-name] (get-user-property-uuid (state/get-current-repo) property-name))
([repo property-name]
(:block/uuid (db/entity repo [:block/name (gp-util/page-name-sanity-lc (name property-name))]))))
(defonce *hidden-built-in-properties (atom #{}))
(defn all-hidden-built-in-properties?
"Checks if the given properties are all hidden built-in properties"
[properties]
(let [repo (state/get-current-repo)]
(when (empty? @*hidden-built-in-properties)
(let [built-in-properties (set (map #(get-built-in-property-uuid repo (name %))
db-property/hidden-built-in-properties))]
(reset! *hidden-built-in-properties built-in-properties)))
(set/subset? (set properties) @*hidden-built-in-properties)))
(defn readable-properties
"Given a DB graph's properties, returns a readable properties map with keys as
property names and property values dereferenced where possible. A property's
value will only be a uuid if it's a page or a block"
[properties]
(->> properties
(map (fn [[k v]]
(let [prop-ent (db/entity [:block/uuid k])]
[(-> prop-ent :block/name keyword)
(if (seq (get-in prop-ent [:block/schema :values])) ; closed values
(when-let [block (db/entity [:block/uuid v])]
(or (:block/original-name block)
(get-in block [:block/schema :value])))
v)])))
(into {})))

View File

@@ -22,6 +22,7 @@
[frontend.handler.common :as common-handler]
[frontend.handler.property :as property-handler]
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]
[frontend.handler.export.html :as export-html]
[frontend.handler.export.text :as export-text]
[frontend.handler.notification :as notification]
@@ -3411,7 +3412,7 @@
(valid-custom-query-block? block)
(and (config/db-based-graph? repo)
(seq (:block/properties block))
(not (pu/all-hidden-built-in-properties? (keys (:block/properties block)))))
(not (db-pu/all-hidden-built-in-properties? (keys (:block/properties block)))))
(and (config/db-based-graph? repo)
(seq (:block/tags block)))
(and

View File

@@ -51,6 +51,7 @@
[frontend.handler.ui :as ui-handler]
[frontend.handler.user :as user-handler]
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]
[frontend.handler.property :as property-handler]
[frontend.handler.whiteboard :as whiteboard-handler]
[frontend.handler.web.nfs :as nfs-handler]
@@ -317,7 +318,7 @@
(contains? shown-properties property)
property-value)]
[:div.flex.flex-row.m-2.justify-between.align-items
[:div (if (uuid? property) (pu/get-property-name property) (name property))]
[:div (if (uuid? property) (db-pu/get-property-name property) (name property))]
[:div.mt-1 (ui/toggle shown?
(fn []
(let [value (not shown?)]

View File

@@ -13,6 +13,7 @@
[frontend.db.model :as model]
[frontend.modules.outliner.core :as outliner-core]
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]
[clojure.string :as string]))
(defn remove-block-property!
@@ -173,7 +174,7 @@
(or
(seq (:block/alias properties))
(and (seq properties)
(not= (keys properties) [(pu/get-built-in-property-uuid :icon)])))))
(not= (keys properties) [(db-pu/get-built-in-property-uuid :icon)])))))
(defn property-create-new-block
[block property value parse-block]

View File

@@ -1,13 +1,11 @@
(ns frontend.handler.property.util
"Utility fns for properties. Most of these are used in file or db graphs.
"Utility fns for properties that are for both file and db graphs.
Some fns like lookup and get-property were written to easily be backwards
compatible with file graphs"
(:require [frontend.config :as config]
[frontend.state :as state]
[logseq.db.frontend.property :as db-property]
[logseq.graph-parser.util :as gp-util]
[frontend.db :as db]
[clojure.set :as set]
[frontend.util :as util]))
(defn lookup
@@ -27,26 +25,13 @@
(when-let [properties (:block/properties block)]
(lookup properties key))))
(defn get-property-name
"Get a property's name given its uuid"
[uuid]
(:block/original-name (db/entity [:block/uuid uuid])))
(defn get-built-in-property-uuid
"Get a built-in property's uuid given its name"
([property-name] (get-built-in-property-uuid (state/get-current-repo) property-name))
([repo property-name]
(:block/uuid (db/entity repo [:block/name (name property-name)]))))
(defn get-user-property-uuid
(defn get-page-uuid
"Get a user property's uuid given its unsanitized name"
([property-name] (get-user-property-uuid (state/get-current-repo) property-name))
;; Get a page's uuid given its unsanitized name
([property-name] (get-page-uuid (state/get-current-repo) property-name))
([repo property-name]
(:block/uuid (db/entity repo [:block/name (gp-util/page-name-sanity-lc (name property-name))]))))
;; Get a page's uuid given its unsanitized name
(def get-page-uuid get-user-property-uuid)
(defn get-pid
"Get a property's id (name or uuid) given its name. For file and db graphs"
[property-name]
@@ -62,34 +47,4 @@
(get-property block :logseq.tldraw.page))
(defn shape-block? [block]
(= :whiteboard-shape (get-property block :ls-type)))
(defonce *hidden-built-in-properties (atom #{}))
(defn all-hidden-built-in-properties?
"Checks if the given properties are all hidden built-in properties"
[properties]
(let [repo (state/get-current-repo)]
(when (empty? @*hidden-built-in-properties)
(let [built-in-properties (set (map #(get-built-in-property-uuid repo (name %))
db-property/hidden-built-in-properties))]
(reset! *hidden-built-in-properties built-in-properties)))
(set/subset? (set properties) @*hidden-built-in-properties)))
(defn readable-properties
"Given a DB graph's properties, returns a readable properties map with keys as
property names and property values dereferenced where possible. A property's
value will only be a uuid if it's a page or a block"
[properties]
(->> properties
(map (fn [[k v]]
(let [prop-ent (db/entity [:block/uuid k])]
[(-> prop-ent
:block/name
keyword)
(if (seq (get-in prop-ent [:block/schema :values])) ; closed values
(when-let [block (db/entity [:block/uuid v])]
(or (:block/original-name block)
(get-in block [:block/schema :value])))
v)])))
(into {})))
(= :whiteboard-shape (get-property block :ls-type)))

View File

@@ -29,6 +29,7 @@
[frontend.handler.common.plugin :as plugin-common-handler]
[frontend.handler.property :as property-handler]
[frontend.handler.property.util :as pu]
[frontend.handler.db-based.property.util :as db-pu]
[frontend.modules.outliner.core :as outliner-core]
[frontend.modules.outliner.tree :as outliner-tree]
[frontend.handler.command-palette :as palette-handler]
@@ -748,7 +749,7 @@
(fn [block-uuid]
(when-let [block (db-model/query-block-by-uuid (sdk-utils/uuid-or-throw-error block-uuid))]
(let [properties (if (config/db-based-graph? (state/get-current-repo))
(pu/readable-properties (:block/properties block))
(db-pu/readable-properties (:block/properties block))
(:block/properties block))]
(bean/->js (sdk-utils/normalize-keyword-for-json properties))))))