@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/constants.ts
CHANGED
|
@@ -1,8 +1,25 @@
|
|
|
1
1
|
import type { Color } from './primitives'
|
|
2
|
+
import type { GlassEffectParams } from './types'
|
|
2
3
|
|
|
3
4
|
export const BLACK: Color = { r: 0, g: 0, b: 0, a: 1 }
|
|
4
5
|
export const WHITE: Color = { r: 1, g: 1, b: 1, a: 1 }
|
|
5
6
|
export const TRANSPARENT: Color = { r: 0, g: 0, b: 0, a: 0 }
|
|
7
|
+
/** The lens of a glass effect whose values are missing: the least it can be, so it bends nothing. */
|
|
8
|
+
export const UNSET_GLASS: Readonly<GlassEffectParams> = {
|
|
9
|
+
refraction: 0,
|
|
10
|
+
depth: 1,
|
|
11
|
+
lightAngle: 0,
|
|
12
|
+
lightIntensity: 0,
|
|
13
|
+
dispersion: 0
|
|
14
|
+
}
|
|
15
|
+
/** The lens a new glass effect starts with when nothing sets its values. */
|
|
16
|
+
export const DEFAULT_GLASS: Readonly<GlassEffectParams> = {
|
|
17
|
+
refraction: 0.5,
|
|
18
|
+
depth: 20,
|
|
19
|
+
lightAngle: -45,
|
|
20
|
+
lightIntensity: 0.5,
|
|
21
|
+
dispersion: 0
|
|
22
|
+
}
|
|
6
23
|
export const DEFAULT_FONT_FAMILY = 'Inter'
|
|
7
24
|
export const DEFAULT_STROKE_MITER_LIMIT = 4
|
|
8
25
|
export const DEFAULT_STROKE_WEIGHT = 1
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { SceneGraph } from './graph'
|
|
2
|
+
import { FITTED_CONTAINER_TYPES } from './node-defaults'
|
|
3
|
+
import type { ConstraintType, SceneNode } from './types'
|
|
4
|
+
|
|
5
|
+
type ConstraintGraph = Pick<SceneGraph, 'getNode' | 'closest'>
|
|
6
|
+
|
|
7
|
+
/** Containers whose children keep their place by their own constraints when they resize. */
|
|
8
|
+
export const CONSTRAINT_PARENT_TYPES: ReadonlySet<SceneNode['type']> = new Set<SceneNode['type']>([
|
|
9
|
+
'FRAME',
|
|
10
|
+
'COMPONENT',
|
|
11
|
+
'COMPONENT_SET',
|
|
12
|
+
'INSTANCE'
|
|
13
|
+
])
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The frame, component, or instance whose resize moves `node` by its constraints: its parent, or,
|
|
17
|
+
* for a layer in groups and booleans, the frame around the outermost of them, as in Figma (live,
|
|
18
|
+
* #1835). Null when nothing places it by constraints: on a page or in a section, or in an auto
|
|
19
|
+
* layout flow.
|
|
20
|
+
*/
|
|
21
|
+
export function constraintFrame(graph: ConstraintGraph, node: SceneNode): SceneNode | null {
|
|
22
|
+
let layer = node
|
|
23
|
+
const parent = node.parentId
|
|
24
|
+
? graph.closest(node.parentId, (ancestor) => {
|
|
25
|
+
if (!FITTED_CONTAINER_TYPES.has(ancestor.type)) return true
|
|
26
|
+
layer = ancestor
|
|
27
|
+
return false
|
|
28
|
+
})
|
|
29
|
+
: undefined
|
|
30
|
+
if (!parent || !CONSTRAINT_PARENT_TYPES.has(parent.type)) return null
|
|
31
|
+
if (parent.layoutMode !== 'NONE' && layer.layoutPositioning !== 'ABSOLUTE') return null
|
|
32
|
+
return parent
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Whether `node` has constraints the Design panel offers and the canvas shows. */
|
|
36
|
+
export function isConstraintEligible(graph: ConstraintGraph, node: SceneNode): boolean {
|
|
37
|
+
return constraintFrame(graph, node) !== null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The layers whose constraints `node` stands for. A group or boolean has none of its own: it shows
|
|
42
|
+
* and sets those of the layers in it, through nested groups and booleans, as in Figma.
|
|
43
|
+
*/
|
|
44
|
+
export function constraintLayers(graph: ConstraintGraph, node: SceneNode): SceneNode[] {
|
|
45
|
+
if (!FITTED_CONTAINER_TYPES.has(node.type)) return [node]
|
|
46
|
+
return node.childIds.flatMap((id) => {
|
|
47
|
+
const child = graph.getNode(id)
|
|
48
|
+
return child ? constraintLayers(graph, child) : []
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The constraint the layers share on one axis, or null when they differ or there are none. */
|
|
53
|
+
export function sharedConstraint(
|
|
54
|
+
layers: readonly SceneNode[],
|
|
55
|
+
key: 'horizontalConstraint' | 'verticalConstraint'
|
|
56
|
+
): ConstraintType | null {
|
|
57
|
+
const first = layers.at(0)?.[key]
|
|
58
|
+
if (first === undefined) return null
|
|
59
|
+
return layers.every((layer) => layer[key] === first) ? first : null
|
|
60
|
+
}
|
package/src/coordinate.ts
CHANGED
|
@@ -288,3 +288,223 @@ export function getParentToContainerMatrix(
|
|
|
288
288
|
}
|
|
289
289
|
return matrix
|
|
290
290
|
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* A selected layer's place in a multi-selection's canvas bounds, taken when a resize starts so
|
|
294
|
+
* every step re-places the layer from where it began rather than from its previewed state.
|
|
295
|
+
*/
|
|
296
|
+
export interface SelectionPlacement {
|
|
297
|
+
node: SceneNode
|
|
298
|
+
center: Vector
|
|
299
|
+
/** Draws the layer from its own (0,0)…(width,height) on the canvas. */
|
|
300
|
+
world: Mat3
|
|
301
|
+
/** Draws the layer's parent on the canvas. */
|
|
302
|
+
parentWorld: Mat3
|
|
303
|
+
/** -1 when the layer is drawn mirrored, so its rotation turns the other way on the canvas. */
|
|
304
|
+
direction: 1 | -1
|
|
305
|
+
/** Maps canvas points into the layer's parent's space, or null when it cannot be inverted. */
|
|
306
|
+
parentInverse: Mat3 | null
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export function selectionPlacement(
|
|
310
|
+
node: SceneNode,
|
|
311
|
+
graph: Pick<SceneGraph, 'getNode' | 'rootId'>
|
|
312
|
+
): SelectionPlacement {
|
|
313
|
+
const world = getWorldMatrix(node, graph)
|
|
314
|
+
const [cx, cy] = Matrix.mapPoints(world, [node.width / 2, node.height / 2])
|
|
315
|
+
const parent = node.parentId ? graph.getNode(node.parentId) : undefined
|
|
316
|
+
const parentWorld = getParentWorldMatrix(parent, graph)
|
|
317
|
+
return {
|
|
318
|
+
node: { ...node },
|
|
319
|
+
center: { x: cx, y: cy },
|
|
320
|
+
world,
|
|
321
|
+
parentWorld,
|
|
322
|
+
direction: world[0] * world[4] - world[1] * world[3] < 0 ? -1 : 1,
|
|
323
|
+
parentInverse: Matrix.invert(parentWorld)
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** The position, in its parent's space, that puts `node`'s center at a canvas point. */
|
|
328
|
+
function positionCenteredAt(node: SceneNode, parentInverse: Mat3 | null, center: Vector): Vector {
|
|
329
|
+
const [px, py] = parentInverse
|
|
330
|
+
? Matrix.mapPoints(parentInverse, [center.x, center.y])
|
|
331
|
+
: [center.x, center.y]
|
|
332
|
+
// The local matrix translates last, so the layer's center at the origin gives the offset.
|
|
333
|
+
const [qx, qy] = Matrix.mapPoints(getNodeLocalMatrix(node, { x: 0, y: 0 }), [
|
|
334
|
+
node.width / 2,
|
|
335
|
+
node.height / 2
|
|
336
|
+
])
|
|
337
|
+
return { x: px - qx, y: py - qy }
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export interface ResizedBoundsOptions {
|
|
341
|
+
/** The layer is locked to its ratio: it scales evenly instead of stretching. */
|
|
342
|
+
keepRatio?: boolean
|
|
343
|
+
/** Adjusts a locked layer's sizes, such as to whole pixels. */
|
|
344
|
+
round?: (size: number) => number
|
|
345
|
+
/**
|
|
346
|
+
* Lands an upright layer's edges on whole pixels from the box's top-left corner, as Figma places
|
|
347
|
+
* the layers of a selection a handle resizes while Snap to pixel grid is on.
|
|
348
|
+
*/
|
|
349
|
+
wholeEdges?: boolean
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** Within floating-point noise of each other. */
|
|
353
|
+
const sameEdge = (a: number, b: number) => Math.abs(a - b) < 1e-6
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* `from`..`to` on the canvas with both ends a whole number of pixels from the box's `start`. An end
|
|
357
|
+
* on the box's own edge stays there, so a box that snapped to a fractional guide keeps its layers on
|
|
358
|
+
* it; a span of no length, such as a level line's height, stays at none.
|
|
359
|
+
*/
|
|
360
|
+
function wholeSpan(
|
|
361
|
+
box: { start: number; end: number },
|
|
362
|
+
from: number,
|
|
363
|
+
to: number
|
|
364
|
+
): { start: number; size: number } {
|
|
365
|
+
const land = (edge: number) =>
|
|
366
|
+
sameEdge(edge, box.end) ? box.end : box.start + Math.round(edge - box.start)
|
|
367
|
+
const start = land(from)
|
|
368
|
+
if (sameEdge(from, to)) return { start, size: 0 }
|
|
369
|
+
return { start, size: Math.max(1, land(to) - start) }
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Where a layer sits in its parent and how large it is, with its angle and flips. */
|
|
373
|
+
export type LayerPlacement = Pick<
|
|
374
|
+
SceneNode,
|
|
375
|
+
'x' | 'y' | 'width' | 'height' | 'rotation' | 'flipX' | 'flipY'
|
|
376
|
+
>
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Where a layer lands, in its parent's space, when a multi-selection's canvas box `from` is
|
|
380
|
+
* resized to `to`. Figma scales the layer's whole transform with the box, which skews a rotated
|
|
381
|
+
* layer under an uneven scale. A layer here cannot skew, so it takes what Figma then reports for
|
|
382
|
+
* it, and what a `.fig` file of that result opens as: its top-left corner and x axis where the
|
|
383
|
+
* scale puts them, its width and height the scaled lengths of its own axes, and its y axis square
|
|
384
|
+
* to its x axis. An even scale, or a layer upright in the box, comes out exactly as in Figma.
|
|
385
|
+
*/
|
|
386
|
+
export function placeInResizedBounds(
|
|
387
|
+
placement: SelectionPlacement,
|
|
388
|
+
from: Rect,
|
|
389
|
+
to: Rect,
|
|
390
|
+
{
|
|
391
|
+
keepRatio = false,
|
|
392
|
+
round = (size: number) => size,
|
|
393
|
+
wholeEdges = false
|
|
394
|
+
}: ResizedBoundsOptions = {}
|
|
395
|
+
): LayerPlacement {
|
|
396
|
+
const { node, world, parentWorld } = placement
|
|
397
|
+
const scaleX = from.width > 0 ? to.width / from.width : 1
|
|
398
|
+
const scaleY = from.height > 0 ? to.height / from.height : 1
|
|
399
|
+
const scale = Matrix.multiply(
|
|
400
|
+
Matrix.translated(to.x - from.x * scaleX, to.y - from.y * scaleY),
|
|
401
|
+
Matrix.scaled(scaleX, scaleY)
|
|
402
|
+
)
|
|
403
|
+
const [a, b, tx, c, d, ty] = Matrix.multiply(scale, world)
|
|
404
|
+
// The scaled axes: x along the first column, y along the second.
|
|
405
|
+
const xLength = Math.hypot(a, c)
|
|
406
|
+
const yLength = Math.hypot(b, d)
|
|
407
|
+
// A layer locked to its ratio scales evenly, by the mean of the box's scales, as in Figma.
|
|
408
|
+
const even = Math.sqrt(Math.abs(scaleX * scaleY))
|
|
409
|
+
let width = keepRatio ? round(node.width * even) : node.width * xLength
|
|
410
|
+
let height = keepRatio ? round(node.height * even) : node.height * yLength
|
|
411
|
+
const ux = xLength > 0 ? a / xLength : 1
|
|
412
|
+
const uy = xLength > 0 ? c / xLength : 0
|
|
413
|
+
const mirrored = a * d - b * c < 0 ? -1 : 1
|
|
414
|
+
let [left, top] = [tx, ty]
|
|
415
|
+
const upright = Math.abs(uy) < 1e-9 && ux > 0 && mirrored > 0
|
|
416
|
+
if (wholeEdges && !keepRatio && upright) {
|
|
417
|
+
const across = wholeSpan({ start: to.x, end: to.x + to.width }, tx, tx + width)
|
|
418
|
+
const down = wholeSpan({ start: to.y, end: to.y + to.height }, ty, ty + height)
|
|
419
|
+
;[left, width, top, height] = [across.start, across.size, down.start, down.size]
|
|
420
|
+
}
|
|
421
|
+
const squared: Mat3 = [ux, -uy * mirrored, left, uy, ux * mirrored, top, 0, 0, 1]
|
|
422
|
+
const sized = { ...node, width, height }
|
|
423
|
+
const { x, y, rotation, flipX, flipY } =
|
|
424
|
+
localTransformFromWorld(sized, squared, parentWorld) ?? node
|
|
425
|
+
return { x, y, width, height, rotation, flipX, flipY }
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Where a layer lands, in its parent's space, when it scales evenly by `scale` about a canvas
|
|
430
|
+
* point, as Figma's Scale tool scales it: its angle and flips stay, and the point stays put.
|
|
431
|
+
*/
|
|
432
|
+
export function placeScaledAbout(
|
|
433
|
+
placement: SelectionPlacement,
|
|
434
|
+
anchor: Vector,
|
|
435
|
+
scale: number
|
|
436
|
+
): LayerPlacement {
|
|
437
|
+
const from = { x: anchor.x, y: anchor.y, width: 1, height: 1 }
|
|
438
|
+
return placeInResizedBounds(placement, from, { ...from, width: scale, height: scale })
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** A canvas pointer movement along the layer's own axes, as its handles measure it. */
|
|
442
|
+
export function canvasDeltaInLayer(placement: SelectionPlacement, dx: number, dy: number): Vector {
|
|
443
|
+
const [a, b, , c, d] = placement.world
|
|
444
|
+
const det = a * d - b * c
|
|
445
|
+
if (det === 0) return { x: dx, y: dy }
|
|
446
|
+
return { x: (d * dx - b * dy) / det, y: (a * dy - c * dx) / det }
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Where a layer lands when one of its handles resizes it in its own frame, from `from` to `to`,
|
|
451
|
+
* both its box in its parent's axes as an upright layer would have it. `to` is signed: a handle
|
|
452
|
+
* dragged past the opposite edge gives a negative size and mirrors the layer, as Figma flips it.
|
|
453
|
+
* The edges the handle leaves stay where they are on the canvas, whatever the layer's angle.
|
|
454
|
+
*/
|
|
455
|
+
export function placeInResizedFrame(
|
|
456
|
+
placement: SelectionPlacement,
|
|
457
|
+
from: Rect,
|
|
458
|
+
to: Rect
|
|
459
|
+
): LayerPlacement {
|
|
460
|
+
const { node, world, parentWorld } = placement
|
|
461
|
+
const width = Math.abs(to.width)
|
|
462
|
+
const height = Math.abs(to.height)
|
|
463
|
+
const mirrorX = to.width < 0
|
|
464
|
+
const mirrorY = to.height < 0
|
|
465
|
+
const turned = node.rotation !== 0 || node.flipX || node.flipY
|
|
466
|
+
if (!turned && !mirrorX && !mirrorY) {
|
|
467
|
+
return { x: to.x, y: to.y, width, height, rotation: 0, flipX: false, flipY: false }
|
|
468
|
+
}
|
|
469
|
+
const placed = Matrix.multiply(
|
|
470
|
+
Matrix.multiply(world, Matrix.translated(to.x - from.x, to.y - from.y)),
|
|
471
|
+
Matrix.scaled(mirrorX ? -1 : 1, mirrorY ? -1 : 1)
|
|
472
|
+
)
|
|
473
|
+
const local = localTransformFromWorld({ ...node, width, height }, placed, parentWorld)
|
|
474
|
+
if (!local)
|
|
475
|
+
return { ...to, width, height, rotation: node.rotation, flipX: node.flipX, flipY: node.flipY }
|
|
476
|
+
// Unmirrored, the angle and flips stay exactly as they were; only the position follows.
|
|
477
|
+
return mirrorX || mirrorY
|
|
478
|
+
? { ...local, width, height }
|
|
479
|
+
: {
|
|
480
|
+
x: local.x,
|
|
481
|
+
y: local.y,
|
|
482
|
+
width,
|
|
483
|
+
height,
|
|
484
|
+
rotation: node.rotation,
|
|
485
|
+
flipX: node.flipX,
|
|
486
|
+
flipY: node.flipY
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Where a layer lands, in its parent's space, when a multi-selection turns `degrees` about a
|
|
492
|
+
* canvas pivot, as Figma rotates a selection about its bounds' center: the layer's center swings
|
|
493
|
+
* around the pivot and the layer itself turns by the same angle on the canvas.
|
|
494
|
+
*/
|
|
495
|
+
export function placeRotatedAbout(
|
|
496
|
+
placement: SelectionPlacement,
|
|
497
|
+
pivot: Vector,
|
|
498
|
+
degrees: number
|
|
499
|
+
): { x: number; y: number; rotation: number } {
|
|
500
|
+
const { node, center, direction, parentInverse } = placement
|
|
501
|
+
const radians = (degrees * Math.PI) / 180
|
|
502
|
+
const cos = Math.cos(radians)
|
|
503
|
+
const sin = Math.sin(radians)
|
|
504
|
+
const dx = center.x - pivot.x
|
|
505
|
+
const dy = center.y - pivot.y
|
|
506
|
+
const target = { x: pivot.x + dx * cos - dy * sin, y: pivot.y + dx * sin + dy * cos }
|
|
507
|
+
const turned = node.rotation + degrees * direction
|
|
508
|
+
const rotation = ((((turned + 180) % 360) + 360) % 360) - 180
|
|
509
|
+
return { ...positionCenteredAt({ ...node, rotation }, parentInverse, target), rotation }
|
|
510
|
+
}
|
package/src/copy.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* no shared references between source and copy.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
import {
|
|
11
|
+
import { readGeometryBlob } from './geometry'
|
|
12
12
|
import { cloneInstanceOverrideState } from './instances/overrides'
|
|
13
13
|
import { createDefaultSourceMetadata } from './node-defaults'
|
|
14
14
|
import type {
|
|
@@ -50,11 +50,16 @@ export function copyStroke(s: Stroke): Stroke {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
export function copyEffect(e: Effect): Effect {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
const copy: Effect = { ...e, color: { ...e.color }, offset: { ...e.offset } }
|
|
54
|
+
if (e.glass) copy.glass = { ...e.glass }
|
|
55
|
+
if (e.progressive) {
|
|
56
|
+
copy.progressive = {
|
|
57
|
+
startRadius: e.progressive.startRadius,
|
|
58
|
+
startOffset: { ...e.progressive.startOffset },
|
|
59
|
+
endOffset: { ...e.progressive.endOffset }
|
|
60
|
+
}
|
|
57
61
|
}
|
|
62
|
+
return copy
|
|
58
63
|
}
|
|
59
64
|
|
|
60
65
|
export function copyStyleRun(r: StyleRun): StyleRun {
|
|
@@ -159,21 +164,15 @@ export function transformGeometryBlob(
|
|
|
159
164
|
ty = 0
|
|
160
165
|
): Uint8Array {
|
|
161
166
|
const out = blob.slice()
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
if (offset + 8 > out.length) break
|
|
170
|
-
const x = dv.getFloat32(offset, true)
|
|
171
|
-
const y = dv.getFloat32(offset + 4, true)
|
|
172
|
-
dv.setFloat32(offset, m00 * x + m01 * y + tx, true)
|
|
173
|
-
dv.setFloat32(offset + 4, m10 * x + m11 * y + ty, true)
|
|
174
|
-
offset += 8
|
|
167
|
+
const view = new DataView(out.buffer, out.byteOffset, out.byteLength)
|
|
168
|
+
readGeometryBlob(out, (_, coords, offset) => {
|
|
169
|
+
for (let i = 0; i < coords.length; i += 2) {
|
|
170
|
+
const x = coords[i]
|
|
171
|
+
const y = coords[i + 1]
|
|
172
|
+
view.setFloat32(offset + i * 4, m00 * x + m01 * y + tx, true)
|
|
173
|
+
view.setFloat32(offset + i * 4 + 4, m10 * x + m11 * y + ty, true)
|
|
175
174
|
}
|
|
176
|
-
}
|
|
175
|
+
})
|
|
177
176
|
return out
|
|
178
177
|
}
|
|
179
178
|
|
package/src/css/blend.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { BlendMode } from '../types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Each blend mode as CSS names it, for `mix-blend-mode` and `background-blend-mode`. Linear
|
|
5
|
+
* burn and linear dodge are the `plus-` modes, which `mix-blend-mode` takes and
|
|
6
|
+
* `background-blend-mode` does not.
|
|
7
|
+
*/
|
|
8
|
+
export const CSS_BLEND_MODE: Partial<Record<BlendMode, string>> = {
|
|
9
|
+
NORMAL: 'normal',
|
|
10
|
+
DARKEN: 'darken',
|
|
11
|
+
MULTIPLY: 'multiply',
|
|
12
|
+
COLOR_BURN: 'color-burn',
|
|
13
|
+
LIGHTEN: 'lighten',
|
|
14
|
+
LINEAR_BURN: 'plus-darker',
|
|
15
|
+
SCREEN: 'screen',
|
|
16
|
+
LINEAR_DODGE: 'plus-lighter',
|
|
17
|
+
COLOR_DODGE: 'color-dodge',
|
|
18
|
+
OVERLAY: 'overlay',
|
|
19
|
+
SOFT_LIGHT: 'soft-light',
|
|
20
|
+
HARD_LIGHT: 'hard-light',
|
|
21
|
+
DIFFERENCE: 'difference',
|
|
22
|
+
EXCLUSION: 'exclusion',
|
|
23
|
+
HUE: 'hue',
|
|
24
|
+
SATURATION: 'saturation',
|
|
25
|
+
COLOR: 'color',
|
|
26
|
+
LUMINOSITY: 'luminosity'
|
|
27
|
+
}
|
package/src/css/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { CSS_BLEND_MODE } from './blend'
|
|
1
2
|
export { parseCSSGridTracks } from './grid'
|
|
2
3
|
export {
|
|
3
4
|
parseCSSAlignItems,
|
|
@@ -6,5 +7,21 @@ export {
|
|
|
6
7
|
parseCSSJustifyContent
|
|
7
8
|
} from './layout'
|
|
8
9
|
export { parseCSSShadows } from './shadows'
|
|
9
|
-
export {
|
|
10
|
+
export {
|
|
11
|
+
CSS_GENERIC_FONT_FAMILIES,
|
|
12
|
+
cssFontFamilyName,
|
|
13
|
+
parseCSSFontFamilies,
|
|
14
|
+
parseCSSFontFamily,
|
|
15
|
+
parseCSSLetterSpacing,
|
|
16
|
+
parseCSSLineHeight,
|
|
17
|
+
resolveCSSFontFamily,
|
|
18
|
+
type CSSFontFamily
|
|
19
|
+
} from './text'
|
|
20
|
+
export {
|
|
21
|
+
parseCSSTransform,
|
|
22
|
+
parseCSSTransformOrigin,
|
|
23
|
+
parseCSSTransforms,
|
|
24
|
+
type CSSTransformBox,
|
|
25
|
+
type CSSTransformStyle
|
|
26
|
+
} from './transform'
|
|
10
27
|
export { parseCSSColor, parseCSSNumber, splitCSSValue } from './values'
|
package/src/css/text.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { DEFAULT_FONT_FAMILY } from '../constants'
|
|
3
2
|
import { percentLength, pixelLength } from '../text/lengths'
|
|
4
3
|
import type { TextLength } from '../types'
|
|
5
|
-
import { parseCSSNumber } from './values'
|
|
4
|
+
import { dimension, parseCSSNumber } from './values'
|
|
6
5
|
|
|
7
6
|
/** A CSS escape: hex digits and one optional whitespace, an escaped line break, or any one character. */
|
|
8
7
|
const CSS_ESCAPE = /\\(?:([0-9a-fA-F]{1,6})(?:\r\n|[ \t\n\r\f])?|(?:\r\n|[\n\r\f])|([\s\S]))?/gu
|
|
@@ -29,28 +28,97 @@ function unescapedIndexOf(text: string, stop: string, from: number): number {
|
|
|
29
28
|
return index
|
|
30
29
|
}
|
|
31
30
|
|
|
31
|
+
/** A family of a CSS `font-family` list: its name, and whether it was a quoted string. */
|
|
32
|
+
export interface CSSFontFamily {
|
|
33
|
+
name: string
|
|
34
|
+
quoted: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The family at the start of `text` and where the next one starts, or null when none is left. */
|
|
38
|
+
function nextFontFamily(text: string): { family: CSSFontFamily | null; rest: string } | null {
|
|
39
|
+
const trimmed = text.trimStart()
|
|
40
|
+
if (!trimmed) return null
|
|
41
|
+
const quote = trimmed[0]
|
|
42
|
+
if (quote === '"' || quote === "'") {
|
|
43
|
+
const end = unescapedIndexOf(trimmed, quote, 1)
|
|
44
|
+
const name = decodeCSSEscapes(trimmed.slice(1, end))
|
|
45
|
+
const comma = unescapedIndexOf(trimmed, ',', end)
|
|
46
|
+
return { family: name ? { name, quoted: true } : null, rest: trimmed.slice(comma + 1) }
|
|
47
|
+
}
|
|
48
|
+
const comma = unescapedIndexOf(trimmed, ',', 0)
|
|
49
|
+
const name = decodeCSSEscapes(trimmed.slice(0, comma)).trim().replace(/\s+/gu, ' ')
|
|
50
|
+
return { family: name ? { name, quoted: false } : null, rest: trimmed.slice(comma + 1) }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Every family of a CSS `font-family` list in order, without quotes and escapes, as
|
|
55
|
+
* `parseCSSFontFamily` reads the first.
|
|
56
|
+
*/
|
|
57
|
+
export function parseCSSFontFamilies(value: string | undefined): CSSFontFamily[] {
|
|
58
|
+
const families: CSSFontFamily[] = []
|
|
59
|
+
for (let next = nextFontFamily(value ?? ''); next; next = nextFontFamily(next.rest)) {
|
|
60
|
+
if (next.family) families.push(next.family)
|
|
61
|
+
}
|
|
62
|
+
return families
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function firstFontFamily(value: string | undefined): CSSFontFamily | null {
|
|
66
|
+
return parseCSSFontFamilies(value)[0] ?? null
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The real family a family of a `font-family` list names: a generic one, such as `sans-serif`,
|
|
71
|
+
* mapped to a real one, and any other name as it is. A quoted name is never a generic.
|
|
72
|
+
*/
|
|
73
|
+
export function cssFontFamilyName(family: CSSFontFamily): string {
|
|
74
|
+
if (family.quoted) return family.name
|
|
75
|
+
return CSS_GENERIC_FONT_FAMILIES[family.name.toLowerCase()] ?? family.name
|
|
76
|
+
}
|
|
77
|
+
|
|
32
78
|
/**
|
|
33
79
|
* The first family of a CSS `font-family` list, without its quotes and escapes. A family is a quoted
|
|
34
80
|
* string or unquoted words, which join with single spaces; a quote opens a string only at the start
|
|
35
81
|
* of the family, so a name list such as draw.io's keeps `O'Reilly`. Null for an empty list.
|
|
36
82
|
*/
|
|
37
83
|
export function parseCSSFontFamily(value: string | undefined): string | null {
|
|
38
|
-
|
|
39
|
-
if (!text) return null
|
|
40
|
-
const quote = text[0]
|
|
41
|
-
if (quote === '"' || quote === "'") {
|
|
42
|
-
return decodeCSSEscapes(text.slice(1, unescapedIndexOf(text, quote, 1))) || null
|
|
43
|
-
}
|
|
44
|
-
const words = decodeCSSEscapes(text.slice(0, unescapedIndexOf(text, ',', 0)))
|
|
45
|
-
return words.trim().replace(/\s+/gu, ' ') || null
|
|
84
|
+
return firstFontFamily(value)?.name ?? null
|
|
46
85
|
}
|
|
47
86
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
87
|
+
const SANS_SERIF_FAMILY = DEFAULT_FONT_FAMILY
|
|
88
|
+
/** The families FigJam sets its Bookish and Technical typefaces in, which every font provider serves. */
|
|
89
|
+
const SERIF_FAMILY = 'Merriweather'
|
|
90
|
+
const MONOSPACE_FAMILY = 'Roboto Mono'
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Real families for CSS generic families, which name no font but whatever the browser picks for
|
|
94
|
+
* them, and for `-apple-system` and `BlinkMacSystemFont`, which browsers read as `system-ui`.
|
|
95
|
+
* Keyed by lowercase name; the rest of the generics, such as `cursive`, draw in the sans-serif one.
|
|
96
|
+
*/
|
|
97
|
+
export const CSS_GENERIC_FONT_FAMILIES: Readonly<Record<string, string>> = {
|
|
98
|
+
serif: SERIF_FAMILY,
|
|
99
|
+
'ui-serif': SERIF_FAMILY,
|
|
100
|
+
monospace: MONOSPACE_FAMILY,
|
|
101
|
+
'ui-monospace': MONOSPACE_FAMILY,
|
|
102
|
+
'sans-serif': SANS_SERIF_FAMILY,
|
|
103
|
+
'ui-sans-serif': SANS_SERIF_FAMILY,
|
|
104
|
+
'ui-rounded': SANS_SERIF_FAMILY,
|
|
105
|
+
'system-ui': SANS_SERIF_FAMILY,
|
|
106
|
+
'-apple-system': SANS_SERIF_FAMILY,
|
|
107
|
+
blinkmacsystemfont: SANS_SERIF_FAMILY,
|
|
108
|
+
cursive: SANS_SERIF_FAMILY,
|
|
109
|
+
fantasy: SANS_SERIF_FAMILY,
|
|
110
|
+
math: SANS_SERIF_FAMILY,
|
|
111
|
+
fangsong: SANS_SERIF_FAMILY
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The family a CSS `font-family` list sets text in: its first family, with a generic family
|
|
116
|
+
* such as `sans-serif` or `system-ui` mapped to a real one. A quoted name is a family's own name,
|
|
117
|
+
* never a generic. Null for an empty list.
|
|
118
|
+
*/
|
|
119
|
+
export function resolveCSSFontFamily(value: string | undefined): string | null {
|
|
120
|
+
const family = firstFontFamily(value)
|
|
121
|
+
return family ? cssFontFamilyName(family) : null
|
|
54
122
|
}
|
|
55
123
|
|
|
56
124
|
/**
|