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 41858d7
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 30 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
4 changes: 2 additions & 2 deletions website/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node16",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"outDir": "dist",
"skipLibCheck": true,
Expand Down
Binary file added website/vscode-web-editors.tgz
Binary file not shown.
16 changes: 8 additions & 8 deletions website/webpack.config.ts → website/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as webpack from "webpack";
import * as path from "path";
import * as HtmlWebpackPlugin from "html-webpack-plugin";
import { CleanWebpackPlugin } from "clean-webpack-plugin";
const webpack = require("webpack");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
import * as CopyPlugin from "copy-webpack-plugin";
const CopyPlugin = require("copy-webpack-plugin");

const r = (file: string) => path.resolve(__dirname, file);
const r = (file) => path.resolve(__dirname, file);

module.exports = {
entry: {
Expand Down Expand Up @@ -135,9 +135,9 @@ module.exports = {
patterns: [{ from: "../out/languages/", to: "./out/languages/" }],
}),
],
} as webpack.Configuration;
};

function getHtml(): string {
function getHtml() {
return `
<!DOCTYPE html>
<html>
Expand Down
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 41858d7

Please sign in to comment.