@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/group-bounds.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import { getAxisAlignedBoundsInParent } from './coordinate'
|
|
1
|
+
import { getAxisAlignedBoundsInParent, getNodeLocalMatrix } from './coordinate'
|
|
2
2
|
import type { SceneGraph } from './graph'
|
|
3
|
+
import Matrix from './matrix'
|
|
3
4
|
import { FITTED_CONTAINER_TYPES } from './node-defaults'
|
|
4
5
|
import type { Rect, Vector } from './primitives'
|
|
5
6
|
import type { SceneNode } from './types'
|
|
6
7
|
|
|
8
|
+
/**
|
|
9
|
+
* What fitting a group reads: its layers and their parents, as the graph has them or as a resize
|
|
10
|
+
* plans them before writing.
|
|
11
|
+
*/
|
|
12
|
+
export type GroupFitGraph = Pick<SceneGraph, 'getNode' | 'rootId'>
|
|
13
|
+
|
|
7
14
|
/** Where a layer sits, with a connector's ends, since a free end is in the parent's axes too. */
|
|
8
15
|
type Placement = Pick<SceneNode, 'x' | 'y' | 'width' | 'height'> &
|
|
9
16
|
Partial<Pick<SceneNode, 'connectorStart' | 'connectorEnd'>>
|
|
@@ -80,7 +87,7 @@ function groupBoundingLayers(children: readonly SceneNode[]): SceneNode[] {
|
|
|
80
87
|
* empty at `origin`, where Figma leaves it.
|
|
81
88
|
*/
|
|
82
89
|
export function groupBounds(
|
|
83
|
-
graph:
|
|
90
|
+
graph: GroupFitGraph,
|
|
84
91
|
children: readonly SceneNode[],
|
|
85
92
|
parentId: string,
|
|
86
93
|
origin: Vector
|
|
@@ -102,6 +109,76 @@ function shiftedBack(child: SceneNode, dx: number, dy: number): Partial<SceneNod
|
|
|
102
109
|
}
|
|
103
110
|
}
|
|
104
111
|
|
|
112
|
+
function isTransformed(node: SceneNode): boolean {
|
|
113
|
+
return node.rotation !== 0 || node.flipX || node.flipY
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The group's new box in its parent's space and how far its children shift in its own frame, so
|
|
118
|
+
* that the group holds its children while they stay put on the canvas. An unrotated group takes
|
|
119
|
+
* their bounds in its parent's space. A rotated or flipped group keeps its angle: it takes their
|
|
120
|
+
* bounds in its own frame, and its position is solved so its frame still maps every child to the
|
|
121
|
+
* same canvas point, as its rotation and flips are unchanged. A rotated or flipped boolean, whose
|
|
122
|
+
* box comes from its result's path, keeps its bounds.
|
|
123
|
+
*/
|
|
124
|
+
function fittedBox(
|
|
125
|
+
graph: GroupFitGraph,
|
|
126
|
+
group: SceneNode,
|
|
127
|
+
parentId: string,
|
|
128
|
+
children: SceneNode[],
|
|
129
|
+
options: GroupFitOptions
|
|
130
|
+
): { box: Rect; shift: Vector } | null {
|
|
131
|
+
if (!isTransformed(group)) {
|
|
132
|
+
const box =
|
|
133
|
+
(group.type === 'BOOLEAN_OPERATION' ? options.booleanBounds?.(group) : null) ??
|
|
134
|
+
groupBounds(graph, children, parentId, group)
|
|
135
|
+
return { box, shift: { x: box.x - group.x, y: box.y - group.y } }
|
|
136
|
+
}
|
|
137
|
+
if (group.type === 'BOOLEAN_OPERATION') return null
|
|
138
|
+
const local = groupBounds(graph, children, group.id, { x: 0, y: 0 })
|
|
139
|
+
const [px, py] = Matrix.mapPoints(getNodeLocalMatrix(group), [local.x, local.y])
|
|
140
|
+
const sized = { ...group, width: local.width, height: local.height }
|
|
141
|
+
const [qx, qy] = Matrix.mapPoints(getNodeLocalMatrix(sized, { x: 0, y: 0 }), [0, 0])
|
|
142
|
+
return {
|
|
143
|
+
box: { x: px - qx, y: py - qy, width: local.width, height: local.height },
|
|
144
|
+
shift: { x: local.x, y: local.y }
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const FIT_TOLERANCE = 1e-9
|
|
149
|
+
|
|
150
|
+
function unchanged(value: number, previous: number): boolean {
|
|
151
|
+
return Math.abs(value - previous) <= FIT_TOLERANCE * Math.max(1, Math.abs(previous))
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The changes that fit `group` to `children`: its box in its parent's space, and each child moved
|
|
156
|
+
* back so it stays put on the canvas. Null when the fit changes nothing.
|
|
157
|
+
*/
|
|
158
|
+
export function groupFitChanges(
|
|
159
|
+
graph: GroupFitGraph,
|
|
160
|
+
group: SceneNode,
|
|
161
|
+
children: SceneNode[],
|
|
162
|
+
options: GroupFitOptions = {}
|
|
163
|
+
): Map<string, Partial<SceneNode>> | null {
|
|
164
|
+
if (!group.parentId) return null
|
|
165
|
+
const fitted = fittedBox(graph, group, group.parentId, children, options)
|
|
166
|
+
if (!fitted) return null
|
|
167
|
+
const { box, shift } = fitted
|
|
168
|
+
// A rotated group's frame leaves rounding residue; a fit within it changes nothing.
|
|
169
|
+
if (
|
|
170
|
+
unchanged(shift.x, 0) &&
|
|
171
|
+
unchanged(shift.y, 0) &&
|
|
172
|
+
unchanged(box.width, group.width) &&
|
|
173
|
+
unchanged(box.height, group.height)
|
|
174
|
+
) {
|
|
175
|
+
return null
|
|
176
|
+
}
|
|
177
|
+
const changes = new Map<string, Partial<SceneNode>>([[group.id, box]])
|
|
178
|
+
for (const child of children) changes.set(child.id, shiftedBack(child, shift.x, shift.y))
|
|
179
|
+
return changes
|
|
180
|
+
}
|
|
181
|
+
|
|
105
182
|
/** Moves the group to its children's bounds, shifting them back so they stay put. */
|
|
106
183
|
function fitGroup(
|
|
107
184
|
graph: SceneGraph,
|
|
@@ -110,22 +187,12 @@ function fitGroup(
|
|
|
110
187
|
fit: GroupFit,
|
|
111
188
|
options: GroupFitOptions
|
|
112
189
|
) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
(group.type === 'BOOLEAN_OPERATION' ? options.booleanBounds?.(group) : null) ??
|
|
116
|
-
groupBounds(graph, children, group.parentId, group)
|
|
117
|
-
const dx = bounds.x - group.x
|
|
118
|
-
const dy = bounds.y - group.y
|
|
119
|
-
if (dx === 0 && dy === 0 && bounds.width === group.width && bounds.height === group.height) {
|
|
120
|
-
return
|
|
121
|
-
}
|
|
190
|
+
const changes = groupFitChanges(graph, group, children, options)
|
|
191
|
+
if (!changes) return
|
|
122
192
|
for (const node of [group, ...children]) {
|
|
123
193
|
if (!fit.before.has(node.id)) fit.before.set(node.id, placement(node))
|
|
124
194
|
}
|
|
125
|
-
graph.updateNode(
|
|
126
|
-
for (const child of children) {
|
|
127
|
-
graph.updateNode(child.id, shiftedBack(child, dx, dy))
|
|
128
|
-
}
|
|
195
|
+
for (const [id, change] of changes) graph.updateNode(id, change)
|
|
129
196
|
for (const node of [group, ...children]) {
|
|
130
197
|
const current = graph.getNode(node.id)
|
|
131
198
|
if (current) fit.after.set(node.id, placement(current))
|
|
@@ -143,8 +210,8 @@ function removeEmptyGroup(graph: SceneGraph, group: SceneNode, fit: GroupFit) {
|
|
|
143
210
|
/**
|
|
144
211
|
* Refits the groups and booleans around these parents to their children, keeping every child where
|
|
145
212
|
* it is on the canvas, and removes groups left empty, as Figma does after a layer inside one moves,
|
|
146
|
-
* resizes, or leaves. Rotated or flipped
|
|
147
|
-
* changed.
|
|
213
|
+
* resizes, or leaves. Rotated or flipped groups keep their angle; a rotated or flipped boolean keeps
|
|
214
|
+
* its bounds. Returns null when nothing changed.
|
|
148
215
|
*/
|
|
149
216
|
export function fitEnclosingGroups(
|
|
150
217
|
graph: SceneGraph,
|
package/src/guides.ts
CHANGED
|
@@ -9,6 +9,11 @@ export interface CanvasGuide {
|
|
|
9
9
|
figGuid?: GUID
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
/** A new guide's id, unique across documents as a copied guide keeps its id. */
|
|
13
|
+
export function newGuideId(): string {
|
|
14
|
+
return `guide:${crypto.randomUUID()}`
|
|
15
|
+
}
|
|
16
|
+
|
|
12
17
|
const ownersByGraph = new WeakMap<SceneGraph, Set<string>>()
|
|
13
18
|
|
|
14
19
|
function ownerSet(graph: SceneGraph): Set<string> {
|
package/src/hit-test.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { hitsConnectorLine } from './connectors/hit'
|
|
|
2
2
|
import { getWorldMatrix } from './coordinate'
|
|
3
3
|
import type { SceneGraph } from './graph'
|
|
4
4
|
import Matrix from './matrix'
|
|
5
|
+
import { hitsShape, isHitPaint, strokeReach } from './outline/hit'
|
|
5
6
|
import type { SceneNode, NodeType } from './types'
|
|
6
7
|
|
|
7
8
|
const CONTAINER_TYPES = new Set<NodeType>([
|
|
@@ -15,8 +16,9 @@ const CONTAINER_TYPES = new Set<NodeType>([
|
|
|
15
16
|
])
|
|
16
17
|
const OPAQUE_CONTAINER_TYPES = new Set<NodeType>(['COMPONENT', 'INSTANCE'])
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
/** Whether a container's own paints show, so its empty area is drawn (see `isHitPaint`). */
|
|
20
|
+
function showsPaint(node: SceneNode): boolean {
|
|
21
|
+
return node.fills.some(isHitPaint) || node.strokes.some(isHitPaint)
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
function isTopLevel(graph: SceneGraph, node: SceneNode): boolean {
|
|
@@ -81,96 +83,97 @@ function containsPoint(
|
|
|
81
83
|
)
|
|
82
84
|
}
|
|
83
85
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
86
|
+
/**
|
|
87
|
+
* How a walk down the layers takes a click. `deep` reaches past containers to the deepest layer;
|
|
88
|
+
* `pad`, in canvas units, grows each leaf layer's box, as Figma takes a click a few screen pixels
|
|
89
|
+
* outside a shape; `instanceBodies` lets a deep walk take an instance's empty area whatever its
|
|
90
|
+
* fill, as a plain click does, where a ⌘-click counts only a fill that shows.
|
|
91
|
+
*/
|
|
92
|
+
interface HitWalk {
|
|
93
|
+
deep: boolean
|
|
92
94
|
pad: number
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
95
|
+
instanceBodies: boolean
|
|
96
|
+
/** Whether locked layers are hit, as by a right-click; see `SelectableHitOptions.locked`. */
|
|
97
|
+
locked: boolean
|
|
98
|
+
transformCache: Map<string, boolean>
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function walkOf(deep: boolean, pad = 0, instanceBodies = false, locked = false): HitWalk {
|
|
102
|
+
return { deep, pad, instanceBodies, locked, transformCache: new Map() }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Whether a container's empty area under the point takes a click reaching past containers. */
|
|
106
|
+
function takesEmptyArea(graph: SceneGraph, node: SceneNode, walk: HitWalk): boolean {
|
|
107
|
+
// Figma counts an empty area only where a paint shows, except a main component's, which always
|
|
108
|
+
// counts, and an instance's when the click is a plain one.
|
|
109
|
+
if (node.type === 'COMPONENT' || showsPaint(node) || ownsItsEmptyArea(graph, node)) return true
|
|
110
|
+
return walk.instanceBodies && node.type === 'INSTANCE'
|
|
99
111
|
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* What a hit inside a container reports: the container itself when it is locked (a locked layer,
|
|
115
|
+
* where one is hit at all, stands in for everything inside it) or the walk stops at containers.
|
|
116
|
+
*/
|
|
117
|
+
function standIn(container: SceneNode, hit: SceneNode, deep: boolean): SceneNode {
|
|
118
|
+
return container.locked || !deep ? container : hit
|
|
119
|
+
}
|
|
120
|
+
|
|
100
121
|
function hitTestTransparentContainer(
|
|
101
122
|
graph: SceneGraph,
|
|
102
123
|
px: number,
|
|
103
124
|
py: number,
|
|
104
125
|
child: SceneNode,
|
|
105
|
-
|
|
106
|
-
deep: boolean,
|
|
107
|
-
transformCache: Map<string, boolean>,
|
|
108
|
-
pad: number
|
|
126
|
+
walk: HitWalk
|
|
109
127
|
): SceneNode | null {
|
|
110
128
|
if (child.type === 'GROUP') {
|
|
111
129
|
// A group fits its layers, so a padded click on one at its edge falls just outside it.
|
|
112
|
-
if (!containsPoint(px, py, child, graph, transformCache, pad)) return null
|
|
130
|
+
if (!containsPoint(px, py, child, graph, walk.transformCache, walk.pad)) return null
|
|
113
131
|
// A group has no area of its own, as in Figma: between its layers the click falls through to
|
|
114
132
|
// whatever lies under the group.
|
|
115
|
-
const childHit = hitTestChildren(graph, px, py,
|
|
116
|
-
return childHit ? (
|
|
133
|
+
const childHit = hitTestChildren(graph, px, py, child.id, walk)
|
|
134
|
+
return childHit ? standIn(child, childHit, walk.deep) : null
|
|
117
135
|
}
|
|
118
136
|
|
|
119
|
-
const childHit = hitTestChildren(graph, px, py,
|
|
120
|
-
if (childHit)
|
|
121
|
-
if (child.locked) return child
|
|
122
|
-
return childHit
|
|
123
|
-
}
|
|
137
|
+
const childHit = hitTestChildren(graph, px, py, child.id, walk)
|
|
138
|
+
if (childHit) return standIn(child, childHit, true)
|
|
124
139
|
|
|
125
140
|
if (
|
|
126
|
-
containsPoint(px, py, child, graph, transformCache) &&
|
|
127
|
-
(
|
|
141
|
+
containsPoint(px, py, child, graph, walk.transformCache) &&
|
|
142
|
+
takesEmptyArea(graph, child, walk)
|
|
128
143
|
)
|
|
129
144
|
return child
|
|
130
145
|
return null
|
|
131
146
|
}
|
|
132
147
|
|
|
133
|
-
/**
|
|
134
|
-
* The topmost layer under the point among a container's children. `pad`, in canvas units, grows
|
|
135
|
-
* each leaf layer's box, as Figma takes a click a few screen pixels outside a shape.
|
|
136
|
-
*/
|
|
148
|
+
/** The topmost layer under the point among a container's children. */
|
|
137
149
|
function hitTestChildren(
|
|
138
150
|
graph: SceneGraph,
|
|
139
151
|
px: number,
|
|
140
152
|
py: number,
|
|
141
153
|
parentId: string,
|
|
142
|
-
|
|
143
|
-
transformCache = new Map<string, boolean>(),
|
|
144
|
-
pad = 0
|
|
154
|
+
walk: HitWalk
|
|
145
155
|
): SceneNode | null {
|
|
146
156
|
const parent = graph.nodes.get(parentId)
|
|
147
157
|
if (!parent) return null
|
|
158
|
+
const { transformCache, pad } = walk
|
|
148
159
|
|
|
149
160
|
if (parent.clipsContent) {
|
|
150
161
|
if (!containsPoint(px, py, parent, graph, transformCache)) return null
|
|
151
162
|
}
|
|
152
163
|
|
|
153
164
|
for (let i = parent.childIds.length - 1; i >= 0; i--) {
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
165
|
+
const child = graph.nodes.get(parent.childIds[i])
|
|
166
|
+
// A locked layer and everything in it are invisible to a press and to hover, as in Figma: a
|
|
167
|
+
// click falls through to what lies under it, and a drag from it draws a marquee.
|
|
168
|
+
if (!child || child.internalOnly || !child.visible || (child.locked && !walk.locked)) continue
|
|
157
169
|
if (CONTAINER_TYPES.has(child.type)) {
|
|
158
|
-
if (OPAQUE_CONTAINER_TYPES.has(child.type) && !deep) {
|
|
159
|
-
|
|
160
|
-
|
|
170
|
+
if (OPAQUE_CONTAINER_TYPES.has(child.type) && !walk.deep) {
|
|
171
|
+
// A plain click takes an instance or component anywhere in its box, with a fill or
|
|
172
|
+
// without, as in Figma.
|
|
173
|
+
if (containsPoint(px, py, child, graph, transformCache)) return child
|
|
161
174
|
continue
|
|
162
175
|
}
|
|
163
|
-
|
|
164
|
-
const hit = hitTestTransparentContainer(
|
|
165
|
-
graph,
|
|
166
|
-
px,
|
|
167
|
-
py,
|
|
168
|
-
child,
|
|
169
|
-
childId,
|
|
170
|
-
deep,
|
|
171
|
-
transformCache,
|
|
172
|
-
pad
|
|
173
|
-
)
|
|
176
|
+
const hit = hitTestTransparentContainer(graph, px, py, child, walk)
|
|
174
177
|
if (hit) return hit
|
|
175
178
|
continue
|
|
176
179
|
}
|
|
@@ -179,12 +182,30 @@ function hitTestChildren(
|
|
|
179
182
|
if (hitsConnectorLine(graph, child, px, py, pad)) return child
|
|
180
183
|
continue
|
|
181
184
|
}
|
|
182
|
-
if (
|
|
185
|
+
if (hitsLeaf(graph, px, py, child, transformCache, pad)) return child
|
|
183
186
|
}
|
|
184
187
|
|
|
185
188
|
return null
|
|
186
189
|
}
|
|
187
190
|
|
|
191
|
+
/**
|
|
192
|
+
* Whether a click lands on a leaf layer: on what its paints draw, as Figma tests shapes (see
|
|
193
|
+
* `hitsShape`), or on its box for layers without an outline to test, such as text.
|
|
194
|
+
*/
|
|
195
|
+
function hitsLeaf(
|
|
196
|
+
graph: SceneGraph,
|
|
197
|
+
px: number,
|
|
198
|
+
py: number,
|
|
199
|
+
node: SceneNode,
|
|
200
|
+
transformCache: Map<string, boolean>,
|
|
201
|
+
pad: number
|
|
202
|
+
): boolean {
|
|
203
|
+
if (!containsPoint(px, py, node, graph, transformCache, pad + strokeReach(node))) return false
|
|
204
|
+
return (
|
|
205
|
+
hitsShape(graph, node, px, py, pad) ?? containsPoint(px, py, node, graph, transformCache, pad)
|
|
206
|
+
)
|
|
207
|
+
}
|
|
208
|
+
|
|
188
209
|
export function hitTest(
|
|
189
210
|
graph: SceneGraph,
|
|
190
211
|
px: number,
|
|
@@ -192,7 +213,7 @@ export function hitTest(
|
|
|
192
213
|
scopeId?: string
|
|
193
214
|
): SceneNode | null {
|
|
194
215
|
const scope = scopeId ?? graph.rootId
|
|
195
|
-
return hitTestChildren(graph, px, py, scope, false)
|
|
216
|
+
return hitTestChildren(graph, px, py, scope, walkOf(false))
|
|
196
217
|
}
|
|
197
218
|
|
|
198
219
|
export function hitTestDeep(
|
|
@@ -203,7 +224,7 @@ export function hitTestDeep(
|
|
|
203
224
|
pad = 0
|
|
204
225
|
): SceneNode | null {
|
|
205
226
|
const scope = scopeId ?? graph.rootId
|
|
206
|
-
return hitTestChildren(graph, px, py, scope, true,
|
|
227
|
+
return hitTestChildren(graph, px, py, scope, walkOf(true, pad))
|
|
207
228
|
}
|
|
208
229
|
|
|
209
230
|
/**
|
|
@@ -228,12 +249,14 @@ function frameChildAt(
|
|
|
228
249
|
container: SceneNode,
|
|
229
250
|
px: number,
|
|
230
251
|
py: number,
|
|
231
|
-
framesOpen = true
|
|
252
|
+
framesOpen = true,
|
|
253
|
+
locked = false
|
|
232
254
|
): SceneNode | null {
|
|
233
255
|
const transformCache = new Map<string, boolean>()
|
|
234
256
|
for (let i = container.childIds.length - 1; i >= 0; i--) {
|
|
235
257
|
const child = graph.nodes.get(container.childIds[i])
|
|
236
258
|
if (!child || child.internalOnly || !child.visible || child.type !== 'FRAME') continue
|
|
259
|
+
if (child.locked && !locked) continue
|
|
237
260
|
// A board, such as a top-level frame in a section, keeps its empty area as background.
|
|
238
261
|
if (opensByItself(graph, child, framesOpen) && !ownsItsEmptyArea(graph, child)) continue
|
|
239
262
|
if (containsPoint(px, py, child, graph, transformCache)) return child
|
|
@@ -297,6 +320,11 @@ export interface SelectableHitOptions {
|
|
|
297
320
|
framesOpen?: boolean
|
|
298
321
|
/** The container a double-click entered, open along with its ancestors. */
|
|
299
322
|
enteredId?: string | null
|
|
323
|
+
/**
|
|
324
|
+
* Whether locked layers are hit, each standing in for everything inside it: a right-click
|
|
325
|
+
* selects one, as in Figma, while a press and hover pass through it.
|
|
326
|
+
*/
|
|
327
|
+
locked?: boolean
|
|
300
328
|
}
|
|
301
329
|
|
|
302
330
|
export function hitTestSelectable(
|
|
@@ -307,11 +335,12 @@ export function hitTestSelectable(
|
|
|
307
335
|
selectedIds: ReadonlySet<string>,
|
|
308
336
|
options: SelectableHitOptions = {}
|
|
309
337
|
): SceneNode | null {
|
|
310
|
-
const { pad = 0, framesOpen = true, enteredId = null } = options
|
|
311
|
-
|
|
338
|
+
const { pad = 0, framesOpen = true, enteredId = null, locked = false } = options
|
|
339
|
+
// A plain click: the walk takes an instance's empty area whatever its fill.
|
|
340
|
+
const deepest = hitTestChildren(graph, px, py, scopeId, walkOf(true, pad, true, locked))
|
|
312
341
|
if (!deepest) {
|
|
313
342
|
const scope = graph.nodes.get(scopeId)
|
|
314
|
-
return scope ? frameChildAt(graph, scope, px, py, framesOpen) : null
|
|
343
|
+
return scope ? frameChildAt(graph, scope, px, py, framesOpen, locked) : null
|
|
315
344
|
}
|
|
316
345
|
|
|
317
346
|
const chain: SceneNode[] = []
|
|
@@ -323,13 +352,13 @@ export function hitTestSelectable(
|
|
|
323
352
|
const opened = openedBySelection(graph, selectedIds, scopeId, framesOpen, enteredId)
|
|
324
353
|
|
|
325
354
|
for (const node of chain) {
|
|
326
|
-
// A locked layer stands in for everything inside it.
|
|
355
|
+
// A locked layer, hit only with `locked`, stands in for everything inside it.
|
|
327
356
|
if (node.locked) return node
|
|
328
357
|
if (!opened.has(node.id) && !opensByItself(graph, node, framesOpen)) return node
|
|
329
358
|
}
|
|
330
359
|
const last = chain.at(-1)
|
|
331
360
|
if (!last) return null
|
|
332
|
-
const frame = frameChildAt(graph, last, px, py, framesOpen)
|
|
361
|
+
const frame = frameChildAt(graph, last, px, py, framesOpen, locked)
|
|
333
362
|
if (frame) return frame
|
|
334
363
|
if (ownsItsEmptyArea(graph, last)) return last
|
|
335
364
|
return opened.has(last.id) && !opensByItself(graph, last, framesOpen) ? last : null
|
|
@@ -351,7 +380,7 @@ export function hitTestOpenContainer(
|
|
|
351
380
|
py: number,
|
|
352
381
|
scopeId: string
|
|
353
382
|
): SceneNode | null {
|
|
354
|
-
const deepest = hitTestChildren(graph, px, py, scopeId, true)
|
|
383
|
+
const deepest = hitTestChildren(graph, px, py, scopeId, walkOf(true))
|
|
355
384
|
for (let node: SceneNode | undefined = deepest ?? undefined; node && node.id !== scopeId;) {
|
|
356
385
|
if (!opensByItself(graph, node) || ownsItsEmptyArea(graph, node)) return null
|
|
357
386
|
node = node.parentId ? graph.nodes.get(node.parentId) : undefined
|
package/src/images.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { sha1 } from '@noble/hashes/legacy.js'
|
|
2
2
|
import { bytesToHex } from '@noble/hashes/utils.js'
|
|
3
3
|
import { imageSize as readImageHeader } from 'image-size'
|
|
4
|
+
import { fromUint8Array, isValid, toUint8Array } from 'js-base64'
|
|
4
5
|
|
|
5
6
|
import type { Matrix, Size } from './primitives'
|
|
7
|
+
import type { Fill } from './types'
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
10
|
* The name an image's bytes are stored under: the hex SHA-1 of the bytes, as Figma names images,
|
|
@@ -13,14 +15,15 @@ export function computeImageHash(data: Uint8Array): string {
|
|
|
13
15
|
return bytesToHex(sha1(data))
|
|
14
16
|
}
|
|
15
17
|
|
|
16
|
-
export type ImageMimeType = 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp'
|
|
18
|
+
export type ImageMimeType = 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp' | 'image/avif'
|
|
17
19
|
|
|
18
20
|
/** The image types documents store, by the name `image-size` gives each format. */
|
|
19
21
|
const IMAGE_MIME_TYPES: Partial<Record<string, ImageMimeType>> = {
|
|
20
22
|
png: 'image/png',
|
|
21
23
|
jpg: 'image/jpeg',
|
|
22
24
|
gif: 'image/gif',
|
|
23
|
-
webp: 'image/webp'
|
|
25
|
+
webp: 'image/webp',
|
|
26
|
+
avif: 'image/avif'
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
/** The raster types a dropped or pasted file can be imported as an image fill from. */
|
|
@@ -44,7 +47,7 @@ export interface ImageHeader extends Size {
|
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
/**
|
|
47
|
-
* The type and size of a PNG, JPEG, GIF, or
|
|
50
|
+
* The type and size of a PNG, JPEG, GIF, WebP, or AVIF, read from its header by `image-size`; null for
|
|
48
51
|
* bytes it cannot read and for other formats.
|
|
49
52
|
*/
|
|
50
53
|
export function imageHeader(data: Uint8Array): ImageHeader | null {
|
|
@@ -66,6 +69,49 @@ export function imageMimeType(data: Uint8Array): ImageMimeType {
|
|
|
66
69
|
return imageHeader(data)?.type ?? 'image/png'
|
|
67
70
|
}
|
|
68
71
|
|
|
72
|
+
export interface DataURL {
|
|
73
|
+
/** The media type in lowercase, without parameters; `text/plain` when the URL names none. */
|
|
74
|
+
mimeType: string
|
|
75
|
+
bytes: Uint8Array
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The media type and bytes a `data:` URL carries: a Base64 body decoded, and a percent-encoded
|
|
80
|
+
* one unescaped as UTF-8 text, as SVG and other markup are written. Null for anything else: Base64
|
|
81
|
+
* cut short in transit, and escapes that are not UTF-8, such as a binary image escaped byte by
|
|
82
|
+
* byte, which a reader that must take any data URL, such as `.pen` loading, gets from `fetch`.
|
|
83
|
+
*/
|
|
84
|
+
export function parseDataURL(url: string): DataURL | null {
|
|
85
|
+
const match = /^\s*data:([^,]*),(.*?)\s*$/is.exec(url)
|
|
86
|
+
if (!match) return null
|
|
87
|
+
const [, header = '', body = ''] = match
|
|
88
|
+
const params = header.split(';').map((param) => param.trim())
|
|
89
|
+
const base64 = params.length > 1 && params.at(-1)?.toLowerCase() === 'base64'
|
|
90
|
+
const mimeType = params[0]?.toLowerCase() || 'text/plain'
|
|
91
|
+
let text: string
|
|
92
|
+
try {
|
|
93
|
+
// A `%` that starts no escape is a literal percent sign, as in `width="100%"`.
|
|
94
|
+
text = decodeURIComponent(body.replace(/%(?![\da-f]{2})/gi, '%25'))
|
|
95
|
+
} catch {
|
|
96
|
+
return null
|
|
97
|
+
}
|
|
98
|
+
if (!base64) return { mimeType, bytes: new TextEncoder().encode(text) }
|
|
99
|
+
const compact = text.replace(/\s+/g, '')
|
|
100
|
+
// Base64 one character past a whole group of four cannot be bytes: it was cut short.
|
|
101
|
+
if (compact.replace(/={1,2}$/, '').length % 4 === 1 || !isValid(compact)) return null
|
|
102
|
+
return { mimeType, bytes: toUint8Array(compact) }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Encoded image bytes as a Base64 data URL of the type their header names. */
|
|
106
|
+
export function imageDataURL(data: Uint8Array): string {
|
|
107
|
+
return `data:${imageMimeType(data)};base64,${fromUint8Array(data)}`
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** SVG markup as a percent-encoded data URL, as CSS `url()` and `<img src>` take one. */
|
|
111
|
+
export function svgDataURL(svg: string): string {
|
|
112
|
+
return `data:image/svg+xml,${encodeURIComponent(svg)}`
|
|
113
|
+
}
|
|
114
|
+
|
|
69
115
|
/** An image's size in pixels, read from its header; null for bytes it cannot read. */
|
|
70
116
|
export function imageSize(data: Uint8Array): Size | null {
|
|
71
117
|
const header = imageHeader(data)
|
|
@@ -87,3 +133,25 @@ export function coverImageTransform(
|
|
|
87
133
|
const m11 = height / scale / imageHeight
|
|
88
134
|
return { m00, m01: 0, m02: (1 - m00) / 2, m10: 0, m11, m12: (1 - m11) / 2 }
|
|
89
135
|
}
|
|
136
|
+
|
|
137
|
+
/** The images `paints` show, by their hashes. */
|
|
138
|
+
export function imagePaintHashes(paints: Iterable<Fill>): string[] {
|
|
139
|
+
return [...paints].flatMap((paint) =>
|
|
140
|
+
paint.type === 'IMAGE' && paint.imageHash ? [paint.imageHash] : []
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Copies the images `hashes` name from `from` into `to`, each as bytes of its own. An image is
|
|
146
|
+
* named by its content, so one `to` already holds stays.
|
|
147
|
+
*/
|
|
148
|
+
export function copyImages(
|
|
149
|
+
from: ReadonlyMap<string, Uint8Array>,
|
|
150
|
+
to: Map<string, Uint8Array>,
|
|
151
|
+
hashes: Iterable<string>
|
|
152
|
+
): void {
|
|
153
|
+
for (const hash of hashes) {
|
|
154
|
+
const bytes = from.get(hash)
|
|
155
|
+
if (bytes && !to.has(hash)) to.set(hash, new Uint8Array(bytes))
|
|
156
|
+
}
|
|
157
|
+
}
|
package/src/instances/fields.ts
CHANGED
|
@@ -43,6 +43,14 @@ export const INSTANCE_FOLLOWED_FIELDS = [
|
|
|
43
43
|
'counterAxisAlignContent',
|
|
44
44
|
'isMask',
|
|
45
45
|
'maskType',
|
|
46
|
+
'maskIsOutline',
|
|
47
|
+
'booleanOperation',
|
|
48
|
+
'isExposedInstance',
|
|
49
|
+
'connectorStart',
|
|
50
|
+
'connectorEnd',
|
|
51
|
+
'connectorLineType',
|
|
52
|
+
'connectorStartCap',
|
|
53
|
+
'connectorEndCap',
|
|
46
54
|
'arcData',
|
|
47
55
|
'pointCount',
|
|
48
56
|
'starInnerRadius',
|