From 4410ae71475c04b2faf93c5a0aae722d7319e9c7 Mon Sep 17 00:00:00 2001 From: defclass Date: Fri, 18 Dec 2020 09:34:21 +0800 Subject: [PATCH] refactor(db): get-config reset-config! --- src/main/frontend/db.cljs | 4 ++-- src/main/frontend/db/model.cljs | 16 ---------------- src/main/frontend/handler/file.cljs | 7 ++++--- src/main/frontend/handler/repo.cljs | 5 +++-- src/main/frontend/handler/utils.cljs | 21 +++++++++++++++++++++ 5 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 src/main/frontend/handler/utils.cljs diff --git a/src/main/frontend/db.cljs b/src/main/frontend/db.cljs index 988ef6a67..7998e2828 100644 --- a/src/main/frontend/db.cljs +++ b/src/main/frontend/db.cljs @@ -41,7 +41,7 @@ get-all-templates get-block-and-children get-block-and-children-no-cache get-block-by-uuid get-block-children get-block-children-ids get-block-content get-block-file get-block-immediate-children get-block-page get-block-page-end-pos get-block-parent get-block-parents get-block-referenced-blocks get-block-refs-count - get-blocks-by-priority get-blocks-contents get-collapsed-blocks get-config get-custom-css + get-blocks-by-priority get-blocks-contents get-collapsed-blocks get-custom-css get-date-scheduled-or-deadlines get-db-type get-empty-pages get-file get-file-after-blocks get-file-after-blocks-meta get-file-blocks get-file-contents get-file-last-modified-at get-file-no-sub get-file-page get-file-page-id get-file-pages get-files get-files-blocks get-files-full get-files-that-referenced-page get-journals-length @@ -50,7 +50,7 @@ get-page-properties-content get-page-referenced-blocks get-page-referenced-pages get-page-unlinked-references get-pages get-pages-relation get-pages-that-mentioned-page get-public-pages get-tag-pages journal-page? local-native-fs? mark-repo-as-cloned! page-alias-set page-blocks-transform pull-block - reset-config! set-file-last-modified-at! transact-files-db! with-block-refs-count get-modified-pages] + set-file-last-modified-at! transact-files-db! with-block-refs-count get-modified-pages] [frontend.db.react get-current-marker get-current-page get-current-priority get-handler-keys set-file-content! set-key-value diff --git a/src/main/frontend/db/model.cljs b/src/main/frontend/db/model.cljs index e560202cf..853df14c6 100644 --- a/src/main/frontend/db/model.cljs +++ b/src/main/frontend/db/model.cljs @@ -997,22 +997,6 @@ repo-url) ffirst))) -(defn get-config - [repo-url] - (get-file repo-url (str config/app-name "/" config/config-file))) - -(defn reset-config! - [repo-url content] - (when-let [content (or content (get-config repo-url))] - (let [config (try - (reader/read-string content) - (catch js/Error e - (println "Parsing config file failed: ") - (js/console.dir e) - {}))] - (state/set-config! repo-url config) - config))) - (defn get-db-type [repo] (db-utils/get-key-value repo :db/type)) diff --git a/src/main/frontend/handler/file.cljs b/src/main/frontend/handler/file.cljs index e67031762..6add96d8d 100644 --- a/src/main/frontend/handler/file.cljs +++ b/src/main/frontend/handler/file.cljs @@ -24,7 +24,8 @@ [cljs-time.core :as t] [cljs-time.coerce :as tc] [frontend.utf8 :as utf8] - ["ignore" :as Ignore])) + ["ignore" :as Ignore] + [frontend.handler.utils :as h-utils])) (defn load-file [repo-url path] @@ -79,10 +80,10 @@ (restore-config! repo-url nil project-changed-check?)) ([repo-url config-content project-changed-check?] (let [config-content (if config-content config-content - (db/get-config repo-url))] + (h-utils/get-config repo-url))] (when config-content (let [old-project (:project (state/get-config)) - new-config (db/reset-config! repo-url config-content)] + new-config (h-utils/reset-config! repo-url config-content)] (when (and (not (config/local-db? repo-url)) project-changed-check?) (let [new-project (:project new-config) diff --git a/src/main/frontend/handler/repo.cljs b/src/main/frontend/handler/repo.cljs index 4d3e91c88..bc7073d08 100644 --- a/src/main/frontend/handler/repo.cljs +++ b/src/main/frontend/handler/repo.cljs @@ -25,7 +25,8 @@ [clojure.string :as string] [frontend.dicts :as dicts] [frontend.helper :as helper] - [frontend.spec :as spec])) + [frontend.spec :as spec] + [frontend.handler.utils :as h-utils])) ;; Project settings should be checked in two situations: ;; 1. User changes the config.edn directly in logseq.com (fn: alter-file) @@ -61,7 +62,7 @@ (db/get-file repo-url path)) content (or old-content default-content)] (file-handler/reset-file! repo-url path content) - (db/reset-config! repo-url content) + (h-utils/reset-config! repo-url content) (when-not (= content old-content) (git-handler/git-add repo-url path)))))))) diff --git a/src/main/frontend/handler/utils.cljs b/src/main/frontend/handler/utils.cljs new file mode 100644 index 000000000..119fb7b23 --- /dev/null +++ b/src/main/frontend/handler/utils.cljs @@ -0,0 +1,21 @@ +(ns frontend.handler.utils + (:require [frontend.state :as state] + [cljs.reader :as reader] + [frontend.config :as config] + [frontend.db :as db])) + +(defn get-config + [repo-url] + (db/get-file repo-url (str config/app-name "/" config/config-file))) + +(defn reset-config! + [repo-url content] + (when-let [content (or content (get-config repo-url))] + (let [config (try + (reader/read-string content) + (catch js/Error e + (println "Parsing config file failed: ") + (js/console.dir e) + {}))] + (state/set-config! repo-url config) + config))) \ No newline at end of file