فهرست منبع

enhance: create an instance for writing if property's value is a tag

Tienson Qin 2 سال پیش
والد
کامیت
57c27d7472
3فایلهای تغییر یافته به همراه48 افزوده شده و 24 حذف شده
  1. 27 14
      src/main/frontend/components/property/value.cljs
  2. 9 9
      src/main/frontend/handler/db_based/property.cljs
  3. 12 1
      src/main/frontend/util.cljc

+ 27 - 14
src/main/frontend/components/property/value.cljs

@@ -272,11 +272,13 @@
     (editor-handler/edit-block! (db/entity [:block/uuid last-block-id]) :max last-block-id)))
 
 (defn create-new-block-from-template!
+  "`template`: tag block"
   [block property template]
   (let [repo (state/get-current-repo)
         {:keys [page blocks]} (property-handler/property-create-new-block-from-template block property template)]
     (db/transact! repo (if page (cons page blocks) blocks) {:outliner-op :insert-blocks})
-    (add-property! block (:block/original-name property) (:block/uuid (last blocks)))))
+    (add-property! block (:block/original-name property) (:block/uuid (last blocks)))
+    (last blocks)))
 
 (defn- new-text-editor-opts
   [repo block property value editor-id]
@@ -297,10 +299,9 @@
                   (not (state/get-editor-action)))
          (when-not backspace? (util/stop e))
          (cond
-           esc?
-           (save-text! repo block property value editor-id e)
-
-           (and enter? new-property?)
+           (or esc?
+               (and enter? new-property?)
+               (and enter? (util/tag? new-value)))
            (save-text! repo block property value editor-id e)
 
            enter?
@@ -505,7 +506,13 @@
                              (set-editing! property editor-id dom-id value {:ref @*ref})))}
               (let [type (or (when (and (= type :default) (uuid? value)) :block)
                              type
-                             :default)]
+                             :default)
+                    type (if (= :block type)
+                           (let [v-block (db/entity [:block/uuid value])]
+                             (if (get-in v-block [:block/metadata :created-from-template])
+                               :template
+                               type))
+                           type)]
                 (if (string/blank? value)
                   (if (= :template type)
                     (let [id (first (:classes schema))
@@ -524,20 +531,26 @@
                                              opts)
 
                     :block
-                    (let [block (db/entity [:block/uuid value])]
+                    (let [v-block (db/entity [:block/uuid value])
+                          class? (contains? (:block/type v-block) "class")]
                       (cond
-                        (:block/page block)
+                        (:block/page v-block)
                         ;; normal block
                         (property-block-value value block-cp editor-box)
 
+                        (and class? (seq (:properties (:block/schema v-block))))
+                        (let [template-instance-block (create-new-block-from-template! block property v-block)]
+                          (property-template-value {:editor-id editor-id}
+                                                   (:block/uuid template-instance-block)
+                                                   opts))
+
                         ;; 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))
+                        (:block/name v-block)
+                        (page-cp {:disable-preview? true
+                                  :hide-close-button? true
+                                  :tag? class?} v-block)
                         :else
-                        (js/console.error "Invalid property value: " block)))
+                        (js/console.error "Invalid property value: " v-block)))
 
                     (inline-text {} :markdown (str value)))))]))]))))
 

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

@@ -12,7 +12,8 @@
             [logseq.db.property.type :as db-property-type]
             [malli.util :as mu]
             [malli.error :as me]
-            [frontend.format.block :as block]))
+            [frontend.format.block :as block]
+            [logseq.graph-parser.util.page-ref :as page-ref]))
 
 ;; schema -> type, cardinality, object's class
 ;;           min, max -> string length, number range, cardinality size limit
@@ -132,13 +133,13 @@
                                   :block/refs refs}]
                                 {:outliner-op :save-block}))))))))))
 
-(defn resolve-tag-when-possible
+(defn resolve-tag
   "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 (and (string? v)
+             (util/tag? (string/trim v)))
+    (let [tag-without-hash (gp-util/safe-subs (string/trim v) 1)
+          tag (or (page-ref/get-page-name tag-without-hash) tag-without-hash)]
       (when-not (string/blank? tag)
         (let [e (db/entity [:block/name (util/page-name-sanity-lc tag)])
               e' (if e
@@ -151,8 +152,7 @@
                                   :block/type #{"class"})]
                      (db/transact! [m])
                      m))]
-          (:block/uuid e'))))
-    v))
+          (:block/uuid e'))))))
 
 (defn set-block-property!
   [repo block-id k-name v {:keys [old-value] :as opts}]
@@ -163,7 +163,7 @@
         property-schema (:block/schema property)
         {:keys [type cardinality]} property-schema
         multiple-values? (= cardinality :many)
-        v (resolve-tag-when-possible v)]
+        v (or (resolve-tag v) 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 ""))]

+ 12 - 1
src/main/frontend/util.cljc

@@ -1528,7 +1528,7 @@ Arg *stop: atom, reset to true to stop the loop"
        (with-meta o meta)
        o)))
 
-   ;; from rum
+;; from rum
 #?(:cljs
    (def schedule
      (or (and (exists? js/window)
@@ -1537,3 +1537,14 @@ Arg *stop: atom, reset to true to stop the loop"
                   js/window.mozRequestAnimationFrame
                   js/window.msRequestAnimationFrame))
          #(js/setTimeout % 16))))
+
+#?(:cljs
+   (defn tag?
+     "Whether `s` is a tag."
+     [s]
+     (and (string? s)
+          (string/starts-with? s "#")
+          (or
+           (not (string/includes? s " "))
+           (string/starts-with? s "#[[")
+           (string/ends-with? s "]]")))))