Skip to content

Commit fa19692

Browse files
committed
refactor: rename text-editor > monaco-editor
1 parent 1f50594 commit fa19692

File tree

9 files changed

+214
-29
lines changed

9 files changed

+214
-29
lines changed

apps/ui/src/layouts/LayoutDefault.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const drawer = useLocalStorage('drawer', true)
1313
:class="drawer ? 'w-72 border-r' : 'w-0'"
1414
/>
1515

16-
<div class="flex-1 min-h-full">
16+
<div class="flex-1 h-dvh">
1717
<slot />
1818
</div>
1919
</is-app>

apps/ui/src/modules/text-editor/TextEditor.vue renamed to apps/ui/src/modules/monaco/MonacoEditorAppPage.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
<script lang="ts" setup>
22
3+
import MonacoEditor from './components/MonacoEditor.vue'
4+
35
// general
46
const path = defineProp<string>('path', {
57
type: String,
68
required: true
79
})
810
11+
const language = defineProp<string>('language', {
12+
type: String,
13+
default: 'plaintext'
14+
})
15+
916
const { drive, encode, decode } = useDrive()
1017
1118
// load
@@ -30,8 +37,9 @@ async function save(){
3037

3138
<template>
3239
<div class="h-dvh">
33-
<is-monaco-editor
40+
<monaco-editor
3441
v-model="text"
42+
:language="language"
3543
@keydown.ctrl.s.prevent="save"
3644
/>
3745
</div>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<script setup lang="ts">
2+
import { onMounted, onUnmounted, ref, watch } from 'vue'
3+
import { useVModel } from 'vue-wind/composables/v-model'
4+
import { loadLibs, type MonacoLibs } from '@/composables/useMonaco'
5+
6+
import { editor as monacoEditor } from 'monaco-editor'
7+
8+
// Props & Emits
9+
interface LeftLineOptions {
10+
show: 'on' | 'off' | 'relative' | 'interval'
11+
decorationsWidth?: number | string
12+
numbersMinChars?: number
13+
}
14+
15+
const props = defineProps({
16+
modelValue: {
17+
type: String,
18+
default: '',
19+
},
20+
language: {
21+
type: String,
22+
default: 'text',
23+
},
24+
minimap: {
25+
type: Boolean,
26+
default: false,
27+
},
28+
readonly: {
29+
type: Boolean,
30+
default: false,
31+
},
32+
glyphMargin: {
33+
type: Boolean,
34+
default: false,
35+
},
36+
folding: {
37+
type: Boolean,
38+
default: false,
39+
},
40+
padding: {
41+
type: Object as () => monacoEditor.IEditorOptions['padding'],
42+
default: undefined,
43+
},
44+
renderLineHighlight: {
45+
type: String as () => monacoEditor.IEditorOptions['renderLineHighlight'],
46+
default: undefined,
47+
},
48+
trimAutoWhitespace: {
49+
type: Boolean,
50+
default: false,
51+
},
52+
scrollbar: {
53+
type: Object as () => monacoEditor.IEditorScrollbarOptions,
54+
default: () => ({
55+
verticalScrollbarSize: 10,
56+
horizontalScrollbarSize: 10,
57+
useShadows: false,
58+
horizontal: 'visible',
59+
vertical: 'visible',
60+
}),
61+
},
62+
lineOptions: {
63+
type: Object as () => LeftLineOptions,
64+
default: () => ({
65+
show: 'on',
66+
}),
67+
},
68+
libs: {
69+
type: Array as () => MonacoLibs[],
70+
default: () => [],
71+
},
72+
})
73+
74+
const emit = defineEmits(['update:modelValue'])
75+
76+
const dispose = ref(() => true)
77+
78+
// load libs
79+
onMounted(() => {
80+
dispose.value = loadLibs(props.libs)
81+
})
82+
83+
onUnmounted(() => {
84+
dispose.value()
85+
})
86+
87+
// mount editor
88+
const root = ref<HTMLElement>()
89+
90+
let editor: ReturnType<typeof useMonaco>
91+
92+
const model = useVModel(props, 'modelValue', emit)
93+
94+
onMounted(() => {
95+
if (!root.value) return
96+
97+
editor = useMonaco(root.value!, {
98+
value: model.value,
99+
language: props.language,
100+
readOnly: props.readonly,
101+
minimap: { enabled: props.minimap },
102+
padding: props.padding,
103+
overviewRulerBorder: false,
104+
lineNumbers: props.lineOptions.show,
105+
lineDecorationsWidth: props.lineOptions.decorationsWidth,
106+
lineNumbersMinChars: props.lineOptions.numbersMinChars,
107+
glyphMargin: props.glyphMargin,
108+
folding: props.folding,
109+
wordWrap: 'on',
110+
scrollbar: props.scrollbar,
111+
renderLineHighlight: props.renderLineHighlight,
112+
trimAutoWhitespace: props.trimAutoWhitespace,
113+
})
114+
115+
editor.getModel()?.onDidChangeContent(() => (model.value = editor.getValue()))
116+
})
117+
118+
onUnmounted(() => {
119+
editor.getModel()?.dispose()
120+
})
121+
122+
watch(
123+
() => props.modelValue,
124+
(value) => {
125+
if (!editor) return
126+
127+
if (value !== editor.getValue()) {
128+
editor.setValue(value)
129+
}
130+
}
131+
)
132+
</script>
133+
<template>
134+
<div
135+
ref="root"
136+
class="w-full h-full"
137+
/>
138+
</template>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import MonacoEditorAppPage from "./MonacoEditorAppPage.vue"
2+
3+
export const monacoEditorAppPage = defineAppPage({
4+
name: 'monaco-editor',
5+
component: MonacoEditorAppPage,
6+
})
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
export const monacoEditorEntryMiddleware = defineEntryMiddleware({
2+
order: 100,
3+
handle: async ({ entry }) => {
4+
5+
if (['txt'].some(ext => entry.path.endsWith(ext))) {
6+
return {
7+
page: 'monaco-editor',
8+
props: {
9+
path: entry.path,
10+
language: 'plaintext',
11+
}
12+
}
13+
}
14+
15+
if (entry.path.endsWith('.js')) {
16+
return {
17+
page: 'monaco-editor',
18+
props: {
19+
path: entry.path,
20+
language: 'javascript',
21+
}
22+
}
23+
}
24+
if (entry.path.endsWith('.ts')) {
25+
return {
26+
page: 'monaco-editor',
27+
props: {
28+
path: entry.path,
29+
language: 'typescript',
30+
}
31+
}
32+
}
33+
34+
if (entry.path.endsWith('.json')) {
35+
return {
36+
page: 'monaco-editor',
37+
props: {
38+
path: entry.path,
39+
language: 'json',
40+
}
41+
}
42+
}
43+
44+
if (entry.path.endsWith('.css')) {
45+
return {
46+
page: 'monaco-editor',
47+
props: {
48+
path: entry.path,
49+
language: 'css',
50+
}
51+
}
52+
}
53+
}
54+
})

apps/ui/src/modules/text-editor/textEditorAppPage.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

apps/ui/src/modules/text-editor/textEditorEntryMiddleware.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

apps/ui/src/pages/AppPage/AppPageRender.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
import type { AppPage } from '@/composables/defineAppPage'
44
import { directoryAppPage } from '@/modules/directory/directoryAppPage';
5-
import { textEditorAppPage } from '@/modules/text-editor/textEditorAppPage';
5+
import { monacoEditorAppPage } from '@/modules/monaco/monacoEditorAppPage';
66
77
const name = defineProp('name', {
88
type: String,
@@ -16,7 +16,7 @@ const pageProps = defineProp<Record<string, any>>('pageProps', {
1616
1717
const pages = shallowRef<AppPage[]>([
1818
directoryAppPage,
19-
textEditorAppPage
19+
monacoEditorAppPage
2020
])
2121
2222
const current = computed(() => pages.value.find(p => p.name === name.value))

apps/ui/src/pages/EntryProvider/EntryProvider.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts" setup>
22
import type { EntryMiddleware, EntryMiddlewareResult } from '@/composables/defineEntryMiddleware'
33
import { directoryEntryMiddleware } from '@/modules/directory/directoryEntryMiddleware';
4-
import { textEditorEntryMiddleware } from '@/modules/text-editor/textEditorEntryMiddleware';
4+
import { monacoEditorEntryMiddleware } from '@/modules/monaco/monacoEditorEntryMiddleware';
55
import AppPageRender from '@/pages/AppPage/AppPageRender.vue'
66
77
import orderBy from 'lodash/orderBy'
@@ -12,7 +12,7 @@ const { drive } = useDrive()
1212
// middlewares
1313
const middlewares = ref<EntryMiddleware[]>([
1414
directoryEntryMiddleware,
15-
textEditorEntryMiddleware
15+
monacoEditorEntryMiddleware
1616
])
1717
1818
// load
@@ -60,7 +60,7 @@ watch(path, load, { immediate: true })
6060
</script>
6161

6262
<template>
63-
<div class="w-full min-h-full">
63+
<div class="w-full h-full">
6464
<div
6565
v-if="loading"
6666
class="w-full flex items-center justify-center min-h-full"
@@ -77,7 +77,7 @@ watch(path, load, { immediate: true })
7777

7878
<div
7979
v-else
80-
class="w-full flex items-center justify-center min-h-full"
80+
class="w-full min-h-full flex items-center justify-center "
8181
>
8282
<div>Error loading entry: {{ error }}</div>
8383

0 commit comments

Comments
 (0)