@singapore-editor/file-animation 0.0.0-stage → 0.2.13

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 (59) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +44 -2
  3. package/dist/file-edit-animation.d.ts +18 -0
  4. package/dist/file-edit-animation.d.ts.map +1 -0
  5. package/dist/file-edit-animation.js +260 -0
  6. package/dist/file-edit-animation.js.map +1 -0
  7. package/dist/file-transition.d.ts +10 -0
  8. package/dist/file-transition.d.ts.map +1 -0
  9. package/dist/file-transition.js +271 -0
  10. package/dist/file-transition.js.map +1 -0
  11. package/dist/index.d.ts +3 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +3 -0
  14. package/dist/measure.d.ts +23 -0
  15. package/dist/measure.d.ts.map +1 -0
  16. package/dist/measure.js +50 -0
  17. package/dist/measure.js.map +1 -0
  18. package/dist/morph-frame.d.ts +49 -0
  19. package/dist/morph-frame.d.ts.map +1 -0
  20. package/dist/morph-frame.js +140 -0
  21. package/dist/morph-frame.js.map +1 -0
  22. package/dist/morph-match.d.ts +19 -0
  23. package/dist/morph-match.d.ts.map +1 -0
  24. package/dist/morph-match.js +157 -0
  25. package/dist/morph-match.js.map +1 -0
  26. package/dist/morph-run.d.ts +58 -0
  27. package/dist/morph-run.d.ts.map +1 -0
  28. package/dist/morph-run.js +244 -0
  29. package/dist/morph-run.js.map +1 -0
  30. package/dist/motion.d.ts +10 -0
  31. package/dist/motion.d.ts.map +1 -0
  32. package/dist/motion.js +21 -0
  33. package/dist/motion.js.map +1 -0
  34. package/dist/noise.d.ts +6 -0
  35. package/dist/noise.d.ts.map +1 -0
  36. package/dist/noise.js +42 -0
  37. package/dist/noise.js.map +1 -0
  38. package/dist/piece-span.d.ts +14 -0
  39. package/dist/piece-span.d.ts.map +1 -0
  40. package/dist/piece-span.js +36 -0
  41. package/dist/piece-span.js.map +1 -0
  42. package/dist/reveal-run.d.ts +24 -0
  43. package/dist/reveal-run.d.ts.map +1 -0
  44. package/dist/reveal-run.js +268 -0
  45. package/dist/reveal-run.js.map +1 -0
  46. package/dist/row-cover.d.ts +31 -0
  47. package/dist/row-cover.d.ts.map +1 -0
  48. package/dist/row-cover.js +81 -0
  49. package/dist/row-cover.js.map +1 -0
  50. package/dist/row-moves.d.ts +40 -0
  51. package/dist/row-moves.d.ts.map +1 -0
  52. package/dist/row-moves.js +91 -0
  53. package/dist/row-moves.js.map +1 -0
  54. package/dist/style.css +72 -0
  55. package/dist/transition-options.d.ts +45 -0
  56. package/dist/transition-options.d.ts.map +1 -0
  57. package/dist/transition-options.js +38 -0
  58. package/dist/transition-options.js.map +1 -0
  59. package/package.json +59 -4
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Shaul Lavo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md CHANGED
@@ -1,3 +1,45 @@
1
- # Temporary Holding Version
1
+ # @singapore-editor/file-animation
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ File transition and file edit animations for the Singapore editor.
4
+
5
+ Part of [Singapore](https://singapore.shaulavo.dev/). A code editor for the browser that keeps every version.
6
+
7
+ ## Install
8
+
9
+ Not on npm yet. Build it from source with the [development guide](https://github.com/ShaulLavo/singapore/blob/main/docs/development.md#set-up).
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { Editor } from '@singapore-editor/core/editor'
15
+ import {
16
+ createFileEditAnimationPlugin,
17
+ createFileTransitionPlugin,
18
+ } from '@singapore-editor/file-animation'
19
+
20
+ const editor = new Editor(document.querySelector<HTMLElement>('#editor')!, {
21
+ plugins: [createFileTransitionPlugin(), createFileEditAnimationPlugin()],
22
+ })
23
+ editor.openDocument({
24
+ documentId: 'example.ts',
25
+ text: 'const value = 1;\n',
26
+ languageId: 'typescript',
27
+ })
28
+ ```
29
+
30
+ ## API highlights
31
+
32
+ - `createFileTransitionPlugin()` animates a file as it opens. The `mode` option picks `autoregressive` (the default), `parallel`, `token`, `diffusion`, or `magicMove`, which slides the text two files share from the previous file to the new one.
33
+ - `createFileEditAnimationPlugin()` animates every edit except typing.
34
+
35
+ [Generated API reference](https://singapore.shaulavo.dev/docs/reference/api/file-animation/overview/)
36
+
37
+ ## In the Singapore family
38
+
39
+ `@singapore-editor/core` owns the editor view. Add these plugins to animate opened files and edits.
40
+
41
+ [Singapore README](https://github.com/ShaulLavo/singapore/blob/main/README.md) · [Documentation](https://singapore.shaulavo.dev/docs/start-here/introduction/)
42
+
43
+ ## License
44
+
45
+ MIT. [License](https://github.com/ShaulLavo/singapore/blob/main/LICENSE)
@@ -0,0 +1,18 @@
1
+ import type { EditorPlugin } from '@singapore-editor/core/extensions';
2
+ import './style.css';
3
+ export type FileEditAnimationPluginOptions = {
4
+ /** Length of one morph in milliseconds. Defaults to `520`. */
5
+ readonly durationMs?: number;
6
+ /** Spring overshoot of moving text, from `0` (none) to `0.5`. Defaults to `0.18`. */
7
+ readonly bounce?: number;
8
+ /** Safety cap on pieces drawn; a larger viewport skips the morph. Defaults to `2500`. */
9
+ readonly maxPieces?: number;
10
+ };
11
+ /**
12
+ * Text changes animate from the old text to the new: text that survives the
13
+ * change slides to its new place, removed text fades out, and new text streams
14
+ * in. Every change morphs except keyboard text input, so typing stays instant.
15
+ * Including this plugin turns the effect on.
16
+ */
17
+ export declare function createFileEditAnimationPlugin(options?: FileEditAnimationPluginOptions): EditorPlugin;
18
+ //# sourceMappingURL=file-edit-animation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-edit-animation.d.ts","sourceRoot":"","sources":["../src/file-edit-animation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,YAAY,EAKb,MAAM,mCAAmC,CAAA;AAS1C,OAAO,aAAa,CAAA;AAEpB,MAAM,MAAM,8BAA8B,GAAG;IAC3C,8DAA8D;IAC9D,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;IAC5B,qFAAqF;IACrF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;IACxB,yFAAyF;IACzF,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAC5B,CAAA;AAMD;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAC3C,OAAO,GAAE,8BAAmC,GAC3C,YAAY,CAcd"}
@@ -0,0 +1,260 @@
1
+ import { TextMeasurer, canvasFont, renderedTabColumns } from "./measure.js";
2
+ import { buildFrame, frameMoved, recolorFrame } from "./morph-frame.js";
3
+ import { matchPieces } from "./morph-match.js";
4
+ import { MorphRun, restingPieces, springEasing } from "./morph-run.js";
5
+ import { drawableRows, reducedMotion, supportsLinearEasing } from "./motion.js";
6
+ import { RowMoves, rowMoves } from "./row-moves.js";
7
+ import { RowCover } from "./row-cover.js";
8
+ import "./style.css";
9
+ import { EditorTokenStore } from "@singapore-editor/core/syntax";
10
+ // src/file-edit-animation.ts
11
+ var ACTIVE_CLASS = "editor-morph-active";
12
+ var TEXT_CHANGES = /* @__PURE__ */ new Set([
13
+ "edit",
14
+ "undo",
15
+ "redo",
16
+ "checkout",
17
+ "reconcile"
18
+ ]);
19
+ /**
20
+ * Text changes animate from the old text to the new: text that survives the
21
+ * change slides to its new place, removed text fades out, and new text streams
22
+ * in. Every change morphs except keyboard text input, so typing stays instant.
23
+ * Including this plugin turns the effect on.
24
+ */
25
+ function createFileEditAnimationPlugin(options = {}) {
26
+ const resolved = {
27
+ durationMs: positive(options.durationMs, 520),
28
+ bounce: Math.min(.5, Math.max(0, options.bounce ?? .18)),
29
+ maxPieces: positive(options.maxPieces, 2500)
30
+ };
31
+ return {
32
+ name: "editor.fileEditAnimation",
33
+ activate: (context) => context.registerViewContribution({ createContribution: (viewContext) => new FileEditAnimationViewContribution(viewContext, resolved) })
34
+ };
35
+ }
36
+ function editFrame(snapshot) {
37
+ const visibleRows = [];
38
+ for (const row of snapshot.visibleRows) {
39
+ const text = row.text;
40
+ if (typeof text !== "string") return null;
41
+ visibleRows.push({
42
+ index: row.index,
43
+ kind: row.kind,
44
+ startOffset: row.startOffset,
45
+ endOffset: row.endOffset,
46
+ text,
47
+ top: row.top,
48
+ height: row.height,
49
+ leftSpacerWidth: row.leftSpacerWidth,
50
+ mountedPaintSupport: row.mountedPaintSupport,
51
+ chunks: row.chunks.map((chunk) => ({
52
+ text: typeof chunk.text === "string" ? chunk.text : text.slice(chunk.rowLocalStart, chunk.rowLocalEnd),
53
+ mountedPaint: chunk.mountedPaint
54
+ }))
55
+ });
56
+ }
57
+ return {
58
+ documentId: snapshot.documentId,
59
+ textVersion: snapshot.textVersion,
60
+ visibleRows,
61
+ tokens: mountedTokens(snapshot.tokens, visibleRows)
62
+ };
63
+ }
64
+ function addRowTokens(indices, tokens, row) {
65
+ if (row.endOffset <= row.startOffset) return;
66
+ const end = tokens.firstStartingAtOrAfter(row.endOffset);
67
+ for (let index = tokens.firstEndingAfter(row.startOffset, end); index < end; index++) if (tokens.endAt(index) > row.startOffset) indices.add(index);
68
+ }
69
+ function mountedTokens(tokens, rows) {
70
+ const indices = /* @__PURE__ */ new Set();
71
+ for (const row of rows) addRowTokens(indices, tokens, row);
72
+ const selected = Array.from(indices);
73
+ selected.sort((first, second) => first - second);
74
+ return EditorTokenStore.fromTokens(selected.map((index) => {
75
+ const { color, fontStyle, fontWeight } = tokens.styleAt(index);
76
+ return {
77
+ start: tokens.startAt(index),
78
+ end: tokens.endAt(index),
79
+ style: {
80
+ color,
81
+ fontStyle,
82
+ fontWeight
83
+ }
84
+ };
85
+ }));
86
+ }
87
+ function mountedReplacements(ranges, rows) {
88
+ return ranges.filter((range) => rows.some((row) => range.start < row.endOffset && range.end > row.startOffset)).map(({ start, end }) => ({
89
+ start,
90
+ end
91
+ }));
92
+ }
93
+ var FileEditAnimationViewContribution = class {
94
+ context;
95
+ options;
96
+ inputs = [
97
+ "content",
98
+ "tokens",
99
+ "viewport",
100
+ "layout",
101
+ "selection"
102
+ ];
103
+ latest = null;
104
+ /** Mounted paint before `latest`'s text, without its complete document source. */
105
+ before = null;
106
+ /** Text painted as something other than its source (chips, hidden markup), for each snapshot. */
107
+ latestReplaced = [];
108
+ beforeReplaced = [];
109
+ moves = null;
110
+ measurer = null;
111
+ run = null;
112
+ /** The snapshot the running morph settles on, and the rows its overlay covers. */
113
+ runSnapshot = null;
114
+ runRows = /* @__PURE__ */ new Set();
115
+ cover;
116
+ timing = null;
117
+ constructor(context, options) {
118
+ this.context = context;
119
+ this.options = options;
120
+ this.cover = new RowCover(context);
121
+ if (context.hasDocument()) this.latest = editFrame(context.getSnapshot());
122
+ this.latestReplaced = mountedReplacements(context.getInlineReplacementRanges(), this.latest?.visibleRows ?? []);
123
+ }
124
+ update(snapshot, kind, change) {
125
+ if (kind === "document" || kind === "clear") {
126
+ this.finish();
127
+ this.before = null;
128
+ this.beforeReplaced = [];
129
+ this.latest = kind === "clear" ? null : editFrame(snapshot);
130
+ this.latestReplaced = kind === "clear" ? [] : mountedReplacements(this.context.getInlineReplacementRanges(), this.latest?.visibleRows ?? []);
131
+ return;
132
+ }
133
+ const captured = editFrame(snapshot);
134
+ if (!captured) {
135
+ this.finish();
136
+ this.latest = null;
137
+ this.before = null;
138
+ this.latestReplaced = [];
139
+ this.beforeReplaced = [];
140
+ return;
141
+ }
142
+ if (!this.latest || this.latest.textVersion !== snapshot.textVersion) {
143
+ this.before = this.latest;
144
+ this.beforeReplaced = this.latestReplaced;
145
+ }
146
+ this.latest = captured;
147
+ if (kind !== "viewport") this.latestReplaced = mountedReplacements(this.context.getInlineReplacementRanges(), captured.visibleRows);
148
+ const previous = this.before;
149
+ if (kind === "layout") this.measurer = null;
150
+ if (kind !== "content" || change && !TEXT_CHANGES.has(change.kind)) {
151
+ this.keepCovering(snapshot, kind);
152
+ return;
153
+ }
154
+ if (!previous || previous.textVersion === snapshot.textVersion) return;
155
+ if (!this.shouldMorph(previous, snapshot, change ?? null)) {
156
+ this.finish();
157
+ return;
158
+ }
159
+ this.morph(previous, snapshot);
160
+ }
161
+ keepCovering(snapshot, kind) {
162
+ const started = this.runSnapshot;
163
+ if (!this.run || !started) return;
164
+ if (snapshot.textVersion !== started.textVersion) {
165
+ if (kind === "content") this.finish();
166
+ return;
167
+ }
168
+ if (frameMoved(started, snapshot, this.runRows)) {
169
+ this.finish();
170
+ return;
171
+ }
172
+ this.cover.refresh(snapshot);
173
+ if (kind === "tokens") this.run.recolor(recolorFrame(this.run.targetFrame, snapshot.tokens));
174
+ }
175
+ dispose() {
176
+ this.finish();
177
+ this.latest = null;
178
+ this.before = null;
179
+ this.latestReplaced = [];
180
+ this.beforeReplaced = [];
181
+ }
182
+ shouldMorph(previous, snapshot, change) {
183
+ if (reducedMotion(this.context)) return false;
184
+ if (previous.documentId !== snapshot.documentId) return false;
185
+ return !change?.typing;
186
+ }
187
+ morph(previous, snapshot) {
188
+ const drawn = drawableRows(snapshot, this.latestReplaced);
189
+ const real = new Set(Array.from(snapshot.visibleRows, (row) => row.index));
190
+ for (const index of drawn) real.delete(index);
191
+ const { moves, taken } = rowMoves(previous, snapshot, real);
192
+ const rows = this.cover.collect(snapshot, drawn);
193
+ const measurer = this.ensureMeasurer(snapshot, rows[0]?.presentation.element);
194
+ const next = buildFrame(snapshot, measurer, this.options.maxPieces, void 0, (row) => drawn.has(row.index));
195
+ const from = this.run ? this.run.visualPieces() : this.restingFrom(previous, measurer, taken);
196
+ if (!next || !from) {
197
+ for (const row of rows) row.presentation.dispose();
198
+ this.finish();
199
+ return;
200
+ }
201
+ const pairs = matchPieces(from, next.pieces);
202
+ this.run?.cancel();
203
+ this.moves?.cancel();
204
+ const timing = this.ensureTiming();
205
+ this.run = new MorphRun(this.context.contentElement, next, from, pairs, timing, () => this.settleOverlay());
206
+ this.moves = new RowMoves(this.context, moves, timing);
207
+ this.context.scrollElement.classList.add(ACTIVE_CLASS);
208
+ this.runSnapshot = snapshot;
209
+ this.runRows = drawn;
210
+ this.cover.cover(rows);
211
+ this.context.log({
212
+ level: "info",
213
+ action: "morph.start",
214
+ pieceCount: next.pieces.length,
215
+ movedCount: pairs.reduce((count, old) => count + (old >= 0 ? 1 : 0), 0),
216
+ fromCount: from.length
217
+ });
218
+ }
219
+ restingFrom(previous, measurer, moving) {
220
+ const drawn = drawableRows(previous, this.beforeReplaced);
221
+ const frame = buildFrame(previous, measurer, this.options.maxPieces, void 0, (row) => drawn.has(row.index) && !moving.has(row.index));
222
+ return frame ? restingPieces(frame) : null;
223
+ }
224
+ finish() {
225
+ this.moves?.cancel();
226
+ this.moves = null;
227
+ this.settleOverlay();
228
+ }
229
+ /** Drops the overlay; rows still moving on their own finish their motion. */
230
+ settleOverlay() {
231
+ this.context.scrollElement.classList.remove(ACTIVE_CLASS);
232
+ this.cover.showAll();
233
+ this.runSnapshot = null;
234
+ this.runRows = /* @__PURE__ */ new Set();
235
+ this.run?.cancel();
236
+ this.run = null;
237
+ }
238
+ ensureMeasurer(snapshot, row) {
239
+ const fontSource = row ?? this.context.contentElement;
240
+ const font = canvasFont(fontSource);
241
+ const tabColumns = renderedTabColumns(this.context.scrollElement, snapshot.tabSize);
242
+ if (this.measurer?.font === font && this.measurer.tabColumns === tabColumns) return this.measurer;
243
+ this.measurer = new TextMeasurer(this.context.scrollElement.ownerDocument, font, snapshot.metrics.characterWidth, tabColumns);
244
+ return this.measurer;
245
+ }
246
+ ensureTiming() {
247
+ this.timing ??= {
248
+ durationMs: this.options.durationMs,
249
+ moveEasing: springEasing(this.options.bounce, supportsLinearEasing(this.context))
250
+ };
251
+ return this.timing;
252
+ }
253
+ };
254
+ function positive(value, fallback) {
255
+ if (value === void 0 || !Number.isFinite(value) || value <= 0) return fallback;
256
+ return value;
257
+ }
258
+ export { createFileEditAnimationPlugin };
259
+
260
+ //# sourceMappingURL=file-edit-animation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-edit-animation.js","names":[],"sources":["../src/file-edit-animation.ts"],"sourcesContent":["import type {\n EditorContributionChange,\n EditorPlugin,\n EditorViewContribution,\n EditorViewContributionContext,\n EditorViewContributionUpdateKind,\n EditorViewSnapshot,\n} from '@singapore-editor/core/extensions'\nimport { EditorTokenStore } from '@singapore-editor/core/syntax'\nimport { TextMeasurer, canvasFont, renderedTabColumns } from './measure'\nimport { buildFrame, frameMoved, recolorFrame, type MorphFrame, type PaintRow } from './morph-frame'\nimport { matchPieces } from './morph-match'\nimport { MorphRun, restingPieces, springEasing, type MorphTiming } from './morph-run'\nimport { RowCover } from './row-cover'\nimport { RowMoves, rowMoves } from './row-moves'\nimport { drawableRows, reducedMotion, supportsLinearEasing, type TextOffsetRange } from './motion'\nimport './style.css'\n\nexport type FileEditAnimationPluginOptions = {\n /** Length of one morph in milliseconds. Defaults to `520`. */\n readonly durationMs?: number\n /** Spring overshoot of moving text, from `0` (none) to `0.5`. Defaults to `0.18`. */\n readonly bounce?: number\n /** Safety cap on pieces drawn; a larger viewport skips the morph. Defaults to `2500`. */\n readonly maxPieces?: number\n}\n\nconst ACTIVE_CLASS = 'editor-morph-active'\n\nconst TEXT_CHANGES = new Set(['edit', 'undo', 'redo', 'checkout', 'reconcile'])\n\n/**\n * Text changes animate from the old text to the new: text that survives the\n * change slides to its new place, removed text fades out, and new text streams\n * in. Every change morphs except keyboard text input, so typing stays instant.\n * Including this plugin turns the effect on.\n */\nexport function createFileEditAnimationPlugin(\n options: FileEditAnimationPluginOptions = {},\n): EditorPlugin {\n const resolved = {\n durationMs: positive(options.durationMs, 520),\n bounce: Math.min(0.5, Math.max(0, options.bounce ?? 0.18)),\n maxPieces: positive(options.maxPieces, 2500),\n }\n return {\n name: 'editor.fileEditAnimation',\n activate: (context) =>\n context.registerViewContribution({\n createContribution: (viewContext) =>\n new FileEditAnimationViewContribution(viewContext, resolved),\n }),\n }\n}\n\ntype ResolvedEditAnimationOptions = {\n readonly durationMs: number\n readonly bounce: number\n readonly maxPieces: number\n}\n\ntype EditFrame = Pick<EditorViewSnapshot, 'documentId' | 'textVersion' | 'tokens'> & {\n readonly visibleRows: readonly PaintRow[]\n}\n\nfunction editFrame(snapshot: EditorViewSnapshot): EditFrame | null {\n const visibleRows: PaintRow[] = []\n for (const row of snapshot.visibleRows) {\n const text = row.text\n // A horizontally windowed row cannot be replayed as a whole line.\n if (typeof text !== 'string') return null\n visibleRows.push({\n index: row.index,\n kind: row.kind,\n startOffset: row.startOffset,\n endOffset: row.endOffset,\n text,\n top: row.top,\n height: row.height,\n leftSpacerWidth: row.leftSpacerWidth,\n mountedPaintSupport: row.mountedPaintSupport,\n chunks: row.chunks.map((chunk) => ({\n text:\n typeof chunk.text === 'string'\n ? chunk.text\n : text.slice(chunk.rowLocalStart, chunk.rowLocalEnd),\n mountedPaint: chunk.mountedPaint,\n })),\n })\n }\n return {\n documentId: snapshot.documentId,\n textVersion: snapshot.textVersion,\n visibleRows,\n tokens: mountedTokens(snapshot.tokens, visibleRows),\n }\n}\n\nfunction addRowTokens(indices: Set<number>, tokens: EditorTokenStore, row: PaintRow): void {\n if (row.endOffset <= row.startOffset) return\n const end = tokens.firstStartingAtOrAfter(row.endOffset)\n for (let index = tokens.firstEndingAfter(row.startOffset, end); index < end; index++) {\n if (tokens.endAt(index) > row.startOffset) indices.add(index)\n }\n}\n\nfunction mountedTokens(tokens: EditorTokenStore, rows: readonly PaintRow[]): EditorTokenStore {\n const indices = new Set<number>()\n for (const row of rows) addRowTokens(indices, tokens, row)\n const selected = Array.from(indices)\n selected.sort((first, second) => first - second)\n return EditorTokenStore.fromTokens(\n selected.map((index) => {\n const { color, fontStyle, fontWeight } = tokens.styleAt(index)\n return {\n start: tokens.startAt(index),\n end: tokens.endAt(index),\n style: { color, fontStyle, fontWeight },\n }\n }),\n )\n}\n\nfunction mountedReplacements(\n ranges: readonly TextOffsetRange[],\n rows: readonly PaintRow[],\n): readonly TextOffsetRange[] {\n return ranges\n .filter((range) =>\n rows.some((row) => range.start < row.endOffset && range.end > row.startOffset),\n )\n .map(({ start, end }) => ({ start, end }))\n}\n\nclass FileEditAnimationViewContribution implements EditorViewContribution {\n public readonly inputs = ['content', 'tokens', 'viewport', 'layout', 'selection'] as const\n private latest: EditFrame | null = null\n /** Mounted paint before `latest`'s text, without its complete document source. */\n private before: EditFrame | null = null\n /** Text painted as something other than its source (chips, hidden markup), for each snapshot. */\n private latestReplaced: readonly TextOffsetRange[] = []\n private beforeReplaced: readonly TextOffsetRange[] = []\n private moves: RowMoves | null = null\n private measurer: TextMeasurer | null = null\n private run: MorphRun | null = null\n /** The snapshot the running morph settles on, and the rows its overlay covers. */\n private runSnapshot: EditorViewSnapshot | null = null\n private runRows: ReadonlySet<number> = new Set()\n private readonly cover: RowCover\n private timing: MorphTiming | null = null\n\n public constructor(\n private readonly context: EditorViewContributionContext,\n private readonly options: ResolvedEditAnimationOptions,\n ) {\n // Added to an editor that already shows a document, the first change still needs its before.\n this.cover = new RowCover(context)\n if (context.hasDocument()) this.latest = editFrame(context.getSnapshot())\n this.latestReplaced = mountedReplacements(\n context.getInlineReplacementRanges(),\n this.latest?.visibleRows ?? [],\n )\n }\n\n public update(\n snapshot: EditorViewSnapshot,\n kind: EditorViewContributionUpdateKind,\n change?: EditorContributionChange | null,\n ): void {\n if (kind === 'document' || kind === 'clear') {\n this.finish()\n this.before = null\n this.beforeReplaced = []\n this.latest = kind === 'clear' ? null : editFrame(snapshot)\n this.latestReplaced =\n kind === 'clear'\n ? []\n : mountedReplacements(\n this.context.getInlineReplacementRanges(),\n this.latest?.visibleRows ?? [],\n )\n return\n }\n const captured = editFrame(snapshot)\n if (!captured) {\n this.finish()\n this.latest = null\n this.before = null\n this.latestReplaced = []\n this.beforeReplaced = []\n return\n }\n // One pass can deliver the new text under several kinds, `viewport` before `content`.\n if (!this.latest || this.latest.textVersion !== snapshot.textVersion) {\n this.before = this.latest\n this.beforeReplaced = this.latestReplaced\n }\n this.latest = captured\n // Read while each snapshot is current: once the text changes, the old replacements are gone.\n if (kind !== 'viewport')\n this.latestReplaced = mountedReplacements(\n this.context.getInlineReplacementRanges(),\n captured.visibleRows,\n )\n const previous = this.before\n if (kind === 'layout') this.measurer = null\n if (kind !== 'content' || (change && !TEXT_CHANGES.has(change.kind))) {\n this.keepCovering(snapshot, kind)\n return\n }\n if (!previous || previous.textVersion === snapshot.textVersion) return\n if (!this.shouldMorph(previous, snapshot, change ?? null)) {\n this.finish()\n return\n }\n this.morph(previous, snapshot)\n }\n\n // Between changes: keep repainted rows hidden and the overlay coloured, or settle when the rows\n // moved under it (new metrics, a rewrap). Newer text waits for its `content` update to retarget.\n private keepCovering(snapshot: EditorViewSnapshot, kind: EditorViewContributionUpdateKind): void {\n const started = this.runSnapshot\n if (!this.run || !started) return\n if (snapshot.textVersion !== started.textVersion) {\n // A text change that is not a morph (a save sync) repaints rows the overlay no longer matches.\n if (kind === 'content') this.finish()\n return\n }\n if (frameMoved(started, snapshot, this.runRows)) {\n this.finish()\n return\n }\n this.cover.refresh(snapshot)\n if (kind === 'tokens') this.run.recolor(recolorFrame(this.run.targetFrame, snapshot.tokens))\n }\n\n public dispose(): void {\n this.finish()\n this.latest = null\n this.before = null\n this.latestReplaced = []\n this.beforeReplaced = []\n }\n\n private shouldMorph(\n previous: EditFrame,\n snapshot: EditorViewSnapshot,\n change: EditorContributionChange | null,\n ): boolean {\n if (reducedMotion(this.context)) return false\n if (previous.documentId !== snapshot.documentId) return false\n // `setContent` on an editor without a session re-renders the whole text with no change record.\n // Typed text shows at once: a keystroke settles any morph instead of joining it.\n return !change?.typing\n }\n\n // Rows painted as their source text morph on the overlay. The rest (rendered Markdown, chips)\n // stay real and move as whole rows, because the overlay cannot draw what they show.\n private morph(previous: EditFrame, snapshot: EditorViewSnapshot): void {\n const drawn = drawableRows(snapshot, this.latestReplaced)\n const real = new Set(Array.from(snapshot.visibleRows, (row) => row.index))\n for (const index of drawn) real.delete(index)\n const { moves, taken } = rowMoves(previous, snapshot, real)\n const rows = this.cover.collect(snapshot, drawn)\n const measurer = this.ensureMeasurer(snapshot, rows[0]?.presentation.element)\n const next = buildFrame(snapshot, measurer, this.options.maxPieces, undefined, (row) =>\n drawn.has(row.index),\n )\n const from = this.run ? this.run.visualPieces() : this.restingFrom(previous, measurer, taken)\n if (!next || !from) {\n for (const row of rows) row.presentation.dispose()\n this.finish()\n return\n }\n const pairs = matchPieces(from, next.pieces)\n\n this.run?.cancel()\n this.moves?.cancel()\n const timing = this.ensureTiming()\n this.run = new MorphRun(this.context.contentElement, next, from, pairs, timing, () =>\n this.settleOverlay(),\n )\n this.moves = new RowMoves(this.context, moves, timing)\n this.context.scrollElement.classList.add(ACTIVE_CLASS)\n this.runSnapshot = snapshot\n this.runRows = drawn\n this.cover.cover(rows)\n this.context.log({\n level: 'info',\n action: 'morph.start',\n pieceCount: next.pieces.length,\n movedCount: pairs.reduce((count, old) => count + (old >= 0 ? 1 : 0), 0),\n fromCount: from.length,\n })\n }\n\n private restingFrom(previous: EditFrame, measurer: TextMeasurer, moving: ReadonlySet<number>) {\n const drawn = drawableRows(previous, this.beforeReplaced)\n const frame: MorphFrame | null = buildFrame(\n previous,\n measurer,\n this.options.maxPieces,\n undefined,\n (row) => drawn.has(row.index) && !moving.has(row.index),\n )\n return frame ? restingPieces(frame) : null\n }\n\n private finish(): void {\n this.moves?.cancel()\n this.moves = null\n this.settleOverlay()\n }\n\n /** Drops the overlay; rows still moving on their own finish their motion. */\n private settleOverlay(): void {\n this.context.scrollElement.classList.remove(ACTIVE_CLASS)\n this.cover.showAll()\n this.runSnapshot = null\n this.runRows = new Set()\n this.run?.cancel()\n this.run = null\n }\n\n private ensureMeasurer(snapshot: EditorViewSnapshot, row: Element | undefined): TextMeasurer {\n const fontSource = row ?? this.context.contentElement\n const font = canvasFont(fontSource)\n const tabColumns = renderedTabColumns(this.context.scrollElement, snapshot.tabSize)\n if (this.measurer?.font === font && this.measurer.tabColumns === tabColumns)\n return this.measurer\n this.measurer = new TextMeasurer(\n this.context.scrollElement.ownerDocument,\n font,\n snapshot.metrics.characterWidth,\n tabColumns,\n )\n return this.measurer\n }\n\n private ensureTiming(): MorphTiming {\n this.timing ??= {\n durationMs: this.options.durationMs,\n moveEasing: springEasing(this.options.bounce, supportsLinearEasing(this.context)),\n }\n return this.timing\n }\n}\n\nfunction positive(value: number | undefined, fallback: number): number {\n if (value === undefined || !Number.isFinite(value) || value <= 0) return fallback\n return value\n}\n"],"mappings":";;;;;;;;;;AA2BA,IAAM,eAAe;AAErB,IAAM,+BAAe,IAAI,IAAI;CAAC;CAAQ;CAAQ;CAAQ;CAAY;AAAW,CAAC;;;;;;;AAQ9E,SAAgB,8BACd,UAA0C,CAAC,GAC7B;CACd,MAAM,WAAW;EACf,YAAY,SAAS,QAAQ,YAAY,GAAG;EAC5C,QAAQ,KAAK,IAAI,IAAK,KAAK,IAAI,GAAG,QAAQ,UAAU,GAAI,CAAC;EACzD,WAAW,SAAS,QAAQ,WAAW,IAAI;CAC7C;CACA,OAAO;EACL,MAAM;EACN,WAAW,YACT,QAAQ,yBAAyB,EAC/B,qBAAq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editor/core/extensions';
2
+ import { type FileTransitionPluginOptions } from './transition-options';
3
+ import './style.css';
4
+ export type { FileTransitionMode, FileTransitionPluginOptions } from './transition-options';
5
+ /**
6
+ * File-open "writes itself" animation. Including this plugin turns the effect on;
7
+ * removing it turns it off. There is no command, setting, or UI — presence is the switch.
8
+ */
9
+ export declare function createFileTransitionPlugin(options?: FileTransitionPluginOptions): EditorPlugin;
10
+ //# sourceMappingURL=file-transition.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-transition.d.ts","sourceRoot":"","sources":["../src/file-transition.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EAKb,MAAM,mCAAmC,CAAA;AAa1C,OAAO,EAEL,KAAK,2BAA2B,EAEjC,MAAM,sBAAsB,CAAA;AAG7B,OAAO,aAAa,CAAA;AAEpB,YAAY,EAAE,kBAAkB,EAAE,2BAA2B,EAAE,MAAM,sBAAsB,CAAA;AAW3F;;;GAGG;AACH,wBAAgB,0BAA0B,CACxC,OAAO,GAAE,2BAAgC,GACxC,YAAY,CAad"}