@open-artifact/motion-stage 0.0.0-stage → 0.12.0

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.
Files changed (173) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +69 -4
  5. package/src/build.d.ts +18 -0
  6. package/src/build.js +73 -0
  7. package/src/host.d.ts +37 -0
  8. package/src/host.js +76 -0
  9. package/src/index.d.ts +11 -0
  10. package/src/index.js +6 -0
  11. package/src/layers.d.ts +30 -0
  12. package/src/layers.js +56 -0
  13. package/src/offline/audio.d.ts +19 -0
  14. package/src/offline/audio.js +20 -0
  15. package/src/offline/data.d.ts +26 -0
  16. package/src/offline/data.js +71 -0
  17. package/src/offline/main.d.ts +9 -0
  18. package/src/offline/main.js +52 -0
  19. package/src/offline/player.d.ts +23 -0
  20. package/src/offline/player.js +155 -0
  21. package/src/protocol/guards.d.ts +6 -0
  22. package/src/protocol/guards.js +208 -0
  23. package/src/protocol/layer-guards.d.ts +7 -0
  24. package/src/protocol/layer-guards.js +97 -0
  25. package/src/protocol/layers.d.ts +123 -0
  26. package/src/protocol/layers.js +1 -0
  27. package/src/protocol/types.d.ts +261 -0
  28. package/src/protocol/types.js +7 -0
  29. package/src/protocol.d.ts +2 -0
  30. package/src/protocol.js +2 -0
  31. package/src/raster.d.ts +4 -0
  32. package/src/raster.js +2 -0
  33. package/src/runtime/assets.d.ts +19 -0
  34. package/src/runtime/assets.js +63 -0
  35. package/src/runtime/captions.d.ts +26 -0
  36. package/src/runtime/captions.js +103 -0
  37. package/src/runtime/console.d.ts +13 -0
  38. package/src/runtime/console.js +99 -0
  39. package/src/runtime/cut.d.ts +7 -0
  40. package/src/runtime/cut.js +63 -0
  41. package/src/runtime/determinism.d.ts +12 -0
  42. package/src/runtime/determinism.js +47 -0
  43. package/src/runtime/film.d.ts +40 -0
  44. package/src/runtime/film.js +29 -0
  45. package/src/runtime/focus.d.ts +8 -0
  46. package/src/runtime/focus.js +71 -0
  47. package/src/runtime/fonts.d.ts +22 -0
  48. package/src/runtime/fonts.js +109 -0
  49. package/src/runtime/frame.d.ts +9 -0
  50. package/src/runtime/frame.js +123 -0
  51. package/src/runtime/kit/camera.d.ts +40 -0
  52. package/src/runtime/kit/camera.js +56 -0
  53. package/src/runtime/kit/chip.d.ts +25 -0
  54. package/src/runtime/kit/chip.js +75 -0
  55. package/src/runtime/kit/cursor.d.ts +46 -0
  56. package/src/runtime/kit/cursor.js +108 -0
  57. package/src/runtime/kit/dom.d.ts +14 -0
  58. package/src/runtime/kit/dom.js +46 -0
  59. package/src/runtime/kit/geometry.d.ts +22 -0
  60. package/src/runtime/kit/geometry.js +46 -0
  61. package/src/runtime/kit/index.d.ts +95 -0
  62. package/src/runtime/kit/index.js +149 -0
  63. package/src/runtime/kit/mark-strokes.d.ts +65 -0
  64. package/src/runtime/kit/mark-strokes.js +226 -0
  65. package/src/runtime/kit/marks.d.ts +21 -0
  66. package/src/runtime/kit/marks.js +165 -0
  67. package/src/runtime/kit/math.d.ts +23 -0
  68. package/src/runtime/kit/math.js +35 -0
  69. package/src/runtime/kit/paper.d.ts +7 -0
  70. package/src/runtime/kit/paper.js +27 -0
  71. package/src/runtime/kit/random.d.ts +8 -0
  72. package/src/runtime/kit/random.js +32 -0
  73. package/src/runtime/kit/text-fx.d.ts +18 -0
  74. package/src/runtime/kit/text-fx.js +55 -0
  75. package/src/runtime/kit/typing.d.ts +21 -0
  76. package/src/runtime/kit/typing.js +42 -0
  77. package/src/runtime/layers/describe.d.ts +41 -0
  78. package/src/runtime/layers/describe.js +327 -0
  79. package/src/runtime/layers/item.d.ts +121 -0
  80. package/src/runtime/layers/item.js +149 -0
  81. package/src/runtime/layers/parts.d.ts +50 -0
  82. package/src/runtime/layers/parts.js +155 -0
  83. package/src/runtime/layers/rows.d.ts +4 -0
  84. package/src/runtime/layers/rows.js +233 -0
  85. package/src/runtime/layers/scan.d.ts +24 -0
  86. package/src/runtime/layers/scan.js +202 -0
  87. package/src/runtime/layers/walk.d.ts +16 -0
  88. package/src/runtime/layers/walk.js +333 -0
  89. package/src/runtime/load.d.ts +10 -0
  90. package/src/runtime/load.js +228 -0
  91. package/src/runtime/main.d.ts +1 -0
  92. package/src/runtime/main.js +69 -0
  93. package/src/runtime/modules.d.ts +23 -0
  94. package/src/runtime/modules.js +53 -0
  95. package/src/runtime/port.d.ts +8 -0
  96. package/src/runtime/port.js +46 -0
  97. package/src/runtime/questions.d.ts +24 -0
  98. package/src/runtime/questions.js +162 -0
  99. package/src/runtime/recut.d.ts +35 -0
  100. package/src/runtime/recut.js +158 -0
  101. package/src/runtime/render.d.ts +80 -0
  102. package/src/runtime/render.js +342 -0
  103. package/src/runtime/retouch/ask.d.ts +17 -0
  104. package/src/runtime/retouch/ask.js +156 -0
  105. package/src/runtime/retouch/entry.d.ts +77 -0
  106. package/src/runtime/retouch/entry.js +1 -0
  107. package/src/runtime/retouch/judge.d.ts +25 -0
  108. package/src/runtime/retouch/judge.js +209 -0
  109. package/src/runtime/retouch/look.d.ts +39 -0
  110. package/src/runtime/retouch/look.js +135 -0
  111. package/src/runtime/retouch/parts.d.ts +22 -0
  112. package/src/runtime/retouch/parts.js +166 -0
  113. package/src/runtime/retouch/retouch.d.ts +37 -0
  114. package/src/runtime/retouch/retouch.js +238 -0
  115. package/src/runtime/retouch/seat.d.ts +40 -0
  116. package/src/runtime/retouch/seat.js +228 -0
  117. package/src/runtime/retouch/state.d.ts +37 -0
  118. package/src/runtime/retouch/state.js +153 -0
  119. package/src/runtime/retouch/type-style.d.ts +19 -0
  120. package/src/runtime/retouch/type-style.js +111 -0
  121. package/src/runtime/scan.d.ts +2 -0
  122. package/src/runtime/scan.js +49 -0
  123. package/src/runtime/scenes/card.d.ts +12 -0
  124. package/src/runtime/scenes/card.js +56 -0
  125. package/src/runtime/scenes/install.d.ts +47 -0
  126. package/src/runtime/scenes/install.js +387 -0
  127. package/src/runtime/scenes/read.d.ts +15 -0
  128. package/src/runtime/scenes/read.js +57 -0
  129. package/src/runtime/scenes/scoped.d.ts +13 -0
  130. package/src/runtime/scenes/scoped.js +40 -0
  131. package/src/runtime/score.d.ts +26 -0
  132. package/src/runtime/score.js +221 -0
  133. package/src/runtime/sketch/pen.d.ts +3 -0
  134. package/src/runtime/sketch/pen.js +371 -0
  135. package/src/runtime/sketch/strokes.d.ts +41 -0
  136. package/src/runtime/sketch/strokes.js +187 -0
  137. package/src/runtime/snapshot/draw.d.ts +14 -0
  138. package/src/runtime/snapshot/draw.js +125 -0
  139. package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
  140. package/src/runtime/snapshot/hold-first-draw.js +128 -0
  141. package/src/runtime/snapshot/plate.d.ts +20 -0
  142. package/src/runtime/snapshot/plate.js +79 -0
  143. package/src/runtime/snapshot/serialize.d.ts +49 -0
  144. package/src/runtime/snapshot/serialize.js +271 -0
  145. package/src/runtime/snapshot/styles.d.ts +8 -0
  146. package/src/runtime/snapshot/styles.js +109 -0
  147. package/src/runtime/snapshot/xml.d.ts +6 -0
  148. package/src/runtime/snapshot/xml.js +110 -0
  149. package/src/runtime/stage-size.d.ts +7 -0
  150. package/src/runtime/stage-size.js +3 -0
  151. package/src/runtime/stage.d.ts +27 -0
  152. package/src/runtime/stage.js +154 -0
  153. package/src/runtime/stretch.d.ts +13 -0
  154. package/src/runtime/stretch.js +16 -0
  155. package/src/runtime/texts.d.ts +24 -0
  156. package/src/runtime/texts.js +210 -0
  157. package/src/runtime/video-sync.d.ts +26 -0
  158. package/src/runtime/video-sync.js +126 -0
  159. package/src/stills.d.ts +41 -0
  160. package/src/stills.js +253 -0
  161. package/src/tools/film.d.ts +13 -0
  162. package/src/tools/film.js +116 -0
  163. package/src/tools/main.d.ts +1 -0
  164. package/src/tools/main.js +216 -0
  165. package/src/tools/score.d.ts +4 -0
  166. package/src/tools/score.js +29 -0
  167. package/src/tools/types.d.ts +69 -0
  168. package/src/tools/types.js +2 -0
  169. package/src/tools.d.ts +5 -0
  170. package/src/tools.js +3 -0
  171. package/src/virtual.d.ts +11 -0
  172. package/src/vite.d.ts +8 -0
  173. package/src/vite.js +43 -0
