forked from quoid/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeMirror.svelte
More file actions
263 lines (233 loc) · 8.87 KB
/
Copy pathCodeMirror.svelte
File metadata and controls
263 lines (233 loc) · 8.87 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
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
254
255
256
257
258
259
260
261
262
263
<script context="module">
// once initialized, references the codemirror instance
let instance;
// saved and session code vars are used to track unsaved changes
let savedCode = null, sessionCode = null;
export function cmChanged() {
if (sessionCode === null || sessionCode.localeCompare(savedCode) === 0) {
return false;
}
return true;
}
export function cmSetSavedCode(code) {
savedCode = code;
}
export function cmSetSessionCode(code) {
sessionCode = code;
}
export function cmGetInstance() {
return instance;
}
</script>
<script>
import {createEventDispatcher} from "svelte";
import CodeMirror from "../../codemirror.js";
import {settings, state} from "../../store.js";
import Search from "./Search.svelte";
// the function to be called when save keybind is pressed
export let saveHandler = () => {};
// message dispatcher
const dispatch = createEventDispatcher();
// save ref to textarea element for codemirror initialization
let textarea;
// used to determine whether or not to auto hint
let keysPressed = [];
// store cursor location on save, when re-enabling restore cursor position
let cursor;
// bound to the search component
let search;
// tracks whether search is open
let searchActive = false;
// linter options - https://jshint.com/docs/options/
const lintOptions = {asi: true, esversion: 9};
// update settings when changed
$: if (instance) {
instance.setOption("autoCloseBrackets", $settings.autoCloseBrackets);
instance.setOption("showInvisibles", $settings.showInvisibles);
instance.setOption("tabSize", parseInt($settings.tabSize));
instance.setOption("indentUnit", parseInt($settings.tabSize));
}
// store cursor position and disable on save
$: if ($state.includes("saving")) {
cursor = instance.getCursor();
disable();
}
// re-enable after, focus and set cursor back save completes
$: if ($state.includes("ready") && state.getOldState().includes("saving")) {
enable();
instance.focus();
instance.setCursor(cursor);
}
// disable when trashing or updating file
$: if ($state.includes("trashing") || $state.includes("updating")) {
disable();
}
// reset saved and session code & re-enable after trashing completes
$: if ($state && state.getOldState().includes("trashing")) {
savedCode = null;
sessionCode = null;
enable();
}
// update session code and re-enable after updating completes
$: if ($state && state.getOldState().includes("updating")) {
sessionCode = instance.getValue();
enable();
}
// track lint settings and update accordingly
$: if (instance && $settings.lint) {
toggleLint("enable");
} else if (instance && !$settings.lint) {
toggleLint("disable");
}
export function init() {
// do lint settings check
const lint = $settings.lint ? lintOptions : false;
// create codemirror instance
instance = CodeMirror.fromTextArea(textarea, {
mode: "javascript",
autoCloseBrackets: $settings.autoCloseBrackets,
continueComments: false,
foldGutter: true,
lineNumbers: true,
lineWrapping: true,
matchBrackets: true,
smartIndent: true,
styleActiveLine: true,
indentUnit: parseInt($settings.tabSize),
showInvisibles: $settings.showInvisibles,
tabSize: parseInt($settings.tabSize),
highlightSelectionMatches: false,
lint: lint,
hintOptions: {
useGlobalScope: true
},
gutters: ["CodeMirror-lint-markers", "CodeMirror-linenumbers", "CodeMirror-foldgutter"],
extraKeys: {
"Ctrl-Space": "autocomplete",
"Cmd-/": "toggleComment",
"Cmd-S": () => saveHandler(),
"Cmd-F": () => activateSearch(),
"Esc": () => {searchActive = false},
Tab: cm => {
// convert tabs to spaces and add invisible elements
var s = Array(cm.getOption("indentUnit") + 1).join(" ");
cm.replaceSelection(s);
}
}
});
// when a user hits a key, save key to keysPressed array
// the keys in the keysPressed array will help determine whether or not to show hints
// if it is a backspace key, empty array
instance.on("keydown", (cm, e) => {
if (e.code === "Backspace") return keysPressed = [];
keysPressed.push(e.code);
autoHint(cm);
});
// on key up empty the keysPressed array
// this allows tracking of multiple key pressed (i.e. keybinds)
instance.on("keyup", (cm, e) => keysPressed.splice(keysPressed.indexOf(e.code), 1));
// if codemirror editor loses focus, empty keysPressed array
instance.on("blur", () => keysPressed = []);
instance.on("change", onChange);
instance.on("beforeChange", preventAutoFullStops);
}
function activateSearch() {
if (searchActive) {
search.focusInput();
} else {
searchActive = true;
}
}
function autoHint(cm) {
// check for valid key combinations
const validKeys = keysPressed.every(v => {
// if a single key press, only show hint if letter/number
if (keysPressed.length === 1) {
return v.startsWith("Digit") || v.startsWith("Key");
// if 2 key combo, show hints with shift as well
} else if (keysPressed.length === 2) {
return v.startsWith("Digit") || v.startsWith("Key") || v.startsWith("Shift");
}
});
if (
// check if setting is enabled
$settings.autoHint
// ensure hinting not active already
&& !cm.state.completionActive
// not first position on the line
&& cm.getCursor().ch != 0
// only hint when 1-2 key combos
&& keysPressed.length < 3
// valid keys combo
&& validKeys
) cm.showHint({completeSingle: false});
}
function onChange(cm, e) {
const inputAction = e.origin;
// if search is active, update count on change
if (searchActive) search.getMatches();
// setValue occurs when programmatically changing codemirror values
// below is all other input actions
if (inputAction !== "setValue") {
sessionCode = cm.getValue();
if (cmChanged()) dispatch("message", {name: "enableButtons"});
}
if ((inputAction === "undo" || inputAction === "redo") && !cmChanged()) {
// back to the point where session and saved code are equal, and buttons enabled
dispatch("message", {name: "disableButtons"});
}
}
function preventAutoFullStops(cm, changeObj) {
if (
changeObj.origin === "+input"
&& changeObj.text.length === 1
&& changeObj.text[0] === ". ")
{
changeObj.update(changeObj.from, changeObj.to, [" "]);
}
}
// disables the editor
function disable() {
instance.setOption("readOnly", "nocursor");
instance.display.wrapper.style.opacity = "0.30";
}
// enables the editor
function enable() {
instance.setOption("readOnly", false);
instance.display.wrapper.removeAttribute("style");
}
function toggleLint(disableOrEnable) {
const lint = disableOrEnable === "enable" ? lintOptions : false;
instance.setOption("lint", lint);
instance.refresh();
}
// function that is called for editor component when clicking discard button
export function discardChanges() {
if ($state.includes("ready")) {
instance.setValue(savedCode);
sessionCode = null;
instance.focus();
dispatch("message", {name: "disableButtons"});
}
}
// allow editor component to get direct access to codemirror instance value
export function getValue() {
return instance.getValue();
}
</script>
<textarea bind:this={textarea}></textarea>
<!--
dynamically add search component, since it requires instance to function
if instance is passed to component before instance is set (init is called in Editor component)
instance will remain undefined, which would require importing cmGetInstance in search component
and creating circular dependency
-->
{#if instance}
<svelte:component
this={Search}
active={searchActive}
bind:this={search}
closeHandler={() => {searchActive = false}}
instance={instance}
/>
{/if}