Browse Source

enhance: built-in properties can be page only

This allows properties like alias and public to not
show up nonsensically in block contexts. In the case
of alias it fixed a bug where it couldn't be removed from a block
context. Also fixed :public property losing its :public?

Part of LOG-2874
Gabriel Horner 1 year ago
parent
commit
3e5e7f9f7e

+ 2 - 1
deps/db/src/logseq/db/frontend/malli_schema.cljs

@@ -159,7 +159,8 @@
         [:map
          [:type (apply vector :enum (into db-property-type/internal-built-in-property-types
                                           db-property-type/user-built-in-property-types))]
-        [:public? {:optional true} :boolean]]
+         [:public? {:optional true} :boolean]
+         [:view-context {:optional true} [:enum :page]]]
         property-common-schema-attrs
         property-type-schema-attrs))]]
     page-attrs

+ 14 - 2
deps/db/src/logseq/db/frontend/property.cljs

@@ -16,6 +16,8 @@
      * :cardinality - property cardinality. Default to one/single cardinality if not set
      * :hide? - Boolean which hides property when set on a block
      * :public? - Boolean which allows property to be used by user e.g. add and remove property to blocks/pages
+     * :view-context - Keyword which indicates which view contexts a property can be in. Valid values are :page.
+       Property can be viewed in any context if not set
    * :original-name - Property's :block/original-name
    * :name - Property's :block/name as a keyword. If none given, one is derived from the db/ident
    * :attribute - Property keyword that is saved to a datascript attribute outside of :block/properties
@@ -26,6 +28,7 @@
                            :attribute :block/alias
                            :schema {:type :page
                                     :cardinality :many
+                                    :view-context :page
                                     :public? true}}
    :logseq.property/tags {:original-name "Tags"
                           :attribute :block/tags
@@ -36,6 +39,7 @@
    :logseq.property/pagetags {:original-name "pageTags"
                               :schema {:type :page
                                        :public? true
+                                       :view-context :page
                                        :cardinality :many}}
    :logseq.property/background-color {:schema {:type :default :hide? true}}
    :logseq.property/background-image {:schema {:type :default :hide? true :public? true}}
@@ -155,9 +159,17 @@
 
    :logseq.property/icon {:original-name "Icon"
                           :schema {:type :map}}
-   :logseq.property/public {:schema {:type :checkbox :hide? true}}
+   :logseq.property/public {:schema
+                            {:type :checkbox
+                             :hide? true
+                             :view-context :page
+                             :public? true}}
    :logseq.property/filters {:schema {:type :map}}
-   :logseq.property/exclude-from-graph-view {:schema {:type :checkbox :hide? true :public? true}}})
+   :logseq.property/exclude-from-graph-view {:schema
+                                             {:type :checkbox
+                                              :hide? true
+                                              :view-context :page
+                                              :public? true}}})
 
 (def built-in-properties
   (->> built-in-properties*

+ 15 - 7
src/main/frontend/components/property.cljs

@@ -17,7 +17,6 @@
             [frontend.handler.property.util :as pu]
             [frontend.handler.db-based.property.util :as db-pu]
             [frontend.modules.shortcut.core :as shortcut]
-            [frontend.search :as search]
             [frontend.state :as state]
             [frontend.ui :as ui]
             [frontend.util :as util]
@@ -416,11 +415,17 @@
 
 (rum/defc property-select
   [exclude-properties on-chosen input-opts]
-  (let [[properties set-properties!] (rum/use-state nil)]
+  (let [[properties set-properties!] (rum/use-state nil)
+        [excluded-properties set-excluded-properties!] (rum/use-state nil)]
     (rum/use-effect!
      (fn []
-       (p/let [properties (search/get-all-properties)]
-         (set-properties! (remove exclude-properties properties))))
+       (p/let [properties (db-async/<db-based-get-all-properties (state/get-current-repo))]
+         (set-properties! (map :block/original-name (remove exclude-properties properties)))
+         (set-excluded-properties! (->> properties
+                                        (filter exclude-properties)
+                                        ;; lower case b/c of case insensitive name lookups
+                                        (map (comp string/lower-case :block/original-name))
+                                        set))))
      [])
     [:div.ls-property-add.flex.flex-row.items-center
     [:span.bullet-container.cursor [:span.bullet]]
@@ -430,7 +435,7 @@
                      :dropdown? true
                      :close-modal? false
                      :show-new-when-not-exact-match? true
-                     :exact-match-exclude-items exclude-properties
+                     :exact-match-exclude-items (fn [s] (contains? excluded-properties (string/lower-case s)))
                      :input-default-placeholder "Add property"
                      :on-chosen on-chosen
                      :input-opts input-opts})]]))
@@ -453,8 +458,11 @@
                                 (map db-property/built-in-properties)
                                 (keep #(when (get entity (:attribute %)) (:original-name %)))
                                 set)
-        exclude-properties* (set/union entity-properties existing-tag-alias)
-        exclude-properties (set/union exclude-properties* (set (map string/lower-case exclude-properties*)))]
+        exclude-property-names (set/union entity-properties existing-tag-alias)
+        exclude-properties (fn [m]
+                             (or (contains? exclude-property-names (:block/original-name m))
+                                 ;; Filters out built-in properties from being in wrong :view-context
+                                 (and in-block-container? (= :page (get-in m [:block/schema :view-context])))))]
     [:div.ls-property-input.flex.flex-1.flex-row.items-center.flex-wrap.gap-1
      (if in-block-container? {:style {:padding-left 22}} {})
      (if @*property-key

+ 5 - 5
src/main/frontend/db/async.cljs

@@ -49,7 +49,7 @@
     (p/let [templates (<get-all-templates repo)]
       (get templates name))))
 
-(defn- <db-based-get-all-properties
+(defn <db-based-get-all-properties
   "Return seq of all property names except for private built-in properties."
   [graph]
   (p/let [result (<q graph
@@ -61,15 +61,15 @@
          ;; remove private built-in properties
          (remove #(and (:db/ident %)
                        (string/starts-with? (namespace (:db/ident %)) "logseq.")
-                       (not (get-in % [:block/schema :public?]))))
-         (map :block/original-name))))
+                       (not (get-in % [:block/schema :public?])))))))
 
-(defn <get-all-properties
+(defn <get-all-property-names
   "Returns a seq of property name strings"
   []
   (when-let [graph (state/get-current-repo)]
     (if (config/db-based-graph? graph)
-      (<db-based-get-all-properties graph)
+      (p/let [properties (<db-based-get-all-properties graph)]
+        (map :block/original-name properties))
       (file-async/<file-based-get-all-properties graph))))
 
 (comment

+ 1 - 1
src/main/frontend/search.cljs

@@ -75,7 +75,7 @@
                          ;; no-op since already removed
                          (constantly false)
                          (set (map name (property-util/hidden-properties))))]
-      (p/let [properties (db-async/<get-all-properties)]
+      (p/let [properties (db-async/<get-all-property-names)]
         (remove hidden-props properties)))))
 
 (defn property-search