-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
253 lines (245 loc) · 8.7 KB
/
main.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* eslint-disable import/no-amd, no-unused-expressions, no-useless-escape */
/* globals xhr fH:writable, fHandle:writable, monaco, themes */
console.time("MAIN");
const globalThis = window.globalThis;
globalThis.issues = [];
globalThis.dev = false;
function logz(inp) {
if (globalThis.dev === true) {
console.log(inp);
}
}
async function openFile() {
[fH] = await window.showOpenFilePicker();
const file = await fH.getFile();
const contents = await file.text();
globalThis.txtValue = contents;
const type = await file.type;
globalThis.curTyp = type;
}
async function saveFile() {
if (!globalThis?.fH) {
let fHandle = await window.showSaveFilePicker();
globalThis.fH = fHandle;
} else {
fHandle = fH;
}
const writableStream = await fHandle.createWritable();
await writableStream.write(globalThis.txtValue);
await writableStream.close();
}
globalThis.showHover = false;
globalThis.showSuggests = false;
globalThis.txtValue = "";
globalThis.langDef = "HW";
require(["vs/editor/editor.main"], function () {
monaco.languages.register({id: globalThis.langDef});
let Theme = "Oceanic-Next-Simple";
// let Theme = "Pencil-simp";
globalThis.theme = Theme;
let txt = ` He is young Leopold, as in a retrospective arrangement, a mirror within a mirror (hey, presto!), he beholdeth himself.
That young figure of then is seen, precious manly, walking on a nipping morning from the old house in Clambrassil to the high school,
his book satchel on him bandolier wise, and in it a goodly hunk of wheaten loaf, a mother's thought.`;
let txt2 = `this is not jjust a great app ... It is an awesome app!!!`;
// Define new themes that contain rules that match this language
console.time("THEME");
for (const [key, value] of Object.entries(themes)) {
monaco.editor.defineTheme(key, value);
}
console.timeEnd("THEME");
const editorElement = document.getElementById("editor");
let editor = monaco.editor.create(editorElement, {
language: "HW",
loading: "Loading . . . ",
cursorBlinking: "phase",
scrollbar: {
horizontal: "hidden",
vertical: "hidden"
},
cursorSmoothCaretAnimation: true,
cursorStyle: "line-thin",
theme: Theme,
fontSize: 15,
lineHeight: 1.5,
minimap: {enabled: false, renderCharacters: false},
multiCursorModifier: "ctrlCmd",
overviewRulerBorder: false,
overviewRulerLanes: 0,
smoothScrolling: true,
cursorSurroundingLines: 12,
suggest: {preview: false, previewMode: "subword-smart"}
});
// Resize the editor when the window size changes
const model = monaco.editor.createModel(txt, globalThis.langDef, monaco.Uri.parse("inmemory://1"));
editor.setModel(model);
window.addEventListener("resize", () =>
editor.layout({
width: editorElement.offsetWidth,
height: editorElement.offsetHeight
})
);
let actionsW = {
short_help: editor.addAction({
id: "short-help",
label: "Show menu",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyMod.Alt | monaco.KeyCode.Space, monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyP],
contextMenuOrder: 0,
run: function (edi) {
edi._actions["editor.action.quickCommand"]._run();
}
}),
re_init: editor.addAction({
id: "re-Init",
label: "Reload",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyMod.Alt | monaco.KeyCode.Space],
contextMenuOrder: 0,
run: function (edi) {
globalThis.pr.init();
monaco.editor.setModelLanguage(model, "HW");
}
}),
show_hover: editor.addAction({
id: "show-hover",
label: "Toggle hover definitions",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyH],
contextMenuOrder: -0.2,
contextMenuGroupId: "1_navigation",
run: function (edi) {
globalThis.showHover = !globalThis.showHover;
}
}),
show_suggests: editor.addAction({
id: "show-suggests",
label: "Toggle suggestions",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyF],
contextMenuOrder: -0.2,
contextMenuGroupId: "1_navigation",
run: function (edi) {
globalThis.showSuggests = !globalThis.showSuggests;
}
}),
re_suggests: editor.addAction({
id: "inspect-tokens",
label: "Inspect Tokens",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyM],
contextMenuOrder: -0.2,
contextMenuGroupId: "1_navigation",
run: function (edi) {
edi._actions["editor.action.inspectTokens"]._run();
}
}),
setTheme: editor.addAction({
id: "theme-toggle",
label: "Toggle Theme",
keybindings: [monaco.KeyMod.chord(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyK, monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyE)],
contextMenuOrder: -0.2,
contextMenuGroupId: "1_navigation",
run: function (edi) {
let themeMap = {"Pencil": "Oceanic-Next", "Oceanic-Next": "Pencil", "Pencil-Simple": "Oceanic-Next-Simple", "Oceanic-Next-Simple": "Pencil-Simple"};
let t = globalThis.theme;
globalThis.theme = themeMap[t];
monaco.editor.setTheme(globalThis.theme);
}
}),
setSimple: editor.addAction({
id: "simple-theme-toggle",
label: "Toggle Simple Theme",
keybindings: [monaco.KeyMod.chord(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyK, monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS)],
contextMenuOrder: -0.2,
contextMenuGroupId: "1_navigation",
run: function (edi) {
let themeMap = {"Pencil": "Pencil-Simple", "Oceanic-Next": "Oceanic-Next-Simple", "Pencil-Simple": "Pencil", "Oceanic-Next-Simple": "Oceanic-Next"};
let t = globalThis.theme;
globalThis.theme = themeMap[t];
monaco.editor.setTheme(globalThis.theme);
}
}),
save_edits: editor.addAction({
id: "save-edits",
label: "Save to cached file",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS],
contextMenuOrder: 0,
run: function (edi) {
globalThis.txtValue = edi.getValue();
// saveFile();
// document.getElementById("saved").innerText = "Saved";
}
}),
save_edits_as: editor.addAction({
id: "save-edits-as",
label: "Save to new file",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyS],
contextMenuOrder: 0,
run: function (edi) {
globalThis.txtValue = edi.getValue();
// saveFile();
}
}),
save_T: editor.addAction({
id: "remove-marks-now",
label: "Clear the Spelling errors",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyI],
contextMenuOrder: 0.2,
contextMenuGroupId: "2_lint",
run: function (edi) {
globalThis.issues = [];
let model = edi.getModel();
logz("Issues:", globalThis.issues);
monaco.editor.setModelMarkers(model, "EN-US ", globalThis.issues);
}
}),
engl_lint: editor.addAction({
id: "engl-lint",
label: "Check spelling and style",
keybindings: [[monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyI], [monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyO]],
contextMenuOrder: -0.2,
contextMenuGroupId: "2_lint",
run: async function (edi) {
let model = edi.getModel();
/* Speed run getting API key revoked: */
let API_KEY = "1iKQaZAKfHX9MQz"; /* Free trial API key, just for POC ... don't take ... Can't be bothered to fix ... */
console.time("LINT");
const response = await xhr(`https://svc.webspellchecker.net/api?cmd=check&lang=en_US&format=json&customerid=${API_KEY}&text=${model.getValue().replaceAll(/[\n\:]/g, " ")}`);
console.timeEnd("LINT");
let errors = JSON.parse(response.responseText);
let errs = errors.result[0].matches;
let mark = [];
errs.forEach(function (err) {
let ereStart = model.getPositionAt(err.offset + 0);
let ereEnd = model.getPositionAt(err.offset + 0 + err.length);
err.suggestions.forEach(function (i) {
mark.push({
startLineNumber: ereStart.lineNumber,
endLineNumber: ereEnd.lineNumber,
startColumn: ereStart.column,
endColumn: ereEnd.column,
code: `${err.type}`,
message: `${err.message} (${err.type})
Fix: [${i}]`,
severity: err.type === "spelling" ? 0 : 2,
source: "EN-US ",
relatedInformation: {message: i, resource: model.uri}
});
});
});
const intersection = (a, b) => a.filter(x => b.includes(x));
let c = intersection(globalThis.issues, mark);
globalThis.issues = mark;
monaco.editor.setModelMarkers(model, "EN-US ", globalThis.issues);
}
}),
open_edits_from: editor.addAction({
id: "open-edits-from",
label: "Open a file",
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyO],
contextMenuOrder: 0,
run: function (edi) {
openFile().then(() => edi.setValue(globalThis.txtValue));
}
})
};
monaco.languages.setMonarchTokensProvider(globalThis.langDef, globalThis.tokenThings);
monaco.languages.setLanguageConfiguration(globalThis.langDef, globalThis.config);
globalThis.pr.init();
});
console.timeEnd("MAIN");