@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
|
@@ -9,10 +9,9 @@ import { atom } from "jotai";
|
|
|
9
9
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
10
10
|
import { MockRequestClient } from "@/__mocks__/requests";
|
|
11
11
|
import { cellId } from "@/__tests__/branded";
|
|
12
|
-
import
|
|
13
|
-
import { getNotebook } from "@/core/cells/cells";
|
|
12
|
+
import { initialNotebookState, notebookAtom } from "@/core/cells/cells";
|
|
14
13
|
import type { CellId } from "@/core/cells/ids";
|
|
15
|
-
import {
|
|
14
|
+
import { createCell } from "@/core/cells/types";
|
|
16
15
|
import { getResolvedMarimoConfig } from "@/core/config/config";
|
|
17
16
|
import type { ConnectionName } from "@/core/datasets/engines";
|
|
18
17
|
import { OverridingHotkeyProvider } from "@/core/hotkeys/hotkeys";
|
|
@@ -20,7 +19,7 @@ import { requestClientAtom } from "@/core/network/requests";
|
|
|
20
19
|
import type { MarimoConfig } from "@/core/network/types";
|
|
21
20
|
import { store } from "@/core/state/jotai";
|
|
22
21
|
import { cellIdState } from "../config/extension";
|
|
23
|
-
import { formatAll,
|
|
22
|
+
import { formatAll, formatCells, formatSQL } from "../format";
|
|
24
23
|
import {
|
|
25
24
|
adaptiveLanguageConfiguration,
|
|
26
25
|
switchLanguage,
|
|
@@ -28,14 +27,13 @@ import {
|
|
|
28
27
|
|
|
29
28
|
const mockRequestClient = MockRequestClient.create();
|
|
30
29
|
|
|
31
|
-
vi.
|
|
32
|
-
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
})
|
|
38
|
-
|
|
30
|
+
const { writtenCodes } = vi.hoisted(() => ({ writtenCodes: vi.fn() }));
|
|
31
|
+
vi.mock("@/core/cells/actions", async () => {
|
|
32
|
+
const { atom } = await import("jotai");
|
|
33
|
+
return {
|
|
34
|
+
setCellCodeAtom: atom(null, (_get, _set, value) => writtenCodes(value)),
|
|
35
|
+
};
|
|
36
|
+
});
|
|
39
37
|
vi.mock("@/core/config/config", () => ({
|
|
40
38
|
getResolvedMarimoConfig: vi.fn(),
|
|
41
39
|
resolvedMarimoConfigAtom: atom({
|
|
@@ -94,88 +92,112 @@ afterEach(() => {
|
|
|
94
92
|
});
|
|
95
93
|
|
|
96
94
|
describe("format", () => {
|
|
97
|
-
describe("
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
95
|
+
describe("formatCells", () => {
|
|
96
|
+
const mounted = cellId("mounted");
|
|
97
|
+
const dormant = cellId("dormant");
|
|
98
|
+
|
|
99
|
+
function setNotebook(view: EditorView) {
|
|
100
|
+
store.set(notebookAtom, {
|
|
101
|
+
...initialNotebookState(),
|
|
102
|
+
cellIds: [mounted, dormant],
|
|
103
|
+
cellData: {
|
|
104
|
+
[mounted]: createCell({ id: mounted, code: "import numpy as np" }),
|
|
105
|
+
[dormant]: createCell({
|
|
106
|
+
id: dormant,
|
|
107
|
+
code: "import pandas as pd",
|
|
108
|
+
}),
|
|
109
|
+
},
|
|
110
|
+
cellHandles: {
|
|
111
|
+
[mounted]: { current: { editorView: view } },
|
|
112
|
+
[dormant]: { current: { editorView: null } },
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
}
|
|
108
116
|
|
|
109
|
-
|
|
117
|
+
function formatted() {
|
|
118
|
+
return {
|
|
110
119
|
codes: {
|
|
111
|
-
[
|
|
112
|
-
[
|
|
120
|
+
[mounted]: "import numpy as np",
|
|
121
|
+
[dormant]: "import pandas as pd",
|
|
113
122
|
},
|
|
114
|
-
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
afterEach(() => {
|
|
127
|
+
store.set(notebookAtom, initialNotebookState());
|
|
128
|
+
});
|
|
115
129
|
|
|
130
|
+
it("formats an editor in place and a cell without one through the document", async () => {
|
|
131
|
+
const view = createEditor("import numpy as np", mounted);
|
|
132
|
+
setNotebook(view);
|
|
133
|
+
mockRequestClient.sendFormat.mockResolvedValueOnce(formatted());
|
|
116
134
|
vi.mocked(getResolvedMarimoConfig).mockReturnValueOnce(mockConfig);
|
|
117
135
|
|
|
118
|
-
await
|
|
136
|
+
await formatCells([mounted, dormant]);
|
|
119
137
|
|
|
120
138
|
expect(mockRequestClient.sendFormat).toHaveBeenCalledWith({
|
|
121
139
|
codes: {
|
|
122
|
-
[
|
|
123
|
-
[
|
|
140
|
+
[mounted]: "import numpy as np",
|
|
141
|
+
[dormant]: "import pandas as pd",
|
|
124
142
|
},
|
|
125
143
|
lineLength: 88,
|
|
126
144
|
});
|
|
127
|
-
|
|
128
|
-
expect(
|
|
129
|
-
|
|
145
|
+
expect(view.state.doc.toString()).toBe("import numpy as np");
|
|
146
|
+
expect(writtenCodes.mock.calls).toEqual([
|
|
147
|
+
[{ cellId: dormant, code: "import pandas as pd" }],
|
|
148
|
+
]);
|
|
130
149
|
});
|
|
131
150
|
|
|
132
|
-
it("
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
const views = {
|
|
136
|
-
[cid]: createEditor(originalCode, cid),
|
|
137
|
-
};
|
|
138
|
-
|
|
151
|
+
it("leaves cells that formatting did not change", async () => {
|
|
152
|
+
const view = createEditor("import numpy as np", mounted);
|
|
153
|
+
setNotebook(view);
|
|
139
154
|
mockRequestClient.sendFormat.mockResolvedValueOnce({
|
|
140
155
|
codes: {
|
|
141
|
-
[
|
|
156
|
+
[mounted]: "import numpy as np",
|
|
157
|
+
[dormant]: "import pandas as pd",
|
|
142
158
|
},
|
|
143
159
|
});
|
|
144
|
-
|
|
145
160
|
vi.mocked(getResolvedMarimoConfig).mockReturnValueOnce(mockConfig);
|
|
146
161
|
|
|
147
|
-
await
|
|
162
|
+
await formatCells([mounted, dormant]);
|
|
148
163
|
|
|
149
|
-
expect(
|
|
164
|
+
expect(view.state.doc.toString()).toBe("import numpy as np");
|
|
165
|
+
expect(writtenCodes).not.toHaveBeenCalled();
|
|
150
166
|
});
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
describe("formatAll", () => {
|
|
154
|
-
it("should format all cells in notebook", async () => {
|
|
155
|
-
const cid1 = cellId("1");
|
|
156
|
-
const cid2 = cellId("2");
|
|
157
|
-
const views = {
|
|
158
|
-
[cid1]: createEditor("import numpy as np", cid1),
|
|
159
|
-
[cid2]: createEditor("import pandas as pd", cid2),
|
|
160
|
-
};
|
|
161
167
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
168
|
+
it("skips a cell edited while the server was formatting", async () => {
|
|
169
|
+
const view = createEditor("import numpy as np", mounted);
|
|
170
|
+
setNotebook(view);
|
|
171
|
+
mockRequestClient.sendFormat.mockImplementationOnce(async () => {
|
|
172
|
+
const notebook = store.get(notebookAtom);
|
|
173
|
+
store.set(notebookAtom, {
|
|
174
|
+
...notebook,
|
|
175
|
+
cellData: {
|
|
176
|
+
...notebook.cellData,
|
|
177
|
+
[dormant]: createCell({ id: dormant, code: "x = 1" }),
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
return formatted();
|
|
169
181
|
});
|
|
182
|
+
vi.mocked(getResolvedMarimoConfig).mockReturnValueOnce(mockConfig);
|
|
183
|
+
|
|
184
|
+
await formatCells([mounted, dormant]);
|
|
185
|
+
|
|
186
|
+
expect(view.state.doc.toString()).toBe("import numpy as np");
|
|
187
|
+
expect(writtenCodes).not.toHaveBeenCalled();
|
|
188
|
+
});
|
|
170
189
|
|
|
190
|
+
it("formats every cell of the notebook with formatAll", async () => {
|
|
191
|
+
setNotebook(createEditor("import numpy as np", mounted));
|
|
192
|
+
mockRequestClient.sendFormat.mockResolvedValueOnce(formatted());
|
|
171
193
|
vi.mocked(getResolvedMarimoConfig).mockReturnValueOnce(mockConfig);
|
|
172
194
|
|
|
173
195
|
await formatAll();
|
|
174
196
|
|
|
175
197
|
expect(mockRequestClient.sendFormat).toHaveBeenCalledWith({
|
|
176
198
|
codes: {
|
|
177
|
-
[
|
|
178
|
-
[
|
|
199
|
+
[mounted]: "import numpy as np",
|
|
200
|
+
[dormant]: "import pandas as pd",
|
|
179
201
|
},
|
|
180
202
|
lineLength: 88,
|
|
181
203
|
});
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
import { EditorView } from "@codemirror/view";
|
|
3
|
+
import { act, cleanup, render } from "@testing-library/react";
|
|
4
|
+
import { Activity, StrictMode, createRef } from "react";
|
|
5
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
6
|
+
import { mockIntersectionObservers } from "@/__mocks__/intersection-observer";
|
|
7
|
+
import { editorMountScheduler } from "../editor-mount-scheduler";
|
|
8
|
+
import { useLazyEditorMount } from "../use-lazy-editor-mount";
|
|
9
|
+
|
|
10
|
+
const { tasks } = vi.hoisted(() => ({ tasks: [] as (() => void)[] }));
|
|
11
|
+
vi.mock("@/utils/schedule-task", () => ({
|
|
12
|
+
scheduleTask: (task: () => void) => tasks.push(task),
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
function flush() {
|
|
16
|
+
act(() => {
|
|
17
|
+
while (tasks.length) {
|
|
18
|
+
tasks.shift()?.();
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
cleanup();
|
|
25
|
+
flush();
|
|
26
|
+
vi.unstubAllGlobals();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("uses current code, builds once, and stops observing after mounting in StrictMode", () => {
|
|
30
|
+
const observers = mockIntersectionObservers();
|
|
31
|
+
const editorViewRef = createRef<EditorView>();
|
|
32
|
+
const build = vi.fn((code: string) => {
|
|
33
|
+
editorViewRef.current = new EditorView({ doc: code });
|
|
34
|
+
});
|
|
35
|
+
function Cell({ code }: { code: string }) {
|
|
36
|
+
const { elementRef, mounted } = useLazyEditorMount({
|
|
37
|
+
cellId: "cell",
|
|
38
|
+
editorViewRef,
|
|
39
|
+
build: () => build(code),
|
|
40
|
+
hidden: false,
|
|
41
|
+
});
|
|
42
|
+
return <div ref={elementRef}>{mounted ? "editor" : "static"}</div>;
|
|
43
|
+
}
|
|
44
|
+
const screen = render(
|
|
45
|
+
<StrictMode>
|
|
46
|
+
<Cell code="old" />
|
|
47
|
+
</StrictMode>,
|
|
48
|
+
);
|
|
49
|
+
screen.rerender(
|
|
50
|
+
<StrictMode>
|
|
51
|
+
<Cell code="new" />
|
|
52
|
+
</StrictMode>,
|
|
53
|
+
);
|
|
54
|
+
expect(build).not.toHaveBeenCalled();
|
|
55
|
+
const observer = observers.at(-1)!;
|
|
56
|
+
act(() => observer.emit(screen.container.firstElementChild!, true));
|
|
57
|
+
flush();
|
|
58
|
+
expect(build).toHaveBeenCalledExactlyOnceWith("new");
|
|
59
|
+
expect(screen.container.textContent).toBe("editor");
|
|
60
|
+
expect(observer.disconnect).toHaveBeenCalledTimes(1);
|
|
61
|
+
act(() => editorMountScheduler.promote("cell"));
|
|
62
|
+
expect(build).toHaveBeenCalledTimes(1);
|
|
63
|
+
const destroy = vi.spyOn(editorViewRef.current!, "destroy");
|
|
64
|
+
screen.unmount();
|
|
65
|
+
expect(destroy).toHaveBeenCalledTimes(1);
|
|
66
|
+
expect(editorViewRef.current).toBeNull();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("does not build hidden code; cancels queued work on hide and unmount", () => {
|
|
70
|
+
const observers = mockIntersectionObservers();
|
|
71
|
+
const editorViewRef = createRef<EditorView>();
|
|
72
|
+
const build = vi.fn(() => {
|
|
73
|
+
editorViewRef.current = new EditorView();
|
|
74
|
+
});
|
|
75
|
+
function Cell({ hidden }: { hidden: boolean }) {
|
|
76
|
+
const { elementRef } = useLazyEditorMount({
|
|
77
|
+
cellId: "cell",
|
|
78
|
+
editorViewRef,
|
|
79
|
+
build,
|
|
80
|
+
hidden,
|
|
81
|
+
});
|
|
82
|
+
return <div ref={elementRef} />;
|
|
83
|
+
}
|
|
84
|
+
const screen = render(<Cell hidden={true} />);
|
|
85
|
+
expect(observers).toHaveLength(0);
|
|
86
|
+
flush();
|
|
87
|
+
expect(build).not.toHaveBeenCalled();
|
|
88
|
+
screen.rerender(<Cell hidden={false} />);
|
|
89
|
+
act(() => observers[0].emit(screen.container.firstElementChild!, true));
|
|
90
|
+
screen.rerender(<Cell hidden={true} />);
|
|
91
|
+
flush();
|
|
92
|
+
expect(build).not.toHaveBeenCalled();
|
|
93
|
+
act(() => editorMountScheduler.promote("cell"));
|
|
94
|
+
expect(build).toHaveBeenCalledTimes(1);
|
|
95
|
+
screen.unmount();
|
|
96
|
+
|
|
97
|
+
const pending = render(<Cell hidden={false} />);
|
|
98
|
+
act(() => observers.at(-1)?.emit(pending.container.firstElementChild!, true));
|
|
99
|
+
pending.unmount();
|
|
100
|
+
flush();
|
|
101
|
+
act(() => editorMountScheduler.promote("cell"));
|
|
102
|
+
expect(build).toHaveBeenCalledTimes(1);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("destroys and rebuilds with current code across Activity hide/show", () => {
|
|
106
|
+
mockIntersectionObservers();
|
|
107
|
+
const editorViewRef = createRef<EditorView>();
|
|
108
|
+
const build = vi.fn((code: string) => {
|
|
109
|
+
editorViewRef.current = new EditorView({ doc: code });
|
|
110
|
+
});
|
|
111
|
+
function Cell({ code }: { code: string }) {
|
|
112
|
+
const { elementRef } = useLazyEditorMount({
|
|
113
|
+
cellId: "activity",
|
|
114
|
+
editorViewRef,
|
|
115
|
+
build: () => build(code),
|
|
116
|
+
hidden: false,
|
|
117
|
+
});
|
|
118
|
+
return <div ref={elementRef} />;
|
|
119
|
+
}
|
|
120
|
+
const screen = render(
|
|
121
|
+
<Activity mode="visible">
|
|
122
|
+
<Cell code="first" />
|
|
123
|
+
</Activity>,
|
|
124
|
+
);
|
|
125
|
+
act(() => editorMountScheduler.promote("activity"));
|
|
126
|
+
const first = editorViewRef.current!;
|
|
127
|
+
const destroy = vi.spyOn(first, "destroy");
|
|
128
|
+
screen.rerender(
|
|
129
|
+
<Activity mode="hidden">
|
|
130
|
+
<Cell code="second" />
|
|
131
|
+
</Activity>,
|
|
132
|
+
);
|
|
133
|
+
expect(destroy).toHaveBeenCalledTimes(1);
|
|
134
|
+
expect(editorViewRef.current).toBeNull();
|
|
135
|
+
act(() => editorMountScheduler.promote("activity"));
|
|
136
|
+
expect(build).toHaveBeenCalledTimes(1);
|
|
137
|
+
screen.rerender(
|
|
138
|
+
<Activity mode="visible">
|
|
139
|
+
<Cell code="second" />
|
|
140
|
+
</Activity>,
|
|
141
|
+
);
|
|
142
|
+
act(() => editorMountScheduler.promote("activity"));
|
|
143
|
+
expect(build.mock.calls).toEqual([["first"], ["second"]]);
|
|
144
|
+
expect(editorViewRef.current).not.toBe(first);
|
|
145
|
+
expect(editorViewRef.current?.state.doc.toString()).toBe("second");
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("mounts only the three visible editors in a 500-cell notebook", () => {
|
|
149
|
+
const observers = mockIntersectionObservers();
|
|
150
|
+
const views: EditorView[] = [];
|
|
151
|
+
const refs = Array.from({ length: 500 }, () => createRef<EditorView>());
|
|
152
|
+
function Cell({ index }: { index: number }) {
|
|
153
|
+
const { elementRef } = useLazyEditorMount({
|
|
154
|
+
cellId: String(index),
|
|
155
|
+
editorViewRef: refs[index],
|
|
156
|
+
hidden: index % 2 === 0,
|
|
157
|
+
build: () => {
|
|
158
|
+
const view = new EditorView();
|
|
159
|
+
refs[index].current = view;
|
|
160
|
+
views.push(view);
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
return <div ref={elementRef} data-cell={index} />;
|
|
164
|
+
}
|
|
165
|
+
const screen = render(
|
|
166
|
+
<>
|
|
167
|
+
{refs.map((_, index) => (
|
|
168
|
+
<Cell key={index} index={index} />
|
|
169
|
+
))}
|
|
170
|
+
</>,
|
|
171
|
+
);
|
|
172
|
+
expect(observers).toHaveLength(1);
|
|
173
|
+
expect(observers[0].observe).toHaveBeenCalledTimes(250);
|
|
174
|
+
expect(views).toHaveLength(0);
|
|
175
|
+
act(() => {
|
|
176
|
+
for (const index of [1, 3, 5]) {
|
|
177
|
+
observers[0].emit(
|
|
178
|
+
screen.container.querySelector(`[data-cell="${index}"]`)!,
|
|
179
|
+
true,
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
flush();
|
|
184
|
+
expect(views).toHaveLength(3);
|
|
185
|
+
const destroys = views.map((view) => vi.spyOn(view, "destroy"));
|
|
186
|
+
screen.unmount();
|
|
187
|
+
flush();
|
|
188
|
+
expect(observers[0].disconnect).toHaveBeenCalledTimes(1);
|
|
189
|
+
for (const destroy of destroys) {
|
|
190
|
+
expect(destroy).toHaveBeenCalledTimes(1);
|
|
191
|
+
}
|
|
192
|
+
expect(refs.every((ref) => ref.current === null)).toBe(true);
|
|
193
|
+
});
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
import {
|
|
3
|
+
type ChangeSpec,
|
|
4
|
+
EditorState,
|
|
5
|
+
type StateEffect,
|
|
6
|
+
} from "@codemirror/state";
|
|
7
|
+
import type { EditorView } from "@codemirror/view";
|
|
8
|
+
import { setCellCodeAtom } from "@/core/cells/actions";
|
|
9
|
+
import {
|
|
10
|
+
ensureCellEditorView,
|
|
11
|
+
getNotebook,
|
|
12
|
+
type NotebookState,
|
|
13
|
+
} from "@/core/cells/cells";
|
|
14
|
+
import type { CellId } from "@/core/cells/ids";
|
|
15
|
+
import { store } from "@/core/state/jotai";
|
|
16
|
+
import {
|
|
17
|
+
getCellCode,
|
|
18
|
+
parseCellCode,
|
|
19
|
+
prepareCellCodeUpdate,
|
|
20
|
+
} from "./language/cell-code";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A cell's text, whether or not it has an editor. Operations across the
|
|
24
|
+
* notebook read and write documents so offscreen cells never need to build one.
|
|
25
|
+
*/
|
|
26
|
+
export interface CellDocument {
|
|
27
|
+
cellId: CellId;
|
|
28
|
+
/** The mounted editor, if the cell has one. */
|
|
29
|
+
view: EditorView | null;
|
|
30
|
+
/** The editor's state, or the state an editor would start from. */
|
|
31
|
+
state: EditorState;
|
|
32
|
+
/** The Python source the notebook stores for this cell. */
|
|
33
|
+
code: string;
|
|
34
|
+
/** Build the editor now. Reserved for explicit navigation or editing. */
|
|
35
|
+
ensureView: () => EditorView | undefined;
|
|
36
|
+
/** Apply changes to `state` and write the result back to the cell. */
|
|
37
|
+
applyChanges: (changes: ChangeSpec, userEvent: string) => void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type ParsedCode = ReturnType<typeof parseCellCode>;
|
|
41
|
+
|
|
42
|
+
const parsedByCell = new Map<CellId, { code: string; parsed: ParsedCode }>();
|
|
43
|
+
|
|
44
|
+
function parsedCodeOf(cellId: CellId, code: string): ParsedCode {
|
|
45
|
+
const cached = parsedByCell.get(cellId);
|
|
46
|
+
if (cached?.code === code) {
|
|
47
|
+
return cached.parsed;
|
|
48
|
+
}
|
|
49
|
+
const parsed = parseCellCode(code);
|
|
50
|
+
parsedByCell.set(cellId, { code, parsed });
|
|
51
|
+
return parsed;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The documents of cells on the page, in notebook order. */
|
|
55
|
+
export function getCellDocuments(): CellDocument[] {
|
|
56
|
+
const { cellIds, cellData, cellHandles } = getNotebook();
|
|
57
|
+
const present = new Set(cellIds);
|
|
58
|
+
for (const cellId of parsedByCell.keys()) {
|
|
59
|
+
if (!present.has(cellId)) {
|
|
60
|
+
parsedByCell.delete(cellId);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const documents: CellDocument[] = [];
|
|
65
|
+
for (const cellId of cellIds) {
|
|
66
|
+
const handle = cellHandles[cellId]?.current;
|
|
67
|
+
// A cell that is not on the page, such as one in a collapsed section, has
|
|
68
|
+
// no editor to show a result in.
|
|
69
|
+
if (!handle) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const view = handle.editorView;
|
|
73
|
+
const source = cellData[cellId].code;
|
|
74
|
+
const { adapter, text, metadata } = parsedCodeOf(cellId, source);
|
|
75
|
+
const state = view?.state ?? EditorState.create({ doc: text });
|
|
76
|
+
documents.push({
|
|
77
|
+
cellId,
|
|
78
|
+
view,
|
|
79
|
+
state,
|
|
80
|
+
code: view ? getCellCode(view.state) : source,
|
|
81
|
+
ensureView: () => ensureCellEditorView(cellId),
|
|
82
|
+
applyChanges: (changes, userEvent) => {
|
|
83
|
+
if (view) {
|
|
84
|
+
view.dispatch({ changes, userEvent });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const nextText = state.update({ changes }).state.doc.toString();
|
|
88
|
+
writeCellCode(cellId, adapter.transformOut(nextText, metadata)[0]);
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return documents;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A mounted editor's code is ahead of the notebook store while the user types. */
|
|
96
|
+
export function getCellPythonCode(
|
|
97
|
+
cellId: CellId,
|
|
98
|
+
notebook: NotebookState = getNotebook(),
|
|
99
|
+
): string {
|
|
100
|
+
const view = notebook.cellHandles[cellId]?.current?.editorView;
|
|
101
|
+
return view
|
|
102
|
+
? getCellCode(view.state)
|
|
103
|
+
: (notebook.cellData[cellId]?.code ?? "");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Write a cell's Python source. A mounted editor takes the update as an edit
|
|
108
|
+
* so selection and history survive; otherwise the notebook document does.
|
|
109
|
+
*/
|
|
110
|
+
export function writeCellCode(
|
|
111
|
+
cellId: CellId,
|
|
112
|
+
code: string,
|
|
113
|
+
options: {
|
|
114
|
+
userEvent?: string;
|
|
115
|
+
effects?: StateEffect<unknown>[];
|
|
116
|
+
/** Let the language follow the new code, as for a freshly loaded cell. */
|
|
117
|
+
initialize?: boolean;
|
|
118
|
+
} = {},
|
|
119
|
+
): void {
|
|
120
|
+
const { userEvent, effects, initialize } = options;
|
|
121
|
+
const view = getNotebook().cellHandles[cellId]?.current?.editorView;
|
|
122
|
+
if (!view) {
|
|
123
|
+
if (getNotebook().cellData[cellId]?.code !== code) {
|
|
124
|
+
store.set(setCellCodeAtom, { cellId, code });
|
|
125
|
+
}
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const update = prepareCellCodeUpdate(view.state, code, { initialize });
|
|
129
|
+
if (update) {
|
|
130
|
+
view.dispatch(update, { userEvent, effects });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -7,24 +7,13 @@ import { store } from "@/core/state/jotai";
|
|
|
7
7
|
import { variablesAtom } from "@/core/variables/state";
|
|
8
8
|
import type { Variables } from "@/core/variables/types";
|
|
9
9
|
import { Objects } from "@/utils/objects";
|
|
10
|
-
import {
|
|
10
|
+
import { getCellPythonCode } from "../cell-documents";
|
|
11
11
|
|
|
12
12
|
export const notebookCellCodes = atom((get) => {
|
|
13
13
|
const notebook = get(notebookAtom);
|
|
14
|
-
|
|
15
|
-
notebook.cellIds.map((id) =>
|
|
16
|
-
// A cell editor mounts progressively, so the notebook store is the only
|
|
17
|
-
// complete source of code until every editor is built.
|
|
18
|
-
const editorView = notebook.cellHandles[id]?.current?.editorViewOrNull;
|
|
19
|
-
return [
|
|
20
|
-
id,
|
|
21
|
-
editorView
|
|
22
|
-
? getEditorCodeAsPython(editorView)
|
|
23
|
-
: (notebook.cellData[id]?.code ?? ""),
|
|
24
|
-
];
|
|
25
|
-
}),
|
|
14
|
+
return Objects.fromEntries(
|
|
15
|
+
notebook.cellIds.map((id) => [id, getCellPythonCode(id, notebook)]),
|
|
26
16
|
);
|
|
27
|
-
return codes;
|
|
28
17
|
});
|
|
29
18
|
|
|
30
19
|
export function getCodes(otherCode: string) {
|