♻️ Refactor geom/shapes.cljc

This commit is contained in:
alonso.torres 2020-11-06 11:39:32 +01:00
parent 10a24d68c9
commit 2c50bb16dc
17 changed files with 781 additions and 634 deletions

View file

@ -13,7 +13,8 @@
[app.common.exceptions :as ex]
[app.common.geom.matrix :as gmt]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as geom]
[app.common.geom.shapes :as gsh]
[app.common.geom.align :as gal]
[app.common.math :as mth]
[app.common.pages :as cp]
[app.common.pages-helpers :as cph]
@ -339,7 +340,7 @@
(let [page-id (:current-page-id state)
objects (dwc/lookup-page-objects state page-id)
shapes (cph/select-toplevel-shapes objects {:include-frames? true})
srect (geom/selection-rect shapes)
srect (gsh/selection-rect shapes)
local (assoc local :vport size :zoom 1)]
(cond
(or (not (mth/finite? (:width srect)))
@ -348,7 +349,7 @@
(or (> (:width srect) width)
(> (:height srect) height))
(let [srect (geom/adjust-to-viewport size srect {:padding 40})
(let [srect (gal/adjust-to-viewport size srect {:padding 40})
zoom (/ (:width size) (:width srect))]
(-> local
(assoc :zoom zoom)
@ -471,10 +472,10 @@
(let [vbox (update vbox :x + (:left-offset vbox))
new-zoom (if (fn? zoom) (zoom (:zoom local)) zoom)
old-zoom (:zoom local)
center (if center center (geom/center vbox))
center (if center center (gsh/center vbox))
scale (/ old-zoom new-zoom)
mtx (gmt/scale-matrix (gpt/point scale) center)
vbox' (geom/transform vbox mtx)
vbox' (gsh/transform vbox mtx)
vbox' (update vbox' :x - (:left-offset vbox))]
(-> local
(assoc :zoom new-zoom)
@ -510,14 +511,14 @@
(let [page-id (:current-page-id state)
objects (dwc/lookup-page-objects state page-id)
shapes (cph/select-toplevel-shapes objects {:include-frames? true})
srect (geom/selection-rect shapes)]
srect (gsh/selection-rect shapes)]
(if (or (mth/nan? (:width srect))
(mth/nan? (:height srect)))
state
(update state :workspace-local
(fn [{:keys [vbox vport] :as local}]
(let [srect (geom/adjust-to-viewport vport srect {:padding 40})
(let [srect (gal/adjust-to-viewport vport srect {:padding 40})
zoom (/ (:width vport) (:width srect))]
(-> local
(assoc :zoom zoom)
@ -534,10 +535,10 @@
objects (dwc/lookup-page-objects state page-id)
srect (->> selected
(map #(get objects %))
(geom/selection-rect))]
(gsh/selection-rect))]
(update state :workspace-local
(fn [{:keys [vbox vport] :as local}]
(let [srect (geom/adjust-to-viewport vport srect {:padding 40})
(let [srect (gal/adjust-to-viewport vport srect {:padding 40})
zoom (/ (:width vport) (:width srect))]
(-> local
(assoc :zoom zoom)
@ -614,8 +615,8 @@
(merge data)
(merge {:x x :y y})
(assoc :frame-id frame-id)
(geom/setup-selrect))]
(rx/of (add-shape shape))))))
(gsh/setup-selrect))]
;; --- Update Shape Attrs

View file

@ -44,7 +44,7 @@
(if (:click-draw? shape) :auto-width :fixed)))
shape (-> shape
gsh/transform-shape
(gsh/transform-shape)
(dissoc :initialized? :click-draw?))]
;; Add & select the created shape to the workspace
(rx/concat

View file

@ -17,6 +17,7 @@
[goog.object :as gobj]
[potok.core :as ptk]
[app.common.geom.shapes :as geom]
[app.common.attrs :as attrs]
[app.main.data.workspace.common :as dwc]
[app.main.fonts :as fonts]
[app.util.object :as obj]
@ -125,7 +126,7 @@
(map #(if (is-text-node? %)
(merge ut/default-text-attrs %)
%)))]
(geom/get-attrs-multi nodes attrs)))
(attrs/get-attrs-multi nodes attrs)))
(defn current-text-values
[{:keys [editor default attrs shape]}]

View file

@ -15,7 +15,8 @@
[app.common.pages :as cp]
[app.common.pages-helpers :as cph]
[app.common.math :as mth]
[app.common.geom.shapes :as geom]
[app.common.geom.shapes :as gsh]
[app.common.geom.align :as gal]
[app.common.geom.point :as gpt]
[app.common.geom.matrix :as gmt]
[app.main.ui.shapes.filters :as filters]
@ -42,9 +43,9 @@
(defn- calculate-dimensions
[{:keys [objects] :as data} vport]
(let [shapes (cph/select-toplevel-shapes objects {:include-frames? true})]
(->> (geom/selection-rect shapes)
(geom/adjust-to-viewport vport)
(geom/fix-invalid-rect-values))))
(->> (gsh/selection-rect shapes)
(gal/adjust-to-viewport vport)
(gsh/fix-invalid-rect-values))))
(declare shape-wrapper-factory)
@ -55,7 +56,7 @@
(mf/fnc frame-wrapper
[{:keys [shape] :as props}]
(let [childs (mapv #(get objects %) (:shapes shape))
shape (geom/transform-shape shape)]
shape (gsh/transform-shape shape)]
[:> shape-container {:shape shape}
[:& frame-shape {:shape shape :childs childs}]]))))
@ -78,7 +79,8 @@
(let [group-wrapper (mf/use-memo (mf/deps objects) #(group-wrapper-factory objects))
frame-wrapper (mf/use-memo (mf/deps objects) #(frame-wrapper-factory objects))]
(when (and shape (not (:hidden shape)))
(let [shape (geom/transform-shape frame shape)
(let [shape (-> (gsh/transform-shape shape)
(gsh/translate-to-frame frame))
opts #js {:shape shape}]
[:> shape-container {:shape shape}
(case (:type shape)

View file

@ -41,6 +41,11 @@
(and (mouse-event? v)
(= :click (:type v))))
(defn mouse-double-click?
[v]
(and (mouse-event? v)
(= :double-click (:type v))))
(defrecord PointerEvent [source pt ctrl shift alt])
(defn pointer-event?

View file

@ -122,7 +122,8 @@
(mf/deps objects)
#(group-container-factory objects))]
(when (and shape (not (:hidden shape)))
(let [shape (geom/transform-shape frame shape)
(let [shape (-> (geom/transform-shape shape)
(geom/translate-to-frame frame))
opts #js {:shape shape
:frame frame}]
(case (:type shape)

View file

@ -149,7 +149,8 @@
shape (unchecked-get props "shape")
frame (unchecked-get props "frame")]
(when (and shape (not (:hidden shape)))
(let [shape (geom/transform-shape frame shape)
(let [shape (-> (geom/transform-shape shape)
(geom/translate-to-frame frame))
opts #js {:shape shape}]
(case (:type shape)
:curve [:> path-wrapper opts]

View file

@ -82,7 +82,8 @@
(let [shape (unchecked-get props "shape")
frame (unchecked-get props "frame")
ghost? (unchecked-get props "ghost?")
shape (geom/transform-shape frame shape)
shape (-> (geom/transform-shape shape)
(geom/translate-to-frame frame))
opts #js {:shape shape
:frame frame}
alt? (mf/use-state false)

View file

@ -11,6 +11,7 @@
(ns app.main.ui.workspace.sidebar.options.group
(:require
[rumext.alpha :as mf]
[app.common.attrs :as attrs]
[app.common.geom.shapes :as geom]
[app.common.pages-helpers :as cph]
[app.main.refs :as refs]
@ -43,7 +44,7 @@
(merge
;; All values extracted from the group shape, except
;; border radius, that needs to be looked up from children
(geom/get-attrs-multi (map #(get-shape-attrs
(attrs/get-attrs-multi (map #(get-shape-attrs
%
measure-attrs
nil
@ -51,7 +52,7 @@
nil)
[shape])
measure-attrs)
(geom/get-attrs-multi (map #(get-shape-attrs
(attrs/get-attrs-multi (map #(get-shape-attrs
%
[:rx :ry]
nil
@ -64,10 +65,10 @@
(select-keys shape component-attrs)
fill-values
(geom/get-attrs-multi shape-with-children fill-attrs)
(attrs/get-attrs-multi shape-with-children fill-attrs)
stroke-values
(geom/get-attrs-multi (map #(get-shape-attrs
(attrs/get-attrs-multi (map #(get-shape-attrs
%
stroke-attrs
nil
@ -77,7 +78,7 @@
stroke-attrs)
font-values
(geom/get-attrs-multi (map #(get-shape-attrs
(attrs/get-attrs-multi (map #(get-shape-attrs
%
nil
text-font-attrs
@ -87,7 +88,7 @@
text-font-attrs)
align-values
(geom/get-attrs-multi (map #(get-shape-attrs
(attrs/get-attrs-multi (map #(get-shape-attrs
%
nil
text-align-attrs
@ -97,7 +98,7 @@
text-align-attrs)
spacing-values
(geom/get-attrs-multi (map #(get-shape-attrs
(attrs/get-attrs-multi (map #(get-shape-attrs
%
nil
text-spacing-attrs
@ -107,7 +108,7 @@
text-spacing-attrs)
valign-values
(geom/get-attrs-multi (map #(get-shape-attrs
(attrs/get-attrs-multi (map #(get-shape-attrs
%
nil
text-valign-attrs
@ -117,7 +118,7 @@
text-valign-attrs)
decoration-values
(geom/get-attrs-multi (map #(get-shape-attrs
(attrs/get-attrs-multi (map #(get-shape-attrs
%
nil
text-decoration-attrs
@ -127,7 +128,7 @@
text-decoration-attrs)
transform-values
(geom/get-attrs-multi (map #(get-shape-attrs
(attrs/get-attrs-multi (map #(get-shape-attrs
%
nil
text-transform-attrs

View file

@ -11,6 +11,7 @@
(:require
[rumext.alpha :as mf]
[app.common.geom.shapes :as geom]
[app.common.attrs :as attrs]
[app.main.data.workspace.texts :as dwt]
[app.main.ui.workspace.sidebar.options.measures :refer [measure-attrs measures-menu]]
[app.main.ui.workspace.sidebar.options.fill :refer [fill-attrs fill-menu]]
@ -48,9 +49,9 @@
text-attrs
convert-attrs
extract-fn))]
(geom/get-attrs-multi (map mapfn shapes) (or attrs text-attrs))))
(attrs/get-attrs-multi (map mapfn shapes) (or attrs text-attrs))))
measure-values (geom/get-attrs-multi shapes measure-attrs)
measure-values (attrs/get-attrs-multi shapes measure-attrs)
fill-values (extract {:attrs fill-attrs
:text-attrs ot/text-fill-attrs