ソースを参照

Move worker.util fns to common.util

Tienson Qin 2 年 前
コミット
a6f83c2445

+ 25 - 1
deps/common/src/logseq/common/util.cljs

@@ -5,7 +5,8 @@
             [clojure.edn :as edn]
             [clojure.string :as string]
             [clojure.walk :as walk]
-            [logseq.common.log :as log]))
+            [logseq.common.log :as log]
+            [goog.string :as gstring]))
 
 (defn safe-decode-uri-component
   [uri]
@@ -166,6 +167,11 @@
   [s]
   (page-name-sanity (string/lower-case s)))
 
+(defn safe-page-name-sanity-lc
+  [s]
+  (if (string? s)
+    (page-name-sanity-lc s) s))
+
 (defn capitalize-all
   [s]
   (some->> (string/split s #" ")
@@ -305,3 +311,21 @@
   {:malli/schema [:=> [:cat :string] :boolean]}
   [s]
   (boolean (safe-re-find exactly-uuid-pattern s)))
+
+(defn format
+  [fmt & args]
+  (apply gstring/format fmt args))
+
+(defn remove-first [pred coll]
+  ((fn inner [coll]
+     (lazy-seq
+      (when-let [[x & xs] (seq coll)]
+        (if (pred x)
+          xs
+          (cons x (inner xs))))))
+   coll))
+
+(defn concat-without-nil
+  [& cols]
+  (->> (apply concat cols)
+       (remove nil?)))

ファイルの差分が大きいため隠しています
+ 3 - 3
deps/graph-parser/test/logseq/graph_parser/util/file_name_test.cljs


+ 7 - 7
src/main/frontend/modules/outliner/core.cljs

@@ -26,7 +26,7 @@
 
 (defn block
   [db m]
-  (assert (or (map? m) (de/entity? m)) (util/format "block data must be map or entity, got: %s %s" (type m) m))
+  (assert (or (map? m) (de/entity? m)) (common-util/format "block data must be map or entity, got: %s %s" (type m) m))
   (let [e (if (or (de/entity? m)
                   (and (:block/uuid m) (:db/id m)))
             m
@@ -105,8 +105,8 @@
                 (let [properties (:block/properties m)
                       alias (set (:alias properties))
                       tags (set (:tags properties))
-                      alias (map (fn [p] {:block/name (util/page-name-sanity-lc p)}) alias)
-                      tags (map (fn [p] {:block/name (util/page-name-sanity-lc p)}) tags)
+                      alias (map (fn [p] {:block/name (common-util/page-name-sanity-lc p)}) alias)
+                      tags (map (fn [p] {:block/name (common-util/page-name-sanity-lc p)}) tags)
                       deleteable-page-attributes {:block/alias alias
                                                   :block/tags tags
                                                   :block/properties properties
@@ -781,7 +781,7 @@
                       (when-let [left (last (filter (fn [b] (= 1 (:block/level b))) tx))]
                         [{:block/uuid (otree/-get-id next conn)
                           :block/left (:db/id left)}]))
-            full-tx (util/concat-without-nil (if (and keep-uuid? replace-empty-target?) (rest uuids-tx) uuids-tx) tx next-tx)]
+            full-tx (common-util/concat-without-nil (if (and keep-uuid? replace-empty-target?) (rest uuids-tx) uuids-tx) tx next-tx)]
         {:tx-data full-tx
          :blocks  tx}))))
 
@@ -820,7 +820,7 @@
        (mapcat (fn [[_page blocks]]
                  (let [blocks (ldb/sort-page-random-blocks db blocks)
                        non-consecutive-blocks (->> (conj (ldb/get-non-consecutive-blocks db blocks) (last blocks))
-                                                   (util/distinct-by :db/id))]
+                                                   (common-util/distinct-by :db/id))]
                    (when (seq non-consecutive-blocks)
                      (map-indexed (fn [idx block]
                                     (when-let [right (get-right-sibling db (:db/id block))]
@@ -958,7 +958,7 @@
                     fix-non-consecutive-tx (->> (fix-non-consecutive-blocks db blocks target-block sibling?)
                                                 (remove (fn [b]
                                                           (contains? (set (map :db/id move-blocks-next-tx)) (:db/id b)))))
-                    full-tx (util/concat-without-nil tx-data move-blocks-next-tx children-page-tx fix-non-consecutive-tx)
+                    full-tx (common-util/concat-without-nil tx-data move-blocks-next-tx children-page-tx fix-non-consecutive-tx)
                     tx-meta (cond-> {:move-blocks (mapv :db/id blocks)
                                      :move-op outliner-op
                                      :target (:db/id target-block)}
@@ -1015,7 +1015,7 @@
             parent (:block/parent first-block)
             concat-tx-fn (fn [& results]
                            {:tx-data (->> (map :tx-data results)
-                                          (apply util/concat-without-nil))
+                                          (apply common-util/concat-without-nil))
                             :tx-meta (:tx-meta (first results))})
             opts {:outliner-op :indent-outdent-blocks}]
         (if indent?

+ 1 - 2
src/main/frontend/modules/outliner/datascript.cljs

@@ -1,7 +1,6 @@
 (ns frontend.modules.outliner.datascript
   (:require [logseq.common.util :as common-util]
             [logseq.graph-parser.util.block-ref :as block-ref]
-            [frontend.worker.util :as worker-util]
             [logseq.db.sqlite.util :as sqlite-util]
             [frontend.worker.file.property-util :as wpu]
             [datascript.core :as d]
@@ -42,7 +41,7 @@
                                              block-content (wpu/remove-properties
                                                             (:block/format block) (:block/content block))
                                              new-content (some-> (:block/content ref)
-                                                                 (string/replace (re-pattern (worker-util/format "(?i){{embed \\(\\(%s\\)\\)\\s?}}" (str (:block/uuid block))))
+                                                                 (string/replace (re-pattern (common-util/format "(?i){{embed \\(\\(%s\\)\\)\\s?}}" (str (:block/uuid block))))
                                                                                  block-content)
                                                                  (string/replace (block-ref/->block-ref (str (:block/uuid block)))
                                                                                  block-content))

+ 2 - 2
src/main/frontend/modules/outliner/utils.cljs

@@ -1,6 +1,6 @@
 (ns frontend.modules.outliner.utils
   (:require [datascript.impl.entity :as e]
-            [frontend.worker.util :as worker-util]))
+            [logseq.common.util :as common-util]))
 
 (defn block-id?
   [id]
@@ -12,7 +12,7 @@
 (defn check-block-id
   [id]
   (assert (block-id? id)
-    (worker-util/format "The id should match block-id?: %s" (pr-str id))))
+    (common-util/format "The id should match block-id?: %s" (pr-str id))))
 
 (defn ->block-lookup-ref
   "

+ 7 - 7
src/main/frontend/util.cljc

@@ -74,11 +74,11 @@
   (string/join "/" parts))
 
 #?(:cljs
-   (def safe-re-find worker-util/safe-re-find))
+   (def safe-re-find common-util/safe-re-find))
 
 #?(:cljs
    (do
-     (def uuid-string? worker-util/uuid-string?)
+     (def uuid-string? common-util/uuid-string?)
      (defn check-password-strength
        {:malli/schema [:=> [:cat :string] [:maybe
                                            [:map
@@ -156,7 +156,7 @@
      (string/starts-with? js/window.location.href "file://")))
 
 #?(:cljs
-   (def format worker-util/format))
+   (def format common-util/format))
 
 #?(:clj
    (defn format
@@ -583,7 +583,7 @@
 
 
 #?(:cljs
-   (def distinct-by worker-util/distinct-by))
+   (def distinct-by common-util/distinct-by))
 
 #?(:cljs
    (defn distinct-by-last-wins
@@ -865,7 +865,7 @@
     @result))
 
 #?(:cljs
-   (def concat-without-nil worker-util/concat-without-nil))
+   (def concat-without-nil common-util/concat-without-nil))
 
 #?(:cljs
    (defn set-title!
@@ -1026,7 +1026,7 @@
      common-util/page-name-sanity-lc))
 
 #?(:cljs
-   (def safe-page-name-sanity-lc worker-util/safe-page-name-sanity-lc))
+   (def safe-page-name-sanity-lc common-util/safe-page-name-sanity-lc))
 
 (defn get-page-original-name
   [page]
@@ -1147,7 +1147,7 @@
      (js/console.trace)))
 
 #?(:cljs
