@motionscript/code 0.0.0-stage → 0.1.0-alpha.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 (103) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +201 -0
  3. package/README.md +76 -3
  4. package/dist/browser/chunks/chunk-HET2RCSD.js +2 -0
  5. package/dist/browser/chunks/chunk-HET2RCSD.js.map +7 -0
  6. package/dist/browser/chunks/chunk-SYZUZB3M.js +2 -0
  7. package/dist/browser/chunks/chunk-SYZUZB3M.js.map +7 -0
  8. package/dist/browser/chunks/dist-3F5TQHM6.js +2 -0
  9. package/dist/browser/chunks/dist-3F5TQHM6.js.map +7 -0
  10. package/dist/browser/chunks/dist-3SIR23P4.js +2 -0
  11. package/dist/browser/chunks/dist-3SIR23P4.js.map +7 -0
  12. package/dist/browser/chunks/dist-AQRNACF3.js +2 -0
  13. package/dist/browser/chunks/dist-AQRNACF3.js.map +7 -0
  14. package/dist/browser/chunks/dist-B46VH2YP.js +2 -0
  15. package/dist/browser/chunks/dist-B46VH2YP.js.map +7 -0
  16. package/dist/browser/chunks/dist-FH6BHJ6A.js +7 -0
  17. package/dist/browser/chunks/dist-FH6BHJ6A.js.map +7 -0
  18. package/dist/browser/chunks/dist-FQDUBZKU.js +2 -0
  19. package/dist/browser/chunks/dist-FQDUBZKU.js.map +7 -0
  20. package/dist/browser/chunks/dist-GPNEVCJI.js +2 -0
  21. package/dist/browser/chunks/dist-GPNEVCJI.js.map +7 -0
  22. package/dist/browser/chunks/dist-LRSXSDHT.js +2 -0
  23. package/dist/browser/chunks/dist-LRSXSDHT.js.map +7 -0
  24. package/dist/browser/chunks/dist-N4CNBNHL.js +2 -0
  25. package/dist/browser/chunks/dist-N4CNBNHL.js.map +7 -0
  26. package/dist/browser/chunks/dist-OXJDTPQ6.js +2 -0
  27. package/dist/browser/chunks/dist-OXJDTPQ6.js.map +7 -0
  28. package/dist/browser/chunks/dist-SH7H4MRK.js +2 -0
  29. package/dist/browser/chunks/dist-SH7H4MRK.js.map +7 -0
  30. package/dist/browser/chunks/dist-UG3NTKWL.js +2 -0
  31. package/dist/browser/chunks/dist-UG3NTKWL.js.map +7 -0
  32. package/dist/browser/chunks/dist-W5ZXIMV7.js +2 -0
  33. package/dist/browser/chunks/dist-W5ZXIMV7.js.map +7 -0
  34. package/dist/browser/index.js +8 -0
  35. package/dist/browser/index.js.map +7 -0
  36. package/dist/browser/manifest.json +11 -0
  37. package/dist/code-range.d.ts +69 -0
  38. package/dist/code-range.d.ts.map +1 -0
  39. package/dist/code-range.js +124 -0
  40. package/dist/code-range.js.map +1 -0
  41. package/dist/diff.d.ts +42 -0
  42. package/dist/diff.d.ts.map +1 -0
  43. package/dist/diff.js +179 -0
  44. package/dist/diff.js.map +1 -0
  45. package/dist/engine.d.ts +15 -0
  46. package/dist/engine.d.ts.map +1 -0
  47. package/dist/engine.js +17 -0
  48. package/dist/engine.js.map +1 -0
  49. package/dist/highlight.d.ts +43 -0
  50. package/dist/highlight.d.ts.map +1 -0
  51. package/dist/highlight.js +243 -0
  52. package/dist/highlight.js.map +1 -0
  53. package/dist/index.d.ts +4 -0
  54. package/dist/index.d.ts.map +1 -0
  55. package/dist/index.js +4 -0
  56. package/dist/index.js.map +1 -0
  57. package/dist/layout.d.ts +77 -0
  58. package/dist/layout.d.ts.map +1 -0
  59. package/dist/layout.js +74 -0
  60. package/dist/layout.js.map +1 -0
  61. package/dist/measure-cache.d.ts +38 -0
  62. package/dist/measure-cache.d.ts.map +1 -0
  63. package/dist/measure-cache.js +0 -0
  64. package/dist/measure-cache.js.map +1 -0
  65. package/dist/node.d.ts +293 -0
  66. package/dist/node.d.ts.map +1 -0
  67. package/dist/node.js +733 -0
  68. package/dist/node.js.map +1 -0
  69. package/dist/nodes.d.ts +18 -0
  70. package/dist/nodes.d.ts.map +1 -0
  71. package/dist/nodes.js +18 -0
  72. package/dist/nodes.js.map +1 -0
  73. package/dist/render.d.ts +28 -0
  74. package/dist/render.d.ts.map +1 -0
  75. package/dist/render.js +223 -0
  76. package/dist/render.js.map +1 -0
  77. package/dist/style.d.ts +88 -0
  78. package/dist/style.d.ts.map +1 -0
  79. package/dist/style.js +203 -0
  80. package/dist/style.js.map +1 -0
  81. package/dist/tokens.d.ts +24 -0
  82. package/dist/tokens.d.ts.map +1 -0
  83. package/dist/tokens.js +50 -0
  84. package/dist/tokens.js.map +1 -0
  85. package/dist/transitions.d.ts +115 -0
  86. package/dist/transitions.d.ts.map +1 -0
  87. package/dist/transitions.js +85 -0
  88. package/dist/transitions.js.map +1 -0
  89. package/package.json +82 -3
  90. package/registry.json +6 -0
  91. package/src/code-range.ts +156 -0
  92. package/src/diff.ts +216 -0
  93. package/src/engine.ts +21 -0
  94. package/src/highlight.ts +291 -0
  95. package/src/index.ts +3 -0
  96. package/src/layout.ts +157 -0
  97. package/src/measure-cache.ts +0 -0
  98. package/src/node.ts +834 -0
  99. package/src/nodes.ts +18 -0
  100. package/src/render.ts +247 -0
  101. package/src/style.ts +251 -0
  102. package/src/tokens.ts +75 -0
  103. package/src/transitions.ts +184 -0
