Ver Fonte

Encapsulate global config state and add global watcher

Correct merge with local state and testing is still TODO
Gabriel Horner há 3 anos atrás
pai
commit
51ed2f94f1

+ 17 - 14
src/electron/electron/fs_watcher.cljs

@@ -21,7 +21,7 @@
                         (send file-watcher-chan
                               (bean/->js {:type type :payload payload})))
                     true))
-        wins (window/get-graph-all-windows dir)]
+        wins (window/get-graph-all-windows (or (:current-repo-dir payload) dir))]
     (if (contains? #{"unlinkDir" "addDir"} type)
       ;; notify every windows
       (doseq [win wins] (send-fn win))
@@ -33,7 +33,7 @@
                                (str "unhandled file event will cause uncatched file modifications!. target:" dir)))))))
 
 (defn- publish-file-event!
-  [dir path event]
+  [dir path event options]
   (let [dir-path? (= dir path)
         content (when (and (not= event "unlink")
                            (not dir-path?)
@@ -42,14 +42,17 @@
         stat (when (and (not= event "unlink")
                         (not dir-path?))
                (fs/statSync path))]
-    (send-file-watcher! dir event {:dir (utils/fix-win-path! dir)
-                                   :path (utils/fix-win-path! path)
-                                   :content content
-                                   :stat stat})))
+    (send-file-watcher! dir event (merge options
+                                         {:dir (utils/fix-win-path! dir)
+                                          :path (utils/fix-win-path! path)
+                                          :content content
+                                          :stat stat}))))
 
 (defn watch-dir!
-  "Watch a directory if no such file watcher exists"
-  [dir]
+  "Watch a directory if no such file watcher exists. Has the following options:
+* :current-repo-dir - Provides current repo-dir for global directories. Needed as watch events need to take place in a repo context in order for window and db to function correctly"
+  [dir options]
+  (prn :WATCH dir options)
   (when-not (get @*file-watcher dir)
     (if (fs/existsSync dir)
       (let [watcher-opts (clj->js
@@ -70,22 +73,22 @@
         (.on dir-watcher "unlinkDir"
              (fn [path]
                (when (= dir path)
-                 (publish-file-event! dir dir "unlinkDir"))))
+                 (publish-file-event! dir dir "unlinkDir" options))))
         (.on dir-watcher "addDir"
              (fn [path]
                (when (= dir path)
-                 (publish-file-event! dir dir "addDir"))))
+                 (publish-file-event! dir dir "addDir" options))))
         (.on dir-watcher "add"
              (fn [path]
-               (publish-file-event! dir path "add")))
+               (publish-file-event! dir path "add" options)))
         (.on dir-watcher "change"
              (fn [path]
-               (publish-file-event! dir path "change")))
+               (publish-file-event! dir path "change" options)))
         (.on dir-watcher "unlink"
              ;; delay 500ms for syncing disks
              (fn [path]
                (js/setTimeout #(when (not (fs/existsSync path))
-                                 (publish-file-event! dir path "unlink"))
+                                 (publish-file-event! dir path "unlink" options))
                               500)))
         (.on dir-watcher "error"
              (fn [path]
@@ -99,7 +102,7 @@
         true)
       ;; retry if the `dir` not exists, which is useful when a graph's folder is
       ;; back after refreshing the window
-      (js/setTimeout #(watch-dir! dir) 5000))))
+      (js/setTimeout #(watch-dir! dir {}) 5000))))
 
 (defn close-watcher!
   "If no `dir` provided, close all watchers;

+ 2 - 2
src/electron/electron/handler.cljs

@@ -470,14 +470,14 @@
         windows (win/get-graph-all-windows dir)]
     (> (count windows) 1)))
 
-(defmethod handle :addDirWatcher [^js _window [_ dir]]
+(defmethod handle :addDirWatcher [^js _window [_ dir options]]
   ;; receive dir path (not repo / graph) from frontend
   ;; Windows on same dir share the same watcher
   ;; Only close file watcher when:
   ;;    1. there is no one window on the same dir
   ;;    2. reset file watcher to resend `add` event on window refreshing
   (when dir
-    (watcher/watch-dir! dir)))
+    (watcher/watch-dir! dir options)))
 
 (defmethod handle :unwatchDir [^js _window [_ dir]]
   (when dir

+ 1 - 1
src/main/frontend/components/page.cljs

@@ -634,7 +634,7 @@
                    (state/set-search-mode! :global)
                    state)}
   [state]
