Save global config edits

Also alert user if write fails
This commit is contained in:
Gabriel Horner
2022-09-01 11:45:41 -04:00
committed by Andelf
parent 9c0bb02dcd
commit 095a4ef1cd
5 changed files with 51 additions and 20 deletions

View File

@@ -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))

View File

@@ -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

View File

@@ -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)))))

View File

@@ -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?))

View File

@@ -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))