Browse Source

Save global config edits

Also alert user if write fails
Gabriel Horner 3 năm trước cách đây
mục cha
commit
095a4ef1cd

+ 1 - 2
src/main/frontend/components/settings.cljs

@@ -546,8 +546,7 @@
      (version-row t version)
      (language-row t preferred-language)
      (theme-modes-row t switch-theme system-theme? dark?)
-     ;; TODO: Disable for mobile
-     (edit-global-config-edn)
+     (when (util/electron?) (edit-global-config-edn))
      (when current-repo (edit-config-edn))
      (when current-repo (edit-custom-css))
      (when current-repo (edit-export-css))

+ 11 - 0
src/main/frontend/handler/common.cljs

@@ -50,10 +50,15 @@
                 (hidden? path patterns))) files)
     files))
 
+;; TODO: Rename to get-repo-config-content
 (defn get-config
   [repo-url]
   (db/get-file repo-url (config/get-config-path)))
 
+(defn get-global-config-content
+  [repo-url]
+  (db/get-file repo-url (config/get-global-config-path)))
+
 (defn safe-read-string
   [content error-message-or-handler]
   (try
@@ -79,6 +84,12 @@
       (state/set-config! repo-url config)
       config)))
 
+(defn reset-global-config!
+  [content]
+  (let [config (reader/read-string content)]
+    (state/set-global-config! config)
+    config))
+
 (defn read-metadata!
   [content]
   (try

+ 31 - 11
src/main/frontend/handler/file.cljs

@@ -17,7 +17,8 @@
             [promesa.core :as p]
             [frontend.mobile.util :as mobile]
             [logseq.graph-parser.config :as gp-config]
-            [logseq.graph-parser :as graph-parser]))
+            [logseq.graph-parser :as graph-parser]
+            ["path" :as path]))
 
 ;; TODO: extract all git ops using a channel
 
@@ -57,10 +58,15 @@
    (restore-config! repo-url nil))
   ([repo-url config-content]
    (let [config-content (if config-content config-content
-                            (common-handler/get-config repo-url))]
+                          (common-handler/get-config repo-url))]
      (when config-content
        (common-handler/reset-config! repo-url config-content)))))
 
+(defn set-global-config-state!
+  [repo-url]
+  (let [config-content (common-handler/get-global-config-content repo-url)]
+    (common-handler/reset-global-config! config-content)))
+
 (defn load-files-contents!
   [repo-url files ok-handler]
   (let [images (only-image-formats files)
@@ -173,29 +179,43 @@
   (let [original-content (db/get-file repo path)
         write-file! (if from-disk?
                       #(p/resolved nil)
-                      #(fs/write-file! repo (config/get-repo-dir repo) path content
-                                       (assoc (when original-content {:old-content original-content})
-                                              :skip-compare? skip-compare?)))
+                      #(let [path-dir (if (= (path/dirname path) (config/get-global-config-dir))
+                                        (config/get-global-config-dir)
+                                        (config/get-repo-dir repo))]
+                         (fs/write-file! repo path-dir path content
+                                        (assoc (when original-content {:old-content original-content})
+                                               :skip-compare? skip-compare?))))
         opts {:new-graph? new-graph?
               :from-disk? from-disk?}]
     (if reset?
       (do
         (when-let [page-id (db/get-file-page-id path)]
           (db/transact! repo
-            [[:db/retract page-id :block/alias]
-             [:db/retract page-id :block/tags]]
-            opts))
+                        [[:db/retract page-id :block/alias]
+                         [:db/retract page-id :block/tags]]
+                        opts))
         (reset-file! repo path content (merge opts
                                               (when (some? verbose) {:verbose verbose}))))
       (db/set-file-content! repo path content opts))
     (util/p-handle (write-file!)
                    (fn [_]
-                     (when (= path (config/get-config-path repo))
-                       (restore-config! repo))
-                     (when (= path (config/get-custom-css-path repo))
+                     (cond
+                       (= path (config/get-config-path repo))
+                       (restore-config! repo)
+
+                       (= path (config/get-global-config-path))
+                       (set-global-config-state! repo)
+
+                       (= path (config/get-custom-css-path repo))
                        (ui-handler/add-style-if-exists!))
+
                      (when re-render-root? (ui-handler/re-render-root!)))
                    (fn [error]
+                     (when (= path (config/get-global-config-path))
+                       (state/pub-event! [:notification/show
+                                         {:content (str "Failed to write to file " path)
+                                          :status :error}]))
+
                      (println "Write file failed, path: " path ", content: " content)
                      (log/error :write/failed error)))))
 

+ 3 - 7
src/main/frontend/handler/web/nfs.cljs

@@ -338,13 +338,9 @@
                                     (when nfs?
                                       (swap! path-handles assoc path handle))))
                     global-dir (config/get-global-config-dir)
-                    global-dir-exists? (fs/dir-exists? global-dir)
-                    ;; TODO: Handle nfs?
-                    global-files-result (if global-dir-exists?
-                                          (fs/get-files global-dir
-                                                        (fn [path handle]
-                                                          (when nfs?
-                                                            (swap! path-handles assoc path handle))))
+                    global-config-supported? (and electron? (fs/dir-exists? global-dir))
+                    global-files-result (if global-config-supported?
+                                          (fs/get-files global-dir (constantly nil))
                                           [])
                     new-local-files (-> (->db-files mobile-native? electron? dir-name local-files-result)
                                         (remove-ignore-files dir-name nfs?))

+ 5 - 0
src/main/frontend/state.cljs

@@ -100,6 +100,7 @@
 
      :config                                {}
      :config/root-dir                       nil
+     :global-config                         {}
      :block/component-editing-mode?         false
      :editor/hidden-editors                 #{}             ;; page names
      :editor/draw-mode?                     false
@@ -1232,6 +1233,10 @@
   [repo-url value]
   (set-state! [:config repo-url] value))
 
+(defn set-global-config!
+  [value]
+  (set-state! [:global-config] value))
+
 (defn get-wide-mode?
   []
   (:ui/wide-mode? @state))