mirror of
https://github.com/penpot/penpot.git
synced 2025-08-03 21:38:23 +02:00
✨ Renamed modifiers functions
This commit is contained in:
parent
b5df7bbfc5
commit
a2e26210d1
17 changed files with 201 additions and 200 deletions
|
@ -152,7 +152,7 @@
|
|||
end-angl (gpt/angle-with-other end-before end-after)
|
||||
target-end (if (mth/close? end-angl 180) (- (gpt/length end-before)) (gpt/length end-before))
|
||||
disp-vector-end (gpt/subtract end-after (gpt/scale (gpt/unit end-after) target-end))]
|
||||
(ctm/move disp-vector-end)))
|
||||
(ctm/move-modifiers disp-vector-end)))
|
||||
|
||||
(defmethod constraint-modifier :fixed
|
||||
[_ axis child-points-before parent-points-before child-points-after parent-points-after transformed-parent]
|
||||
|
@ -174,9 +174,8 @@
|
|||
resize-angl (gpt/angle-with-other before-vec after-vec)
|
||||
resize-sign (if (mth/close? resize-angl 180) -1 1)
|
||||
|
||||
scale (* resize-sign (/ (gpt/length after-vec) (gpt/length before-vec)))
|
||||
]
|
||||
(ctm/resize (get-scale axis scale) c0 (:transform transformed-parent) (:transform-inverse transformed-parent))))
|
||||
scale (* resize-sign (/ (gpt/length after-vec) (gpt/length before-vec)))]
|
||||
(ctm/resize-modifiers (get-scale axis scale) c0 (:transform transformed-parent) (:transform-inverse transformed-parent))))
|
||||
|
||||
(defmethod constraint-modifier :center
|
||||
[_ axis child-points-before parent-points-before child-points-after parent-points-after]
|
||||
|
@ -185,7 +184,7 @@
|
|||
center-angl (gpt/angle-with-other center-before center-after)
|
||||
target-center (if (mth/close? center-angl 180) (- (gpt/length center-before)) (gpt/length center-before))
|
||||
disp-vector-center (gpt/subtract center-after (gpt/scale (gpt/unit center-after) target-center))]
|
||||
(ctm/move disp-vector-center)))
|
||||
(ctm/move-modifiers disp-vector-center)))
|
||||
|
||||
(defmethod constraint-modifier :default [_ _ _ _ _]
|
||||
[])
|
||||
|
@ -242,10 +241,10 @@
|
|||
|
||||
(cond-> modifiers
|
||||
(not= :scale constraints-h)
|
||||
(ctm/set-resize (gpt/point scale-x 1) resize-origin transform transform-inverse)
|
||||
(ctm/resize (gpt/point scale-x 1) resize-origin transform transform-inverse)
|
||||
|
||||
(not= :scale constraints-v)
|
||||
(ctm/set-resize (gpt/point 1 scale-y) resize-origin transform transform-inverse))))
|
||||
(ctm/resize (gpt/point 1 scale-y) resize-origin transform transform-inverse))))
|
||||
|
||||
(defn calc-child-modifiers
|
||||
[parent child modifiers ignore-constraints transformed-parent]
|
||||
|
|
|
@ -23,11 +23,11 @@
|
|||
scale-x (/ (:width child-bb-before) (:width child-bb-after))
|
||||
scale-y (/ (:height child-bb-before) (:height child-bb-after))
|
||||
|
||||
resize-origin (-> transformed-parent :points first) ;; TODO LAYOUT: IS always the origin?n
|
||||
resize-origin (-> transformed-parent :points first) ;; TODO LAYOUT: IS always the origin
|
||||
resize-vector (gpt/point scale-x scale-y)]
|
||||
(-> modifiers
|
||||
(ctm/select-child-modifiers)
|
||||
(ctm/set-resize resize-vector resize-origin transform transform-inverse))))
|
||||
(ctm/resize resize-vector resize-origin transform transform-inverse))))
|
||||
|
||||
(defn calc-fill-width-data
|
||||
"Calculates the size and modifiers for the width of an auto-fill child"
|
||||
|
@ -41,7 +41,7 @@
|
|||
(let [target-width (max (get-in children-data [(:id child) :child-width]) 0.01)
|
||||
fill-scale (/ target-width child-width)]
|
||||
{:width target-width
|
||||
:modifiers (ctm/resize (gpt/point fill-scale 1) child-origin transform transform-inverse)})
|
||||
:modifiers (ctm/resize-modifiers (gpt/point fill-scale 1) child-origin transform transform-inverse)})
|
||||
|
||||
(ctl/col? parent)
|
||||
(let [target-width (max (- line-width (ctl/child-width-margin child)) 0.01)
|
||||
|
@ -49,7 +49,7 @@
|
|||
target-width (min max-width target-width)
|
||||
fill-scale (/ target-width child-width)]
|
||||
{:width target-width
|
||||
:modifiers (ctm/resize (gpt/point fill-scale 1) child-origin transform transform-inverse)})))
|
||||
:modifiers (ctm/resize-modifiers (gpt/point fill-scale 1) child-origin transform transform-inverse)})))
|
||||
|
||||
(defn calc-fill-height-data
|
||||
"Calculates the size and modifiers for the height of an auto-fill child"
|
||||
|
@ -63,7 +63,7 @@
|
|||
(let [target-height (max (get-in children-data [(:id child) :child-height]) 0.01)
|
||||
fill-scale (/ target-height child-height)]
|
||||
{:height target-height
|
||||
:modifiers (ctm/resize (gpt/point 1 fill-scale) child-origin transform transform-inverse)})
|
||||
:modifiers (ctm/resize-modifiers (gpt/point 1 fill-scale) child-origin transform transform-inverse)})
|
||||
|
||||
(ctl/row? parent)
|
||||
(let [target-height (max (- line-height (ctl/child-height-margin child)) 0.01)
|
||||
|
@ -71,7 +71,7 @@
|
|||
target-height (min max-height target-height)
|
||||
fill-scale (/ target-height child-height)]
|
||||
{:height target-height
|
||||
:modifiers (ctm/resize (gpt/point 1 fill-scale) child-origin transform transform-inverse)})))
|
||||
:modifiers (ctm/resize-modifiers (gpt/point 1 fill-scale) child-origin transform transform-inverse)})))
|
||||
|
||||
(defn layout-child-modifiers
|
||||
"Calculates the modifiers for the layout"
|
||||
|
@ -93,9 +93,9 @@
|
|||
move-vec (gpt/to-vec child-origin corner-p)
|
||||
|
||||
modifiers
|
||||
(-> (ctm/empty-modifiers)
|
||||
(-> (ctm/empty)
|
||||
(cond-> fill-width (ctm/add-modifiers (:modifiers fill-width)))
|
||||
(cond-> fill-height (ctm/add-modifiers (:modifiers fill-height)))
|
||||
(ctm/set-move move-vec))]
|
||||
(ctm/move move-vec))]
|
||||
|
||||
[modifiers layout-line]))
|
||||
|
|
|
@ -98,7 +98,7 @@
|
|||
(let [child-modifiers (gct/calc-child-modifiers parent child modifiers ignore-constraints transformed-parent)
|
||||
child-modifiers (cond-> child-modifiers snap-pixel? (gpp/set-pixel-precision child))]
|
||||
(cond-> modif-tree
|
||||
(not (ctm/empty-modifiers? child-modifiers))
|
||||
(not (ctm/empty? child-modifiers))
|
||||
(update-in [(:id child) :modifiers] ctm/add-modifiers child-modifiers))))]
|
||||
|
||||
(reduce set-child modif-tree children)))
|
||||
|
@ -111,7 +111,7 @@
|
|||
(ctm/select-child-geometry-modifiers)
|
||||
(gcl/normalize-child-modifiers parent child transformed-parent))]
|
||||
(cond-> modif-tree
|
||||
(not (ctm/empty-modifiers? child-modifiers))
|
||||
(not (ctm/empty? child-modifiers))
|
||||
(update-in [(:id child) :modifiers] ctm/add-modifiers child-modifiers))))]
|
||||
(let [children (map (d/getf objects) (:shapes transformed-parent))]
|
||||
(reduce process-child modif-tree children))))
|
||||
|
@ -178,14 +178,14 @@
|
|||
(let [origin (-> parent :points first)
|
||||
scale-width (/ auto-width (-> parent :selrect :width) )]
|
||||
(-> modifiers
|
||||
(ctm/set-resize-parent (gpt/point scale-width 1) origin (:transform parent) (:transform-inverse parent)))))
|
||||
(ctm/resize-parent (gpt/point scale-width 1) origin (:transform parent) (:transform-inverse parent)))))
|
||||
|
||||
(set-parent-auto-height
|
||||
[modifiers parent auto-height]
|
||||
(let [origin (-> parent :points first)
|
||||
scale-height (/ auto-height (-> parent :selrect :height) )]
|
||||
(-> modifiers
|
||||
(ctm/set-resize-parent (gpt/point 1 scale-height) origin (:transform parent) (:transform-inverse parent)))))]
|
||||
(ctm/resize-parent (gpt/point 1 scale-height) origin (:transform parent) (:transform-inverse parent)))))]
|
||||
|
||||
(let [modifiers (get-in modif-tree [(:id parent) :modifiers])
|
||||
children (->> parent
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
ratio-height (/ target-height curr-height)
|
||||
scalev (gpt/point ratio-width ratio-height)]
|
||||
(-> modifiers
|
||||
(ctm/set-resize scalev origin transform transform-inverse))))
|
||||
(ctm/resize scalev origin transform transform-inverse))))
|
||||
|
||||
(defn position-pixel-precision
|
||||
[modifiers shape]
|
||||
|
@ -42,7 +42,7 @@
|
|||
target-corner (gpt/round corner)
|
||||
deltav (gpt/to-vec corner target-corner)]
|
||||
(-> modifiers
|
||||
(ctm/set-move deltav))))
|
||||
(ctm/move deltav))))
|
||||
|
||||
(defn set-pixel-precision
|
||||
"Adjust modifiers so they adjust to the pixel grid"
|
||||
|
|
|
@ -403,7 +403,7 @@
|
|||
|
||||
([shape modifiers]
|
||||
(cond-> shape
|
||||
(and (some? modifiers) (not (ctm/empty-modifiers? modifiers)))
|
||||
(and (some? modifiers) (not (ctm/empty? modifiers)))
|
||||
(apply-modifiers modifiers))))
|
||||
|
||||
(defn transform-bounds
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
;; Copyright (c) UXBOX Labs SL
|
||||
|
||||
(ns app.common.types.modifiers
|
||||
(:refer-clojure :exclude [empty empty?])
|
||||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.geom.matrix :as gmt]
|
||||
|
@ -13,7 +14,9 @@
|
|||
[app.common.math :as mth]
|
||||
[app.common.pages.helpers :as cph]
|
||||
[app.common.spec :as us]
|
||||
[app.common.text :as txt]))
|
||||
[app.common.text :as txt]
|
||||
#?(:cljs [cljs.core :as c]
|
||||
:clj [clojure.core :as c])))
|
||||
|
||||
;; --- Modifiers
|
||||
|
||||
|
@ -39,7 +42,7 @@
|
|||
|
||||
;; Public builder API
|
||||
|
||||
(defn empty-modifiers []
|
||||
(defn empty []
|
||||
{})
|
||||
|
||||
(defn move-vec? [vector]
|
||||
|
@ -50,16 +53,16 @@
|
|||
(or (not (mth/almost-zero? (- (:x vector) 1)))
|
||||
(not (mth/almost-zero? (- (:y vector) 1)))))
|
||||
|
||||
(defn set-move-parent
|
||||
(defn move-parent
|
||||
([modifiers x y]
|
||||
(set-move-parent modifiers (gpt/point x y)))
|
||||
(move-parent modifiers (gpt/point x y)))
|
||||
|
||||
([modifiers vector]
|
||||
(cond-> modifiers
|
||||
(move-vec? vector)
|
||||
(update :geometry-parent conjv {:type :move :vector vector}))))
|
||||
|
||||
(defn set-resize-parent
|
||||
(defn resize-parent
|
||||
([modifiers vector origin]
|
||||
(cond-> modifiers
|
||||
(resize-vec? vector)
|
||||
|
@ -75,16 +78,16 @@
|
|||
:origin origin
|
||||
:transform transform
|
||||
:transform-inverse transform-inverse}))))
|
||||
(defn set-move
|
||||
(defn move
|
||||
([modifiers x y]
|
||||
(set-move modifiers (gpt/point x y)))
|
||||
(move modifiers (gpt/point x y)))
|
||||
|
||||
([modifiers vector]
|
||||
(cond-> modifiers
|
||||
(move-vec? vector)
|
||||
(update :geometry-child conjv {:type :move :vector vector}))))
|
||||
|
||||
(defn set-resize
|
||||
(defn resize
|
||||
([modifiers vector origin]
|
||||
(cond-> modifiers
|
||||
(resize-vec? vector)
|
||||
|
@ -101,7 +104,7 @@
|
|||
:transform transform
|
||||
:transform-inverse transform-inverse}))))
|
||||
|
||||
(defn set-rotation
|
||||
(defn rotation
|
||||
[modifiers center angle]
|
||||
(cond-> modifiers
|
||||
(not (mth/close? angle 0))
|
||||
|
@ -111,14 +114,14 @@
|
|||
:center center
|
||||
:rotation angle}))))
|
||||
|
||||
(defn set-remove-children
|
||||
(defn remove-children
|
||||
[modifiers shapes]
|
||||
(cond-> modifiers
|
||||
(d/not-empty? shapes)
|
||||
(update :structure-parent conjv {:type :remove-children
|
||||
:value shapes})))
|
||||
|
||||
(defn set-add-children
|
||||
(defn add-children
|
||||
[modifiers shapes index]
|
||||
(cond-> modifiers
|
||||
(d/not-empty? shapes)
|
||||
|
@ -126,17 +129,17 @@
|
|||
:value shapes
|
||||
:index index})))
|
||||
|
||||
(defn set-reflow
|
||||
(defn reflow
|
||||
[modifiers]
|
||||
(-> modifiers
|
||||
(update :structure-parent conjv {:type :reflow})))
|
||||
|
||||
(defn set-scale-content
|
||||
(defn scale-content
|
||||
[modifiers value]
|
||||
(-> modifiers
|
||||
(update :structure-child conjv {:type :scale-content :value value})))
|
||||
|
||||
(defn set-change-property
|
||||
(defn change-property
|
||||
[modifiers property value]
|
||||
(-> modifiers
|
||||
(update :structure-child conjv {:type :change-property
|
||||
|
@ -162,94 +165,66 @@
|
|||
|
||||
;; These are convenience methods to create single operation modifiers without the builder
|
||||
|
||||
(defn move
|
||||
(defn move-modifiers
|
||||
([x y]
|
||||
(set-move (empty-modifiers) (gpt/point x y)))
|
||||
(move (empty) (gpt/point x y)))
|
||||
|
||||
([vector]
|
||||
(set-move (empty-modifiers) vector)))
|
||||
(move (empty) vector)))
|
||||
|
||||
(defn move-parent
|
||||
(defn move-parent-modifiers
|
||||
([x y]
|
||||
(set-move-parent (empty-modifiers) (gpt/point x y)))
|
||||
(move-parent (empty) (gpt/point x y)))
|
||||
|
||||
([vector]
|
||||
(set-move-parent (empty-modifiers) vector)))
|
||||
(move-parent (empty) vector)))
|
||||
|
||||
(defn resize
|
||||
(defn resize-modifiers
|
||||
([vector origin]
|
||||
(set-resize (empty-modifiers) vector origin))
|
||||
(resize (empty) vector origin))
|
||||
|
||||
([vector origin transform transform-inverse]
|
||||
(set-resize (empty-modifiers) vector origin transform transform-inverse)))
|
||||
(resize (empty) vector origin transform transform-inverse)))
|
||||
|
||||
(defn resize-parent
|
||||
(defn resize-parent-modifiers
|
||||
([vector origin]
|
||||
(set-resize-parent (empty-modifiers) vector origin))
|
||||
(resize-parent (empty) vector origin))
|
||||
|
||||
([vector origin transform transform-inverse]
|
||||
(set-resize-parent (empty-modifiers) vector origin transform transform-inverse)))
|
||||
(resize-parent (empty) vector origin transform transform-inverse)))
|
||||
|
||||
(defn rotation
|
||||
(defn rotation-modifiers
|
||||
[shape center angle]
|
||||
(let [shape-center (gco/center-shape shape)
|
||||
rotation (-> (gmt/matrix)
|
||||
(gmt/rotate angle center)
|
||||
(gmt/rotate (- angle) shape-center))]
|
||||
|
||||
(-> (empty-modifiers)
|
||||
(set-rotation shape-center angle)
|
||||
(set-move (gpt/transform (gpt/point 0 0) rotation)))))
|
||||
(-> (empty)
|
||||
(rotation shape-center angle)
|
||||
(move (gpt/transform (gpt/point 0 0) rotation)))))
|
||||
|
||||
(defn remove-children
|
||||
(defn remove-children-modifiers
|
||||
[shapes]
|
||||
(-> (empty-modifiers)
|
||||
(set-remove-children shapes)))
|
||||
(-> (empty)
|
||||
(remove-children shapes)))
|
||||
|
||||
(defn add-children
|
||||
(defn add-children-modifiers
|
||||
[shapes index]
|
||||
(-> (empty-modifiers)
|
||||
(set-add-children shapes index)))
|
||||
(-> (empty)
|
||||
(add-children shapes index)))
|
||||
|
||||
(defn reflow
|
||||
(defn reflow-modifiers
|
||||
[]
|
||||
(-> (empty-modifiers)
|
||||
(set-reflow)))
|
||||
(-> (empty)
|
||||
(reflow)))
|
||||
|
||||
(defn scale-content
|
||||
(defn scale-content-modifiers
|
||||
[value]
|
||||
(-> (empty-modifiers)
|
||||
(set-scale-content value)))
|
||||
(-> (empty)
|
||||
(scale-content value)))
|
||||
|
||||
(defn child-modifiers?
|
||||
[{:keys [geometry-child structure-child]}]
|
||||
(or (d/not-empty? geometry-child)
|
||||
(d/not-empty? structure-child)))
|
||||
|
||||
(defn select-child-modifiers
|
||||
[modifiers]
|
||||
(select-keys modifiers [:geometry-child :structure-child]))
|
||||
|
||||
(defn select-child-geometry-modifiers
|
||||
[modifiers]
|
||||
(select-keys modifiers [:geometry-child]))
|
||||
|
||||
(defn select-parent-modifiers
|
||||
[modifiers]
|
||||
(select-keys modifiers [:geometry-parent :structure-parent]))
|
||||
|
||||
(defn select-structure
|
||||
[modifiers]
|
||||
(select-keys modifiers [:structure-parent]))
|
||||
|
||||
(defn empty-modifiers?
|
||||
[modifiers]
|
||||
(and (empty? (:geometry-child modifiers))
|
||||
(empty? (:geometry-parent modifiers))
|
||||
(empty? (:structure-parent modifiers))
|
||||
(empty? (:structure-child modifiers))))
|
||||
|
||||
(defn change-dimensions
|
||||
(defn change-dimensions-modifiers
|
||||
[shape attr value]
|
||||
(us/assert map? shape)
|
||||
(us/assert #{:width :height} attr)
|
||||
|
@ -281,7 +256,7 @@
|
|||
scalev (gpt/divide (gpt/point width height)
|
||||
(gpt/point sr-width sr-height))]
|
||||
|
||||
(resize scalev origin shape-transform shape-transform-inv)))
|
||||
(resize-modifiers scalev origin shape-transform shape-transform-inv)))
|
||||
|
||||
(defn change-orientation-modifiers
|
||||
[shape orientation]
|
||||
|
@ -304,28 +279,56 @@
|
|||
scalev (gpt/divide (gpt/point new-width new-height)
|
||||
(gpt/point sr-width sr-height))]
|
||||
|
||||
(resize scalev origin shape-transform shape-transform-inv)))
|
||||
(resize-modifiers scalev origin shape-transform shape-transform-inv)))
|
||||
|
||||
(defn merge-modifiers
|
||||
[objects modifiers]
|
||||
;; Predicates
|
||||
|
||||
(let [set-modifier
|
||||
(fn [objects [id modifiers]]
|
||||
(-> objects
|
||||
(d/update-when id merge modifiers)))]
|
||||
(->> modifiers
|
||||
(reduce set-modifier objects))))
|
||||
(defn empty?
|
||||
[modifiers]
|
||||
(and (c/empty? (:geometry-child modifiers))
|
||||
(c/empty? (:geometry-parent modifiers))
|
||||
(c/empty? (:structure-parent modifiers))
|
||||
(c/empty? (:structure-child modifiers))))
|
||||
|
||||
(defn child-modifiers?
|
||||
[{:keys [geometry-child structure-child]}]
|
||||
(or (d/not-empty? geometry-child)
|
||||
(d/not-empty? structure-child)))
|
||||
|
||||
(defn only-move?
|
||||
"Returns true if there are only move operations"
|
||||
[modifier]
|
||||
(or (and (= 1 (-> modifier :geometry-child count))
|
||||
(= :move (-> modifier :geometry-child first :type)))
|
||||
(and (= 1 (-> modifier :geometry-parent count))
|
||||
(= :move (-> modifier :geometry-parent first :type)))))
|
||||
|
||||
(defn get-frame-add-children
|
||||
[modif-tree]
|
||||
(defn has-geometry?
|
||||
[{:keys [geometry-parent geometry-child]}]
|
||||
(or (d/not-empty? geometry-parent)
|
||||
(d/not-empty? geometry-child)))
|
||||
|
||||
;; Extract subsets of modifiers
|
||||
|
||||
(defn select-child-modifiers
|
||||
[modifiers]
|
||||
(select-keys modifiers [:geometry-child :structure-child]))
|
||||
|
||||
(defn select-child-geometry-modifiers
|
||||
[modifiers]
|
||||
(select-keys modifiers [:geometry-child]))
|
||||
|
||||
(defn select-parent-modifiers
|
||||
[modifiers]
|
||||
(select-keys modifiers [:geometry-parent :structure-parent]))
|
||||
|
||||
(defn select-structure
|
||||
[modifiers]
|
||||
(select-keys modifiers [:structure-parent]))
|
||||
|
||||
(defn added-children-frames
|
||||
"Returns the frames that have an 'add-children' operation"
|
||||
[modif-tree]
|
||||
(let [structure-changes
|
||||
(into {}
|
||||
(comp (filter (fn [[_ val]] (-> val :modifiers :structure-parent some?)))
|
||||
|
@ -340,7 +343,10 @@
|
|||
(->> value (map (fn [id] {:frame frame-id :shape id}))))))))
|
||||
structure-changes)))
|
||||
|
||||
;; Main transformation functions
|
||||
|
||||
(defn modifiers->transform
|
||||
"Given a set of modifiers returns its transformation matrix"
|
||||
[modifiers]
|
||||
(letfn [(apply-modifier [matrix {:keys [type vector rotation center origin transform transform-inverse] :as modifier}]
|
||||
(case type
|
||||
|
@ -360,7 +366,6 @@
|
|||
matrix)
|
||||
|
||||
:rotation
|
||||
;; TODO LAYOUT: Maybe an issue when no center data
|
||||
(gmt/multiply
|
||||
(-> (gmt/matrix)
|
||||
(gmt/translate center)
|
||||
|
@ -373,63 +378,60 @@
|
|||
(->> modifiers
|
||||
(reduce apply-modifier (gmt/matrix))))))
|
||||
|
||||
(defn scale-text-content
|
||||
[content value]
|
||||
|
||||
(->> content
|
||||
(txt/transform-nodes
|
||||
txt/is-text-node?
|
||||
(fn [attrs]
|
||||
(let [font-size (-> (get attrs :font-size 14)
|
||||
(d/parse-double)
|
||||
(* value)
|
||||
(str)) ]
|
||||
(d/txt-merge attrs {:font-size font-size}))))))
|
||||
|
||||
(defn apply-scale-content
|
||||
[shape value]
|
||||
|
||||
(cond-> shape
|
||||
(cph/text-shape? shape)
|
||||
(update :content scale-text-content value)))
|
||||
|
||||
(defn apply-structure-modifiers
|
||||
"Apply structure changes to a shape"
|
||||
[shape modifiers]
|
||||
(let [remove-children
|
||||
(fn [shapes children-to-remove]
|
||||
(let [remove? (set children-to-remove)]
|
||||
(d/removev remove? shapes)))
|
||||
(letfn [(scale-text-content
|
||||
[content value]
|
||||
|
||||
apply-modifier
|
||||
(fn [shape {:keys [type property value index rotation]}]
|
||||
(cond-> shape
|
||||
(= type :rotation)
|
||||
(update :rotation #(mod (+ % rotation) 360))
|
||||
(->> content
|
||||
(txt/transform-nodes
|
||||
txt/is-text-node?
|
||||
(fn [attrs]
|
||||
(let [font-size (-> (get attrs :font-size 14)
|
||||
(d/parse-double)
|
||||
(* value)
|
||||
(str)) ]
|
||||
(d/txt-merge attrs {:font-size font-size}))))))
|
||||
|
||||
(and (= type :add-children) (some? index))
|
||||
(update :shapes
|
||||
(fn [shapes]
|
||||
(if (vector? shapes)
|
||||
(cph/insert-at-index shapes index value)
|
||||
(d/concat-vec shapes value))))
|
||||
(apply-scale-content
|
||||
[shape value]
|
||||
|
||||
(and (= type :add-children) (nil? index))
|
||||
(update :shapes d/concat-vec value)
|
||||
(cond-> shape
|
||||
(cph/text-shape? shape)
|
||||
(update :content scale-text-content value)))]
|
||||
(let [remove-children
|
||||
(fn [shapes children-to-remove]
|
||||
(let [remove? (set children-to-remove)]
|
||||
(d/removev remove? shapes)))
|
||||
|
||||
(= type :remove-children)
|
||||
(update :shapes remove-children value)
|
||||
apply-modifier
|
||||
(fn [shape {:keys [type property value index rotation]}]
|
||||
(cond-> shape
|
||||
(= type :rotation)
|
||||
(update :rotation #(mod (+ % rotation) 360))
|
||||
|
||||
(= type :scale-content)
|
||||
(apply-scale-content value)
|
||||
(and (= type :add-children) (some? index))
|
||||
(update :shapes
|
||||
(fn [shapes]
|
||||
(if (vector? shapes)
|
||||
(cph/insert-at-index shapes index value)
|
||||
(d/concat-vec shapes value))))
|
||||
|
||||
(= type :change-property)
|
||||
(assoc property value)))]
|
||||
(and (= type :add-children) (nil? index))
|
||||
(update :shapes d/concat-vec value)
|
||||
|
||||
(= type :remove-children)
|
||||
(update :shapes remove-children value)
|
||||
|
||||
(= type :scale-content)
|
||||
(apply-scale-content value)
|
||||
|
||||
(= type :change-property)
|
||||
(assoc property value)))]
|
||||
|
||||
(as-> shape $
|
||||
(reduce apply-modifier $ (:structure-parent modifiers))
|
||||
(reduce apply-modifier $ (:structure-child modifiers))))))
|
||||
|
||||
(as-> shape $
|
||||
(reduce apply-modifier $ (:structure-parent modifiers))
|
||||
(reduce apply-modifier $ (:structure-child modifiers)))))
|
||||
|
||||
(defn has-geometry?
|
||||
[{:keys [geometry-parent geometry-child]}]
|
||||
(or (d/not-empty? geometry-parent)
|
||||
(d/not-empty? geometry-child)))
|
||||
|
|
|
@ -60,7 +60,7 @@
|
|||
|
||||
(t/testing "Transform shape with translation modifiers"
|
||||
(t/are [type]
|
||||
(let [modifiers (ctm/move (gpt/point 10 -10))]
|
||||
(let [modifiers (ctm/move-modifiers (gpt/point 10 -10))]
|
||||
(let [shape-before (create-test-shape type {:modifiers modifiers})
|
||||
shape-after (gsh/transform-shape shape-before)]
|
||||
(t/is (not= shape-before shape-after))
|
||||
|
@ -92,7 +92,7 @@
|
|||
|
||||
(t/testing "Transform shape with resize modifiers"
|
||||
(t/are [type]
|
||||
(let [modifiers (ctm/resize (gpt/point 2 2) (gpt/point 0 0))
|
||||
(let [modifiers (ctm/resize-modifiers (gpt/point 2 2) (gpt/point 0 0))
|
||||
shape-before (create-test-shape type {:modifiers modifiers})
|
||||
shape-after (gsh/transform-shape shape-before)]
|
||||
(t/is (not= shape-before shape-after))
|
||||
|
@ -112,7 +112,7 @@
|
|||
|
||||
(t/testing "Transform with empty resize"
|
||||
(t/are [type]
|
||||
(let [modifiers (ctm/resize (gpt/point 1 1) (gpt/point 0 0))
|
||||
(let [modifiers (ctm/resize-modifiers (gpt/point 1 1) (gpt/point 0 0))
|
||||
shape-before (create-test-shape type {:modifiers modifiers})
|
||||
shape-after (gsh/transform-shape shape-before)]
|
||||
(t/are [prop]
|
||||
|
@ -123,7 +123,7 @@
|
|||
|
||||
(t/testing "Transform with resize=0"
|
||||
(t/are [type]
|
||||
(let [modifiers (ctm/resize (gpt/point 0 0) (gpt/point 0 0))
|
||||
(let [modifiers (ctm/resize-modifiers (gpt/point 0 0) (gpt/point 0 0))
|
||||
shape-before (create-test-shape type {:modifiers modifiers})
|
||||
shape-after (gsh/transform-shape shape-before)]
|
||||
(t/is (> (get-in shape-before [:selrect :width])
|
||||
|
@ -138,7 +138,7 @@
|
|||
(t/testing "Transform shape with rotation modifiers"
|
||||
(t/are [type]
|
||||
(let [shape-before (create-test-shape type)
|
||||
modifiers (ctm/rotation shape-before (gsh/center-shape shape-before) 30 )
|
||||
modifiers (ctm/rotation-modifiers shape-before (gsh/center-shape shape-before) 30 )
|
||||
shape-before (assoc shape-before :modifiers modifiers)
|
||||
shape-after (gsh/transform-shape shape-before)]
|
||||
|
||||
|
@ -160,7 +160,7 @@
|
|||
(t/testing "Transform shape with rotation = 0 should leave equal selrect"
|
||||
(t/are [type]
|
||||
(let [shape-before (create-test-shape type)
|
||||
modifiers (ctm/rotation shape-before (gsh/center-shape shape-before) 0)
|
||||
modifiers (ctm/rotation-modifiers shape-before (gsh/center-shape shape-before) 0)
|
||||
shape-after (gsh/transform-shape (assoc shape-before :modifiers modifiers))]
|
||||
(t/are [prop]
|
||||
(t/is (close? (get-in shape-before [:selrect prop])
|
||||
|
@ -170,7 +170,7 @@
|
|||
|
||||
(t/testing "Transform shape with invalid selrect fails gracefully"
|
||||
(t/are [type selrect]
|
||||
(let [modifiers (ctm/move 0 0)
|
||||
(let [modifiers (ctm/move-modifiers 0 0)
|
||||
shape-before (-> (create-test-shape type {:modifiers modifiers})
|
||||
(assoc :selrect selrect))
|
||||
shape-after (gsh/transform-shape shape-before)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue