/
EditorPanel.vue
142 lines (132 loc) · 3.37 KB
/
EditorPanel.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<template>
<div>
<div class="h-full">
<div
class="w-full h-8 border-light-100 border-blue-50 flex justify-center items-center"
v-if="ide.currentFile"
>
<span class="text-lg"
>{{ ide.currentFile.name }}
<v-icon name="oi-dot-fill" v-if="hasUnsavedChanges"
/></span>
</div>
<div ref="editorContainer" class="h-full"></div>
</div>
</div>
</template>
<script setup lang="ts">
import {
ref,
onMounted,
onUnmounted,
shallowRef,
watch,
ShallowRef,
computed,
} from "vue";
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
import { debounce } from "@/utils/debounce";
import { useIDE } from "@/stores/ide-store";
import { OpenedFile } from "@/filesystem/file-types";
export type MonacoEditor = ReturnType<typeof monaco.editor.create>;
const editor = shallowRef<MonacoEditor | null>(null);
const editorContainer = ref<HTMLDivElement | null>(null);
const ide = useIDE();
const models = ref<Record<string, ShallowRef<monaco.editor.ITextModel>>>({});
const hasUnsavedChanges = computed(() => {
return ide.currentFile?.unsavedChanges ?? false;
});
onMounted(() => {
if (editorContainer.value) {
if (!editor.value) {
editor.value = monaco.editor.create(editorContainer.value!, {
language: "narrat",
value: "",
automaticLayout: true,
theme: "vs-dark",
});
editor.value.onDidChangeModelContent(
debounce(() => {
editorContentChanged();
}, 1000),
);
}
} else {
console.error("editorContainer is null");
}
});
watch(
() => ide.currentFile,
(file) => {
if (!editor.value) {
return;
}
if (!file?.content) {
editor.value.setValue("");
} else {
// editor.value.setValue(file.content);
// (editor.value.getModel() as any).setLanguage(detectSyntax(file));
const existingModel = getModel(file.path);
if (existingModel) {
editor.value.setModel(existingModel);
editor.value.setValue(file.content);
} else {
const model = createModel(file.path, file.content);
editor.value.setModel(model);
}
}
},
);
function editorContentChanged() {
if (editor.value) {
ide.updateCurrentFileCode(editor.value.getValue());
} else {
console.error("editor is null");
}
}
function getModel(path: string) {
if (models.value[path]?.value) {
return models.value[path].value;
} else {
return null;
}
}
function createModel(path: string, content: string) {
// const uri = monaco.Uri.parse(path);
const syntax = detectSyntax(ide.currentFile!);
console.log(`Creating model for ${path} - language ${syntax}`);
const model = monaco.editor.createModel(content, syntax);
models.value[path] = shallowRef(model);
return model;
}
function detectSyntax(file: OpenedFile) {
const name = file.name;
const extension = name.split(".").pop();
switch (extension) {
case "narrat":
return "narrat";
case "ts":
return "typescript";
case "js":
return "javascript";
case "json":
return "json";
case "html":
return "html";
case "css":
return "css";
case "md":
return "markdown";
case "yaml":
return "yaml";
default:
return "narrat";
}
}
onUnmounted(() => {
if (editor.value) {
editor.value.dispose();
}
});
</script>
<style scoped></style>