fix: ask the permission when refreshing local graph

pull/724/head
Tienson Qin 2020-12-06 11:06:27 +08:00
parent 0c4407d191
commit 45b318e2f8
6 changed files with 46 additions and 17 deletions

View File

@ -9,6 +9,19 @@
(-> ((gobj/get jsdiff "diffLines") s1 s2)
bean/->clj))
(defn diff-words
[s1 s2]
(-> ((gobj/get jsdiff "diffWords") s1 s2)
bean/->clj))
(defn removed?
[s1 s2]
(when (and s1 s2)
(let [diff-result (diff-words s1 s2)]
(->> diff-result
(some :removed)
(boolean)))))
;; (find-position "** hello _w_" "hello w")
(defn find-position
[markup text]

View File

@ -5,6 +5,7 @@
[frontend.idb :as idb]
[promesa.core :as p]
[goog.object :as gobj]
[frontend.diff :as diff]
["/frontend/utils" :as utils]))
;; We need to cache the file handles in the memory so that
@ -55,7 +56,7 @@
(add-nfs-file-handle! handle-path handle)
(println "Stored handle: " (str root-handle "/" new-dir)))
(p/catch (fn [error]
(println "mkdir error: " error)
(println "mkdir error: " error ", dir: " dir)
(js/console.error error)))))))
(and dir js/window.pfs)
@ -140,8 +141,11 @@
(p/let [local-file (.getFile file-handle)
local-content (.text local-file)]
(if (and local-content old-content
(= (string/trim local-content)
(string/trim old-content))) ; to prevent overwritten
;; To prevent data loss, it's not enough to just compare using `=`.
;; Also, we need to benchmark the performance of `diff/diff-words `
(not (diff/removed?
(string/trim local-content)
(string/trim old-content))))
(utils/writeFile file-handle content)
(js/alert (str "The file has been modified in your local disk! File path: " path
", save your changes and click the refresh button to reload it."))))

View File

@ -104,6 +104,8 @@
:repos repos}))))))
(store-schema!))
(nfs/ask-permission-if-local?)
(page-handler/init-commands!)
(if (seq (:repos me))
;; FIXME: handle error

View File

@ -5,6 +5,7 @@
[frontend.handler.git :as git-handler]
[frontend.handler.ui :as ui-handler]
[frontend.handler.repo :as repo-handler]
[frontend.handler.file :as file-handler]
[frontend.handler.notification :as notification]
[frontend.handler.draw :as draw]
[frontend.handler.expand :as expand]
@ -614,6 +615,7 @@
(block/parse-block (assoc block :block/content new-value) format))
parse-result)
after-blocks (rebuild-after-blocks repo file (:end-pos meta) end-pos)
files [[file-path new-content]]
transact-fn (fn []
(repo-handler/transact-react-and-alter-file!
repo
@ -624,7 +626,7 @@
after-blocks)
{:key :block/insert
:data (map (fn [block] (assoc block :block/page page)) blocks)}
[[file-path new-content]])
files)
(state/set-editor-op! nil))]
;; Replace with batch transactions

View File

@ -239,9 +239,9 @@
(when (seq pages)
(let [children-tx (mapcat #(db/rebuild-page-blocks-children repo %) pages)]
(when (seq children-tx)
(db/transact! repo children-tx)))))
(when (seq files)
(file-handler/alter-files repo files)))
(db/transact! repo children-tx))))
(when (seq files)
(file-handler/alter-files repo files))))
(declare push)

View File

@ -119,13 +119,22 @@
(when handle
(utils/verifyPermission handle true))))
(defn trigger-check! [cb]
(let [repo (state/get-current-repo)
nfs-repo? (config/local-db? repo)]
(if nfs-repo?
(p/let [_ (check-directory-permission! repo)]
(cb))
(cb))))
(defn ask-permission
[repo]
(fn [close-fn]
[:div
[:p.text-gray-700
"Grant native filesystem permission for directory: "
[:b (config/get-local-dir repo)]]
(ui/button
"Grant"
:on-click (fn []
(check-directory-permission! repo)
(close-fn)))]))
(defn ask-permission-if-local? []
(when-let [repo (get-local-repo)]
(state/set-modal! (ask-permission repo))))
(defn- compute-diffs
[old-files new-files]
@ -205,7 +214,7 @@
(rename-f "modify" modified))]
(when (or (and (seq diffs) (seq modified-files))
(seq diffs) ; delete
)
)
(repo-handler/load-repo-to-db! repo
{:diffs diffs
:nfs-files modified-files})))))
@ -217,8 +226,7 @@
(defn- refresh!
[repo]
(when repo
(p/let [_ (check-directory-permission! repo)]
(reload-dir! repo))))
(reload-dir! repo)))
(defn supported?
[]