Browse Source

fix: display class properties

Tienson Qin 1 year ago
parent
commit
cda9ac3e26

+ 7 - 6
deps/db/src/logseq/db.cljs

@@ -562,16 +562,17 @@
 
 (defn built-in?
   "Built-in page or block"
-  [db entity]
-  (get (:block/properties entity) (:block/uuid (d/entity db :logseq.property/built-in?))))
+  [entity]
+  (:logseq.property/built-in? entity))
 
 (defn built-in-class-property?
   "Whether property a built-in property for the specific class"
-  [db class-entity property-entity]
-  (and (built-in? db class-entity)
+  [class-entity property-entity]
+  (and (built-in? class-entity)
        (contains? (:block/type class-entity) "class")
-       (built-in? db property-entity)
-       (contains? (set (get-in class-entity [:block/schema :properties])) (:block/uuid property-entity))))
+       (built-in? property-entity)
+       (contains? (set (map :db/ident (:class/schema.properties class-entity)))
+                  (:db/ident property-entity))))
 
 (def write-transit-str sqlite-util/write-transit-str)
 (def read-transit-str sqlite-util/read-transit-str)

+ 4 - 7
deps/db/src/logseq/db/frontend/property.cljs

@@ -212,16 +212,11 @@
   [repo db block]
   (= :whiteboard-shape (get-block-property-value repo db block :ls-type)))
 
-(defn get-built-in
-  "Gets a built-in page/class/property/X by its :db/ident"
-  [db db-ident]
-  (d/entity db db-ident))
-
 (defn get-by-ident-or-name
   "Gets a property by ident or name"
   [db ident-or-name]
   (if (and (keyword? ident-or-name) (namespace ident-or-name))
-    (get-built-in db ident-or-name)
+    (d/entity db ident-or-name)
     (d/entity db [:block/name (common-util/page-name-sanity-lc (name ident-or-name))])))
 
 (defn get-closed-property-values
@@ -246,7 +241,9 @@
 
 (defn property?
   [k]
-  (contains? #{:logseq.property :user.property} (keyword (namespace k))))
+  (let [k-name (name k)]
+    (or (string/starts-with? k-name "logseq.property")
+        (string/starts-with? k-name "user.property"))))
 
 ;; TODO: db ident should obey clojure's rules for keywords
 (defn get-db-ident-from-name

+ 7 - 7
src/main/frontend/components/db_based/page.cljs

@@ -26,7 +26,7 @@
         configure-opts {:selected? false
                         :page-configure? true}
         has-viewable-properties? (db-property-handler/block-has-viewable-properties? page)
-        has-class-properties? (seq (:properties (:block/schema page)))
+        has-class-properties? (seq (:class/schema.properties page))
         has-tags? (seq (:block/tags page))
         hide-properties? (get-in page [:block/properties (:block/uuid (db/entity :logseq.property/hide-properties?))])]
     (when (or configure?
@@ -217,15 +217,15 @@
             (when (or @*hover? (not collapsed?))
               [:div.px-1.absolute.right-0.top-0
                (shui/button
-                 {:variant :ghost :size :sm}
-                 (if collapsed?
-                   [:span.opacity-80.flex.items-center
-                    (ui/icon "adjustments-horizontal" {:size 16})]
-                   (ui/icon "x")))])])]
+                {:variant :ghost :size :sm}
+                (if collapsed?
+                  [:span.opacity-80.flex.items-center
+                   (ui/icon "adjustments-horizontal" {:size 16})]
+                  (ui/icon "x")))])])]
         (when show-info?
           (if collapsed?
             (when (or (seq (:block/properties page))
-                      (and class? (seq (:properties (:block/schema page)))))
+                      (and class? (seq (:class/schema.properties page))))
               [:div.px-4
                (page-properties page {:mode (if class? :class :page)})])
             [:div.pt-2.px-4

+ 1 - 1
src/main/frontend/components/page.cljs

@@ -363,7 +363,7 @@
                                (when (and (not hls-page?)
                                           (not fmt-journal?)
                                           (not config/publishing?)
-                                          (not (ldb/built-in? (db/get-db) page)))
+                                          (not (ldb/built-in? page)))
                                  (reset! *input-value (if untitled? "" old-name))
                                  (reset! *edit? true))))))}
 

+ 8 - 14
src/main/frontend/components/page_menu.cljs

