浏览代码

fix: properties handling

Tienson Qin 4 年之前
父节点
当前提交
46685ba4da

+ 1 - 4
src/main/frontend/components/block.cljs

@@ -1458,10 +1458,7 @@
            (rum/with-key elem (str (random-uuid)))))
 
        :else
-       (let [page-name (string/lower-case (str v))]
-         (if (db/entity [:block/name page-name])
-           (page-cp config {:block/name page-name})
-           (inline-text (:block/format block) (str v)))))]))
+       (inline-text (:block/format block) (str v)))]))
 
 (rum/defc properties-cp
   [config block]

+ 8 - 7
src/main/frontend/handler/editor.cljs

@@ -635,13 +635,17 @@
                                  last-block-id (:db/id (last blocks))]
                              (when last-block-id
                                (db/pull last-block-id))))
+              format (or
+                      (:block/format block)
+                      (db/get-page-format (:db/id block))
+                      :markdown)
+              content (if (seq properties)
+                        (property/insert-properties format content properties)
+                        content)
               new-block (-> (select-keys block [:block/page :block/file :block/journal?
                                                 :block/journal-day])
                             (assoc :block/content content
-                                   :block/format (or
-                                                  (:block/format block)
-                                                  (db/get-page-format (:db/id block))
-                                                  :markdown))
+                                   :block/format format)
                             (wrap-parse-block)
                             (assoc :block/uuid (db/new-block-id)))
               new-block (if (:block/page new-block)
@@ -649,9 +653,6 @@
                           (assoc new-block :block/page (:db/id block)))
               new-block (if-let [db-id (:db/id (:block/file block))]
                           (assoc new-block :block/file db-id)
-                          new-block)
-              new-block (if (and (map? properties) (seq properties))
-                          (update new-block :block/properties (fn [m] (merge m properties)))
                           new-block)]
           (let [[block-m sibling?] (cond
                                      before?

+ 15 - 1
src/main/frontend/util/property.cljs

@@ -226,7 +226,21 @@
 
 (defn insert-properties
   [format content kvs]
-  (reduce (fn [content [k v]] (insert-property format content k v)) content kvs))
+  (reduce
+   (fn [content [k v]]
+     (let [k (if (string? k)
+               (keyword (-> (string/lower-case k)
+                            (string/replace " " "-")))
+               k)
+           v (if (coll? v)
+               (some->>
+                (seq v)
+                (distinct)
+                (map (fn [item] (util/format "[[%s]]" (text/page-ref-un-brackets! item))))
+                (string/join ", "))
+               v)]
+       (insert-property format content k v)))
+   content kvs))
 
 (defn remove-property
   ([format key content]

+ 23 - 0
src/test/frontend/util/property_test.cljs

@@ -77,6 +77,29 @@
   #+END_QUOTE" "c" "d")
     "c:: d\n#+BEGIN_QUOTE\n hello world\n  #+END_QUOTE"))
 
+(deftest test-insert-properties
+  (are [x y] (= x y)
+    (property/insert-properties :markdown "" {:foo "bar"})
+    "foo:: bar"
+
+    (property/insert-properties :markdown "" {"foo" "bar"})
+    "foo:: bar"
+
+    (property/insert-properties :markdown "" {"foo space" "bar"})
+    "foo-space:: bar"
+
+    (property/insert-properties :markdown "" {:foo #{"bar" "baz"}})
+    "foo:: [[bar]], [[baz]]"
+
+    (property/insert-properties :markdown "" {:foo ["bar" "bar" "baz"]})
+    "foo:: [[bar]], [[baz]]"
+
+    (property/insert-properties :markdown "a\nb\n" {:foo ["bar" "bar" "baz"]})
+    "a\nfoo:: [[bar]], [[baz]]\nb"
+
+    (property/insert-properties :markdown "" {:foo "\"bar, baz\""})
+    "foo:: \"bar, baz\""))
+
 (deftest test->new-properties
   (are [x y] (= (property/->new-properties x) y)
     ":PROPERTIES:\n:foo: bar\n:END:"