@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,131 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
3
|
+
import type { PluginDataEntry } from '../types'
|
|
4
|
+
|
|
5
|
+
/** The plugin id OpenPencil keeps its own state under, on nodes and in `.fig` files. */
|
|
6
|
+
export const OPEN_PENCIL_PLUGIN_ID = 'open-pencil'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* What a field holds:
|
|
10
|
+
* - `content` belongs to the node like its fills do and exists only as plugin data, so it
|
|
11
|
+
* travels with a component copied into a library or another document;
|
|
12
|
+
* - `format` repeats a node or variable field for files that have no slot for it, where the
|
|
13
|
+
* node field is what counts;
|
|
14
|
+
* - `bookkeeping` records where a node or document came from and stays behind.
|
|
15
|
+
*/
|
|
16
|
+
export type PluginDataRole = 'content' | 'format' | 'bookkeeping'
|
|
17
|
+
|
|
18
|
+
/** One OpenPencil plugin-data key. */
|
|
19
|
+
export interface PluginDataKey {
|
|
20
|
+
readonly key: string
|
|
21
|
+
readonly role: PluginDataRole
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A plugin-data key and how its string value is read and written. */
|
|
25
|
+
export interface PluginDataField<T> extends PluginDataKey {
|
|
26
|
+
/** The stored value, or `undefined` when it is missing or not a valid value. */
|
|
27
|
+
decode(value: string | null | undefined): T | undefined
|
|
28
|
+
encode(value: T): string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A value stored as JSON. Invalid JSON and a wrong shape both read as missing: plugin data
|
|
33
|
+
* comes from files anyone can edit, so a bad entry is ignored rather than thrown on.
|
|
34
|
+
*/
|
|
35
|
+
export function jsonPluginDataField<T>(
|
|
36
|
+
key: string,
|
|
37
|
+
role: PluginDataRole,
|
|
38
|
+
schema: v.GenericSchema<unknown, T>
|
|
39
|
+
): PluginDataField<T> {
|
|
40
|
+
const stored = v.pipe(v.string(), v.parseJson(), schema)
|
|
41
|
+
return {
|
|
42
|
+
key,
|
|
43
|
+
role,
|
|
44
|
+
decode(value) {
|
|
45
|
+
const parsed = v.safeParse(stored, value)
|
|
46
|
+
return parsed.success ? parsed.output : undefined
|
|
47
|
+
},
|
|
48
|
+
encode: (value) => JSON.stringify(value)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A value stored as one of a fixed set of plain strings. */
|
|
53
|
+
export function textPluginDataField<const T extends string>(
|
|
54
|
+
key: string,
|
|
55
|
+
role: PluginDataRole,
|
|
56
|
+
values: readonly T[]
|
|
57
|
+
): PluginDataField<T> {
|
|
58
|
+
const schema = v.picklist(values)
|
|
59
|
+
return {
|
|
60
|
+
key,
|
|
61
|
+
role,
|
|
62
|
+
decode(value) {
|
|
63
|
+
const parsed = v.safeParse(schema, value)
|
|
64
|
+
return parsed.success ? parsed.output : undefined
|
|
65
|
+
},
|
|
66
|
+
encode: (value) => value
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function isPluginDataEntry(entry: PluginDataEntry, field: PluginDataKey): boolean {
|
|
71
|
+
return entry.pluginId === OPEN_PENCIL_PLUGIN_ID && entry.key === field.key
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function hasPluginData(
|
|
75
|
+
entries: readonly PluginDataEntry[] | undefined,
|
|
76
|
+
field: PluginDataKey
|
|
77
|
+
): boolean {
|
|
78
|
+
return entries?.some((entry) => isPluginDataEntry(entry, field)) ?? false
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function readPluginData<T>(
|
|
82
|
+
entries: readonly PluginDataEntry[] | undefined,
|
|
83
|
+
field: PluginDataField<T>
|
|
84
|
+
): T | undefined {
|
|
85
|
+
return field.decode(entries?.find((entry) => isPluginDataEntry(entry, field))?.value)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Every valid value of a field kept as several entries under one key. */
|
|
89
|
+
export function readAllPluginData<T>(
|
|
90
|
+
entries: readonly PluginDataEntry[] | undefined,
|
|
91
|
+
field: PluginDataField<T>
|
|
92
|
+
): T[] {
|
|
93
|
+
return (entries ?? []).flatMap((entry) => {
|
|
94
|
+
if (!isPluginDataEntry(entry, field)) return []
|
|
95
|
+
const value = field.decode(entry.value)
|
|
96
|
+
return value === undefined ? [] : [value]
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function pluginDataEntry<T>(field: PluginDataField<T>, value: T): PluginDataEntry {
|
|
101
|
+
return { pluginId: OPEN_PENCIL_PLUGIN_ID, key: field.key, value: field.encode(value) }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Entries other than those of `fields`; other plugins' entries are always kept. */
|
|
105
|
+
export function withoutPluginData(
|
|
106
|
+
entries: readonly PluginDataEntry[],
|
|
107
|
+
fields: readonly PluginDataKey[]
|
|
108
|
+
): PluginDataEntry[] {
|
|
109
|
+
return entries.filter((entry) => !fields.some((field) => isPluginDataEntry(entry, field)))
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Entries with `field` set to `value`, appended last; `undefined` removes the field. */
|
|
113
|
+
export function withPluginData<T>(
|
|
114
|
+
entries: readonly PluginDataEntry[],
|
|
115
|
+
field: PluginDataField<T>,
|
|
116
|
+
value: T | undefined
|
|
117
|
+
): PluginDataEntry[] {
|
|
118
|
+
return withAllPluginData(entries, field, value === undefined ? [] : [value])
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Entries with every entry of `field` replaced by one per value. */
|
|
122
|
+
export function withAllPluginData<T>(
|
|
123
|
+
entries: readonly PluginDataEntry[],
|
|
124
|
+
field: PluginDataField<T>,
|
|
125
|
+
values: readonly T[]
|
|
126
|
+
): PluginDataEntry[] {
|
|
127
|
+
return [
|
|
128
|
+
...withoutPluginData(entries, [field]),
|
|
129
|
+
...values.map((value) => pluginDataEntry(field, value))
|
|
130
|
+
]
|
|
131
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
3
|
+
import { behaviourSchema } from '../behaviours/schema'
|
|
4
|
+
import type { OkHCLPayload } from '../color/okhcl'
|
|
5
|
+
import { isExportFormatId, type ExportFormatId } from '../export-format'
|
|
6
|
+
import { clampExportScale } from '../export-scale'
|
|
7
|
+
import { iconSchema, iconTintSchema } from '../icons/schema'
|
|
8
|
+
import type { Rect } from '../primitives'
|
|
9
|
+
import { shaderPaintSchema } from '../shaders/schema'
|
|
10
|
+
import {
|
|
11
|
+
MODE_ATTRIBUTE_PATTERN,
|
|
12
|
+
TOKEN_UNITS,
|
|
13
|
+
type CommentThread,
|
|
14
|
+
type EnabledLibraryBinding,
|
|
15
|
+
type ExportSetting,
|
|
16
|
+
type LayoutDirection,
|
|
17
|
+
type LibraryAssetSource,
|
|
18
|
+
type PluginDataEntry,
|
|
19
|
+
type SourceLibraryPublication,
|
|
20
|
+
type TextDirection,
|
|
21
|
+
type TokenUnit
|
|
22
|
+
} from '../types'
|
|
23
|
+
import {
|
|
24
|
+
jsonPluginDataField,
|
|
25
|
+
textPluginDataField,
|
|
26
|
+
withoutPluginData,
|
|
27
|
+
type PluginDataKey
|
|
28
|
+
} from './field'
|
|
29
|
+
|
|
30
|
+
const DIRECTIONS = ['AUTO', 'LTR', 'RTL'] as const satisfies readonly (
|
|
31
|
+
| TextDirection
|
|
32
|
+
| LayoutDirection
|
|
33
|
+
)[]
|
|
34
|
+
|
|
35
|
+
const textPathBox: v.GenericSchema<unknown, Rect> = v.pipe(
|
|
36
|
+
v.object({ x: v.number(), y: v.number(), width: v.number(), height: v.number() }),
|
|
37
|
+
v.check(
|
|
38
|
+
({ x, y, width, height }) => Number.isFinite(x + y + width + height) && width > 0 && height > 0
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
/** String-valued entries of a JSON object; other entries are dropped, not rejected. */
|
|
43
|
+
const boundVariables = v.pipe(
|
|
44
|
+
v.custom<Record<string, unknown>>(
|
|
45
|
+
(value) => typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
46
|
+
),
|
|
47
|
+
v.transform((value) =>
|
|
48
|
+
Object.fromEntries(
|
|
49
|
+
Object.entries(value).filter(
|
|
50
|
+
(entry): entry is [string, string] => typeof entry[1] === 'string'
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
)
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
/** Every entry must be valid, or the whole value is ignored in favour of native settings. */
|
|
57
|
+
const exportSettings: v.GenericSchema<unknown, ExportSetting[]> = v.array(
|
|
58
|
+
v.pipe(
|
|
59
|
+
v.object({
|
|
60
|
+
scale: v.pipe(v.number(), v.finite()),
|
|
61
|
+
format: v.custom<ExportFormatId>(isExportFormatId)
|
|
62
|
+
}),
|
|
63
|
+
// Clamp at the file boundary: imported plugin data may carry a scale the UI never produces.
|
|
64
|
+
v.transform(({ scale, format }) => ({ scale: clampExportScale(scale), format }))
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
const librarySource: v.GenericSchema<unknown, LibraryAssetSource> = v.pipe(
|
|
69
|
+
v.object({
|
|
70
|
+
identity: v.object({ libraryId: v.string(), assetKey: v.string(), revisionId: v.string() }),
|
|
71
|
+
sourceNodeId: v.optional(v.unknown()),
|
|
72
|
+
readOnly: v.optional(v.unknown())
|
|
73
|
+
}),
|
|
74
|
+
v.transform(({ identity, sourceNodeId, readOnly }) => ({
|
|
75
|
+
identity,
|
|
76
|
+
sourceNodeId: typeof sourceNodeId === 'string' ? sourceNodeId : null,
|
|
77
|
+
readOnly: readOnly === true
|
|
78
|
+
}))
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
const enabledLibrary = v.object({
|
|
82
|
+
libraryId: v.string(),
|
|
83
|
+
revisionId: v.string(),
|
|
84
|
+
enabled: v.optional(v.unknown())
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
/** Invalid entries are skipped one by one, so one bad binding keeps the others. */
|
|
88
|
+
const enabledLibraries: v.GenericSchema<unknown, EnabledLibraryBinding[]> = v.pipe(
|
|
89
|
+
v.array(v.unknown()),
|
|
90
|
+
v.transform((items) =>
|
|
91
|
+
items.flatMap((item) => {
|
|
92
|
+
const parsed = v.safeParse(enabledLibrary, item)
|
|
93
|
+
if (!parsed.success) return []
|
|
94
|
+
const { libraryId, revisionId, enabled } = parsed.output
|
|
95
|
+
return [{ libraryId, revisionId, enabled: enabled === true }]
|
|
96
|
+
})
|
|
97
|
+
)
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
// Shape only. Whether a string is valid CSS is checked where it is written into a stylesheet.
|
|
101
|
+
const cssText = v.pipe(v.string(), v.trim(), v.nonEmpty(), v.maxLength(1000))
|
|
102
|
+
|
|
103
|
+
/** Token fields Figma has no slot for, with mode ids as the file spells them. */
|
|
104
|
+
export interface StoredToken {
|
|
105
|
+
unit?: TokenUnit
|
|
106
|
+
expressions?: Record<string, { css: string; resolved: number }>
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const token: v.GenericSchema<unknown, StoredToken> = v.object({
|
|
110
|
+
unit: v.optional(v.picklist(TOKEN_UNITS)),
|
|
111
|
+
expressions: v.optional(
|
|
112
|
+
v.record(v.string(), v.object({ css: cssText, resolved: v.pipe(v.number(), v.finite()) }))
|
|
113
|
+
)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
const okhcl: v.GenericSchema<unknown, OkHCLPayload> = v.object({
|
|
117
|
+
version: v.literal(1),
|
|
118
|
+
kind: v.picklist(['fill', 'stroke']),
|
|
119
|
+
index: v.number(),
|
|
120
|
+
color: v.object({
|
|
121
|
+
h: v.number(),
|
|
122
|
+
c: v.number(),
|
|
123
|
+
l: v.number(),
|
|
124
|
+
a: v.fallback(v.optional(v.number()), undefined)
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
const sourceLibraryPublication: v.GenericSchema<unknown, SourceLibraryPublication> = v.object({
|
|
129
|
+
libraryId: v.string(),
|
|
130
|
+
revisionId: v.string(),
|
|
131
|
+
name: v.string(),
|
|
132
|
+
catalogSource: v.optional(v.string())
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
// Lenient on purpose: other tools may write comments, so missing fields get defaults and one
|
|
136
|
+
// malformed thread is skipped instead of hiding every comment.
|
|
137
|
+
const commentAuthorColor = v.fallback(
|
|
138
|
+
v.optional(v.object({ r: v.number(), g: v.number(), b: v.number(), a: v.number() })),
|
|
139
|
+
undefined
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
/** What a comment and each reply say, who wrote them and when, and whether they were deleted. */
|
|
143
|
+
const commentEntry = {
|
|
144
|
+
author: v.fallback(v.string(), ''),
|
|
145
|
+
authorColor: commentAuthorColor,
|
|
146
|
+
text: v.fallback(v.string(), ''),
|
|
147
|
+
createdAt: v.fallback(v.string(), ''),
|
|
148
|
+
deleted: v.optional(v.boolean())
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const commentReply = v.object({ id: v.string(), ...commentEntry })
|
|
152
|
+
|
|
153
|
+
const commentThread = v.object({
|
|
154
|
+
id: v.string(),
|
|
155
|
+
pageId: v.string(),
|
|
156
|
+
pageName: v.optional(v.string()),
|
|
157
|
+
nodeId: v.optional(v.nullable(v.string())),
|
|
158
|
+
nodeName: v.optional(v.nullable(v.string())),
|
|
159
|
+
offsetX: v.optional(v.number()),
|
|
160
|
+
offsetY: v.optional(v.number()),
|
|
161
|
+
x: v.fallback(v.number(), 0),
|
|
162
|
+
y: v.fallback(v.number(), 0),
|
|
163
|
+
...commentEntry,
|
|
164
|
+
updatedAt: v.fallback(v.string(), ''),
|
|
165
|
+
resolved: v.fallback(v.boolean(), false),
|
|
166
|
+
resolvedAt: v.optional(v.nullable(v.string())),
|
|
167
|
+
replies: v.fallback(v.array(commentReply), [])
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
const comments: v.GenericSchema<unknown, CommentThread[]> = v.pipe(
|
|
171
|
+
v.array(v.unknown()),
|
|
172
|
+
v.transform((items) =>
|
|
173
|
+
items.flatMap((item) => {
|
|
174
|
+
const parsed = v.safeParse(commentThread, item)
|
|
175
|
+
if (!parsed.success) return []
|
|
176
|
+
const thread = parsed.output
|
|
177
|
+
return [{ ...thread, updatedAt: thread.updatedAt || thread.createdAt }]
|
|
178
|
+
})
|
|
179
|
+
)
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Every plugin-data entry OpenPencil writes under {@link OPEN_PENCIL_PLUGIN_ID}: state Figma's
|
|
184
|
+
* format has no field for. Add a key here, never as a string elsewhere, so reading and writing
|
|
185
|
+
* it share one schema.
|
|
186
|
+
*/
|
|
187
|
+
export const OPEN_PENCIL_PLUGIN_DATA = {
|
|
188
|
+
/** A text node's direction, on TEXT nodes. */
|
|
189
|
+
textDirection: textPluginDataField('textDirection', 'format', DIRECTIONS),
|
|
190
|
+
/** An auto-layout frame's direction. */
|
|
191
|
+
layoutDirection: textPluginDataField('layoutDirection', 'format', DIRECTIONS),
|
|
192
|
+
/** Marks a component set Figma would read as a plain frame. */
|
|
193
|
+
nodeType: textPluginDataField('nodeType', 'format', ['COMPONENT_SET']),
|
|
194
|
+
/** Variable bindings by field path, including those Figma cannot store natively. */
|
|
195
|
+
boundVariables: jsonPluginDataField('boundVariables', 'format', boundVariables),
|
|
196
|
+
exportSettings: jsonPluginDataField('exportSettings', 'format', exportSettings),
|
|
197
|
+
/** The node-local rect a TEXT_PATH layout maps onto, which Kiwi has no field for. */
|
|
198
|
+
textPathBox: jsonPluginDataField('textPathBox', 'format', textPathBox),
|
|
199
|
+
/** The library asset a node was inserted from. */
|
|
200
|
+
librarySource: jsonPluginDataField('librarySource', 'bookkeeping', librarySource),
|
|
201
|
+
/** Libraries enabled for the document, on the DOCUMENT node. */
|
|
202
|
+
enabledLibraries: jsonPluginDataField('enabledLibraries', 'bookkeeping', enabledLibraries),
|
|
203
|
+
/** The library this document publishes as, on the DOCUMENT node. */
|
|
204
|
+
sourceLibraryPublication: jsonPluginDataField(
|
|
205
|
+
'sourceLibraryPublication',
|
|
206
|
+
'bookkeeping',
|
|
207
|
+
sourceLibraryPublication
|
|
208
|
+
),
|
|
209
|
+
/** Canvas comment threads, deleted ones included, on the DOCUMENT node. */
|
|
210
|
+
comments: jsonPluginDataField('comments', 'bookkeeping', comments),
|
|
211
|
+
/** Unit and CSS expressions, on each VARIABLE. */
|
|
212
|
+
token: jsonPluginDataField('token', 'format', token),
|
|
213
|
+
/** Mode conditions by mode id, on each VARIABLE_SET. */
|
|
214
|
+
modeConditions: jsonPluginDataField('modeConditions', 'format', v.record(v.string(), cssText)),
|
|
215
|
+
/** The attribute that switches manual modes, on each VARIABLE_SET. */
|
|
216
|
+
modeAttribute: jsonPluginDataField(
|
|
217
|
+
'modeAttribute',
|
|
218
|
+
'format',
|
|
219
|
+
v.pipe(v.string(), v.trim(), v.maxLength(100), v.regex(MODE_ATTRIBUTE_PATTERN))
|
|
220
|
+
),
|
|
221
|
+
/** One entry per paint picked in OkHCL, so the picker reopens on the same coordinates. */
|
|
222
|
+
okhcl: jsonPluginDataField('okhcl', 'content', okhcl),
|
|
223
|
+
/** How a main component or component set behaves as a control in preview. */
|
|
224
|
+
behaviour: jsonPluginDataField('behaviour', 'content', behaviourSchema),
|
|
225
|
+
/** The icon a frame draws, such as `lucide:home`, on the frame its paths are in. */
|
|
226
|
+
icon: jsonPluginDataField('icon', 'content', iconSchema),
|
|
227
|
+
/** Which of an icon path's paints its icon's color sets, on each path the source drew in it. */
|
|
228
|
+
iconTint: jsonPluginDataField('iconTint', 'content', iconTintSchema),
|
|
229
|
+
/** One entry per paint that draws a shader, matched to its image paint by the frame's hash. */
|
|
230
|
+
shader: jsonPluginDataField('shader', 'content', shaderPaintSchema)
|
|
231
|
+
} satisfies Record<string, PluginDataKey>
|
|
232
|
+
|
|
233
|
+
const NOT_CONTENT = Object.values(OPEN_PENCIL_PLUGIN_DATA).filter(
|
|
234
|
+
(field) => field.role !== 'content'
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* A node's plugin data that is part of its content, in a stable order: OpenPencil's content
|
|
239
|
+
* fields and every other plugin's entries, which the node carries wherever it goes. Format
|
|
240
|
+
* copies of node fields and bookkeeping are left out.
|
|
241
|
+
*/
|
|
242
|
+
export function contentPluginData(entries: readonly PluginDataEntry[]): PluginDataEntry[] {
|
|
243
|
+
return withoutPluginData(entries, NOT_CONTENT).sort(
|
|
244
|
+
(a, b) => a.pluginId.localeCompare(b.pluginId) || a.key.localeCompare(b.key)
|
|
245
|
+
)
|
|
246
|
+
}
|
package/src/polygon.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { polygonVertices } from './geometry'
|
|
2
|
+
import type { Vector } from './primitives'
|
|
3
|
+
import type { SceneNode } from './types'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Rounded polygon and star outlines, as Figma draws them. Each corner, convex or not, is rounded by
|
|
7
|
+
* a circular arc tangent to both edges, capped so that it and its neighbours fit along every edge;
|
|
8
|
+
* corner smoothing replaces the arc's ends with curves that ease into the edges, as on rectangles.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Figma keeps a polygon's sides or a star's points within this range, and its plugin API clamps to it. */
|
|
12
|
+
export const POINT_COUNT_RANGE = { min: 3, max: 60 } as const
|
|
13
|
+
|
|
14
|
+
export type PolygonOutlineCommand =
|
|
15
|
+
| { type: 'M' | 'L'; x: number; y: number }
|
|
16
|
+
| { type: 'C'; x1: number; y1: number; x2: number; y2: number; x: number; y: number }
|
|
17
|
+
| { type: 'Z' }
|
|
18
|
+
|
|
19
|
+
type PolygonNode = Pick<
|
|
20
|
+
SceneNode,
|
|
21
|
+
| 'type'
|
|
22
|
+
| 'width'
|
|
23
|
+
| 'height'
|
|
24
|
+
| 'pointCount'
|
|
25
|
+
| 'starInnerRadius'
|
|
26
|
+
| 'cornerRadius'
|
|
27
|
+
| 'cornerSmoothing'
|
|
28
|
+
>
|
|
29
|
+
|
|
30
|
+
export interface PolygonCorner {
|
|
31
|
+
vertex: Vector
|
|
32
|
+
/** The unit direction of the edge arriving at the vertex. */
|
|
33
|
+
incoming: Vector
|
|
34
|
+
/** The unit direction of the edge leaving the vertex. */
|
|
35
|
+
outgoing: Vector
|
|
36
|
+
/** How far the outline turns at the vertex, in radians: 0 for a straight line. */
|
|
37
|
+
turn: number
|
|
38
|
+
/** The radius the corner is drawn with, after fitting it along its edges. */
|
|
39
|
+
radius: number
|
|
40
|
+
/** How far along each edge from the vertex the rounding starts. */
|
|
41
|
+
extent: number
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const sub = (a: Vector, b: Vector): Vector => ({ x: a.x - b.x, y: a.y - b.y })
|
|
45
|
+
const along = (point: Vector, direction: Vector, distance: number): Vector => ({
|
|
46
|
+
x: point.x + direction.x * distance,
|
|
47
|
+
y: point.y + direction.y * distance
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
function unit(vector: Vector): Vector {
|
|
51
|
+
const length = Math.hypot(vector.x, vector.y)
|
|
52
|
+
return length > 0 ? { x: vector.x / length, y: vector.y / length } : { x: 0, y: 0 }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The perpendicular of `direction` on the side the outline turns towards. */
|
|
56
|
+
function inward(direction: Vector, towards: Vector): Vector {
|
|
57
|
+
const normal = { x: -direction.y, y: direction.x }
|
|
58
|
+
return normal.x * towards.x + normal.y * towards.y < 0
|
|
59
|
+
? { x: direction.y, y: -direction.x }
|
|
60
|
+
: normal
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The polygon's or star's corners with the radius each is drawn with. A corner turning by α keeps
|
|
65
|
+
* r·tan(α/2) of each edge, so on an edge of length L between corners turning by α and β the radius
|
|
66
|
+
* is at most L / (tan(α/2) + tan(β/2)); each corner takes the least of its two edges' limits.
|
|
67
|
+
*/
|
|
68
|
+
export function polygonCorners(node: PolygonNode): PolygonCorner[] {
|
|
69
|
+
const vertices = polygonVertices(node)
|
|
70
|
+
const count = vertices.length
|
|
71
|
+
const at = (index: number) => vertices[(index + count) % count]
|
|
72
|
+
const shape = vertices.map((vertex, index) => {
|
|
73
|
+
const incoming = unit(sub(vertex, at(index - 1)))
|
|
74
|
+
const outgoing = unit(sub(at(index + 1), vertex))
|
|
75
|
+
const cos = Math.min(1, Math.max(-1, incoming.x * outgoing.x + incoming.y * outgoing.y))
|
|
76
|
+
const turn = Math.acos(cos)
|
|
77
|
+
return { vertex, incoming, outgoing, turn, factor: Math.tan(turn / 2) }
|
|
78
|
+
})
|
|
79
|
+
const edgeLimit = (index: number) => {
|
|
80
|
+
const from = shape[(index + count) % count]
|
|
81
|
+
const to = shape[(index + 1 + count) % count]
|
|
82
|
+
const length = Math.hypot(to.vertex.x - from.vertex.x, to.vertex.y - from.vertex.y)
|
|
83
|
+
const factors = from.factor + to.factor
|
|
84
|
+
return factors > 0 ? length / factors : Number.POSITIVE_INFINITY
|
|
85
|
+
}
|
|
86
|
+
const requested = Math.max(0, node.cornerRadius)
|
|
87
|
+
return shape.map(({ factor, ...corner }, index) => {
|
|
88
|
+
const radius = Math.min(requested, edgeLimit(index - 1), edgeLimit(index))
|
|
89
|
+
return { ...corner, radius, extent: radius * factor }
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
interface SmoothCorner {
|
|
94
|
+
/** Where the rounding starts, along each edge from the vertex. */
|
|
95
|
+
p: number
|
|
96
|
+
a: number
|
|
97
|
+
b: number
|
|
98
|
+
c: number
|
|
99
|
+
d: number
|
|
100
|
+
/** The angle each eased end takes from the arc, in radians. */
|
|
101
|
+
ease: number
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Figma's smoothing for a corner turning by any angle: the rectangle construction with the
|
|
106
|
+
* tangent length r·tan(α/2) in place of r and α/2 in place of 45°. Smoothing grows the rounded
|
|
107
|
+
* part of each edge by up to its tangent length again.
|
|
108
|
+
*/
|
|
109
|
+
function smoothCorner(corner: PolygonCorner, smoothing: number): SmoothCorner {
|
|
110
|
+
const { radius, extent } = corner
|
|
111
|
+
const p = (1 + smoothing) * extent
|
|
112
|
+
const ease = (corner.turn * smoothing) / 2
|
|
113
|
+
const toEase = radius * Math.tan(ease / 2)
|
|
114
|
+
const c = toEase * Math.cos(ease)
|
|
115
|
+
const d = toEase * Math.sin(ease)
|
|
116
|
+
const arcStart = extent - radius * Math.sin(ease) - d
|
|
117
|
+
const b = (p - arcStart - c - d) / 3
|
|
118
|
+
return { p, a: 2 * b, b, c, d, ease }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function rotate(direction: Vector, towards: Vector, angle: number): Vector {
|
|
122
|
+
const normal = inward(direction, towards)
|
|
123
|
+
return {
|
|
124
|
+
x: direction.x * Math.cos(angle) + normal.x * Math.sin(angle),
|
|
125
|
+
y: direction.y * Math.cos(angle) + normal.y * Math.sin(angle)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function cubic(c1: Vector, c2: Vector, to: Vector): PolygonOutlineCommand {
|
|
130
|
+
return { type: 'C', x1: c1.x, y1: c1.y, x2: c2.x, y2: c2.y, x: to.x, y: to.y }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The rounded corner from where it leaves the incoming edge to where it joins the outgoing one. */
|
|
134
|
+
function cornerCommands(corner: PolygonCorner, smooth: SmoothCorner): PolygonOutlineCommand[] {
|
|
135
|
+
const { vertex, incoming, outgoing, radius, turn } = corner
|
|
136
|
+
const { p, a, b, c, d, ease } = smooth
|
|
137
|
+
const back = { x: -incoming.x, y: -incoming.y }
|
|
138
|
+
const start = along(vertex, back, p)
|
|
139
|
+
const end = along(vertex, outgoing, p)
|
|
140
|
+
const normalIn = inward(incoming, outgoing)
|
|
141
|
+
const normalOut = inward(outgoing, back)
|
|
142
|
+
const arcFrom = along(along(start, incoming, a + b + c), normalIn, d)
|
|
143
|
+
const arcTo = along(along(end, outgoing, -(a + b + c)), normalOut, d)
|
|
144
|
+
const commands: PolygonOutlineCommand[] = []
|
|
145
|
+
if (ease > 0)
|
|
146
|
+
commands.push(cubic(along(start, incoming, a), along(start, incoming, a + b), arcFrom))
|
|
147
|
+
const sweep = turn - 2 * ease
|
|
148
|
+
if (sweep > 0) {
|
|
149
|
+
const handle = (4 / 3) * Math.tan(sweep / 4) * radius
|
|
150
|
+
const leaving = rotate(incoming, outgoing, ease)
|
|
151
|
+
const arriving = rotate(outgoing, incoming, ease)
|
|
152
|
+
commands.push(cubic(along(arcFrom, leaving, handle), along(arcTo, arriving, -handle), arcTo))
|
|
153
|
+
}
|
|
154
|
+
if (ease > 0) commands.push(cubic(along(end, outgoing, -(a + b)), along(end, outgoing, -a), end))
|
|
155
|
+
return commands
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The smoothing every corner can take: on each edge a corner may use the part its tangent length
|
|
160
|
+
* takes of both corners' tangent lengths, and Figma lowers smoothing for the whole shape to what
|
|
161
|
+
* its tightest corner allows rather than corner by corner.
|
|
162
|
+
*/
|
|
163
|
+
function fittedSmoothing(corners: readonly PolygonCorner[], smoothing: number): number {
|
|
164
|
+
const count = corners.length
|
|
165
|
+
const share = (corner: PolygonCorner, neighbour: PolygonCorner) => {
|
|
166
|
+
const length = Math.hypot(
|
|
167
|
+
neighbour.vertex.x - corner.vertex.x,
|
|
168
|
+
neighbour.vertex.y - corner.vertex.y
|
|
169
|
+
)
|
|
170
|
+
return (length * corner.extent) / (corner.extent + neighbour.extent)
|
|
171
|
+
}
|
|
172
|
+
return corners.reduce(
|
|
173
|
+
(fitted, corner, index) => {
|
|
174
|
+
if (!(corner.extent > 0)) return fitted
|
|
175
|
+
const budget = Math.min(
|
|
176
|
+
share(corner, corners[(index - 1 + count) % count]),
|
|
177
|
+
share(corner, corners[(index + 1) % count])
|
|
178
|
+
)
|
|
179
|
+
return Math.min(fitted, budget / corner.extent - 1)
|
|
180
|
+
},
|
|
181
|
+
Math.min(Math.max(smoothing, 0), 1)
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The outline of a polygon or star with its corner radius and smoothing, in its own pixels. */
|
|
186
|
+
export function polygonOutline(node: PolygonNode): PolygonOutlineCommand[] {
|
|
187
|
+
const corners = polygonCorners(node)
|
|
188
|
+
const smoothing = Math.max(0, fittedSmoothing(corners, node.cornerSmoothing))
|
|
189
|
+
const commands: PolygonOutlineCommand[] = []
|
|
190
|
+
let current: Vector | null = null
|
|
191
|
+
// Corners whose rounding meets along an edge leave no line between them.
|
|
192
|
+
const lineTo = ({ x, y }: Vector) => {
|
|
193
|
+
if (current && Math.hypot(x - current.x, y - current.y) < 1e-6) return
|
|
194
|
+
commands.push({ type: current ? 'L' : 'M', x, y })
|
|
195
|
+
current = { x, y }
|
|
196
|
+
}
|
|
197
|
+
for (const corner of corners) {
|
|
198
|
+
if (!(corner.radius > 0) || !(corner.turn > 0)) {
|
|
199
|
+
lineTo(corner.vertex)
|
|
200
|
+
continue
|
|
201
|
+
}
|
|
202
|
+
const smooth = smoothCorner(corner, smoothing)
|
|
203
|
+
lineTo(along(corner.vertex, { x: -corner.incoming.x, y: -corner.incoming.y }, smooth.p))
|
|
204
|
+
for (const command of cornerCommands(corner, smooth)) {
|
|
205
|
+
commands.push(command)
|
|
206
|
+
if (command.type === 'C') current = { x: command.x, y: command.y }
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
commands.push({ type: 'Z' })
|
|
210
|
+
return commands
|
|
211
|
+
}
|
package/src/preview.ts
CHANGED
package/src/random.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export function randomHex(bytes = 16): string {
|
|
2
|
+
if (!Number.isInteger(bytes) || bytes < 0) {
|
|
3
|
+
throw new RangeError('bytes must be a non-negative integer')
|
|
4
|
+
}
|
|
5
|
+
const buf = crypto.getRandomValues(new Uint8Array(bytes))
|
|
6
|
+
let hex = ''
|
|
7
|
+
for (const b of buf) hex += b.toString(16).padStart(2, '0')
|
|
8
|
+
return hex
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function randomInt(): number {
|
|
12
|
+
return crypto.getRandomValues(new Int32Array(1))[0]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function randomIndex(length: number): number {
|
|
16
|
+
if (!Number.isInteger(length) || length <= 0) {
|
|
17
|
+
throw new RangeError('length must be a positive integer')
|
|
18
|
+
}
|
|
19
|
+
const max = 0x1_0000_0000
|
|
20
|
+
const limit = max - (max % length)
|
|
21
|
+
const buffer = new Uint32Array(1)
|
|
22
|
+
|
|
23
|
+
do {
|
|
24
|
+
crypto.getRandomValues(buffer)
|
|
25
|
+
} while (buffer[0] >= limit)
|
|
26
|
+
|
|
27
|
+
return buffer[0] % length
|
|
28
|
+
}
|