|
|
@@ -19,8 +19,7 @@
|
|
|
[logseq.common.config :as common-config]
|
|
|
[logseq.common.util.block-ref :as block-ref]
|
|
|
[promesa.core :as p]
|
|
|
- [frontend.db.async :as db-async]
|
|
|
- [frontend.db.transact :as db-transact]))
|
|
|
+ [frontend.db.async :as db-async]))
|
|
|
|
|
|
;; all IPC paths must be normalized! (via common-util/path-normalize)
|
|
|
|
|
|
@@ -43,9 +42,12 @@
|
|
|
(defn- handle-add-and-change!
|
|
|
[repo path content db-content mtime backup?]
|
|
|
(let [config (state/get-config repo)
|
|
|
- path-hidden-patterns (:hidden config)]
|
|
|
- (when-not (and (seq path-hidden-patterns)
|
|
|
- (common-config/hidden? path path-hidden-patterns))
|
|
|
+ path-hidden-patterns (:hidden config)
|
|
|
+ db-last-modified-at (db/get-file-last-modified-at repo path)]
|
|
|
+ (when-not (or (and (seq path-hidden-patterns)
|
|
|
+ (common-config/hidden? path path-hidden-patterns))
|
|
|
+ ;; File not changed
|
|
|
+ (= db-last-modified-at mtime))
|
|
|
(p/let [;; save the previous content in a versioned bak file to avoid data overwritten.
|
|
|
_ (when backup?
|
|
|
(-> (when-let [repo-dir (config/get-local-dir repo)]
|
|
|
@@ -60,87 +62,77 @@
|
|
|
|
|
|
(defn handle-changed!
|
|
|
[type {:keys [dir path content stat global-dir] :as payload}]
|
|
|
- (let [repo (state/get-current-repo)
|
|
|
- ^js sqlite @state/*db-worker]
|
|
|
- (p/let [writes-finished? (when sqlite (.file-writes-finished? sqlite repo))]
|
|
|
- (when (and dir writes-finished? (db-transact/request-finished?))
|
|
|
- (let [;; Global directory events don't know their originating repo so we rely
|
|
|
+ (let [repo (state/get-current-repo)]
|
|
|
+ (when dir
|
|
|
+ (let [;; Global directory events don't know their originating repo so we rely
|
|
|
;; on the client to correctly identify it
|
|
|
- repo (cond
|
|
|
- global-dir (state/get-current-repo)
|
|
|
+ repo (cond
|
|
|
+ global-dir repo
|
|
|
;; FIXME(andelf): hack for demo graph, demo graph does not bind to local directory
|
|
|
- (string/starts-with? dir "memory://") "Logseq demo"
|
|
|
- :else (config/get-local-repo dir))
|
|
|
- repo-dir (config/get-local-dir repo)
|
|
|
- {:keys [mtime]} stat
|
|
|
- ext (keyword (path/file-ext path))]
|
|
|
- (when (contains? #{:org :md :markdown :css :js :edn :excalidraw :tldr} ext)
|
|
|
- (p/let [db-content (db-async/<get-file repo path)
|
|
|
- exists-in-db? (not (nil? db-content))
|
|
|
- db-content (or db-content "")]
|
|
|
- (when (or content (contains? #{"unlink" "unlinkDir" "addDir"} type))
|
|
|
- (cond
|
|
|
- (and (= "unlinkDir" type) dir)
|
|
|
- (state/pub-event! [:graph/dir-gone dir])
|
|
|
-
|
|
|
- (and (= "addDir" type) dir)
|
|
|
- (state/pub-event! [:graph/dir-back repo dir])
|
|
|
-
|
|
|
- (contains? (:file/unlinked-dirs @state/state) dir)
|
|
|
- nil
|
|
|
-
|
|
|
- (and (= "add" type)
|
|
|
- (not= (string/trim content) (string/trim db-content)))
|
|
|
- (let [backup? (not (string/blank? db-content))]
|
|
|
- (handle-add-and-change! repo path content db-content mtime backup?))
|
|
|
-
|
|
|
- (and (= "change" type)
|
|
|
- (= dir repo-dir)
|
|
|
- (not= (string/trim content) (string/trim db-content))
|
|
|
- (not (common-config/local-asset? path)))
|
|
|
- (when-not (and
|
|
|
- (string/includes? path (str "/" (config/get-journals-directory) "/"))
|
|
|
- (or
|
|
|
- (= (string/trim content)
|
|
|
- (string/trim (or (state/get-default-journal-template) "")))
|
|
|
- (= (string/trim content) "-")
|
|
|
- (= (string/trim content) "*")))
|
|
|
- (handle-add-and-change! repo path content db-content mtime (not global-dir))) ;; no backup for global dir
|
|
|
-
|
|
|
- (and (= "unlink" type)
|
|
|
- exists-in-db?)
|
|
|
- (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 #()))))
|
|
|
+ (string/starts-with? dir "memory://") "Logseq demo"
|
|
|
+ :else (config/get-local-repo dir))
|
|
|
+ repo-dir (config/get-local-dir repo)
|
|
|
+ {:keys [mtime]} stat
|
|
|
+ ext (keyword (path/file-ext path))]
|
|
|
+ (when (contains? #{:org :md :markdown :css :js :edn :excalidraw :tldr} ext)
|
|
|
+ (p/let [db-content (db-async/<get-file repo path)
|
|
|
+ exists-in-db? (not (nil? db-content))
|
|
|
+ db-content (or db-content "")]
|
|
|
+ (when (or content (contains? #{"unlink" "unlinkDir" "addDir"} type))
|
|
|
+ (cond
|
|
|
+ (and (= "unlinkDir" type) dir)
|
|
|
+ (state/pub-event! [:graph/dir-gone dir])
|
|
|
+
|
|
|
+ (and (= "addDir" type) dir)
|
|
|
+ (state/pub-event! [:graph/dir-back repo dir])
|
|
|
+
|
|
|
+ (contains? (:file/unlinked-dirs @state/state) dir)
|
|
|
+ nil
|
|
|
+
|
|
|
+ (and (= "add" type)
|
|
|
+ (not= (string/trim content) (string/trim db-content)))
|
|
|
+ (let [backup? (not (string/blank? db-content))]
|
|
|
+ (handle-add-and-change! repo path content db-content mtime backup?))
|
|
|
+
|
|
|
+ (and (= "change" type)
|
|
|
+ (= dir repo-dir)
|
|
|
+ (not (common-config/local-asset? path)))
|
|
|
+ (handle-add-and-change! repo path content db-content mtime (not global-dir)) ;; no backup for global dir
|
|
|
+
|
|
|
+ (and (= "unlink" type)
|
|
|
+ exists-in-db?)
|
|
|
+ (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 #()))))
|
|
|
|
|
|
;; global config handling
|
|
|
- (and (= "change" type)
|
|
|
- (= dir (global-config-handler/global-config-dir)))
|
|
|
- (when (= path "config.edn")
|
|
|
- (file-handler/alter-global-file
|
|
|
- (global-config-handler/global-config-path) content {:from-disk? true}))
|
|
|
+ (and (= "change" type)
|
|
|
+ (= dir (global-config-handler/global-config-dir)))
|
|
|
+ (when (= path "config.edn")
|
|
|
+ (file-handler/alter-global-file
|
|
|
+ (global-config-handler/global-config-path) content {:from-disk? true}))
|
|
|
|
|
|
- (and (= "change" type)
|
|
|
- (not exists-in-db?))
|
|
|
- (js/console.error "Can't get file in the db: " path)
|
|
|
+ (and (= "change" type)
|
|
|
+ (not exists-in-db?))
|
|
|
+ (js/console.error "Can't get file in the db: " path)
|
|
|
|
|
|
- (and (contains? #{"add" "change" "unlink"} type)
|
|
|
- (string/ends-with? path "logseq/custom.css"))
|
|
|
- (do
|
|
|
- (println "reloading custom.css")
|
|
|
- (ui-handler/add-style-if-exists!))
|
|
|
+ (and (contains? #{"add" "change" "unlink"} type)
|
|
|
+ (string/ends-with? path "logseq/custom.css"))
|
|
|
+ (do
|
|
|
+ (println "reloading custom.css")
|
|
|
+ (ui-handler/add-style-if-exists!))
|
|
|
|
|
|
- (contains? #{"add" "change" "unlink"} type)
|
|
|
- nil
|
|
|
+ (contains? #{"add" "change" "unlink"} type)
|
|
|
+ nil
|
|
|
|
|
|
- :else
|
|
|
- (log/error :fs/watcher-no-handler {:type type
|
|
|
- :payload payload})))))
|
|
|
+ :else
|
|
|
+ (log/error :fs/watcher-no-handler {:type type
|
|
|
+ :payload payload})))))
|
|
|
|
|
|
;; return nil, otherwise the entire db will be transferred by ipc
|
|
|
- nil)))))
|
|
|
+ nil))))
|
|
|
|
|
|
(defn load-graph-files!
|
|
|
"This fn replaces the former initial fs watcher"
|