Skip to content

Commit 08f5af2

Browse files
authored
Merge pull request #12 from JunHaoShih/monaco-editor
feat: add monaco editor component
2 parents a3c76f3 + be9df4b commit 08f5af2

File tree

6 files changed

+123
-26
lines changed

6 files changed

+123
-26
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 CompileError
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"firebase": "^10.1.0",
2222
"highlight.js": "^11.8.0",
2323
"markdown-it": "^13.0.1",
24+
"monaco-editor": "^0.41.0",
2425
"pinia": "^2.0.11",
2526
"quasar": "^2.6.0",
2627
"uuid": "^9.0.0",

src/components/MonacoEditor.vue

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<template>
2+
<div ref="divDom"></div>
3+
</template>
4+
5+
<script setup lang="ts">
6+
import * as monaco from 'monaco-editor';
7+
import {
8+
computed, onMounted, ref, watch,
9+
} from 'vue';
10+
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
11+
import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
12+
import CssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker';
13+
import HtmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker';
14+
import TsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker';
15+
16+
const divDom = ref<HTMLElement>();
17+
18+
let editor: monaco.editor.IStandaloneCodeEditor;
19+
20+
const props = defineProps<{
21+
modelValue: string,
22+
}>();
23+
24+
type Emit = {
25+
(e: 'update:modelValue', value: string): void
26+
}
27+
const emit = defineEmits<Emit>();
28+
29+
const text = computed({
30+
get: () => props.modelValue,
31+
set: (value) => emit('update:modelValue', value),
32+
});
33+
34+
onMounted(() => {
35+
// eslint-disable-next-line no-restricted-globals
36+
self.MonacoEnvironment = {
37+
getWorker(_, label) {
38+
if (label === 'json') {
39+
return new JsonWorker();
40+
}
41+
if (label === 'css' || label === 'scss' || label === 'less') {
42+
return new CssWorker();
43+
}
44+
if (label === 'html' || label === 'handlebars' || label === 'razor') {
45+
return new HtmlWorker();
46+
}
47+
if (label === 'typescript' || label === 'javascript') {
48+
return new TsWorker();
49+
}
50+
return new EditorWorker();
51+
},
52+
};
53+
if (divDom.value) {
54+
editor = monaco.editor.create(divDom.value, {
55+
value: text.value,
56+
language: 'markdown',
57+
automaticLayout: true,
58+
minimap: {
59+
enabled: false,
60+
},
61+
scrollbar: {
62+
verticalScrollbarSize: 10,
63+
horizontalSliderSize: 10,
64+
},
65+
});
66+
67+
editor.onDidChangeModelContent(() => {
68+
text.value = editor.getValue();
69+
});
70+
}
71+
});
72+
73+
watch(text, (newValue) => {
74+
const currentValue = editor.getValue();
75+
if (currentValue !== newValue) {
76+
editor.setValue(newValue);
77+
}
78+
});
79+
</script>

src/modules/markdown/components/MarkdownEditor.vue

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66
:limits="limits"
77
>
88
<template v-slot:before>
9-
<q-input
10-
ref="inputRef"
9+
<MonacoEditor
1110
v-model="mdText"
12-
type="textarea"
13-
autogrow
14-
:label="$t('markdownPage.editHere')"
15-
v-on:keydown.tab.prevent="onTabPressed"
16-
/>
11+
class="main-panel q-pt-xs q-pr-sm"
12+
></MonacoEditor>
1713
</template>
1814
<template v-slot:after>
1915
<markdown-viewer
@@ -26,18 +22,17 @@
2622
</template>
2723

2824
<script setup lang="ts">
29-
import { computed, ref, watch } from 'vue';
30-
import { QInput, QSplitter } from 'quasar';
25+
import {
26+
computed, ref, watch,
27+
} from 'vue';
28+
import { QSplitter } from 'quasar';
29+
import MonacoEditor from 'src/components/MonacoEditor.vue';
3130
import MarkdownViewer from './MarkdownViewer.vue';
3231
3332
export type EditorType = 'edit' | 'view' | 'split';
3433
35-
const inputRef = ref<QInput>();
36-
3734
const splitterModel = ref(50);
3835
39-
const indentSize = ref(4);
40-
4136
const limits = ref<QSplitter['limits']>([0, Infinity]);
4237
4338
const props = withDefaults(defineProps<{
@@ -55,14 +50,6 @@ const mdText = computed({
5550
set: (value) => emit('update:modelValue', value),
5651
});
5752
58-
function onTabPressed() {
59-
const nativeInput = inputRef.value?.getNativeElement();
60-
if (nativeInput) {
61-
const indent = ' '.repeat(indentSize.value);
62-
document.execCommand('insertText', false, indent);
63-
}
64-
}
65-
6653
watch(() => props.type, (newValue) => {
6754
if (newValue === 'edit') {
6855
splitterModel.value = 100;

src/pages/workspace/MarkdownPage.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@
5151
@click="editorType = 'view'"
5252
></q-btn>
5353
</div>
54-
<MarkdownEditor
55-
v-model="mdEdit"
56-
:type="editorType"
57-
>
58-
</MarkdownEditor>
54+
<div v-for="repo in markdownsStore.repos" :key="repo.id">
55+
<MarkdownEditor
56+
:style="repo.id === id ? '' : 'display: none'"
57+
v-model="repo.edit"
58+
:type="editorType"
59+
>
60+
</MarkdownEditor>
61+
</div>
5962
</div>
6063
</template>
6164

0 commit comments

Comments
 (0)