-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditor.ts
More file actions
139 lines (111 loc) · 3.42 KB
/
editor.ts
File metadata and controls
139 lines (111 loc) · 3.42 KB
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
import { shallowRef, unref, onMounted, ref, watchEffect } from "vue";
import type { Ref, MaybeRef, ShallowRef } from "vue";
import type * as Monaco from "monaco-editor";
import { useColorThemeOnMonacoEditor } from "./theme";
import { useModel } from "./model";
import type { UseModelOptions } from "./model";
const editorOptionsBase: Monaco.editor.IEditorOptions = {
minimap: { enabled: false },
scrollbar: { vertical: "auto", horizontal: "auto" },
fontFamily: "Fira Code",
fontSize: 14,
fontWeight: "500",
fontLigatures: true,
lineHeight: 24,
automaticLayout: true,
scrollBeyondLastLine: false,
glyphMargin: true,
quickSuggestions: false,
};
const editorOptionsEditor: Monaco.editor.IEditorOptions = {
readOnly: false,
matchBrackets: "always",
selectionHighlight: true,
occurrencesHighlight: "singleFile",
renderLineHighlight: "line",
};
const editorOptionsViewer: Monaco.editor.IEditorOptions = {
readOnly: true,
matchBrackets: "never",
selectionHighlight: false,
occurrencesHighlight: "off",
renderLineHighlight: "none",
};
type Mode = "editor" | "viewer";
const defaultMode: Mode = "viewer";
const modelOptionsMap: Record<Mode, Monaco.editor.IEditorOptions> = {
editor: editorOptionsEditor,
viewer: editorOptionsViewer,
};
export interface UseMonacoEditorOptions extends UseModelOptions {
element: HTMLElement | Ref<HTMLElement | undefined>;
mode?: MaybeRef<Mode>;
}
interface _UseMonacoEditorReturn {
editor: ShallowRef<Monaco.editor.IStandaloneCodeEditor | undefined>;
model: ShallowRef<Monaco.editor.ITextModel | undefined>;
source: Ref<string>;
mode: Ref<Mode>;
ready: Promise<void>;
}
type UseMonacoEditorReturn = _UseMonacoEditorReturn &
PromiseLike<_UseMonacoEditorReturn>;
export function useMonacoEditor(
options: UseMonacoEditorOptions
): UseMonacoEditorReturn {
const { element, mode: mode_, ...modelOptions } = options;
const monaco = ref<typeof Monaco>();
const mode = ref(mode_ ?? defaultMode);
const {
model,
source,
beforeSetValue,
afterSetValue,
ready: readyModel,
} = useModel(modelOptions);
const editor = shallowRef<Monaco.editor.IStandaloneCodeEditor>();
const isMounted = ref(false);
onMounted(() => {
isMounted.value = true;
});
watchEffect(() => {
if (!isMounted.value) return;
const htmlElement = unref(element);
if (!htmlElement) return;
const model_ = unref(model);
if (!model_) return;
const options = { model: model_, ...editorOptionsBase };
editor.value = monaco.value?.editor.create(htmlElement, options);
});
watchEffect(() => {
editor.value?.updateOptions(modelOptionsMap[mode.value]);
});
let lastPosition: Monaco.Position | null = null;
beforeSetValue(() => {
// save cursor position
if (!editor.value) return;
lastPosition = editor.value.getPosition();
});
afterSetValue(() => {
// restore cursor position
if (!editor.value) return;
if (!lastPosition) return;
editor.value.setPosition(lastPosition);
editor.value.focus();
lastPosition = null;
});
async function loadMonaco() {
await useColorThemeOnMonacoEditor();
monaco.value = await import("monaco-editor");
await readyModel;
}
const ready = loadMonaco();
const ret = { editor, model, source, mode, ready };
return {
...ret,
async then(onFulfilled, onRejected) {
await ready;
return Promise.resolve(ret).then(onFulfilled, onRejected);
},
};
}