mirror of
https://github.com/logseq/logseq.git
synced 2026-04-28 16:15:21 +00:00
feat(chrome native fs): add sync button
This commit is contained in:
@@ -10,10 +10,41 @@
|
||||
[frontend.idb :as idb]
|
||||
[frontend.state :as state]
|
||||
[clojure.string :as string]
|
||||
[clojure.set :as set]
|
||||
[frontend.ui :as ui]
|
||||
[frontend.fs :as fs]
|
||||
[frontend.db :as db]
|
||||
[frontend.config :as config]))
|
||||
|
||||
(defn- ->db-files
|
||||
[dir-name result]
|
||||
(let [result (flatten (bean/->clj result))]
|
||||
(map (fn [file]
|
||||
(let [handle (gobj/get file "handle")
|
||||
get-attr #(gobj/get file %)
|
||||
path (-> (get-attr "webkitRelativePath")
|
||||
(string/replace-first (str dir-name "/") ""))]
|
||||
{:file/path path
|
||||
:file/last-modified-at (get-attr "lastModified")
|
||||
:file/size (get-attr "size")
|
||||
:file/type (get-attr "type")
|
||||
:file/file file
|
||||
:file/handle handle})) result)))
|
||||
|
||||
(defn- filter-markup-and-built-in-files
|
||||
[files]
|
||||
(filter (fn [file]
|
||||
(contains? (set/union config/markup-formats #{:css :edn})
|
||||
(keyword (util/get-file-ext (:file/path file)))))
|
||||
files))
|
||||
|
||||
(defn- set-files!
|
||||
[handles]
|
||||
(doseq [[path handle] handles]
|
||||
(let [handle-path (str config/local-handle-prefix path)]
|
||||
(idb/set-item! handle-path handle)
|
||||
(fs/add-nfs-file-handle! handle-path handle))))
|
||||
|
||||
(defn ls-dir-files
|
||||
[]
|
||||
(let [path-handles (atom {})]
|
||||
@@ -27,28 +58,10 @@
|
||||
root-handle-path (str config/local-handle-prefix dir-name)
|
||||
_ (idb/set-item! root-handle-path root-handle)
|
||||
_ (fs/add-nfs-file-handle! root-handle-path root-handle)
|
||||
_ (doseq [[path handle] @path-handles]
|
||||
(let [handle-path (str config/local-handle-prefix path)]
|
||||
(idb/set-item! handle-path handle)
|
||||
(fs/add-nfs-file-handle! handle-path handle)))
|
||||
_ (set-files! @path-handles)
|
||||
result (nth result 1)
|
||||
result (flatten (bean/->clj result))
|
||||
files (doall
|
||||
(map (fn [file]
|
||||
(let [handle (gobj/get file "handle")
|
||||
get-attr #(gobj/get file %)
|
||||
path (-> (get-attr "webkitRelativePath")
|
||||
(string/replace-first (str dir-name "/") ""))]
|
||||
{:file/path path
|
||||
:file/last-modified-at (get-attr "lastModified")
|
||||
:file/size (get-attr "size")
|
||||
:file/type (get-attr "type")
|
||||
:file/file file
|
||||
:file/handle handle})) result))
|
||||
markup-files (filter (fn [file]
|
||||
(contains? config/markup-formats
|
||||
(keyword (util/get-file-ext (:file/path file)))))
|
||||
files)]
|
||||
files (->db-files dir-name result)
|
||||
markup-files (filter-markup-and-built-in-files files)]
|
||||
(-> (p/all (map (fn [file]
|
||||
(p/let [content (.text (:file/file file))]
|
||||
(assoc file :file/content content))) markup-files))
|
||||
@@ -101,3 +114,62 @@
|
||||
(defn trigger-check! []
|
||||
(when-let [repo (get-local-repo)]
|
||||
(state/set-modal! (ask-permission repo))))
|
||||
|
||||
(defn- compute-diffs
|
||||
[old-files new-files]
|
||||
(let [ks [:file/path :file/last-modified-at]
|
||||
->set (fn [files ks]
|
||||
(when (seq files)
|
||||
(->> files
|
||||
(map #(select-keys % ks))
|
||||
set)))
|
||||
old-files (->set old-files ks)
|
||||
new-files (->set new-files ks)
|
||||
diff (fn [col1 col2]
|
||||
(->> (set/difference col1 col2)
|
||||
(map :file/path)
|
||||
set))
|
||||
new-diff (diff new-files old-files)
|
||||
old-diff (diff old-files new-files)]
|
||||
{:added (set/difference new-diff old-diff)
|
||||
:modified (let [both-exist (set/union new-diff old-diff)]
|
||||
(when (seq both-exist)
|
||||
(->>
|
||||
(filter (fn [{:file/keys [path last-modified-at]}]
|
||||
(when-let [old-file (some #(when (= (:file/path %) path) %) old-files)]
|
||||
;; TODO: the `last-modified-at` attribute in the db is always after
|
||||
;; the file system one because we transact to db first and write to the
|
||||
;; file system then.
|
||||
;; It doesn't mean this is a bug, but it could impact the performance.
|
||||
(> last-modified-at (:file/last-modified-at old-file))))
|
||||
new-files)
|
||||
(map :file/path))))
|
||||
:deleted (set/difference old-diff new-diff)}))
|
||||
|
||||
(defn- reload-dir!
|
||||
[repo]
|
||||
(when (and repo (config/local-db? repo))
|
||||
(let [old-files (db/get-files-path-size-modified-at repo)
|
||||
dir-name (config/get-local-dir repo)
|
||||
handle-path (str config/local-handle-prefix dir-name)
|
||||
path-handles (atom {})]
|
||||
(p/let [handle (idb/get-item handle-path)
|
||||
files-result (utils/getFiles handle true
|
||||
(fn [path handle]
|
||||
(swap! path-handles assoc path handle)))
|
||||
_ (set-files! @path-handles)
|
||||
new-files (->db-files dir-name files-result)
|
||||
diffs (compute-diffs old-files new-files)]
|
||||
;; (-> (p/all (map (fn [file]
|
||||
;; (p/let [content (.text (:file/file file))]
|
||||
;; (assoc file :file/content content))) files-result))
|
||||
;; (p/then (fn [result]
|
||||
;; (let [files (map #(select-keys % [:file/path :file/content]) result)])))
|
||||
;; (p/catch (fn [error]
|
||||
;; (println "Load files content error: ")
|
||||
;; (js/console.dir error))))
|
||||
(prn {:diffs diffs})))))
|
||||
|
||||
(defn- refresh!
|
||||
[repo]
|
||||
(reload-dir! repo))
|
||||
|
||||
Reference in New Issue
Block a user