🐛 Fix reflow layout when changes in paths and texts

This commit is contained in:
alonso.torres 2022-11-22 17:08:23 +01:00
parent 54fd836dd4
commit 441e142349
7 changed files with 100 additions and 37 deletions

View file

@ -55,7 +55,6 @@
[app.main.data.workspace.selection :as dws] [app.main.data.workspace.selection :as dws]
[app.main.data.workspace.shapes :as dwsh] [app.main.data.workspace.shapes :as dwsh]
[app.main.data.workspace.shapes-update-layout :as dwul] [app.main.data.workspace.shapes-update-layout :as dwul]
[app.main.data.workspace.shapes-update-layout :as dwul]
[app.main.data.workspace.state-helpers :as wsh] [app.main.data.workspace.state-helpers :as wsh]
[app.main.data.workspace.thumbnails :as dwth] [app.main.data.workspace.thumbnails :as dwth]
[app.main.data.workspace.transforms :as dwt] [app.main.data.workspace.transforms :as dwt]

View file

@ -15,6 +15,7 @@
[app.common.pages.helpers :as cph] [app.common.pages.helpers :as cph]
[app.common.spec :as us] [app.common.spec :as us]
[app.common.types.modifiers :as ctm] [app.common.types.modifiers :as ctm]
[app.common.types.shape :as cts]
[app.common.types.shape.layout :as ctl] [app.common.types.shape.layout :as ctl]
[app.main.data.workspace.changes :as dch] [app.main.data.workspace.changes :as dch]
[app.main.data.workspace.comments :as-alias dwcm] [app.main.data.workspace.comments :as-alias dwcm]
@ -188,6 +189,42 @@
[(get-in objects [k :name]) v])) [(get-in objects [k :name]) v]))
modif-tree))) modif-tree)))
(defn apply-text-modifiers
[objects text-modifiers]
(letfn [(apply-text-modifier
[shape {:keys [width height]}]
(cond-> shape
(some? width)
(assoc :width width)
(some? height)
(assoc :height height)
(or (some? width) (some? height))
(cts/setup-rect-selrect)))]
(loop [modifiers (seq text-modifiers)
result objects]
(if (empty? modifiers)
result
(let [[id text-modifier] (first modifiers)]
(recur (rest modifiers)
(update objects id apply-text-modifier text-modifier)))))))
#_(defn apply-path-modifiers
[objects path-modifiers]
(letfn [(apply-path-modifier
[shape {:keys [content-modifiers]}]
(let [shape (update shape :content upc/apply-content-modifiers content-modifiers)
[points selrect] (helpers/content->points+selrect shape (:content shape))]
(assoc shape :selrect selrect :points points)))]
(loop [modifiers (seq path-modifiers)
result objects]
(if (empty? modifiers)
result
(let [[id path-modifier] (first modifiers)]
(recur (rest modifiers)
(update objects id apply-path-modifier path-modifier)))))))
(defn set-modifiers (defn set-modifiers
([modif-tree] ([modif-tree]
(set-modifiers modif-tree false)) (set-modifiers modif-tree false))
@ -206,7 +243,10 @@
(and (not ignore-snap-pixel) (contains? (:workspace-layout state) :snap-pixel-grid)) (and (not ignore-snap-pixel) (contains? (:workspace-layout state) :snap-pixel-grid))
modif-tree modif-tree
(gsh/set-objects-modifiers modif-tree objects ignore-constraints snap-pixel?)] (as-> objects $
(apply-text-modifiers $ (get state :workspace-text-modifier))
;;(apply-path-modifiers $ (get-in state [:workspace-local :edit-path]))
(gsh/set-objects-modifiers modif-tree $ ignore-constraints snap-pixel?))]
(assoc state :workspace-modifiers modif-tree)))))) (assoc state :workspace-modifiers modif-tree))))))

View file

