@openartifactsai/motion-stage 0.0.0-stage → 0.23.0-rc.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 (225) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +70 -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 +157 -0
  21. package/src/protocol/clip-guards.d.ts +5 -0
  22. package/src/protocol/clip-guards.js +22 -0
  23. package/src/protocol/guards.d.ts +6 -0
  24. package/src/protocol/guards.js +257 -0
  25. package/src/protocol/layer-guards.d.ts +7 -0
  26. package/src/protocol/layer-guards.js +100 -0
  27. package/src/protocol/layers.d.ts +135 -0
  28. package/src/protocol/layers.js +1 -0
  29. package/src/protocol/types.d.ts +340 -0
  30. package/src/protocol/types.js +7 -0
  31. package/src/protocol.d.ts +2 -0
  32. package/src/protocol.js +2 -0
  33. package/src/raster.d.ts +4 -0
  34. package/src/raster.js +2 -0
  35. package/src/runtime/assets.d.ts +19 -0
  36. package/src/runtime/assets.js +63 -0
  37. package/src/runtime/caption-box.d.ts +3 -0
  38. package/src/runtime/caption-box.js +16 -0
  39. package/src/runtime/captions.d.ts +31 -0
  40. package/src/runtime/captions.js +115 -0
  41. package/src/runtime/clip-captions.d.ts +9 -0
  42. package/src/runtime/clip-captions.js +27 -0
  43. package/src/runtime/clips.d.ts +83 -0
  44. package/src/runtime/clips.js +259 -0
  45. package/src/runtime/console.d.ts +13 -0
  46. package/src/runtime/console.js +99 -0
  47. package/src/runtime/cut.d.ts +7 -0
  48. package/src/runtime/cut.js +63 -0
  49. package/src/runtime/determinism.d.ts +12 -0
  50. package/src/runtime/determinism.js +47 -0
  51. package/src/runtime/film.d.ts +43 -0
  52. package/src/runtime/film.js +31 -0
  53. package/src/runtime/focus.d.ts +8 -0
  54. package/src/runtime/focus.js +71 -0
  55. package/src/runtime/fonts.d.ts +22 -0
  56. package/src/runtime/fonts.js +109 -0
  57. package/src/runtime/frame.d.ts +9 -0
  58. package/src/runtime/frame.js +140 -0
  59. package/src/runtime/fx/backdrop.d.ts +60 -0
  60. package/src/runtime/fx/backdrop.js +264 -0
  61. package/src/runtime/fx/color.d.ts +19 -0
  62. package/src/runtime/fx/color.js +84 -0
  63. package/src/runtime/fx/css-color.d.ts +6 -0
  64. package/src/runtime/fx/css-color.js +44 -0
  65. package/src/runtime/fx/curve.d.ts +12 -0
  66. package/src/runtime/fx/curve.js +34 -0
  67. package/src/runtime/fx/gl.d.ts +29 -0
  68. package/src/runtime/fx/gl.js +207 -0
  69. package/src/runtime/fx/glsl.d.ts +35 -0
  70. package/src/runtime/fx/glsl.js +113 -0
  71. package/src/runtime/fx/index.d.ts +23 -0
  72. package/src/runtime/fx/index.js +151 -0
  73. package/src/runtime/fx/key.d.ts +11 -0
  74. package/src/runtime/fx/key.js +21 -0
  75. package/src/runtime/fx/numbers.d.ts +14 -0
  76. package/src/runtime/fx/numbers.js +57 -0
  77. package/src/runtime/fx/pace.d.ts +17 -0
  78. package/src/runtime/fx/pace.js +26 -0
  79. package/src/runtime/fx/paint.d.ts +28 -0
  80. package/src/runtime/fx/paint.js +150 -0
  81. package/src/runtime/guard.d.ts +4 -0
  82. package/src/runtime/guard.js +141 -0
  83. package/src/runtime/hand-titles/index.d.ts +9 -0
  84. package/src/runtime/hand-titles/index.js +73 -0
  85. package/src/runtime/hand-titles/layer.d.ts +20 -0
  86. package/src/runtime/hand-titles/layer.js +118 -0
  87. package/src/runtime/hand-titles/place.d.ts +27 -0
  88. package/src/runtime/hand-titles/place.js +45 -0
  89. package/src/runtime/held-store.d.ts +35 -0
  90. package/src/runtime/held-store.js +63 -0
  91. package/src/runtime/held.d.ts +19 -0
  92. package/src/runtime/held.js +225 -0
  93. package/src/runtime/kit/camera.d.ts +59 -0
  94. package/src/runtime/kit/camera.js +81 -0
  95. package/src/runtime/kit/chip.d.ts +25 -0
  96. package/src/runtime/kit/chip.js +75 -0
  97. package/src/runtime/kit/count.d.ts +37 -0
  98. package/src/runtime/kit/count.js +100 -0
  99. package/src/runtime/kit/cursor.d.ts +46 -0
  100. package/src/runtime/kit/cursor.js +108 -0
  101. package/src/runtime/kit/dom.d.ts +14 -0
  102. package/src/runtime/kit/dom.js +46 -0
  103. package/src/runtime/kit/geometry.d.ts +24 -0
  104. package/src/runtime/kit/geometry.js +56 -0
  105. package/src/runtime/kit/index.d.ts +113 -0
  106. package/src/runtime/kit/index.js +157 -0
  107. package/src/runtime/kit/mark-strokes.d.ts +65 -0
  108. package/src/runtime/kit/mark-strokes.js +226 -0
  109. package/src/runtime/kit/marks.d.ts +21 -0
  110. package/src/runtime/kit/marks.js +165 -0
  111. package/src/runtime/kit/math.d.ts +23 -0
  112. package/src/runtime/kit/math.js +35 -0
  113. package/src/runtime/kit/paper.d.ts +7 -0
  114. package/src/runtime/kit/paper.js +27 -0
  115. package/src/runtime/kit/random.d.ts +8 -0
  116. package/src/runtime/kit/random.js +32 -0
  117. package/src/runtime/kit/text-fx.d.ts +18 -0
  118. package/src/runtime/kit/text-fx.js +55 -0
  119. package/src/runtime/kit/typing.d.ts +21 -0
  120. package/src/runtime/kit/typing.js +42 -0
  121. package/src/runtime/laid/layer.d.ts +44 -0
  122. package/src/runtime/laid/layer.js +370 -0
  123. package/src/runtime/layers/describe.d.ts +41 -0
  124. package/src/runtime/layers/describe.js +327 -0
  125. package/src/runtime/layers/item.d.ts +123 -0
  126. package/src/runtime/layers/item.js +150 -0
  127. package/src/runtime/layers/parts.d.ts +53 -0
  128. package/src/runtime/layers/parts.js +158 -0
  129. package/src/runtime/layers/rows.d.ts +4 -0
  130. package/src/runtime/layers/rows.js +233 -0
  131. package/src/runtime/layers/scan.d.ts +24 -0
  132. package/src/runtime/layers/scan.js +204 -0
  133. package/src/runtime/layers/walk.d.ts +16 -0
  134. package/src/runtime/layers/walk.js +337 -0
  135. package/src/runtime/load.d.ts +10 -0
  136. package/src/runtime/load.js +267 -0
  137. package/src/runtime/main.d.ts +1 -0
  138. package/src/runtime/main.js +90 -0
  139. package/src/runtime/modules.d.ts +23 -0
  140. package/src/runtime/modules.js +53 -0
  141. package/src/runtime/port.d.ts +8 -0
  142. package/src/runtime/port.js +46 -0
  143. package/src/runtime/questions.d.ts +24 -0
  144. package/src/runtime/questions.js +162 -0
  145. package/src/runtime/recut.d.ts +81 -0
  146. package/src/runtime/recut.js +325 -0
  147. package/src/runtime/render.d.ts +80 -0
  148. package/src/runtime/render.js +358 -0
  149. package/src/runtime/retouch/ask.d.ts +17 -0
  150. package/src/runtime/retouch/ask.js +171 -0
  151. package/src/runtime/retouch/entry.d.ts +77 -0
  152. package/src/runtime/retouch/entry.js +1 -0
  153. package/src/runtime/retouch/judge.d.ts +25 -0
  154. package/src/runtime/retouch/judge.js +209 -0
  155. package/src/runtime/retouch/look.d.ts +39 -0
  156. package/src/runtime/retouch/look.js +135 -0
  157. package/src/runtime/retouch/own-styles.d.ts +19 -0
  158. package/src/runtime/retouch/own-styles.js +141 -0
  159. package/src/runtime/retouch/parts.d.ts +22 -0
  160. package/src/runtime/retouch/parts.js +166 -0
  161. package/src/runtime/retouch/retouch.d.ts +37 -0
  162. package/src/runtime/retouch/retouch.js +238 -0
  163. package/src/runtime/retouch/seat.d.ts +40 -0
  164. package/src/runtime/retouch/seat.js +228 -0
  165. package/src/runtime/retouch/state.d.ts +37 -0
  166. package/src/runtime/retouch/state.js +153 -0
  167. package/src/runtime/retouch/type-style.d.ts +19 -0
  168. package/src/runtime/retouch/type-style.js +111 -0
  169. package/src/runtime/scan.d.ts +2 -0
  170. package/src/runtime/scan.js +49 -0
  171. package/src/runtime/scenes/card.d.ts +12 -0
  172. package/src/runtime/scenes/card.js +56 -0
  173. package/src/runtime/scenes/install.d.ts +69 -0
  174. package/src/runtime/scenes/install.js +528 -0
  175. package/src/runtime/scenes/read.d.ts +27 -0
  176. package/src/runtime/scenes/read.js +73 -0
  177. package/src/runtime/scenes/scoped.d.ts +13 -0
  178. package/src/runtime/scenes/scoped.js +46 -0
  179. package/src/runtime/score.d.ts +32 -0
  180. package/src/runtime/score.js +262 -0
  181. package/src/runtime/sketch/pen.d.ts +3 -0
  182. package/src/runtime/sketch/pen.js +371 -0
  183. package/src/runtime/sketch/strokes.d.ts +41 -0
  184. package/src/runtime/sketch/strokes.js +187 -0
  185. package/src/runtime/snapshot/draw.d.ts +14 -0
  186. package/src/runtime/snapshot/draw.js +125 -0
  187. package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
  188. package/src/runtime/snapshot/hold-first-draw.js +128 -0
  189. package/src/runtime/snapshot/lift.d.ts +41 -0
  190. package/src/runtime/snapshot/lift.js +161 -0
  191. package/src/runtime/snapshot/plate.d.ts +20 -0
  192. package/src/runtime/snapshot/plate.js +79 -0
  193. package/src/runtime/snapshot/serialize.d.ts +51 -0
  194. package/src/runtime/snapshot/serialize.js +286 -0
  195. package/src/runtime/snapshot/styles.d.ts +8 -0
  196. package/src/runtime/snapshot/styles.js +109 -0
  197. package/src/runtime/snapshot/xml.d.ts +6 -0
  198. package/src/runtime/snapshot/xml.js +110 -0
  199. package/src/runtime/stage-size.d.ts +7 -0
  200. package/src/runtime/stage-size.js +3 -0
  201. package/src/runtime/stage.d.ts +27 -0
  202. package/src/runtime/stage.js +154 -0
  203. package/src/runtime/stretch.d.ts +13 -0
  204. package/src/runtime/stretch.js +16 -0
  205. package/src/runtime/texts.d.ts +24 -0
  206. package/src/runtime/texts.js +211 -0
  207. package/src/runtime/video-sync.d.ts +51 -0
  208. package/src/runtime/video-sync.js +218 -0
  209. package/src/stills.d.ts +90 -0
  210. package/src/stills.js +329 -0
  211. package/src/tools/film.d.ts +13 -0
  212. package/src/tools/film.js +116 -0
  213. package/src/tools/levels.d.ts +8 -0
  214. package/src/tools/levels.js +28 -0
  215. package/src/tools/main.d.ts +1 -0
  216. package/src/tools/main.js +223 -0
  217. package/src/tools/score.d.ts +4 -0
  218. package/src/tools/score.js +35 -0
  219. package/src/tools/types.d.ts +84 -0
  220. package/src/tools/types.js +2 -0
  221. package/src/tools.d.ts +6 -0
  222. package/src/tools.js +4 -0
  223. package/src/virtual.d.ts +11 -0
  224. package/src/vite.d.ts +8 -0
  225. package/src/vite.js +43 -0
