Skip to content

Commit

Permalink
Adds web editor support to playground
Browse files Browse the repository at this point in the history
  • Loading branch information
hediet committed May 2, 2024
1 parent 7073387 commit f52689c
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 27 deletions.
3 changes: 2 additions & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"react": "^17.0.2",
"react-bootstrap": "^2.4.0",
"react-dom": "^17.0.2",
"typedoc": "^0.23.26"
"typedoc": "^0.23.26",
"@vscode/web-editors": "./vscode-web-editors.tgz"
},
"devDependencies": {
"@types/classnames": "^2.3.1",
Expand Down
2 changes: 1 addition & 1 deletion website/src/website/components/Nav.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React = require("react");
import * as React from "react";
import { home, playground, docs, monarch } from "../pages/routes";
import { Container, Navbar, Nav, NavDropdown } from "./bootstrap";

Expand Down
2 changes: 1 addition & 1 deletion website/src/website/components/Page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React = require("react");
import * as React from "react";
import { PageNav } from "./Nav";

export function Page(props: { children: React.ReactNode }) {
Expand Down
2 changes: 1 addition & 1 deletion website/src/website/components/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { observer } from "mobx-react";
import React = require("react");
import * as React from "react";
import { IReference } from "../utils/ref";
import { Form } from "./bootstrap";

Expand Down
2 changes: 1 addition & 1 deletion website/src/website/monacoEditorVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as packageJson from "monaco-editor/package.json";
import packageJson from "monaco-editor/package.json";

export const monacoEditorVersion = packageJson.version;
2 changes: 1 addition & 1 deletion website/src/website/pages/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Home } from "./home/Home";
import { PlaygroundPage } from "./playground/PlaygroundPage";
import { docs, home, monarch, playground } from "./routes";
import React = require("react");
import * as React from "react";
import { DocsPage } from "./DocsPage";
import { MonarchPage } from "./MonarchPage";

Expand Down
2 changes: 1 addition & 1 deletion website/src/website/pages/DocsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
IHistoryModel,
ILocation,
} from "../utils/ObservableHistory";
import React = require("react");
import * as React from "react";

