Jelajahi Sumber

fix: add back support for alias and tags

Tienson Qin 2 tahun lalu
induk
melakukan
4f24c80b33

+ 5 - 1
deps/graph-parser/src/logseq/graph_parser/property.cljs

@@ -82,7 +82,11 @@
 
 ;; FIXME: no support for built-in-extended-properties
 (def db-built-in-properties
-  {:background-color {:schema {:type :default}}
+  {:alias {:schema {:type :page
+                    :cardinality :many}}
+   :tags {:schema {:type :page
+                   :cardinality :many}}
+   :background-color {:schema {:type :default}}
    :heading {:schema {:type :any}}      ; number (1-6) or boolean for auto heading
    :query-table {:schema {:type :checkbox}}
    :query-properties {:schema {:type :coll}}

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

@@ -380,7 +380,7 @@
                         f (if (and class? class-schema?)
                             property-handler/class-remove-property!
                             property-handler/remove-block-property!)]
-                    (f repo block (:block/uuid property))))}
+                    (f repo (:block/uuid block) (:block/uuid property))))}
      (t :context-menu/delete-property)
      nil)]))
 

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

@@ -460,7 +460,9 @@
                   (config/db-based-graph? repo)
                   (not block?)
                   (not whiteboard?)
-                  (seq (:block/properties page)))
+                  (or (seq (:block/properties page))
+                      (seq (:block/alias page))
+                      (seq (:block/tags page))))
              (let [edit-input-id (str "edit-block-" (:block/uuid page) "-schema")]
                (component-block/db-properties-cp
                 {:editor-box editor/box}

+ 13 - 4
src/main/frontend/components/property.cljs

@@ -111,7 +111,7 @@
          class? (= (:block/type block) "class")]
      (when property-key
        (if (and class? class-schema?)
-         (property-handler/class-add-property! repo block property-key)
+         (property-handler/class-add-property! repo (:block/uuid block) property-key)
          (property-handler/set-block-property! repo (:block/uuid block) property-key property-value)))
      (when exit-edit?
        (exit-edit-property)))))
@@ -326,7 +326,9 @@
                                      :editor-box editor-box})]
                  (if multiple-values?
                    (property-handler/delete-property-value! repo block (:block/uuid property) value)
-                   (property-handler/remove-block-property! repo (:block/uuid block) (:block/uuid property))))
+                   (property-handler/remove-block-property! repo
+                                                            (:block/uuid block)
+                                                            (:block/uuid property))))
 
                (inline-text {} :markdown (str value)))))])))))
 
@@ -469,7 +471,7 @@
 (rum/defcs property-value < rum/reactive
   [state block property value opts]
   (let [k (:block/uuid property)
-        v (get (:block/properties block) k)
+        v (or (get (:block/properties block) k) value)
         dom-id (str "ls-property-" (:blocks-container-id opts) "-" k)
         editor-id (str "ls-property-" (:blocks-container-id opts) "-" (:db/id block) "-" (:db/id property))
         schema (:block/schema property)
@@ -541,6 +543,13 @@
                      (let [properties (:properties (:block/schema block))]
                        (map (fn [k] [k nil]) properties))
                      (:block/properties block))
+        alias (set (map :block/uuid (:block/alias block)))
+        tags (set (map :block/uuid (:block/tags block)))
+        properties (cond-> properties
+                     (seq alias)
+                     (assoc (:block/uuid (db/entity [:block/name "alias"])) alias)
+                     (seq tags)
+                     (assoc (:block/uuid (db/entity [:block/name "tags"])) tags))
         new-property? (= edit-input-id (state/sub :ui/new-property-input-id))
         class-properties (->> (:block/tags block)
                               (mapcat (fn [tag]
@@ -572,5 +581,5 @@
                   [:div.property-description.col-span-3.font-light
                    (get-in property [:block/schema :description])]
                   [:div.property-value.col-span-3
-                   (property-value block property v (assoc opts :parsed-value v))])]))))
+                   (property-value block property v opts)])]))))
        (new-property block edit-input-id properties new-property? opts)])))

+ 2 - 0
src/main/frontend/db/model.cljs

@@ -429,6 +429,8 @@ independent of format as format specific heading characters are stripped"
                           :priority (:block/priority e)
                           :properties (:block/properties e)
                           :properties-values (:block/properties-text-values e)
+                          :alias (:block/alias e)
+                          :tags (:block/tags e)
                           :children children
                           :collapsed? (:block/collapsed? e)}]))}
        nil)

+ 96 - 58
src/main/frontend/handler/db_based/property.cljs

@@ -125,7 +125,7 @@
                            {:block/schema schema
                             :block/uuid property-uuid
                             :block/type "property"})]
