diff --git a/src/main/frontend/components/editor.cljs b/src/main/frontend/components/editor.cljs index 5243ba6886..d7419013de 100644 --- a/src/main/frontend/components/editor.cljs +++ b/src/main/frontend/components/editor.cljs @@ -674,7 +674,7 @@ (page-handler/rename-when-alter-title-property! old-page-name path format content value) (file/alter-file (state/get-current-repo) path (string/trim value) {:re-render-root? true})))) - (when-not (contains? #{:insert :indent-outdent} (state/get-editor-op)) + (when-not (contains? #{:insert :indent-outdent :auto-save} (state/get-editor-op)) (editor-handler/save-block! (get-state state) value)))) state)} [state {:keys [on-hide dummy? node format block block-parent-id] diff --git a/src/main/frontend/db.cljs b/src/main/frontend/db.cljs index 142aec3b98..028c5e6369 100644 --- a/src/main/frontend/db.cljs +++ b/src/main/frontend/db.cljs @@ -1981,15 +1981,10 @@ (doall (for [{:keys [url]} repos] (let [repo url - db-name (datascript-files-db repo) db-conn (d/create-conn db-schema/files-db-schema)] (swap! conns assoc db-name db-conn) - (p/let [stored (-> (idb/get-item db-name) - (p/then (fn [result] - result)) - (p/catch (fn [error] - nil))) + (p/let [stored (idb/get-item db-name) _ (when stored (let [stored-db (string->db stored) attached-db (d/db-with stored-db [(me-tx stored-db me)])] diff --git a/src/main/frontend/handler.cljs b/src/main/frontend/handler.cljs index fe244c3e65..611f5be880 100644 --- a/src/main/frontend/handler.cljs +++ b/src/main/frontend/handler.cljs @@ -165,5 +165,4 @@ (restore-and-setup! me repos logged?))) (db/run-batch-txs!) (file-handler/run-writes-chan!) - ;; (editor-handler/periodically-save!) -)) + (editor-handler/periodically-save!))) diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 0990510618..ac37866944 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -1351,22 +1351,29 @@ [] (when-let [repo (state/get-current-repo)] (when (state/input-idle? repo) - (let [input-id (state/get-edit-input-id) - block (state/get-edit-block) - elem (and input-id (gdom/getElement input-id)) - db-block (db/entity [:block/uuid (:block/uuid block)]) - db-content (:block/content db-block) - db-content-without-heading (and db-content - (util/safe-subs db-content (:block/level db-block))) - value (and elem (gobj/get elem "value"))] - (when (and block value db-content-without-heading - (not= (string/trim db-content-without-heading) - (string/trim value))) - (let [cur-pos (util/get-input-pos elem)] - (save-block-aux! block value (:block/format block)) - ;; Restore the cursor after saving the block - (when (and elem cur-pos) - (util/set-caret-pos! elem cur-pos)))))))) + (state/set-editor-op! :auto-save) + (try + (let [input-id (state/get-edit-input-id) + block (state/get-edit-block) + db-block (when-let [block-id (:block/uuid block)] + (db/pull [:block/uuid block-id])) + elem (and input-id (gdom/getElement input-id)) + db-content (:block/content db-block) + db-content-without-heading (and db-content + (util/safe-subs db-content (:block/level db-block))) + value (and elem (gobj/get elem "value"))] + (when (and block value db-content-without-heading + (or + (not= (string/trim db-content-without-heading) + (string/trim value)))) + (let [cur-pos (util/get-input-pos elem)] + (save-block-aux! db-block value (:block/format db-block)) + ;; Restore the cursor after saving the block + (when (and elem cur-pos) + (util/set-caret-pos! elem cur-pos))))) + (catch js/Error error + (log/error :save-block-failed error))) + (state/set-editor-op! nil)))) (defn on-up-down [state e up?] diff --git a/src/main/frontend/handler/web/nfs.cljs b/src/main/frontend/handler/web/nfs.cljs index d02d08f3b4..552349ceeb 100644 --- a/src/main/frontend/handler/web/nfs.cljs +++ b/src/main/frontend/handler/web/nfs.cljs @@ -167,63 +167,64 @@ handle-path (str config/local-handle-prefix dir-name) path-handles (atom {})] (state/set-graph-syncing? true) - (p/let [handle (idb/get-item handle-path) - _ (when handle (utils/verifyPermission handle true)) - files-result (utils/getFiles handle true - (fn [path handle] - (swap! path-handles assoc path handle))) - new-files (-> (->db-files dir-name files-result) - remove-ignore-files) - _ (let [file-paths (set (map :file/path new-files))] - (swap! path-handles (fn [handles] - (->> handles - (filter (fn [[path _handle]] - (contains? file-paths - (string/replace-first path (str dir-name "/") "")))) - (into {}))))) - _ (set-files! @path-handles) - get-file-f (fn [path files] (some #(when (= (:file/path %) path) %) files)) - {:keys [added modified deleted] :as diffs} (compute-diffs old-files new-files) - ;; Use the same labels as isomorphic-git - rename-f (fn [typ col] (mapv (fn [file] {:type typ :path file}) col)) - _ (when (seq deleted) - (p/all (map (fn [path] - (let [handle-path (str handle-path path)] - (idb/remove-item! handle-path) - (fs/remove-nfs-file-handle! handle-path))) deleted))) - added-or-modified (set (concat added modified)) - _ (when (seq added-or-modified) - (p/all (map (fn [path] - (when-let [handle (get @path-handles path)] - (idb/set-item! (str handle-path path) handle))) added-or-modified)))] - (-> (p/all (map (fn [path] - (when-let [file (get-file-f path new-files)] - (p/let [content (.text (:file/file file))] - (assoc file :file/content content)))) added-or-modified)) - (p/then (fn [result] - (let [files (map #(dissoc % :file/file :file/handle) result) - non-modified? (fn [file] - (let [content (:file/content file) - old-content (:file/content (get-file-f (:file/path file) old-files))] - (= content old-content))) - non-modified-files (->> (filter non-modified? files) - (map :file/path)) - modified-files (remove non-modified? files) - modified (set/difference (set modified) (set non-modified-files)) - diffs (concat - (rename-f "remove" deleted) - (rename-f "add" added) - (rename-f "modify" modified))] - (when (or (and (seq diffs) (seq modified-files)) - (seq diffs) ; delete + (p/let [handle (idb/get-item handle-path)] + (when handle + (p/let [_ (when handle (utils/verifyPermission handle true)) + files-result (utils/getFiles handle true + (fn [path handle] + (swap! path-handles assoc path handle))) + new-files (-> (->db-files dir-name files-result) + remove-ignore-files) + _ (let [file-paths (set (map :file/path new-files))] + (swap! path-handles (fn [handles] + (->> handles + (filter (fn [[path _handle]] + (contains? file-paths + (string/replace-first path (str dir-name "/") "")))) + (into {}))))) + _ (set-files! @path-handles) + get-file-f (fn [path files] (some #(when (= (:file/path %) path) %) files)) + {:keys [added modified deleted] :as diffs} (compute-diffs old-files new-files) + ;; Use the same labels as isomorphic-git + rename-f (fn [typ col] (mapv (fn [file] {:type typ :path file}) col)) + _ (when (seq deleted) + (p/all (map (fn [path] + (let [handle-path (str handle-path path)] + (idb/remove-item! handle-path) + (fs/remove-nfs-file-handle! handle-path))) deleted))) + added-or-modified (set (concat added modified)) + _ (when (seq added-or-modified) + (p/all (map (fn [path] + (when-let [handle (get @path-handles path)] + (idb/set-item! (str handle-path path) handle))) added-or-modified)))] + (-> (p/all (map (fn [path] + (when-let [file (get-file-f path new-files)] + (p/let [content (.text (:file/file file))] + (assoc file :file/content content)))) added-or-modified)) + (p/then (fn [result] + (let [files (map #(dissoc % :file/file :file/handle) result) + non-modified? (fn [file] + (let [content (:file/content file) + old-content (:file/content (get-file-f (:file/path file) old-files))] + (= content old-content))) + non-modified-files (->> (filter non-modified? files) + (map :file/path)) + modified-files (remove non-modified? files) + modified (set/difference (set modified) (set non-modified-files)) + diffs (concat + (rename-f "remove" deleted) + (rename-f "add" added) + (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}))))) - (p/catch (fn [error] - (log/error :nfs/load-files-error error))) - (p/finally (fn [_] - (state/set-graph-syncing? false)))))))) + (repo-handler/load-repo-to-db! repo + {:diffs diffs + :nfs-files modified-files}))))) + (p/catch (fn [error] + (log/error :nfs/load-files-error error))) + (p/finally (fn [_] + (state/set-graph-syncing? false)))))))))) (defn- refresh! [repo] diff --git a/src/main/frontend/state.cljs b/src/main/frontend/state.cljs index 137540f79c..c6dbcd646f 100644 --- a/src/main/frontend/state.cljs +++ b/src/main/frontend/state.cljs @@ -797,16 +797,16 @@ (if (= (:repo project) current-repo) (conj acc (merge project new-kvs)) (conj acc project))) - [] - projects)] + [] + projects)] (set-state! [:me :projects] new-projects)))) (defn remove-current-project [] (when-let [current-repo (get-current-repo)] (update-state! [:me :projects] - (fn [projects] - (remove #(= (:repo %) current-repo) projects))))) + (fn [projects] + (remove #(= (:repo %) current-repo) projects))))) (defn set-indexedb-support! [value] @@ -939,7 +939,7 @@ (defn set-published-pages [pages] (when-let [repo (get-current-repo)] - (set-state! [:me :published-pages repo] pages))) + (set-state! [:me :published-pages repo] pages))) (defn reset-published-pages []