Kaynağa Gözat

feat: save property value as a block UUID if it's a tag string

Tienson Qin 2 yıl önce
ebeveyn
işleme
0c093f778b

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

@@ -664,9 +664,10 @@
   (when (uuid? k)
     (when-let [property (db/sub-block (:db/id (db/entity [:block/uuid k])))]
       (let [type (get-in property [:block/schema :type] :default)
-            block? (and (contains? #{:default :template} type)
-                        (uuid? v)
-                        (db/entity [:block/uuid v]))
+            v-block (when (uuid? v) (db/entity [:block/uuid v]))
+            block? (and v-block
+                        (:block/page v-block)
+                        (contains? #{:default :template} type))
             collapsed? (when block? (property-collapsed? block property))
             date? (= type :date)]
         [:div {:class (cond

+ 15 - 2
src/main/frontend/components/property/value.cljs

@@ -444,7 +444,7 @@
 
 (rum/defcs property-scalar-value < rum/reactive db-mixins/query
   (rum/local nil ::ref)
-  [state block property value {:keys [inline-text block-cp
+  [state block property value {:keys [inline-text block-cp page-cp
                                       editor-id dom-id row?
                                       editor-box editor-args editing?
                                       on-chosen]
@@ -524,7 +524,20 @@
                                              opts)
 
                     :block
-                    (property-block-value value block-cp editor-box)
+                    (let [block (db/entity [:block/uuid value])]
+                      (cond
+                        (:block/page block)
+                        ;; normal block
+                        (property-block-value value block-cp editor-box)
+
+                        ;; page/class/etc.
+                        (:block/name block)
+                        (let [class? (contains? (:block/type block) "class")]
+                          (page-cp {:disable-preview? true
+                                    :hide-close-button? true
+                                    :tag? class?} block))
+                        :else
+                        (js/console.error "Invalid property value: " block)))
 
                     (inline-text {} :markdown (str value)))))]))]))))
 

+ 26 - 2
src/main/frontend/handler/db_based/property.cljs

@@ -11,7 +11,8 @@
             [logseq.db.sqlite.util :as sqlite-util]
             [logseq.db.property.type :as db-property-type]
             [malli.util :as mu]
-            [malli.error :as me]))
+            [malli.error :as me]
+            [frontend.format.block :as block]))
 
 ;; schema -> type, cardinality, object's class
 ;;           min, max -> string length, number range, cardinality size limit
@@ -131,6 +132,28 @@
                                   :block/refs refs}]
                                 {:outliner-op :save-block}))))))))))
 
+(defn resolve-tag-when-possible
+  "Change `v` to a tag's UUID if v is a string tag, e.g. `#book`"
+  [v]
+  (if (and (string? v)
+           (string/starts-with? v "#")
+           (not (string/includes? v " ")))
+    (let [tag (gp-util/safe-subs (string/trim v) 1)]
+      (when-not (string/blank? tag)
+        (let [e (db/entity [:block/name (util/page-name-sanity-lc tag)])
+              e' (if e
+                   (do
+                     (when-not (contains? (:block/type e) "tag")
+                       (db/transact! [{:db/id (:db/id e)
+                                       :block/type (set (conj (:block/type e) "class"))}]))
+                     e)
+                   (let [m (assoc (block/page-name->map tag true)
+                                  :block/type #{"class"})]
+                     (db/transact! [m])
+                     m))]
+          (:block/uuid e'))))
+    v))
+
 (defn set-block-property!
   [repo block-id k-name v {:keys [old-value] :as opts}]
   (let [block (db/entity repo [:block/uuid block-id])
@@ -139,7 +162,8 @@
         property-uuid (or (:block/uuid property) (db/new-block-id))
         property-schema (:block/schema property)
         {:keys [type cardinality]} property-schema
-        multiple-values? (= cardinality :many)]
+        multiple-values? (= cardinality :many)
+        v (resolve-tag-when-possible v)]
     (if (and multiple-values? (coll? v))
       (reset-block-property-multiple-values! repo block-id k-name v opts)
       (let [v (if property v (or v ""))]