0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-03 21:09:00 -05:00

Move frames with guides move the guides

This commit is contained in:
alonso.torres 2022-01-24 11:46:04 +01:00
parent 3f89baa1fe
commit 62f7323acf
5 changed files with 122 additions and 105 deletions

View file

@ -6,7 +6,10 @@
(ns app.main.data.workspace.guides
(:require
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[app.common.spec :as us]
[app.common.types.page-options :as tpo]
[app.main.data.workspace.changes :as dwc]
[app.main.data.workspace.state-helpers :as wsh]
[beicon.core :as rx]
@ -20,7 +23,7 @@
(merge guide))))
(defn update-guides [guide]
;; TODO CHECK SPEC
(us/verify ::tpo/guide guide)
(ptk/reify ::update-guides
ptk/WatchEvent
(watch [it state _]
@ -44,7 +47,7 @@
:origin it}))))))
(defn remove-guide [guide]
;; TODO CHECK SPEC
(us/verify ::tpo/guide guide)
(ptk/reify ::remove-guide
ptk/WatchEvent
(watch [it state _]
@ -75,26 +78,24 @@
(watch [_ state _]
(let [objects (wsh/lookup-page-objects state)
frame-ids (->> ids (filter #(= :frame (get-in objects [% :type]))) (into #{}))
frame-ids? (->> ids (filter #(= :frame (get-in objects [% :type]))) (into #{}))
object-modifiers (get state :workspace-modifiers)
moved-guide?
(fn [guide]
(let [frame-id (:frame-id guide)]
(and (contains? frame-ids frame-id)
(some? (get-in object-modifiers [frame-id :modifiers :displacement])))))
build-move-event
(fn [guide]
(let [disp (get-in object-modifiers [(:frame-id guide) :modifiers :displacement])
guide (if (= :x (:axis guide))
(update guide :position + (:e disp))
(update guide :position + (:f disp)))]
(let [frame (get objects (:frame-id guide))
frame' (-> (merge frame (get object-modifiers (:frame-id guide)))
(gsh/transform-shape))
moved (gpt/to-vec (gpt/point (:x frame) (:y frame))
(gpt/point (:x frame') (:y frame')))
guide (update guide :position + (get moved (:axis guide)))]
(update-guides guide)))]
(->> (wsh/lookup-page-options state)
:guides
(vals)
(filter moved-guide?)
(filter (comp frame-ids? :frame-id))
(map build-move-event)
(rx/from))))))

View file

@ -171,6 +171,7 @@
ignore-tree (get-ignore-tree object-modifiers objects ids)]
(rx/of (dwu/start-undo-transaction)
(dwg/move-frame-guides ids-with-children)
(dch/update-shapes
ids-with-children
(fn [shape]
@ -189,7 +190,6 @@
:rotation
:flip-x
:flip-y]})
(dwg/move-frame-guides ids-with-children)
(clear-local-transform)
(dwu/commit-undo-transaction))))))

View file

@ -158,7 +158,7 @@
(mf/defc export-guides
[{:keys [guides]}]
[:> "penpot:guides" #js {}
(for [{:keys [id position frame-id axis]} (vals guides)]
(for [{:keys [position frame-id axis]} (vals guides)]
[:> "penpot:guide" #js {:position position
:frame-id frame-id
:axis (d/name axis)}])])

View file

@ -370,5 +370,6 @@
{:zoom zoom
:vbox vbox
:hover-frame frame-parent
:modifiers modifiers
:disabled-guides? disabled-guides?}]])]]]))

View file

@ -7,6 +7,8 @@
(ns app.main.ui.workspace.viewport.guides
(:require
[app.common.colors :as colors]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[app.common.math :as mth]
[app.common.uuid :as uuid]
[app.main.data.workspace :as dw]
@ -137,11 +139,7 @@
{:x (+ (:x vbox) rules-pos)
:y (- pos (/ guide-active-area 2))
:width (:width vbox)
:height guide-active-area}
)))
:height guide-active-area})))
(defn guide-line-axis
([pos vbox axis]
@ -232,9 +230,19 @@
:width (:width vbox)
:height (/ 24 zoom)}))
(defn is-guide-inside-frame?
[guide frame]
(if (= :x (:axis guide))
(and (>= (:position guide) (:x frame) )
(<= (:position guide) (+ (:x frame) (:width frame)) ))
(and (>= (:position guide) (:y frame) )
(<= (:position guide) (+ (:y frame) (:height frame)) ))))
(mf/defc guide
{::mf/wrap [mf/memo]}
[{:keys [guide hover? on-guide-change get-hover-frame vbox zoom hover-frame disabled-guides?]}]
[{:keys [guide hover? on-guide-change get-hover-frame vbox zoom hover-frame disabled-guides? frame-modifier]}]
(let [axis (:axis guide)
@ -254,11 +262,17 @@
state
frame]} (use-guide handle-change-position get-hover-frame zoom guide)
frame (or frame hover-frame)
pos (or (:new-position @state) (:position guide))
base-frame (or frame hover-frame)
frame (gsh/transform-shape (merge base-frame frame-modifier))
move-vec (gpt/to-vec (gpt/point (:x base-frame) (:y base-frame))
(gpt/point (:x frame) (:y frame)))
pos (+ (or (:new-position @state) (:position guide)) (get move-vec axis))
guide-width (/ guide-width zoom)
guide-pill-corner-radius (/ guide-pill-corner-radius zoom)]
(when (or (nil? frame) (is-guide-inside-frame? (assoc guide :position pos) frame))
[:g.guide-area {:data-guide-frame-id (when (some? frame) (str (:id frame)))}
(when-not disabled-guides?
(let [{:keys [x y width height]} (guide-area-axis pos vbox zoom frame axis)]
@ -343,7 +357,7 @@
:style {:font-size (/ 13 zoom)
:font-family "sourcesanspro"
:fill colors/black}}
(str (mth/round pos))]]))]))
(str (mth/round pos))]]))])))
(mf/defc new-guide-area
[{:keys [vbox zoom axis get-hover-frame disabled-guides?]}]
@ -395,7 +409,7 @@
(mf/defc viewport-guides
{::mf/wrap [mf/memo]}
[{:keys [zoom vbox hover-frame disabled-guides?]}]
[{:keys [zoom vbox hover-frame disabled-guides? modifiers]}]
(let [page (mf/deref refs/workspace-page)
@ -445,6 +459,7 @@
:guide current
:vbox vbox
:zoom zoom
:frame-modifier (get modifiers (:frame-id current))
:get-hover-frame get-hover-frame
:on-guide-change on-guide-change
:disabled-guides? disabled-guides?}])]))