Răsfoiți Sursa

Fix property values not autocompleting full ref values

Page refs and tags were not autocompleting their characters
which led to unreferenced completions
Gabriel Horner 3 ani în urmă
părinte
comite
350323ad19
2 a modificat fișierele cu 49 adăugiri și 5 ștergeri
  1. 24 5
      src/main/frontend/db/model.cljs
  2. 25 0
      src/test/frontend/db/model_test.cljs

+ 24 - 5
src/main/frontend/db/model.cljs

@@ -20,6 +20,7 @@
             [logseq.db.schema :as db-schema]
             [logseq.graph-parser.config :as gp-config]
             [logseq.graph-parser.text :as text]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [logseq.graph-parser.util.db :as db-util]
             [logseq.graph-parser.util :as gp-util]))
 
@@ -1424,19 +1425,37 @@ independent of format as format specific heading characters are stripped"
          distinct
          sort)))
 
+(defn- property-value-for-refs-and-text
+  "Given a property value's refs and full text, determines the value to
+  autocomplete"
+  [[refs text]]
+  (if (or (not (coll? refs)) (= 1 (count refs)))
+    text
+    (map #(cond
+            (string/includes? text (page-ref/->page-ref %))
+            (page-ref/->page-ref %)
+            (string/includes? text (str "#" %))
+            (str "#" %)
+            :else
+            %)
+         refs)))
+
 (defn get-property-values
   [property]
-  (let [pred (fn [_db properties]
-               (get properties property))]
+  (let [pred (fn [_db properties text-properties]
+               [(get properties property)
+                (get text-properties property)])]
     (->>
      (d/q
-      '[:find [?property-val ...]
+      '[:find ?property-val ?text-property-val
         :in $ ?pred
         :where
-        [_ :block/properties ?p]
-        [(?pred $ ?p) ?property-val]]
+        [?b :block/properties ?p]
+        [?b :block/properties-text-values ?p2]
+        [(?pred $ ?p ?p2) [?property-val ?text-property-val]]]
       (conn/get-db)
       pred)
+     (map property-value-for-refs-and-text)
      (map (fn [x] (if (coll? x) x [x])))
      (apply concat)
      (map str)

+ 25 - 0
src/test/frontend/db/model_test.cljs

@@ -7,6 +7,7 @@
             [frontend.test.helper :as test-helper :refer [load-test-files]]
             [datascript.core :as d]
             [shadow.resource :as rc]
+            [clojure.set :as set]
             [clojure.edn :as edn]))
 
 (use-fixtures :each {:before test-helper/start-test-db!
@@ -161,3 +162,27 @@ foo:: bar"}])
            (try (model/get-block-children-ids-in-db db #uuid"e538d319-48d4-4a6d-ae70-c03bb55b6fe4")
                 (catch :default e
                   (ex-message e)))))))
+
+(deftest get-property-values
+  (load-test-files [{:file/path "pages/Feature.md"
+                     :file/content "type:: [[Class]]"}
+                    {:file/path "pages/Class.md"
+                     :file/content "type:: https://schema.org/Class\npublic:: true"}
+                    {:file/path "pages/DatePicker.md"
+                     :file/content "type:: #Feature, #Command"}
+                    {:file/path "pages/Whiteboard___Tool___Eraser.md"
+                     :file/content "type:: [[Tool]], [[Whiteboard/Object]]"}])
+
+  (let [type-values (set (model/get-property-values :type))
+        public-values (set (model/get-property-values :public))]
+
+    (is (contains? type-values "[[Class]]")
+        "Property value from single page-ref is wrapped in square brackets")
+    (is (= #{} (set/difference #{"[[Tool]]" "[[Whiteboard/Object]]"} type-values))
+        "Property values from multiple page-refs are wrapped in square brackets")
+    (is (= #{} (set/difference #{"#Feature" "#Command"} type-values))
+        "Property values from multiple tags have hashtags")
+    (is (contains? type-values "https://schema.org/Class")
+        "Property value text is not modified")
+    (is (contains? public-values "true")
+        "Property value that is not text is not modified")))