/ concept-collection / stan-web-ide
Sign in
concept-collection / stan-web-ide
stan-web-ide / src / ide.ts
73 lines · 2.6 KBBlameHistoryRaw
1import { createWorkbench, type WorkbenchTheme } from 'minwebide';
2import { openProjectFileSystem, touchProject, type ProjectInfo } from './projects';
3import { createStanRunner } from './stan/runner';
4import { createSampleEditorProvider } from './stan/sampleEditor';
5import { showServerDialog } from './stan/serverDialog';
6import { getServerUrl, onDidChangeServerUrl, probeServer } from './stan/settings';
8/** Opens the IDE for a project. Returns a disposable view. */
9export async function openIde(container: HTMLElement, project: ProjectInfo, theme: WorkbenchTheme): Promise<{ dispose(): void }> {
10 touchProject(project.id);
11 document.title = `${project.name} — stan web IDE`;
13 const fs = await openProjectFileSystem(project.id);
14 const stan = createStanRunner(fs);
16 const workbench = createWorkbench(container, {
17 fileSystem: fs,
18 theme,
19 workspaceName: project.name,
20 });
21 workbench.registerRunner(stan.runner);
22 workbench.registerCustomEditor(createSampleEditorProvider(fs, workbench, { stop: stan.stop }));
24 // the project indicator: click to go back to the project list
25 workbench.statusBar.setItem('project', 'left', project.name, {
26 icon: 'folder-opened',
27 title: 'Back to projects',
28 onClick: () => { location.hash = '#/'; },
29 });
30 // replace the default branding item with the project indicator
31 workbench.statusBar.removeItem('branding');
33 // compile-server status: shows connectivity, click to change the URL
34 let disposed = false;
35 const refreshServerItem = async () => {
36 const url = getServerUrl();
37 workbench.statusBar.setItem('stan-server', 'right', 'Stan server: checking...', {
38 icon: 'server',
39 title: `${url}\nClick to change the compilation server`,
40 onClick: () => showServerDialog(container),
41 });
42 const ok = await probeServer(url);
43 if (disposed || url !== getServerUrl()) {
44 return;
45 }
46 workbench.statusBar.setItem('stan-server', 'right', `Stan server: ${ok ? 'connected' : 'offline'}`, {
47 icon: ok ? 'server' : 'warning',
48 title: `${url}${ok ? 'connected' : 'not reachable'}\nClick to change the compilation server`,
49 onClick: () => showServerDialog(container),
50 });
51 };
52 void refreshServerItem();
53 const serverListener = onDidChangeServerUrl(() => void refreshServerItem());
55 // open the most useful starting file
56 for (const path of ['/fit.sample', '/main.stan', '/README.md']) {
57 const uri = fs.root.with({ path });
58 if (await fs.fileService.exists(uri)) {
59 await workbench.openFile(uri);
60 break;
61 }
62 }
64 return {
65 dispose() {
66 disposed = true;
67 serverListener.dispose();
68 stan.dispose();
69 workbench.dispose();
70 fs.dispose();
71 },
72 };
moveopenescclose