浏览代码

chore: cleanup page-property file based ns

All of frontend.util.page-property was file based except for one line.
Moved that ns to a file-based ns and moved the line out to the more
appropriate property-handler
Gabriel Horner 2 年之前
父节点
当前提交
4b0100cb25

+ 1 - 0
.clj-kondo/config.edn

@@ -83,6 +83,7 @@
              frontend.handler.db-based.property db-property-handler
              frontend.handler.file-based.page file-page-handler
              frontend.handler.file-based.property file-property
+             frontend.handler.file-based.page-property file-page-property
              frontend.handler.file-based.property.util property-util
              frontend.handler.file-based.recent file-recent-handler
              frontend.handler.plugin plugin-handler

+ 3 - 3
docs/dev-practices.md

@@ -179,9 +179,9 @@ For this workflow:
   1. Add `^:focus` metadata flags to tests e.g. `(deftest ^:focus test-name ...)`.
   2. In another shell, run `node static/tests.js -i focus` to only run those
   tests. To run all tests except those tests run `node static/tests.js -e focus`.
-3. Or focus namespaces: Using the regex option `-r`, run tests for `frontend.util.page-property-test` with `node static/tests.js -r page-property`.
+3. Or focus namespaces: Using the regex option `-r`, run tests for `frontend.db.query-dsl-test` with `node static/tests.js -r query-dsl`.
 
-Multiple options can be specified to AND selections. For example, to run all `frontend.util.page-property-test` tests except for the focused one: `node static/tests.js -r page-property -e focus`
+Multiple options can be specified to AND selections. For example, to run all `frontend.db.query-dsl-test` tests except for the focused one: `node static/tests.js -r query-dsl -e focus`
 
 For help on more options, run `node static/tests.js -h`.
 
