@open-pencil/scene-graph 0.15.1 → 0.16.0-beta.1
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/LICENSE +21 -0
- package/README.md +1 -1
- package/dist/chunks/behaviours/kinds.js +273 -0
- package/dist/chunks/behaviours/kinds.js.map +1 -0
- package/dist/chunks/behaviours/layers.js +596 -0
- package/dist/chunks/behaviours/layers.js.map +1 -0
- package/dist/chunks/behaviours/spec.js +181 -0
- package/dist/chunks/behaviours/spec.js.map +1 -0
- package/dist/chunks/buffered-events.js +57 -0
- package/dist/chunks/buffered-events.js.map +1 -0
- package/dist/chunks/color/contrast.js +57 -0
- package/dist/chunks/color/contrast.js.map +1 -0
- package/dist/chunks/{components/properties.js → color/index.js} +573 -521
- package/dist/chunks/color/index.js.map +1 -0
- package/dist/chunks/comments/document.js +32 -0
- package/dist/chunks/comments/document.js.map +1 -0
- package/dist/chunks/comments/list.js +182 -0
- package/dist/chunks/comments/list.js.map +1 -0
- package/dist/chunks/comments/merge.js +58 -0
- package/dist/chunks/comments/merge.js.map +1 -0
- package/dist/chunks/components/exposure.js +2021 -0
- package/dist/chunks/components/exposure.js.map +1 -0
- package/dist/chunks/components/variants.js +32 -0
- package/dist/chunks/components/variants.js.map +1 -0
- package/dist/chunks/coordinate.js +93 -55
- package/dist/chunks/coordinate.js.map +1 -1
- package/dist/chunks/css/grid.js +94 -0
- package/dist/chunks/css/grid.js.map +1 -0
- package/dist/chunks/css/index.js +160 -0
- package/dist/chunks/css/index.js.map +1 -0
- package/dist/chunks/fields/geometry.js +14 -0
- package/dist/chunks/fields/geometry.js.map +1 -0
- package/dist/chunks/fields/text.js +36 -0
- package/dist/chunks/fields/text.js.map +1 -0
- package/dist/chunks/group-bounds.js +104 -0
- package/dist/chunks/group-bounds.js.map +1 -0
- package/dist/chunks/icons/glyph.js +147 -0
- package/dist/chunks/icons/glyph.js.map +1 -0
- package/dist/chunks/index.js +1711 -661
- package/dist/chunks/index.js.map +1 -1
- package/dist/color.d.ts +72 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +3 -0
- package/dist/constants.d.ts +2 -2
- package/dist/constants.js +2 -2
- package/dist/constants2.d.ts +3 -1
- package/dist/constants2.d.ts.map +1 -1
- package/dist/coordinate.d.ts +2 -448
- package/dist/coordinate.js +2 -2
- package/dist/copy.d.ts +1 -1
- package/dist/copy.js +1 -1
- package/dist/css.d.ts +39 -0
- package/dist/css.d.ts.map +1 -0
- package/dist/css.js +3 -0
- package/dist/geometry.d.ts +1 -1
- package/dist/geometry.js +1 -1
- package/dist/gradient.d.ts +51 -0
- package/dist/gradient.d.ts.map +1 -0
- package/dist/gradient.js +132 -0
- package/dist/gradient.js.map +1 -0
- package/dist/guides.d.ts +2 -2
- package/dist/guides.js +39 -0
- package/dist/guides.js.map +1 -0
- package/dist/hit-test.d.ts +2 -9
- package/dist/hit-test.js +154 -18
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +4 -8
- package/dist/index.js +20 -5
- package/dist/instances.d.ts +2 -2
- package/dist/instances.js +2 -2
- package/dist/layout.d.ts +2 -0
- package/dist/layout.js +4 -0
- package/dist/node-defaults.d.ts +2 -8
- package/dist/node-defaults.js +2 -2
- package/dist/order-keys.d.ts +39 -0
- package/dist/order-keys.d.ts.map +1 -0
- package/dist/order-keys.js +136 -0
- package/dist/order-keys.js.map +1 -0
- package/dist/parse-path.d.ts +7 -1
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js +6 -2
- package/dist/parse-path.js.map +1 -1
- package/dist/polygon.d.ts +53 -0
- package/dist/polygon.d.ts.map +1 -0
- package/dist/polygon.js +205 -0
- package/dist/polygon.js.map +1 -0
- package/dist/preview.d.ts +1 -1
- package/dist/random.d.ts +7 -0
- package/dist/random.d.ts.map +1 -0
- package/dist/random.js +2 -0
- package/dist/resize.d.ts +11 -2
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +2 -184
- package/dist/snap.d.ts +1 -1
- package/dist/text-direction.d.ts +13 -0
- package/dist/text-direction.d.ts.map +1 -0
- package/dist/text-direction.js +31 -0
- package/dist/text-direction.js.map +1 -0
- package/dist/text-picture.d.ts +1 -1
- package/dist/text-picture.d.ts.map +1 -1
- package/dist/text.d.ts +2 -0
- package/dist/text.js +3 -0
- package/dist/types.d.ts +2063 -13
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -1
- package/dist/types2.d.ts +2 -2
- package/dist/undo.d.ts +2 -2
- package/dist/undo.js +2 -2
- package/dist/undo2.d.ts +29 -1
- package/dist/undo2.d.ts.map +1 -1
- package/dist/variables.d.ts +2 -29
- package/dist/variables.js +2 -2
- package/dist/variant-properties.d.ts +9 -1
- package/dist/variant-properties.d.ts.map +1 -1
- package/dist/variant-properties.js +36 -1
- package/dist/variant-properties.js.map +1 -1
- package/dist/vector-network.d.ts +1 -1
- package/dist/vector-network.js +1 -1
- package/package.json +60 -9
- package/src/behaviours/kinds.ts +185 -0
- package/src/behaviours/layers.ts +51 -0
- package/src/behaviours/model.ts +192 -0
- package/src/behaviours/schema.ts +58 -0
- package/src/behaviours/spec.ts +223 -0
- package/src/buffered-events.ts +64 -0
- package/src/color/contrast.ts +31 -0
- package/src/color/index.ts +88 -0
- package/src/color/management.ts +210 -0
- package/src/color/normalize.ts +7 -0
- package/src/color/okhcl.ts +201 -0
- package/src/comments/document.ts +51 -0
- package/src/comments/list.ts +52 -0
- package/src/comments/merge.ts +63 -0
- package/src/comments/threads.ts +156 -0
- package/src/components/exposure.ts +61 -0
- package/src/components/properties.ts +18 -15
- package/src/components/variants.ts +53 -0
- package/src/constants.ts +2 -0
- package/src/coordinate.ts +100 -43
- package/src/copy.ts +13 -5
- package/src/css/grid.ts +70 -0
- package/src/css/index.ts +9 -0
- package/src/css/layout.ts +53 -0
- package/src/css/shadows.ts +68 -0
- package/src/css/values.ts +42 -0
- package/src/export-format.ts +12 -0
- package/src/fields/geometry.ts +11 -0
- package/src/fields/text.ts +35 -0
- package/src/gradient.ts +166 -0
- package/src/group-bounds.ts +131 -0
- package/src/guides.ts +39 -0
- package/src/hit-test.ts +219 -25
- package/src/icons/glyph.ts +128 -0
- package/src/icons/model.ts +54 -0
- package/src/icons/schema.ts +18 -0
- package/src/index.ts +343 -55
- package/src/instance-overrides.ts +27 -0
- package/src/instances/copies.ts +239 -0
- package/src/instances/cycles.ts +73 -0
- package/src/instances/fields.ts +143 -0
- package/src/instances/main-component.ts +19 -0
- package/src/instances/placement.ts +29 -0
- package/src/instances/source-walk.ts +41 -0
- package/src/instances/sync.ts +599 -0
- package/src/instances.ts +122 -330
- package/src/layout/aspect-ratio.ts +51 -0
- package/src/{layout-guides.ts → layout/guides.ts} +1 -1
- package/src/layout/index.ts +4 -0
- package/src/layout/keywords.ts +73 -0
- package/src/layout/sizing.ts +196 -0
- package/src/mutation-impact.ts +25 -10
- package/src/node-defaults.ts +50 -2
- package/src/order-keys.ts +162 -0
- package/src/parse-path.ts +10 -2
- package/src/plugin-data/field.ts +131 -0
- package/src/plugin-data/fields.ts +246 -0
- package/src/polygon.ts +211 -0
- package/src/preview.ts +1 -1
- package/src/random.ts +28 -0
- package/src/resize.ts +99 -12
- package/src/scaling/index.ts +2 -0
- package/src/scaling/node.ts +164 -0
- package/src/scaling/tree.ts +50 -0
- package/src/shaders/model.ts +98 -0
- package/src/shaders/schema.ts +48 -0
- package/src/slots/authoring.ts +101 -0
- package/src/slots/content.ts +124 -0
- package/src/slots/frames.ts +160 -0
- package/src/slots/limits.ts +49 -0
- package/src/text/direction.ts +55 -0
- package/src/text/index.ts +2 -0
- package/src/text/lists.ts +138 -0
- package/src/text/paragraphs.ts +241 -0
- package/src/{text-picture.ts → text/picture.ts} +21 -29
- package/src/transfer/apply.ts +84 -0
- package/src/transfer/history.ts +49 -0
- package/src/transfer/identity.ts +16 -0
- package/src/transfer/index.ts +6 -0
- package/src/transfer/nodes.ts +108 -0
- package/src/transfer/plan.ts +107 -0
- package/src/transfer/references.ts +9 -0
- package/src/transfer/remove.ts +39 -0
- package/src/transfer/validation.ts +54 -0
- package/src/transfer/variables.ts +82 -0
- package/src/types.ts +221 -11
- package/src/undo.ts +54 -2
- package/src/variables/bindings.ts +186 -0
- package/src/variables/fields.ts +43 -0
- package/src/variables/token.ts +14 -0
- package/src/variables/units.ts +15 -0
- package/src/variables.ts +118 -69
- package/src/variant-properties.ts +50 -0
- package/dist/chunks/components/properties.js.map +0 -1
- package/dist/chunks/export-scale.js +0 -19
- package/dist/chunks/export-scale.js.map +0 -1
- package/dist/coordinate.d.ts.map +0 -1
- package/dist/coordinate2.d.ts +0 -2
- package/dist/guides2.d.ts +0 -11
- package/dist/guides2.d.ts.map +0 -1
- package/dist/hit-test.d.ts.map +0 -1
- package/dist/layout-guides.d.ts +0 -2
- package/dist/layout-guides.js +0 -2
- package/dist/layout-guides2.d.ts +0 -16
- package/dist/layout-guides2.d.ts.map +0 -1
- package/dist/node-defaults.d.ts.map +0 -1
- package/dist/preview2.d.ts +0 -12
- package/dist/preview2.d.ts.map +0 -1
- package/dist/resize.js.map +0 -1
- package/dist/variables.d.ts.map +0 -1
- package/dist/vector-network2.d.ts +0 -32
- package/dist/vector-network2.d.ts.map +0 -1
- package/src/instance-overrides.test.ts +0 -67
package/dist/polygon.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { polygonVertices } from "./chunks/color/index.js";
|
|
2
|
+
//#region src/polygon.ts
|
|
3
|
+
/**
|
|
4
|
+
* Rounded polygon and star outlines, as Figma draws them. Each corner, convex or not, is rounded by
|
|
5
|
+
* a circular arc tangent to both edges, capped so that it and its neighbours fit along every edge;
|
|
6
|
+
* corner smoothing replaces the arc's ends with curves that ease into the edges, as on rectangles.
|
|
7
|
+
*/
|
|
8
|
+
/** Figma keeps a polygon's sides or a star's points within this range, and its plugin API clamps to it. */
|
|
9
|
+
const POINT_COUNT_RANGE = {
|
|
10
|
+
min: 3,
|
|
11
|
+
max: 60
|
|
12
|
+
};
|
|
13
|
+
const sub = (a, b) => ({
|
|
14
|
+
x: a.x - b.x,
|
|
15
|
+
y: a.y - b.y
|
|
16
|
+
});
|
|
17
|
+
const along = (point, direction, distance) => ({
|
|
18
|
+
x: point.x + direction.x * distance,
|
|
19
|
+
y: point.y + direction.y * distance
|
|
20
|
+
});
|
|
21
|
+
function unit(vector) {
|
|
22
|
+
const length = Math.hypot(vector.x, vector.y);
|
|
23
|
+
return length > 0 ? {
|
|
24
|
+
x: vector.x / length,
|
|
25
|
+
y: vector.y / length
|
|
26
|
+
} : {
|
|
27
|
+
x: 0,
|
|
28
|
+
y: 0
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** The perpendicular of `direction` on the side the outline turns towards. */
|
|
32
|
+
function inward(direction, towards) {
|
|
33
|
+
const normal = {
|
|
34
|
+
x: -direction.y,
|
|
35
|
+
y: direction.x
|
|
36
|
+
};
|
|
37
|
+
return normal.x * towards.x + normal.y * towards.y < 0 ? {
|
|
38
|
+
x: direction.y,
|
|
39
|
+
y: -direction.x
|
|
40
|
+
} : normal;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The polygon's or star's corners with the radius each is drawn with. A corner turning by α keeps
|
|
44
|
+
* r·tan(α/2) of each edge, so on an edge of length L between corners turning by α and β the radius
|
|
45
|
+
* is at most L / (tan(α/2) + tan(β/2)); each corner takes the least of its two edges' limits.
|
|
46
|
+
*/
|
|
47
|
+
function polygonCorners(node) {
|
|
48
|
+
const vertices = polygonVertices(node);
|
|
49
|
+
const count = vertices.length;
|
|
50
|
+
const at = (index) => vertices[(index + count) % count];
|
|
51
|
+
const shape = vertices.map((vertex, index) => {
|
|
52
|
+
const incoming = unit(sub(vertex, at(index - 1)));
|
|
53
|
+
const outgoing = unit(sub(at(index + 1), vertex));
|
|
54
|
+
const cos = Math.min(1, Math.max(-1, incoming.x * outgoing.x + incoming.y * outgoing.y));
|
|
55
|
+
const turn = Math.acos(cos);
|
|
56
|
+
return {
|
|
57
|
+
vertex,
|
|
58
|
+
incoming,
|
|
59
|
+
outgoing,
|
|
60
|
+
turn,
|
|
61
|
+
factor: Math.tan(turn / 2)
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
const edgeLimit = (index) => {
|
|
65
|
+
const from = shape[(index + count) % count];
|
|
66
|
+
const to = shape[(index + 1 + count) % count];
|
|
67
|
+
const length = Math.hypot(to.vertex.x - from.vertex.x, to.vertex.y - from.vertex.y);
|
|
68
|
+
const factors = from.factor + to.factor;
|
|
69
|
+
return factors > 0 ? length / factors : Number.POSITIVE_INFINITY;
|
|
70
|
+
};
|
|
71
|
+
const requested = Math.max(0, node.cornerRadius);
|
|
72
|
+
return shape.map(({ factor, ...corner }, index) => {
|
|
73
|
+
const radius = Math.min(requested, edgeLimit(index - 1), edgeLimit(index));
|
|
74
|
+
return {
|
|
75
|
+
...corner,
|
|
76
|
+
radius,
|
|
77
|
+
extent: radius * factor
|
|
78
|
+
};
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Figma's smoothing for a corner turning by any angle: the rectangle construction with the
|
|
83
|
+
* tangent length r·tan(α/2) in place of r and α/2 in place of 45°. Smoothing grows the rounded
|
|
84
|
+
* part of each edge by up to its tangent length again.
|
|
85
|
+
*/
|
|
86
|
+
function smoothCorner(corner, smoothing) {
|
|
87
|
+
const { radius, extent } = corner;
|
|
88
|
+
const p = (1 + smoothing) * extent;
|
|
89
|
+
const ease = corner.turn * smoothing / 2;
|
|
90
|
+
const toEase = radius * Math.tan(ease / 2);
|
|
91
|
+
const c = toEase * Math.cos(ease);
|
|
92
|
+
const d = toEase * Math.sin(ease);
|
|
93
|
+
const b = (p - (extent - radius * Math.sin(ease) - d) - c - d) / 3;
|
|
94
|
+
return {
|
|
95
|
+
p,
|
|
96
|
+
a: 2 * b,
|
|
97
|
+
b,
|
|
98
|
+
c,
|
|
99
|
+
d,
|
|
100
|
+
ease
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function rotate(direction, towards, angle) {
|
|
104
|
+
const normal = inward(direction, towards);
|
|
105
|
+
return {
|
|
106
|
+
x: direction.x * Math.cos(angle) + normal.x * Math.sin(angle),
|
|
107
|
+
y: direction.y * Math.cos(angle) + normal.y * Math.sin(angle)
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function cubic(c1, c2, to) {
|
|
111
|
+
return {
|
|
112
|
+
type: "C",
|
|
113
|
+
x1: c1.x,
|
|
114
|
+
y1: c1.y,
|
|
115
|
+
x2: c2.x,
|
|
116
|
+
y2: c2.y,
|
|
117
|
+
x: to.x,
|
|
118
|
+
y: to.y
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** The rounded corner from where it leaves the incoming edge to where it joins the outgoing one. */
|
|
122
|
+
function cornerCommands(corner, smooth) {
|
|
123
|
+
const { vertex, incoming, outgoing, radius, turn } = corner;
|
|
124
|
+
const { p, a, b, c, d, ease } = smooth;
|
|
125
|
+
const back = {
|
|
126
|
+
x: -incoming.x,
|
|
127
|
+
y: -incoming.y
|
|
128
|
+
};
|
|
129
|
+
const start = along(vertex, back, p);
|
|
130
|
+
const end = along(vertex, outgoing, p);
|
|
131
|
+
const normalIn = inward(incoming, outgoing);
|
|
132
|
+
const normalOut = inward(outgoing, back);
|
|
133
|
+
const arcFrom = along(along(start, incoming, a + b + c), normalIn, d);
|
|
134
|
+
const arcTo = along(along(end, outgoing, -(a + b + c)), normalOut, d);
|
|
135
|
+
const commands = [];
|
|
136
|
+
if (ease > 0) commands.push(cubic(along(start, incoming, a), along(start, incoming, a + b), arcFrom));
|
|
137
|
+
const sweep = turn - 2 * ease;
|
|
138
|
+
if (sweep > 0) {
|
|
139
|
+
const handle = 4 / 3 * Math.tan(sweep / 4) * radius;
|
|
140
|
+
const leaving = rotate(incoming, outgoing, ease);
|
|
141
|
+
const arriving = rotate(outgoing, incoming, ease);
|
|
142
|
+
commands.push(cubic(along(arcFrom, leaving, handle), along(arcTo, arriving, -handle), arcTo));
|
|
143
|
+
}
|
|
144
|
+
if (ease > 0) commands.push(cubic(along(end, outgoing, -(a + b)), along(end, outgoing, -a), end));
|
|
145
|
+
return commands;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The smoothing every corner can take: on each edge a corner may use the part its tangent length
|
|
149
|
+
* takes of both corners' tangent lengths, and Figma lowers smoothing for the whole shape to what
|
|
150
|
+
* its tightest corner allows rather than corner by corner.
|
|
151
|
+
*/
|
|
152
|
+
function fittedSmoothing(corners, smoothing) {
|
|
153
|
+
const count = corners.length;
|
|
154
|
+
const share = (corner, neighbour) => {
|
|
155
|
+
return Math.hypot(neighbour.vertex.x - corner.vertex.x, neighbour.vertex.y - corner.vertex.y) * corner.extent / (corner.extent + neighbour.extent);
|
|
156
|
+
};
|
|
157
|
+
return corners.reduce((fitted, corner, index) => {
|
|
158
|
+
if (!(corner.extent > 0)) return fitted;
|
|
159
|
+
const budget = Math.min(share(corner, corners[(index - 1 + count) % count]), share(corner, corners[(index + 1) % count]));
|
|
160
|
+
return Math.min(fitted, budget / corner.extent - 1);
|
|
161
|
+
}, Math.min(Math.max(smoothing, 0), 1));
|
|
162
|
+
}
|
|
163
|
+
/** The outline of a polygon or star with its corner radius and smoothing, in its own pixels. */
|
|
164
|
+
function polygonOutline(node) {
|
|
165
|
+
const corners = polygonCorners(node);
|
|
166
|
+
const smoothing = Math.max(0, fittedSmoothing(corners, node.cornerSmoothing));
|
|
167
|
+
const commands = [];
|
|
168
|
+
let current = null;
|
|
169
|
+
const lineTo = ({ x, y }) => {
|
|
170
|
+
if (current && Math.hypot(x - current.x, y - current.y) < 1e-6) return;
|
|
171
|
+
commands.push({
|
|
172
|
+
type: current ? "L" : "M",
|
|
173
|
+
x,
|
|
174
|
+
y
|
|
175
|
+
});
|
|
176
|
+
current = {
|
|
177
|
+
x,
|
|
178
|
+
y
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
for (const corner of corners) {
|
|
182
|
+
if (!(corner.radius > 0) || !(corner.turn > 0)) {
|
|
183
|
+
lineTo(corner.vertex);
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const smooth = smoothCorner(corner, smoothing);
|
|
187
|
+
lineTo(along(corner.vertex, {
|
|
188
|
+
x: -corner.incoming.x,
|
|
189
|
+
y: -corner.incoming.y
|
|
190
|
+
}, smooth.p));
|
|
191
|
+
for (const command of cornerCommands(corner, smooth)) {
|
|
192
|
+
commands.push(command);
|
|
193
|
+
if (command.type === "C") current = {
|
|
194
|
+
x: command.x,
|
|
195
|
+
y: command.y
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
commands.push({ type: "Z" });
|
|
200
|
+
return commands;
|
|
201
|
+
}
|
|
202
|
+
//#endregion
|
|
203
|
+
export { POINT_COUNT_RANGE, polygonCorners, polygonOutline };
|
|
204
|
+
|
|
205
|
+
//# sourceMappingURL=polygon.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polygon.js","names":[],"sources":["../src/polygon.ts"],"sourcesContent":["import { polygonVertices } from './geometry'\nimport type { Vector } from './primitives'\nimport type { SceneNode } from './types'\n\n/**\n * Rounded polygon and star outlines, as Figma draws them. Each corner, convex or not, is rounded by\n * a circular arc tangent to both edges, capped so that it and its neighbours fit along every edge;\n * corner smoothing replaces the arc's ends with curves that ease into the edges, as on rectangles.\n */\n\n/** Figma keeps a polygon's sides or a star's points within this range, and its plugin API clamps to it. */\nexport const POINT_COUNT_RANGE = { min: 3, max: 60 } as const\n\nexport type PolygonOutlineCommand =\n | { type: 'M' | 'L'; x: number; y: number }\n | { type: 'C'; x1: number; y1: number; x2: number; y2: number; x: number; y: number }\n | { type: 'Z' }\n\ntype PolygonNode = Pick<\n SceneNode,\n | 'type'\n | 'width'\n | 'height'\n | 'pointCount'\n | 'starInnerRadius'\n | 'cornerRadius'\n | 'cornerSmoothing'\n>\n\nexport interface PolygonCorner {\n vertex: Vector\n /** The unit direction of the edge arriving at the vertex. */\n incoming: Vector\n /** The unit direction of the edge leaving the vertex. */\n outgoing: Vector\n /** How far the outline turns at the vertex, in radians: 0 for a straight line. */\n turn: number\n /** The radius the corner is drawn with, after fitting it along its edges. */\n radius: number\n /** How far along each edge from the vertex the rounding starts. */\n extent: number\n}\n\nconst sub = (a: Vector, b: Vector): Vector => ({ x: a.x - b.x, y: a.y - b.y })\nconst along = (point: Vector, direction: Vector, distance: number): Vector => ({\n x: point.x + direction.x * distance,\n y: point.y + direction.y * distance\n})\n\nfunction unit(vector: Vector): Vector {\n const length = Math.hypot(vector.x, vector.y)\n return length > 0 ? { x: vector.x / length, y: vector.y / length } : { x: 0, y: 0 }\n}\n\n/** The perpendicular of `direction` on the side the outline turns towards. */\nfunction inward(direction: Vector, towards: Vector): Vector {\n const normal = { x: -direction.y, y: direction.x }\n return normal.x * towards.x + normal.y * towards.y < 0\n ? { x: direction.y, y: -direction.x }\n : normal\n}\n\n/**\n * The polygon's or star's corners with the radius each is drawn with. A corner turning by α keeps\n * r·tan(α/2) of each edge, so on an edge of length L between corners turning by α and β the radius\n * is at most L / (tan(α/2) + tan(β/2)); each corner takes the least of its two edges' limits.\n */\nexport function polygonCorners(node: PolygonNode): PolygonCorner[] {\n const vertices = polygonVertices(node)\n const count = vertices.length\n const at = (index: number) => vertices[(index + count) % count]\n const shape = vertices.map((vertex, index) => {\n const incoming = unit(sub(vertex, at(index - 1)))\n const outgoing = unit(sub(at(index + 1), vertex))\n const cos = Math.min(1, Math.max(-1, incoming.x * outgoing.x + incoming.y * outgoing.y))\n const turn = Math.acos(cos)\n return { vertex, incoming, outgoing, turn, factor: Math.tan(turn / 2) }\n })\n const edgeLimit = (index: number) => {\n const from = shape[(index + count) % count]\n const to = shape[(index + 1 + count) % count]\n const length = Math.hypot(to.vertex.x - from.vertex.x, to.vertex.y - from.vertex.y)\n const factors = from.factor + to.factor\n return factors > 0 ? length / factors : Number.POSITIVE_INFINITY\n }\n const requested = Math.max(0, node.cornerRadius)\n return shape.map(({ factor, ...corner }, index) => {\n const radius = Math.min(requested, edgeLimit(index - 1), edgeLimit(index))\n return { ...corner, radius, extent: radius * factor }\n })\n}\n\ninterface SmoothCorner {\n /** Where the rounding starts, along each edge from the vertex. */\n p: number\n a: number\n b: number\n c: number\n d: number\n /** The angle each eased end takes from the arc, in radians. */\n ease: number\n}\n\n/**\n * Figma's smoothing for a corner turning by any angle: the rectangle construction with the\n * tangent length r·tan(α/2) in place of r and α/2 in place of 45°. Smoothing grows the rounded\n * part of each edge by up to its tangent length again.\n */\nfunction smoothCorner(corner: PolygonCorner, smoothing: number): SmoothCorner {\n const { radius, extent } = corner\n const p = (1 + smoothing) * extent\n const ease = (corner.turn * smoothing) / 2\n const toEase = radius * Math.tan(ease / 2)\n const c = toEase * Math.cos(ease)\n const d = toEase * Math.sin(ease)\n const arcStart = extent - radius * Math.sin(ease) - d\n const b = (p - arcStart - c - d) / 3\n return { p, a: 2 * b, b, c, d, ease }\n}\n\nfunction rotate(direction: Vector, towards: Vector, angle: number): Vector {\n const normal = inward(direction, towards)\n return {\n x: direction.x * Math.cos(angle) + normal.x * Math.sin(angle),\n y: direction.y * Math.cos(angle) + normal.y * Math.sin(angle)\n }\n}\n\nfunction cubic(c1: Vector, c2: Vector, to: Vector): PolygonOutlineCommand {\n return { type: 'C', x1: c1.x, y1: c1.y, x2: c2.x, y2: c2.y, x: to.x, y: to.y }\n}\n\n/** The rounded corner from where it leaves the incoming edge to where it joins the outgoing one. */\nfunction cornerCommands(corner: PolygonCorner, smooth: SmoothCorner): PolygonOutlineCommand[] {\n const { vertex, incoming, outgoing, radius, turn } = corner\n const { p, a, b, c, d, ease } = smooth\n const back = { x: -incoming.x, y: -incoming.y }\n const start = along(vertex, back, p)\n const end = along(vertex, outgoing, p)\n const normalIn = inward(incoming, outgoing)\n const normalOut = inward(outgoing, back)\n const arcFrom = along(along(start, incoming, a + b + c), normalIn, d)\n const arcTo = along(along(end, outgoing, -(a + b + c)), normalOut, d)\n const commands: PolygonOutlineCommand[] = []\n if (ease > 0)\n commands.push(cubic(along(start, incoming, a), along(start, incoming, a + b), arcFrom))\n const sweep = turn - 2 * ease\n if (sweep > 0) {\n const handle = (4 / 3) * Math.tan(sweep / 4) * radius\n const leaving = rotate(incoming, outgoing, ease)\n const arriving = rotate(outgoing, incoming, ease)\n commands.push(cubic(along(arcFrom, leaving, handle), along(arcTo, arriving, -handle), arcTo))\n }\n if (ease > 0) commands.push(cubic(along(end, outgoing, -(a + b)), along(end, outgoing, -a), end))\n return commands\n}\n\n/**\n * The smoothing every corner can take: on each edge a corner may use the part its tangent length\n * takes of both corners' tangent lengths, and Figma lowers smoothing for the whole shape to what\n * its tightest corner allows rather than corner by corner.\n */\nfunction fittedSmoothing(corners: readonly PolygonCorner[], smoothing: number): number {\n const count = corners.length\n const share = (corner: PolygonCorner, neighbour: PolygonCorner) => {\n const length = Math.hypot(\n neighbour.vertex.x - corner.vertex.x,\n neighbour.vertex.y - corner.vertex.y\n )\n return (length * corner.extent) / (corner.extent + neighbour.extent)\n }\n return corners.reduce(\n (fitted, corner, index) => {\n if (!(corner.extent > 0)) return fitted\n const budget = Math.min(\n share(corner, corners[(index - 1 + count) % count]),\n share(corner, corners[(index + 1) % count])\n )\n return Math.min(fitted, budget / corner.extent - 1)\n },\n Math.min(Math.max(smoothing, 0), 1)\n )\n}\n\n/** The outline of a polygon or star with its corner radius and smoothing, in its own pixels. */\nexport function polygonOutline(node: PolygonNode): PolygonOutlineCommand[] {\n const corners = polygonCorners(node)\n const smoothing = Math.max(0, fittedSmoothing(corners, node.cornerSmoothing))\n const commands: PolygonOutlineCommand[] = []\n let current: Vector | null = null\n // Corners whose rounding meets along an edge leave no line between them.\n const lineTo = ({ x, y }: Vector) => {\n if (current && Math.hypot(x - current.x, y - current.y) < 1e-6) return\n commands.push({ type: current ? 'L' : 'M', x, y })\n current = { x, y }\n }\n for (const corner of corners) {\n if (!(corner.radius > 0) || !(corner.turn > 0)) {\n lineTo(corner.vertex)\n continue\n }\n const smooth = smoothCorner(corner, smoothing)\n lineTo(along(corner.vertex, { x: -corner.incoming.x, y: -corner.incoming.y }, smooth.p))\n for (const command of cornerCommands(corner, smooth)) {\n commands.push(command)\n if (command.type === 'C') current = { x: command.x, y: command.y }\n }\n }\n commands.push({ type: 'Z' })\n return commands\n}\n"],"mappings":";;;;;;;;AAWA,MAAa,oBAAoB;CAAE,KAAK;CAAG,KAAK;AAAG;AAgCnD,MAAM,OAAO,GAAW,OAAuB;CAAE,GAAG,EAAE,IAAI,EAAE;CAAG,GAAG,EAAE,IAAI,EAAE;AAAE;AAC5E,MAAM,SAAS,OAAe,WAAmB,cAA8B;CAC7E,GAAG,MAAM,IAAI,UAAU,IAAI;CAC3B,GAAG,MAAM,IAAI,UAAU,IAAI;AAC7B;AAEA,SAAS,KAAK,QAAwB;CACpC,MAAM,SAAS,KAAK,MAAM,OAAO,GAAG,OAAO,CAAC;CAC5C,OAAO,SAAS,IAAI;EAAE,GAAG,OAAO,IAAI;EAAQ,GAAG,OAAO,IAAI;CAAO,IAAI;EAAE,GAAG;EAAG,GAAG;CAAE;AACpF;;AAGA,SAAS,OAAO,WAAmB,SAAyB;CAC1D,MAAM,SAAS;EAAE,GAAG,CAAC,UAAU;EAAG,GAAG,UAAU;CAAE;CACjD,OAAO,OAAO,IAAI,QAAQ,IAAI,OAAO,IAAI,QAAQ,IAAI,IACjD;EAAE,GAAG,UAAU;EAAG,GAAG,CAAC,UAAU;CAAE,IAClC;AACN;;;;;;AAOA,SAAgB,eAAe,MAAoC;CACjE,MAAM,WAAW,gBAAgB,IAAI;CACrC,MAAM,QAAQ,SAAS;CACvB,MAAM,MAAM,UAAkB,UAAU,QAAQ,SAAS;CACzD,MAAM,QAAQ,SAAS,KAAK,QAAQ,UAAU;EAC5C,MAAM,WAAW,KAAK,IAAI,QAAQ,GAAG,QAAQ,CAAC,CAAC,CAAC;EAChD,MAAM,WAAW,KAAK,IAAI,GAAG,QAAQ,CAAC,GAAG,MAAM,CAAC;EACh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AU,MAAM;GAAK;GAAG;EAAE,CAAC;EACjD,UAAU;GAAE;GAAG;EAAE;CACnB;CACA,KAAK,MAAM,UAAU,SAAS;EAC5B,IAAI,EAAE,OAAO,SAAS,MAAM,EAAE,OAAO,OAAO,IAAI;GAC9C,OAAO,OAAO,MAAM;GACpB;EACF;EACA,MAAM,SAAS,aAAa,QAAQ,SAAS;EAC7C,OAAO,MAAM,OAAO,QAAQ;GAAE,GAAG,CAAC,OAAO,SAAS;GAAG,GAAG,CAAC,OAAO,SAAS;EAAE,GAAG,OAAO,CAAC,CAAC;EACvF,KAAK,MAAM,WAAW,eAAe,QAAQ,MAAM,GAAG;GACpD,SAAS,KAAK,OAAO;GACrB,IAAI,QAAQ,SAAS,KAAK,UAAU;IAAE,GAAG,QAAQ;IAAG,GAAG,QAAQ;GAAE;EACnE;CACF;CACA,SAAS,KAAK,EAAE,MAAM,IAAI,CAAC;CAC3B,OAAO;AACT"}
|
package/dist/preview.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { NodePreviewObserver, updateNodePreview } from "./
|
|
1
|
+
import { NodePreviewObserver, updateNodePreview } from "./types.js";
|
|
2
2
|
export { NodePreviewObserver, updateNodePreview };
|
package/dist/random.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/random.d.ts
|
|
2
|
+
declare function randomHex(bytes?: number): string;
|
|
3
|
+
declare function randomInt(): number;
|
|
4
|
+
declare function randomIndex(length: number): number;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { randomHex, randomIndex, randomInt };
|
|
7
|
+
//# sourceMappingURL=random.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random.d.ts","names":[],"sources":["../src/random.ts"],"mappings":";iBAAgB,UAAU;iBAUV;iBAIA,YAAY"}
|
package/dist/random.js
ADDED
package/dist/resize.d.ts
CHANGED
|
@@ -11,7 +11,16 @@ type ResizeSnapshot = Pick<SceneNode, 'x' | 'y' | 'width' | 'height' | 'vectorNe
|
|
|
11
11
|
interface ResizeGraph {
|
|
12
12
|
getNode(id: string): SceneNode | undefined;
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
/** A child's box after its parent resizes, keeping a locked aspect ratio as Figma does. */
|
|
15
|
+
declare function constrainedChildRect(child: Rect, parentBefore: Pick<Rect, 'width' | 'height'>, parentAfter: Pick<Rect, 'width' | 'height'>, horizontal: ConstraintType, vertical: ConstraintType, aspectRatio?: number | null): Rect;
|
|
16
|
+
/**
|
|
17
|
+
* Where a child goes when layout resizes its parent, as Figma places a layer that ignores auto
|
|
18
|
+
* layout when its Hug frame grows (measured on desktop 126): Right and Left & right move or grow
|
|
19
|
+
* it by the change in the parent's size rounded to whole pixels, Center by the change in its
|
|
20
|
+
* rounded half, and Scale by the exact ratio, its position to 1/128 of a pixel. Sizes are not
|
|
21
|
+
* rounded, so applying a run of changes one at a time lands where applying them at once does.
|
|
22
|
+
*/
|
|
23
|
+
declare function layoutConstrainedChildRect(child: Rect, parentBefore: Pick<Rect, 'width' | 'height'>, parentAfter: Pick<Rect, 'width' | 'height'>, horizontal: ConstraintType, vertical: ConstraintType, aspectRatio?: number | null): Rect;
|
|
15
24
|
declare function scaledChildRect(child: Rect, parentBefore: Pick<Rect, 'width' | 'height'>, parentAfter: Pick<Rect, 'width' | 'height'>): Rect;
|
|
16
25
|
declare function scaleVectorNetworkForResize(vectorNetwork: VectorNetwork | null, originalWidth: number, originalHeight: number, width: number, height: number): VectorNetwork | null;
|
|
17
26
|
/** Snapshot one node's resize-relevant state (rect + geometry that must scale with it). */
|
|
@@ -26,5 +35,5 @@ declare function scaledGeometryChanges(orig: Pick<ResizeSnapshot, 'vectorNetwork
|
|
|
26
35
|
declare function collectResizeDescendants(graph: ResizeGraph, rootId: string): Map<string, ResizeSnapshot> | null;
|
|
27
36
|
declare function computeConstrainedResizeChanges(graph: ResizeGraph, rootId: string, rootBefore: Pick<Rect, 'width' | 'height'>, rootAfter: Pick<Rect, 'width' | 'height'>, originals: ReadonlyMap<string, ResizeSnapshot>): Map<string, Partial<SceneNode>>;
|
|
28
37
|
//#endregion
|
|
29
|
-
export { ResizeSnapshot, collectResizeDescendants, computeConstrainedResizeChanges, constrainedChildRect, createResizeSnapshot, scaleVectorNetworkForResize, scaledChildRect, scaledGeometryChanges };
|
|
38
|
+
export { ResizeSnapshot, collectResizeDescendants, computeConstrainedResizeChanges, constrainedChildRect, createResizeSnapshot, layoutConstrainedChildRect, scaleVectorNetworkForResize, scaledChildRect, scaledGeometryChanges };
|
|
30
39
|
//# sourceMappingURL=resize.d.ts.map
|
package/dist/resize.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resize.d.ts","names":[],"sources":["../src/resize.ts"],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"resize.d.ts","names":[],"sources":["../src/resize.ts"],"mappings":";;;;;;;;;KAkBY,iBAAiB,KAC3B;UAcQ;EACR,QAAQ,aAAa;;;iBAgEP,qBACd,OAAO,MACP,cAAc,KAAK,2BACnB,aAAa,KAAK,2BAClB,YAAY,gBACZ,UAAU,gBACV,8BACC;;;;;;;;iBAuCa,2BACd,OAAO,MACP,cAAc,KAAK,2BACnB,aAAa,KAAK,2BAClB,YAAY,gBACZ,UAAU,gBACV,8BACC;iBAgBa,gBACd,OAAO,MACP,cAAc,KAAK,2BACnB,aAAa,KAAK,4BACjB;iBAIa,4BACd,eAAe,sBACf,uBACA,wBACA,eACA,iBACC;;iBA6Ba,qBAAqB,MAAM,YAAY;;;;;;;iBA6CvC,sBACd,MAAM,KACJ,yIASF,mBACA,oBACA,eACA,iBACC,QAAQ;iBA+CK,yBACd,OAAO,aACP,iBACC,YAAY;iBAoBC,gCACd,OAAO,aACP,gBACA,YAAY,KAAK,2BACjB,WAAW,KAAK,2BAChB,WAAW,oBAAoB,kBAC9B,YAAY,QAAQ"}
|
package/dist/resize.js
CHANGED
|
@@ -1,184 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
const CONSTRAINT_CONTAINER_TYPES = /* @__PURE__ */ new Set([
|
|
4
|
-
"FRAME",
|
|
5
|
-
"COMPONENT",
|
|
6
|
-
"COMPONENT_SET",
|
|
7
|
-
"INSTANCE",
|
|
8
|
-
"GROUP",
|
|
9
|
-
"BOOLEAN_OPERATION"
|
|
10
|
-
]);
|
|
11
|
-
function constrainedAxis(position, size, parentBefore, parentAfter, constraint) {
|
|
12
|
-
const delta = parentAfter - parentBefore;
|
|
13
|
-
if (constraint === "MAX") return {
|
|
14
|
-
position: position + delta,
|
|
15
|
-
size
|
|
16
|
-
};
|
|
17
|
-
if (constraint === "CENTER") return {
|
|
18
|
-
position: position + delta / 2,
|
|
19
|
-
size
|
|
20
|
-
};
|
|
21
|
-
if (constraint === "STRETCH") return {
|
|
22
|
-
position,
|
|
23
|
-
size: Math.max(1, size + delta)
|
|
24
|
-
};
|
|
25
|
-
if (constraint === "SCALE" && parentBefore > 0) {
|
|
26
|
-
const scale = parentAfter / parentBefore;
|
|
27
|
-
return {
|
|
28
|
-
position: position * scale,
|
|
29
|
-
size: Math.max(1, size * scale)
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
return {
|
|
33
|
-
position,
|
|
34
|
-
size
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
function constrainedChildRect(child, parentBefore, parentAfter, horizontal, vertical) {
|
|
38
|
-
const x = constrainedAxis(child.x, child.width, parentBefore.width, parentAfter.width, horizontal);
|
|
39
|
-
const y = constrainedAxis(child.y, child.height, parentBefore.height, parentAfter.height, vertical);
|
|
40
|
-
return {
|
|
41
|
-
x: Math.round(x.position),
|
|
42
|
-
y: Math.round(y.position),
|
|
43
|
-
width: Math.round(x.size),
|
|
44
|
-
height: Math.round(y.size)
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
function scaledChildRect(child, parentBefore, parentAfter) {
|
|
48
|
-
return constrainedChildRect(child, parentBefore, parentAfter, "SCALE", "SCALE");
|
|
49
|
-
}
|
|
50
|
-
function scaleVectorNetworkForResize(vectorNetwork, originalWidth, originalHeight, width, height) {
|
|
51
|
-
if (!vectorNetwork || originalWidth <= 0 || originalHeight <= 0) return null;
|
|
52
|
-
const scaleX = width / originalWidth;
|
|
53
|
-
const scaleY = height / originalHeight;
|
|
54
|
-
if (scaleX === 1 && scaleY === 1) return null;
|
|
55
|
-
return {
|
|
56
|
-
vertices: vectorNetwork.vertices.map((vertex) => ({
|
|
57
|
-
...vertex,
|
|
58
|
-
x: vertex.x * scaleX,
|
|
59
|
-
y: vertex.y * scaleY
|
|
60
|
-
})),
|
|
61
|
-
segments: vectorNetwork.segments.map((segment) => ({
|
|
62
|
-
...segment,
|
|
63
|
-
tangentStart: {
|
|
64
|
-
x: segment.tangentStart.x * scaleX,
|
|
65
|
-
y: segment.tangentStart.y * scaleY
|
|
66
|
-
},
|
|
67
|
-
tangentEnd: {
|
|
68
|
-
x: segment.tangentEnd.x * scaleX,
|
|
69
|
-
y: segment.tangentEnd.y * scaleY
|
|
70
|
-
}
|
|
71
|
-
})),
|
|
72
|
-
regions: vectorNetwork.regions
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
/** Snapshot one node's resize-relevant state (rect + geometry that must scale with it). */
|
|
76
|
-
function createResizeSnapshot(node) {
|
|
77
|
-
return {
|
|
78
|
-
x: node.x,
|
|
79
|
-
y: node.y,
|
|
80
|
-
width: node.width,
|
|
81
|
-
height: node.height,
|
|
82
|
-
vectorNetwork: node.vectorNetwork ? cloneVectorNetwork(node.vectorNetwork) : null,
|
|
83
|
-
fillGeometry: copyGeometryPaths(node.fillGeometry),
|
|
84
|
-
strokeGeometry: copyGeometryPaths(node.strokeGeometry),
|
|
85
|
-
derivedTextGlyphs: copyDerivedGlyphs(node.derivedTextGlyphs),
|
|
86
|
-
strokes: copyStrokes(node.strokes),
|
|
87
|
-
textPathData: node.textPathData ? structuredClone(node.textPathData) : null,
|
|
88
|
-
textPathBox: node.textPathBox ? { ...node.textPathBox } : null
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
function scaleDerivedGlyphs(glyphs, sx, sy) {
|
|
92
|
-
if (!glyphs?.length) return glyphs;
|
|
93
|
-
return glyphs.map((g) => ({
|
|
94
|
-
...g,
|
|
95
|
-
x: g.x * sx,
|
|
96
|
-
y: g.y * sy,
|
|
97
|
-
scaleX: (g.scaleX ?? 1) * sx,
|
|
98
|
-
scaleY: (g.scaleY ?? 1) * sy,
|
|
99
|
-
commandsBlob: new Uint8Array(g.commandsBlob)
|
|
100
|
-
}));
|
|
101
|
-
}
|
|
102
|
-
function scaleStrokes(strokes, sx, sy) {
|
|
103
|
-
if (strokes.length === 0) return strokes;
|
|
104
|
-
const weightScale = (Math.abs(sx) + Math.abs(sy)) / 2;
|
|
105
|
-
return strokes.map((s) => ({
|
|
106
|
-
...copyStroke(s),
|
|
107
|
-
weight: s.weight * weightScale
|
|
108
|
-
}));
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* All geometry that must track a width/height change. Historically only
|
|
112
|
-
* vectorNetwork + fillGeometry were updated — path-text strokeGeometry and
|
|
113
|
-
* glyphs stayed at the pre-resize size, so shrinking a sticker made the white
|
|
114
|
-
* OUTSIDE outlines look massively thick (DomeSticker resize bug).
|
|
115
|
-
*/
|
|
116
|
-
function scaledGeometryChanges(orig, origWidth, origHeight, width, height) {
|
|
117
|
-
if (origWidth <= 0 || origHeight <= 0) return {};
|
|
118
|
-
const sx = width / origWidth;
|
|
119
|
-
const sy = height / origHeight;
|
|
120
|
-
if (sx === 1 && sy === 1) return {};
|
|
121
|
-
const changes = {};
|
|
122
|
-
const resizedVN = scaleVectorNetworkForResize(orig.vectorNetwork, origWidth, origHeight, width, height);
|
|
123
|
-
if (resizedVN) changes.vectorNetwork = resizedVN;
|
|
124
|
-
if (orig.fillGeometry.length > 0) changes.fillGeometry = scaleGeometryPaths(orig.fillGeometry, sx, sy);
|
|
125
|
-
if (orig.strokeGeometry.length > 0) changes.strokeGeometry = scaleGeometryPaths(orig.strokeGeometry, sx, sy);
|
|
126
|
-
if (orig.derivedTextGlyphs?.length) {
|
|
127
|
-
changes.derivedTextGlyphs = scaleDerivedGlyphs(orig.derivedTextGlyphs, sx, sy);
|
|
128
|
-
if (orig.textPathBox) changes.textPathBox = {
|
|
129
|
-
x: orig.textPathBox.x * sx,
|
|
130
|
-
y: orig.textPathBox.y * sy,
|
|
131
|
-
width: orig.textPathBox.width * sx,
|
|
132
|
-
height: orig.textPathBox.height * sy
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
if (orig.strokes.length > 0) changes.strokes = scaleStrokes(orig.strokes, sx, sy);
|
|
136
|
-
return changes;
|
|
137
|
-
}
|
|
138
|
-
function collectResizeDescendants(graph, rootId) {
|
|
139
|
-
const root = graph.getNode(rootId);
|
|
140
|
-
if (!root || !CONSTRAINT_CONTAINER_TYPES.has(root.type)) return null;
|
|
141
|
-
const snapshots = /* @__PURE__ */ new Map();
|
|
142
|
-
const collect = (parentId) => {
|
|
143
|
-
const parent = graph.getNode(parentId);
|
|
144
|
-
if (!parent) return;
|
|
145
|
-
for (const childId of parent.childIds) {
|
|
146
|
-
const child = graph.getNode(childId);
|
|
147
|
-
if (!child) continue;
|
|
148
|
-
snapshots.set(childId, createResizeSnapshot(child));
|
|
149
|
-
collect(childId);
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
collect(rootId);
|
|
153
|
-
return snapshots.size > 0 ? snapshots : null;
|
|
154
|
-
}
|
|
155
|
-
function computeConstrainedResizeChanges(graph, rootId, rootBefore, rootAfter, originals) {
|
|
156
|
-
const changes = /* @__PURE__ */ new Map();
|
|
157
|
-
const compute = (parentId, parentBefore, parentAfter) => {
|
|
158
|
-
const parent = graph.getNode(parentId);
|
|
159
|
-
if (!parent) return;
|
|
160
|
-
const scalesChildren = parent.type === "GROUP" || parent.type === "BOOLEAN_OPERATION";
|
|
161
|
-
for (const childId of parent.childIds) {
|
|
162
|
-
const original = originals.get(childId);
|
|
163
|
-
const child = graph.getNode(childId);
|
|
164
|
-
if (!original || !child) continue;
|
|
165
|
-
if (parent.layoutMode !== "NONE" && child.layoutPositioning !== "ABSOLUTE") {
|
|
166
|
-
compute(childId, original, child);
|
|
167
|
-
continue;
|
|
168
|
-
}
|
|
169
|
-
const rect = scalesChildren ? scaledChildRect(original, parentBefore, parentAfter) : constrainedChildRect(original, parentBefore, parentAfter, child.horizontalConstraint, child.verticalConstraint);
|
|
170
|
-
const childChanges = {
|
|
171
|
-
...rect,
|
|
172
|
-
...scaledGeometryChanges(original, original.width, original.height, rect.width, rect.height)
|
|
173
|
-
};
|
|
174
|
-
changes.set(childId, childChanges);
|
|
175
|
-
compute(childId, original, child.layoutMode === "NONE" ? rect : child);
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
compute(rootId, rootBefore, rootAfter);
|
|
179
|
-
return changes;
|
|
180
|
-
}
|
|
181
|
-
//#endregion
|
|
182
|
-
export { collectResizeDescendants, computeConstrainedResizeChanges, constrainedChildRect, createResizeSnapshot, scaleVectorNetworkForResize, scaledChildRect, scaledGeometryChanges };
|
|
183
|
-
|
|
184
|
-
//# sourceMappingURL=resize.js.map
|
|
1
|
+
import { collectResizeDescendants, computeConstrainedResizeChanges, constrainedChildRect, createResizeSnapshot, layoutConstrainedChildRect, scaleVectorNetworkForResize, scaledChildRect, scaledGeometryChanges } from "./chunks/components/exposure.js";
|
|
2
|
+
export { collectResizeDescendants, computeConstrainedResizeChanges, constrainedChildRect, createResizeSnapshot, layoutConstrainedChildRect, scaleVectorNetworkForResize, scaledChildRect, scaledGeometryChanges };
|
package/dist/snap.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { SnapGuide, SnapResult, computeSelectionBounds, computeSnap } from "./
|
|
1
|
+
import { SnapGuide, SnapResult, computeSelectionBounds, computeSnap } from "./types.js";
|
|
2
2
|
export { SnapGuide, SnapResult, computeSelectionBounds, computeSnap };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { LayoutDirection, SceneNode, TextDirection } from "./types.js";
|
|
2
|
+
//#region src/text/direction.d.ts
|
|
3
|
+
declare function detectTextDirection(text: string): Exclude<TextDirection, 'AUTO'>;
|
|
4
|
+
declare function resolveTextDirection(direction: TextDirection, text: string): Exclude<TextDirection, 'AUTO'>;
|
|
5
|
+
declare function resolveNodeTextDirection(node: Pick<SceneNode, 'textDirection' | 'text'>): 'LTR' | 'RTL';
|
|
6
|
+
declare function resolveNodeLayoutDirection(node: {
|
|
7
|
+
layoutDirection?: LayoutDirection;
|
|
8
|
+
}, inheritedDirection?: Exclude<LayoutDirection, 'AUTO'>): Exclude<LayoutDirection, 'AUTO'>;
|
|
9
|
+
declare function isLogicalTextAlignStart(node: Pick<SceneNode, 'textAlignHorizontal' | 'textDirection' | 'text'>): boolean;
|
|
10
|
+
declare function isLogicalTextAlignEnd(node: Pick<SceneNode, 'textAlignHorizontal' | 'textDirection' | 'text'>): boolean;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { detectTextDirection, isLogicalTextAlignEnd, isLogicalTextAlignStart, resolveNodeLayoutDirection, resolveNodeTextDirection, resolveTextDirection };
|
|
13
|
+
//# sourceMappingURL=text-direction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text-direction.d.ts","names":[],"sources":["../src/text/direction.ts"],"mappings":";;iBAMgB,oBAAoB,eAAe,QAAQ;iBAQ3C,qBACd,WAAW,eACX,eACC,QAAQ;iBAIK,yBACd,MAAM,KAAK;iBAKG,2BACd;EAAQ,kBAAkB;GAC1B,qBAAoB,QAAQ,2BAC3B,QAAQ;iBAMK,wBACd,MAAM,KAAK;iBASG,sBACd,MAAM,KAAK"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/text/direction.ts
|
|
2
|
+
const RTL_CHAR_RE = /\p{Script=Arabic}|\p{Script=Hebrew}|\p{Script=Syriac}|\p{Script=Thaana}|\p{Script=Nko}|\p{Script=Adlam}/u;
|
|
3
|
+
const LTR_CHAR_RE = /\p{Script=Latin}|\p{Script=Cyrillic}|\p{Script=Greek}/u;
|
|
4
|
+
function detectTextDirection(text) {
|
|
5
|
+
for (const char of text) {
|
|
6
|
+
if (RTL_CHAR_RE.test(char)) return "RTL";
|
|
7
|
+
if (LTR_CHAR_RE.test(char)) return "LTR";
|
|
8
|
+
}
|
|
9
|
+
return "LTR";
|
|
10
|
+
}
|
|
11
|
+
function resolveTextDirection(direction, text) {
|
|
12
|
+
return direction === "AUTO" ? detectTextDirection(text) : direction;
|
|
13
|
+
}
|
|
14
|
+
function resolveNodeTextDirection(node) {
|
|
15
|
+
return resolveTextDirection(node.textDirection, node.text);
|
|
16
|
+
}
|
|
17
|
+
function resolveNodeLayoutDirection(node, inheritedDirection = "LTR") {
|
|
18
|
+
return !node.layoutDirection || node.layoutDirection === "AUTO" ? inheritedDirection : node.layoutDirection;
|
|
19
|
+
}
|
|
20
|
+
function isLogicalTextAlignStart(node) {
|
|
21
|
+
const direction = resolveNodeTextDirection(node);
|
|
22
|
+
return direction === "LTR" && node.textAlignHorizontal === "LEFT" || direction === "RTL" && node.textAlignHorizontal === "RIGHT";
|
|
23
|
+
}
|
|
24
|
+
function isLogicalTextAlignEnd(node) {
|
|
25
|
+
const direction = resolveNodeTextDirection(node);
|
|
26
|
+
return direction === "LTR" && node.textAlignHorizontal === "RIGHT" || direction === "RTL" && node.textAlignHorizontal === "LEFT";
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { detectTextDirection, isLogicalTextAlignEnd, isLogicalTextAlignStart, resolveNodeLayoutDirection, resolveNodeTextDirection, resolveTextDirection };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=text-direction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text-direction.js","names":[],"sources":["../src/text/direction.ts"],"sourcesContent":["import type { LayoutDirection, SceneNode, TextDirection } from '../types'\n\nconst RTL_CHAR_RE =\n /\\p{Script=Arabic}|\\p{Script=Hebrew}|\\p{Script=Syriac}|\\p{Script=Thaana}|\\p{Script=Nko}|\\p{Script=Adlam}/u\nconst LTR_CHAR_RE = /\\p{Script=Latin}|\\p{Script=Cyrillic}|\\p{Script=Greek}/u\n\nexport function detectTextDirection(text: string): Exclude<TextDirection, 'AUTO'> {\n for (const char of text) {\n if (RTL_CHAR_RE.test(char)) return 'RTL'\n if (LTR_CHAR_RE.test(char)) return 'LTR'\n }\n return 'LTR'\n}\n\nexport function resolveTextDirection(\n direction: TextDirection,\n text: string\n): Exclude<TextDirection, 'AUTO'> {\n return direction === 'AUTO' ? detectTextDirection(text) : direction\n}\n\nexport function resolveNodeTextDirection(\n node: Pick<SceneNode, 'textDirection' | 'text'>\n): 'LTR' | 'RTL' {\n return resolveTextDirection(node.textDirection, node.text)\n}\n\nexport function resolveNodeLayoutDirection(\n node: { layoutDirection?: LayoutDirection },\n inheritedDirection: Exclude<LayoutDirection, 'AUTO'> = 'LTR'\n): Exclude<LayoutDirection, 'AUTO'> {\n return !node.layoutDirection || node.layoutDirection === 'AUTO'\n ? inheritedDirection\n : node.layoutDirection\n}\n\nexport function isLogicalTextAlignStart(\n node: Pick<SceneNode, 'textAlignHorizontal' | 'textDirection' | 'text'>\n): boolean {\n const direction = resolveNodeTextDirection(node)\n return (\n (direction === 'LTR' && node.textAlignHorizontal === 'LEFT') ||\n (direction === 'RTL' && node.textAlignHorizontal === 'RIGHT')\n )\n}\n\nexport function isLogicalTextAlignEnd(\n node: Pick<SceneNode, 'textAlignHorizontal' | 'textDirection' | 'text'>\n): boolean {\n const direction = resolveNodeTextDirection(node)\n return (\n (direction === 'LTR' && node.textAlignHorizontal === 'RIGHT') ||\n (direction === 'RTL' && node.textAlignHorizontal === 'LEFT')\n )\n}\n"],"mappings":";AAEA,MAAM,cACJ;AACF,MAAM,cAAc;AAEpB,SAAgB,oBAAoB,MAA8C;CAChF,KAAK,MAAM,QAAQ,MAAM;EACvB,IAAI,YAAY,KAAK,IAAI,GAAG,OAAO;EACnC,IAAI,YAAY,KAAK,IAAI,GAAG,OAAO;CACrC;CACA,OAAO;AACT;AAEA,SAAgB,qBACd,WACA,MACgC;CAChC,OAAO,cAAc,SAAS,oBAAoB,IAAI,IAAI;AAC5D;AAEA,SAAgB,yBACd,MACe;CACf,OAAO,qBAAqB,KAAK,eAAe,KAAK,IAAI;AAC3D;AAEA,SAAgB,2BACd,MACA,qBAAuD,OACrB;CAClC,OAAO,CAAC,KAAK,mBAAmB,KAAK,oBAAoB,SACrD,qBACA,KAAK;AACX;AAEA,SAAgB,wBACd,MACS;CACT,MAAM,YAAY,yBAAyB,IAAI;CAC/C,OACG,cAAc,SAAS,KAAK,wBAAwB,UACpD,cAAc,SAAS,KAAK,wBAAwB;AAEzD;AAEA,SAAgB,sBACd,MACS;CACT,MAAM,YAAY,yBAAyB,IAAI;CAC/C,OACG,cAAc,SAAS,KAAK,wBAAwB,WACpD,cAAc,SAAS,KAAK,wBAAwB;AAEzD"}
|
package/dist/text-picture.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text-picture.d.ts","names":[],"sources":["../src/text
|
|
1
|
+
{"version":3,"file":"text-picture.d.ts","names":[],"sources":["../src/text/picture.ts"],"mappings":";;;;;;cASa,mBAAmB;cAQnB,sBAAsB;;;;;;iBAOnB,6BACd,MAAM,WACN,SAAS,QAAQ,aAChB,QAAQ;iBA2BK,qBAAqB,MAAM,WAAW,SAAS,QAAQ"}
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { LIST_BULLET, ListNumberFormat, MAX_LIST_INDENTATION, PLAIN_PARAGRAPH, TEXT_PARAGRAPH_SPACING_FIELDS, TextListItem, TextParagraphRange, formatListNumber, hasParagraphSpacing, hasTextList, listNumberFormat, paragraphCount, paragraphIndexAt, paragraphSpacingAt, paragraphStyleAt, paragraphStylesAfterEdit, paragraphStylesAfterTextChange, paragraphStylesInRange, paragraphStylesWithoutSpacing, sharedParagraphSpacing, textListItems, textParagraphRanges, topLevelNumberDigits, trimParagraphStyles, withIndentation, withListType, withParagraphSpacing } from "./types.js";
|
|
2
|
+
export { LIST_BULLET, ListNumberFormat, MAX_LIST_INDENTATION, PLAIN_PARAGRAPH, TEXT_PARAGRAPH_SPACING_FIELDS, TextListItem, TextParagraphRange, formatListNumber, hasParagraphSpacing, hasTextList, listNumberFormat, paragraphCount, paragraphIndexAt, paragraphSpacingAt, paragraphStyleAt, paragraphStylesAfterEdit, paragraphStylesAfterTextChange, paragraphStylesInRange, paragraphStylesWithoutSpacing, sharedParagraphSpacing, textListItems, textParagraphRanges, topLevelNumberDigits, trimParagraphStyles, withIndentation, withListType, withParagraphSpacing };
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { LIST_BULLET, formatListNumber, listNumberFormat, textListItems, topLevelNumberDigits } from "./chunks/index.js";
|
|
2
|
+
import { MAX_LIST_INDENTATION, PLAIN_PARAGRAPH, TEXT_PARAGRAPH_SPACING_FIELDS, hasParagraphSpacing, hasTextList, paragraphCount, paragraphIndexAt, paragraphSpacingAt, paragraphStyleAt, paragraphStylesAfterEdit, paragraphStylesAfterTextChange, paragraphStylesInRange, paragraphStylesWithoutSpacing, sharedParagraphSpacing, textParagraphRanges, trimParagraphStyles, withIndentation, withListType, withParagraphSpacing } from "./chunks/components/exposure.js";
|
|
3
|
+
export { LIST_BULLET, MAX_LIST_INDENTATION, PLAIN_PARAGRAPH, TEXT_PARAGRAPH_SPACING_FIELDS, formatListNumber, hasParagraphSpacing, hasTextList, listNumberFormat, paragraphCount, paragraphIndexAt, paragraphSpacingAt, paragraphStyleAt, paragraphStylesAfterEdit, paragraphStylesAfterTextChange, paragraphStylesInRange, paragraphStylesWithoutSpacing, sharedParagraphSpacing, textListItems, textParagraphRanges, topLevelNumberDigits, trimParagraphStyles, withIndentation, withListType, withParagraphSpacing };
|