diff --git a/frontend/src/app/main/data/workspace/shape_layout.cljs b/frontend/src/app/main/data/workspace/shape_layout.cljs index 6f6cfba59d..5c212cd7c1 100644 --- a/frontend/src/app/main/data/workspace/shape_layout.cljs +++ b/frontend/src/app/main/data/workspace/shape_layout.cljs @@ -101,13 +101,14 @@ ;; Never call this directly but through the data-event `:layout/update` ;; Otherwise a lot of cycle dependencies could be generated (defn- update-layout-positions - [{:keys [page-id ids undo-group reflow-tasks]}] + [{:keys [page-id ids undo-group reflow-tasks save-undo?]}] (ptk/reify ::update-layout-positions ptk/WatchEvent (watch [_ state stream] - (let [page-id (or page-id (:current-page-id state)) - objects (dsh/lookup-page-objects state page-id) - ids (->> ids (remove uuid/zero?) (filter #(contains? objects %))) + (let [page-id (or page-id (:current-page-id state)) + objects (dsh/lookup-page-objects state page-id) + ids (->> ids (remove uuid/zero?) (filter #(contains? objects %))) + save-undo? (not (false? save-undo?)) update-positions-stream (if (d/not-empty? ids) @@ -116,11 +117,13 @@ (= page-id (:current-page-id state))) (rx/of (dwm/apply-wasm-modifiers modif-tree :stack-undo? true + :save-undo? save-undo? :undo-group undo-group :ignore-touched true)) (rx/of (dwm/apply-modifiers {:page-id page-id :modifiers modif-tree :stack-undo? true + :save-undo? save-undo? :ignore-touched true :undo-group undo-group})))) (rx/empty)) @@ -169,11 +172,14 @@ (rx/filter #(d/not-empty? %)) (rx/mapcat (fn [updates] - (->> (group-by :page-id updates) - (map (fn [[page-id updates]] + ;; Updates flagged `:save-undo? false` (automatic text size + ;; heals) are laid out apart, so their reflow records no undo. + (->> (group-by (juxt :page-id #(not (false? (:save-undo? %)))) updates) + (map (fn [[[page-id save-undo?] updates]] (let [ids (into #{} (mapcat :ids) updates)] (update-layout-positions {:page-id page-id :ids ids + :save-undo? save-undo? :reflow-tasks (mapv ::reflow-task updates)}))))))) (rx/take-until stopper) ;; On workspace teardown clear everything still pending. diff --git a/frontend/src/app/main/data/workspace/texts.cljs b/frontend/src/app/main/data/workspace/texts.cljs index 1e219992b3..2cd51138e9 100644 --- a/frontend/src/app/main/data/workspace/texts.cljs +++ b/frontend/src/app/main/data/workspace/texts.cljs @@ -864,50 +864,81 @@ shape))] - (let [ids (into #{} (filter changed-text?) (keys props))] - (rx/of (dwu/start-undo-transaction undo-id) - (dwsh/update-shapes ids update-fn {:with-objects? true - :reg-objects? true - :stack-undo? true - :ignore-touched true}) - (ptk/data-event :layout/update {:ids ids}) - (dwu/commit-undo-transaction undo-id)))))))) + (let [changed-ids (into #{} (filter changed-text?) (keys props)) + ;; Automatic size heals record no undo step, their parents' + ;; reflow included, and do not sync to component copies. + heal-ids (into #{} + (filter (or (::resize-text-no-undo-ids state) #{})) + changed-ids) + ids (into #{} (remove heal-ids) changed-ids) + opts {:with-objects? true + :reg-objects? true + :stack-undo? true + :ignore-touched true}] + (rx/concat + (rx/of (dwu/start-undo-transaction undo-id) + (dwsh/update-shapes ids update-fn opts)) + (if (seq heal-ids) + (rx/of (dwsh/update-shapes heal-ids update-fn + (assoc opts + :save-undo? false + :skip-component-sync? true))) + (rx/empty)) + (rx/of (ptk/data-event :layout/update {:ids ids})) + (if (seq heal-ids) + (rx/of (ptk/data-event :layout/update {:ids heal-ids :save-undo? false})) + (rx/empty)) + (rx/of (dwu/commit-undo-transaction undo-id))))))))) + +(defn- track-no-undo-id + "Keep `id` in the no-undo set at `key` when `opts` ask for no undo step, + drop it otherwise (the latest request for a shape wins)." + [state key id opts] + (if (false? (:save-undo? opts)) + (update state key (fnil conj #{}) id) + (update state key disj id))) (defn resize-text - [id new-width new-height] + "Resize an auto-grow text to its measured size. The requests are batched + and committed together; `opts` with `:save-undo? false` commit this one + without an undo step." + ([id new-width new-height] + (resize-text id new-width new-height nil)) + ([id new-width new-height opts] + (let [cur-event (js/Symbol) + reflow-task (wrf/task :text-resize [id])] + (ptk/reify ::resize-text + ptk/UpdateEvent + (update [_ state] + (-> state + (update ::resize-text-debounce-props (fnil assoc {}) id [new-width new-height]) + (update ::resize-text-reflow-tasks (fnil conj []) reflow-task) + (track-no-undo-id ::resize-text-no-undo-ids id opts) + (cond-> (nil? (::resize-text-debounce-event state)) + (assoc ::resize-text-debounce-event cur-event)))) - (let [cur-event (js/Symbol) - reflow-task (wrf/task :text-resize [id])] - (ptk/reify ::resize-text - ptk/UpdateEvent - (update [_ state] - (-> state - (update ::resize-text-debounce-props (fnil assoc {}) id [new-width new-height]) - (update ::resize-text-reflow-tasks (fnil conj []) reflow-task) - (cond-> (nil? (::resize-text-debounce-event state)) - (assoc ::resize-text-debounce-event cur-event)))) - - ptk/WatchEvent - (watch [_ state stream] - (wrf/start! reflow-task) - (if (= (::resize-text-debounce-event state) cur-event) - (let [stopper (->> stream (rx/filter (ptk/type? ::dw/finalize-workspace)))] - (rx/concat - (rx/merge - (->> stream - (rx/filter (ptk/type? ::resize-text)) - (rx/debounce 50) - (rx/take 1) - (rx/map #(commit-resize-text)) - (rx/take-until stopper)) - (rx/of (resize-text id new-width new-height))) - (rx/of (fn [state] - (wrf/finish-tasks! (::resize-text-reflow-tasks state)) - (dissoc state - ::resize-text-debounce-props - ::resize-text-reflow-tasks - ::resize-text-debounce-event))))) - (rx/empty)))))) + ptk/WatchEvent + (watch [_ state stream] + (wrf/start! reflow-task) + (if (= (::resize-text-debounce-event state) cur-event) + (let [stopper (->> stream (rx/filter (ptk/type? ::dw/finalize-workspace)))] + (rx/concat + (rx/merge + (->> stream + (rx/filter (ptk/type? ::resize-text)) + (rx/debounce 50) + (rx/take 1) + (rx/map #(commit-resize-text)) + (rx/take-until stopper)) + (rx/of (resize-text id new-width new-height opts))) + (rx/of (fn [state] + (wrf/finish-tasks! (::resize-text-reflow-tasks state)) + (dissoc state + ::resize-text-debounce-props + ::resize-text-reflow-tasks + ::resize-text-no-undo-ids + ::resize-text-debounce-event))))) + (rx/empty))))))) (defn save-default-font [data] @@ -1052,51 +1083,68 @@ ptk/WatchEvent (watch [_ state _] - (let [position-data (::update-position-data state)] - (rx/of (dwsh/update-shapes - (keys position-data) - (fn [shape] - (-> shape - (assoc :position-data (get position-data (:id shape))))) - {:stack-undo? true :reg-objects? false})))))) + (let [position-data (::update-position-data state) + ;; Automatic size heals record no undo step and do not sync to + ;; component copies. + no-undo-ids (or (::update-position-data-no-undo-ids state) #{}) + update-fn (fn [shape] + (-> shape + (assoc :position-data (get position-data (:id shape))))) + opts {:stack-undo? true :reg-objects? false} + ids (into [] (remove no-undo-ids) (keys position-data)) + heal-ids (into [] (filter no-undo-ids) (keys position-data))] + (rx/concat + (rx/of (dwsh/update-shapes ids update-fn opts)) + (if (seq heal-ids) + (rx/of (dwsh/update-shapes heal-ids update-fn + (assoc opts + :save-undo? false + :skip-component-sync? true))) + (rx/empty))))))) (defn update-position-data - [id position-data] + "Store the measured position-data of a text. The requests are batched and + committed together; `opts` with `:save-undo? false` commit this one + without an undo step." + ([id position-data] + (update-position-data id position-data nil)) + ([id position-data opts] + (let [cur-event (js/Symbol) + reflow-task (wrf/task :text-position [id])] + (ptk/reify ::update-position-data + ptk/UpdateEvent + (update [_ state] + (let [state (assoc-in state [:workspace-text-modifier id :position-data] position-data)] + (-> state + (update ::update-position-data-reflow-tasks (fnil conj []) reflow-task) + (track-no-undo-id ::update-position-data-no-undo-ids id opts) + (cond-> (nil? (::update-position-data-debounce state)) + (assoc ::update-position-data-debounce cur-event)) + (cond-> (some? (::update-position-data-debounce state)) + (assoc-in [::update-position-data id] position-data))))) - (let [cur-event (js/Symbol) - reflow-task (wrf/task :text-position [id])] - (ptk/reify ::update-position-data - ptk/UpdateEvent - (update [_ state] - (let [state (assoc-in state [:workspace-text-modifier id :position-data] position-data)] - (-> state - (update ::update-position-data-reflow-tasks (fnil conj []) reflow-task) - (cond-> (nil? (::update-position-data-debounce state)) - (assoc ::update-position-data-debounce cur-event)) - (cond-> (some? (::update-position-data-debounce state)) - (assoc-in [::update-position-data id] position-data))))) - - ptk/WatchEvent - (watch [_ state stream] - (wrf/start! reflow-task) - (if (= (::update-position-data-debounce state) cur-event) - (let [stopper (text-work-stopper stream)] - (rx/concat - (rx/merge - (->> stream - (rx/filter (ptk/type? ::update-position-data)) - (rx/debounce 50) - (rx/take 1) - (rx/map #(commit-position-data)) - (rx/take-until stopper)) - (rx/of (update-position-data id position-data))) - (rx/of (fn [state] - (wrf/finish-tasks! (::update-position-data-reflow-tasks state)) - (dissoc state - ::update-position-data-debounce - ::update-position-data - ::update-position-data-reflow-tasks))))) - (rx/empty)))))) + ptk/WatchEvent + (watch [_ state stream] + (wrf/start! reflow-task) + (if (= (::update-position-data-debounce state) cur-event) + (let [stopper (text-work-stopper stream)] + (rx/concat + (rx/merge + (->> stream + (rx/filter (ptk/type? ::update-position-data)) + (rx/debounce 50) + (rx/take 1) + (rx/map #(commit-position-data)) + (rx/take-until stopper)) + (rx/of (update-position-data id position-data opts))) + (rx/of (fn [state] + (wrf/finish-tasks! (::update-position-data-reflow-tasks state)) + (dissoc state + ::update-position-data-debounce + ::update-position-data + ::update-position-data-no-undo-ids + ::update-position-data-reflow-tasks))))) + (rx/empty))))))) (defn update-attrs [id attrs] diff --git a/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs b/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs index b190719e65..615955fd93 100644 --- a/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs @@ -92,15 +92,21 @@ (defn- text-heal "How to commit a measured auto-grow text in heal `phase`: nil commits - nothing, otherwise a map with the `:next` heal phase, if any. - The `:check` phase runs when the page opens: it commits only when the size + nothing, otherwise a map with the commit `:opts` and the `:next` heal + phase, if any. + The `:check` phase runs when the page opens. It commits when the size drifted, so rounding differences between renderers or browsers are not - written back on every open. Without a phase the text is committed as - usual." + written back on every open, or when the text has no position-data. A heal + records no undo step. A resize makes the text be measured again at its new + box, in the `:settle` phase, which is part of the heal too. Without a phase + the text is committed as usual." [phase shape size] (case phase - :check (when (and (some? size) (apply dwt/text-size-drifted? shape size)) - {}) + :check (let [drifted? (and (some? size) (apply dwt/text-size-drifted? shape size))] + (when (or drifted? (nil? (:position-data shape))) + (cond-> {:opts {:save-undo? false}} + drifted? (assoc :next :settle)))) + :settle {:opts {:save-undo? false}} {})) (defn- update-text-shape @@ -110,14 +116,15 @@ (->> (tsp/calc-position-data id) (p/fmap (fn [position-data] (let [size (measured-auto-size shape node) - heal (text-heal phase shape size)] + heal (text-heal phase shape size) + opts (:opts heal)] (when (some? heal) ;; At least one paragraph needs to be inside the bounding box (when (gsht/overlaps-position-data? shape position-data) - (st/emit! (dwt/update-position-data id position-data))) + (st/emit! (dwt/update-position-data id position-data opts))) (when-let [[width height] size] - (st/emit! (dwt/resize-text id width height)))) + (st/emit! (dwt/resize-text id width height opts)))) (st/emit! (dwt/clean-text-modifier id)) (:next heal)))) @@ -187,14 +194,6 @@ (mth/close? (dm/get-prop shape :width) (dm/get-prop other :width)) (mth/close? (dm/get-prop shape :height) (dm/get-prop other :height))))) -(defn text-phase - "Heal phase `shape` is measured in, from `phases`. A text without - position-data gets none: it is measured and committed as usual, so it - always gets a layout." - [phases shape] - (when (some? (:position-data shape)) - (get phases (:id shape)))) - (defn text-changed? "True when `new-shape` must be measured, given its previous version and its heal `phase`." @@ -232,16 +231,16 @@ ;; Heal phase per text id. Copies whose text is overridden start in ;; `:check` and are measured on page open: a sync while their page was - ;; closed can give them the main's size. + ;; closed can give them the main's size. A text in `:settle` waits for + ;; its heal resize to change the shape, which measures it again. phases* (mf/use-state #(override-copy-heal-phases text-shapes)) phases (deref phases*) text-change? (fn [id] - (let [new-shape (get text-shapes id)] - (text-changed? (get prev-text-shapes id) - new-shape - (text-phase phases new-shape)))) + (text-changed? (get prev-text-shapes id) + (get text-shapes id) + (get phases id))) changed-texts (mf/with-memo [text-shapes pending-update phases] @@ -278,7 +277,7 @@ (for [{:keys [id] :as shape} changed-texts] [:& text-container {:key (dm/str "text-container-" id) :shape shape - :phase (text-phase phases shape) + :phase (get phases id) :on-update handle-update-shape}])])) (mf/defc text-modifiers-renderer diff --git a/frontend/test/frontend_tests/data/workspace_heal_undo_test.cljs b/frontend/test/frontend_tests/data/workspace_heal_undo_test.cljs index cf214edd12..9eaee02e3e 100644 --- a/frontend/test/frontend_tests/data/workspace_heal_undo_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_heal_undo_test.cljs @@ -14,6 +14,8 @@ [app.common.test-helpers.ids-map :as cthi] [app.common.test-helpers.shapes :as cths] [app.main.data.changes :as dch] + [app.main.data.workspace.shape-layout :as dwsl] + [app.main.data.workspace.texts :as dwt] [app.main.data.workspace.wasm-text :as dwwt] [beicon.v2.core :as rx] [cljs.test :as t :include-macros true] @@ -96,3 +98,139 @@ "both heals land in one commit") (t/is (false? (:save-undo? commit)) "a batch of heals is not added to the undo history"))) + +;; SVG renderer + +(defn- collect-layout-updates + "Subscribe before emitting; accumulates `:layout/update` payloads." + [store] + (let [acc (atom [])] + (->> (ptk/input-stream store) + (rx/filter (ptk/type? :layout/update)) + (rx/map deref) + (rx/subs! #(swap! acc conj %))) + acc)) + +(t/deftest ^:async svg-heal-resize-records-no-undo-step + (let [file (-> (cthf/sample-file :file1) + (ctho/add-frame-with-text :frame :text "hello")) + store (ths/setup-store file {:renderer :svg}) + id (:id (cths/get-shape file :text)) + commits (collect-commits store id) + layouts (collect-layout-updates store)] + (ptk/emit! store (dwt/resize-text id 50 30 {:save-undo? false})) + (await (async/wait-for #(seq @commits) "a resize commit for the text" {:max-ticks 200})) + (t/is (false? (:save-undo? (first @commits))) + "the heal commit is not added to the undo history") + (t/is (true? (:skip-component-sync? (first @commits))) + "the heal does not sync to component copies") + (t/is (some #(and (false? (:save-undo? %)) (contains? (set (:ids %)) id)) @layouts) + "the parents' reflow is asked for without undo too"))) + +(defn- ^:async svg-mixed-commits + "Emit `heal-event` for one text and `user-event` for another in the same + debounce window; resolve to the first commit that touches each, as + `[heal-commit user-commit]`." + [heal-event user-event] + (let [file (-> (cthf/sample-file :file1) + (ctho/add-frame-with-text :frame-a :text-a "hello") + (ctho/add-frame-with-text :frame-b :text-b "world")) + store (ths/setup-store file {:renderer :svg}) + heal-id (:id (cths/get-shape file :text-a)) + user-id (:id (cths/get-shape file :text-b)) + heal (collect-commits store heal-id) + user (collect-commits store user-id)] + (ptk/emit! store (heal-event heal-id)) + (ptk/emit! store (user-event user-id)) + (await (async/wait-for #(and (seq @heal) (seq @user)) "a commit for each text" + {:max-ticks 400})) + [(first @heal) (first @user)])) + +(t/deftest ^:async svg-resize-batch-keeps-the-undo-step-of-a-user-resize + (let [[heal user] (await (svg-mixed-commits #(dwt/resize-text % 50 30 {:save-undo? false}) + #(dwt/resize-text % 60 40)))] + (t/is (false? (:save-undo? heal)) + "the heal in the batch records no undo step") + (t/is (true? (:save-undo? user)) + "the user resize in the same batch keeps its undo step") + (t/is (not (:skip-component-sync? user)) + "the user resize still syncs to component copies"))) + +(def ^:private position-data + [{:x 0 :y 0 :width 10 :height 10 :text "x" :font-size "14" :font-family "sourcesanspro" + :font-weight "400" :font-style "normal" :text-decoration "none" + :text-transform "none" :letter-spacing "normal" :direction "ltr" + :fills [{:fill-color "#000000" :fill-opacity 1}]}]) + +(t/deftest ^:async svg-position-data-batch-keeps-the-undo-step-of-a-user-change + (let [[heal user] (await (svg-mixed-commits #(dwt/update-position-data % position-data {:save-undo? false}) + #(dwt/update-position-data % position-data)))] + (t/is (false? (:save-undo? heal)) + "the heal in the batch records no undo step") + (t/is (true? (:skip-component-sync? heal)) + "the heal does not sync to component copies") + (t/is (true? (:save-undo? user)) + "the user change in the same batch keeps its undo step") + (t/is (not (:skip-component-sync? user)) + "the user change still syncs to component copies"))) + +(defn- ^:async svg-same-text-commit + "Emit `heal-event` and then `user-event` for the same text in one debounce + window; resolve to the first commit that touches it." + [heal-event user-event] + (let [file (-> (cthf/sample-file :file1) + (ctho/add-frame-with-text :frame :text "hello")) + store (ths/setup-store file {:renderer :svg}) + id (:id (cths/get-shape file :text)) + commits (collect-commits store id)] + (ptk/emit! store (heal-event id)) + (ptk/emit! store (user-event id)) + (await (async/wait-for #(seq @commits) "a commit for the text" {:max-ticks 200})) + (first @commits))) + +(t/deftest ^:async svg-user-resize-after-a-heal-of-the-same-text-keeps-undo + (let [commit (await (svg-same-text-commit #(dwt/resize-text % 50 30 {:save-undo? false}) + #(dwt/resize-text % 60 40)))] + (t/is (true? (:save-undo? commit)) + "the later user resize decides: it keeps its undo step") + (t/is (not (:skip-component-sync? commit)) + "and syncs to component copies"))) + +(t/deftest ^:async svg-user-change-after-a-heal-of-the-same-text-keeps-undo + (let [commit (await (svg-same-text-commit #(dwt/update-position-data % position-data {:save-undo? false}) + #(dwt/update-position-data % position-data)))] + (t/is (true? (:save-undo? commit)) + "the later user change decides: it keeps its undo step") + (t/is (not (:skip-component-sync? commit)) + "and syncs to component copies"))) + +(defn- flex-board-with-rect + "A flex column board with a rect placed off its layout position, so a + reflow of the board always moves the rect." + [file board-label rect-label x] + (-> file + (ctho/add-frame board-label {:x x :y 0 :width 200 :height 200 + :layout :flex :layout-flex-dir :column}) + (ctho/add-rect rect-label {:parent-label board-label + :x (+ x 50) :y 50 :width 20 :height 20}))) + +(t/deftest ^:async reflow-of-a-heal-records-no-undo-step + (let [file (-> (cthf/sample-file :file1) + (flex-board-with-rect :board-a :rect-a 0) + (flex-board-with-rect :board-b :rect-b 300)) + store (ths/setup-store file {:renderer :svg}) + rect-a (:id (cths/get-shape file :rect-a)) + rect-b (:id (cths/get-shape file :rect-b)) + heal (collect-commits store rect-a) + user (collect-commits store rect-b)] + (ptk/emit! store (dwsl/initialize-shape-layout)) + ;; Both updates land in the same 100 ms layout buffer. + (ptk/emit! store (ptk/data-event :layout/update {:ids [rect-a] :save-undo? false})) + (ptk/emit! store (ptk/data-event :layout/update {:ids [rect-b]})) + (await (async/wait-for #(and (seq @heal) (seq @user)) "a reflow commit for each board" + {:max-ticks 400})) + (ptk/emit! store (dwsl/finalize-shape-layout)) + (t/is (false? (:save-undo? (first @heal))) + "the reflow of a heal is not added to the undo history") + (t/is (true? (:save-undo? (first @user))) + "a user reflow in the same buffer keeps its undo step"))) diff --git a/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs b/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs index 0225ceafbd..13f8caff4e 100644 --- a/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs +++ b/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs @@ -101,59 +101,90 @@ (t/deftest unchanged-copy-is-measured-in-the-check-phase (let [copy (text :auto-height #{:content-group})] - (t/is (vth/text-changed? copy copy (vth/text-phase {(:id copy) :check} copy)) + (t/is (vth/text-changed? copy copy :check) "an unchanged copy waiting for its check is measured") - (t/is (not (vth/text-changed? copy copy (vth/text-phase {} copy))) + (t/is (not (vth/text-changed? copy copy nil)) "once checked, an unchanged copy is left alone"))) -(t/deftest text-without-position-data-gets-no-phase - (let [copy (assoc (text :auto-height #{:content-group}) :position-data nil)] - (t/is (nil? (vth/text-phase {(:id copy) :check} copy)) - "a text without position-data is committed as usual"))) - (defn- measure-with-box "Measure `shape` in heal `phase` with its layout box measured at `width` x - `height`. Resolves to the events emitted to the store." + `height`. Resolves to the events emitted to the store and the next heal + phase. The resize and position-data requests are recorded as `::resize` + and `::position-data` events carrying their options." [shape phase width height] - (let [emitted (atom [])] + (let [emitted (atom []) + record (fn [type] (fn [& args] (ptk/data-event type {:opts (last args)})))] (->> (mock/with-mocks* - {tsp/calc-position-data (mock/stub (constantly (p/resolved [fragment]))) - dom/query (mock/stub (constantly #js {})) - dom/get-bounding-rect (mock/stub (constantly {:width width :height height})) - st/emit! (mock/stub (fn [& events] (swap! emitted into events) nil))} + {tsp/calc-position-data (mock/stub (constantly (p/resolved [fragment]))) + dom/query (mock/stub (constantly #js {})) + dom/get-bounding-rect (mock/stub (constantly {:width width :height height})) + dwt/resize-text (mock/stub (record ::resize)) + dwt/update-position-data (mock/stub (record ::position-data)) + st/emit! (mock/stub (fn [& events] (swap! emitted into events) nil))} (await (measure-text shape #js {} phase))) - (p/fmap (fn [_] @emitted))))) + (p/fmap (fn [next-phase] {:events @emitted :next next-phase}))))) -(defn- emitted? +(defn- committed + "Options of the `type` request among `events`, `::none` if there is none." [type events] - (some #(= type (ptk/type %)) events)) + (or (some #(when (= type (ptk/type %)) (or (:opts (deref %)) {})) events) + ::none)) -(t/deftest ^:async check-phase-commits-a-drifted-copy - (let [events (await (measure-with-box (text :auto-height #{:content-group}) :check 200 34))] - (t/is (emitted? ::dwt/resize-text events) - "a copy measured taller than its stored box is resized") - (t/is (emitted? ::dwt/update-position-data events) - "its position-data is written with the new layout"))) +(def ^:private no-undo {:save-undo? false}) + +(t/deftest ^:async check-phase-heals-a-drifted-copy-without-undo + (let [{:keys [events next]} + (await (measure-with-box (text :auto-height #{:content-group}) :check 200 34))] + (t/is (= no-undo (committed ::resize events)) + "a copy measured taller than its stored box is resized, with no undo step") + (t/is (= no-undo (committed ::position-data events)) + "its position-data is written with the new layout, with no undo step") + (t/is (= :settle next) + "the measurement caused by the resize belongs to the heal"))) (t/deftest ^:async check-phase-leaves-a-matching-copy-alone - (let [events (await (measure-with-box (text :auto-height #{:content-group}) :check 200 17.4))] - (t/is (not (emitted? ::dwt/resize-text events)) + (let [{:keys [events next]} + (await (measure-with-box (text :auto-height #{:content-group}) :check 200 17.4))] + (t/is (= ::none (committed ::resize events)) "a copy that matches its stored box is not resized") - (t/is (not (emitted? ::dwt/update-position-data events)) + (t/is (= ::none (committed ::position-data events)) "nor is its position-data written back") - (t/is (emitted? ::dwt/clean-text-modifier events) - "the measurement still completes"))) + (t/is (some #(= ::dwt/clean-text-modifier (ptk/type %)) events) + "the measurement still completes") + (t/is (nil? next) + "the heal ends"))) + +(t/deftest ^:async check-phase-heals-a-copy-without-position-data + (let [copy (assoc (text :auto-height #{:content-group}) :position-data nil) + {:keys [events next]} (await (measure-with-box copy :check 200 17.4))] + (t/is (= no-undo (committed ::position-data events)) + "a copy that lost its position-data in a sync gets it back, with no undo step") + (t/is (nil? next) + "nothing resizes it, so the heal ends"))) (t/deftest ^:async check-phase-commits-nothing-when-it-cannot-measure - (let [events (await (measure-with-box (text :auto-height #{:content-group}) :check 0 0))] - (t/is (not (emitted? ::dwt/resize-text events)) + (let [{:keys [events]} + (await (measure-with-box (text :auto-height #{:content-group}) :check 0 0))] + (t/is (= ::none (committed ::resize events)) "an empty layout box is not a size") - (t/is (not (emitted? ::dwt/update-position-data events)) + (t/is (= ::none (committed ::position-data events)) "nothing is written for a text that could not be measured"))) +(t/deftest ^:async settle-phase-commits-without-undo + (let [{:keys [events next]} + (await (measure-with-box (text :auto-height #{:content-group}) :settle 200 34))] + (t/is (= no-undo (committed ::resize events)) + "the measurement after a heal resize records no undo step") + (t/is (= no-undo (committed ::position-data events)) + "nor does its position-data") + (t/is (nil? next) + "the heal ends"))) + (t/deftest ^:async no-phase-commits-as-usual - (let [events (await (measure-with-box (text :auto-height nil) nil 200 17.4))] - (t/is (emitted? ::dwt/resize-text events) - "outside a heal, a size within 1px is still committed") - (t/is (emitted? ::dwt/update-position-data events) - "and so is the position-data"))) + (let [{:keys [events next]} + (await (measure-with-box (text :auto-height nil) nil 200 17.4))] + (t/is (= {} (committed ::resize events)) + "outside a heal, a size within 1px is still committed, with undo") + (t/is (= {} (committed ::position-data events)) + "and so is the position-data") + (t/is (nil? next))))