@ -21,6 +21,7 @@
[app.main.data.workspace.path.state :as st] [app.main.data.workspace.path.state :as st]
[app.main.data.workspace.path.streams :as streams] [app.main.data.workspace.path.streams :as streams]
[app.main.data.workspace.path.undo :as undo] [app.main.data.workspace.path.undo :as undo]
[app.main.data.workspace.shapes-update-layout :as dwul]
[app.main.data.workspace.state-helpers :as wsh] [app.main.data.workspace.state-helpers :as wsh]
[app.main.streams :as ms] [app.main.streams :as ms]
[app.util.path.tools :as upt] [app.util.path.tools :as upt]
@ -297,22 +298,25 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [mode (get-in state [:workspace-local :edit-path id :edit-mode])] (let [mode (get-in state [:workspace-local :edit-path id :edit-mode])
stopper (->> stream (rx/filter (ptk/type? ::start-path-edit)))
interrupt (->> stream (rx/filter #(= % :interrupt)) (rx/take 1))]
(rx/concat (rx/concat
(rx/of (undo/start-path-undo)) (rx/of (undo/start-path-undo))
(rx/of (drawing/change-edit-mode mode)) (rx/of (drawing/change-edit-mode mode))
(->> stream (->> interrupt
(rx/take-until (->> stream (rx/filter (ptk/type? ::start-path-edit)))) (rx/map #(stop-path-edit id))
(rx/filter #(= % :interrupt)) (rx/take-until stopper)))))))
(rx/take 1)
(rx/map #(stop-path-edit))))))))
(defn stop-path-edit [] (defn stop-path-edit [id]
(ptk/reify ::stop-path-edit (ptk/reify ::stop-path-edit
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [id (get-in state [:workspace-local :edition])] (update state :workspace-local dissoc :edit-path id))
(update state :workspace-local dissoc :edit-path id)))))
ptk/WatchEvent
(watch [_ _ _]
(rx/of (dwul/update-layout-positions [id])))))
(defn split-segments (defn split-segments
[{:keys [from-p to-p t]}] [{:keys [from-p to-p t]}]

View file

@ -8,9 +8,7 @@
(:require (:require
[app.common.data :as d] [app.common.data :as d]
[app.common.types.modifiers :as ctm] [app.common.types.modifiers :as ctm]
[app.common.types.shape.layout :as ctl]
[app.main.data.workspace.modifiers :as dwm] [app.main.data.workspace.modifiers :as dwm]
[app.main.data.workspace.state-helpers :as wsh]
[beicon.core :as rx] [beicon.core :as rx]
[potok.core :as ptk])) [potok.core :as ptk]))
@ -18,11 +16,9 @@
[ids] [ids]
(ptk/reify ::update-layout-positions (ptk/reify ::update-layout-positions
ptk/WatchEvent ptk/WatchEvent
(watch [_ state _] (watch [_ _ _]
(let [objects (wsh/lookup-page-objects state)
ids (->> ids (filter (partial ctl/layout? objects)))]
(if (d/not-empty? ids) (if (d/not-empty? ids)
(let [modif-tree (dwm/create-modif-tree ids (ctm/reflow-modifiers))] (let [modif-tree (dwm/create-modif-tree ids (ctm/reflow-modifiers))]
(rx/of (dwm/set-modifiers modif-tree) (rx/of (dwm/set-modifiers modif-tree)
(dwm/apply-modifiers))) (dwm/apply-modifiers)))
(rx/empty)))))) (rx/empty)))))

View file

@ -14,11 +14,13 @@
[app.common.pages.helpers :as cph] [app.common.pages.helpers :as cph]
[app.common.text :as txt] [app.common.text :as txt]
[app.common.types.modifiers :as ctm] [app.common.types.modifiers :as ctm]
[app.common.types.shape :as cts]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.main.data.workspace.changes :as dch] [app.main.data.workspace.changes :as dch]
[app.main.data.workspace.common :as dwc] [app.main.data.workspace.common :as dwc]
[app.main.data.workspace.selection :as dws] [app.main.data.workspace.selection :as dws]
[app.main.data.workspace.shapes :as dwsh] [app.main.data.workspace.shapes :as dwsh]
[app.main.data.workspace.shapes-update-layout :as dwul]
[app.main.data.workspace.state-helpers :as wsh] [app.main.data.workspace.state-helpers :as wsh]
[app.main.data.workspace.undo :as dwu] [app.main.data.workspace.undo :as dwu]
[app.util.router :as rt] [app.util.router :as rt]
@ -75,7 +77,8 @@
(dch/update-shapes [id] (fn [shape] (dch/update-shapes [id] (fn [shape]
(-> shape (-> shape
(assoc :content content) (assoc :content content)
(merge modifiers)))) (merge modifiers)
(cts/setup-rect-selrect))))
(dwu/commit-undo-transaction))))) (dwu/commit-undo-transaction)))))
(when (some? id) (when (some? id)
@ -316,7 +319,8 @@
[id new-width new-height] [id new-width new-height]
(ptk/reify ::resize-text (ptk/reify ::resize-text
ptk/WatchEvent ptk/WatchEvent
(watch [_ _ _] (watch [_ state _]
(let [shape (wsh/lookup-shape state id)]
(letfn [(update-fn [shape] (letfn [(update-fn [shape]
(let [{:keys [selrect grow-type]} shape (let [{:keys [selrect grow-type]} shape
{shape-width :width shape-height :height} selrect] {shape-width :width shape-height :height} selrect]
@ -328,7 +332,11 @@
(or (= grow-type :auto-height) (= grow-type :auto-width))) (or (= grow-type :auto-height) (= grow-type :auto-width)))
(gsh/transform-shape (ctm/change-dimensions-modifiers shape :height new-height)))))] (gsh/transform-shape (ctm/change-dimensions-modifiers shape :height new-height)))))]
(rx/of (dch/update-shapes [id] update-fn {:reg-objects? true :save-undo? false})))))) (when (or (and (not-changed? (:width shape) new-width) (= (:grow-type shape) :auto-width))
(and (not-changed? (:height shape) new-height)
(or (= (:grow-type shape) :auto-height) (= (:grow-type shape) :auto-width))))
(rx/of (dch/update-shapes [id] update-fn {:reg-objects? true :save-undo? false})
(dwul/update-layout-positions [id]))))))))
(defn save-font (defn save-font
[data] [data]
@ -368,7 +376,16 @@
(ptk/reify ::update-text-modifier (ptk/reify ::update-text-modifier
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(update-in state [:workspace-text-modifier id] (fnil merge {}) props)))) (update-in state [:workspace-text-modifier id] (fnil merge {}) props))
ptk/WatchEvent
(watch [_ state _]
(let [shape (wsh/lookup-shape state id)]
(when (or (and (some? (:width props))
(not (mth/close? (:width props) (:width shape))))
(and (some? (:height props))
(not (mth/close? (:height props) (:height shape)))))
(rx/of (dwul/update-layout-positions [id])))))))
(defn clean-text-modifier (defn clean-text-modifier
[id] [id]

View file

@ -28,9 +28,12 @@
(let [selfn #(get-in % [:edit-path id])] (let [selfn #(get-in % [:edit-path id])]
#(l/derived selfn refs/workspace-local)))) #(l/derived selfn refs/workspace-local))))
(defn content-modifiers-ref
[id]
(l/derived #(get-in % [:edit-path id :content-modifiers]) refs/workspace-local))
(defn make-content-modifiers-ref [id] (defn make-content-modifiers-ref [id]
(mf/use-memo (mf/use-memo
(mf/deps id) (mf/deps id)
(let [selfn #(get-in % [:edit-path id :content-modifiers])] #(content-modifiers-ref id)))
#(l/derived selfn refs/workspace-local))))

View file

@ -155,7 +155,11 @@
(not (identical? old-shape new-shape)) (not (identical? old-shape new-shape))
(not= (dissoc old-shape :migrate) (not= (dissoc old-shape :migrate)
(dissoc new-shape :migrate))) (dissoc new-shape :migrate)))
(not= new-modifiers old-modifiers) (and (not= new-modifiers old-modifiers)
(or (nil? new-modifiers)
(nil? old-modifiers)
(not (ctm/only-move? new-modifiers))
(not (ctm/only-move? old-modifiers))))
;; When the position data is nil we force to recalculate ;; When the position data is nil we force to recalculate
(:migrate new-shape)))) (:migrate new-shape))))