-  (let [settings (state/sub-graph-config-settings)
+  (let [settings (state/graph-settings)
         theme (state/sub :ui/theme)
         graph (graph-handler/build-global-graph theme settings)
         search-graph-filters (state/sub :search/graph-filters)

+ 1 - 1
src/main/frontend/components/page_menu.cljs

@@ -67,7 +67,7 @@
           contents? (= page-name "contents")
           properties (:block/properties page)
           public? (true? (:public properties))
-          favorites (:favorites (state/sub-graph-config))
+          favorites (:favorites (state/sub-config))
           favorited? (contains? (set (map util/page-name-sanity-lc favorites))
                                 page-name)
           developer-mode? (state/sub [:ui/developer-mode?])

+ 1 - 1
src/main/frontend/components/sidebar.cljs

@@ -135,7 +135,7 @@
       (rfe/push-state :page {:name "Favorites"})
       (util/stop e))}
 
-   (let [favorites (->> (:favorites (state/sub-graph-config))
+   (let [favorites (->> (:favorites (state/sub-config))
                         (remove string/blank?)
                         (filter string?))]
      (when (seq favorites)

+ 1 - 1
src/main/frontend/config.cljs

@@ -434,4 +434,4 @@
 
 (defn get-block-hidden-properties
   []
-  (get-in @state/state [:config (state/get-current-repo) :block-hidden-properties]))
+  (:block-hidden-properties (state/get-config)))

+ 1 - 1
src/main/frontend/extensions/zotero/setting.cljs

@@ -19,7 +19,7 @@
 
 (defn sub-zotero-config
   []
-  (:zotero/settings-v2 (get (state/sub-config) (state/get-current-repo))))
+  (:zotero/settings-v2 (state/sub-config)))
 
 (defn all-profiles []
   (let [profiles (-> (sub-zotero-config) keys set)

+ 2 - 2
src/main/frontend/fs.cljs

@@ -172,8 +172,8 @@
         result))))
 
 (defn watch-dir!
-  [dir]
-  (protocol/watch-dir! (get-record) dir))
+  ([dir] (watch-dir! dir {}))
+  ([dir options] (protocol/watch-dir! (get-record) dir options)))
 
 (defn unwatch-dir!
   [dir]

+ 1 - 1
src/main/frontend/fs/bfs.cljs

@@ -34,7 +34,7 @@
     nil)
   (get-files [_this _path-or-handle _ok-handler]
     nil)
-  (watch-dir! [_this _dir]
+  (watch-dir! [_this _dir _options]
     nil)
   (unwatch-dir! [_this _dir]
     nil))

+ 1 - 1
src/main/frontend/fs/capacitor_fs.cljs

@@ -352,7 +352,7 @@
       (into [] (concat [{:path path}] files))))
   (get-files [_this path-or-handle _ok-handler]
     (readdir path-or-handle))
-  (watch-dir! [_this dir]
+  (watch-dir! [_this dir _options]
     (p/do!
      (.unwatch mobile-util/fs-watcher)
      (.watch mobile-util/fs-watcher (clj->js {:path dir}))))

+ 1 - 1
src/main/frontend/fs/nfs.cljs

@@ -239,5 +239,5 @@
     (utils/getFiles path-or-handle true ok-handler))
 
   ;; TODO:
-  (watch-dir! [_this _dir]
+  (watch-dir! [_this _dir _options]
     nil))

+ 2 - 2
src/main/frontend/fs/node.cljs

@@ -124,7 +124,7 @@
     (open-dir))
   (get-files [_this path-or-handle _ok-handler]
     (ipc/ipc "getFiles" path-or-handle))
