From 77e63f6461fde17dfb0a6b68b9cbe9242cad10e3 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Mon, 9 Jan 2023 22:24:08 +0800 Subject: [PATCH] enhance: allows empty content only for delete-blocks and move-blocks (#8199) allows empty content only for delete-blocks and move-blocks --- .gitignore | 2 + src/main/frontend/modules/file/core.cljs | 18 +++--- src/main/frontend/modules/outliner/file.cljs | 63 ++++++++++--------- .../frontend/modules/outliner/pipeline.cljs | 2 +- 4 files changed, 48 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 9dfd0b7924..98ca090745 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,5 @@ android/app/src/main/assets/capacitor.config.json *.sublime-* /public/static +.yarn/ +.yarnrc.yml diff --git a/src/main/frontend/modules/file/core.cljs b/src/main/frontend/modules/file/core.cljs index c13f6fe72f..2e447334ee 100644 --- a/src/main/frontend/modules/file/core.cljs +++ b/src/main/frontend/modules/file/core.cljs @@ -136,7 +136,7 @@ (defn- remove-transit-ids [block] (dissoc block :db/id :block/file)) (defn save-tree-aux! - [page-block tree] + [page-block tree blocks-just-deleted?] (let [page-block (db/pull (:db/id page-block)) file-db-id (-> page-block :block/file :db/id) file-path (-> (db-utils/entity file-db-id) :file/path)] @@ -146,17 +146,21 @@ (up/ugly-pr-str {:blocks tree :pages (list (remove-transit-ids page-block))}) (string/triml)) - (tree->file-content tree {:init-level init-level})) - files [[file-path new-content]] - repo (state/get-current-repo)] - (file-handler/alter-files-handler! repo files {} {})) + (tree->file-content tree {:init-level init-level}))] + (if (and (string/blank? new-content) + (not blocks-just-deleted?)) + (state/pub-event! [:capture-error {:error (js/Error. "Empty content") + :payload {:file-path file-path}}]) + (let [files [[file-path new-content]] + repo (state/get-current-repo)] + (file-handler/alter-files-handler! repo files {} {})))) ;; In e2e tests, "card" page in db has no :file/path (js/console.error "File path from page-block is not valid" page-block tree)))) (defn save-tree! - [page-block tree] + [page-block tree blocks-just-deleted?] {:pre [(map? page-block)]} - (let [ok-handler #(save-tree-aux! page-block tree) + (let [ok-handler #(save-tree-aux! page-block tree blocks-just-deleted?) file (or (:block/file page-block) (when-let [page (:db/id (:block/page page-block))] (:block/file (db-utils/entity page))))] diff --git a/src/main/frontend/modules/outliner/file.cljs b/src/main/frontend/modules/outliner/file.cljs index 93e0e24aaa..cf62d1fcde 100644 --- a/src/main/frontend/modules/outliner/file.cljs +++ b/src/main/frontend/modules/outliner/file.cljs @@ -44,34 +44,37 @@ (defn do-write-file! - [repo page-db-id] + [repo page-db-id outliner-op] (let [page-block (db/pull repo '[*] page-db-id) page-db-id (:db/id page-block) whiteboard? (= "whiteboard" (:block/type page-block)) - blocks-count (model/get-page-blocks-count repo page-db-id)] - (if (or (and (> blocks-count 500) - (not (state/input-idle? repo {:diff 3000}))) ;; long page - ;; when this whiteboard page is just being updated - (and whiteboard? (not (state/whiteboard-page-idle? repo page-block)))) - (async/put! (state/get-file-write-chan) [repo page-db-id]) - (let [pull-keys (if whiteboard? whiteboard-blocks-pull-keys-with-persisted-ids '[*]) - blocks (model/get-page-blocks-no-cache repo (:block/name page-block) {:pull-keys pull-keys}) - blocks (if whiteboard? (map cleanup-whiteboard-block blocks) blocks)] - (when-not (and (= 1 (count blocks)) - (string/blank? (:block/content (first blocks))) - (nil? (:block/file page-block))) - (let [tree-or-blocks (if whiteboard? blocks - (tree/blocks->vec-tree repo blocks (:block/name page-block)))] - (if page-block - (file/save-tree! page-block tree-or-blocks) - (js/console.error (str "can't find page id: " page-db-id))))))))) + blocks-count (model/get-page-blocks-count repo page-db-id) + blocks-just-deleted? (and (zero? blocks-count) + (contains? #{:delete-blocks :move-blocks} outliner-op))] + (when (or (>= blocks-count 1) blocks-just-deleted?) + (if (or (and (> blocks-count 500) + (not (state/input-idle? repo {:diff 3000}))) ;; long page + ;; when this whiteboard page is just being updated + (and whiteboard? (not (state/whiteboard-page-idle? repo page-block)))) + (async/put! (state/get-file-write-chan) [repo page-db-id outliner-op]) + (let [pull-keys (if whiteboard? whiteboard-blocks-pull-keys-with-persisted-ids '[*]) + blocks (model/get-page-blocks-no-cache repo (:block/name page-block) {:pull-keys pull-keys}) + blocks (if whiteboard? (map cleanup-whiteboard-block blocks) blocks)] + (when-not (and (= 1 (count blocks)) + (string/blank? (:block/content (first blocks))) + (nil? (:block/file page-block))) + (let [tree-or-blocks (if whiteboard? blocks + (tree/blocks->vec-tree repo blocks (:block/name page-block)))] + (if page-block + (file/save-tree! page-block tree-or-blocks blocks-just-deleted?) + (js/console.error (str "can't find page id: " page-db-id)))))))))) (defn write-files! [pages] (when (seq pages) (when-not config/publishing? - (doseq [[repo page-id] (set pages)] - (try (do-write-file! repo page-id) + (doseq [[repo page-id outliner-op] (set pages)] + (try (do-write-file! repo page-id outliner-op) (catch :default e (notification/show! [:div @@ -81,15 +84,17 @@ (log/error :file/write-file-error {:error e}))))))) (defn sync-to-file - [{page-db-id :db/id}] - (if (nil? page-db-id) - (notification/show! - "Write file failed, can't find the current page!" - :error) - (when-let [repo (state/get-current-repo)] - (if (:graph/importing @state/state) ; write immediately - (write-files! [[repo page-db-id]]) - (async/put! (state/get-file-write-chan) [repo page-db-id (tc/to-long (t/now))]))))) + ([page] + (sync-to-file page nil)) + ([{page-db-id :db/id} outliner-op] + (if (nil? page-db-id) + (notification/show! + "Write file failed, can't find the current page!" + :error) + (when-let [repo (state/get-current-repo)] + (if (:graph/importing @state/state) ; write immediately + (write-files! [[repo page-db-id outliner-op]]) + (async/put! (state/get-file-write-chan) [repo page-db-id outliner-op (tc/to-long (t/now))])))))) (def *writes-finished? (atom {})) diff --git a/src/main/frontend/modules/outliner/pipeline.cljs b/src/main/frontend/modules/outliner/pipeline.cljs index bc7de4b337..a7854a9a1e 100644 --- a/src/main/frontend/modules/outliner/pipeline.cljs +++ b/src/main/frontend/modules/outliner/pipeline.cljs @@ -11,7 +11,7 @@ (defn updated-page-hook [tx-report page] (when-not (get-in tx-report [:tx-meta :created-from-journal-template?]) - (file/sync-to-file page))) + (file/sync-to-file page (:outliner-op (:tx-meta tx-report))))) ;; TODO: it'll be great if we can calculate the :block/path-refs before any ;; outliner transaction, this way we can group together the real outliner tx