@glissade/scene 0.4.3 → 0.4.4
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/index.d.ts +51 -1
- package/dist/index.js +182 -3
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -28,6 +28,56 @@ declare class Highlight extends Node {
|
|
|
28
28
|
/** `children: [highlight(title, { color: '#ffe066' }), title]` — marker behind the text. */
|
|
29
29
|
declare function highlight(text: Text, props?: Omit<HighlightProps, 'text'>): Highlight;
|
|
30
30
|
//#endregion
|
|
31
|
+
//#region src/tokenHighlight.d.ts
|
|
32
|
+
interface TokenRange {
|
|
33
|
+
/** token text (whitespace-insensitive run match) or inclusive [from, to] wordBoxes indices */
|
|
34
|
+
match: string | readonly [number, number];
|
|
35
|
+
/** which occurrence of a string match; default 1 (the first) */
|
|
36
|
+
occurrence?: number;
|
|
37
|
+
/** range id for track targets ('<nodeId>/<rangeId>/fill' …); default 'r<index>' */
|
|
38
|
+
id?: string;
|
|
39
|
+
fill?: PropInit<string>;
|
|
40
|
+
opacity?: PropInit<number>;
|
|
41
|
+
/** 0→1 left-to-right reveal across the range; default 1 */
|
|
42
|
+
progress?: PropInit<number>;
|
|
43
|
+
/** scale about the range rect's center; default 1 */
|
|
44
|
+
scale?: PropInit<number>;
|
|
45
|
+
}
|
|
46
|
+
interface TokenHighlightProps extends NodeProps {
|
|
47
|
+
/** the Text whose tokens get highlighted; place this node as an EARLIER sibling */
|
|
48
|
+
text: Text;
|
|
49
|
+
ranges: TokenRange[];
|
|
50
|
+
/** marker overhang beyond the ink box, [x, y] px; default [4, 2] */
|
|
51
|
+
padding?: [number, number];
|
|
52
|
+
cornerRadius?: number;
|
|
53
|
+
/** re-resolve string matches every frame (animated text); default false — drift throws */
|
|
54
|
+
rematch?: boolean;
|
|
55
|
+
}
|
|
56
|
+
declare class TokenMatchError extends Error {
|
|
57
|
+
constructor(message: string);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Find the Nth whitespace-stripped consecutive run of boxes equal to token.
|
|
61
|
+
* Boundary-exact: a run that diverges mid-segment is not a match; ending
|
|
62
|
+
* mid-segment throws (with the real segment list) rather than half-boxing.
|
|
63
|
+
*/
|
|
64
|
+
declare function matchTokenRun(boxes: WordBox[], token: string, occurrence?: number): [number, number];
|
|
65
|
+
declare class TokenHighlight extends Node {
|
|
66
|
+
readonly target: Text;
|
|
67
|
+
readonly padding: [number, number];
|
|
68
|
+
readonly cornerRadius: number;
|
|
69
|
+
readonly rematch: boolean;
|
|
70
|
+
private readonly ranges;
|
|
71
|
+
constructor(props: TokenHighlightProps);
|
|
72
|
+
private resolveRun;
|
|
73
|
+
protected draw(out: DisplayListBuilder, ctx: EvalContext): void;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* `children: [tokenHighlight(para, { ranges: [{ match: '$48,200', fill: cat.money }] }), para]`
|
|
77
|
+
* — each range animates independently via '<id>/<rangeId>/fill|opacity|progress|scale'.
|
|
78
|
+
*/
|
|
79
|
+
declare function tokenHighlight(text: Text, props: Omit<TokenHighlightProps, 'text'>): TokenHighlight;
|
|
80
|
+
//#endregion
|
|
31
81
|
//#region src/assets.d.ts
|
|
32
82
|
/**
|
|
33
83
|
* Asset contracts (DESIGN.md §3.8): evaluate() never awaits — callers warm
|
|
@@ -218,4 +268,4 @@ declare function bindScene(scene: Scene, doc: Timeline): BindingCacheEntry;
|
|
|
218
268
|
*/
|
|
219
269
|
declare function evaluate(scene: Scene, doc: Timeline, t: number): DisplayList;
|
|
220
270
|
//#endregion
|
|
221
|
-
export { type AnchorSpec, type BindablePropTarget, type BlendMode, type CanvasLike, Circle, ColdAssetError, type Ctx2DLike, type DisplayList, type DisplayListBuilder, type DrawCommand, DuplicateNodeIdError, type EvalContext, type FilterSpec, FilterValidationError, type FontSpec, Group, Highlight, type HighlightProps, type HitArea, IDENTITY, type ImageHandle, ImageNode, type ImageProps, type LayoutBox, type LayoutChildSpec, type LayoutContainerSpec, type LayoutEngine, LayoutEngineMissingError, type LineBox, type Mat2x3, Node, type NodeProps, type Paint, Path, type PathLike, type PathProps, type PathSeg, type PropInit, Raster2D, type Raster2DHost, Rect, type Rect$1 as RectShape, type Resource, type ResourceId, type Scene, type SceneInit, type SceneModule, type ShaderCaps, ShaderEffect, type ShaderEffectProps, type ShaderRef, type ShapeProps, type StrokeStyle, Text, type TextMeasurer, type TextMetricsLite, type TextProps, Video, type VideoFrameSource, type VideoProps, type WordBox, applyToPoint, bindScene, breakLines, createDisplayListBuilder, createScene, estimatingMeasurer, evaluate, filtersToCanvasFilter, fontString, fromTRS, getLayoutEngine, glow, highlight, invert, matEquals, multiply, quantize, requireLayoutEngine, resolveAnchor, roundedRectSegs, segmentWords, setDefaultMeasurer, setLayoutEngine, validateFilters };
|
|
271
|
+
export { type AnchorSpec, type BindablePropTarget, type BlendMode, type CanvasLike, Circle, ColdAssetError, type Ctx2DLike, type DisplayList, type DisplayListBuilder, type DrawCommand, DuplicateNodeIdError, type EvalContext, type FilterSpec, FilterValidationError, type FontSpec, Group, Highlight, type HighlightProps, type HitArea, IDENTITY, type ImageHandle, ImageNode, type ImageProps, type LayoutBox, type LayoutChildSpec, type LayoutContainerSpec, type LayoutEngine, LayoutEngineMissingError, type LineBox, type Mat2x3, Node, type NodeProps, type Paint, Path, type PathLike, type PathProps, type PathSeg, type PropInit, Raster2D, type Raster2DHost, Rect, type Rect$1 as RectShape, type Resource, type ResourceId, type Scene, type SceneInit, type SceneModule, type ShaderCaps, ShaderEffect, type ShaderEffectProps, type ShaderRef, type ShapeProps, type StrokeStyle, Text, type TextMeasurer, type TextMetricsLite, type TextProps, TokenHighlight, type TokenHighlightProps, TokenMatchError, type TokenRange, Video, type VideoFrameSource, type VideoProps, type WordBox, applyToPoint, bindScene, breakLines, createDisplayListBuilder, createScene, estimatingMeasurer, evaluate, filtersToCanvasFilter, fontString, fromTRS, getLayoutEngine, glow, highlight, invert, matEquals, matchTokenRun, multiply, quantize, requireLayoutEngine, resolveAnchor, roundedRectSegs, segmentWords, setDefaultMeasurer, setLayoutEngine, tokenHighlight, validateFilters };
|
package/dist/index.js
CHANGED
|
@@ -19,8 +19,8 @@ var Highlight = class extends Node {
|
|
|
19
19
|
constructor(props) {
|
|
20
20
|
super(props);
|
|
21
21
|
this.target = props.text;
|
|
22
|
-
this.color = init(signal("#ffe066"), props.color);
|
|
23
|
-
this.progress = init(signal(1), props.progress);
|
|
22
|
+
this.color = init$1(signal("#ffe066"), props.color);
|
|
23
|
+
this.progress = init$1(signal(1), props.progress);
|
|
24
24
|
this.padding = props.padding ?? [4, 2];
|
|
25
25
|
this.cornerRadius = props.cornerRadius ?? 4;
|
|
26
26
|
this.registerTarget("progress", this.progress);
|
|
@@ -73,6 +73,185 @@ function highlight(text, props = {}) {
|
|
|
73
73
|
text
|
|
74
74
|
});
|
|
75
75
|
}
|
|
76
|
+
function init$1(sig, v) {
|
|
77
|
+
if (typeof v === "function") sig.bindSource(v);
|
|
78
|
+
else if (v !== void 0) sig.set(v);
|
|
79
|
+
return sig;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
//#region src/tokenHighlight.ts
|
|
83
|
+
/**
|
|
84
|
+
* Multi-range token highlight: sub-line ranges over a Text node's wordBoxes,
|
|
85
|
+
* each with its OWN animatable fill/opacity/progress/scale — four-color
|
|
86
|
+
* category passes, per-token flips, karaoke-with-color. Design answers from
|
|
87
|
+
* downstream production (the NNDL verification ritual):
|
|
88
|
+
* - ranges VALIDATE at construction and THROW on copy drift at draw — the
|
|
89
|
+
* throw is load-bearing (it catches edited copy that no longer matches);
|
|
90
|
+
* `rematch: true` opts animated text into per-frame re-resolution.
|
|
91
|
+
* - a range spanning a wrap produces one rect per line segment.
|
|
92
|
+
* - string matches are whitespace-stripped consecutive box runs and must end
|
|
93
|
+
* boundary-exact (mid-segment end = error listing the actual segments);
|
|
94
|
+
* `[wordIndex, wordIndex]` ranges sidestep matching entirely.
|
|
95
|
+
*/
|
|
96
|
+
var TokenMatchError = class extends Error {
|
|
97
|
+
constructor(message) {
|
|
98
|
+
super(message);
|
|
99
|
+
this.name = "TokenMatchError";
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const stripWs = (s) => s.replace(/\s+/g, "");
|
|
103
|
+
/**
|
|
104
|
+
* Find the Nth whitespace-stripped consecutive run of boxes equal to token.
|
|
105
|
+
* Boundary-exact: a run that diverges mid-segment is not a match; ending
|
|
106
|
+
* mid-segment throws (with the real segment list) rather than half-boxing.
|
|
107
|
+
*/
|
|
108
|
+
function matchTokenRun(boxes, token, occurrence = 1) {
|
|
109
|
+
const want = stripWs(token);
|
|
110
|
+
if (want.length === 0) throw new TokenMatchError("empty token");
|
|
111
|
+
let seen = 0;
|
|
112
|
+
for (let i = 0; i < boxes.length; i++) {
|
|
113
|
+
let acc = "";
|
|
114
|
+
for (let j = i; j < boxes.length; j++) {
|
|
115
|
+
acc += stripWs(boxes[j].text);
|
|
116
|
+
if (acc === want) {
|
|
117
|
+
seen++;
|
|
118
|
+
if (seen === occurrence) return [i, j];
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
if (!want.startsWith(acc)) break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
throw new TokenMatchError(`no match for token '${token}'${occurrence > 1 ? ` (occurrence ${occurrence})` : ""} — segments are [${boxes.map((b) => `'${b.text}'`).join(", ")}]`);
|
|
125
|
+
}
|
|
126
|
+
var TokenHighlight = class extends Node {
|
|
127
|
+
target;
|
|
128
|
+
padding;
|
|
129
|
+
cornerRadius;
|
|
130
|
+
rematch;
|
|
131
|
+
ranges;
|
|
132
|
+
constructor(props) {
|
|
133
|
+
super(props);
|
|
134
|
+
this.target = props.text;
|
|
135
|
+
this.padding = props.padding ?? [4, 2];
|
|
136
|
+
this.cornerRadius = props.cornerRadius ?? 4;
|
|
137
|
+
this.rematch = props.rematch ?? false;
|
|
138
|
+
const boxes = this.target.wordBoxes();
|
|
139
|
+
this.ranges = props.ranges.map((spec, index) => {
|
|
140
|
+
const run = this.resolveRun(boxes, spec);
|
|
141
|
+
const id = spec.id ?? `r${index}`;
|
|
142
|
+
const r = {
|
|
143
|
+
spec,
|
|
144
|
+
fill: init(signal("#ffe066"), spec.fill),
|
|
145
|
+
opacity: init(signal(1), spec.opacity),
|
|
146
|
+
progress: init(signal(1), spec.progress),
|
|
147
|
+
scale: init(signal(1), spec.scale),
|
|
148
|
+
run,
|
|
149
|
+
bound: runText(boxes, run)
|
|
150
|
+
};
|
|
151
|
+
this.registerTarget(`${id}/fill`, r.fill);
|
|
152
|
+
this.registerTarget(`${id}/opacity`, r.opacity);
|
|
153
|
+
this.registerTarget(`${id}/progress`, r.progress);
|
|
154
|
+
this.registerTarget(`${id}/scale`, r.scale);
|
|
155
|
+
return r;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
resolveRun(boxes, spec) {
|
|
159
|
+
if (typeof spec.match !== "string") {
|
|
160
|
+
const [from, to] = spec.match;
|
|
161
|
+
if (from < 0 || to >= boxes.length || from > to) throw new TokenMatchError(`word index range [${from}, ${to}] out of bounds (${boxes.length} boxes)`);
|
|
162
|
+
return [from, to];
|
|
163
|
+
}
|
|
164
|
+
return matchTokenRun(boxes, spec.match, spec.occurrence ?? 1);
|
|
165
|
+
}
|
|
166
|
+
draw(out, ctx) {
|
|
167
|
+
const boxes = this.target.wordBoxes(ctx.measurer);
|
|
168
|
+
if (boxes.length === 0) return;
|
|
169
|
+
const tm = this.target.localMatrix();
|
|
170
|
+
const [px, py] = this.padding;
|
|
171
|
+
let pushedTransform = false;
|
|
172
|
+
for (const r of this.ranges) {
|
|
173
|
+
const opacity = r.opacity();
|
|
174
|
+
if (opacity <= 0) continue;
|
|
175
|
+
const progress = Math.min(1, Math.max(0, r.progress()));
|
|
176
|
+
if (progress <= 0) continue;
|
|
177
|
+
let run = r.run;
|
|
178
|
+
if (this.rematch && typeof r.spec.match === "string") run = matchTokenRun(boxes, r.spec.match, r.spec.occurrence ?? 1);
|
|
179
|
+
else if (runText(boxes, run) !== r.bound) throw new TokenMatchError(`bound token '${r.bound}' no longer matches boxes [${run[0]}, ${run[1]}] — the text changed (segments now [${boxes.slice(run[0], run[1] + 1).map((b) => `'${b.text}'`).join(", ")}]); pass rematch: true for animated text`);
|
|
180
|
+
const lineRects = [];
|
|
181
|
+
for (let i = run[0]; i <= run[1]; i++) {
|
|
182
|
+
const b = boxes[i];
|
|
183
|
+
const last = lineRects[lineRects.length - 1];
|
|
184
|
+
const cur = i > run[0] && boxes[i - 1].line === b.line ? last : void 0;
|
|
185
|
+
if (cur) {
|
|
186
|
+
cur.w = b.x + b.w + px - cur.x;
|
|
187
|
+
cur.y = Math.min(cur.y, b.y - py);
|
|
188
|
+
cur.h = Math.max(cur.h, b.y + b.h + py - cur.y);
|
|
189
|
+
} else lineRects.push({
|
|
190
|
+
x: b.x - px,
|
|
191
|
+
y: b.y - py,
|
|
192
|
+
w: b.w + 2 * px,
|
|
193
|
+
h: b.h + 2 * py
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
if (!pushedTransform && !matEquals(tm, IDENTITY)) {
|
|
197
|
+
out.push({
|
|
198
|
+
op: "transform",
|
|
199
|
+
m: tm
|
|
200
|
+
});
|
|
201
|
+
pushedTransform = true;
|
|
202
|
+
}
|
|
203
|
+
const grouped = opacity < 1;
|
|
204
|
+
if (grouped) out.push({
|
|
205
|
+
op: "pushGroup",
|
|
206
|
+
opacity,
|
|
207
|
+
blend: "source-over",
|
|
208
|
+
filters: []
|
|
209
|
+
});
|
|
210
|
+
const fill = r.fill();
|
|
211
|
+
const scale = r.scale();
|
|
212
|
+
let remaining = progress * lineRects.reduce((sum, q) => sum + q.w, 0);
|
|
213
|
+
for (const q of lineRects) {
|
|
214
|
+
const fillW = Math.min(q.w, remaining);
|
|
215
|
+
remaining -= fillW;
|
|
216
|
+
if (fillW <= 0) break;
|
|
217
|
+
const cx = q.x + q.w / 2;
|
|
218
|
+
const cy = q.y + q.h / 2;
|
|
219
|
+
const w = fillW * scale;
|
|
220
|
+
const h = q.h * scale;
|
|
221
|
+
const x = cx - q.w / 2 * scale;
|
|
222
|
+
const y = cy - h / 2;
|
|
223
|
+
const rad = Math.min(this.cornerRadius, w / 2, h / 2);
|
|
224
|
+
const path = out.resource({
|
|
225
|
+
kind: "path",
|
|
226
|
+
segs: roundedRectSegs(x, y, w, h, rad)
|
|
227
|
+
});
|
|
228
|
+
out.push({
|
|
229
|
+
op: "fillPath",
|
|
230
|
+
path,
|
|
231
|
+
paint: {
|
|
232
|
+
kind: "color",
|
|
233
|
+
color: fill
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
if (remaining <= 0) break;
|
|
237
|
+
}
|
|
238
|
+
if (grouped) out.push({ op: "popGroup" });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
function runText(boxes, run) {
|
|
243
|
+
return boxes.slice(run[0], run[1] + 1).map((b) => stripWs(b.text)).join("");
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* `children: [tokenHighlight(para, { ranges: [{ match: '$48,200', fill: cat.money }] }), para]`
|
|
247
|
+
* — each range animates independently via '<id>/<rangeId>/fill|opacity|progress|scale'.
|
|
248
|
+
*/
|
|
249
|
+
function tokenHighlight(text, props) {
|
|
250
|
+
return new TokenHighlight({
|
|
251
|
+
...props,
|
|
252
|
+
text
|
|
253
|
+
});
|
|
254
|
+
}
|
|
76
255
|
function init(sig, v) {
|
|
77
256
|
if (typeof v === "function") sig.bindSource(v);
|
|
78
257
|
else if (v !== void 0) sig.set(v);
|
|
@@ -563,4 +742,4 @@ function evaluate(scene, doc, t) {
|
|
|
563
742
|
});
|
|
564
743
|
}
|
|
565
744
|
//#endregion
|
|
566
|
-
export { Circle, ColdAssetError, DuplicateNodeIdError, FilterValidationError, Group, Highlight, IDENTITY, ImageNode, LayoutEngineMissingError, Node, Path, Raster2D, Rect, ShaderEffect, Text, Video, applyToPoint, bindScene, breakLines, createDisplayListBuilder, createScene, estimatingMeasurer, evaluate, filtersToCanvasFilter, fontString, fromTRS, getLayoutEngine, glow, highlight, invert, matEquals, multiply, quantize, requireLayoutEngine, resolveAnchor, roundedRectSegs, segmentWords, setDefaultMeasurer, setLayoutEngine, validateFilters };
|
|
745
|
+
export { Circle, ColdAssetError, DuplicateNodeIdError, FilterValidationError, Group, Highlight, IDENTITY, ImageNode, LayoutEngineMissingError, Node, Path, Raster2D, Rect, ShaderEffect, Text, TokenHighlight, TokenMatchError, Video, applyToPoint, bindScene, breakLines, createDisplayListBuilder, createScene, estimatingMeasurer, evaluate, filtersToCanvasFilter, fontString, fromTRS, getLayoutEngine, glow, highlight, invert, matEquals, matchTokenRun, multiply, quantize, requireLayoutEngine, resolveAnchor, roundedRectSegs, segmentWords, setDefaultMeasurer, setLayoutEngine, tokenHighlight, validateFilters };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glissade/scene",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "glissade scene graph: nodes, transforms, DisplayList emission. Renderer-agnostic; zero DOM/Node dependencies.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"yoga-layout": "^3.2.1",
|
|
23
|
-
"@glissade/core": "0.4.
|
|
23
|
+
"@glissade/core": "0.4.4"
|
|
24
24
|
},
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|