@@ -0,0 +1,184 @@
1
+ import type { CodeLayout } from "./layout";
2
+ import { IdLine } from "./tokens";
3
+
4
+ // A single animated token entry owned by one dim transition. Keyed by stable
5
+ // token id rather than (line, tokenIndex) so concurrent animations don't clobber
6
+ // each other when one shifts the indices of tokens the other is animating.
7
+ export interface AnimToken {
8
+ id: number;
9
+ opacity: number[];
10
+ opacityKeys: number[];
11
+ }
12
+
13
+ /**
14
+ * A highlight cross-fade: opacity, and nothing else.
15
+ *
16
+ * Structure is untouched, so several of these stack happily and a listing held
17
+ * under a highlight still hits the static-layout cache.
18
+ */
19
+ export interface DimTransition {
20
+ kind: "dim";
21
+ tokens: AnimToken[];
22
+ progress: number;
23
+ }
24
+
25
+ /**
26
+ * When each part of a structural edit happens, in normalized transition time.
27
+ *
28
+ * The order is what an edit *looks like* being made: what is going away leaves
29
+ * first, what stays reflows into its new place, and only then does what is new
30
+ * appear. Running them together is what made a replacement look like a
31
+ * cross-dissolve of two unrelated listings.
32
+ */
33
+ export interface Phases {
34
+ /** Removed tokens fade out. Null when the edit removes nothing. */
35
+ out: [number, number] | null;
36
+ /** Surviving tokens travel, and the block resizes. Always present. */
37
+ move: [number, number];
38
+ /** Added tokens fade (and slide) in. Null when the edit adds nothing. */
39
+ in: [number, number] | null;
40
+ }
41
+
42
+ /**
43
+ * One edit in flight: the structure before, the structure after, and who is
44
+ * doing what.
45
+ *
46
+ * Both endpoints are fixed for the transition's whole life, which is why the
47
+ * two resolved layouts hang off it — they are computed once and reused for
48
+ * every frame the edit spans, rather than rebuilt per frame from a structure
49
+ * that is being mutated underneath.
50
+ */
51
+ export interface StructuralTransition {
52
+ kind: "structural";
53
+ from: IdLine[];
54
+ to: IdLine[];
55
+ removedIds: Set<number>;
56
+ addedIds: Set<number>;
57
+ /**
58
+ * Ids of rows in `to` that did not exist before, and so enter as a whole
59
+ * row rather than as tokens spliced into one.
60
+ *
61
+ * They enter by fading in place, and that is the *only* thing they do. An
62
+ * earlier version slid each one in from the edge the block had just grown
63
+ * along — appended rows rising from below, prepended ones descending, ones
64
+ * spliced into the middle arriving from the left. Every one of those is a
65
+ * claim about where the line came from, and a listing has no such story to
66
+ * tell: text is written where it belongs, it does not travel there. Sliding
67
+ * also puts the row under the neighbours it is meant to sit between for the
68
+ * length of its entrance, which reads as the listing redrawing itself.
69
+ */
70
+ enteringLineIds: Set<number>;
71
+ fromColorById: Map<number, string | undefined>;
72
+ phases: Phases;
73
+ progress: number;
74
+ /** @internal Layout cache — see {@link StructuralTransition}. */
75
+ layoutKey: string | null;
76
+ fromLayout: CodeLayout | null;
77
+ toLayout: CodeLayout | null;
78
+ }
79
+
80
+ export type CodeTransition = DimTransition | StructuralTransition;
81
+
82
+ /**
83
+ * Decides *when* (in normalized transition time) each part of a structural
84
+ * edit happens, given only whether the edit removes/adds anything.
85
+ * `phasesFor` below is the default implementation (exported as
86
+ * `defaultCodePhases`) — pass a `phaseStrategy` of this shape to `<Code>` to
87
+ * replace it with your own timing.
88
+ */
89
+ export type CodePhaseStrategy = (hasRemoved: boolean, hasAdded: boolean) => Phases;
90
+
91
+ export interface TokenState {
92
+ opacity: number;
93
+ }
94
+
95
+ /** Sample a piecewise-linear curve (keys → values) at progress `p` in [0, 1]. */
96
+ export function sampleCurve(keys: number[], values: number[], p: number): number {
97
+ if (p <= keys[0]) return values[0];
98
+ if (p >= keys[keys.length - 1]) return values[values.length - 1];
99
+ for (let i = 0; i < keys.length - 1; i++) {
100
+ if (p <= keys[i + 1]) {
101
+ const span = keys[i + 1] - keys[i];
102
+ const local = span === 0 ? 0 : (p - keys[i]) / span;
103
+ return values[i] + (values[i + 1] - values[i]) * local;
104
+ }
105
+ }
106
+ return values[values.length - 1];
107
+ }
108
+
109
+ /** Progress *within* a phase window, clamped to [0, 1]. A null window is complete. */
110
+ export function windowProgress(w: [number, number] | null, p: number): number {
111
+ if (!w) return 1;
112
+ const [a, b] = w;
113
+ if (p <= a) return 0;
114
+ if (p >= b) return 1;
115
+ return b === a ? 1 : (p - a) / (b - a);
116
+ }
117
+
118
+ /** Smooth start and end, so a phase never begins or ends with a velocity step. */
119
+ export function smoothstep(t: number): number {
120
+ return t * t * (3 - 2 * t);
121
+ }
122
+
123
+ export function makeAnim(id: number, curve: { keys: number[]; values: number[] }): AnimToken {
124
+ return { id, opacity: curve.values, opacityKeys: curve.keys };
125
+ }
126
+
127
+ /**
128
+ * The phase schedule for an edit, shaped by what the edit actually contains.
129
+ *
130
+ * A fixed schedule wastes time it does not need: a pure append has nothing to
131
+ * fade out, so holding the first 40% of the duration empty just makes the
132
+ * animation feel late. Each shape gets the whole duration spent on the parts
133
+ * that exist, with adjacent phases overlapping enough to read as one movement.
134
+ */
135
+ export function phasesFor(hasRemoved: boolean, hasAdded: boolean): Phases {
136
+ // The overlaps are deliberate and small. Butt-joining the phases leaves the
137
+ // block sitting empty at its new size for a beat between the old content
138
+ // leaving and the new content arriving; letting the arrival start while the
139
+ // reflow's eased tail is still running closes that gap without blurring the
140
+ // order the edit reads in.
141
+ if (hasRemoved && hasAdded) return { out: [0, 0.38], move: [0.10, 0.68], in: [0.52, 1] };
142
+ if (hasRemoved) return { out: [0, 0.45], move: [0.22, 1], in: null };
143
+ if (hasAdded) return { out: null, move: [0, 0.55], in: [0.38, 1] };
144
+ return { out: null, move: [0, 1], in: null };
145
+ }
146
+
147
+ /** {@link phasesFor} under the name a `phaseStrategy` default is looked up by. */
148
+ export const defaultCodePhases: CodePhaseStrategy = phasesFor;
149
+
150
+ /** How far through the "everything reflows" phase a frame is, eased. A settled frame (no edit) is always fully moved. */
151
+ export function moveProgress(edit: { phases: Phases; progress: number } | null): number {
152
+ return edit ? smoothstep(windowProgress(edit.phases.move, edit.progress)) : 1;
153
+ }
154
+
155
+ /**
156
+ * Resolve the per-token opacity multiplier for the current frame from the
157
+ * persistent highlight dim plus every active dim transition.
158
+ */
159
+ export function resolveTokenStates(
160
+ tokenLines: IdLine[],
161
+ transitions: CodeTransition[],
162
+ highlightDimOpacity: number | null,
163
+ highlightedIds: Set<number>,
164
+ ): Map<number, TokenState> {
165
+ const out = new Map<number, TokenState>();
166
+
167
+ if (highlightDimOpacity !== null) {
168
+ const dim = highlightDimOpacity;
169
+ for (const line of tokenLines) {
170
+ for (const tok of line.tokens) {
171
+ out.set(tok.id, { opacity: highlightedIds.has(tok.id) ? 1 : dim });
172
+ }
173
+ }
174
+ }
175
+
176
+ for (const tr of transitions) {
177
+ if (tr.kind !== "dim") continue;
178
+ for (const at of tr.tokens) {
179
+ out.set(at.id, { opacity: sampleCurve(at.opacityKeys, at.opacity, tr.progress) });
180
+ }
181
+ }
182
+
183
+ return out;
184
+ }