@@ -19,8 +19,7 @@
             [frontend.handler.user :as user-handler]
             [frontend.handler.file-sync :as file-sync-handler]
             [logseq.common.path :as path]
-            [frontend.handler.property.util :as pu]
-            [logseq.db.frontend.property :as db-property]))
+            [frontend.handler.property.util :as pu]))
 
 (defn- delete-page!
   [page-name]
@@ -48,9 +47,9 @@
 (defn ^:large-vars/cleanup-todo page-menu
   [page-name]
   (when-let [page-name (or
-                         page-name
-                         (state/get-current-page)
-                         (state/get-current-whiteboard))]
+                        page-name
+                        (state/get-current-page)
+                        (state/get-current-whiteboard))]
     (let [page-name (util/page-name-sanity-lc page-name)
           repo (state/sub :git/current-repo)
           page (db/entity repo [:block/name page-name])
@@ -58,8 +57,7 @@
           whiteboard? (contains? (set (:block/type page)) "whiteboard")
           block? (and page (util/uuid-string? page-name) (not whiteboard?))
           contents? (= page-name "contents")
-          properties (:block/properties page)
-          public? (true? (pu/lookup properties :public))
+          public? (true? (pu/lookup (:block/properties page) :public))
           _favorites-updated? (state/sub :favorites/updated?)
           favorited? (page-handler/favorited? page-name)
           developer-mode? (state/sub [:ui/developer-mode?])
@@ -70,8 +68,6 @@
                                     ;; FIXME: Sync state is not cleared when switching to a new graph
                                     (file-sync-handler/current-graph-sync-on?)
                                     (file-sync-handler/get-current-graph-uuid))
-          built-in-property? (and (contains? (:block/type page) "property")
-                                  (contains? db-property/built-in-properties-keys-str page-name))
           db-based? (config/db-based-graph? repo)]
       (when (and page (not block?))
         (->>
@@ -106,14 +102,12 @@
              :options {:on-click #(page-handler/copy-page-url page-original-name)}})
 
           (when-not (or contents?
-                        config/publishing?
-                        (and db-based?
-                             built-in-property?))
+                        config/publishing?)
             {:title   (t :page/delete)
              :options {:on-click #(delete-page-confirm! page-name)}})
 
           (when (and (not (mobile-util/native-platform?))
-                  (state/get-current-page))
+                     (state/get-current-page))
             {:title (t :page/slide-view)
              :options {:on-click (fn []
                                    (state/sidebar-add-block!
@@ -138,7 +132,7 @@
              :options {:on-click #(shui/dialog-open!
                                    (fn []
                                      (export/export-blocks (:block/name page) {:whiteboard? whiteboard?}))
-                                    {:class "w-auto md:max-w-4xl max-h-[80vh] overflow-y-auto"})}})
+                                   {:class "w-auto md:max-w-4xl max-h-[80vh] overflow-y-auto"})}})
 
           (when (util/electron?)
             {:title   (t (if public? :page/make-private :page/make-public))

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

@@ -222,7 +222,7 @@
             *property-schema (::property-schema state)
             built-in-property? (contains? db-property/built-in-properties-keys-str (:block/original-name property))
             property (db/sub-block (:db/id property))
-            built-in? (ldb/built-in? (db/get-db) property)
+            built-in? (ldb/built-in? property)
             disabled? (or built-in? config/publishing?)
             property-type (get-in property [:block/schema :type])
             save-property-fn (fn [] (components-pu/update-property! property @*property-name @*property-schema))
@@ -264,7 +264,7 @@
 
           [:div.grid.grid-cols-4.gap-1.items-center.leading-8
            [:label.col-span-1 "Schema type:"]
-           (if (or (ldb/built-in? (db/get-db) property)
+           (if (or (ldb/built-in? property)
                    (and property-type (seq values)))
              [:div.flex.items-center.col-span-2
               (property-type-label property-type)
@@ -439,7 +439,7 @@
                                                :as opts}]
   (let [*show-new-property-config? (::show-new-property-config? state)
         entity-properties (->> (keys (:block/properties entity))
-                               (map #(:block/original-name (db/entity [:block/uuid %])))
+                               (map #(:block/original-name (db/entity %)))
                                (remove nil?)
                                (set))
         existing-tag-alias (reduce (fn [acc prop]
@@ -549,7 +549,7 @@
                                                             (components-pu/update-property! property property-name (assoc schema :hide? true))
                                                             (shui/popup-hide!)))}
                                              "Hide property")
-                                            (when-not (ldb/built-in-class-property? (db/get-db) block property)
+                                            (when-not (ldb/built-in-class-property? block property)
                                               (shui/dropdown-menu-item
                                                {:on-click (fn []
                                                             (handle-delete-property! block property {:class-schema? class-schema?})
@@ -701,8 +701,8 @@
         block (resolve-linked-block-if-exists target-block)
         block-properties (:block/properties block)
         properties (if (and class-schema? page-configure?)
-                     (let [properties (:properties (:block/schema block))]
-                       (map (fn [k] [k nil]) properties))
+                     (let [properties (:class/schema.properties block)]
+                       (map (fn [p] [(:db/ident p) nil]) properties))
                      (sort-by first block-properties))
         alias (set (map :db/id (:block/alias block)))
         alias-properties (when (seq alias)
@@ -751,7 +751,7 @@
                                  properties #{}
                                  result []]
                             (if-let [class (first classes)]
-                              (let [cur-properties (->> (:properties (:block/schema class))
+                              (let [cur-properties (->> (:class/schema.properties class)
                                                         (remove properties)
                                                         (remove hide-with-property-id))]
                                 (recur (rest classes)

+ 3 - 3
src/main/frontend/components/property/value.cljs

@@ -523,11 +523,11 @@
                 (:block/page v-block)
                 (property-normal-block-value v-block block-cp editor-box)
 
-                (and class? (seq (:properties (:block/schema v-block))))
+                (and class? (seq (:class/schema.properties v-block)))
                 (if template-instance
                   (property-template-value {:editor-id editor-id}
-                                          (:block/uuid template-instance)
-                                          opts)
+                                           (:block/uuid template-instance)
+                                           opts)
                   (create-template-block! block property v-block *template-instance))
 
               ;; page/class/etc.

+ 1 - 1
src/main/frontend/extensions/pdf/assets.cljs

@@ -179,7 +179,7 @@
 
         ;; try to update file path
         (do
-          (property-handler/add-page-property! page-name :file-path url)
+          (property-handler/add-page-property! page-name :logseq.property/file-path url)
           page)))))
 
 (defn ensure-ref-block!

+ 6 - 37
src/main/frontend/format/block.cljs

@@ -10,52 +10,21 @@
             [frontend.state :as state]
             [logseq.graph-parser.block :as gp-block]
             [logseq.graph-parser.property :as gp-property]
-            [frontend.handler.db-based.property.util :as db-pu]
             [lambdaisland.glogi :as log]
-            [datascript.core :as d]
-            [logseq.db.frontend.property :as db-property]
             [frontend.format.mldoc :as mldoc]))
 
-(defn- update-extracted-block-properties
-  "Updates DB graph blocks to ensure that built-in properties are using uuids
-  for property ids"
-  [blocks]
-  (let [repo (state/get-current-repo)
-        update-properties (fn [props]
-                            (update-keys props #(if (contains? db-property/built-in-properties-keys %)
-                                                  (db-pu/get-built-in-property-uuid repo %)
-                                                  %)))]
-    (if (config/db-based-graph? repo)
-     (->> blocks
-          (map (fn [b]
-                 (if (:block/properties b)
-                   (-> b
-                       (dissoc :block/properties-order)
-                       (update :block/properties update-properties))
-                   b)))
-          (map (fn [b]
-                 (if (:block/macros b)
-                   (update b :block/macros
-                           (fn [macros]
-                             (map #(-> %
-                                       (assoc :block/uuid (d/squuid))
-                                       (update :block/properties update-properties)) macros)))
-                   b))))
-     blocks)))
-
 (defn extract-blocks
   "Wrapper around logseq.graph-parser.block/extract-blocks that adds in system state
 and handles unexpected failure."
   [blocks content format {:keys [with-id? page-name]
                           :or {with-id? true}}]
   (try
-    (update-extracted-block-properties
-     (gp-block/extract-blocks blocks content with-id? format
-                              {:user-config (state/get-config)
-                               :block-pattern (config/get-block-pattern format)
-                               :db (db/get-db (state/get-current-repo))
-                               :date-formatter (state/get-date-formatter)
-                               :page-name page-name}))
+    (gp-block/extract-blocks blocks content with-id? format
+                             {:user-config (state/get-config)
+                              :block-pattern (config/get-block-pattern format)
+                              :db (db/get-db (state/get-current-repo))
+                              :date-formatter (state/get-date-formatter)
+                              :page-name page-name})
     (catch :default e
       (log/error :exception e)
       (state/pub-event! [:capture-error {:error e

+ 5 - 5
src/main/frontend/handler/db_based/property.cljs

@@ -252,7 +252,7 @@
             property-values (db-async/<get-block-property-values repo property-id)]
       (when (or (not type-changed?)
                 ;; only change type if property hasn't been used yet
-                (and (not (ldb/built-in? (db/get-db) property)) (empty? property-values)))
+                (and (not (ldb/built-in? property)) (empty? property-values)))
         (let [tx-data (cond-> (merge {:db/ident property-id} properties)
                         property-name (merge
                                        {:block/original-name property-name})
@@ -284,7 +284,7 @@
   (when-let [class (db/entity repo [:block/uuid class-uuid])]
     (when (contains? (:block/type class) "class")
       (when-let [property (db/entity repo property-id)]
-        (when-not (ldb/built-in-class-property? (db/get-db) class property)
+        (when-not (ldb/built-in-class-property? class property)
           (db/transact! repo [[:db/retract (:db/id class) :class/schema.properties property-id]]
             {:outliner-op :save-block}))))))
 
@@ -438,9 +438,9 @@
         namespace-parents (get-namespace-parents classes)
         all-classes (->> (concat classes namespace-parents)
                          (filter (fn [class]
-                                   (seq (:properties (:block/schema class))))))
+                                   (seq (:class/schema.properties class)))))
         all-properties (-> (mapcat (fn [class]
-                                     (seq (:properties (:block/schema class)))) all-classes)
+                                     (map :db/ident (:class/schema.properties class))) all-classes)
                            distinct)]
     {:classes classes
      :all-classes all-classes           ; block own classes + parent classes
@@ -700,7 +700,7 @@
   "Returns true when deleted or if not deleted displays warning and returns false"
   [db property value-block]
   (cond
-    (ldb/built-in? db value-block)
+    (ldb/built-in? value-block)
     (do (notification/show! "The choice can't be deleted because it's built-in." :warning)
         false)
 

+ 5 - 5
src/main/frontend/handler/db_based/property/util.cljs

@@ -7,9 +7,9 @@
             [logseq.db.frontend.property :as db-property]))
 
 (defn get-property-name
-  "Get a property's name given its uuid"
-  [uuid]
-  (:block/original-name (db-utils/entity [:block/uuid uuid])))
+  "Get a property's name given its id"
+  [id]
+  (:block/original-name (db-utils/entity id)))
 
 (defn get-built-in-property-uuid
   "Get a built-in property's uuid given its name"
@@ -27,7 +27,7 @@
   "Checks if the given properties are all hidden properties"
   [properties]
   (every? (fn [id]
-            (:hide? (:block/schema (db/entity [:block/uuid id])))) properties))
+            (:hide? (:block/schema (db/entity id)))) properties))
 
 (defn readable-properties
   "Given a DB graph's properties, returns a readable properties map with keys as
@@ -36,7 +36,7 @@
   [properties]
   (->> properties
        (map (fn [[k v]]
-              (let [prop-ent (db-utils/entity [:block/uuid k])
+              (let [prop-ent (db-utils/entity k)
                     readable-property-val
                     #(if (seq (get-in prop-ent [:block/schema :values])) ; closed values
                        (when-let [block (db-utils/entity [:block/uuid %])]

+ 3 - 4
src/main/frontend/handler/editor.cljs

@@ -893,8 +893,7 @@
 (defn set-block-query-properties!
   [block-id all-properties key add?]
   (when-let [block (db/entity [:block/uuid block-id])]
-    (let [properties (:block/properties block)
-          query-properties (pu/lookup properties :query-properties)
+    (let [query-properties (:query-properties (:block/properties block))
           repo (state/get-current-repo)
           db-based? (config/db-based-graph? repo)
           query-properties (if db-based?
@@ -910,7 +909,7 @@
           query-properties (vec query-properties)]
       (if (seq query-properties)
         (property-handler/set-block-property! repo block-id
-                                              :query-properties
+                                              :logseq.property/query-properties
                                               (if db-based?
                                                 query-properties
                                                 (str query-properties)))
@@ -3456,7 +3455,7 @@
                     (not (db-pu/all-hidden-properties? (keys (:block/properties block)))))
                (and db-based? (seq tags)
                     (some (fn [t]
-                            (let [properties (:properties (:block/schema t))]
+                            (let [properties (map :db/ident (:class/schema.properties (:block/schema t)))]
                               (and (seq properties)
                                    (not (db-pu/all-hidden-properties? properties))))) tags))
                (and

+ 1 - 1
src/main/frontend/handler/events.cljs

@@ -311,7 +311,7 @@
                       (contains? shown-properties property)
                       property-value)]
          [:div.flex.flex-row.m-2.justify-between.align-items
-          [:div (if (uuid? property) (db-pu/get-property-name property) (name property))]
+          [:div (if (keyword? property) (db-pu/get-property-name property) (name property))]
           [:div.mt-1 (ui/toggle shown?
                        (fn []
                          (let [value (not shown?)]

+ 2 - 2
src/main/frontend/handler/page.cljs

@@ -170,7 +170,7 @@
 
 (defn update-public-attribute!
   [page-name value]
-  (property-handler/add-page-property! page-name :public value))
+  (property-handler/add-page-property! page-name :logseq.property/public value))
 
 (defn get-page-ref-text
   [page]
@@ -249,7 +249,7 @@
 
 (defn save-filter!
   [page-name filter-state]
-  (property-handler/add-page-property! page-name :filters filter-state))
+  (property-handler/add-page-property! page-name :logseq.property/filters filter-state))
 
 ;; Editor
 (defn page-not-exists-handler

+ 1 - 1
src/main/frontend/handler/property.cljs

@@ -29,7 +29,7 @@
   [page-name key value]
   (let [repo (state/get-current-repo)]
     (if (config/db-based-graph? repo)
-      (when-let [page (db/pull [:block/name (util/page-name-sanity-lc page-name)])]
+      (when-let [page (db/entity [:block/name (util/page-name-sanity-lc page-name)])]
        (set-block-property! repo (:block/uuid page) key value))
       (file-page-property/add-property! page-name key value))))
 

+ 1 - 1
src/main/frontend/worker/handler/page.cljs

@@ -211,7 +211,7 @@
                                      [:db.fn/retractEntity [:block/uuid (:block/uuid block)]])
                                    blocks)
           db-based? (sqlite-util/db-based-graph? repo)]
-      (if (ldb/built-in? @conn page)
+      (if (ldb/built-in? page)
         (do
           (error-handler {:msg "Built-in page cannot be deleted"})
           false)

+ 1 - 1
src/main/frontend/worker/handler/page/rename.cljs

@@ -254,7 +254,7 @@
         new-page-e (d/entity db [:block/name new-page-name])
         name-changed? (not= old-name new-name)]
     (cond
-      (ldb/built-in? @conn page-e)
+      (ldb/built-in? page-e)
       :built-in-page
 
       (string/blank? new-name)

+ 4 - 4
src/test/frontend/handler/db_based/property_test.cljs

@@ -196,14 +196,14 @@
       (db-property-handler/class-add-property! repo c1id "property-2")
       ;; repeated adding property-2
       (db-property-handler/class-add-property! repo c1id "property-2")
-      (is (= 2 (count (:properties (:block/schema (db/entity (:db/id c1))))))))
+      (is (= 2 (count (:class/schema.properties (db/entity (:db/id c1)))))))
 
     (testing "Class remove property"
       (db-property-handler/class-remove-property! repo c1id (:block/uuid (db/entity [:block/name "property-1"])))
-      (is (= 1 (count (:properties (:block/schema (db/entity (:db/id c1))))))))
+      (is (= 1 (count (:class/schema.properties (db/entity (:db/id c1)))))))
     (testing "Add classes to a block"
-        (test-helper/save-block! repo fbid "Block 1" {:tags ["class1" "class2" "class3"]})
-        (is (= 3 (count (:block/tags (db/entity [:block/uuid fbid]))))))
+      (test-helper/save-block! repo fbid "Block 1" {:tags ["class1" "class2" "class3"]})
+      (is (= 3 (count (:block/tags (db/entity [:block/uuid fbid]))))))
     ;; FIXME: @tiensonqin https://github.com/logseq/logseq/commit/575624c650b2b7e919033a79aa5d14b97507d86f
     #_(testing "Remove a class from a block"
       ;; make sure class2 will not be deleted when removing it from the first block