@open-pencil/scene-graph 0.15.1 → 0.16.0-beta.2
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 +607 -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} +577 -522
- 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 +2291 -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/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 +1724 -663
- 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 +176 -34
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +4 -8
- package/dist/index.js +19 -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 +2121 -15
- 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 +17 -6
- 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 +262 -46
- 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 +347 -57
- package/src/instance-overrides.ts +27 -0
- package/src/instances/content-size.ts +58 -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 +600 -0
- package/src/instances/vector-geometry.ts +81 -0
- package/src/instances.ts +125 -330
- package/src/layout/aspect-ratio.ts +51 -0
- package/src/{layout-guides.ts → layout/guides.ts} +41 -12
- 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/shared-styles.ts +5 -2
- 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 +3 -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/text/references.ts +50 -0
- 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 +120 -0
- package/src/transfer/plan.ts +130 -0
- package/src/transfer/references.ts +9 -0
- package/src/transfer/remove.ts +39 -0
- package/src/transfer/validation.ts +59 -0
- package/src/transfer/variables.ts +82 -0
- package/src/types.ts +257 -13
- package/src/undo.ts +54 -2
- package/src/variables/bindings.ts +224 -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
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import valueParser from 'postcss-value-parser'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
parseAutoLayoutDirection,
|
|
5
|
+
parseCounterAxisAlign,
|
|
6
|
+
parseLayoutAlignSelf,
|
|
7
|
+
parsePrimaryAxisAlign,
|
|
8
|
+
type AutoLayoutDirection
|
|
9
|
+
} from '../layout/keywords'
|
|
10
|
+
import type { LayoutAlign, LayoutAlignSelf, LayoutCounterAlign } from '../types'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The one alignment keyword a CSS box alignment value names, or `undefined` when it names
|
|
14
|
+
* something auto layout cannot express. An overflow position (`safe center`) is dropped, and
|
|
15
|
+
* `first baseline` is the baseline auto layout aligns to.
|
|
16
|
+
*/
|
|
17
|
+
function alignmentKeyword(value: string | undefined): string | undefined {
|
|
18
|
+
if (!value) return undefined
|
|
19
|
+
const nodes = valueParser(value).nodes.filter((node) => node.type !== 'space')
|
|
20
|
+
if (nodes.some((node) => node.type !== 'word')) return undefined
|
|
21
|
+
const words = nodes.map((node) => node.value.toLowerCase())
|
|
22
|
+
if (words[0] === 'safe' || words[0] === 'unsafe') words.shift()
|
|
23
|
+
if (words[0] === 'first' && words[1] === 'baseline') words.shift()
|
|
24
|
+
return words.length === 1 ? words[0] : undefined
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A stack direction from `flex-direction`; the reversed directions have no auto layout. */
|
|
28
|
+
export function parseCSSFlexDirection(value: string | undefined): AutoLayoutDirection | undefined {
|
|
29
|
+
const keyword = alignmentKeyword(value)
|
|
30
|
+
return keyword === 'row' || keyword === 'column' ? parseAutoLayoutDirection(keyword) : undefined
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Primary axis alignment from `justify-content`; `normal` packs flex items at the start. Design
|
|
35
|
+
* JSX's `between` is not CSS, which browsers ignore, so it is not read here.
|
|
36
|
+
*/
|
|
37
|
+
export function parseCSSJustifyContent(value: string | undefined): LayoutAlign | undefined {
|
|
38
|
+
const keyword = alignmentKeyword(value)
|
|
39
|
+
if (keyword === 'between') return undefined
|
|
40
|
+
return keyword === 'normal' ? 'MIN' : parsePrimaryAxisAlign(keyword)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Cross axis alignment from `align-items`; `normal` stretches flex items. */
|
|
44
|
+
export function parseCSSAlignItems(value: string | undefined): LayoutCounterAlign | undefined {
|
|
45
|
+
const keyword = alignmentKeyword(value)
|
|
46
|
+
return keyword === 'normal' ? 'STRETCH' : parseCounterAxisAlign(keyword)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** One flex item's cross axis alignment from `align-self`; `normal` stretches it. */
|
|
50
|
+
export function parseCSSAlignSelf(value: string | undefined): LayoutAlignSelf | undefined {
|
|
51
|
+
const keyword = alignmentKeyword(value)
|
|
52
|
+
return keyword === 'normal' ? 'STRETCH' : parseLayoutAlignSelf(keyword)
|
|
53
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import valueParser, { type Node } from 'postcss-value-parser'
|
|
2
|
+
|
|
3
|
+
import { tryParseColor } from '../color'
|
|
4
|
+
import { BLACK } from '../constants'
|
|
5
|
+
import type { Color } from '../primitives'
|
|
6
|
+
import type { Effect } from '../types'
|
|
7
|
+
import { parseCSSNumber } from './values'
|
|
8
|
+
|
|
9
|
+
/** The layers of a shadow list, split at its top-level commas. */
|
|
10
|
+
function layersOf(nodes: Node[]): Node[][] {
|
|
11
|
+
const layers: Node[][] = []
|
|
12
|
+
let current: Node[] = []
|
|
13
|
+
for (const node of nodes) {
|
|
14
|
+
if (node.type === 'div' && node.value === ',') {
|
|
15
|
+
layers.push(current)
|
|
16
|
+
current = []
|
|
17
|
+
} else if (node.type !== 'space' && node.type !== 'comment') {
|
|
18
|
+
current.push(node)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
layers.push(current)
|
|
22
|
+
return layers
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* One layer: `inset`, two to four lengths, and a color in any order. Without a color it is
|
|
27
|
+
* black, as `currentColor` usually is; a layer with anything else, such as `none`, is dropped.
|
|
28
|
+
*/
|
|
29
|
+
function shadowOf(nodes: Node[]): Effect[] {
|
|
30
|
+
let inset = false
|
|
31
|
+
let color: Color | null = null
|
|
32
|
+
const lengths: number[] = []
|
|
33
|
+
for (const node of nodes) {
|
|
34
|
+
if (node.type === 'word' && node.value.toLowerCase() === 'inset') {
|
|
35
|
+
inset = true
|
|
36
|
+
continue
|
|
37
|
+
}
|
|
38
|
+
const length = node.type === 'word' ? parseCSSNumber(node.value) : null
|
|
39
|
+
if (length !== null) {
|
|
40
|
+
lengths.push(length)
|
|
41
|
+
continue
|
|
42
|
+
}
|
|
43
|
+
const parsed = tryParseColor(valueParser.stringify(node))
|
|
44
|
+
if (!parsed || color) return []
|
|
45
|
+
color = parsed
|
|
46
|
+
}
|
|
47
|
+
if (lengths.length < 2 || lengths.length > 4 || color?.a === 0) return []
|
|
48
|
+
const [x = 0, y = 0, radius = 0, spread = 0] = lengths
|
|
49
|
+
return [
|
|
50
|
+
{
|
|
51
|
+
type: inset ? 'INNER_SHADOW' : 'DROP_SHADOW',
|
|
52
|
+
color: color ?? { ...BLACK },
|
|
53
|
+
offset: { x, y },
|
|
54
|
+
radius,
|
|
55
|
+
spread,
|
|
56
|
+
visible: true
|
|
57
|
+
}
|
|
58
|
+
]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The shadows of a CSS `box-shadow` or `text-shadow` list, such as
|
|
63
|
+
* `0 4px 8px rgb(0 0 0 / 0.2), inset 0 1px #fff`: drop shadows, and inner shadows for `inset`.
|
|
64
|
+
*/
|
|
65
|
+
export function parseCSSShadows(value: string | undefined): Effect[] {
|
|
66
|
+
if (!value) return []
|
|
67
|
+
return layersOf(valueParser(value).nodes).flatMap(shadowOf)
|
|
68
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import valueParser from 'postcss-value-parser'
|
|
2
|
+
|
|
3
|
+
import { tryParseColor } from '../color'
|
|
4
|
+
import type { Color } from '../primitives'
|
|
5
|
+
|
|
6
|
+
/** Pixels in a rem, as browsers use by default. */
|
|
7
|
+
const ROOT_FONT_SIZE = 16
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A CSS number in pixels: unitless, `px`, or `rem`. Null for `auto`, for units that depend on
|
|
11
|
+
* what the value applies to (`%`, `em`, `vh`), and for anything that is not a number.
|
|
12
|
+
*/
|
|
13
|
+
export function parseCSSNumber(value: string | undefined): number | null {
|
|
14
|
+
if (!value) return null
|
|
15
|
+
const dimension = valueParser.unit(value.trim())
|
|
16
|
+
if (!dimension) return null
|
|
17
|
+
const number = Number(dimension.number)
|
|
18
|
+
if (!Number.isFinite(number)) return null
|
|
19
|
+
switch (dimension.unit.toLowerCase()) {
|
|
20
|
+
case '':
|
|
21
|
+
case 'px':
|
|
22
|
+
return number
|
|
23
|
+
case 'rem':
|
|
24
|
+
return number * ROOT_FONT_SIZE
|
|
25
|
+
default:
|
|
26
|
+
return null
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A CSS color that paints something; null for an invalid or fully transparent one. */
|
|
31
|
+
export function parseCSSColor(value: string | undefined): Color | null {
|
|
32
|
+
if (!value) return null
|
|
33
|
+
const color = tryParseColor(value.trim())
|
|
34
|
+
return color && color.a > 0 ? color : null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The space-separated parts of a CSS value, keeping functions such as `rgb(0, 0, 0)` whole. */
|
|
38
|
+
export function splitCSSValue(value: string): string[] {
|
|
39
|
+
return valueParser(value)
|
|
40
|
+
.nodes.filter((node) => node.type !== 'space' && node.type !== 'comment')
|
|
41
|
+
.map((node) => valueParser.stringify(node))
|
|
42
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a node's export settings may persist. Each id must name a Core IO
|
|
3
|
+
* export adapter that supports node export; labels and options come from that
|
|
4
|
+
* adapter, so this list only decides what the Export panel offers.
|
|
5
|
+
*/
|
|
6
|
+
export const EXPORT_FORMAT_IDS = ['png', 'jpg', 'webp', 'svg', 'pdf', 'pptx'] as const
|
|
7
|
+
|
|
8
|
+
export type ExportFormatId = (typeof EXPORT_FORMAT_IDS)[number]
|
|
9
|
+
|
|
10
|
+
export function isExportFormatId(value: unknown): value is ExportFormatId {
|
|
11
|
+
return EXPORT_FORMAT_IDS.some((id) => id === value)
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SceneNode } from '../types'
|
|
2
|
+
|
|
3
|
+
/** Placement/orientation only; dimensions are a separate geometry contract. */
|
|
4
|
+
export const TRANSFORM_FIELDS = [
|
|
5
|
+
'x',
|
|
6
|
+
'y',
|
|
7
|
+
'rotation',
|
|
8
|
+
'flipX',
|
|
9
|
+
'flipY'
|
|
10
|
+
] as const satisfies readonly (keyof SceneNode)[]
|
|
11
|
+
export const SIZE_FIELDS = ['width', 'height'] as const satisfies readonly (keyof SceneNode)[]
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { SceneNode } from '../types'
|
|
2
|
+
|
|
3
|
+
/** Fields shared by shaping, paragraph measurement, and auto-width invalidation. */
|
|
4
|
+
export const TEXT_METRIC_FIELDS = [
|
|
5
|
+
'text',
|
|
6
|
+
'fontSize',
|
|
7
|
+
'fontFamily',
|
|
8
|
+
'fontWeight',
|
|
9
|
+
'italic',
|
|
10
|
+
'letterSpacing',
|
|
11
|
+
'styleRuns',
|
|
12
|
+
'fontVariations',
|
|
13
|
+
'fontFeatures',
|
|
14
|
+
'textParagraphs',
|
|
15
|
+
'listSpacing',
|
|
16
|
+
'paragraphSpacing',
|
|
17
|
+
'paragraphIndent',
|
|
18
|
+
'hangingList'
|
|
19
|
+
] as const satisfies readonly (keyof SceneNode)[]
|
|
20
|
+
|
|
21
|
+
export const TEXT_SHAPING_FIELDS = [
|
|
22
|
+
...TEXT_METRIC_FIELDS,
|
|
23
|
+
'lineHeight',
|
|
24
|
+
'textCase',
|
|
25
|
+
'textDirection',
|
|
26
|
+
'textAlignHorizontal',
|
|
27
|
+
'textAlignVertical'
|
|
28
|
+
] as const satisfies readonly (keyof SceneNode)[]
|
|
29
|
+
|
|
30
|
+
export const TEXT_LAYOUT_FIELDS = [
|
|
31
|
+
'width',
|
|
32
|
+
'height',
|
|
33
|
+
'textAutoResize',
|
|
34
|
+
'maxLines'
|
|
35
|
+
] as const satisfies readonly (keyof SceneNode)[]
|
package/src/gradient.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import type { Color, Vector } from './primitives'
|
|
2
|
+
import type { Fill, GradientStop, GradientTransform } from './types'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Gradient paints in Figma's convention. A paint's `gradientTransform` maps the layer's unit
|
|
6
|
+
* square to gradient space, as Figma's plugin API, `.fig` files, and the clipboard store it. In
|
|
7
|
+
* gradient space a linear gradient runs from (0, 0.5) to (1, 0.5); radial, angular, and diamond
|
|
8
|
+
* gradients are centred on (0.5, 0.5) with radius 0.5, and an angular one starts at +x and turns
|
|
9
|
+
* clockwise. Every reader and writer of gradients goes through this module.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export type GradientFillType = Extract<
|
|
13
|
+
Fill['type'],
|
|
14
|
+
'GRADIENT_LINEAR' | 'GRADIENT_RADIAL' | 'GRADIENT_ANGULAR' | 'GRADIENT_DIAMOND'
|
|
15
|
+
>
|
|
16
|
+
|
|
17
|
+
export function isGradientFill(type: Fill['type']): type is GradientFillType {
|
|
18
|
+
return (
|
|
19
|
+
type === 'GRADIENT_LINEAR' ||
|
|
20
|
+
type === 'GRADIENT_RADIAL' ||
|
|
21
|
+
type === 'GRADIENT_ANGULAR' ||
|
|
22
|
+
type === 'GRADIENT_DIAMOND'
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Left to right for a linear gradient; centred and filling the layer for the others. */
|
|
27
|
+
export const IDENTITY_GRADIENT_TRANSFORM: GradientTransform = {
|
|
28
|
+
m00: 1,
|
|
29
|
+
m01: 0,
|
|
30
|
+
m02: 0,
|
|
31
|
+
m10: 0,
|
|
32
|
+
m11: 1,
|
|
33
|
+
m12: 0
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Figma's transform when a fill becomes a gradient: top to bottom, and centred when switched to
|
|
38
|
+
* another kind (Figma desktop 126).
|
|
39
|
+
*/
|
|
40
|
+
export const DEFAULT_GRADIENT_TRANSFORM: GradientTransform = {
|
|
41
|
+
m00: 0,
|
|
42
|
+
m01: 1,
|
|
43
|
+
m02: 0,
|
|
44
|
+
m10: -1,
|
|
45
|
+
m11: 0,
|
|
46
|
+
m12: 1
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** How dark a new gradient's second stop is next to the fill colour, as in Figma. */
|
|
50
|
+
const DEFAULT_GRADIENT_SHADE = 0.6
|
|
51
|
+
|
|
52
|
+
/** Figma's stops for a new gradient: the fill colour, then a 60% shade of it. */
|
|
53
|
+
export function defaultGradientStops(color: Color): GradientStop[] {
|
|
54
|
+
return [
|
|
55
|
+
{ color: { ...color }, position: 0 },
|
|
56
|
+
{
|
|
57
|
+
color: {
|
|
58
|
+
r: color.r * DEFAULT_GRADIENT_SHADE,
|
|
59
|
+
g: color.g * DEFAULT_GRADIENT_SHADE,
|
|
60
|
+
b: color.b * DEFAULT_GRADIENT_SHADE,
|
|
61
|
+
a: color.a
|
|
62
|
+
},
|
|
63
|
+
position: 1
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function isIdentityGradientTransform(t: GradientTransform): boolean {
|
|
69
|
+
return (Object.keys(IDENTITY_GRADIENT_TRANSFORM) as (keyof GradientTransform)[]).every(
|
|
70
|
+
(key) => t[key] === IDENTITY_GRADIENT_TRANSFORM[key]
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The inverse affine map; a singular one falls back to the identity. Inverting a
|
|
76
|
+
* `gradientTransform` gives the map from gradient space to the layer's unit square.
|
|
77
|
+
*/
|
|
78
|
+
export function invertGradientTransform(t: GradientTransform): GradientTransform {
|
|
79
|
+
const det = t.m00 * t.m11 - t.m01 * t.m10
|
|
80
|
+
if (Math.abs(det) < 1e-12) return IDENTITY_GRADIENT_TRANSFORM
|
|
81
|
+
const m00 = t.m11 / det
|
|
82
|
+
const m01 = -t.m01 / det
|
|
83
|
+
const m10 = -t.m10 / det
|
|
84
|
+
const m11 = t.m00 / det
|
|
85
|
+
return {
|
|
86
|
+
m00,
|
|
87
|
+
m01,
|
|
88
|
+
m02: -(m00 * t.m02 + m01 * t.m12),
|
|
89
|
+
m10,
|
|
90
|
+
m11,
|
|
91
|
+
m12: -(m10 * t.m02 + m11 * t.m12)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A point through an affine map. */
|
|
96
|
+
export function applyGradientTransform(t: GradientTransform, point: Vector): Vector {
|
|
97
|
+
return {
|
|
98
|
+
x: t.m00 * point.x + t.m01 * point.y + t.m02,
|
|
99
|
+
y: t.m10 * point.x + t.m11 * point.y + t.m12
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Where a point of gradient space falls in the layer's unit square. */
|
|
104
|
+
export function gradientPoint(t: GradientTransform, point: Vector): Vector {
|
|
105
|
+
return applyGradientTransform(invertGradientTransform(t), point)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The transform of a linear gradient from `start` to `end` whose bands run along `across`, all in
|
|
110
|
+
* the layer's unit square, such as an SVG gradient mapped through its own transform.
|
|
111
|
+
*/
|
|
112
|
+
export function linearGradientTransformFromAxes(
|
|
113
|
+
start: Vector,
|
|
114
|
+
end: Vector,
|
|
115
|
+
across: Vector
|
|
116
|
+
): GradientTransform {
|
|
117
|
+
return invertGradientTransform({
|
|
118
|
+
m00: end.x - start.x,
|
|
119
|
+
m01: across.x,
|
|
120
|
+
m02: start.x - across.x / 2,
|
|
121
|
+
m10: end.y - start.y,
|
|
122
|
+
m11: across.y,
|
|
123
|
+
m12: start.y - across.y / 2
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The transform of a linear gradient from `start` to `end`, both in the layer's unit square, in a
|
|
129
|
+
* `width` × `height` layer. Its second axis is square to the line on screen and `ratio` times as
|
|
130
|
+
* long; Figma keeps that ratio when a handle is dragged (measured on desktop 126), and it starts
|
|
131
|
+
* as the layer's height over its width.
|
|
132
|
+
*/
|
|
133
|
+
export function linearGradientTransform(
|
|
134
|
+
start: Vector,
|
|
135
|
+
end: Vector,
|
|
136
|
+
width = 1,
|
|
137
|
+
height = 1,
|
|
138
|
+
ratio = height / width
|
|
139
|
+
): GradientTransform {
|
|
140
|
+
const axis = { x: end.x - start.x, y: end.y - start.y }
|
|
141
|
+
return linearGradientTransformFromAxes(start, end, {
|
|
142
|
+
x: (-axis.y * height * ratio) / width,
|
|
143
|
+
y: (axis.x * width * ratio) / height
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The transform of a radial, angular, or diamond gradient from its centre and the ends of its two
|
|
149
|
+
* radii, all in the layer's unit square.
|
|
150
|
+
*/
|
|
151
|
+
export function ellipticalGradientTransform(
|
|
152
|
+
center: Vector,
|
|
153
|
+
radiusX: Vector,
|
|
154
|
+
radiusY: Vector
|
|
155
|
+
): GradientTransform {
|
|
156
|
+
const xAxis = { x: 2 * (radiusX.x - center.x), y: 2 * (radiusX.y - center.y) }
|
|
157
|
+
const yAxis = { x: 2 * (radiusY.x - center.x), y: 2 * (radiusY.y - center.y) }
|
|
158
|
+
return invertGradientTransform({
|
|
159
|
+
m00: xAxis.x,
|
|
160
|
+
m01: yAxis.x,
|
|
161
|
+
m02: center.x - (xAxis.x + yAxis.x) / 2,
|
|
162
|
+
m10: xAxis.y,
|
|
163
|
+
m11: yAxis.y,
|
|
164
|
+
m12: center.y - (xAxis.y + yAxis.y) / 2
|
|
165
|
+
})
|
|
166
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { getAxisAlignedBoundsInParent } from './coordinate'
|
|
2
|
+
import type { SceneGraph } from './index'
|
|
3
|
+
import { FITTED_CONTAINER_TYPES } from './node-defaults'
|
|
4
|
+
import type { Rect } from './primitives'
|
|
5
|
+
import type { SceneNode } from './types'
|
|
6
|
+
|
|
7
|
+
type Placement = Pick<SceneNode, 'x' | 'y' | 'width' | 'height'>
|
|
8
|
+
|
|
9
|
+
/** What `fitEnclosingGroups` changed, so a caller can undo and redo it. */
|
|
10
|
+
export interface GroupFit {
|
|
11
|
+
before: Map<string, Placement>
|
|
12
|
+
after: Map<string, Placement>
|
|
13
|
+
/** Emptied groups, outermost last, with their index in their parent. */
|
|
14
|
+
removed: Array<{ node: SceneNode; index: number }>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface GroupFitOptions {
|
|
18
|
+
/**
|
|
19
|
+
* The box of a boolean operation's result in its parent's space, which Figma sizes a boolean to,
|
|
20
|
+
* or null to fit its operands. Measuring needs path operations, so the renderer supplies it.
|
|
21
|
+
*/
|
|
22
|
+
booleanBounds?: (node: SceneNode) => Rect | null
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function placement(node: SceneNode): Placement {
|
|
26
|
+
return { x: node.x, y: node.y, width: node.width, height: node.height }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Groups and booleans holding the parents, innermost first. */
|
|
30
|
+
function fittedAncestors(graph: SceneGraph, parentIds: Iterable<string>): SceneNode[] {
|
|
31
|
+
const found = new Map<string, { node: SceneNode; depth: number }>()
|
|
32
|
+
for (const parentId of parentIds) {
|
|
33
|
+
const chain: SceneNode[] = []
|
|
34
|
+
for (let node = graph.getNode(parentId); node && FITTED_CONTAINER_TYPES.has(node.type);) {
|
|
35
|
+
chain.push(node)
|
|
36
|
+
node = node.parentId ? graph.getNode(node.parentId) : undefined
|
|
37
|
+
}
|
|
38
|
+
for (const [index, node] of chain.entries()) {
|
|
39
|
+
found.set(node.id, {
|
|
40
|
+
node,
|
|
41
|
+
depth: Math.max(found.get(node.id)?.depth ?? 0, chain.length - index)
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return [...found.values()].sort((a, b) => b.depth - a.depth).map(({ node }) => node)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The layers a group's box covers. A mask hides the layers above it outside its shape, so, as in
|
|
50
|
+
* Figma, they count only through the mask: the box is the layers below it and the masks.
|
|
51
|
+
*/
|
|
52
|
+
function boundingChildren(children: SceneNode[]): SceneNode[] {
|
|
53
|
+
const firstMask = children.findIndex((child) => child.isMask)
|
|
54
|
+
if (firstMask === -1) return children
|
|
55
|
+
return [...children.slice(0, firstMask), ...children.slice(firstMask).filter((c) => c.isMask)]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Moves the group to its children's bounds, shifting them back so they stay put. */
|
|
59
|
+
function fitGroup(
|
|
60
|
+
graph: SceneGraph,
|
|
61
|
+
group: SceneNode,
|
|
62
|
+
children: SceneNode[],
|
|
63
|
+
fit: GroupFit,
|
|
64
|
+
options: GroupFitOptions
|
|
65
|
+
) {
|
|
66
|
+
if (!group.parentId || group.rotation !== 0 || group.flipX || group.flipY) return
|
|
67
|
+
const bounds =
|
|
68
|
+
(group.type === 'BOOLEAN_OPERATION' ? options.booleanBounds?.(group) : null) ??
|
|
69
|
+
getAxisAlignedBoundsInParent(boundingChildren(children), group.parentId, graph)
|
|
70
|
+
const dx = bounds.x - group.x
|
|
71
|
+
const dy = bounds.y - group.y
|
|
72
|
+
if (dx === 0 && dy === 0 && bounds.width === group.width && bounds.height === group.height) {
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
for (const node of [group, ...children]) {
|
|
76
|
+
if (!fit.before.has(node.id)) fit.before.set(node.id, placement(node))
|
|
77
|
+
}
|
|
78
|
+
graph.updateNode(group.id, bounds)
|
|
79
|
+
for (const child of children) {
|
|
80
|
+
graph.updateNode(child.id, { x: child.x - dx, y: child.y - dy })
|
|
81
|
+
}
|
|
82
|
+
for (const node of [group, ...children]) {
|
|
83
|
+
const current = graph.getNode(node.id)
|
|
84
|
+
if (current) fit.after.set(node.id, placement(current))
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** A group left without layers goes away; an empty boolean stays, as in Figma. */
|
|
89
|
+
function removeEmptyGroup(graph: SceneGraph, group: SceneNode, fit: GroupFit) {
|
|
90
|
+
if (group.type !== 'GROUP' || !group.parentId) return
|
|
91
|
+
const index = graph.getNode(group.parentId)?.childIds.indexOf(group.id) ?? -1
|
|
92
|
+
fit.removed.push({ node: structuredClone(group), index })
|
|
93
|
+
graph.deleteNode(group.id)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Refits the groups and booleans around these parents to their children, keeping every child where
|
|
98
|
+
* it is on the canvas, and removes groups left empty, as Figma does after a layer inside one moves,
|
|
99
|
+
* resizes, or leaves. Rotated or flipped containers keep their bounds. Returns null when nothing
|
|
100
|
+
* changed.
|
|
101
|
+
*/
|
|
102
|
+
export function fitEnclosingGroups(
|
|
103
|
+
graph: SceneGraph,
|
|
104
|
+
parentIds: Iterable<string>,
|
|
105
|
+
options: GroupFitOptions = {}
|
|
106
|
+
): GroupFit | null {
|
|
107
|
+
const fit: GroupFit = { before: new Map(), after: new Map(), removed: [] }
|
|
108
|
+
for (const group of fittedAncestors(graph, parentIds)) {
|
|
109
|
+
const children = graph.getChildren(group.id)
|
|
110
|
+
if (children.length === 0) removeEmptyGroup(graph, group, fit)
|
|
111
|
+
else fitGroup(graph, group, children, fit, options)
|
|
112
|
+
}
|
|
113
|
+
return fit.before.size === 0 && fit.removed.length === 0 ? null : fit
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Reapplies a fit after `undoGroupFit`. */
|
|
117
|
+
export function redoGroupFit(graph: SceneGraph, fit: GroupFit): void {
|
|
118
|
+
for (const [id, value] of fit.after) graph.updateNode(id, value)
|
|
119
|
+
for (const { node } of fit.removed) graph.deleteNode(node.id)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Brings back the groups a fit removed and their earlier bounds. */
|
|
123
|
+
export function undoGroupFit(graph: SceneGraph, fit: GroupFit): void {
|
|
124
|
+
// Outer groups were removed after inner ones, so they come back first.
|
|
125
|
+
for (const { node, index } of fit.removed.toReversed()) {
|
|
126
|
+
const parentId = node.parentId ?? graph.rootId
|
|
127
|
+
graph.createNode(node.type, parentId, { ...structuredClone(node), childIds: [] })
|
|
128
|
+
if (index >= 0) graph.insertChildAt(node.id, parentId, index)
|
|
129
|
+
}
|
|
130
|
+
for (const [id, value] of fit.before) graph.updateNode(id, value)
|
|
131
|
+
}
|
package/src/guides.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { SceneGraph } from './index'
|
|
1
2
|
import type { GUID } from './primitives'
|
|
3
|
+
import type { SceneNode } from './types'
|
|
2
4
|
|
|
3
5
|
export interface CanvasGuide {
|
|
4
6
|
id: string
|
|
@@ -6,3 +8,40 @@ export interface CanvasGuide {
|
|
|
6
8
|
position: number
|
|
7
9
|
figGuid?: GUID
|
|
8
10
|
}
|
|
11
|
+
|
|
12
|
+
const ownersByGraph = new WeakMap<SceneGraph, Set<string>>()
|
|
13
|
+
|
|
14
|
+
function ownerSet(graph: SceneGraph): Set<string> {
|
|
15
|
+
const existing = ownersByGraph.get(graph)
|
|
16
|
+
if (existing) return existing
|
|
17
|
+
const owners = new Set<string>()
|
|
18
|
+
for (const node of graph.getAllNodes()) if (node.guides.length > 0) owners.add(node.id)
|
|
19
|
+
const track = (node: SceneNode | undefined, id: string) => {
|
|
20
|
+
if (node && node.guides.length > 0) owners.add(id)
|
|
21
|
+
else owners.delete(id)
|
|
22
|
+
}
|
|
23
|
+
graph.onNodeEvents({
|
|
24
|
+
created: (node) => track(node, node.id),
|
|
25
|
+
updated: (id, changes) => {
|
|
26
|
+
if ('guides' in changes) track(graph.getNode(id), id)
|
|
27
|
+
},
|
|
28
|
+
deleted: (id) => owners.delete(id)
|
|
29
|
+
})
|
|
30
|
+
ownersByGraph.set(graph, owners)
|
|
31
|
+
return owners
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The layers on a page that carry guides, which a frame anywhere in the tree can, as in Figma.
|
|
36
|
+
* The graph keeps the set current from its node events, so drawing and hit-testing guides do not
|
|
37
|
+
* walk the page's whole tree on every frame.
|
|
38
|
+
*/
|
|
39
|
+
export function guideOwnersOnPage(graph: SceneGraph, pageId: string): SceneNode[] {
|
|
40
|
+
const owners: SceneNode[] = []
|
|
41
|
+
for (const id of ownerSet(graph)) {
|
|
42
|
+
const node = graph.getNode(id)
|
|
43
|
+
if (!node || node.guides.length === 0) continue
|
|
44
|
+
if (graph.pageOf(id) === pageId) owners.push(node)
|
|
45
|
+
}
|
|
46
|
+
return owners
|
|
47
|
+
}
|