-  (watch-dir! [_this dir]
-    (ipc/ipc "addDirWatcher" dir))
+  (watch-dir! [_this dir options]
+    (ipc/ipc "addDirWatcher" dir options))
   (unwatch-dir! [_this dir]
     (ipc/ipc "unwatchDir" dir)))

+ 1 - 1
src/main/frontend/fs/protocol.cljs

@@ -15,7 +15,7 @@
   (stat [this dir path])
   (open-dir [this ok-handler])
   (get-files [this path-or-handle ok-handler])
-  (watch-dir! [this dir])
+  (watch-dir! [this dir options])
   (unwatch-dir! [this dir])
   ;; Ensure the dir is watched, window agnostic.
   ;; Implementation should handle the actual watcher's construction / destruction.

+ 6 - 6
src/main/frontend/fs/watcher_handler.cljs

@@ -45,10 +45,10 @@
     (db/set-file-last-modified-at! repo path mtime)))
 
 (defn handle-changed!
-  [type {:keys [dir path content stat] :as payload}]
+  [type {:keys [dir path content stat current-repo-dir] :as payload}]
   (when dir
     (let [path (gp-util/path-normalize path)
-          repo (config/get-local-repo dir)
+          repo (config/get-local-repo (or current-repo-dir dir))
           pages-metadata-path (config/get-pages-metadata-path)
           {:keys [mtime]} stat
           db-content (or (db/get-file repo path) "")]
@@ -90,10 +90,10 @@
           (and (= "unlink" type)
                (db/file-exists? repo path))
           (p/let [dir-exists? (fs/file-exists? dir "")]
-            (when dir-exists?
-              (when-let [page-name (db/get-file-page path)]
-                (println "Delete page: " page-name ", file path: " path ".")
-                (page-handler/delete! page-name #() :delete-file? false))))
+                 (when dir-exists?
+                   (when-let [page-name (db/get-file-page path)]
+                     (println "Delete page: " page-name ", file path: " path ".")
+                     (page-handler/delete! page-name #() :delete-file? false))))
 
           (and (contains? #{"add" "change" "unlink"} type)
                (string/ends-with? path "logseq/custom.css"))

+ 1 - 0
src/main/frontend/handler.cljs

@@ -115,6 +115,7 @@
 
          (watch-for-date!)
          (file-handler/watch-for-current-graph-dir!)
+         (file-handler/watch-for-global-config-dir!)
          (state/pub-event! [:graph/restored (state/get-current-repo)])))
       (p/catch (fn [error]
                  (log/error :exception error)))))

+ 10 - 0
src/main/frontend/handler/file.cljs

@@ -176,6 +176,7 @@
                            re-render-root? false
                            from-disk? false
                            skip-compare? false}}]
+  (prn :ALTER repo path)
   (let [original-content (db/get-file repo path)
         write-file! (if from-disk?
                       #(p/resolved nil)
@@ -282,6 +283,15 @@
       (fs/unwatch-dir! dir)
       (fs/watch-dir! dir))))
 
+(defn watch-for-global-config-dir!
+  []
+  (let [dir (config/get-global-config-dir)
+        repo-dir (config/get-repo-dir (state/get-current-repo))]
+    (fs/unwatch-dir! dir)
+    ;; Even a global dir needs to know it's current graph in order to send
+    ;; change events to the right window and graph db
+    (fs/watch-dir! dir {:current-repo-dir repo-dir})))
+
 (defn create-metadata-file
   [repo-url encrypted?]
   (let [repo-dir (config/get-repo-dir repo-url)

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

@@ -246,10 +246,10 @@
         new-tag (if (re-find #"[\s\t]+" new-name)
                   (util/format "#[[%s]]" new-name)
                   (str "#" new-name))]
-    ;; hash tag parsing rules https://github.com/logseq/mldoc/blob/701243eaf9b4157348f235670718f6ad19ebe7f8/test/test_markdown.ml#L631 
+    ;; hash tag parsing rules https://github.com/logseq/mldoc/blob/701243eaf9b4157348f235670718f6ad19ebe7f8/test/test_markdown.ml#L631
     ;; Safari doesn't support look behind, don't use
     ;; TODO: parse via mldoc
-    (string/replace content 
+    (string/replace content
                     (re-pattern (str "(?i)(^|\\s)(" (util/escape-regex-chars old-tag) ")(?=[,\\.]*($|\\s))"))
                     ;;    case_insense^    ^lhs   ^_grp2                       look_ahead^         ^_grp3
                     (fn [[_match lhs _grp2 _grp3]]
@@ -327,7 +327,7 @@
 (defn toggle-favorite! []
   ;; NOTE: in journals or settings, current-page is nil
   (when-let [page-name (state/get-current-page)]
-   (let [favorites  (:favorites (state/sub-graph-config))
+   (let [favorites  (:favorites (state/sub-config))
          favorited? (contains? (set (map string/lower-case favorites))
                                (string/lower-case page-name))]
     (if favorited?

+ 31 - 51
src/main/frontend/state.cljs

@@ -100,7 +100,6 @@
 
      :config                                {}
      :config/root-dir                       nil
-     :global-config                         {}
      :block/component-editing-mode?         false
      :editor/hidden-editors                 #{}             ;; page names
      :editor/draw-mode?                     false
@@ -334,6 +333,8 @@
    (get-config (get-current-repo)))
   ([repo-url]
    (merge default-config
+          ;; TODO: Confirm merging works for all cases
+          (get-in @state [:config ::global-config])
           (get-in @state [:config repo-url]))))
 
 (defn get-arweave-gateway
@@ -350,8 +351,11 @@
     (:macros (get-config))))
 
 (defn sub-config
-  []
-  (sub :config))
+  "Sub equivalent to get-config"
+  ([] (sub-config (get-current-repo)))
+  ([repo]
+   (let [config (sub :config)]
+     (merge (get config ::global-config) (get config repo)))))
 
 (defn get-custom-css-link
   []
@@ -375,36 +379,27 @@
 
 (defn enable-grammarly?
   []
-  (true? (:feature/enable-grammarly?
-           (get (sub-config) (get-current-repo)))))
-
-;; (defn store-block-id-in-file?
-;;   []
-;;   (true? (:block/store-id-in-file? (get-config))))
+  (true? (:feature/enable-grammarly? (sub-config))))
 
 (defn scheduled-deadlines-disabled?
   []
-  (true? (:feature/disable-scheduled-and-deadline-query?
-           (get (sub-config) (get-current-repo)))))
+  (true? (:feature/disable-scheduled-and-deadline-query? (sub-config))))
 
 (defn enable-timetracking?
   []
-  (not (false? (:feature/enable-timetracking?
-                 (get (sub-config) (get-current-repo))))))
+  (not (false? (:feature/enable-timetracking? (sub-config)))))
 
 (defn enable-journals?
   ([]
    (enable-journals? (get-current-repo)))
   ([repo]
-   (not (false? (:feature/enable-journals?
-                 (get (sub-config) repo))))))
+   (not (false? (:feature/enable-journals? (sub-config repo))))))
 
 (defn enable-flashcards?
   ([]
    (enable-flashcards? (get-current-repo)))
   ([repo]
-   (not (false? (:feature/enable-flashcards?
-                 (get (sub-config) repo))))))
+   (not (false? (:feature/enable-flashcards? (sub-config repo))))))
 
 (defn user-groups
   []
@@ -416,32 +411,24 @@
 
 (defn export-heading-to-list?
   []
-  (not (false? (:export/heading-to-list?
-                 (get (sub-config) (get-current-repo))))))
+  (not (false? (:export/heading-to-list? (sub-config)))))
 
 (defn enable-git-auto-push?
   [repo]
-  (not (false? (:git-auto-push
-                 (get (sub-config) repo)))))
+  (not (false? (:git-auto-push (sub-config repo)))))
 
 (defn enable-block-timestamps?
   []
-  (true? (:feature/enable-block-timestamps?
-           (get (sub-config) (get-current-repo)))))
+  (true? (:feature/enable-block-timestamps? (sub-config))))
 
-(defn sub-graph-config
+(defn graph-settings
   []
-  (get (sub-config) (get-current-repo)))
-
-(defn sub-graph-config-settings
-  []
-  (:graph/settings (sub-graph-config)))
+  (:graph/settings (sub-config)))
 
 ;; Enable by default
 (defn show-brackets?
   []
-  (not (false? (:ui/show-brackets?
-                 (get (sub-config) (get-current-repo))))))
+  (not (false? (:ui/show-brackets? (sub-config)))))
 
 (defn get-default-home
   []
@@ -449,7 +436,7 @@
 
 (defn sub-default-home-page
   []
-  (get-in (sub-config) [(get-current-repo) :default-home :page] ""))
+  (get-in (sub-config) [:default-home :page] ""))
 
 (defn custom-home-page?
   []
@@ -896,7 +883,7 @@
 
 (defn block-content-max-length
   [repo]
-  (or (:block/content-max-length (get (sub-config) repo)) 5000))
+  (or (:block/content-max-length (sub-config repo)) 5000))
 
 (defn clear-edit!
   []
@@ -1055,7 +1042,7 @@
   (gp-config/get-date-formatter (get-config)))
 
 (defn shortcuts []
-  (get-in @state [:config (get-current-repo) :shortcuts]))
+  (:shortcuts (get-config)))
 
 (defn get-me
   []
@@ -1193,7 +1180,7 @@
 (defn doc-mode-enter-for-new-line?
   []
   (and (document-mode?)
-       (not (:shortcut/doc-mode-enter-for-new-block? (sub-graph-config)))))
+       (not (:shortcut/doc-mode-enter-for-new-block? (sub-config)))))
 
 (defn toggle-document-mode!
   []
@@ -1219,15 +1206,11 @@
   []
   (if (mobile?)
     false
-    (get (get (sub-config) (get-current-repo))
-         :ui/enable-tooltip?
-         true)))
+    (get (sub-config) :ui/enable-tooltip? true)))
 
 (defn show-command-doc?
   []
-  (get (get (sub-config) (get-current-repo))
-       :ui/show-command-doc?
-       true))
+  (get (sub-config) :ui/show-command-doc? true))
 
 (defn set-config!
   [repo-url value]
@@ -1235,7 +1218,8 @@
 
 (defn set-global-config!
   [value]
-  (set-state! [:global-config] value))
+  ;; Placed under :config so cursors can work seamlessly
+  (set-config! ::global-config value))
 
 (defn get-wide-mode?
   []
@@ -1433,11 +1417,9 @@
 
 (defn get-start-of-week
   []
-  (or
-    (when-let [repo (get-current-repo)]
-      (get-in @state [:config repo :start-of-week]))
-    (get-in @state [:me :settings :start-of-week])
-    6))
+  (or (:start-of-week (get-config))
+      (get-in @state [:me :settings :start-of-week])
+      6))
 
 (defn get-ref-open-blocks-level
   []
@@ -1507,8 +1489,7 @@
 
 (defn logical-outdenting?
   []
-  (:editor/logical-outdenting?
-    (get (sub-config) (get-current-repo))))
+  (:editor/logical-outdenting? (sub-config)))
 
 (defn get-editor-args
   []
@@ -1753,8 +1734,7 @@
 
 (defn enable-encryption?
   [repo]
-  (:feature/enable-encryption?
-   (get (sub-config) repo)))
+  (:feature/enable-encryption? (sub-config repo)))
 
 (defn set-mobile-app-state-change
   [is-active?]