export class DocsPage extends React.Component implements IHistoryModel {
private _lastIFrame: HTMLIFrameElement | null = null;
Expand Down
2 changes: 1 addition & 1 deletion website/src/website/pages/MonarchPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React = require("react");
import * as React from "react";
import { Page } from "../components/Page";

export class MonarchPage extends React.Component<{}, {}> {
Expand Down
2 changes: 1 addition & 1 deletion website/src/website/pages/home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ControlledMonacoEditor,
} from "../../components/monaco/MonacoEditor";
import { ObservablePromise } from "../../utils/ObservablePromise";
import React = require("react");
import * as React from "react";
import { ref } from "../../utils/ref";
import { monacoEditorVersion } from "../../monacoEditorVersion";

Expand Down
19 changes: 12 additions & 7 deletions website/src/website/pages/playground/LocationModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ export class LocationModel implements IHistoryModel {
*/
@observable historyId: number = 0;

constructor(private readonly model: PlaygroundModel) {
this.dispose.track(
new HistoryController((initialLocation) => {
this.updateLocation(initialLocation);
return this;
})
);
constructor(
private readonly model: PlaygroundModel,
createHistoryController = true
) {
if (createHistoryController) {
this.dispose.track(
new HistoryController((initialLocation) => {
this.updateLocation(initialLocation);
return this;
})
);
}
}

get location(): ILocation {
Expand Down
32 changes: 31 additions & 1 deletion website/src/website/pages/playground/PlaygroundModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from "./SettingsModel";
import { BisectModel } from "./BisectModel";
import { LocationModel } from "./LocationModel";
import { createJsonWebEditorClient, vObj, vString } from "@vscode/web-editors";

export class PlaygroundModel {
public readonly dispose = Disposable.fn();
Expand All @@ -47,7 +48,25 @@ export class PlaygroundModel {
@observable
public reloadKey = 0;

public readonly historyModel = new LocationModel(this);
private readonly webEditorClient = createJsonWebEditorClient(
vObj({
js: vString(),
html: vString(),
css: vString(),
}),
(data) => {
runInAction(() => {
this.html = data.html;
this.js = data.js;
this.css = data.css;
});
}
);

public readonly historyModel = new LocationModel(
this,
this.webEditorClient === undefined
);

public reload(): void {
this.reloadKey++;
Expand Down Expand Up @@ -163,6 +182,17 @@ export class PlaygroundModel {
constructor() {
let lastState: IPreviewState | undefined = undefined;

this.webEditorClient?.onDidConnect.then(() => {
autorun(() => {
const state = this.playgroundProject;
this.webEditorClient!.updateContent({
js: state.js,
html: state.html,
css: state.css,
});
});
});

this.dispose.track({
dispose: reaction(
() => ({ state: this.state }),
Expand Down
13 changes: 11 additions & 2 deletions website/src/website/pages/playground/SettingsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export class SettingsModel {
}

constructor() {
const settingsStr = localStorage.getItem(this.settingsKey);
const settingsStr = "";
try {
localStorage.getItem(this.settingsKey);
} catch (e) {
console.error("Failed to load settings from localStorage", e);
}
if (settingsStr) {
this._settings = JSON.parse(settingsStr);
} else {
Expand All @@ -54,7 +59,11 @@ export class SettingsModel {
setSettings(settings: Settings): void {
const settingsJson = JSON.stringify(toJS(settings));
this._settings = JSON.parse(settingsJson);
localStorage.setItem(this.settingsKey, settingsJson);
try {
localStorage.setItem(this.settingsKey, settingsJson);
} catch (e) {
console.error("Failed to save settings to localStorage", e);
}
}
}

Expand Down
1 change: 0 additions & 1 deletion website/src/website/pages/playground/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { normalizeLineEnding } from "./utils";
import { IPlaygroundProject } from "../../../shared";

export function findLastIndex<T>(
Expand Down
11 changes: 4 additions & 7 deletions website/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node16",
"moduleResolution": "Node",
"strict": true,
"outDir": "dist",
"skipLibCheck": true,
"rootDir": "./src",
"resolveJsonModule": true,
"newLine": "LF",
"sourceMap": true,
"jsx": "react",
"experimentalDecorators": true,
// to enable mobx decorators
"useDefineForClassFields": false
"useDefineForClassFields": false,
"noEmit": true
},
"include": ["src/**/*", "./node_modules/monaco-editor/monaco.d.ts"]
"exclude": ["src/**/*", "./node_modules/monaco-editor/monaco.d.ts"]
}
19 changes: 19 additions & 0 deletions website/tsconfig.web.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "esnext",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"outDir": "dist",
"skipLibCheck": true,
"rootDir": "./src",
"resolveJsonModule": true,
"newLine": "LF",
"sourceMap": true,
"jsx": "react",
"experimentalDecorators": true,
"useDefineForClassFields": false,
"noEmit": true
},
"include": ["src/**/*", "./node_modules/monaco-editor/monaco.d.ts"]
}
Binary file added website/vscode-web-editors.tgz
Binary file not shown.
4 changes: 4 additions & 0 deletions website/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,10 @@
dependencies:
"@types/node" "*"

"@vscode/web-editors@./vscode-web-editors.tgz":
version "0.1.0"
resolved "./vscode-web-editors.tgz#657c1b47d50dfd1a457f660e3184fb88121f8b24"

"@webassemblyjs/ast@1.11.6", "@webassemblyjs/ast@^1.11.5":
version "1.11.6"
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.11.6.tgz#db046555d3c413f8966ca50a95176a0e2c642e24"
Expand Down

0 comments on commit f52689c

Please sign in to comment.