Skip to content

Commit efb93f2

Browse files
committed
feat: implement Monaco Editor component with HTML, CSS, and JavaScript integration
1 parent 59f033b commit efb93f2

File tree

8 files changed

+89
-0
lines changed

8 files changed

+89
-0
lines changed

app/home/home.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ <h2>Home</h2>
1212
<li><a href="/material-icons">Material Icons</a></li>
1313
<li><a href="/activity-state">Activity State</a></li>
1414
<li><a href="/web-nn">Web NN</a></li>
15+
<li><a href="/monaco-editor">Monaco Editor</a></li>
1516
</ul>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
:host {
2+
display: grid;
3+
grid-template-rows: auto 1fr;
4+
padding: var(--padding);
5+
width: 100%;
6+
height: 100%;
7+
overflow: hidden;
8+
}
9+
10+
#editor-wrapper {
11+
height: 100%;
12+
width: 100%;
13+
}
14+
15+
#editor-container {
16+
height: 100%;
17+
width: 100%;
18+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<app-header><h2>Monaco Editor</h2></app-header>
2+
<monaco-editor></monaco-editor>

app/monaco-editor/monaco-editor.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import "./../../components/monaco-editor/monaco-editor.js";
2+
3+
export default class MonacoEditorView extends HTMLElement {
4+
static tag = "monaco-editor-view";
5+
6+
constructor() {
7+
super();
8+
this.attachShadow({ mode: "open" });
9+
}
10+
11+
async connectedCallback() {
12+
this.shadowRoot.innerHTML = await api.call("component", "load_html", {
13+
url: import.meta.url,
14+
});
15+
}
16+
}
17+
18+
customElements.define(MonacoEditorView.tag, MonacoEditorView);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
:host {
2+
display: block;
3+
width: 100%;
4+
height: 100%;
5+
overflow: hidden;
6+
}

components/monaco-editor/monaco-editor.html

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const thisCSSUrl = import.meta.url.replace(".html.js", ".css");
2+
const monacoCSSUrl = new URL("./node_modules/monaco-editor/min/vs/editor/editor.main.css", import.meta.url).href;
3+
4+
export const HTML = `
5+
<link rel="stylesheet" href="${thisCSSUrl}"/>
6+
<link rel="stylesheet" href="${monacoCSSUrl}" data-name="vs/editor/editor.main"/>
7+
`;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { HTML } from "./monaco-editor.html.js";
2+
import "./require.js";
3+
4+
export default class MonacoEditor extends HTMLElement {
5+
static tag = "monaco-editor";
6+
7+
#editor;
8+
9+
get language() {
10+
return this.dataset.language ?? "json";
11+
}
12+
13+
get theme() {
14+
return this.dataset.theme ?? "vs-dark";
15+
}
16+
17+
constructor() {
18+
super();
19+
this.attachShadow({ mode: "open" });
20+
this.shadowRoot.innerHTML = HTML;
21+
}
22+
23+
async connectedCallback() {
24+
await import("./node_modules/monaco-editor/min/vs/loader.js");
25+
await import("./node_modules/monaco-editor/min/vs/editor/editor.main.js");
26+
27+
28+
this.#editor = monaco.editor.create(this.shadowRoot.querySelector("#editor"), {
29+
value: this.textContent,
30+
language: this.language,
31+
theme: this.theme,
32+
automaticLayout: true,
33+
});
34+
}
35+
};
36+
37+
customElements.define(MonacoEditor.tag, MonacoEditor);

0 commit comments

Comments
 (0)