enhance: allows empty content only for delete-blocks and move-blocks (#8199)

allows empty content only for delete-blocks and move-blocks
This commit is contained in:
Tienson Qin
2023-01-09 22:24:08 +08:00
committed by GitHub
parent 6dba93a0d7
commit 77e63f6461
4 changed files with 48 additions and 37 deletions

2
.gitignore vendored
View File

@@ -55,3 +55,5 @@ android/app/src/main/assets/capacitor.config.json
*.sublime-*
/public/static
.yarn/
.yarnrc.yml

View File

@@ -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))))]

View File

@@ -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 {}))

View File

@@ -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