diff --git a/src/main/frontend/diff.cljs b/src/main/frontend/diff.cljs index c0e15ac32..8523e5350 100644 --- a/src/main/frontend/diff.cljs +++ b/src/main/frontend/diff.cljs @@ -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] diff --git a/src/main/frontend/fs.cljs b/src/main/frontend/fs.cljs index 5e1dd13a1..62ff9c339 100644 --- a/src/main/frontend/fs.cljs +++ b/src/main/frontend/fs.cljs @@ -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.")))) diff --git a/src/main/frontend/handler.cljs b/src/main/frontend/handler.cljs index 3d31b0b50..9fba71fc1 100644 --- a/src/main/frontend/handler.cljs +++ b/src/main/frontend/handler.cljs @@ -104,6 +104,8 @@ :repos repos})))))) (store-schema!)) + (nfs/ask-permission-if-local?) + (page-handler/init-commands!) (if (seq (:repos me)) ;; FIXME: handle error diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 277040e2b..fe949ab81 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -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 diff --git a/src/main/frontend/handler/repo.cljs b/src/main/frontend/handler/repo.cljs index 063a55123..fa42686d5 100644 --- a/src/main/frontend/handler/repo.cljs +++ b/src/main/frontend/handler/repo.cljs @@ -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) diff --git a/src/main/frontend/handler/web/nfs.cljs b/src/main/frontend/handler/web/nfs.cljs index 464ae2c51..520974986 100644 --- a/src/main/frontend/handler/web/nfs.cljs +++ b/src/main/frontend/handler/web/nfs.cljs @@ -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? []