@@ -190,7 +190,7 @@ For help on more options, run `node static/tests.js -h`.
 To run tests automatically on file save, run `clojure -M:test watch test
 --config-merge '{:autorun true}'`. Specific namespace(s) can be auto run with
 the `:ns-regexp` option e.g. `clojure -M:test watch test --config-merge
-'{:autorun true :ns-regexp "frontend.util.page-property-test"}'`.
+'{:autorun true :ns-regexp "frontend.db.query-dsl-test"}'`.
 
 #### REPL tests
 

+ 1 - 2
src/main/frontend/extensions/pdf/assets.cljs

@@ -14,7 +14,6 @@
             [frontend.ui :as ui]
             [frontend.context.i18n :refer [t]]
             [frontend.extensions.lightbox :as lightbox]
-            [frontend.util.page-property :as page-property]
             [frontend.state :as state]
             [frontend.util :as util]
             [frontend.extensions.pdf.utils :as pdf-utils]
@@ -179,7 +178,7 @@
         (db-model/get-page page-name))
 
       ;; try to update file path
-      (page-property/add-property! page-name :file-path url))
+      (property-handler/add-page-property! page-name :file-path url))
     page))
 
 (defn ensure-ref-block!

+ 2 - 2
src/main/frontend/handler/common/page.cljs

@@ -13,9 +13,9 @@
             [frontend.handler.config :as config-handler]
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.file-based.editor :as file-editor-handler]
+            [frontend.handler.file-based.page-property :as file-page-property]
             [frontend.handler.route :as route-handler]
             [frontend.handler.ui :as ui-handler]
-            [frontend.util.page-property :as page-property]
             [frontend.util.fs :as fs-util]
             [frontend.util :as util]
             [logseq.db.schema :as db-schema]
@@ -46,7 +46,7 @@
              ps (merge p properties)
              content (if db-based?
                        ""
-                       (page-property/insert-properties format "" ps))
+                       (file-page-property/insert-properties format "" ps))
              refs (gp-block/get-page-refs-from-properties properties
                                                           (db/get-db repo)
                                                           (state/get-date-formatter)

+ 4 - 6
src/main/frontend/handler/file_based/page.cljs

@@ -1,11 +1,11 @@
 (ns frontend.handler.file-based.page
   "Page handlers for file based graphs"
-  (:require [frontend.config :as config]
-            [frontend.db :as db]
+  (:require [frontend.db :as db]
             [frontend.db.conn :as conn]
             [frontend.db.utils :as db-utils]
             [frontend.db.model :as model]
             [frontend.handler.file-based.property :as file-property]
+            [frontend.handler.file-based.page-property :as file-page-property]
             [frontend.handler.file-based.recent :as file-recent-handler]
             [frontend.handler.config :as config-handler]
             [frontend.handler.common.page :as page-common-handler]
@@ -15,7 +15,6 @@
             [frontend.state :as state]
             [frontend.util :as util]
             [frontend.util.fs :as fs-util]
-            [frontend.util.page-property :as page-property]
             [frontend.modules.outliner.core :as outliner-core]
             [frontend.modules.outliner.file :as outliner-file]
             [frontend.modules.outliner.tree :as outliner-tree]
@@ -225,9 +224,8 @@
 
         (db/transact! repo page-txs)
 
-        (when (and (not (config/db-based-graph? repo))
-                   (fs-util/create-title-property? new-page-name))
-          (page-property/add-property! new-page-name :title new-name))
+        (when (fs-util/create-title-property? new-page-name)
+          (file-page-property/add-property! new-page-name :title new-name))
 
         (when (and file (not journal?))
           (rename-file! file new-file-name-body (fn [] nil)))

+ 91 - 0
src/main/frontend/handler/file_based/page_property.cljs

@@ -0,0 +1,91 @@
+(ns frontend.handler.file-based.page-property
+  "Page property fns for file graphs"
+  (:require [clojure.string :as string]
+            [frontend.db :as db]
+            [frontend.modules.outliner.core :as outliner-core]
+            [frontend.modules.outliner.file :as outliner-file]
+            [frontend.modules.outliner.transaction :as outliner-tx]
+            [frontend.state :as state]
+            [frontend.util :as util]))
+
+(defn insert-property
+  [format content key value]
+  (when (and (string? content) (not (string/blank? (name key))))
+    (let [key (if (string? key) (keyword key) key)
+          key-part (util/format (case format
+                                  :org "#+%s: "
+                                  "%s:: ") (string/lower-case (name key)))
+          new-property-line (str key-part value)
+          lines (string/split-lines content)
+          key-exists? (atom false)
+          lines (doall
+                 (map (fn [line]
+                        (if (and (string/starts-with?
+                                  (string/lower-case line)
+                                  (string/lower-case key-part))
+                                 (not @key-exists?)) ; only replace the first match
+                          (do
+                            (reset! key-exists? true)
+                            new-property-line)
+                          line)) lines))
+          lines (if (= lines [""]) nil lines)
+          lines (if @key-exists? lines (cons new-property-line lines))]
+      (string/join "\n" lines))))
+
+(defn insert-properties
+  "Updates multiple page properties. Mainly just used in legacy title context"
+  [format 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)
+                (string/join ", "))
+               v)]
+       (insert-property format content k v)))
+   content kvs))
+
+(defn add-property!
+  [page-name key value]
+  (let [repo (state/get-current-repo)]
+    (when-let [page (db/pull [:block/name (util/page-name-sanity-lc page-name)])]
+      (let [key (keyword key)
+            pre-block (db/get-pre-block repo (:db/id page))
+            format (state/get-preferred-format)
+            page-id {:db/id (:db/id page)}
+            org? (= format :org)
+            value (if (contains? #{:filters} key) (pr-str value) value)]
+        (if pre-block
+          (let [properties (:block/properties pre-block)
+                new-properties (assoc properties key value)
+                content (:block/content pre-block)
+                new-content (insert-property format content key value)
+                block {:db/id (:db/id pre-block)
+                       :block/properties new-properties
+                       :block/content new-content
+                       :block/page page-id}
+                tx [(assoc page-id :block/properties new-properties)
+                    block]]
+              ;; (util/pprint tx)
+            (db/transact! tx))
+          (let [block {:block/uuid (db/new-block-id)
+                       :block/left page-id
+                       :block/parent page-id
+                       :block/page page-id
+                       :block/content (if org?
+                                        (str "#+" (string/upper-case (name key)) ": " value)
+                                        (str (name key) ":: " value))
+                       :block/format format
+                       :block/properties {key value}
+                       :block/pre-block? true}
+                page-properties-tx [(assoc page-id :block/properties {key value})]]
+            (outliner-tx/transact!
+             {:outliner-op :insert-blocks
+              :additional-tx page-properties-tx}
+             (outliner-core/insert-blocks! block page {:sibling? false}))))
+        (outliner-file/sync-to-file page-id)))))

+ 3 - 3
src/main/frontend/handler/page.cljs

@@ -17,13 +17,13 @@
             [frontend.handler.notification :as notification]
             [frontend.handler.db-based.page :as db-page-handler]
             [frontend.handler.file-based.page :as file-page-handler]
+            [frontend.handler.property :as property-handler]
             [frontend.handler.ui :as ui-handler]
             [frontend.handler.web.nfs :as web-nfs]
             [frontend.mobile.util :as mobile-util]
             [frontend.state :as state]
             [frontend.util :as util]
             [frontend.util.cursor :as cursor]
-            [frontend.util.page-property :as page-property]
             [frontend.util.page :as page-util]
             [frontend.util.url :as url-util]
             [goog.functions :refer [debounce]]
@@ -98,7 +98,7 @@
 
 (defn update-public-attribute!
   [page-name value]
-  (page-property/add-property! page-name :public value))
+  (property-handler/add-page-property! page-name :public value))
 
 (defn get-page-ref-text
   [page]
@@ -177,7 +177,7 @@
 
 (defn save-filter!
   [page-name filter-state]
-  (page-property/add-property! page-name :filters filter-state))
+  (property-handler/add-page-property! page-name :filters filter-state))
 
 ;; Editor
 (defn page-not-exists-handler

+ 13 - 2
src/main/frontend/handler/property.cljs

@@ -2,7 +2,9 @@
   "Block properties handler."
   (:require [frontend.handler.db-based.property :as db-property-handler]
             [frontend.handler.file-based.property :as file-property]
+            [frontend.handler.file-based.page-property :as file-page-property]
             [frontend.config :as config]
+            [frontend.util :as util]
             [frontend.state :as state]
             [frontend.db :as db]))
 
@@ -35,6 +37,15 @@
   (when (config/db-based-graph? repo)
     (db-property-handler/delete-property-value! repo block property-id property-value)))
 
+(defn add-page-property!
+  "Sanitized page-name, unsanitized key / value"
+  [page-name key value]
+  (let [repo (state/get-current-repo)]
+    (if (config/db-based-graph? repo)
+      (when-let [page (db/pull [:block/name (util/page-name-sanity-lc page-name)])]
+       (set-block-property! repo (:block/uuid page) key value))
+      (file-page-property/add-property! page-name key value))))
+ 
 (defn set-editing-new-property!
   [value]
   (state/set-state! :ui/new-property-input-id value))
@@ -48,13 +59,13 @@
   [repo class-uuid k-name]
   (when-let [class (db/entity repo [:block/uuid class-uuid])]
     (when (config/db-based-graph? repo)
-     (db-property-handler/class-add-property! repo class k-name))))
+      (db-property-handler/class-add-property! repo class k-name))))
 
 (defn class-remove-property!
   [repo class-uuid k-uuid]
   (when-let [class (db/entity repo [:block/uuid class-uuid])]
     (when (config/db-based-graph? repo)
-     (db-property-handler/class-remove-property! repo class k-uuid))))
+      (db-property-handler/class-remove-property! repo class k-uuid))))
 
 (defn remove-id-property
   [repo format content]

+ 3 - 0
src/main/frontend/handler/property/util.cljs

@@ -1,4 +1,7 @@
 (ns frontend.handler.property.util
+  "Utility fns for properties. Most of these are used in file or db graphs.
+  Some fns like lookup and get-property were written to easily be backwards
+  compatible with file graphs"
   (:require [frontend.config :as config]
             [frontend.state :as state]
             [logseq.db.property :as db-property]

+ 0 - 94
src/main/frontend/util/page_property.cljs

@@ -1,94 +0,0 @@
-(ns ^:no-doc frontend.util.page-property
-  (:require [clojure.string :as string]
-            [frontend.db :as db]
-            [frontend.modules.outliner.core :as outliner-core]
-            [frontend.modules.outliner.file :as outliner-file]
-            [frontend.modules.outliner.transaction :as outliner-tx]
-            [frontend.state :as state]
-            [frontend.util :as util]
-            [frontend.handler.property :as property-handler]
-            [frontend.config :as config]))
-
-(defn insert-property
-  [format content key value]
-  (when (and (string? content) (not (string/blank? (name key))))
-    (let [key (if (string? key) (keyword key) key)
-          key-part (util/format (case format
-                                  :org "#+%s: "
-                                  "%s:: ") (string/lower-case (name key)))
-          new-property-line (str key-part value)
-          lines (string/split-lines content)
-          key-exists? (atom false)
-          lines (doall
-                 (map (fn [line]
-                        (if (and (string/starts-with?
-                                  (string/lower-case line)
-                                  (string/lower-case key-part))
-                                 (not @key-exists?)) ; only replace the first match
-                          (do
-                            (reset! key-exists? true)
-                            new-property-line)
-                          line)) lines))
-          lines (if (= lines [""]) nil lines)
-          lines (if @key-exists? lines (cons new-property-line lines))]
-      (string/join "\n" lines))))
-
-(defn insert-properties
-  [format 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)
-                (string/join ", "))
-               v)]
-       (insert-property format content k v)))
-   content kvs))
-
-(defn add-property!
-  "Sanitized page-name, unsanitized key / value"
-  [page-name key value]
-  (let [repo (state/get-current-repo)]
-    (when-let [page (db/pull [:block/name (util/page-name-sanity-lc page-name)])]
-      (if (config/db-based-graph? repo)
-        (property-handler/set-block-property! repo (:block/uuid page) key value)
-        (let [key (keyword key)
-              pre-block (db/get-pre-block repo (:db/id page))
-              format (state/get-preferred-format)
-              page-id {:db/id (:db/id page)}
-              org? (= format :org)
-              value (if (contains? #{:filters} key) (pr-str value) value)]
-          (if pre-block
-            (let [properties (:block/properties pre-block)
-                  new-properties (assoc properties key value)
-                  content (:block/content pre-block)
-                  new-content (insert-property format content key value)
-                  block {:db/id (:db/id pre-block)
-                         :block/properties new-properties
-                         :block/content new-content
-                         :block/page page-id}
-                  tx [(assoc page-id :block/properties new-properties)
-                      block]]
-              ;; (util/pprint tx)
-              (db/transact! tx))
-            (let [block {:block/uuid (db/new-block-id)
-                         :block/left page-id
-                         :block/parent page-id
-                         :block/page page-id
-                         :block/content (if org?
-                                          (str "#+" (string/upper-case (name key)) ": " value)
-                                          (str (name key) ":: " value))
-                         :block/format format
-                         :block/properties {key value}
-                         :block/pre-block? true}
-                  page-properties-tx [(assoc page-id :block/properties {key value})]]
-              (outliner-tx/transact!
-                {:outliner-op :insert-blocks
-                 :additional-tx page-properties-tx}
-                (outliner-core/insert-blocks! block page {:sibling? false}))))
-          (outliner-file/sync-to-file page-id))))))

+ 26 - 26
src/test/frontend/util/page_property_test.cljs → src/test/frontend/handler/file_based/page_property_test.cljs

@@ -1,76 +1,76 @@
-(ns frontend.util.page-property-test
+(ns frontend.handler.file-based.page-property-test
   (:require [cljs.test :refer [are deftest testing]]
-            [frontend.util.page-property :as property]))
+            [frontend.handler.file-based.page-property :as file-page-property]))
 
 (deftest test-insert-property
   (testing "add org page property"
     (are [x y] (= x y)
-      (property/insert-property :org "" :title "title")
+      (file-page-property/insert-property :org "" :title "title")
       "#+title: title"
 
-      (property/insert-property :org "hello" :title "title")
+      (file-page-property/insert-property :org "hello" :title "title")
       "#+title: title\nhello"
 
-      (property/insert-property :org "#+title: title\nhello" :title "new title")
+      (file-page-property/insert-property :org "#+title: title\nhello" :title "new title")
       "#+title: new title\nhello"
 
-      (property/insert-property :org "#+title: title\nhello" :alias "alias1")
+      (file-page-property/insert-property :org "#+title: title\nhello" :alias "alias1")
       "#+alias: alias1\n#+title: title\nhello"
 
-      (property/insert-property :org "#+title: title\n#+alias: alias1\nhello" :alias "alias2")
+      (file-page-property/insert-property :org "#+title: title\n#+alias: alias1\nhello" :alias "alias2")
       "#+title: title\n#+alias: alias2\nhello"
 
-      (property/insert-property :org "#+title: title\n#+alias: alias1, alias2\nhello" :alias "alias3")
+      (file-page-property/insert-property :org "#+title: title\n#+alias: alias1, alias2\nhello" :alias "alias3")
       "#+title: title\n#+alias: alias3\nhello"))
 
   (testing "add markdown page property"
     (are [x y] (= x y)
-      (property/insert-property :markdown "" :title "title")
+      (file-page-property/insert-property :markdown "" :title "title")
       "title:: title"
 
-      (property/insert-property :markdown "hello" :title "title")
+      (file-page-property/insert-property :markdown "hello" :title "title")
       "title:: title\nhello"
 
-      (property/insert-property :markdown "title:: title\nhello" :title "new title")
+      (file-page-property/insert-property :markdown "title:: title\nhello" :title "new title")
       "title:: new title\nhello"
 
-      (property/insert-property :markdown "title:: title\nhello" :alias "alias1")
+      (file-page-property/insert-property :markdown "title:: title\nhello" :alias "alias1")
       "alias:: alias1\ntitle:: title\nhello"
 
-      (property/insert-property :markdown "title:: title\nalias:: alias1\nhello" :alias "alias2")
+      (file-page-property/insert-property :markdown "title:: title\nalias:: alias1\nhello" :alias "alias2")
       "title:: title\nalias:: alias2\nhello"
 
-      (property/insert-property :markdown "title:: title\nalias:: alias1, alias2\nhello" :alias "alias3")
+      (file-page-property/insert-property :markdown "title:: title\nalias:: alias1, alias2\nhello" :alias "alias3")
       "title:: title\nalias:: alias3\nhello"
 
-      (property/insert-property :markdown "title:: title\nalias:: alias1, alias2\nhello" :aliases "aliases1")
+      (file-page-property/insert-property :markdown "title:: title\nalias:: alias1, alias2\nhello" :aliases "aliases1")
       "aliases:: aliases1\ntitle:: title\nalias:: alias1, alias2\nhello"
 
-      (property/insert-property :markdown "title:: title\nalias:: alias1, alias2\naliases:: aliases1\nhello" :aliases "aliases2")
+      (file-page-property/insert-property :markdown "title:: title\nalias:: alias1, alias2\naliases:: aliases1\nhello" :aliases "aliases2")
       "title:: title\nalias:: alias1, alias2\naliases:: aliases2\nhello")))
 
 (deftest test-insert-properties
   (testing "add org page properties"
     (are [x y] (= x y)
 
-      (property/insert-properties :org "" {:title "title"})
+      (file-page-property/insert-properties :org "" {:title "title"})
       "#+title: title"
 
-      (property/insert-properties :org "hello" {:title "title"})
+      (file-page-property/insert-properties :org "hello" {:title "title"})
       "#+title: title\nhello"
 
-      (property/insert-properties :org "#+title: title\nhello"
+      (file-page-property/insert-properties :org "#+title: title\nhello"
                                   {:title "new title"
                                    :alias "alias1"})
       "#+alias: alias1\n#+title: new title\nhello"
 
-      (property/insert-properties :org "#+title: title\n#+alias: alias1\nhello"
+      (file-page-property/insert-properties :org "#+title: title\n#+alias: alias1\nhello"
                                   {:title "new title"
                                    :alias "alias2"
                                    :aliases "aliases1"})
       "#+aliases: aliases1\n#+title: new title\n#+alias: alias2\nhello"
 
-      (property/insert-properties :org "#+title: title\n#+alias: alias1, alias2\n#+aliases: aliases1\nhello"
+      (file-page-property/insert-properties :org "#+title: title\n#+alias: alias1, alias2\n#+aliases: aliases1\nhello"
                                   {:title "new title"
                                    :alias "alias2"
                                    :aliases "aliases1"})
@@ -78,24 +78,24 @@
 
   (testing "add markdown page properties"
     (are [x y] (= x y)
-      (property/insert-properties :markdown "" {:title "title"})
+      (file-page-property/insert-properties :markdown "" {:title "title"})
       "title:: title"
 
-      (property/insert-properties :markdown "hello" {:title "title"})
+      (file-page-property/insert-properties :markdown "hello" {:title "title"})
       "title:: title\nhello"
 
-      (property/insert-properties :markdown "title:: title\nhello"
+      (file-page-property/insert-properties :markdown "title:: title\nhello"
                                   {:title "new title"
                                    :alias "alias1"})
       "alias:: alias1\ntitle:: new title\nhello"
 
-      (property/insert-properties :markdown "title:: title\nalias:: alias1\nhello"
+      (file-page-property/insert-properties :markdown "title:: title\nalias:: alias1\nhello"
                                   {:title "new title"
                                    :alias "alias2"
                                    :aliases "aliases1"})
       "aliases:: aliases1\ntitle:: new title\nalias:: alias2\nhello"
 
-      (property/insert-properties :markdown "title:: title\nalias:: alias1, alias2\naliases:: aliases1\nhello"
+      (file-page-property/insert-properties :markdown "title:: title\nalias:: alias1, alias2\naliases:: aliases1\nhello"
                                   {:title "new title"
                                    :alias "alias2"
                                    :aliases "aliases1"})