0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-12 07:41:43 -05:00

Merge pull request #2802 from penpot/alotor-polishing-6

Polishing after confluence test
This commit is contained in:
Eva Marco 2023-01-19 12:55:59 +01:00 committed by GitHub
commit fe77ef4438
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 177 additions and 117 deletions

View file

@ -359,7 +359,7 @@
to-reflow
(cond-> to-reflow
(and (ctl/layout-child-id? objects current)
(and (ctl/layout-descent? objects parent-base)
(not= uuid/zero (:frame-id parent-base)))
(conj (:frame-id parent-base)))]
(recur modif-tree
@ -389,8 +389,8 @@
(set-objects-modifiers nil modif-tree objects params))
([old-modif-tree modif-tree objects
{:keys [ignore-constraints snap-pixel? snap-precision]
:or {ignore-constraints false snap-pixel? false snap-precision 1}}]
{:keys [ignore-constraints snap-pixel? snap-precision snap-ignore-axis]
:or {ignore-constraints false snap-pixel? false snap-precision 1 snap-ignore-axis nil}}]
(let [objects (-> objects
(cond-> (some? old-modif-tree)
(apply-structure-modifiers old-modif-tree))
@ -398,7 +398,7 @@
modif-tree
(cond-> modif-tree
snap-pixel? (gpp/adjust-pixel-precision objects snap-precision))
snap-pixel? (gpp/adjust-pixel-precision objects snap-precision snap-ignore-axis))
bounds (d/lazy-map (keys objects) #(dm/get-in objects [% :points]))
bounds (cond-> bounds

View file

@ -39,16 +39,25 @@
(ctm/resize scalev origin transform transform-inverse {:precise? true}))))
(defn position-pixel-precision
[modifiers _ points precision]
[modifiers _ points precision ignore-axis]
(let [bounds (gpr/bounds->rect points)
corner (gpt/point bounds)
target-corner (gpt/round-step corner precision)
target-corner
(cond-> corner
(= ignore-axis :x)
(update :y mth/round precision)
(= ignore-axis :y)
(update :x mth/round precision)
(nil? ignore-axis)
(gpt/round-step precision))
deltav (gpt/to-vec corner target-corner)]
(ctm/move modifiers deltav)))
(defn set-pixel-precision
"Adjust modifiers so they adjust to the pixel grid"
[modifiers shape precision]
[modifiers shape precision ignore-axis]
(let [points (-> shape :points (gco/transform-points (ctm/modifiers->transform modifiers)))
has-resize? (not (ctm/only-move? modifiers))
@ -63,16 +72,16 @@
(gco/transform-points (ctm/modifiers->transform modifiers)) )
points)]
[modifiers points])]
(position-pixel-precision modifiers shape points precision)))
(position-pixel-precision modifiers shape points precision ignore-axis)))
(defn adjust-pixel-precision
[modif-tree objects precision]
[modif-tree objects precision ignore-axis]
(let [update-modifiers
(fn [modif-tree shape]
(let [modifiers (dm/get-in modif-tree [(:id shape) :modifiers])]
(cond-> modif-tree
(ctm/has-geometry? modifiers)
(update-in [(:id shape) :modifiers] set-pixel-precision shape precision))))]
(update-in [(:id shape) :modifiers] set-pixel-precision shape precision ignore-axis))))]
(->> (keys modif-tree)
(map (d/getf objects))

View file

@ -6,6 +6,7 @@
(ns app.common.types.shape.layout
(:require
[app.common.data.macros :as dm]
[app.common.spec :as us]
[clojure.spec.alpha :as s]))
@ -99,15 +100,21 @@
([shape]
(and (= :frame (:type shape)) (= :flex (:layout shape)))))
(defn layout-child? [objects shape]
(defn layout-immediate-child? [objects shape]
(let [parent-id (:parent-id shape)
parent (get objects parent-id)]
(layout? parent)))
(defn layout-immediate-child-id? [objects id]
(let [parent-id (dm/get-in objects [id :parent-id])
parent (get objects parent-id)]
(layout? parent)))
(defn layout-descent? [objects shape]
(let [frame-id (:frame-id shape)
frame (get objects frame-id)]
(layout? frame)))
(defn layout-child-id? [objects id]
(let [shape (get objects id)]
(layout-child? objects shape)))
(defn inside-layout?
"Check if the shape is inside a layout"
[objects shape]

View file

@ -846,8 +846,8 @@
:text
(rx/of (dwe/start-edition-mode id))
(:group :bool)
(rx/of (dws/select-shapes (into (d/ordered-set) [(last shapes)])))
(:group :bool :frame)
(rx/of (dws/select-shapes (into (d/ordered-set) shapes)))
:svg-raw
nil

View file

@ -219,7 +219,7 @@
result
(let [[id text-modifier] (first modifiers)]
(recur (rest modifiers)
(update objects id apply-text-modifier text-modifier))))))
(update result id apply-text-modifier text-modifier))))))
#_(defn apply-path-modifiers
[objects path-modifiers]
@ -241,6 +241,9 @@
(calculate-modifiers state false false modif-tree))
([state ignore-constraints ignore-snap-pixel modif-tree]
(calculate-modifiers state ignore-constraints ignore-snap-pixel modif-tree nil))
([state ignore-constraints ignore-snap-pixel modif-tree params]
(let [objects
(wsh/lookup-page-objects state)
@ -253,7 +256,11 @@
(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 ignore-constraints :snap-pixel? snap-pixel? :snap-precision snap-precision})))))
(gsh/set-objects-modifiers modif-tree $ (merge
params
{:ignore-constraints ignore-constraints
:snap-pixel? snap-pixel?
:snap-precision snap-precision}))))))
(defn- calculate-update-modifiers
[old-modif-tree state ignore-constraints ignore-snap-pixel modif-tree]
@ -292,10 +299,13 @@
(set-modifiers modif-tree ignore-constraints false))
([modif-tree ignore-constraints ignore-snap-pixel]
(set-modifiers modif-tree ignore-constraints ignore-snap-pixel nil))
([modif-tree ignore-constraints ignore-snap-pixel params]
(ptk/reify ::set-modifiers
ptk/UpdateEvent
(update [_ state]
(assoc state :workspace-modifiers (calculate-modifiers state ignore-constraints ignore-snap-pixel modif-tree))))))
(assoc state :workspace-modifiers (calculate-modifiers state ignore-constraints ignore-snap-pixel modif-tree params))))))
;; Rotation use different algorithm to calculate children modifiers (and do not use child constraints).
(defn set-rotation-modifiers

View file

@ -279,11 +279,14 @@
state)))
ptk/WatchEvent
(watch [_ _ _]
(rx/of (setup-frame-path)
(dwdc/handle-finish-drawing)
(dwe/start-edition-mode shape-id)
(change-edit-mode :draw)))))
(watch [_ state _]
(let [content (get-in state [:workspace-drawing :object :content] [])]
(if (seq content)
(rx/of (setup-frame-path)
(dwdc/handle-finish-drawing)
(dwe/start-edition-mode shape-id)
(change-edit-mode :draw))
(rx/of (dwdc/handle-finish-drawing)))))))
(defn handle-new-shape
"Creates a new path shape"

View file

@ -15,7 +15,6 @@
[app.common.pages.helpers :as cph]
[app.common.text :as txt]
[app.common.types.modifiers :as ctm]
[app.common.uuid :as uuid]
[app.main.data.workspace.changes :as dch]
[app.main.data.workspace.common :as dwc]
[app.main.data.workspace.modifiers :as dwm]
@ -318,16 +317,25 @@
(defn not-changed? [old-dim new-dim]
(> (mth/abs (- old-dim new-dim)) 1))
(defn resize-text
[id new-width new-height]
(ptk/reify ::resize-text
(defn commit-resize-text
[]
(ptk/reify ::commit-resize-text
ptk/WatchEvent
(watch [_ state _]
(let [shape (wsh/lookup-shape state id)
(let [props (::resize-text-debounce-props state)
objects (wsh/lookup-page-objects state)
undo-id (js/Symbol)]
(letfn [(update-fn [shape]
(let [{:keys [selrect grow-type]} shape
{shape-width :width shape-height :height} selrect
(letfn [(changed-text? [id]
(let [shape (get objects id)
[new-width new-height] (get props id)]
(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))))))
(update-fn [{:keys [id selrect grow-type] :as shape}]
(let [{shape-width :width shape-height :height} selrect
[new-width new-height] (get props id)
shape
(cond-> shape
@ -342,14 +350,39 @@
shape))]
(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))))
(let [ids (->> (keys props) (filter changed-text?))]
(rx/of (dwu/start-undo-transaction undo-id)
(dch/update-shapes [id] update-fn {:reg-objects? true :save-undo? true})
(ptk/data-event :layout/update [id])
(dch/update-shapes ids update-fn {:reg-objects? true :save-undo? true})
(ptk/data-event :layout/update ids)
(dwu/commit-undo-transaction undo-id))))))))
(defn resize-text
[id new-width new-height]
(let [cur-event (js/Symbol)]
(ptk/reify ::resize-text
ptk/UpdateEvent
(update [_ state]
(-> state
(update ::resize-text-debounce-props (fnil assoc {}) id [new-width new-height])
(cond-> (nil? (::resize-text-debounce-event state))
(assoc ::resize-text-debounce-event cur-event))))
ptk/WatchEvent
(watch [_ state stream]
(if (= (::resize-text-debounce-event state) cur-event)
(let [stopper (->> stream (rx/filter (ptk/type? :app.main.data.workspace/finalize)))]
(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 #(dissoc % ::resize-text-debounce-props ::resize-text-debounce-event))))
(rx/empty))))))
(defn save-font
[data]
@ -384,37 +417,43 @@
new-shape))
(defn update-text-modifier-state
[id props]
(ptk/reify ::update-text-modifier-state
ptk/UpdateEvent
(update [_ state]
(update-in state [:workspace-text-modifier id] (fnil merge {}) props))))
(defn commit-update-text-modifier
[]
(ptk/reify ::commit-update-text-modifier
ptk/WatchEvent
(watch [_ state _]
(let [ids (::update-text-modifier-debounce-ids state)]
(let [modif-tree (dwm/create-modif-tree ids (ctm/reflow-modifiers))]
(rx/of (dwm/update-modifiers modif-tree false true)))))))
(defn update-text-modifier
[id props]
(ptk/reify ::update-text-modifier
ptk/WatchEvent
(watch [_ state _]
(let [modifiers (get-in (:workspace-modifiers state) [id :modifiers])
shape (-> (wsh/lookup-shape state id)
(gsh/transform-shape modifiers))
(let [cur-event (js/Symbol)]
(ptk/reify ::update-text-modifier
ptk/UpdateEvent
(update [_ state]
(-> state
(update-in [:workspace-text-modifier id] (fnil merge {}) props)
(update ::update-text-modifier-debounce-ids (fnil conj #{}) id)
(cond-> (nil? (::update-text-modifier-debounce-event state))
(assoc ::update-text-modifier-debounce-event cur-event))))
current-width (:width shape)
current-height (:height shape)]
(rx/concat
(rx/of (update-text-modifier-state id props))
(if (or (and (some? (:width props))
(not (mth/close? (:width props) current-width)))
(and (some? (:height props))
(not (mth/close? (:height props) current-height))))
(let [modif-tree (dwm/create-modif-tree [id] (ctm/reflow-modifiers))]
(->> (rx/of (dwm/update-modifiers modif-tree false true))
(rx/observe-on :async)))
(rx/empty)))))))
ptk/WatchEvent
(watch [_ state stream]
(if (= (::update-text-modifier-debounce-event state) cur-event)
(let [stopper (->> stream (rx/filter (ptk/type? :app.main.data.workspace/finalize)))]
(rx/concat
(rx/merge
(->> stream
(rx/filter (ptk/type? ::update-text-modifier))
(rx/debounce 50)
(rx/take 1)
(rx/map #(commit-update-text-modifier))
(rx/take-until stopper))
(rx/of (update-text-modifier id props)))
(rx/of #(dissoc % ::update-text-modifier-debounce-event ::update-text-modifier-debounce-ids))))
(rx/empty))))))
(defn clean-text-modifier
[id]
@ -464,18 +503,18 @@
(defn update-position-data
[id position-data]
(let [start (uuid/next)]
(let [cur-event (js/Symbol)]
(ptk/reify ::update-position-data
ptk/UpdateEvent
(update [_ state]
(let [state (assoc-in state [:workspace-text-modifier id :position-data] position-data)]
(if (nil? (::update-position-data-debounce state))
(assoc state ::update-position-data-debounce start)
(assoc state ::update-position-data-debounce cur-event)
(assoc-in state [::update-position-data id] position-data))))
ptk/WatchEvent
(watch [_ state stream]
(if (= (::update-position-data-debounce state) start)
(if (= (::update-position-data-debounce state) cur-event)
(let [stopper (->> stream (rx/filter (ptk/type? :app.main.data.workspace/finalize)))]
(rx/merge
(->> stream

View file

@ -440,21 +440,11 @@
exclude-frames-siblings
(into exclude-frames
(comp (mapcat (partial cph/get-siblings-ids objects))
(filter (partial ctl/layout-child-id? objects)))
(filter (partial ctl/layout-immediate-child-id? objects)))
selected)
fix-axis
(fn [[position shift?]]
(let [delta (gpt/to-vec from-position position)]
(if shift?
(if (> (mth/abs (:x delta)) (mth/abs (:y delta)))
(gpt/point (:x delta) 0)
(gpt/point 0 (:y delta)))
delta)))
position (->> ms/mouse-position
(rx/with-latest-from ms/mouse-position-shift)
(rx/map #(fix-axis %)))
(rx/map #(gpt/to-vec from-position %)))
snap-delta (rx/concat
;; We send the nil first so the stream is not waiting for the first value
@ -491,11 +481,24 @@
(rx/merge
;; Temporary modifiers stream
(->> move-stream
(rx/with-latest-from ms/mouse-position-shift)
(rx/map
(fn [[move-vector target-frame drop-index]]
(-> (dwm/create-modif-tree ids (ctm/move-modifiers move-vector))
(dwm/build-change-frame-modifiers objects selected target-frame drop-index)
(dwm/set-modifiers)))))
(fn [[[move-vector target-frame drop-index] shift?]]
(let [x-disp? (> (mth/abs (:x move-vector)) (mth/abs (:y move-vector)))
[move-vector snap-ignore-axis]
(cond
(and shift? x-disp?)
[(assoc move-vector :y 0) :y]
shift?
[(assoc move-vector :x 0) :x]
:else
[move-vector nil])]
(-> (dwm/create-modif-tree ids (ctm/move-modifiers move-vector))
(dwm/build-change-frame-modifiers objects selected target-frame drop-index)
(dwm/set-modifiers false false {:snap-ignore-axis snap-ignore-axis}))))))
(->> move-stream
(rx/map (comp set-ghost-displacement first)))
@ -640,7 +643,7 @@
(let [objects (wsh/lookup-page-objects state)
selected (wsh/lookup-selected state {:omit-blocked? true})
selected-shapes (->> selected (map (d/getf objects)))]
(if (every? (partial ctl/layout-child? objects) selected-shapes)
(if (every? (partial ctl/layout-immediate-child-id? objects) selected-shapes)
(rx/of (reorder-selected-layout-child direction))
(rx/of (nudge-selected-shapes direction shift?)))))))
@ -750,36 +753,22 @@
(ptk/reify ::flip-horizontal-selected
ptk/WatchEvent
(watch [_ state _]
(let [objects (wsh/lookup-page-objects state)
selected (wsh/lookup-selected state {:omit-blocked? true})
shapes (map #(get objects %) selected)
selrect (gsh/selection-rect shapes)
origin (gpt/point (:x selrect) (+ (:y selrect) (/ (:height selrect) 2)))
modif-tree (dwm/create-modif-tree
selected
(-> (ctm/empty)
(ctm/resize (gpt/point -1.0 1.0) origin)
(ctm/move (gpt/point (:width selrect) 0))))]
(rx/of (dwm/set-modifiers modif-tree true)
(dwm/apply-modifiers))))))
(let [objects (wsh/lookup-page-objects state)
selected (wsh/lookup-selected state {:omit-blocked? true})
shapes (map #(get objects %) selected)
selrect (gsh/selection-rect shapes)
center (gsh/center-selrect selrect)
modifiers (dwm/create-modif-tree selected (ctm/resize-modifiers (gpt/point -1.0 1.0) center))]
(rx/of (dwm/apply-modifiers {:modifiers modifiers}))))))
(defn flip-vertical-selected []
(ptk/reify ::flip-vertical-selected
ptk/WatchEvent
(watch [_ state _]
(let [objects (wsh/lookup-page-objects state)
selected (wsh/lookup-selected state {:omit-blocked? true})
shapes (map #(get objects %) selected)
selrect (gsh/selection-rect shapes)
origin (gpt/point (+ (:x selrect) (/ (:width selrect) 2)) (:y selrect))
modif-tree (dwm/create-modif-tree
selected
(-> (ctm/empty)
(ctm/resize (gpt/point 1.0 -1.0) origin)
(ctm/move (gpt/point 0 (:height selrect)))))]
(rx/of (dwm/set-modifiers modif-tree true)
(dwm/apply-modifiers))))))
(let [objects (wsh/lookup-page-objects state)
selected (wsh/lookup-selected state {:omit-blocked? true})
shapes (map #(get objects %) selected)
selrect (gsh/selection-rect shapes)
center (gsh/center-selrect selrect)
modifiers (dwm/create-modif-tree selected (ctm/resize-modifiers (gpt/point 1.0 -1.0) center))]
(rx/of (dwm/apply-modifiers {:modifiers modifiers}))))))

View file

@ -402,7 +402,7 @@
(let [objects (wsh/lookup-page-objects state)]
(into []
(comp (map (d/getf objects))
(filter (partial ctl/layout-child? objects)))
(filter (partial ctl/layout-immediate-child? objects)))
ids)))
st/state =))
@ -481,7 +481,7 @@
(fn [objects]
(->> ids
(map (d/getf objects))
(some (partial ctl/layout-child? objects))))
(some (partial ctl/layout-immediate-child? objects))))
workspace-page-objects))
(defn get-flex-child-viewer
@ -491,7 +491,7 @@
(let [objects (wsh/lookup-viewer-objects state page-id)]
(into []
(comp (map (d/getf objects))
(filter (partial ctl/layout-child? objects)))
(filter (partial ctl/layout-immediate-child? objects)))
ids)))
st/state =))

View file

@ -183,6 +183,9 @@
(dom/remove-attribute! node "data-old-width")
(dom/remove-attribute! node "data-old-height"))
(dom/class? node "frame-title")
(dom/remove-attribute! node "data-old-transform")
:else
(let [old-transform (dom/get-attribute node "data-old-transform")]
(if (some? old-transform)

View file

@ -82,7 +82,7 @@
grid-y-data (get-grids-snap-points frame :y)]
(cond-> page-data
(not (ctl/layout-child? objects frame))
(not (ctl/layout-descent? objects frame))
(-> ;; Update root frame information
(assoc-in [uuid/zero :objects-data frame-id] frame-data)
@ -106,7 +106,7 @@
:id (:id shape)
:pt %)))]
(cond-> page-data
(not (ctl/layout-child? objects shape))
(not (ctl/layout-descent? objects shape))
(-> (assoc-in [frame-id :objects-data (:id shape)] shape-data)
(update-in [frame-id :x] (make-insert-tree-data shape-data :x))
(update-in [frame-id :y] (make-insert-tree-data shape-data :y))))))