-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.tsx
180 lines (165 loc) · 5.46 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import merge from "lodash/merge.js";
import React, {
CSSProperties,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import { SqLinker, SqProject } from "@quri/squiggle-lang";
import { SquiggleOutput } from "../../lib/hooks/useSquiggle.js";
import { DynamicSquiggleViewer } from "../DynamicSquiggleViewer.js";
import {
PartialPlaygroundSettings,
defaultPlaygroundSettings,
type PlaygroundSettings,
} from "../PlaygroundSettings.js";
import { SquiggleViewerHandle } from "../SquiggleViewer/index.js";
import {
LeftPlaygroundPanel,
LeftPlaygroundPanelHandle,
} from "./LeftPlaygroundPanel/index.js";
import { ResizableTwoPanelLayout } from "./ResizableTwoPanelLayout.js";
export type ModelExport = {
variableName: string;
variableType: string;
title?: string;
docstring: string;
};
/*
* We don't support `project` or `continues` in the playground.
* First, because playground will support multi-file mode by itself.
* Second, because environment is configurable through playground settings and it should match the project.getEnvironment(), so this component owns the project to guarantee that.
*/
export type SquigglePlaygroundProps = {
/*
* Playground code is not reactive, because Codemirror editor is stateful and it would be hard/impossible to support code updates.
* For example, it's not clear what we could do with the cursor position or selection if this prop is changed.
* So updates to it are completely ignored.
*/
defaultCode?: string;
sourceId?: string;
linker?: SqLinker;
onCodeChange?(code: string): void;
onExportsChange?(exports: ModelExport[]): void;
/* When settings change */
onSettingsChange?(settings: PlaygroundSettings): void;
/* Height of the playground */
height?: CSSProperties["height"];
} & Pick<
Parameters<typeof LeftPlaygroundPanel>[0],
"renderExtraControls" | "renderExtraDropdownItems" | "renderExtraModal"
> &
PartialPlaygroundSettings;
// Left panel ref is used for local settings modal positioning in ItemSettingsMenu.tsx
type PlaygroundContextShape = {
getLeftPanelElement: () => HTMLDivElement | undefined;
};
export const PlaygroundContext = React.createContext<PlaygroundContextShape>({
getLeftPanelElement: () => undefined,
});
export const SquigglePlayground: React.FC<SquigglePlaygroundProps> = (
props
) => {
const {
defaultCode,
linker,
onCodeChange,
onExportsChange,
onSettingsChange,
renderExtraControls,
renderExtraDropdownItems,
renderExtraModal,
height = 500,
sourceId,
...defaultSettings
} = props;
// `settings` are owned by SquigglePlayground.
// This can cause some unnecessary renders (e.g. settings form), but most heavy playground subcomponents
// should rerender on settings changes (e.g. right panel), so that's fine.
const [settings, setSettings] = useState(
() =>
merge(
{},
defaultPlaygroundSettings,
Object.fromEntries(
Object.entries(defaultSettings).filter(([, v]) => v !== undefined)
)
) as PlaygroundSettings
);
const handleSettingsChange = useCallback(
(newSettings: PlaygroundSettings) => {
setSettings(newSettings);
onSettingsChange?.(newSettings);
},
[onSettingsChange]
);
const [project] = useState(() => {
// not reactive on `linker` changes; TODO?
return new SqProject({ linker });
});
useEffect(() => {
project.setEnvironment(settings.environment);
leftPanelRef.current?.invalidate();
}, [project, settings.environment]);
const [output, setOutput] = useState<{
output: SquiggleOutput | undefined;
isRunning: boolean;
}>({ output: undefined, isRunning: false });
useEffect(() => {
const _output = output.output?.output;
if (_output && _output.ok) {
const exports = _output.value.exports;
const _exports: ModelExport[] = exports.entries().map((e) => ({
variableName: e[0],
variableType: e[1].tag,
title: e[1].title(),
docstring: e[1].context?.docstring() || "",
}));
onExportsChange && onExportsChange(_exports);
} else {
onExportsChange && onExportsChange([]);
}
}, [output, onExportsChange]);
const leftPanelRef = useRef<LeftPlaygroundPanelHandle>(null);
const rightPanelRef = useRef<SquiggleViewerHandle>(null);
const getLeftPanelElement = useCallback(
() => leftPanelRef.current?.getLeftPanelElement() ?? undefined,
[]
);
const renderLeft = () => (
<LeftPlaygroundPanel
project={project}
defaultCode={defaultCode}
sourceId={sourceId}
onCodeChange={onCodeChange}
settings={settings}
onSettingsChange={handleSettingsChange}
onOutputChange={setOutput}
renderExtraControls={renderExtraControls}
renderExtraDropdownItems={renderExtraDropdownItems}
renderExtraModal={renderExtraModal}
onViewValuePath={(path) => rightPanelRef.current?.viewValuePath(path)}
ref={leftPanelRef}
/>
);
const renderRight = () => (
<DynamicSquiggleViewer
squiggleOutput={output.output}
isRunning={output.isRunning}
// FIXME - this will cause viewer to be rendered twice on initial render
editor={leftPanelRef.current?.getEditor() ?? undefined}
ref={rightPanelRef}
{...settings}
/>
);
return (
<PlaygroundContext.Provider value={{ getLeftPanelElement }}>
<ResizableTwoPanelLayout
height={height}
renderLeft={renderLeft}
renderRight={renderRight}
/>
</PlaygroundContext.Provider>
);
};