@marimo-team/islands 0.26.1-dev27 → 0.26.1-dev29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{ErrorBoundary-zpe6C93k.js → ErrorBoundary-CPcZlpvU.js} +1 -1
- package/dist/{chat-ui-DOvIzlMA.js → chat-ui-CWmSnBs8.js} +899 -899
- package/dist/{code-visibility-BtvMUvQ6.js → code-visibility-CHvnQsso.js} +2 -2
- package/dist/{common-B46KECgx.js → common-9OlCmjdr.js} +6565 -6565
- package/dist/{config-DL9e-OWE.js → config-1ku0tAiU.js} +27 -28
- package/dist/{data-grid-overlay-editor-CLCJ44fs.js → data-grid-overlay-editor-D6x_MBSD.js} +1 -1
- package/dist/{dropdown-menu-DYN2RV5d.js → dropdown-menu-D65ZUqka.js} +1983 -1983
- package/dist/{error-entries-CncQR572.js → error-entries-DFHEq_SO.js} +515 -519
- package/dist/{feedback-button-CnyHlwaY.js → feedback-button-CodkZ3hF.js} +19 -19
- package/dist/{glide-data-editor-C3znsea0.js → glide-data-editor-BZxvtSp-.js} +35 -35
- package/dist/{globals-DHoGn4Qt.js → globals-DFf-tSyE.js} +1 -1
- package/dist/{html-to-image-BU7H-AM1.js → html-to-image-D6Gtk5ik.js} +24005 -23992
- package/dist/main.js +864 -864
- package/dist/{number-overlay-editor-DwK5S3_f.js → number-overlay-editor-CfwFMv_U.js} +1 -1
- package/dist/{process-output-xJm8T5-E.js → process-output-ZBdibqlG.js} +2 -2
- package/dist/{reveal-component-Dsp5kSt3.js → reveal-component-CDfNuSFv.js} +3 -3
- package/dist/style.css +1 -1
- package/dist/{vega-component-DpTyTrz3.js → vega-component-D61CTW1X.js} +1 -1
- package/package.json +1 -1
- package/src/__mocks__/intersection-observer.ts +45 -0
- package/src/components/databases/engine-variable.tsx +2 -2
- package/src/components/dependency-graph/panels.tsx +2 -2
- package/src/components/editor/actions/useCellActionButton.tsx +2 -8
- package/src/components/editor/cell/code/__tests__/code-placeholder.test.tsx +40 -0
- package/src/components/editor/cell/code/cell-editor.tsx +41 -104
- package/src/components/editor/cell/code/code-placeholder.css +61 -11
- package/src/components/editor/cell/code/code-placeholder.tsx +52 -29
- package/src/components/editor/cell/useSplitCell.tsx +2 -2
- package/src/components/editor/chrome/wrapper/__tests__/pending-ai-cells.test.tsx +6 -10
- package/src/components/editor/errors/auto-fix.tsx +8 -7
- package/src/components/editor/navigation/__tests__/navigation.test.ts +3 -12
- package/src/components/editor/navigation/focus-utils.ts +20 -8
- package/src/components/editor/navigation/multi-cell-action-toolbar.tsx +3 -11
- package/src/components/editor/navigation/navigation.ts +2 -2
- package/src/components/editor/notebook-cell.tsx +15 -24
- package/src/components/editor/output/MarimoTracebackOutput.tsx +2 -2
- package/src/components/editor/renderers/vertical-layout/__tests__/useFocusFirstEditor.test.ts +6 -9
- package/src/components/editor/renderers/vertical-layout/useFocusFirstEditor.ts +2 -5
- package/src/components/find-replace/find-replace.tsx +3 -7
- package/src/components/variables/__tests__/variables-table.test.tsx +1 -2
- package/src/core/ai/__tests__/staged-cells.test.ts +6 -39
- package/src/core/ai/staged-cells.ts +2 -20
- package/src/core/ai/tools/__tests__/edit-notebook-tool.test.ts +15 -13
- package/src/core/ai/tools/edit-notebook-tool.ts +13 -8
- package/src/core/cells/__tests__/cell-editor-view.test.ts +44 -0
- package/src/core/cells/__tests__/cells.test.ts +0 -1
- package/src/core/cells/__tests__/focus.test.ts +1 -7
- package/src/core/cells/__tests__/harness.ts +0 -1
- package/src/core/cells/cells.ts +15 -10
- package/src/core/cells/focus.ts +1 -1
- package/src/core/cells/scrollCellIntoView.ts +4 -14
- package/src/core/cells/utils.ts +0 -13
- package/src/core/codemirror/__tests__/cell-documents.test.ts +79 -0
- package/src/core/codemirror/__tests__/editor-mount-scheduler.test.ts +75 -105
- package/src/core/codemirror/__tests__/format.test.ts +85 -63
- package/src/core/codemirror/__tests__/use-lazy-editor-mount.test.tsx +193 -0
- package/src/core/codemirror/cell-documents.ts +132 -0
- package/src/core/codemirror/copilot/__tests__/getCodes.test.ts +0 -1
- package/src/core/codemirror/copilot/getCodes.ts +3 -14
- package/src/core/codemirror/editor-mount-scheduler.ts +41 -48
- package/src/core/codemirror/extensions.ts +2 -2
- package/src/core/codemirror/find-replace/__tests__/navigate.test.ts +42 -16
- package/src/core/codemirror/find-replace/__tests__/search-highlight.test.ts +30 -0
- package/src/core/codemirror/find-replace/__tests__/unmounted-cells.test.ts +145 -0
- package/src/core/codemirror/find-replace/navigate.ts +88 -129
- package/src/core/codemirror/find-replace/search-highlight.ts +3 -1
- package/src/core/codemirror/find-replace/state.ts +7 -2
- package/src/core/codemirror/format.ts +25 -31
- package/src/core/codemirror/go-to-definition/__tests__/utils.test.ts +8 -9
- package/src/core/codemirror/language/cell-code.ts +7 -0
- package/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts +25 -0
- package/src/core/codemirror/lsp/notebook-lsp.ts +6 -16
- package/src/core/codemirror/use-lazy-editor-mount.ts +67 -0
- package/src/core/codemirror/utils.ts +2 -6
- package/src/utils/__tests__/shared-intersection-observer.test.ts +49 -0
- package/src/utils/shared-intersection-observer.ts +53 -0
|
@@ -7,63 +7,59 @@ import {
|
|
|
7
7
|
type BuildFn = () => void;
|
|
8
8
|
|
|
9
9
|
export interface EditorMountScheduler {
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Register a cell's editor build. It stays dormant until the cell is
|
|
12
|
+
* prioritized or promoted. Returns a disposer for this registration only.
|
|
13
|
+
*/
|
|
14
|
+
register(cellId: string, build: BuildFn): () => void;
|
|
15
|
+
/** Build a registered cell's editor synchronously, right now (e.g. on focus). */
|
|
13
16
|
promote(cellId: string): void;
|
|
14
17
|
/**
|
|
15
|
-
*
|
|
18
|
+
* Queue a registered cell to build on a future task, keeping the
|
|
16
19
|
* one-build-per-task pacing (e.g. when the cell scrolls into view).
|
|
17
20
|
*/
|
|
18
21
|
prioritize(cellId: string): void;
|
|
19
|
-
/** Drop a cell's
|
|
22
|
+
/** Drop a cell's queued build, leaving it registered (e.g. when it scrolls away). */
|
|
20
23
|
deprioritize(cellId: string): void;
|
|
21
|
-
/** Remove a cell's queued build entirely. */
|
|
22
|
-
cancel(cellId: string): void;
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* be prioritized (e.g. when they scroll into view) to jump ahead of the queue
|
|
30
|
-
* without breaking the one-build-per-macrotask pacing.
|
|
27
|
+
* Builds CodeMirror editors on demand. Registrations are dormant, so a large
|
|
28
|
+
* notebook only pays for the cells that are prioritized, one build per
|
|
29
|
+
* macrotask to keep the page responsive, or promoted by an explicit action.
|
|
31
30
|
*/
|
|
32
31
|
export function createEditorMountScheduler(
|
|
33
32
|
scheduleTask: ScheduleTaskFn = defaultScheduleTask,
|
|
34
33
|
): EditorMountScheduler {
|
|
35
|
-
const
|
|
36
|
-
const
|
|
34
|
+
const registrations = new Map<string, { build: BuildFn }>();
|
|
35
|
+
const prioritized = new Set<string>();
|
|
37
36
|
let isProcessingScheduled = false;
|
|
38
37
|
|
|
39
38
|
function scheduleProcessing(): void {
|
|
40
|
-
if (isProcessingScheduled ||
|
|
39
|
+
if (isProcessingScheduled || prioritized.size === 0) {
|
|
41
40
|
return;
|
|
42
41
|
}
|
|
43
42
|
isProcessingScheduled = true;
|
|
44
43
|
scheduleTask(processNext);
|
|
45
44
|
}
|
|
46
45
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
46
|
+
function unregister(cellId: string): void {
|
|
47
|
+
registrations.delete(cellId);
|
|
48
|
+
prioritized.delete(cellId);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function buildNow(cellId: string): void {
|
|
52
|
+
const registration = registrations.get(cellId);
|
|
53
|
+
unregister(cellId);
|
|
54
|
+
registration?.build();
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
function processNext(): void {
|
|
59
58
|
isProcessingScheduled = false;
|
|
60
59
|
try {
|
|
61
|
-
const
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
queue.delete(cellId);
|
|
65
|
-
priority.delete(cellId);
|
|
66
|
-
build?.();
|
|
60
|
+
const next = prioritized.values().next();
|
|
61
|
+
if (!next.done) {
|
|
62
|
+
buildNow(next.value);
|
|
67
63
|
}
|
|
68
64
|
} finally {
|
|
69
65
|
scheduleProcessing();
|
|
@@ -71,29 +67,26 @@ export function createEditorMountScheduler(
|
|
|
71
67
|
}
|
|
72
68
|
|
|
73
69
|
return {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
70
|
+
register(cellId, build) {
|
|
71
|
+
const registration = { build };
|
|
72
|
+
unregister(cellId);
|
|
73
|
+
registrations.set(cellId, registration);
|
|
74
|
+
return () => {
|
|
75
|
+
// An old effect's cleanup must not cancel a newer registration.
|
|
76
|
+
if (registrations.get(cellId) === registration) {
|
|
77
|
+
unregister(cellId);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
85
80
|
},
|
|
81
|
+
promote: buildNow,
|
|
86
82
|
prioritize(cellId) {
|
|
87
|
-
if (
|
|
88
|
-
|
|
83
|
+
if (registrations.has(cellId)) {
|
|
84
|
+
prioritized.add(cellId);
|
|
85
|
+
scheduleProcessing();
|
|
89
86
|
}
|
|
90
87
|
},
|
|
91
88
|
deprioritize(cellId) {
|
|
92
|
-
|
|
93
|
-
},
|
|
94
|
-
cancel(cellId) {
|
|
95
|
-
queue.delete(cellId);
|
|
96
|
-
priority.delete(cellId);
|
|
89
|
+
prioritized.delete(cellId);
|
|
97
90
|
},
|
|
98
91
|
};
|
|
99
92
|
}
|
|
@@ -4,7 +4,7 @@ import type { HotkeyProvider } from "@/core/hotkeys/hotkeys";
|
|
|
4
4
|
import { invariant } from "@/utils/invariant";
|
|
5
5
|
import { smartScrollIntoView } from "../../utils/scroll";
|
|
6
6
|
import { cellActionsState, cellIdState } from "./cells/state";
|
|
7
|
-
import {
|
|
7
|
+
import { formatCells, formattingChangeEffect } from "./format";
|
|
8
8
|
import {
|
|
9
9
|
getCurrentLanguageAdapter,
|
|
10
10
|
toggleToLanguage,
|
|
@@ -20,7 +20,7 @@ export function formatKeymapExtension(hotkeys: HotkeyProvider) {
|
|
|
20
20
|
preventDefault: true,
|
|
21
21
|
run: (ev) => {
|
|
22
22
|
const cellId = ev.state.facet(cellIdState);
|
|
23
|
-
|
|
23
|
+
formatCells([cellId]);
|
|
24
24
|
return true;
|
|
25
25
|
},
|
|
26
26
|
},
|
|
@@ -3,8 +3,10 @@
|
|
|
3
3
|
import { EditorState, Text } from "@codemirror/state";
|
|
4
4
|
import type { EditorView } from "@codemirror/view";
|
|
5
5
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
6
|
+
import { cellId } from "@/__tests__/branded";
|
|
6
7
|
import { store } from "@/core/state/jotai";
|
|
7
8
|
import { invariant } from "@/utils/invariant";
|
|
9
|
+
import { cellIdState } from "../../config/extension";
|
|
8
10
|
import {
|
|
9
11
|
findNext,
|
|
10
12
|
findPrev,
|
|
@@ -14,10 +16,24 @@ import {
|
|
|
14
16
|
} from "../navigate";
|
|
15
17
|
import { findReplaceAtom } from "../state";
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
const cell1 = cellId("cell-1");
|
|
20
|
+
const cell2 = cellId("cell-2");
|
|
21
|
+
|
|
22
|
+
// Present the test views as the mounted documents of two cells.
|
|
18
23
|
const mockGetAllEditorViews = vi.fn();
|
|
19
|
-
vi.
|
|
20
|
-
|
|
24
|
+
const { writeCellCode } = vi.hoisted(() => ({ writeCellCode: vi.fn() }));
|
|
25
|
+
vi.mock("../../cell-documents", () => ({
|
|
26
|
+
getCellDocuments: () =>
|
|
27
|
+
mockGetAllEditorViews().map((view: EditorView) => ({
|
|
28
|
+
cellId: view.state.facet(cellIdState),
|
|
29
|
+
view,
|
|
30
|
+
state: view.state,
|
|
31
|
+
code: view.state.doc.toString(),
|
|
32
|
+
ensureView: () => view,
|
|
33
|
+
applyChanges: (changes: unknown, userEvent: string) =>
|
|
34
|
+
view.dispatch({ changes, userEvent } as never),
|
|
35
|
+
})),
|
|
36
|
+
writeCellCode,
|
|
21
37
|
}));
|
|
22
38
|
|
|
23
39
|
/**
|
|
@@ -49,10 +65,12 @@ describe("navigate", () => {
|
|
|
49
65
|
// Create test editor views with different content
|
|
50
66
|
const state1 = EditorState.create({
|
|
51
67
|
doc: Text.of(["Hello world", "This is a test", "Hello again"]),
|
|
68
|
+
extensions: [cellIdState.of(cell1)],
|
|
52
69
|
});
|
|
53
70
|
|
|
54
71
|
const state2 = EditorState.create({
|
|
55
72
|
doc: Text.of(["Another hello", "Different content", "Hello there"]),
|
|
73
|
+
extensions: [cellIdState.of(cell2)],
|
|
56
74
|
});
|
|
57
75
|
|
|
58
76
|
// Create mock views that track dispatch calls
|
|
@@ -116,7 +134,7 @@ describe("navigate", () => {
|
|
|
116
134
|
|
|
117
135
|
// Check that state was updated
|
|
118
136
|
const state = store.get(findReplaceAtom);
|
|
119
|
-
expect(state.currentView?.
|
|
137
|
+
expect(state.currentView?.cellId).toBe(cell1);
|
|
120
138
|
expect(state.currentView?.range).toEqual({ from: 0, to: 5 });
|
|
121
139
|
});
|
|
122
140
|
|
|
@@ -136,7 +154,7 @@ describe("navigate", () => {
|
|
|
136
154
|
expect(view1.dispatch).toHaveBeenCalled();
|
|
137
155
|
|
|
138
156
|
const state = store.get(findReplaceAtom);
|
|
139
|
-
expect(state.currentView?.
|
|
157
|
+
expect(state.currentView?.cellId).toBeDefined();
|
|
140
158
|
});
|
|
141
159
|
|
|
142
160
|
it("should wrap around to beginning when reaching end", () => {
|
|
@@ -153,7 +171,7 @@ describe("navigate", () => {
|
|
|
153
171
|
invariant(result, "findNext should return a result");
|
|
154
172
|
// Should wrap back to view1
|
|
155
173
|
const state = store.get(findReplaceAtom);
|
|
156
|
-
expect(state.currentView?.
|
|
174
|
+
expect(state.currentView?.cellId).toBe(cell1);
|
|
157
175
|
});
|
|
158
176
|
|
|
159
177
|
it("should return false when no matches found", () => {
|
|
@@ -291,7 +309,7 @@ describe("navigate", () => {
|
|
|
291
309
|
invariant(result, "findNext should return a result");
|
|
292
310
|
// Should find match in view2 or wrap to view1
|
|
293
311
|
const state = store.get(findReplaceAtom);
|
|
294
|
-
expect([
|
|
312
|
+
expect([cell1, cell2]).toContain(state.currentView?.cellId);
|
|
295
313
|
});
|
|
296
314
|
|
|
297
315
|
it("should return false when no previous matches", () => {
|
|
@@ -329,10 +347,15 @@ describe("navigate", () => {
|
|
|
329
347
|
undoHandler();
|
|
330
348
|
}
|
|
331
349
|
// Should restore original content
|
|
332
|
-
expect(
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
}
|
|
350
|
+
expect(writeCellCode).toHaveBeenCalledWith(
|
|
351
|
+
cell1,
|
|
352
|
+
"Hello world\nThis is a test\nHello again",
|
|
353
|
+
{ userEvent: "input.replace.all" },
|
|
354
|
+
);
|
|
355
|
+
expect(writeCellCode).toHaveBeenCalledWith(
|
|
356
|
+
cell2,
|
|
357
|
+
"Another hello\nDifferent content\nHello there",
|
|
358
|
+
{ userEvent: "input.replace.all" },
|
|
336
359
|
);
|
|
337
360
|
});
|
|
338
361
|
|
|
@@ -340,7 +363,10 @@ describe("navigate", () => {
|
|
|
340
363
|
// Create a read-only state
|
|
341
364
|
const readOnlyState = EditorState.create({
|
|
342
365
|
doc: Text.of(["Hello world", "This is a test"]),
|
|
343
|
-
extensions: [
|
|
366
|
+
extensions: [
|
|
367
|
+
EditorState.readOnly.of(true),
|
|
368
|
+
cellIdState.of(cellId("cell-3")),
|
|
369
|
+
],
|
|
344
370
|
});
|
|
345
371
|
|
|
346
372
|
const readOnlyView = createMockView(readOnlyState);
|
|
@@ -497,8 +523,8 @@ describe("navigate", () => {
|
|
|
497
523
|
|
|
498
524
|
invariant(result, "getMatches should return a result");
|
|
499
525
|
// Check that positions are stored correctly
|
|
500
|
-
const view1Positions = result.position.get(
|
|
501
|
-
const view2Positions = result.position.get(
|
|
526
|
+
const view1Positions = result.position.get(cell1);
|
|
527
|
+
const view2Positions = result.position.get(cell2);
|
|
502
528
|
|
|
503
529
|
invariant(view1Positions, "Expected positions for the first view");
|
|
504
530
|
expect(view1Positions.size).toBeGreaterThan(0);
|
|
@@ -575,8 +601,8 @@ describe("navigate", () => {
|
|
|
575
601
|
const isDifferentMatch =
|
|
576
602
|
firstMatchRange?.from !== secondMatchRange?.from ||
|
|
577
603
|
firstMatchRange?.to !== secondMatchRange?.to ||
|
|
578
|
-
stateAfterFirst.currentView?.
|
|
579
|
-
stateAfterSecond.currentView?.
|
|
604
|
+
stateAfterFirst.currentView?.cellId !==
|
|
605
|
+
stateAfterSecond.currentView?.cellId;
|
|
580
606
|
|
|
581
607
|
expect(isDifferentMatch).toBe(true);
|
|
582
608
|
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
import { EditorState } from "@codemirror/state";
|
|
3
|
+
import { afterEach, expect, it } from "vitest";
|
|
4
|
+
import { store } from "@/core/state/jotai";
|
|
5
|
+
import { searchState } from "../search-highlight";
|
|
6
|
+
import { findReplaceAtom } from "../state";
|
|
7
|
+
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
store.set(findReplaceAtom, { type: "setIsOpen", isOpen: false });
|
|
10
|
+
store.set(findReplaceAtom, { type: "setFind", find: "" });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
function searchOfNewEditor() {
|
|
14
|
+
return EditorState.create({ extensions: [searchState] }).field(searchState)
|
|
15
|
+
.spec.search;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
it("seeds an editor built while the panel is open with the current search", () => {
|
|
19
|
+
store.set(findReplaceAtom, { type: "setFind", find: "needle" });
|
|
20
|
+
store.set(findReplaceAtom, { type: "setIsOpen", isOpen: true });
|
|
21
|
+
|
|
22
|
+
expect(searchOfNewEditor()).toBe("needle");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("does not highlight a previous search in an editor built after the panel closed", () => {
|
|
26
|
+
store.set(findReplaceAtom, { type: "setFind", find: "needle" });
|
|
27
|
+
store.set(findReplaceAtom, { type: "setIsOpen", isOpen: false });
|
|
28
|
+
|
|
29
|
+
expect(searchOfNewEditor()).toBe("");
|
|
30
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
import { EditorState } from "@codemirror/state";
|
|
3
|
+
import { EditorView } from "@codemirror/view";
|
|
4
|
+
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
|
5
|
+
import { cellId } from "@/__tests__/branded";
|
|
6
|
+
import type { CellHandle } from "@/components/editor/notebook-cell";
|
|
7
|
+
import { initialNotebookState, notebookAtom } from "@/core/cells/cells";
|
|
8
|
+
import type { CellId } from "@/core/cells/ids";
|
|
9
|
+
import { createCell } from "@/core/cells/types";
|
|
10
|
+
import { store } from "@/core/state/jotai";
|
|
11
|
+
import { cellIdState } from "../../config/extension";
|
|
12
|
+
import { editorMountScheduler } from "../../editor-mount-scheduler";
|
|
13
|
+
import { findNext, getMatches, replaceAll } from "../navigate";
|
|
14
|
+
import { findReplaceAtom } from "../state";
|
|
15
|
+
|
|
16
|
+
const { writes } = vi.hoisted(() => ({ writes: vi.fn() }));
|
|
17
|
+
vi.mock("@/core/cells/actions", async () => {
|
|
18
|
+
const { atom } = await import("jotai");
|
|
19
|
+
return {
|
|
20
|
+
setCellCodeAtom: atom(null, (_get, _set, update) => writes(update)),
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
/** Follow the document, as the real action does. */
|
|
25
|
+
function writeToNotebook({ cellId, code }: { cellId: CellId; code: string }) {
|
|
26
|
+
const notebook = store.get(notebookAtom);
|
|
27
|
+
store.set(notebookAtom, {
|
|
28
|
+
...notebook,
|
|
29
|
+
cellData: {
|
|
30
|
+
...notebook.cellData,
|
|
31
|
+
[cellId]: createCell({ id: cellId, code }),
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const withoutEditor: CellHandle = { editorView: null };
|
|
37
|
+
|
|
38
|
+
function setNotebook(cells: [CellId, string, CellHandle | null][]) {
|
|
39
|
+
store.set(notebookAtom, {
|
|
40
|
+
...initialNotebookState(),
|
|
41
|
+
cellIds: cells.map(([id]) => id),
|
|
42
|
+
cellData: Object.fromEntries(
|
|
43
|
+
cells.map(([id, code]) => [id, createCell({ id, code })]),
|
|
44
|
+
),
|
|
45
|
+
cellHandles: Object.fromEntries(
|
|
46
|
+
cells.map(([id, , handle]) => [id, { current: handle }]),
|
|
47
|
+
),
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
writes.mockImplementation(writeToNotebook);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
writes.mockReset();
|
|
57
|
+
store.set(notebookAtom, initialNotebookState());
|
|
58
|
+
store.set(findReplaceAtom, { type: "clearCurrentView" });
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("counts matches in cells without editors and builds only the navigation target", () => {
|
|
62
|
+
const [a, b] = [cellId("a"), cellId("b")];
|
|
63
|
+
setNotebook([
|
|
64
|
+
[a, "x = 1", withoutEditor],
|
|
65
|
+
[b, 'mo.md(r"""needle""")', withoutEditor],
|
|
66
|
+
]);
|
|
67
|
+
const view = new EditorView({ state: EditorState.create({ doc: "needle" }) });
|
|
68
|
+
const build = vi.fn(() => {
|
|
69
|
+
store.get(notebookAtom).cellHandles[b].current = {
|
|
70
|
+
editorView: view,
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
editorMountScheduler.register(b, build);
|
|
74
|
+
store.set(findReplaceAtom, { type: "setFind", find: "needle" });
|
|
75
|
+
|
|
76
|
+
expect(getMatches()).toMatchObject({ count: 1 });
|
|
77
|
+
expect(build).not.toHaveBeenCalled();
|
|
78
|
+
expect(findNext()).toMatchObject({ from: 0, to: 6 });
|
|
79
|
+
expect(build).toHaveBeenCalledTimes(1);
|
|
80
|
+
view.destroy();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("moves past a match whose editor cannot be built", () => {
|
|
84
|
+
const [stuck, buildable] = [cellId("stuck"), cellId("buildable")];
|
|
85
|
+
setNotebook([
|
|
86
|
+
[stuck, "needle = 1", withoutEditor],
|
|
87
|
+
[buildable, "needle = 2", withoutEditor],
|
|
88
|
+
]);
|
|
89
|
+
const view = new EditorView({
|
|
90
|
+
state: EditorState.create({
|
|
91
|
+
doc: "needle = 2",
|
|
92
|
+
extensions: cellIdState.of(buildable),
|
|
93
|
+
}),
|
|
94
|
+
});
|
|
95
|
+
editorMountScheduler.register(buildable, () => {
|
|
96
|
+
store.get(notebookAtom).cellHandles[buildable].current = {
|
|
97
|
+
editorView: view,
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
store.set(findReplaceAtom, { type: "setFind", find: "needle" });
|
|
101
|
+
|
|
102
|
+
expect(findNext()).toMatchObject({ from: 0, to: 6 });
|
|
103
|
+
expect(store.get(findReplaceAtom).currentView?.cellId).toBe(buildable);
|
|
104
|
+
view.destroy();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("ignores cells that are not on the page", () => {
|
|
108
|
+
const [visible, folded] = [cellId("visible"), cellId("folded")];
|
|
109
|
+
setNotebook([
|
|
110
|
+
[visible, "needle = 1", withoutEditor],
|
|
111
|
+
[folded, "needle = 2", null],
|
|
112
|
+
]);
|
|
113
|
+
store.set(findReplaceAtom, { type: "setFind", find: "needle" });
|
|
114
|
+
store.set(findReplaceAtom, { type: "setReplace", replace: "found" });
|
|
115
|
+
|
|
116
|
+
expect(getMatches()).toMatchObject({ count: 1 });
|
|
117
|
+
replaceAll();
|
|
118
|
+
expect(writes).toHaveBeenCalledExactlyOnceWith({
|
|
119
|
+
cellId: visible,
|
|
120
|
+
code: "found = 1",
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("replaces and undoes matches in markdown without building its editor", () => {
|
|
125
|
+
const id = cellId("markdown");
|
|
126
|
+
const code = 'mo.md(r"""needle""")';
|
|
127
|
+
setNotebook([[id, code, withoutEditor]]);
|
|
128
|
+
const build = vi.fn();
|
|
129
|
+
const release = editorMountScheduler.register(id, build);
|
|
130
|
+
store.set(findReplaceAtom, { type: "setFind", find: "needle" });
|
|
131
|
+
store.set(findReplaceAtom, { type: "setReplace", replace: "updated" });
|
|
132
|
+
|
|
133
|
+
const undo = replaceAll();
|
|
134
|
+
expect(writes).toHaveBeenLastCalledWith({
|
|
135
|
+
cellId: id,
|
|
136
|
+
code: expect.stringContaining("updated"),
|
|
137
|
+
});
|
|
138
|
+
expect(typeof undo).toBe("function");
|
|
139
|
+
if (undo) {
|
|
140
|
+
undo();
|
|
141
|
+
}
|
|
142
|
+
expect(writes).toHaveBeenLastCalledWith({ cellId: id, code });
|
|
143
|
+
expect(build).not.toHaveBeenCalled();
|
|
144
|
+
release();
|
|
145
|
+
});
|