@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/arrow-caps.d.ts.map +1 -1
- package/dist/arrow-caps.js +2 -5
- package/dist/arrow-caps.js.map +1 -1
- package/dist/asset-keys.d.ts +11 -1
- package/dist/asset-keys.d.ts.map +1 -1
- package/dist/asset-keys.js +16 -1
- package/dist/asset-keys.js.map +1 -1
- package/dist/behaviours/index.d.ts +2 -2
- package/dist/behaviours/index.js +2 -2
- package/dist/behaviours/kinds.d.ts +11 -5
- package/dist/behaviours/kinds.d.ts.map +1 -1
- package/dist/behaviours/kinds.js +76 -75
- package/dist/behaviours/kinds.js.map +1 -1
- package/dist/behaviours/layers.d.ts.map +1 -1
- package/dist/behaviours/layers.js +3 -2
- package/dist/behaviours/layers.js.map +1 -1
- package/dist/behaviours/model.d.ts +7 -4
- package/dist/behaviours/model.d.ts.map +1 -1
- package/dist/behaviours/model.js +16 -9
- package/dist/behaviours/model.js.map +1 -1
- package/dist/behaviours/schema.d.ts +118 -4
- package/dist/behaviours/schema.d.ts.map +1 -1
- package/dist/behaviours/schema.js +18 -3
- package/dist/behaviours/schema.js.map +1 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +3 -2
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/bindings.js +9 -1
- package/dist/bindings.js.map +1 -1
- package/dist/checkpoint.d.ts +31 -2
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +211 -10
- package/dist/checkpoint.js.map +1 -1
- package/dist/components/index.d.ts +3 -3
- package/dist/components/index.js +3 -3
- package/dist/components/properties.d.ts +18 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +66 -3
- package/dist/components/properties.js.map +1 -1
- package/dist/components/variants.d.ts +16 -10
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +39 -13
- package/dist/components/variants.js.map +1 -1
- package/dist/connectors/hit.d.ts.map +1 -1
- package/dist/connectors/hit.js +1 -7
- package/dist/connectors/hit.js.map +1 -1
- package/dist/connectors/pieces.d.ts.map +1 -1
- package/dist/connectors/pieces.js +2 -5
- package/dist/connectors/pieces.js.map +1 -1
- package/dist/constants.d.ts +6 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +17 -1
- package/dist/constants.js.map +1 -1
- package/dist/constraints.d.ts +25 -0
- package/dist/constraints.d.ts.map +1 -0
- package/dist/constraints.js +51 -0
- package/dist/constraints.js.map +1 -0
- package/dist/coordinate.d.ts +64 -1
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +217 -1
- package/dist/coordinate.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +17 -16
- package/dist/copy.js.map +1 -1
- package/dist/css/blend.d.ts +11 -0
- package/dist/css/blend.d.ts.map +1 -0
- package/dist/css/blend.js +30 -0
- package/dist/css/blend.js.map +1 -0
- package/dist/css/index.d.ts +4 -2
- package/dist/css/index.js +4 -2
- package/dist/css/text.d.ts +28 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +84 -17
- package/dist/css/text.js.map +1 -1
- package/dist/css/transform.d.ts +34 -0
- package/dist/css/transform.d.ts.map +1 -0
- package/dist/css/transform.js +320 -0
- package/dist/css/transform.js.map +1 -0
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +11 -1
- package/dist/css/values.js.map +1 -1
- package/dist/cubic.d.ts +7 -0
- package/dist/cubic.d.ts.map +1 -0
- package/dist/cubic.js +17 -0
- package/dist/cubic.js.map +1 -0
- package/dist/export-format.d.ts +14 -1
- package/dist/export-format.d.ts.map +1 -1
- package/dist/export-format.js +26 -1
- package/dist/export-format.js.map +1 -1
- package/dist/fields/text.d.ts +2 -2
- package/dist/fields/text.d.ts.map +1 -1
- package/dist/fields/text.js +2 -1
- package/dist/fields/text.js.map +1 -1
- package/dist/figjam/index.d.ts +2 -2
- package/dist/figjam/index.js +2 -2
- package/dist/figjam/palette.d.ts +13 -2
- package/dist/figjam/palette.d.ts.map +1 -1
- package/dist/figjam/palette.js +30 -2
- package/dist/figjam/palette.js.map +1 -1
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.js +2 -2
- package/dist/generated/shape-with-text.d.ts +7 -2
- package/dist/generated/shape-with-text.d.ts.map +1 -1
- package/dist/generated/shape-with-text.js +20 -3
- package/dist/generated/shape-with-text.js.map +1 -1
- package/dist/geometry.d.ts +37 -46
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +80 -137
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +49 -12
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +125 -27
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts +14 -4
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +76 -11
- package/dist/group-bounds.js.map +1 -1
- package/dist/guides.d.ts +3 -1
- package/dist/guides.d.ts.map +1 -1
- package/dist/guides.js +5 -1
- package/dist/guides.js.map +1 -1
- package/dist/hit-test.d.ts +5 -0
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +57 -38
- package/dist/hit-test.js.map +1 -1
- package/dist/images.d.ts +27 -3
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +57 -3
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/fields.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +8 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +1 -3
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +39 -30
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/kept-overrides.js +204 -0
- package/dist/instances/kept-overrides.js.map +1 -0
- package/dist/instances/main-component.d.ts +3 -1
- package/dist/instances/main-component.d.ts.map +1 -1
- package/dist/instances/main-component.js +10 -2
- package/dist/instances/main-component.js.map +1 -1
- package/dist/instances/overrides.d.ts +4 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +18 -2
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/placement.js +55 -12
- package/dist/instances/placement.js.map +1 -1
- package/dist/instances/swap.js +291 -0
- package/dist/instances/swap.js.map +1 -0
- package/dist/instances/sync.js +112 -39
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +2 -1
- package/dist/node-defaults.js.map +1 -1
- package/dist/order-keys.d.ts +3 -1
- package/dist/order-keys.d.ts.map +1 -1
- package/dist/order-keys.js +6 -1
- package/dist/order-keys.js.map +1 -1
- package/dist/outline/flatten.d.ts +15 -0
- package/dist/outline/flatten.d.ts.map +1 -0
- package/dist/outline/flatten.js +189 -0
- package/dist/outline/flatten.js.map +1 -0
- package/dist/outline/heads.js +34 -0
- package/dist/outline/heads.js.map +1 -0
- package/dist/outline/hit.d.ts +21 -0
- package/dist/outline/hit.d.ts.map +1 -0
- package/dist/outline/hit.js +276 -0
- package/dist/outline/hit.js.map +1 -0
- package/dist/outline/index.d.ts +5 -0
- package/dist/outline/index.js +5 -0
- package/dist/outline/point.d.ts +16 -0
- package/dist/outline/point.d.ts.map +1 -0
- package/dist/outline/point.js +42 -0
- package/dist/outline/point.js.map +1 -0
- package/dist/outline/shapes.d.ts +35 -0
- package/dist/outline/shapes.d.ts.map +1 -0
- package/dist/outline/shapes.js +181 -0
- package/dist/outline/shapes.js.map +1 -0
- package/dist/plugin-data/fields.d.ts +4 -1
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +21 -4
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/resize.d.ts +22 -11
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +227 -93
- package/dist/resize.js.map +1 -1
- package/dist/scaling/index.d.ts +3 -2
- package/dist/scaling/index.js +3 -2
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +5 -1
- package/dist/scaling/node.js.map +1 -1
- package/dist/scaling/snapshot.d.ts +25 -0
- package/dist/scaling/snapshot.d.ts.map +1 -0
- package/dist/scaling/snapshot.js +39 -0
- package/dist/scaling/snapshot.js.map +1 -0
- package/dist/scaling/tree.d.ts +24 -2
- package/dist/scaling/tree.d.ts.map +1 -1
- package/dist/scaling/tree.js +38 -15
- package/dist/scaling/tree.js.map +1 -1
- package/dist/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +2 -1
- package/dist/shaders/model.js.map +1 -1
- package/dist/shared-styles.d.ts +16 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +69 -2
- package/dist/shared-styles.js.map +1 -1
- package/dist/slots/authoring.d.ts +3 -2
- package/dist/slots/authoring.d.ts.map +1 -1
- package/dist/slots/authoring.js +5 -3
- package/dist/slots/authoring.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +6 -1
- package/dist/slots/content.js.map +1 -1
- package/dist/slots/frames.d.ts +2 -0
- package/dist/slots/frames.d.ts.map +1 -1
- package/dist/slots/frames.js +7 -2
- package/dist/slots/frames.js.map +1 -1
- package/dist/text/direction.d.ts +3 -1
- package/dist/text/direction.d.ts.map +1 -1
- package/dist/text/direction.js +5 -1
- package/dist/text/direction.js.map +1 -1
- package/dist/text/index.d.ts +2 -2
- package/dist/text/index.js +3 -3
- package/dist/text/lengths.d.ts +6 -1
- package/dist/text/lengths.d.ts.map +1 -1
- package/dist/text/lengths.js +10 -1
- package/dist/text/lengths.js.map +1 -1
- package/dist/text/overflow.d.ts +2 -2
- package/dist/text/overflow.js.map +1 -1
- package/dist/text/picture.d.ts +1 -2
- package/dist/text/picture.d.ts.map +1 -1
- package/dist/text/picture.js +1 -4
- package/dist/text/picture.js.map +1 -1
- package/dist/transfer/apply.d.ts.map +1 -1
- package/dist/transfer/apply.js +1 -6
- package/dist/transfer/apply.js.map +1 -1
- package/dist/transfer/copied.d.ts +35 -3
- package/dist/transfer/copied.d.ts.map +1 -1
- package/dist/transfer/copied.js +44 -13
- package/dist/transfer/copied.js.map +1 -1
- package/dist/transfer/dependencies.d.ts +38 -0
- package/dist/transfer/dependencies.d.ts.map +1 -0
- package/dist/transfer/dependencies.js +100 -0
- package/dist/transfer/dependencies.js.map +1 -0
- package/dist/transfer/history.js +0 -1
- package/dist/transfer/history.js.map +1 -1
- package/dist/transfer/index.d.ts +5 -3
- package/dist/transfer/index.js +5 -3
- package/dist/transfer/nodes.d.ts.map +1 -1
- package/dist/transfer/nodes.js +16 -19
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/transfer/plan.d.ts +0 -1
- package/dist/transfer/plan.js +3 -5
- package/dist/transfer/plan.js.map +1 -1
- package/dist/transfer/remove.js +5 -2
- package/dist/transfer/remove.js.map +1 -1
- package/dist/transfer/resources.d.ts +21 -0
- package/dist/transfer/resources.d.ts.map +1 -0
- package/dist/transfer/resources.js +38 -0
- package/dist/transfer/resources.js.map +1 -0
- package/dist/transfer/variables.d.ts +8 -1
- package/dist/transfer/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +24 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +73 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +28 -3
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +24 -7
- package/dist/undo.js.map +1 -1
- package/dist/variables/bindings.d.ts +37 -11
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +122 -74
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/fields.d.ts +19 -1
- package/dist/variables/fields.d.ts.map +1 -1
- package/dist/variables/fields.js +36 -1
- package/dist/variables/fields.js.map +1 -1
- package/dist/variables/formula/check.d.ts +1 -1
- package/dist/variables/formula/index.d.ts +2 -2
- package/dist/variables/formula/index.js +1 -1
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/formula/print.d.ts +1 -1
- package/dist/variables/formula/signatures.js +1 -1
- package/dist/variables/index.d.ts +7 -7
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +57 -26
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/paints.d.ts +57 -0
- package/dist/variables/paints.d.ts.map +1 -0
- package/dist/variables/paints.js +231 -0
- package/dist/variables/paints.js.map +1 -0
- package/dist/variables/resolution.d.ts +14 -11
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +36 -17
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +17 -0
- package/dist/variables/subtree.js.map +1 -0
- package/dist/vector-network.d.ts.map +1 -1
- package/dist/vector-network.js +2 -8
- package/dist/vector-network.js.map +1 -1
- package/package.json +14 -1
- package/src/arrow-caps.ts +2 -5
- package/src/asset-keys.ts +20 -0
- package/src/behaviours/kinds.ts +51 -34
- package/src/behaviours/layers.ts +4 -2
- package/src/behaviours/model.ts +22 -11
- package/src/behaviours/schema.ts +20 -4
- package/src/behaviours/spec.ts +8 -2
- package/src/bindings.ts +11 -2
- package/src/checkpoint.ts +319 -17
- package/src/components/properties.ts +101 -2
- package/src/components/variants.ts +51 -30
- package/src/connectors/hit.ts +1 -12
- package/src/connectors/pieces.ts +2 -5
- package/src/constants.ts +17 -0
- package/src/constraints.ts +60 -0
- package/src/coordinate.ts +220 -0
- package/src/copy.ts +18 -19
- package/src/css/blend.ts +27 -0
- package/src/css/index.ts +18 -1
- package/src/css/text.ts +85 -17
- package/src/css/transform.ts +266 -0
- package/src/css/values.ts +8 -0
- package/src/cubic.ts +14 -0
- package/src/export-format.ts +29 -0
- package/src/fields/text.ts +3 -1
- package/src/figjam/palette.ts +29 -1
- package/src/generated/shape-with-text.ts +28 -2
- package/src/geometry.ts +97 -206
- package/src/graph.ts +155 -45
- package/src/group-bounds.ts +84 -17
- package/src/guides.ts +5 -0
- package/src/hit-test.ts +93 -64
- package/src/images.ts +71 -3
- package/src/instances/fields.ts +8 -0
- package/src/instances/index.ts +62 -31
- package/src/instances/kept-overrides.ts +280 -0
- package/src/instances/main-component.ts +12 -0
- package/src/instances/overrides.ts +27 -1
- package/src/instances/placement.ts +63 -10
- package/src/instances/swap.ts +456 -0
- package/src/instances/sync.ts +176 -50
- package/src/node-defaults.ts +3 -1
- package/src/order-keys.ts +6 -0
- package/src/outline/flatten.ts +190 -0
- package/src/outline/heads.ts +52 -0
- package/src/outline/hit.ts +369 -0
- package/src/outline/index.ts +4 -0
- package/src/outline/point.ts +59 -0
- package/src/outline/shapes.ts +207 -0
- package/src/plugin-data/fields.ts +21 -2
- package/src/resize.ts +267 -119
- package/src/scaling/index.ts +9 -1
- package/src/scaling/node.ts +9 -1
- package/src/scaling/snapshot.ts +51 -0
- package/src/scaling/tree.ts +63 -19
- package/src/shaders/model.ts +2 -3
- package/src/shared-styles.ts +90 -1
- package/src/slots/authoring.ts +5 -3
- package/src/slots/content.ts +6 -1
- package/src/slots/frames.ts +9 -7
- package/src/text/direction.ts +5 -0
- package/src/text/lengths.ts +10 -0
- package/src/text/overflow.ts +2 -2
- package/src/text/picture.ts +0 -4
- package/src/transfer/apply.ts +1 -13
- package/src/transfer/copied.ts +83 -15
- package/src/transfer/dependencies.ts +126 -0
- package/src/transfer/history.ts +0 -6
- package/src/transfer/index.ts +20 -2
- package/src/transfer/nodes.ts +17 -31
- package/src/transfer/plan.ts +3 -11
- package/src/transfer/remove.ts +5 -2
- package/src/transfer/resources.ts +54 -0
- package/src/transfer/variables.ts +34 -0
- package/src/types.ts +83 -1
- package/src/undo.ts +48 -10
- package/src/variables/bindings.ts +152 -88
- package/src/variables/fields.ts +46 -1
- package/src/variables/formula/index.ts +3 -1
- package/src/variables/index.ts +90 -39
- package/src/variables/paints.ts +296 -0
- package/src/variables/resolution.ts +39 -28
- package/src/variables/subtree.ts +15 -0
- package/src/vector-network.ts +4 -2
package/src/resize.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { CONSTRAINT_PARENT_TYPES } from './constraints'
|
|
2
|
+
import { getNodeLocalMatrix } from './coordinate'
|
|
1
3
|
import {
|
|
2
4
|
cloneVectorNetwork,
|
|
3
5
|
copyDerivedGlyphs,
|
|
@@ -6,7 +8,10 @@ import {
|
|
|
6
8
|
copyStrokes,
|
|
7
9
|
scaleGeometryPaths
|
|
8
10
|
} from './copy'
|
|
11
|
+
import { groupFitChanges } from './group-bounds'
|
|
9
12
|
import { enforcedAspectRatio } from './layout/aspect-ratio'
|
|
13
|
+
import Matrix, { type Mat3 } from './matrix'
|
|
14
|
+
import { FITTED_CONTAINER_TYPES } from './node-defaults'
|
|
10
15
|
import type { Rect } from './primitives'
|
|
11
16
|
import type { ConstraintType, DerivedTextGlyph, SceneNode, Stroke, VectorNetwork } from './types'
|
|
12
17
|
|
|
@@ -23,6 +28,8 @@ export type ResizeSnapshot = Pick<
|
|
|
23
28
|
| 'y'
|
|
24
29
|
| 'width'
|
|
25
30
|
| 'height'
|
|
31
|
+
| 'flipX'
|
|
32
|
+
| 'flipY'
|
|
26
33
|
| 'vectorNetwork'
|
|
27
34
|
| 'fillGeometry'
|
|
28
35
|
| 'strokeGeometry'
|
|
@@ -33,38 +40,58 @@ export type ResizeSnapshot = Pick<
|
|
|
33
40
|
>
|
|
34
41
|
|
|
35
42
|
interface ResizeGraph {
|
|
43
|
+
readonly rootId: string
|
|
36
44
|
getNode(id: string): SceneNode | undefined
|
|
37
45
|
}
|
|
38
46
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Containers whose resize moves their children: the constraint parents, and groups and booleans,
|
|
49
|
+
* which scale theirs.
|
|
50
|
+
*/
|
|
51
|
+
const CONSTRAINT_CONTAINER_TYPES = new Set<SceneNode['type']>([
|
|
52
|
+
...CONSTRAINT_PARENT_TYPES,
|
|
53
|
+
...FITTED_CONTAINER_TYPES
|
|
46
54
|
])
|
|
47
55
|
|
|
56
|
+
type AxisPlacement = { position: number; size: number; flipped?: boolean }
|
|
57
|
+
|
|
58
|
+
/** A child's box after its parent resizes, and the axes on which it turned over. */
|
|
59
|
+
export type ConstrainedRect = Rect & { flipsX?: boolean; flipsY?: boolean }
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Where a constraint places a child on one axis when its parent goes from `parentBefore` to
|
|
63
|
+
* `parentAfter`, as Figma does (live-measured, #1060, #1816): Right and Left & right move or grow
|
|
64
|
+
* it by the change in the parent's size rounded to whole pixels, Center by the change in its
|
|
65
|
+
* rounded half, and Scale by the exact ratio. The child's own box is never rounded, so a resize
|
|
66
|
+
* to the same size changes nothing. Left & right that would leave no size keeps the axis as it
|
|
67
|
+
* was; past zero, the child turns over and its box runs back from the edge it was pinned to, as
|
|
68
|
+
* the layer is mirrored in Figma.
|
|
69
|
+
*/
|
|
48
70
|
function constrainedAxis(
|
|
49
71
|
position: number,
|
|
50
72
|
size: number,
|
|
51
73
|
parentBefore: number,
|
|
52
74
|
parentAfter: number,
|
|
53
75
|
constraint: ConstraintType
|
|
54
|
-
):
|
|
55
|
-
const delta = parentAfter - parentBefore
|
|
76
|
+
): AxisPlacement {
|
|
77
|
+
const delta = Math.round(parentAfter) - Math.round(parentBefore)
|
|
56
78
|
if (constraint === 'MAX') return { position: position + delta, size }
|
|
57
|
-
if (constraint === 'CENTER')
|
|
58
|
-
|
|
79
|
+
if (constraint === 'CENTER') {
|
|
80
|
+
return { position: position + Math.round(parentAfter / 2) - Math.round(parentBefore / 2), size }
|
|
81
|
+
}
|
|
82
|
+
if (constraint === 'STRETCH') {
|
|
83
|
+
const stretched = size + delta
|
|
84
|
+
if (stretched === 0) return { position, size }
|
|
85
|
+
if (stretched > 0) return { position, size: stretched }
|
|
86
|
+
return { position: position + stretched, size: -stretched, flipped: true }
|
|
87
|
+
}
|
|
59
88
|
if (constraint === 'SCALE' && parentBefore > 0) {
|
|
60
89
|
const scale = parentAfter / parentBefore
|
|
61
|
-
return { position: position * scale, size:
|
|
90
|
+
return { position: position * scale, size: size * scale }
|
|
62
91
|
}
|
|
63
92
|
return { position, size }
|
|
64
93
|
}
|
|
65
94
|
|
|
66
|
-
type AxisPlacement = { position: number; size: number }
|
|
67
|
-
|
|
68
95
|
const FLEXIBLE_CONSTRAINTS: ReadonlySet<ConstraintType> = new Set(['STRETCH', 'SCALE'])
|
|
69
96
|
|
|
70
97
|
/** The axis at `size`, keeping the edge or centre its constraint pins where it was placed. */
|
|
@@ -108,74 +135,29 @@ function keepLockedRatio(
|
|
|
108
135
|
: { x: resizedInPlace(x, y.size * aspectRatio, horizontal), y }
|
|
109
136
|
}
|
|
110
137
|
|
|
111
|
-
/** A child's box after its parent resizes, keeping a locked aspect ratio as Figma does. */
|
|
112
|
-
export function constrainedChildRect(
|
|
113
|
-
child: Rect,
|
|
114
|
-
parentBefore: Pick<Rect, 'width' | 'height'>,
|
|
115
|
-
parentAfter: Pick<Rect, 'width' | 'height'>,
|
|
116
|
-
horizontal: ConstraintType,
|
|
117
|
-
vertical: ConstraintType,
|
|
118
|
-
aspectRatio: number | null = null
|
|
119
|
-
): Rect {
|
|
120
|
-
let x = constrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal)
|
|
121
|
-
let y = constrainedAxis(child.y, child.height, parentBefore.height, parentAfter.height, vertical)
|
|
122
|
-
;({ x, y } = keepLockedRatio(x, y, horizontal, vertical, aspectRatio))
|
|
123
|
-
return {
|
|
124
|
-
x: Math.round(x.position),
|
|
125
|
-
y: Math.round(y.position),
|
|
126
|
-
width: Math.round(x.size),
|
|
127
|
-
height: Math.round(y.size)
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function layoutConstrainedAxis(
|
|
132
|
-
position: number,
|
|
133
|
-
size: number,
|
|
134
|
-
parentBefore: number,
|
|
135
|
-
parentAfter: number,
|
|
136
|
-
constraint: ConstraintType
|
|
137
|
-
): { position: number; size: number } {
|
|
138
|
-
const delta = Math.round(parentAfter) - Math.round(parentBefore)
|
|
139
|
-
if (constraint === 'MAX') return { position: position + delta, size }
|
|
140
|
-
if (constraint === 'CENTER') {
|
|
141
|
-
return { position: position + Math.round(parentAfter / 2) - Math.round(parentBefore / 2), size }
|
|
142
|
-
}
|
|
143
|
-
if (constraint === 'STRETCH') return { position, size: Math.abs(size + delta) }
|
|
144
|
-
if (constraint === 'SCALE' && parentBefore > 0) {
|
|
145
|
-
const scale = parentAfter / parentBefore
|
|
146
|
-
return { position: Math.round(position * scale * 128) / 128, size: size * scale }
|
|
147
|
-
}
|
|
148
|
-
return { position, size }
|
|
149
|
-
}
|
|
150
|
-
|
|
151
138
|
/**
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
* it by the change in the parent's size rounded to whole pixels, Center by the change in its
|
|
155
|
-
* rounded half, and Scale by the exact ratio, its position to 1/128 of a pixel. Sizes are not
|
|
156
|
-
* rounded, so applying a run of changes one at a time lands where applying them at once does.
|
|
139
|
+
* A child's box after its parent resizes, by a handle, `resize()`, layout, or an instance taking
|
|
140
|
+
* another size than its component, keeping a locked aspect ratio as Figma does.
|
|
157
141
|
*/
|
|
158
|
-
export function
|
|
142
|
+
export function constrainedChildRect(
|
|
159
143
|
child: Rect,
|
|
160
144
|
parentBefore: Pick<Rect, 'width' | 'height'>,
|
|
161
145
|
parentAfter: Pick<Rect, 'width' | 'height'>,
|
|
162
146
|
horizontal: ConstraintType,
|
|
163
147
|
vertical: ConstraintType,
|
|
164
148
|
aspectRatio: number | null = null
|
|
165
|
-
):
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
149
|
+
): ConstrainedRect {
|
|
150
|
+
const { x, y } = keepLockedRatio(
|
|
151
|
+
constrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal),
|
|
152
|
+
constrainedAxis(child.y, child.height, parentBefore.height, parentAfter.height, vertical),
|
|
169
153
|
horizontal,
|
|
170
154
|
vertical,
|
|
171
155
|
aspectRatio
|
|
172
156
|
)
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
height: placed.y.size
|
|
178
|
-
}
|
|
157
|
+
const rect: ConstrainedRect = { x: x.position, y: y.position, width: x.size, height: y.size }
|
|
158
|
+
if (x.flipped) rect.flipsX = true
|
|
159
|
+
if (y.flipped) rect.flipsY = true
|
|
160
|
+
return rect
|
|
179
161
|
}
|
|
180
162
|
|
|
181
163
|
export function scaledChildRect(
|
|
@@ -186,6 +168,11 @@ export function scaledChildRect(
|
|
|
186
168
|
return constrainedChildRect(child, parentBefore, parentAfter, 'SCALE', 'SCALE')
|
|
187
169
|
}
|
|
188
170
|
|
|
171
|
+
/** How much a side grows; a flat side, as a straight line's, has nothing to scale along it. */
|
|
172
|
+
function sideScale(original: number, size: number): number {
|
|
173
|
+
return original > 0 ? size / original : 1
|
|
174
|
+
}
|
|
175
|
+
|
|
189
176
|
export function scaleVectorNetworkForResize(
|
|
190
177
|
vectorNetwork: VectorNetwork | null,
|
|
191
178
|
originalWidth: number,
|
|
@@ -195,9 +182,8 @@ export function scaleVectorNetworkForResize(
|
|
|
195
182
|
): VectorNetwork | null {
|
|
196
183
|
if (!vectorNetwork || (originalWidth <= 0 && originalHeight <= 0)) return null
|
|
197
184
|
|
|
198
|
-
|
|
199
|
-
const
|
|
200
|
-
const scaleY = originalHeight > 0 ? height / originalHeight : 1
|
|
185
|
+
const scaleX = sideScale(originalWidth, width)
|
|
186
|
+
const scaleY = sideScale(originalHeight, height)
|
|
201
187
|
if (scaleX === 1 && scaleY === 1) return null
|
|
202
188
|
|
|
203
189
|
return {
|
|
@@ -229,6 +215,8 @@ export function createResizeSnapshot(node: SceneNode): ResizeSnapshot {
|
|
|
229
215
|
y: node.y,
|
|
230
216
|
width: node.width,
|
|
231
217
|
height: node.height,
|
|
218
|
+
flipX: node.flipX,
|
|
219
|
+
flipY: node.flipY,
|
|
232
220
|
vectorNetwork: node.vectorNetwork ? cloneVectorNetwork(node.vectorNetwork) : null,
|
|
233
221
|
fillGeometry: copyGeometryPaths(node.fillGeometry),
|
|
234
222
|
strokeGeometry: copyGeometryPaths(node.strokeGeometry),
|
|
@@ -289,9 +277,10 @@ export function scaledGeometryChanges(
|
|
|
289
277
|
width: number,
|
|
290
278
|
height: number
|
|
291
279
|
): Partial<SceneNode> {
|
|
292
|
-
if (origWidth <= 0
|
|
293
|
-
|
|
294
|
-
const
|
|
280
|
+
if (origWidth <= 0 && origHeight <= 0) return {}
|
|
281
|
+
// A straight line's other side still scales.
|
|
282
|
+
const sx = sideScale(origWidth, width)
|
|
283
|
+
const sy = sideScale(origHeight, height)
|
|
295
284
|
if (sx === 1 && sy === 1) return {}
|
|
296
285
|
|
|
297
286
|
const changes: Partial<SceneNode> = {}
|
|
@@ -341,6 +330,38 @@ export function scaledGeometryChanges(
|
|
|
341
330
|
return changes
|
|
342
331
|
}
|
|
343
332
|
|
|
333
|
+
/** Copies of the geometry a resize may change, as the snapshot held it before the resize. */
|
|
334
|
+
function snapshotGeometry(orig: ResizeSnapshot): Partial<SceneNode> {
|
|
335
|
+
return {
|
|
336
|
+
vectorNetwork: orig.vectorNetwork ? cloneVectorNetwork(orig.vectorNetwork) : null,
|
|
337
|
+
fillGeometry: copyGeometryPaths(orig.fillGeometry),
|
|
338
|
+
strokeGeometry: copyGeometryPaths(orig.strokeGeometry),
|
|
339
|
+
derivedTextGlyphs: copyDerivedGlyphs(orig.derivedTextGlyphs),
|
|
340
|
+
strokes: copyStrokes(orig.strokes),
|
|
341
|
+
textPathBox: orig.textPathBox ? { ...orig.textPathBox } : null
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** Whether a box of `size` scales the snapshot's geometry, a flat side left out. */
|
|
346
|
+
function scalesGeometry(orig: ResizeSnapshot, size: Pick<Rect, 'width' | 'height'>): boolean {
|
|
347
|
+
return sideScale(orig.width, size.width) !== 1 || sideScale(orig.height, size.height) !== 1
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* A child's geometry for its constrained `box`, scaled from the snapshot. At a box that scales
|
|
352
|
+
* nothing, after an earlier frame of the same drag scaled it, the child takes its snapshot
|
|
353
|
+
* geometry again, as nothing else would undo that scaling.
|
|
354
|
+
*/
|
|
355
|
+
function constrainedGeometryChanges(
|
|
356
|
+
orig: ResizeSnapshot,
|
|
357
|
+
current: Pick<SceneNode, 'width' | 'height'>,
|
|
358
|
+
box: Pick<Rect, 'width' | 'height'>
|
|
359
|
+
): Partial<SceneNode> {
|
|
360
|
+
if (scalesGeometry(orig, box))
|
|
361
|
+
return scaledGeometryChanges(orig, orig.width, orig.height, box.width, box.height)
|
|
362
|
+
return scalesGeometry(orig, current) ? snapshotGeometry(orig) : {}
|
|
363
|
+
}
|
|
364
|
+
|
|
344
365
|
export function collectResizeDescendants(
|
|
345
366
|
graph: ResizeGraph,
|
|
346
367
|
rootId: string
|
|
@@ -364,52 +385,179 @@ export function collectResizeDescendants(
|
|
|
364
385
|
return snapshots.size > 0 ? snapshots : null
|
|
365
386
|
}
|
|
366
387
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
388
|
+
/**
|
|
389
|
+
* The flips a child takes when its constraints place it from `from`, written only where they
|
|
390
|
+
* differ from the ones it has now, so a drag that crosses back undoes its own turn.
|
|
391
|
+
*/
|
|
392
|
+
export function flipChanges(
|
|
393
|
+
from: Pick<SceneNode, 'flipX' | 'flipY'>,
|
|
394
|
+
current: Pick<SceneNode, 'flipX' | 'flipY'>,
|
|
395
|
+
flipsX = false,
|
|
396
|
+
flipsY = false
|
|
397
|
+
): Partial<Pick<SceneNode, 'flipX' | 'flipY'>> {
|
|
398
|
+
const changes: Partial<Pick<SceneNode, 'flipX' | 'flipY'>> = {}
|
|
399
|
+
const flipX = from.flipX !== flipsX
|
|
400
|
+
const flipY = from.flipY !== flipsY
|
|
401
|
+
if (flipX !== current.flipX) changes.flipX = flipX
|
|
402
|
+
if (flipY !== current.flipY) changes.flipY = flipY
|
|
403
|
+
return changes
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
type Size = Pick<Rect, 'width' | 'height'>
|
|
407
|
+
|
|
408
|
+
/** One constrained resize: the graph, where each layer was when it began, and what it changes. */
|
|
409
|
+
interface ResizePlan {
|
|
410
|
+
graph: ResizeGraph
|
|
372
411
|
originals: ReadonlyMap<string, ResizeSnapshot>
|
|
373
|
-
|
|
374
|
-
|
|
412
|
+
changes: Map<string, Partial<SceneNode>>
|
|
413
|
+
}
|
|
375
414
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
415
|
+
/** The layer as the resize found it, or as the resize leaves it with its changes so far. */
|
|
416
|
+
function startOf(plan: ResizePlan, id: string): SceneNode | undefined {
|
|
417
|
+
const node = plan.graph.getNode(id)
|
|
418
|
+
const original = plan.originals.get(id)
|
|
419
|
+
return node && original ? { ...node, ...original } : node
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function plannedOf(plan: ResizePlan, id: string): SceneNode | undefined {
|
|
423
|
+
const node = startOf(plan, id)
|
|
424
|
+
const changes = plan.changes.get(id)
|
|
425
|
+
return node && changes ? { ...node, ...changes } : node
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function mergeChanges(plan: ResizePlan, id: string, changes: Partial<SceneNode>) {
|
|
429
|
+
plan.changes.set(id, { ...plan.changes.get(id), ...changes })
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** Writes where a layer's constraints put it, then places its own children. */
|
|
433
|
+
function placeConstrained(
|
|
434
|
+
plan: ResizePlan,
|
|
435
|
+
child: SceneNode,
|
|
436
|
+
original: ResizeSnapshot,
|
|
437
|
+
rect: ConstrainedRect,
|
|
438
|
+
toParent: Mat3 | null
|
|
439
|
+
) {
|
|
440
|
+
const { flipsX, flipsY, ...box } = rect
|
|
441
|
+
const position = toParent ? Matrix.mapPoint(toParent, box) : box
|
|
442
|
+
plan.changes.set(child.id, {
|
|
443
|
+
...box,
|
|
444
|
+
...position,
|
|
445
|
+
...flipChanges(original, child, flipsX, flipsY),
|
|
446
|
+
...constrainedGeometryChanges(original, child, box)
|
|
447
|
+
})
|
|
448
|
+
// The final pass sees layout containers after Yoga has resolved HUG/FILL sizing.
|
|
449
|
+
placeChildren(plan, child.id, original, child.layoutMode === 'NONE' ? box : child)
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Places the layers in a group or boolean whose frame resized. The group owns no constraints: each
|
|
454
|
+
* layer in it keeps its place in the frame by its own, through nested groups, and the groups then
|
|
455
|
+
* fit around them, as in Figma (live, #1835). A layer is placed in the frame's space and written
|
|
456
|
+
* back in its group's.
|
|
457
|
+
*/
|
|
458
|
+
function placeGroupedLayers(
|
|
459
|
+
plan: ResizePlan,
|
|
460
|
+
group: SceneNode,
|
|
461
|
+
toFrame: Mat3,
|
|
462
|
+
frameBefore: Size,
|
|
463
|
+
frameAfter: Size
|
|
464
|
+
) {
|
|
465
|
+
for (const childId of group.childIds) {
|
|
466
|
+
const child = plan.graph.getNode(childId)
|
|
467
|
+
const original = plan.originals.get(childId)
|
|
468
|
+
const start = startOf(plan, childId)
|
|
469
|
+
if (!child || !original || !start) continue
|
|
470
|
+
if (FITTED_CONTAINER_TYPES.has(child.type)) {
|
|
471
|
+
const inner = Matrix.multiply(toFrame, getNodeLocalMatrix(start))
|
|
472
|
+
placeGroupedLayers(plan, start, inner, frameBefore, frameAfter)
|
|
473
|
+
continue
|
|
410
474
|
}
|
|
475
|
+
const inFrame = Matrix.mapPoint(toFrame, original)
|
|
476
|
+
const rect = constrainedChildRect(
|
|
477
|
+
{ ...inFrame, width: original.width, height: original.height },
|
|
478
|
+
frameBefore,
|
|
479
|
+
frameAfter,
|
|
480
|
+
child.horizontalConstraint,
|
|
481
|
+
child.verticalConstraint,
|
|
482
|
+
enforcedAspectRatio(child)
|
|
483
|
+
)
|
|
484
|
+
placeConstrained(plan, child, original, rect, Matrix.invert(toFrame))
|
|
411
485
|
}
|
|
486
|
+
fitPlannedGroup(plan, group.id)
|
|
487
|
+
}
|
|
412
488
|
|
|
413
|
-
|
|
414
|
-
|
|
489
|
+
const PLACEMENT_KEYS = ['x', 'y', 'width', 'height'] as const
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Fits a group to its layers where the resize put them, when any of them moved: a boolean's box
|
|
493
|
+
* is its result's, which only the renderer measures, so one whose operands stay keeps its box. A
|
|
494
|
+
* drag places from the start each time, so the group's box is always written.
|
|
495
|
+
*/
|
|
496
|
+
function fitPlannedGroup(plan: ResizePlan, groupId: string) {
|
|
497
|
+
const group = plannedOf(plan, groupId)
|
|
498
|
+
if (!group) return
|
|
499
|
+
const { x, y, width, height } = group
|
|
500
|
+
mergeChanges(plan, groupId, { x, y, width, height })
|
|
501
|
+
const moved = group.childIds.some((id) => {
|
|
502
|
+
const start = startOf(plan, id)
|
|
503
|
+
const planned = plannedOf(plan, id)
|
|
504
|
+
return start && planned && PLACEMENT_KEYS.some((key) => start[key] !== planned[key])
|
|
505
|
+
})
|
|
506
|
+
if (!moved) return
|
|
507
|
+
const reader = { rootId: plan.graph.rootId, getNode: (id: string) => plannedOf(plan, id) }
|
|
508
|
+
const children = group.childIds.flatMap((id) => reader.getNode(id) ?? [])
|
|
509
|
+
const fit = groupFitChanges(reader, group, children)
|
|
510
|
+
for (const [id, changes] of fit ?? []) mergeChanges(plan, id, changes)
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Places the children of a layer that went from `parentBefore` to `parentAfter`: a frame's by their
|
|
515
|
+
* constraints, the layers in its groups and booleans by theirs, a group's or boolean's own scaled.
|
|
516
|
+
*/
|
|
517
|
+
function placeChildren(plan: ResizePlan, parentId: string, parentBefore: Size, parentAfter: Size) {
|
|
518
|
+
const parent = plan.graph.getNode(parentId)
|
|
519
|
+
if (!parent) return
|
|
520
|
+
const scalesChildren = FITTED_CONTAINER_TYPES.has(parent.type)
|
|
521
|
+
for (const childId of parent.childIds) {
|
|
522
|
+
const original = plan.originals.get(childId)
|
|
523
|
+
const child = plan.graph.getNode(childId)
|
|
524
|
+
if (!original || !child) continue
|
|
525
|
+
const isInFlow = parent.layoutMode !== 'NONE' && child.layoutPositioning !== 'ABSOLUTE'
|
|
526
|
+
if (isInFlow) {
|
|
527
|
+
placeChildren(plan, childId, original, child)
|
|
528
|
+
continue
|
|
529
|
+
}
|
|
530
|
+
if (!scalesChildren && FITTED_CONTAINER_TYPES.has(child.type)) {
|
|
531
|
+
const start = { ...child, ...original }
|
|
532
|
+
placeGroupedLayers(plan, start, getNodeLocalMatrix(start), parentBefore, parentAfter)
|
|
533
|
+
continue
|
|
534
|
+
}
|
|
535
|
+
const rect: ConstrainedRect = scalesChildren
|
|
536
|
+
? scaledChildRect(original, parentBefore, parentAfter)
|
|
537
|
+
: constrainedChildRect(
|
|
538
|
+
original,
|
|
539
|
+
parentBefore,
|
|
540
|
+
parentAfter,
|
|
541
|
+
child.horizontalConstraint,
|
|
542
|
+
child.verticalConstraint,
|
|
543
|
+
enforcedAspectRatio(child)
|
|
544
|
+
)
|
|
545
|
+
placeConstrained(plan, child, original, rect, null)
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Where the descendants of a layer go when it resizes from `rootBefore` to `rootAfter`, from the
|
|
551
|
+
* places `originals` recorded.
|
|
552
|
+
*/
|
|
553
|
+
export function computeConstrainedResizeChanges(
|
|
554
|
+
graph: ResizeGraph,
|
|
555
|
+
rootId: string,
|
|
556
|
+
rootBefore: Size,
|
|
557
|
+
rootAfter: Size,
|
|
558
|
+
originals: ReadonlyMap<string, ResizeSnapshot>
|
|
559
|
+
): Map<string, Partial<SceneNode>> {
|
|
560
|
+
const plan: ResizePlan = { graph, originals, changes: new Map() }
|
|
561
|
+
placeChildren(plan, rootId, rootBefore, rootAfter)
|
|
562
|
+
return plan.changes
|
|
415
563
|
}
|
package/src/scaling/index.ts
CHANGED
|
@@ -1,2 +1,10 @@
|
|
|
1
1
|
export { scaleNodeChanges } from './node'
|
|
2
|
-
export {
|
|
2
|
+
export {
|
|
3
|
+
MIN_RESCALE,
|
|
4
|
+
rescaleNodeChanges,
|
|
5
|
+
rescaleNodeTree,
|
|
6
|
+
rescaleTargets,
|
|
7
|
+
scaleRoots,
|
|
8
|
+
type RescaleOptions
|
|
9
|
+
} from './tree'
|
|
10
|
+
export { scaledSnapshotChanges, snapshotScale, type ScaleSnapshot } from './snapshot'
|
package/src/scaling/node.ts
CHANGED
|
@@ -29,7 +29,15 @@ function scaledEffects(effects: readonly Effect[], scale: number): Effect[] {
|
|
|
29
29
|
...structuredClone(effect),
|
|
30
30
|
offset: { x: effect.offset.x * scale, y: effect.offset.y * scale },
|
|
31
31
|
radius: effect.radius * scale,
|
|
32
|
-
spread: effect.spread * scale
|
|
32
|
+
spread: effect.spread * scale,
|
|
33
|
+
...(effect.progressive
|
|
34
|
+
? {
|
|
35
|
+
progressive: {
|
|
36
|
+
...structuredClone(effect.progressive),
|
|
37
|
+
startRadius: effect.progressive.startRadius * scale
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
: {})
|
|
33
41
|
}))
|
|
34
42
|
}
|
|
35
43
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { placeScaledAbout, selectionPlacement, type SelectionPlacement } from '../coordinate'
|
|
2
|
+
import type { SceneGraph } from '../graph'
|
|
3
|
+
import type { Vector } from '../primitives'
|
|
4
|
+
import type { SceneNode } from '../types'
|
|
5
|
+
import { rescaleNodeChanges, rescaleTargets } from './tree'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The layers a Scale edit changes, as they were when it began, so every step of a drag or a
|
|
9
|
+
* scrubbed field scales from the same start rather than from its own last preview.
|
|
10
|
+
*/
|
|
11
|
+
export interface ScaleSnapshot {
|
|
12
|
+
/** Each scaled root's place in its parent and on the canvas, by ID. */
|
|
13
|
+
roots: Map<string, SelectionPlacement>
|
|
14
|
+
/** Every layer the edit changes, roots first; locked layers inside are left out. */
|
|
15
|
+
nodes: Map<string, SceneNode>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Records the roots and the layers inside them that Figma's Scale tool changes. */
|
|
19
|
+
export function snapshotScale(graph: SceneGraph, roots: readonly SceneNode[]): ScaleSnapshot {
|
|
20
|
+
const snapshot: ScaleSnapshot = { roots: new Map(), nodes: new Map() }
|
|
21
|
+
for (const root of roots) {
|
|
22
|
+
snapshot.roots.set(root.id, selectionPlacement(root, graph))
|
|
23
|
+
for (const node of rescaleTargets(graph, root, { keepLocked: true }))
|
|
24
|
+
snapshot.nodes.set(node.id, { ...node })
|
|
25
|
+
}
|
|
26
|
+
return snapshot
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* What scaling the snapshot evenly by `scale` about a canvas point writes to each layer: its
|
|
31
|
+
* lengths, and for a root its place, so the point stays put and the root keeps its angle.
|
|
32
|
+
*/
|
|
33
|
+
export function scaledSnapshotChanges(
|
|
34
|
+
graph: SceneGraph,
|
|
35
|
+
snapshot: ScaleSnapshot,
|
|
36
|
+
scale: number,
|
|
37
|
+
anchor: Vector
|
|
38
|
+
): Map<string, Partial<SceneNode>> {
|
|
39
|
+
const changes = new Map<string, Partial<SceneNode>>()
|
|
40
|
+
for (const [id, node] of snapshot.nodes) {
|
|
41
|
+
const placement = snapshot.roots.get(id)
|
|
42
|
+
const change = rescaleNodeChanges(graph, node, scale, placement !== undefined)
|
|
43
|
+
// At its own size, a root takes back exactly the place it started from.
|
|
44
|
+
if (placement) {
|
|
45
|
+
const { x, y } = scale === 1 ? node : placeScaledAbout(placement, anchor, scale)
|
|
46
|
+
Object.assign(change, { x, y })
|
|
47
|
+
}
|
|
48
|
+
changes.set(id, change)
|
|
49
|
+
}
|
|
50
|
+
return changes
|
|
51
|
+
}
|
package/src/scaling/tree.ts
CHANGED
|
@@ -4,7 +4,9 @@ import type { SceneNode } from '../types'
|
|
|
4
4
|
import { scaleVariableBindingUnits } from '../variables/units'
|
|
5
5
|
import { scaleNodeChanges } from './node'
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/** The smallest scale a node tree takes. */
|
|
8
|
+
export const MIN_RESCALE = 0.01
|
|
9
|
+
|
|
8
10
|
const RESCALABLE_NODE_TYPES = new Set<SceneNode['type']>([
|
|
9
11
|
'BOOLEAN_OPERATION',
|
|
10
12
|
'COMPONENT',
|
|
@@ -22,30 +24,72 @@ const RESCALABLE_NODE_TYPES = new Set<SceneNode['type']>([
|
|
|
22
24
|
'VECTOR'
|
|
23
25
|
])
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
export interface RescaleOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Locked layers below the root, and everything inside them, keep their size and their place in
|
|
30
|
+
* their parent, as Figma's Scale tool leaves them; the plugin API's `rescale()` scales them.
|
|
31
|
+
*/
|
|
32
|
+
keepLocked?: boolean
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The selected layers the Scale tool scales: unlocked layers of a type that rescales, outside any
|
|
37
|
+
* instance, and not inside another of them, which scales them with itself.
|
|
38
|
+
*/
|
|
39
|
+
export function scaleRoots(graph: SceneGraph, nodes: readonly SceneNode[]): SceneNode[] {
|
|
40
|
+
const ids = new Set(nodes.map((node) => node.id))
|
|
41
|
+
return nodes.filter((node) => {
|
|
42
|
+
if (node.locked || !RESCALABLE_NODE_TYPES.has(node.type) || !node.parentId) return false
|
|
43
|
+
if (findInstanceAncestor(graph, node.parentId)) return false
|
|
44
|
+
return !graph.closest(node.parentId, (parent) => ids.has(parent.id))
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The root and every layer below it that a rescale changes, root first. */
|
|
49
|
+
export function rescaleTargets(
|
|
50
|
+
graph: SceneGraph,
|
|
51
|
+
root: SceneNode,
|
|
52
|
+
{ keepLocked = false }: RescaleOptions = {}
|
|
53
|
+
): SceneNode[] {
|
|
32
54
|
const nodes: SceneNode[] = [root]
|
|
33
55
|
const collect = (node: SceneNode): void => {
|
|
34
|
-
for (const
|
|
35
|
-
|
|
36
|
-
if (!child) continue
|
|
56
|
+
for (const child of graph.getChildren(node.id)) {
|
|
57
|
+
if (keepLocked && child.locked) continue
|
|
37
58
|
nodes.push(child)
|
|
38
59
|
collect(child)
|
|
39
60
|
}
|
|
40
61
|
}
|
|
41
62
|
collect(root)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
63
|
+
return nodes
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* What a rescale by `scale` writes to `node`: its own lengths and those of its bound variables,
|
|
68
|
+
* the scale of an instance's occurrence, and, below the root, its place in its parent.
|
|
69
|
+
*/
|
|
70
|
+
export function rescaleNodeChanges(
|
|
71
|
+
graph: SceneGraph,
|
|
72
|
+
node: SceneNode,
|
|
73
|
+
scale: number,
|
|
74
|
+
isRoot: boolean
|
|
75
|
+
): Partial<SceneNode> {
|
|
76
|
+
return {
|
|
77
|
+
...scaleNodeChanges(node, scale, !isRoot),
|
|
78
|
+
...scaleVariableBindingUnits(node, scale),
|
|
79
|
+
componentScale: findInstanceAncestor(graph, node.id)
|
|
80
|
+
? node.componentScale * scale
|
|
81
|
+
: node.componentScale
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Scale a node tree from the root node's top-left, matching Figma's `rescale()`. */
|
|
86
|
+
export function rescaleNodeTree(graph: SceneGraph, rootId: string, scale: number): void {
|
|
87
|
+
if (!Number.isFinite(scale)) throw new TypeError('Scale must be a finite number')
|
|
88
|
+
if (scale < MIN_RESCALE) throw new RangeError(`Scale must be at least ${MIN_RESCALE}`)
|
|
89
|
+
const root = graph.getNode(rootId)
|
|
90
|
+
if (!root || !RESCALABLE_NODE_TYPES.has(root.type))
|
|
91
|
+
throw new Error('rescale() is not supported on this node')
|
|
92
|
+
for (const node of rescaleTargets(graph, root)) {
|
|
93
|
+
graph.updateNode(node.id, rescaleNodeChanges(graph, node, scale, node.id === rootId))
|
|
50
94
|
}
|
|
51
95
|
}
|