@@ -0,0 +1,166 @@
1
+ import { RETOUCH_PARTS_MANY as MANY, RETOUCH_PARTS_NAMES as NAMES } from '@openartifactsai/motion-core';
2
+ import { isPart, kidsOf, saidOf } from "../layers/parts.js";
3
+ /*
4
+ * What a hide on a g inside an svg is also found by (an entry's "parts"), once a change inside the g ends its print:
5
+ * a hash of each named g in it and of each of its shapes. A g whose shapes are mostly still there, with little put in,
6
+ * is still the g that was hidden. The hashes match the player's that the record is shared with, to the letter.
7
+ */
8
+ /** The longest a `parts` gets. */
9
+ const MOST = 32;
10
+ /** What a shape's outline and place are written in. A missing one counts as the word "null". */
11
+ const OUTLINE = ['d', 'points', 'x', 'y', 'width', 'height', 'cx', 'cy', 'r', 'rx', 'ry', 'x1', 'y1', 'x2', 'y2', 'transform', 'xlink:href'];
12
+ /** FNV-1a over UTF-16 code units, unsigned. */
13
+ export function fnv(s) {
14
+ let h = 0x811c9dc5;
15
+ for (let i = 0; i < s.length; i++)
16
+ h = Math.imul(h ^ s.charCodeAt(i), 16777619);
17
+ return h >>> 0;
18
+ }
19
+ const isGroup = (el) => /^g$/i.test(el.tagName);
20
+ /** The name a g has of its own: its id, its classes, its data-label and its aria-label. */
21
+ function named(g) {
22
+ return [g.id, g.getAttribute('class') ?? '', g.getAttribute('data-label') ?? '', g.getAttribute('aria-label') ?? ''].join(' ').trim();
23
+ }
24
+ /** What a g is known by: the shapes under it (parts that are no g's) and the g's under it that have a name, however deep. */
25
+ function piecesIn(up, out = []) {
26
+ for (const el of kidsOf(up).filter(isPart)) {
27
+ if (!isGroup(el) || named(el))
28
+ out.push(el);
29
+ if (isGroup(el))
30
+ piecesIn(el, out);
31
+ }
32
+ return out;
33
+ }
34
+ /**
35
+ * The paint written on the svg and the g's round a part, as one short hash. A holder's tag, id and classes do not
36
+ * count, so a drawing is known again under another name; a holder with no paint adds nothing, so a new wrapper
37
+ * changes nothing.
38
+ */
39
+ function lent(el, memo) {
40
+ const up = el.parentElement;
41
+ if (!up)
42
+ return '';
43
+ const known = (memo.lent ??= new Map()).get(up);
44
+ if (known !== undefined)
45
+ return known;
46
+ const above = isPart(up) ? lent(up, memo) : '';
47
+ const paint = saidOf(up).slice((up.tagName + ' ' + up.id + ' ' + (up.getAttribute('class') ?? '')).length);
48
+ const out = /\S/.test(paint) ? String(fnv(above + paint)) : above;
49
+ memo.lent.set(up, out);
50
+ return out;
51
+ }
52
+ /**
53
+ * A piece in six hex digits. A g: a hash of its name, led by 0, which tells it from a shape. A shape: a hash from
54
+ * 100000 up of how it is written: what its own attributes say (saidOf), its outline and place, and the paint it may
55
+ * take from what holds it.
56
+ */
57
+ export function written(el, memo) {
58
+ const known = (memo.written ??= new Map()).get(el);
59
+ if (known !== undefined)
60
+ return known;
61
+ const n = isGroup(el)
62
+ ? fnv(named(el)) >>> 12
63
+ : ((fnv(lent(el, memo) + saidOf(el) + OUTLINE.map((name) => ' ' + String(el.getAttribute(name))).join('')) >>> 8) % 0xf00000) + 0x100000;
64
+ const out = ('00000' + n.toString(16)).slice(-6);
65
+ memo.written.set(el, out);
66
+ return out;
67
+ }
68
+ /** How many of the pieces are written each way. */
69
+ function tally(pieces, memo) {
70
+ const n = new Map();
71
+ for (const el of pieces) {
72
+ const w = written(el, memo);
73
+ n.set(w, (n.get(w) ?? 0) + 1);
74
+ }
75
+ return n;
76
+ }
77
+ /** The shapes on the whole stage, tallied. `memo.art` is read: the caller has listed the stage's g's (peers). */
78
+ function onStage(memo) {
79
+ return (memo.pieces ??= tally((memo.art ?? []).filter((el) => isPart(el) && !isGroup(el)), memo));
80
+ }
81
+ /** A g holds something that may draw and is not read: a nested svg, a link, a switch, the kit's marks, what a foreignObject holds. */
82
+ function odd(g) {
83
+ return Array.from(g.getElementsByTagName('*')).some((el) => /^(svg|a|switch)$/i.test(el.tagName) || el.hasAttribute('data-film-mark') || /^foreignObject$/i.test(el.parentElement?.tagName ?? ''));
84
+ }
85
+ /** A use on the stage draws a copy of the g, which would be hidden with it. */
86
+ function copied(g, memo) {
87
+ return !!g.id && (memo.art ?? []).some((el) => /^use$/i.test(el.tagName) && (el.getAttribute('href') || el.getAttribute('xlink:href')) === '#' + g.id);
88
+ }
89
+ /** The lowest of some hashes, none kept more than MANY times, so that many alike do not crowd out the rest. */
90
+ function lowest(hashes, most) {
91
+ return hashes
92
+ .slice()
93
+ .sort()
94
+ .filter((w, i, all) => w !== all[i - MANY])
95
+ .slice(0, most);
96
+ }
97
+ /**
98
+ * The `parts` of an entry that hides this g now: the lowest hashes of the named g's in it (NAMES at most), then of its
99
+ * shapes, MOST in all; those kept do not depend on the drawing's order. null: it holds what may draw unread, or at most
100
+ * half of its shapes stand only in it, so a copy of it could pass for it. The caller has seen that it has no words.
101
+ */
102
+ export function partsOf(g, memo) {
103
+ const own = piecesIn(g);
104
+ const inside = tally(own, memo);
105
+ const all = onStage(memo);
106
+ const said = own.map((el) => written(el, memo));
107
+ const names = lowest(said.filter((w) => w < '1'), NAMES);
108
+ const shapes = said.filter((w) => w >= '1');
109
+ if (odd(g) || 2 * shapes.filter((w) => all.get(w) === inside.get(w)).length <= shapes.length)
110
+ return null;
111
+ return names.concat(lowest(shapes, MOST - names.length)).join('.');
112
+ }
113
+ /**
114
+ * Whether a g is still the one that held these pieces: more of the saved shapes are in it than elsewhere on the
115
+ * stage, and over three quarters of them are (or nothing new stands in it); and little was put in, so that not much is
116
+ * hidden that nobody hid: new shapes are at most a third of its shapes, and under a g whose name is new, or on more g's
117
+ * than were saved, stand only saved shapes. Nor may it hold what draws unread, or be drawn again by a use.
118
+ */
119
+ export function holds(g, parts, memo) {
120
+ const inside = tally(piecesIn(g), memo);
121
+ const all = onStage(memo);
122
+ const times = new Map();
123
+ const seen = new Map();
124
+ const shapes = parts.filter((w) => w >= '1');
125
+ // Where all MOST places are used, a shape hashed above the highest saved may be saved or new.
126
+ const top = parts.length < MOST ? 'g' : parts[MOST - 1];
127
+ // More of a piece than were saved; one saved MANY times may have been there any number of times.
128
+ const over = (w, n) => n > (times.get(w) ?? 0) && times.get(w) !== MANY;
129
+ let here = 0;
130
+ let away = 0;
131
+ let old = 0;
132
+ let fresh = 0;
133
+ let dark = 0;
134
+ let alien = false;
135
+ for (const w of parts) {
136
+ const t = (times.get(w) ?? 0) + 1;
137
+ times.set(w, t);
138
+ if (w < '1')
139
+ continue;
140
+ if (t <= (inside.get(w) ?? 0))
141
+ here++;
142
+ if (t <= (all.get(w) ?? 0) - (inside.get(w) ?? 0))
143
+ away++;
144
+ }
145
+ const walk = (up, strange) => {
146
+ for (const el of kidsOf(up).filter(isPart)) {
147
+ const w = written(el, memo);
148
+ if (isGroup(el)) {
149
+ walk(el, strange || over(w, inside.get(w) ?? 0));
150
+ continue;
151
+ }
152
+ const n = (seen.get(w) ?? 0) + 1;
153
+ seen.set(w, n);
154
+ if (!over(w, n))
155
+ old++;
156
+ else if (w < top)
157
+ fresh++;
158
+ else
159
+ dark++;
160
+ if (strange && over(w, inside.get(w) ?? 0))
161
+ alien = true;
162
+ }
163
+ };
164
+ walk(g, false);
165
+ return here > away && !odd(g) && !copied(g, memo) && !alien && 3 * fresh <= old + fresh && (4 * here > 3 * shapes.length || !(fresh || dark));
166
+ }
@@ -0,0 +1,37 @@
1
+ import type { RetouchedRecord } from '@openartifactsai/motion-core';
2
+ import type { ReadMemo } from './entry.js';
3
+ import type { LineMemo } from './seat.js';
4
+ /**
5
+ * Draws a moment of the stretch, in its own seconds. true: drawn; false: the film threw there; null: nothing more can
6
+ * be drawn this load (a render gave a promise).
7
+ */
8
+ export type DrawMoment = (s: number) => boolean | null;
9
+ export declare const stageOf: () => HTMLElement | null;
10
+ /** Takes the record; the caller draws the frame again. */
11
+ export declare function setRecord(record: RetouchedRecord | null): void;
12
+ export declare const anyRetouch: () => boolean;
13
+ export declare const isDue: (scene: string, lo: number) => boolean;
14
+ /**
15
+ * Reaches the verdicts on the entries of a stretch (lo to hi) not told yet, with no mark on the stage: at each
16
+ * entry's own moment, where its thing must stand, then at moments spread over the stretch, since what the film
17
+ * itself changes in its course is the film's. `draw` draws a second of the stretch; `far` one anywhere in the film,
18
+ * for an entry of another stretch.
19
+ */
20
+ export declare function tell(scene: string, lo: number, hi: number, draw: DrawMoment, far: DrawMoment): void;
21
+ /** Marks what is laid on the stretch just drawn, which starts at lo. scene null: nothing is laid (a rough scene is a sketch). */
22
+ export declare function apply(scene: string | null, lo: number): void;
23
+ /** The entries whose own moment lies in the stretch and whose thing could not be told then, or is another entry's. */
24
+ export declare function lostIn(scene: string, lo: number, hi: number): string[];
25
+ /**
26
+ * Of those, the hides that would count on their thing, where another entry's hide is laid: the thing is hidden all the
27
+ * same, so the page says nothing of them.
28
+ */
29
+ export declare function underHideIn(scene: string, lo: number, hi: number): string[];
30
+ /** 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. */
31
+ export declare function noteBox(node: HTMLElement): void;
32
+ /** The kin of an element held to the classes `cls`, on the stage as it is now. */
33
+ export declare function kinFor(el: Element, cls: string, memo: ReadMemo): string | null;
34
+ /** 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. */
35
+ export declare function partsFor(el: Element, memo: ReadMemo): string | null;
36
+ /** The line of words a text node of the stage is part of. */
37
+ export declare function lineFor(node: Node, memo: Map<Element, LineMemo>): string | null;
@@ -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.js';
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;