-        {:outliner-op :update-property}))
+        {:outliner-op :save-block}))
     (when (nil? property) ;if property not exists yet
       (db/transact! repo [(outliner-core/block-with-timestamps
                            {:block/schema schema
@@ -133,7 +133,7 @@
                             :block/name (util/page-name-sanity-lc k-name)
                             :block/uuid property-uuid
                             :block/type "property"})]
-        {:outliner-op :create-new-property}))))
+        {:outliner-op :insert-blocks}))))
 
 (defn set-block-property!
   [repo block-id k-name v {:keys [old-value]}]
@@ -154,42 +154,53 @@
                  (convert-property-input-string property-type v)
                  (catch :default e
                    (notification/show! (str e) :error false)
-                   nil))]
-        (when-not (contains? (if (set? value) value #{value}) v*)
-          (if-let [msg (me/humanize (mu/explain-data schema v*))]
-            (let [msg' (str "\"" k-name "\"" " " (if (coll? msg) (first msg) msg))]
-              (notification/show! msg' :warning))
-            (do
-              (upsert-property! repo k-name {:type property-type}
-                                {:property-uuid property-uuid})
-              (let [new-value (cond
-                                (and multiple-values? old-value
-                                     (not= old-value :frontend.components.property/new-value-placeholder))
-                                (if (coll? v*)
-                                  (vec (distinct (concat value v*)))
-                                  (let [v (mapv (fn [x] (if (= x old-value) v* x)) value)]
-                                    (if (contains? (set v) v*)
-                                      v
-                                      (conj v v*))))
-
-                                multiple-values?
-                                (let [f (if (coll? v*) concat conj)]
-                                  (f value v*))
-
-                                :else
-                                v*)
-                    new-value (if (coll? new-value)
-                                (set (remove string/blank? new-value))
-                                new-value)
-                    block-properties (assoc properties property-uuid new-value)
-                    refs (outliner-core/rebuild-block-refs block block-properties)]
-                ;; TODO: fix block/properties-order
-                (db/transact! repo
-                  [[:db/retract (:db/id block) :block/refs]
-                   {:block/uuid (:block/uuid block)
-                    :block/properties block-properties
-                    :block/refs refs}]
-                  {:outliner-op :add-property})))))))))
+                   nil))
+            tags-or-alias? (and (contains? #{"tags" "alias"} k-name) (uuid? v*))]
+        (if tags-or-alias?
+          (let [property-value-id (:db/id (db/entity [:block/uuid v*]))
+                attribute (case k-name
+                            "alias"
+                            :block/alias
+                            "tags"
+                            :block/tags)]
+            (db/transact! repo
+              [[:db/add (:db/id block) attribute property-value-id]]
+              {:outliner-op :save-block}))
+          (when-not (contains? (if (set? value) value #{value}) v*)
+            (if-let [msg (me/humanize (mu/explain-data schema v*))]
+              (let [msg' (str "\"" k-name "\"" " " (if (coll? msg) (first msg) msg))]
+                (notification/show! msg' :warning))
+              (do
+                (upsert-property! repo k-name {:type property-type}
+                                  {:property-uuid property-uuid})
+                (let [new-value (cond
+                                  (and multiple-values? old-value
+                                       (not= old-value :frontend.components.property/new-value-placeholder))
+                                  (if (coll? v*)
+                                    (vec (distinct (concat value v*)))
+                                    (let [v (mapv (fn [x] (if (= x old-value) v* x)) value)]
+                                      (if (contains? (set v) v*)
+                                        v
+                                        (conj v v*))))
+
+                                  multiple-values?
+                                  (let [f (if (coll? v*) concat conj)]
+                                    (f value v*))
+
+                                  :else
+                                  v*)
+                      new-value (if (coll? new-value)
+                                  (set (remove string/blank? new-value))
+                                  new-value)
+                      block-properties (assoc properties property-uuid new-value)
+                      refs (outliner-core/rebuild-block-refs block block-properties)]
+                  ;; TODO: fix block/properties-order
+                  (db/transact! repo
+                    [[:db/retract (:db/id block) :block/refs]
+                     {:block/uuid (:block/uuid block)
+                      :block/properties block-properties
+                      :block/refs refs}]
+                    {:outliner-op :save-block}))))))))))
 
 (defn- fix-cardinality-many-values!
   [repo property-uuid]
@@ -202,7 +213,7 @@
                           :block/properties (assoc properties property-uuid #{v})})) ev)]
     (when (seq tx-data)
       (db/transact! repo tx-data
-        {:outliner-op :property-fix-cardinality}))))
+        {:outliner-op :save-block}))))
 
 (defn update-property!
   [repo property-uuid {:keys [property-name property-schema]}]
@@ -219,7 +230,7 @@
                     property-schema (assoc :block/schema property-schema)
                     true outliner-core/block-with-updated-at)]
       (db/transact! repo [tx-data]
-        {:outliner-op :update-property}))))
+        {:outliner-op :save-block}))))
 
 (defn delete-property-value!
   "Delete value if a property has multiple values"
@@ -227,19 +238,33 @@
   (when (and block (uuid? property-id))
     (when (not= property-id (:block/uuid block))
       (when-let [property (db/pull [:block/uuid property-id])]
-        (let [schema (:block/schema property)]
-          (when (= :many (:cardinality schema))
-            (let [properties (:block/properties block)
-                  properties' (update properties property-id
-                                      (fn [col]
-                                        (set (remove #{property-value} col))))
-                  refs (outliner-core/rebuild-block-refs block properties')]
-              (db/transact! repo
-                [[:db/retract (:db/id block) :block/refs]
-                 {:block/uuid (:block/uuid block)
-                  :block/properties properties'
-                  :block/refs refs}]
-                {:outliner-op :delete-property-value})))
+        (let [schema (:block/schema property)
+              k-name (:block/name property)
+              tags-or-alias? (and (contains? #{"tags" "alias"} k-name)
+                                  (uuid? property-value))]
+          (if tags-or-alias?
+            (let [property-value-id (:db/id (db/entity [:block/uuid property-value]))
+                  attribute (case k-name
+                              "alias"
+                              :block/alias
+                              "tags"
+                              :block/tags)]
+              (when property-value-id
+                (db/transact! repo
+                  [[:db/retract (:db/id block) attribute property-value-id]]
+                  {:outliner-op :save-block})))
+            (when (= :many (:cardinality schema))
+              (let [properties (:block/properties block)
+                    properties' (update properties property-id
+                                        (fn [col]
+                                          (set (remove #{property-value} col))))
+                    refs (outliner-core/rebuild-block-refs block properties')]
+                (db/transact! repo
+                  [[:db/retract (:db/id block) :block/refs]
+                   {:block/uuid (:block/uuid block)
+                    :block/properties properties'
+                    :block/refs refs}]
+                  {:outliner-op :save-block}))))
           (state/clear-edit!))))))
 
 (defn class-add-property!
@@ -257,7 +282,7 @@
       (db/transact! repo
         [{:db/id (:db/id class)
           :block/schema class-new-schema}]
-        {:outliner-op :class-add-property}))))
+        {:outliner-op :save-block}))))
 
 (defn class-remove-property!
   [repo class k-uuid]
@@ -269,7 +294,7 @@
             class-new-schema (assoc class-schema :properties new-properties)]
         (db/transact! repo [{:db/id (:db/id class)
                              :block/schema class-new-schema}]
-          {:outliner-op :class-remove-property})))))
+          {:outliner-op :save-block})))))
 
 (defn batch-set-property!
   "Notice that this works only for properties with cardinality equals to `one`."
@@ -304,7 +329,7 @@
                        :block/refs refs}]))))
              block-ids)]
     (when (seq txs)
-      (db/transact! repo txs {:outliner-op :batch-add-property}))))
+      (db/transact! repo txs {:outliner-op :save-block}))))
 
 (defn batch-remove-property!
   [repo block-ids key]
@@ -324,11 +349,24 @@
                            :block/refs refs}])))))
                block-ids)]
       (when (seq txs)
-        (db/transact! repo txs {:outliner-op :remove-property})))))
+        (db/transact! repo txs {:outliner-op :save-block})))))
 
 (defn remove-block-property!
   [repo block-id key]
-  (batch-remove-property! repo [block-id] key))
+  (let [k-name (if (uuid? key)
+                 (:block/original-name (db/entity [:block/uuid key]))
+                 (name key))]
+    (if (contains? #{"alias" "tags"} k-name)
+      (let [attribute (case k-name
+                        "alias"
+                        :block/alias
+                        "tags"
+                        :block/tags)
+            block (db/entity [:block/uuid block-id])]
+        (db/transact! repo
+          [[:db/retract (:db/id block) attribute]]
+          {:outliner-op :save-block}))
+      (batch-remove-property! repo [block-id] key))))
 
 (defn replace-key-with-id!
   "Notice: properties need to be created first"

+ 10 - 7
src/main/frontend/handler/property.cljs

@@ -4,7 +4,8 @@
             [frontend.handler.file-based.property :as file-property]
             [frontend.config :as config]
             [frontend.state :as state]
-            [logseq.graph-parser.util :as gp-util]))
+            [logseq.graph-parser.util :as gp-util]
+            [frontend.db :as db]))
 
 (def builtin-schema-types db-property/builtin-schema-types)
 
@@ -46,14 +47,16 @@
   (state/clear-edit!))
 
 (defn class-add-property!
-  [repo class k-name]
-  (when (config/db-based-graph? repo)
-    (db-property/class-add-property! repo class k-name)))
+  [repo class-uuid k-name]
+  (when-let [class (db/entity repo [:block/uuid class-uuid])]
+    (when (config/db-based-graph? repo)
+     (db-property/class-add-property! repo class k-name))))
 
 (defn class-remove-property!
-  [repo class k-uuid]
-  (when (config/db-based-graph? repo)
-    (db-property/class-remove-property! repo class k-uuid)))
+  [repo class-uuid k-uuid]
+  (when-let [class (db/entity repo [:block/uuid class-uuid])]
+    (when (config/db-based-graph? repo)
+     (db-property/class-remove-property! repo class k-uuid))))
 
 (defn remove-id-property
   [repo format content]