@@ -0,0 +1,238 @@
1
+ import { inOwnColors } from "../snapshot/serialize.js";
2
+ import { stageParts } from "../stage.js";
3
+ import { ART } from "../layers/parts.js";
4
+ import { boxes, judge, marksOf } from "./judge.js";
5
+ import { partsOf } from "./parts.js";
6
+ import { find, kinOf, lineOf, peers, printOf, seat, wordsOf } from "./seat.js";
7
+ import { LAID, LAPSED, MARK, basis, beaten, entriesOf, home, rulesText, state, takes, toldAt, unmark } from "./state.js";
8
+ /** The moments spread evenly over a stretch at which what the film itself changes is looked for. */
9
+ const LOOKS = 10;
10
+ /** A verdict rests on the entry's basis and the film, so it is kept for the runner's life (bounded): a drag, or a record put back, is not told anew. */
11
+ let known = new Map();
12
+ export const stageOf = () => stageParts()?.stage ?? null;
13
+ /** Takes the record; the caller draws the frame again. */
14
+ export function setRecord(record) {
15
+ unmark();
16
+ for (const e of state.list)
17
+ known.set(basis(e), e.told);
18
+ if (known.size > 512) {
19
+ known = new Map();
20
+ for (const e of state.list)
21
+ known.set(basis(e), e.told);
22
+ }
23
+ state.list = record ? entriesOf(record) : [];
24
+ for (const e of state.list) {
25
+ const was = known.get(basis(e));
26
+ if (was)
27
+ e.told = was;
28
+ }
29
+ state.sheet ??= document.createElement('style');
30
+ state.sheet.textContent = rulesText(state.list);
31
+ }
32
+ export const anyRetouch = () => state.list.length > 0 || state.marked.length > 0;
33
+ export const isDue = (scene, lo) => state.list.some((e) => e.scene === scene && !e.told.has(lo));
34
+ /**
35
+ * Reaches the verdicts on the entries of a stretch (lo to hi) not told yet, with no mark on the stage: at each
36
+ * entry's own moment, where its thing must stand, then at moments spread over the stretch, since what the film
37
+ * itself changes in its course is the film's. `draw` draws a second of the stretch; `far` one anywhere in the film,
38
+ * for an entry of another stretch.
39
+ */
40
+ export function tell(scene, lo, hi, draw, far) {
41
+ const stage = stageOf();
42
+ // A page that is not displayed computes no styles, which would read as another film: a later frame will tell.
43
+ if (!stage || !getComputedStyle(stage).display)
44
+ return;
45
+ unmark();
46
+ const due = state.list.filter((e) => e.scene === scene && !e.told.has(lo)).sort((p, q) => p.at - q.at);
47
+ const seats = due.map(() => null);
48
+ let last = null;
49
+ let up = false;
50
+ let stop = false;
51
+ let memo = {};
52
+ const drawAt = (how, s) => {
53
+ last = s;
54
+ const drawn = how(s);
55
+ stop = drawn === null;
56
+ up = drawn !== false && !stop;
57
+ memo = {};
58
+ };
59
+ const got = due.map((e, n) => {
60
+ if (!home(e, lo, hi))
61
+ return 'unmet';
62
+ if (e.at !== last && !stop)
63
+ drawAt(draw, e.at);
64
+ if (!up)
65
+ return 'undrawn';
66
+ return inOwnColors(() => {
67
+ const found = seat(stage, e, memo, true);
68
+ seats[n] = found;
69
+ return found ? (judge(stage, found.el, e, null, true) ?? 'absent') : 'absent';
70
+ });
71
+ });
72
+ /*
73
+ * A part of an svg (but an svg text with no hide) whose own moment lies in another stretch: its print tells its
74
+ * turn in the drawing, which the film may change by chapter, so it is seated at its own moment, by `far`.
75
+ */
76
+ const afar = due.map((e) => !stop && !home(e, lo, hi) && ART.test(e.tag) && (e.tag !== 'text' || !!e.pairs.hidden));
77
+ const before = stop;
78
+ last = null;
79
+ due.forEach((e, n) => {
80
+ if (!afar[n])
81
+ return;
82
+ if (e.at !== last && !stop)
83
+ drawAt(far, e.at);
84
+ if (!up)
85
+ return;
86
+ const found = seat(stage, e, memo, true);
87
+ if (!found)
88
+ got[n] = 'absent';
89
+ // A part with words is found here by its words, as html lines are.
90
+ else
91
+ seats[n] = found.worded ? null : found;
92
+ });
93
+ if (stop)
94
+ afar.forEach((a, n) => a && (got[n] = 'undrawn'));
95
+ stop = before;
96
+ const look = (e, n) => {
97
+ const so = got[n];
98
+ if (so === 'absent' || so === 'undrawn' || so === undefined)
99
+ return;
100
+ const at = seats[n] ?? seat(stage, e, memo, false);
101
+ seats[n] = at;
102
+ if (!at)
103
+ return;
104
+ const el = find(stage, e, at, memo);
105
+ if (el && (at.says === undefined || printOf(stage, el, memo) === at.says))
106
+ got[n] = judge(stage, el, e, so === 'unmet' ? null : so, false) ?? so;
107
+ };
108
+ const looks = (some) => {
109
+ for (let i = 0; i < LOOKS && !stop; i++) {
110
+ const drawn = draw(lo + ((hi - lo) * i) / (LOOKS - 1));
111
+ stop = drawn === null;
112
+ memo = {};
113
+ if (drawn !== false && !stop)
114
+ inOwnColors(() => due.forEach((e, n) => some(n) && look(e, n)));
115
+ }
116
+ };
117
+ looks(() => true);
118
+ /*
119
+ * An entry of another stretch no look found by its print. If `far`, at its own moment, finds it by its turn (it was
120
+ * reworded, or the print is on several), that node is its node here while on the stage, and a look counts only while
121
+ * the node has the words it had then.
122
+ */
123
+ const late = due.map((e, n) => !stop && !!e.print && !home(e, lo, hi) && got[n] === 'unmet' && !seats[n]);
124
+ const whole = stop;
125
+ if (late.includes(true)) {
126
+ last = null;
127
+ due.forEach((e, n) => {
128
+ if (!late[n])
129
+ return;
130
+ if (e.at !== last && !stop)
131
+ drawAt(far, e.at);
132
+ const found = up ? seat(stage, e, memo, true) : null;
133
+ if (found && !found.sure)
134
+ seats[n] = { el: found.el, sure: false, worded: found.worded, k: 0, n: -1, print: null, says: printOf(stage, found.el, memo) };
135
+ });
136
+ if (late.some((l, n) => l && seats[n]))
137
+ looks((n) => !!late[n] && !!seats[n]);
138
+ if (stop)
139
+ late.forEach((l, n) => l && (got[n] = 'undrawn'));
140
+ stop = whole;
141
+ }
142
+ due.forEach((e, n) => {
143
+ const verdict = got[n];
144
+ const at = seats[n];
145
+ // A hide does not count on a thing with words found by its turn: it could be another thing, and nobody would see why that is gone.
146
+ if (typeof verdict === 'object' && e.pairs.hidden && e.print && at && !at.sure && at.worded)
147
+ verdict.holds.hidden = false;
148
+ e.told.set(lo, stop ? null : typeof verdict === 'object' && at ? { verdict, seat: at, laid: marksOf(e, verdict, true), lapsed: marksOf(e, verdict, false) } : verdict === 'undrawn' ? null : false);
149
+ });
150
+ }
151
+ /** Marks what is laid on the stretch just drawn, which starts at lo. scene null: nothing is laid (a rough scene is a sketch). */
152
+ export function apply(scene, lo) {
153
+ if (state.marked.length)
154
+ unmark();
155
+ const stage = stageOf();
156
+ if (!state.list.length || scene === null || !stage || !state.sheet)
157
+ return;
158
+ if (state.sheet.parentNode !== stage)
159
+ stage.appendChild(state.sheet);
160
+ const out = beaten(scene, lo);
161
+ const memo = {};
162
+ const on = new Map();
163
+ state.list.forEach((e, i) => {
164
+ const told = e.scene === scene ? toldAt(e, lo) : null;
165
+ const el = told && !out.includes(e) ? find(stage, e, told.seat, memo) : null;
166
+ if (!told || !el)
167
+ return;
168
+ // In a film that makes its nodes anew two entries seated on two nodes may meet on one.
169
+ const w = on.get(el);
170
+ if (w && !takes(e, w, lo))
171
+ return;
172
+ el.setAttribute(MARK, String(i));
173
+ el.setAttribute(LAID, told.laid);
174
+ if (told.lapsed)
175
+ el.setAttribute(LAPSED, told.lapsed);
176
+ else
177
+ el.removeAttribute(LAPSED);
178
+ if (!w)
179
+ state.marked.push(el);
180
+ on.set(el, e);
181
+ });
182
+ }
183
+ /** The entries whose own moment lies in the stretch and whose thing could not be told then, or is another entry's. */
184
+ export function lostIn(scene, lo, hi) {
185
+ const out = beaten(scene, lo);
186
+ return state.list.filter((e) => e.scene === scene && home(e, lo, hi) && (e.told.get(lo) === false || out.includes(e))).map((e) => e.id);
187
+ }
188
+ /**
189
+ * Of those, the hides that would count on their thing, where another entry's hide is laid: the thing is hidden all the
190
+ * same, so the page says nothing of them.
191
+ */
192
+ export function underHideIn(scene, lo, hi) {
193
+ const by = new Map();
194
+ for (const e of state.list) {
195
+ const told = e.scene === scene ? toldAt(e, lo) : null;
196
+ if (!told)
197
+ continue;
198
+ const w = by.get(told.seat.el);
199
+ if (!w || takes(e, w, lo))
200
+ by.set(told.seat.el, e);
201
+ }
202
+ return beaten(scene, lo)
203
+ .filter((e) => {
204
+ const told = toldAt(e, lo);
205
+ const w = told ? by.get(told.seat.el) : undefined;
206
+ return !!told && home(e, lo, hi) && told.verdict.holds.hidden === true && w !== undefined && w !== e && toldAt(w, lo)?.verdict.holds.hidden === true;
207
+ })
208
+ .map((e) => e.id);
209
+ }
210
+ /** Told of each scene's box as it comes up: notes the classes in it, so a class counts as an element's own alike in every runner. */
211
+ export function noteBox(node) {
212
+ const names = new Set();
213
+ for (const el of Array.from(node.getElementsByTagName('*')))
214
+ for (const name of Array.from(el.classList))
215
+ names.add(name);
216
+ boxes.push({ node, names });
217
+ }
218
+ /** The kin of an element held to the classes `cls`, on the stage as it is now. */
219
+ export function kinFor(el, cls, memo) {
220
+ const stage = stageOf();
221
+ if (!stage)
222
+ return null;
223
+ return kinOf(stage, peers(stage, memo, el.tagName.toLowerCase()), el, cls.split(' ').filter(Boolean), memo);
224
+ }
225
+ /** The parts a hide on this g inside an svg would also be found by, on the stage as it is now; null: none would hold. */
226
+ export function partsFor(el, memo) {
227
+ const stage = stageOf();
228
+ if (!stage)
229
+ return null;
230
+ // Lists the stage's svg parts, which the hashes are counted against.
231
+ peers(stage, memo, 'g');
232
+ return wordsOf(el, memo) ? null : partsOf(el, memo);
233
+ }
234
+ /** The line of words a text node of the stage is part of. */
235
+ export function lineFor(node, memo) {
236
+ const stage = stageOf();
237
+ return stage ? inOwnColors(() => lineOf(stage, node, memo)) : null;
238
+ }
@@ -0,0 +1,40 @@
1
+ import type { Entry, Peer, ReadMemo, Seat } from './entry.ts';
2
+ /** What each element met on the way to a line of words is known as, for one report. */
3
+ export interface LineMemo {
4
+ k: number;
5
+ words: boolean | null;
6
+ places: Map<Element, number> | null;
7
+ }
8
+ /**
9
+ * The line of words a text node is part of, as "place/tag": which child of which, from the stage down, then the tag.
10
+ * null: there is none, or the way leads into a picture, the cursor or the fx canvas, each of which is one thing. An
11
+ * inline svg is entered only through g parts, down to a text.
12
+ */
13
+ export declare function lineOf(stage: HTMLElement, node: Node, memo: Map<Element, LineMemo>): string | null;
14
+ export declare function fits(el: Element, tag: string, cls: readonly string[]): boolean;
15
+ export declare function standsAt(stage: HTMLElement, path: readonly number[]): Element | null;
16
+ /** An element's words, a sheet's or a script's text left out. */
17
+ export declare function wordsOf(el: Element, memo: ReadMemo): string;
18
+ /**
19
+ * A thing's print: a short hash (FNV-1a) of its words; with none, of "|" and the words of the nearest element round
20
+ * it that has some; for a part of an svg with none, of "#", where it stands in the drawing and how it is drawn.
21
+ */
22
+ export declare function printOf(stage: HTMLElement, el: Element, memo: ReadMemo): string;
23
+ /** The things of a tag as they are now, their classes as values: the stage may be drawn anew before they are read. */
24
+ export declare function peers(stage: HTMLElement, memo: ReadMemo, tag: string): Peer[];
25
+ /** Those of them that have every class of `cls`, with their prints, made now unless they were before. */
26
+ export declare function like(stage: HTMLElement, all: Peer[], cls: readonly string[], memo: ReadMemo): Peer[];
27
+ /** An entry's kin: which of how many like things the element is, then its print if no other of them has it. null: it is not one of them. */
28
+ export declare function kinOf(stage: HTMLElement, all: Peer[], el: Element, cls: readonly string[], memo: ReadMemo): string | null;
29
+ /**
30
+ * An entry's thing on the stage as drawn: the one like thing with its print; else, among as many as there were, the
31
+ * one in its turn, unless its print is on others only; and a hidden g whose print is on nothing, by its parts. null:
32
+ * it cannot be told. `own`: at the entry's own moment; in another stretch one that has a print is found by that alone.
33
+ */
34
+ export declare function seat(stage: HTMLElement, e: Entry, memo: ReadMemo, own: boolean): Seat | null;
35
+ /**
36
+ * The element an entry is laid on now: the node it was found on, while that is on the stage. Once the node has left,
37
+ * the like thing in the same turn while there are as many; else the one thing of its tag with the print it had then.
38
+ * What is found by its print alone: the one like part with the entry's print, or the print it had when its parts found it.
39
+ */
40
+ export declare function find(stage: HTMLElement, e: Entry, s: Seat, memo: ReadMemo): Element | null;
@@ -0,0 +1,228 @@
1
+ import { inOwnColors } from "../snapshot/serialize.js";
2
+ import { ART, SKIP, UNIT, drawnAs, isArtWords, isPart, isWords, kidsOf, saidOf } from "../layers/parts.js";
3
+ import { holds } from "./parts.js";
4
+ /*
5
+ * How an entry of the record finds its thing again on a stage that may have been drawn anew: by its tag and classes,
6
+ * its turn among like things, and its print, a short hash of its words.
7
+ */
8
+ const SVG_NS = 'http://www.w3.org/2000/svg';
9
+ /**
10
+ * The line of words a text node is part of, as "place/tag": which child of which, from the stage down, then the tag.
11
+ * null: there is none, or the way leads into a picture, the cursor or the fx canvas, each of which is one thing. An
12
+ * inline svg is entered only through g parts, down to a text.
13
+ */
14
+ export function lineOf(stage, node, memo) {
15
+ const way = [];
16
+ let el = node.parentElement;
17
+ for (; el && el !== stage; el = el.parentElement)
18
+ way.unshift(el);
19
+ if (!el)
20
+ return null;
21
+ const of = (x) => {
22
+ let known = memo.get(x);
23
+ if (!known)
24
+ memo.set(x, (known = { k: -1, words: null, places: null }));
25
+ return known;
26
+ };
27
+ const path = [];
28
+ let up = stage;
29
+ for (const step of way) {
30
+ const above = of(up);
31
+ const known = of(step);
32
+ if (!above.places) {
33
+ const places = new Map();
34
+ kidsOf(up).forEach((child, k) => places.set(child, k));
35
+ above.places = places;
36
+ }
37
+ if (known.words === null) {
38
+ const k = above.places.get(step);
39
+ const art = step.namespaceURI === SVG_NS;
40
+ const shut = art && up.namespaceURI === step.namespaceURI ? !(isPart(step) && /^(g|text)$/i.test(step.tagName)) : !art && UNIT.test(step.tagName);
41
+ known.k = k === undefined || (step.id === 'fx' && up === stage) || shut || step.classList.contains('film-cursor') ? -1 : k;
42
+ known.words = known.k >= 0 && (!art || isArtWords(step)) && isWords(step);
43
+ }
44
+ if (known.k < 0)
45
+ return null;
46
+ path.push(known.k);
47
+ if (known.words)
48
+ return path.join('.') + '/' + step.tagName.toLowerCase();
49
+ up = step;
50
+ }
51
+ return null;
52
+ }
53
+ export function fits(el, tag, cls) {
54
+ return el.tagName.toLowerCase() === tag && cls.every((c) => el.classList.contains(c));
55
+ }
56
+ export function standsAt(stage, path) {
57
+ let el = stage;
58
+ for (let k = 0; el && k < path.length; k++)
59
+ el = kidsOf(el)[path[k]];
60
+ return el ?? null;
61
+ }
62
+ /** The things on the stage as drawn, shown or not, in document order: not the fx canvas, nor what is inside a picture, the cursor or a line of words. */
63
+ function things(stage) {
64
+ const out = [];
65
+ const walk = (up) => {
66
+ for (const el of kidsOf(up)) {
67
+ if (el.id === 'fx' && up === stage)
68
+ continue;
69
+ out.push(el);
70
+ if (!UNIT.test(el.tagName) && !el.classList.contains('film-cursor') && !isWords(el))
71
+ walk(el);
72
+ }
73
+ };
74
+ inOwnColors(() => walk(stage));
75
+ return out;
76
+ }
77
+ /** The parts inside the svgs among `all`, in document order, listed apart so every other thing counts as if the svgs were closed. */
78
+ function partsIn(all) {
79
+ const out = [];
80
+ const walk = (up) => {
81
+ for (const el of kidsOf(up).filter(isPart)) {
82
+ out.push(el);
83
+ if (/^g$/i.test(el.tagName))
84
+ walk(el);
85
+ }
86
+ };
87
+ all.filter((el) => /^svg$/i.test(el.tagName)).forEach(walk);
88
+ return out;
89
+ }
90
+ /** Where a part stands in its drawing: down from the svg, its turn among the parts of its tag beside it, their number, and what its own attributes say. */
91
+ function standing(el, memo) {
92
+ const turns = (memo.turns ??= new Map());
93
+ const parent = el.parentElement;
94
+ if (!parent)
95
+ return '';
96
+ if (!turns.has(el)) {
97
+ const all = kidsOf(parent).filter(isPart);
98
+ const n = new Map();
99
+ const k = new Map();
100
+ for (const c of all)
101
+ n.set(c.tagName, (n.get(c.tagName) ?? 0) + 1);
102
+ for (const c of all) {
103
+ k.set(c.tagName, (k.get(c.tagName) ?? 0) + 1);
104
+ turns.set(c, k.get(c.tagName) + '/' + n.get(c.tagName) + ' ' + saidOf(c));
105
+ }
106
+ }
107
+ return (isPart(parent) ? standing(parent, memo) : saidOf(parent)) + ' > ' + (turns.get(el) ?? '');
108
+ }
109
+ /** An element's words, a sheet's or a script's text left out. */
110
+ export function wordsOf(el, memo) {
111
+ const words = (memo.words ??= new Map());
112
+ const known = words.get(el);
113
+ if (known !== undefined)
114
+ return known;
115
+ let s = '';
116
+ for (let n = el.firstChild; n; n = n.nextSibling)
117
+ s += n instanceof Text ? n.data : n instanceof Element && !SKIP.test(n.tagName) ? ' ' + wordsOf(n, memo) + ' ' : '';
118
+ const out = s.replace(/\s+/g, ' ').trim();
119
+ words.set(el, out);
120
+ return out;
121
+ }
122
+ /**
123
+ * A thing's print: a short hash (FNV-1a) of its words; with none, of "|" and the words of the nearest element round
124
+ * it that has some; for a part of an svg with none, of "#", where it stands in the drawing and how it is drawn.
125
+ */
126
+ export function printOf(stage, el, memo) {
127
+ let s = wordsOf(el, memo);
128
+ if (!s && isPart(el))
129
+ s = '#' + standing(el, memo) + ' ' + drawnAs(el, false, (memo.drawn ??= new Map()));
130
+ else if (!s) {
131
+ for (let up = el.parentElement; up && up !== stage && !s; up = up.parentElement)
132
+ s = wordsOf(up, memo);
133
+ s = '|' + s;
134
+ }
135
+ let h = 0x811c9dc5;
136
+ for (let i = 0; i < s.length; i++)
137
+ h = Math.imul(h ^ s.charCodeAt(i), 16777619);
138
+ return (h >>> 0).toString(36);
139
+ }
140
+ /** The things of a tag as they are now, their classes as values: the stage may be drawn anew before they are read. */
141
+ export function peers(stage, memo, tag) {
142
+ const all = (memo.all ??= things(stage));
143
+ const tags = (memo.tags ??= new Map());
144
+ const known = tags.get(tag);
145
+ if (known)
146
+ return known;
147
+ const pool = ART.test(tag) ? (memo.art ??= all.concat(partsIn(all))) : all;
148
+ const out = pool.filter((el) => el.tagName.toLowerCase() === tag).map((el) => ({ el, cls: Array.from(el.classList), print: null }));
149
+ tags.set(tag, out);
150
+ return out;
151
+ }
152
+ /** Those of them that have every class of `cls`, with their prints, made now unless they were before. */
153
+ export function like(stage, all, cls, memo) {
154
+ return all
155
+ .filter((x) => cls.every((c) => x.cls.includes(c)))
156
+ .map((x) => {
157
+ if (x.print === null)
158
+ x.print = printOf(stage, x.el, memo);
159
+ return x;
160
+ });
161
+ }
162
+ /** An entry's kin: which of how many like things the element is, then its print if no other of them has it. null: it is not one of them. */
163
+ export function kinOf(stage, all, el, cls, memo) {
164
+ const l = like(stage, all, cls, memo);
165
+ const k = l.findIndex((x) => x.el === el);
166
+ if (k < 0)
167
+ return null;
168
+ return k + '/' + l.length + (l.some((x, i) => i !== k && x.print === l[k].print) ? '' : '/' + l[k].print);
169
+ }
170
+ /** A part of an svg is found by its print alone, but for an svg text that the entry does not hide. */
171
+ const byPrintAlone = (e) => (x) => isPart(x.el) && (!!e.pairs.hidden || !isArtWords(x.el));
172
+ /**
173
+ * The hidden g of a scene that its entry's print no longer finds, after a change inside it: only where one g had the
174
+ * entry's classes when it was saved and one has them now, with no other class and no words, and it still holds the
175
+ * entry's parts.
176
+ */
177
+ function byParts(e, l, memo) {
178
+ const only = l.length === 1 ? l[0] : null;
179
+ if (!only || !e.pairs.hidden || !e.print || !e.parts || e.scene === 'film' || e.n !== 1)
180
+ return null;
181
+ if (!/^g$/i.test(only.el.tagName) || !only.cls.every((c) => e.cls.includes(c)) || wordsOf(only.el, memo))
182
+ return null;
183
+ return holds(only.el, e.parts, memo) ? only : null;
184
+ }
185
+ /**
186
+ * An entry's thing on the stage as drawn: the one like thing with its print; else, among as many as there were, the
187
+ * one in its turn, unless its print is on others only; and a hidden g whose print is on nothing, by its parts. null:
188
+ * it cannot be told. `own`: at the entry's own moment; in another stretch one that has a print is found by that alone.
189
+ */
190
+ export function seat(stage, e, memo, own) {
191
+ const l = like(stage, peers(stage, memo, e.tag), e.cls, memo);
192
+ const said = e.print ? l.filter((x) => x.print === e.print) : [];
193
+ const inTurn = l[e.k];
194
+ const found = said.length === 1 ? said[0] : !l.some(byPrintAlone(e)) && (own || !e.print) && l.length === e.n && inTurn && (!said.length || said.includes(inTurn)) ? inTurn : null;
195
+ const held = found ? null : byParts(e, l, memo);
196
+ const it = found ?? held;
197
+ if (!it)
198
+ return null;
199
+ const shared = like(stage, peers(stage, memo, e.tag), [], memo).some((x) => x !== it && x.print === it.print);
200
+ return {
201
+ el: it.el,
202
+ sure: said.length === 1,
203
+ ...(held ? { held: true, had: it.print } : {}),
204
+ worded: !!wordsOf(it.el, memo),
205
+ k: l.indexOf(it),
206
+ n: l.length,
207
+ print: shared ? null : it.print,
208
+ };
209
+ }
210
+ /**
211
+ * The element an entry is laid on now: the node it was found on, while that is on the stage. Once the node has left,
212
+ * the like thing in the same turn while there are as many; else the one thing of its tag with the print it had then.
213
+ * What is found by its print alone: the one like part with the entry's print, or the print it had when its parts found it.
214
+ */
215
+ export function find(stage, e, s, memo) {
216
+ if (stage.contains(s.el))
217
+ return s.el;
218
+ const all = peers(stage, memo, e.tag);
219
+ const l = like(stage, all, e.cls, memo);
220
+ const print = s.held ? s.had : e.print;
221
+ const mine = (e.pairs.hidden || e.pairs.svgfill || e.pairs.svgstroke) && e.print ? l.filter((x) => x.print === print) : [];
222
+ if (mine.length || l.some(byPrintAlone(e)))
223
+ return mine.length === 1 ? mine[0].el : null;
224
+ if (l.length === s.n)
225
+ return l[s.k]?.el ?? null;
226
+ const said = s.print === null ? [] : like(stage, all, [], memo).filter((x) => x.print === s.print);
227
+ return said.length === 1 ? said[0].el : null;
228
+ }
@@ -0,0 +1,37 @@
1
+ import type { RetouchedRecord } from '@open-artifact/motion-core';
2
+ import type { Entry, Told } from './entry.ts';
3
+ /** Which entry marks the element. */
4
+ export declare const MARK = "data-film-over";
5
+ /** What of it is laid on the element: its keys; for the radius, the corners that take it; for an svg text's ink, "paint". */
6
+ export declare const LAID = "data-film-laid";
7
+ /** What of it no longer counts. */
8
+ export declare const LAPSED = "data-film-lapsed";
9
+ export declare const ID: RegExp;
10
+ export declare const state: {
11
+ list: Entry[];
12
+ /** The elements this stage marked at its last pass: a mark a film's copy of a node brings along does not count. */
13
+ marked: Element[];
14
+ sheet: HTMLStyleElement | null;
15
+ };
16
+ /** The record's entries as this stage lays them; the record is parsed again, since this is the trust boundary for the sheet. */
17
+ export declare function entriesOf(record: RetouchedRecord): Entry[];
18
+ /** The sheet's text. Of a pair only `v` reaches it, a colour, a number or a word out of a closed set; no selector is made of anything in the record. */
19
+ export declare function rulesText(list: readonly Entry[]): string;
20
+ export declare function unmark(): void;
21
+ /** The entry that marks an element. */
22
+ export declare function marks(el: Element): Entry | undefined;
23
+ export declare const lapsedOn: (el: Element) => string[];
24
+ export declare function toldAt(e: Entry, lo: number): Told | null;
25
+ /** Of an entry of the scene nothing could be told in this load: which element is its own is not known, so no look is told of any. */
26
+ export declare const blind: (scene: string, lo: number) => boolean;
27
+ /** An entry's own moment lies in the stretch: a scene's always does, since it counts from the scene's start. */
28
+ export declare const home: (e: Entry, lo: number, hi: number) => boolean;
29
+ /**
30
+ * One found otherwise keeps an element from one found by its parts, and one found by its print from one found by its
31
+ * turn; else the later in the record, changed last, takes it.
32
+ */
33
+ export declare function takes(e: Entry, w: Entry, lo: number): boolean;
34
+ /** Of entries seated on one node one is laid; the others, which apply passes over and lost names. */
35
+ export declare function beaten(scene: string, lo: number): Entry[];
36
+ /** All of an entry that a verdict on it rests on: not `v`. */
37
+ export declare function basis(e: Entry): string;