-   (def remove-first worker-util/remove-first))
+   (def remove-first common-util/remove-first))
 
 #?(:cljs
    (defn backward-kill-word

+ 4 - 4
src/main/frontend/worker/file/property_util.cljs

@@ -12,9 +12,9 @@
     (let [org? (= format :org)
           kv-format (if org? ":%s: %s" (str "%s" gp-property/colons " %s"))
           full-format (if org? ":PROPERTIES:\n%s\n:END:" "%s\n")
-          properties-content (->> (map (fn [[k v]] (util/format kv-format (name k) v)) properties)
+          properties-content (->> (map (fn [[k v]] (common-util/format kv-format (name k) v)) properties)
                                   (string/join "\n"))]
-      (util/format full-format properties-content))))
+      (common-util/format full-format properties-content))))
 
 (defn simplified-property?
   [line]
@@ -26,7 +26,7 @@
   [line]
   (boolean
    (and (string? line)
-        (util/safe-re-find #"^\s*[^ ]+:" line))))
+        (common-util/safe-re-find #"^\s*[^ ]+:" line))))
 
 (defn insert-property
   "Only accept nake content (without any indentation)"
@@ -127,7 +127,7 @@
    (when (not (string/blank? (name key)))
      (let [format (or format :markdown)
            key (string/lower-case (name key))
-           remove-f (if first? util/remove-first remove)]
+           remove-f (if first? common-util/remove-first remove)]
        (if (and (= format :org) (not (gp-property/contains-properties? content)))
          content
          (let [lines (->> (string/split-lines content)

+ 2 - 3
src/main/frontend/worker/file/util.cljs

@@ -1,8 +1,7 @@
 (ns frontend.worker.file.util
   "File name fns"
   (:require [clojure.string :as string]
-            [logseq.common.util :as common-util]
-            [frontend.worker.util :as util]))
+            [logseq.common.util :as common-util]))
 
 ;; Update repo/invalid-graph-name-warning if characters change
 (def multiplatform-reserved-chars ":\\*\\?\"<>|\\#\\\\")
@@ -72,7 +71,7 @@
 (defn include-reserved-chars?
   "Includes reserved characters that would broken FS"
   [s]
-  (util/safe-re-find reserved-chars-pattern s))
+  (common-util/safe-re-find reserved-chars-pattern s))
 
 ;; A fast pprint alternative.
 (defn print-prefix-map* [prefix m print-one writer opts]

+ 2 - 2
src/main/frontend/worker/mldoc.cljs

@@ -7,7 +7,7 @@
             [logseq.graph-parser.text :as text]
             [clojure.walk :as walk]
             [logseq.graph-parser.block :as gp-block]
-            [frontend.worker.util :as worker-util]))
+            [logseq.common.util :as common-util]))
 
 (defn get-default-config
   "Gets a mldoc default config for the given format. Works for DB and file graphs"
@@ -97,7 +97,7 @@
           refs' (->> (concat page-refs block-refs)
                      (remove string/blank?)
                      distinct)]
-      (-> (map #(if (worker-util/uuid-string? %)
+      (-> (map #(if (common-util/uuid-string? %)
                   {:block/uuid (uuid %)}
                   (gp-block/page-name->map % true db true date-formatter))
                refs')

+ 2 - 2
src/main/frontend/worker/react.cljs

@@ -1,7 +1,7 @@
 (ns frontend.worker.react
   "Compute reactive query affected keys"
   (:require [datascript.core :as d]
-            [frontend.worker.util :as util]
+            [logseq.common.util :as common-util]
             [cljs.spec.alpha :as s]))
 
 ;;; keywords specs for reactive query, used by `react/q` calls
@@ -42,7 +42,7 @@
                           (map :e))
         blocks (-> (concat blocks other-blocks) distinct)
         block-entities (keep (fn [block-id]
-                               (let [block-id (if (and (string? block-id) (util/uuid-string? block-id))
+                               (let [block-id (if (and (string? block-id) (common-util/uuid-string? block-id))
                                                 [:block/uuid block-id]
                                                 block-id)]
                                  (d/entity db-after block-id))) blocks)

+ 6 - 5
src/main/frontend/worker/search.cljs

@@ -8,7 +8,8 @@
             [datascript.core :as d]
             [frontend.search.fuzzy :as fuzzy]
             [frontend.worker.util :as util]
-            [logseq.db.sqlite.util :as sqlite-util]))
+            [logseq.db.sqlite.util :as sqlite-util]
+            [logseq.common.util :as common-util]))
 
 ;; TODO: use sqlite for fuzzy search
 (defonce indices (atom nil))
@@ -162,7 +163,7 @@
                                     :page page})))]
       (->>
        all-result
-       (util/distinct-by :uuid)
+       (common-util/distinct-by :uuid)
        (take limit)))))
 
 (defn truncate-table!
@@ -311,7 +312,7 @@
   (when page
     (if (string? page)
       (and (string/starts-with? page "$$$")
-           (util/uuid-string? (safe-subs page 3)))
+           (common-util/uuid-string? (safe-subs page 3)))
       (contains? (set (:block/type page)) "hidden"))))
 
 (defn get-all-pages
@@ -393,7 +394,7 @@
                      (.remove indice
                               (fn [page]
                                 (= (:block/name page-entity)
-                                   (util/safe-page-name-sanity-lc (gobj/get page "original-name"))))))
+                                   (common-util/safe-page-name-sanity-lc (gobj/get page "original-name"))))))
                    (doseq [page pages-to-add]
                      (.add indice (bean/->js (original-page-name->index
                                               (or (:block/original-name page)
@@ -434,7 +435,7 @@
               result (->> (.search indice q (clj->js {:limit limit}))
                           (bean/->clj))]
           (->> result
-               (util/distinct-by (fn [i] (string/trim (get-in i [:item :name]))))
+               (common-util/distinct-by (fn [i] (string/trim (get-in i [:item :name]))))
                (map
                 (fn [{:keys [item]}]
                   (:original-name item)))

+ 8 - 45
src/main/frontend/worker/util.cljs

@@ -3,9 +3,7 @@
   (:refer-clojure :exclude [format])
   (:require [clojure.string :as string]
             ["remove-accents" :as removeAccents]
-            [medley.core :as medley]
             [logseq.common.util :as common-util]
-            [goog.string :as gstring]
             [clojure.core.async :as async]
             [cljs.core.async.impl.channels :refer [ManyToManyChannel]]
             [cljs-time.coerce :as tc]
@@ -13,43 +11,13 @@
             [cljs-bean.core :as bean]))
 
 (defn search-normalize
-     "Normalize string for searching (loose)"
-     [s remove-accents?]
-     (when s
-       (let [normalize-str (.normalize (string/lower-case s) "NFKC")]
-         (if remove-accents?
-           (removeAccents normalize-str)
-           normalize-str))))
-
-(def safe-re-find common-util/safe-re-find)
-
-(def uuid-string? common-util/uuid-string?)
-
-(def page-name-sanity-lc
-  "Delegate to common-util to loosely couple app usages to graph-parser"
-  common-util/page-name-sanity-lc)
-
-(defn safe-page-name-sanity-lc
-  [s]
-  (if (string? s)
-    (page-name-sanity-lc s) s))
-
-(defn distinct-by
-  [f col]
-  (medley/distinct-by f (seq col)))
-
-(defn format
-  [fmt & args]
-  (apply gstring/format fmt args))
-
-(defn remove-first [pred coll]
-  ((fn inner [coll]
-     (lazy-seq
-      (when-let [[x & xs] (seq coll)]
-        (if (pred x)
-          xs
-          (cons x (inner xs))))))
-   coll))
+  "Normalize string for searching (loose)"
+  [s remove-accents?]
+  (when s
+    (let [normalize-str (.normalize (string/lower-case s) "NFKC")]
+      (if remove-accents?
+        (removeAccents normalize-str)
+        normalize-str))))
 
 (defn drain-chan
   "drop all stuffs in CH, and return all of them"
@@ -98,7 +66,7 @@
                                filter-v)]
                (if filter-v*
                  (recur timeout-ch (cond->> (conj coll e)
-                                     distinct-key-fn (distinct-by distinct-key-fn)
+                                     distinct-key-fn (common-util/distinct-by distinct-key-fn)
                                      true vec))
                  (recur timeout-ch coll)))
 
@@ -116,8 +84,3 @@
 (defn post-message
   [type data]
   (.postMessage js/self (bean/->js [type data])))
-
-(defn concat-without-nil
-  [& cols]
-  (->> (apply concat cols)
-       (remove nil?)))

この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません