🐛 Fixed issues when changing pages quickly while resizing texts

This commit is contained in:
alonso.torres 2021-01-29 11:50:59 +01:00
parent bb04181abf
commit bc3275e624

View file

@ -228,55 +228,56 @@
(let [page-id (:current-page-id state) (let [page-id (:current-page-id state)
objects0 (get-in state [:workspace-file :data :pages-index page-id :objects]) objects0 (get-in state [:workspace-file :data :pages-index page-id :objects])
objects1 (get-in state [:workspace-data :pages-index page-id :objects]) objects1 (get-in state [:workspace-data :pages-index page-id :objects])]
(if-not (every? #(contains? objects1(first %)) changes)
(rx/empty)
(let [change-text-shape
(fn [objects [id [new-width new-height]]]
(when (contains? objects id)
(let [shape (get objects id)
{:keys [selrect grow-type overflow-text]} (gsh/transform-shape shape)
{shape-width :width shape-height :height} selrect
change-text-shape modifier-width (gsh/resize-modifiers shape :width new-width)
(fn [objects [id [new-width new-height]]] modifier-height (gsh/resize-modifiers shape :height new-height)
(let [shape (get objects id) shape (cond-> shape
{:keys [selrect grow-type overflow-text]} (gsh/transform-shape shape) (and overflow-text (not= :fixed grow-type))
{shape-width :width shape-height :height} selrect (assoc :overflow-text false)
modifier-width (gsh/resize-modifiers shape :width new-width) (and (= :fixed grow-type) (not overflow-text) (> new-height shape-height))
modifier-height (gsh/resize-modifiers shape :height new-height) (assoc :overflow-text true)
shape (cond-> shape (and (= :fixed grow-type) overflow-text (<= new-height shape-height))
(and overflow-text (not= :fixed grow-type)) (assoc :overflow-text true)
(assoc :overflow-text false)
(and (= :fixed grow-type) (not overflow-text) (> new-height shape-height)) (and (not-changed? shape-width new-width) (= grow-type :auto-width))
(assoc :overflow-text true) (-> (assoc :modifiers modifier-width)
(gsh/transform-shape))
(and (= :fixed grow-type) overflow-text (<= new-height shape-height)) (and (not-changed? shape-height new-height)
(assoc :overflow-text true) (or (= grow-type :auto-height) (= grow-type :auto-width)))
(-> (assoc :modifiers modifier-height)
(gsh/transform-shape)))]
(assoc objects id shape))))
(and (not-changed? shape-width new-width) (= grow-type :auto-width)) undo-transaction (get-in state [:workspace-undo :transaction])
(-> (assoc :modifiers modifier-width) objects2 (->> changes (reduce change-text-shape objects1))
(gsh/transform-shape))
(and (not-changed? shape-height new-height) regchg {:type :reg-objects
(or (= grow-type :auto-height) (= grow-type :auto-width))) :page-id page-id
(-> (assoc :modifiers modifier-height) :shapes (vec (keys changes))}
(gsh/transform-shape)))]
(assoc objects id shape)))
undo-transaction (get-in state [:workspace-undo :transaction]) rchanges (dwc/generate-changes page-id objects1 objects2)
objects2 (->> changes (reduce change-text-shape objects1)) uchanges (dwc/generate-changes page-id objects2 objects0)]
regchg {:type :reg-objects (if (seq rchanges)
:page-id page-id (rx/concat
:shapes (vec (keys changes))} (when-not undo-transaction
(rx/of (dwc/start-undo-transaction)))
rchanges (dwc/generate-changes page-id objects1 objects2) (rx/of (dwc/commit-changes (conj rchanges regchg) (conj uchanges regchg) {:commit-local? true}))
uchanges (dwc/generate-changes page-id objects2 objects0)] (when-not undo-transaction
(rx/of (dwc/discard-undo-transaction)))))))))))
(if (seq rchanges)
(rx/concat
(when-not undo-transaction
(rx/of (dwc/start-undo-transaction)))
(rx/of (dwc/commit-changes (conj rchanges regchg) (conj uchanges regchg) {:commit-local? true}))
(when-not undo-transaction
(rx/of (dwc/discard-undo-transaction)))))))))
;; When a resize-event arrives we start "buffering" for a time ;; When a resize-event arrives we start "buffering" for a time
;; after that time we invoke `resize-text-batch` with all the changes ;; after that time we invoke `resize-text-batch` with all the changes
@ -292,22 +293,33 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [;; This stream aggregates the events of "resizing" (let [;; This stream aggregates the events of "resizing"
resize-events (rx/merge resize-events
(->> (rx/of (resize-text id new-width new-height))) (rx/merge
(->> stream (rx/filter (ptk/type? ::resize-text)))) (->> (rx/of (resize-text id new-width new-height)))
(->> stream (rx/filter (ptk/type? ::resize-text))))
;; Stop buffering after time without resizes ;; Stop buffering after time without resizes
stop-buffer (->> resize-events (rx/debounce 100))] stop-buffer (->> resize-events (rx/debounce 100))
;; Agregates the resizes so only send the resize when the sizes are stable
resize-batch
(->> resize-events
(rx/take-until stop-buffer)
(rx/reduce (fn [acc event]
(assoc acc (:id @event) [(:width @event) (:height @event)]))
{id [new-width new-height]})
(rx/map #(resize-text-batch %)))
;; This stream retrieves the changes of page so we cancel the agregation
change-page
(->> stream
(rx/filter (ptk/type? :app.main.data.workspace/finalize-page))
(rx/take 1)
(rx/ignore))]
(if-not (::handling-texts state) (if-not (::handling-texts state)
(->> (rx/concat (->> (rx/concat
(rx/of #(assoc % ::handling-texts true)) (rx/of #(assoc % ::handling-texts true))
(->> resize-events (rx/race resize-batch change-page)
(rx/take-until stop-buffer)
(rx/reduce (fn [acc event]
(assoc acc (:id @event) [(:width @event) (:height @event)]))
{id [new-width new-height]})
(rx/map #(resize-text-batch %)))
(rx/of #(dissoc % ::handling-texts)))) (rx/of #(dissoc % ::handling-texts))))
(rx/empty)))))) (rx/empty))))))