Skip to content

Commit 304b31e

Browse files
committed
feat: add rsbuild-plugin-monaco-editor-nls example
1 parent 5d398ed commit 304b31e

File tree

8 files changed

+711
-233
lines changed

8 files changed

+711
-233
lines changed

pnpm-lock.yaml

Lines changed: 572 additions & 233 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Rsbuild Project
2+
3+
## Setup
4+
5+
Install the dependencies:
6+
7+
```bash
8+
pnpm install
9+
```
10+
11+
## Get Started
12+
13+
Start the dev server:
14+
15+
```bash
16+
pnpm dev
17+
```
18+
19+
Build the app for production:
20+
21+
```bash
22+
pnpm build
23+
```
24+
25+
Preview the production build locally:
26+
27+
```bash
28+
pnpm preview
29+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "rsbuild-react-monaco-editor-nls",
3+
"private": true,
4+
"version": "1.0.0",
5+
"scripts": {
6+
"dev": "npx rsbuild dev",
7+
"build": "npx rsbuild build"
8+
},
9+
"dependencies": {
10+
"monaco-editor": "0.51.0",
11+
"react": "^18.3.1",
12+
"react-dom": "^18.3.1",
13+
"rsbuild-plugin-monaco-editor-nls": "^0.1.1"
14+
},
15+
"devDependencies": {
16+
"@rsbuild/plugin-react": "^1.0.2",
17+
"@types/react": "^18.3.8",
18+
"@types/react-dom": "^18.3.0",
19+
"typescript": "^5.6.2"
20+
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from '@rsbuild/core';
2+
import { pluginReact } from '@rsbuild/plugin-react';
3+
import { Languages, pluginMonacoEditorNls } from 'rsbuild-plugin-monaco-editor-nls';
4+
5+
export default defineConfig({
6+
plugins: [pluginReact(), pluginMonacoEditorNls({ locale: Languages.zh_hans })],
7+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import * as monaco from 'monaco-editor';
2+
import React, { useEffect, useRef } from 'react';
3+
4+
const MONACO_EDITOR_OPTIONS = {
5+
automaticLayout: true,
6+
scrollBeyondLastLine: false,
7+
fixedOverflowWidgets: true,
8+
theme: 'vs-dark',
9+
language: 'javascript',
10+
} as monaco.editor.IEditorConstructionOptions;
11+
12+
self.MonacoEnvironment = {
13+
getWorker(_: string, label: string) {
14+
if (label === 'json') {
15+
return new Worker(new URL('monaco-editor/esm/vs/language/json/json.worker', import.meta.url));
16+
}
17+
if (label === 'css' || label === 'scss' || label === 'less') {
18+
return new Worker(new URL('monaco-editor/esm/vs/language/css/css.worker', import.meta.url));
19+
}
20+
if (label === 'html' || label === 'handlebars' || label === 'razor') {
21+
return new Worker(new URL('monaco-editor/esm/vs/language/html/html.worker', import.meta.url));
22+
}
23+
if (label === 'typescript' || label === 'javascript') {
24+
return new Worker(
25+
new URL('monaco-editor/esm/vs/language/typescript/ts.worker', import.meta.url),
26+
);
27+
}
28+
return new Worker(new URL('monaco-editor/esm/vs/editor/editor.worker', import.meta.url));
29+
},
30+
};
31+
32+
function Editor() {
33+
const editor_ref = useRef<HTMLDivElement>(null);
34+
35+
useEffect(() => {
36+
if (editor_ref.current) {
37+
const editor = monaco.editor.create(editor_ref.current, {
38+
value: `const text = "Hello World";`,
39+
...MONACO_EDITOR_OPTIONS,
40+
});
41+
42+
editor.onDidChangeModelContent(() => {
43+
const value = editor.getValue();
44+
console.log(value);
45+
});
46+
}
47+
}, []);
48+
49+
return (
50+
<div id="monaco_editor" style={{ flex: 1, margin: 30, height: '600px' }} ref={editor_ref} />
51+
);
52+
}
53+
54+
export default React.memo(Editor);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body {
2+
margin: 0;
3+
color: #fff;
4+
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
5+
background-image: linear-gradient(to bottom, #020917, #101725);
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createRoot } from 'react-dom/client';
2+
import Editor from './Editor';
3+
import './index.css';
4+
5+
const rootEl = document.querySelector('#root');
6+
if (rootEl) {
7+
createRoot(rootEl).render(<Editor />);
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"sourceMap": true,
4+
"module": "ES2020",
5+
"moduleResolution": "node",
6+
"strict": true,
7+
"target": "ES6",
8+
"lib": ["dom", "es5", "es2015.collection", "es2015.promise"],
9+
"types": [],
10+
"jsx": "preserve",
11+
"esModuleInterop": true
12+
},
13+
"include": ["src"]
14+
}

0 commit comments

Comments
 (0)