Skip to content

Commit 65aad83

Browse files
committed
feat(ui): add c-monaco-editor to ui lib
1 parent fd0a723 commit 65aad83

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ declare module '@vue/runtime-core' {
5050
'CModal.demo': typeof import('./src/ui/c-modal/c-modal.demo.vue')['default']
5151
CModalValue: typeof import('./src/ui/c-modal-value/c-modal-value.vue')['default']
5252
'CModalValue.demo': typeof import('./src/ui/c-modal-value/c-modal-value.demo.vue')['default']
53+
CMonacoEditor: typeof import('./src/ui/c-monaco-editor/c-monaco-editor.vue')['default']
5354
CollapsibleToolMenu: typeof import('./src/components/CollapsibleToolMenu.vue')['default']
5455
ColorConverter: typeof import('./src/tools/color-converter/color-converter.vue')['default']
5556
ColoredCard: typeof import('./src/components/ColoredCard.vue')['default']

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"release": "node ./scripts/release.mjs"
3636
},
3737
"dependencies": {
38+
"@guolao/vue-monaco-editor": "^1.4.1",
3839
"@it-tools/bip39": "^0.0.4",
3940
"@it-tools/oggen": "^1.3.0",
4041
"@sindresorhus/slugify": "^2.2.1",

pnpm-lock.yaml

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { createApp } from 'vue';
22
import { createPinia } from 'pinia';
33
import { createHead } from '@vueuse/head';
44

5+
import { install as VueMonacoEditorPlugin, loader } from '@guolao/vue-monaco-editor';
6+
import * as monaco from 'monaco-editor';
7+
58
import { registerSW } from 'virtual:pwa-register';
69
import { plausible } from './plugins/plausible.plugin';
710

@@ -13,10 +16,14 @@ import App from './App.vue';
1316
import router from './router';
1417
import { i18nPlugin } from './plugins/i18n.plugin';
1518

19+
// loaded monaco-editor from `node_modules`
20+
loader.config({ monaco });
21+
1622
registerSW();
1723

1824
const app = createApp(App);
1925

26+
app.use(VueMonacoEditorPlugin);
2027
app.use(createPinia());
2128
app.use(createHead());
2229
app.use(i18nPlugin);
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<script setup lang="ts">
2+
import * as monacoEditor from 'monaco-editor';
3+
import type { MonacoEditor } from '@guolao/vue-monaco-editor';
4+
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
5+
import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
6+
import CssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker';
7+
import HtmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker';
8+
import TsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker';
9+
import { useStyleStore } from '@/stores/style.store';
10+
11+
const props = withDefaults(defineProps<EditorProps>(), {
12+
theme: 'vs',
13+
options: () => ({}),
14+
overrideServices: () => ({}),
15+
saveViewState: true,
16+
width: '100%',
17+
height: '100%',
18+
});
19+
20+
const emits = defineEmits<{
21+
(e: 'update:value', value: string | undefined): void
22+
(e: 'beforeMount', monaco: MonacoEditor): void
23+
(e: 'mount', editor: monacoEditor.editor.IStandaloneCodeEditor, monaco: MonacoEditor): void
24+
(e: 'change', value: string | undefined, event: monacoEditor.editor.IModelContentChangedEvent): void
25+
(e: 'validate', markers: monacoEditor.editor.IMarker[]): void
26+
}>();
27+
28+
interface MonacoEnvironment {
29+
getWorker(_: any, label: string): Worker
30+
}
31+
// eslint-disable-next-line @typescript-eslint/no-namespace
32+
declare module globalThis {
33+
let MonacoEnvironment: MonacoEnvironment;
34+
}
35+
36+
const value = useVModel(props, 'value', emits);
37+
38+
globalThis.MonacoEnvironment = {
39+
getWorker(_: any, label: string) {
40+
if (label === 'json') {
41+
return new JsonWorker();
42+
}
43+
if (label === 'css' || label === 'scss' || label === 'less') {
44+
return new CssWorker();
45+
}
46+
if (label === 'html' || label === 'handlebars' || label === 'razor') {
47+
return new HtmlWorker();
48+
}
49+
if (label === 'typescript' || label === 'javascript') {
50+
return new TsWorker();
51+
}
52+
return new EditorWorker();
53+
},
54+
};
55+
56+
export interface EditorProps {
57+
defaultValue?: string
58+
defaultPath?: string
59+
defaultLanguage?: string
60+
value?: string
61+
language?: string
62+
path?: string
63+
64+
/* === */
65+
66+
theme: 'vs' | string
67+
line?: number
68+
options?: monacoEditor.editor.IStandaloneEditorConstructionOptions
69+
overrideServices?: monacoEditor.editor.IEditorOverrideServices
70+
saveViewState?: boolean
71+
72+
/* === */
73+
74+
width?: number | string
75+
height?: number | string
76+
className?: string
77+
}
78+
79+
monacoEditor.editor.defineTheme('it-tools-dark', {
80+
base: 'vs-dark',
81+
inherit: true,
82+
rules: [],
83+
colors: {
84+
'editor.background': '#00000000',
85+
},
86+
});
87+
88+
monacoEditor.editor.defineTheme('it-tools-light', {
89+
base: 'vs',
90+
inherit: true,
91+
rules: [],
92+
colors: {
93+
'editor.background': '#00000000',
94+
},
95+
});
96+
97+
const styleStore = useStyleStore();
98+
99+
watch(
100+
() => styleStore.isDarkTheme,
101+
isDarkTheme => monacoEditor.editor.setTheme(isDarkTheme ? 'it-tools-dark' : 'it-tools-light'),
102+
{ immediate: true },
103+
);
104+
105+
const attrs = useAttrs();
106+
const inheritedAttrs = { ...attrs, ...props };
107+
</script>
108+
109+
<script lang="ts">
110+
export default {
111+
inheritAttrs: false,
112+
};
113+
</script>
114+
115+
<template>
116+
<vue-monaco-editor
117+
v-bind="inheritedAttrs"
118+
v-model:value="value"
119+
@before-mount="(monaco: MonacoEditor) => emits('beforeMount', monaco)"
120+
@mount="(editor: monacoEditor.editor.IStandaloneCodeEditor, monaco: MonacoEditor) => emits('mount', editor, monaco)"
121+
@change="(value: string | undefined, event: monacoEditor.editor.IModelContentChangedEvent) => emits('change', value, event)"
122+
@validate="(markers: monacoEditor.editor.IMarker[]) => emits('validate', markers)"
123+
/>
124+
</template>

0 commit comments

Comments
 (0)