-
Notifications
You must be signed in to change notification settings - Fork 2
/
commands.ts
506 lines (470 loc) · 16.9 KB
/
commands.ts
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/** @module commands */
import { EditorOptions, InputSelection, BasicExtension, PrismEditor } from "../index.js"
import { isMac, preventDefault, languageMap, addTextareaListener } from "../core.js"
import {
getLanguage,
insertText,
getLineBefore,
getLines,
regexEscape,
getModifierCode,
prevSelection,
} from "../utils/index.js"
import { getLineStart } from "../utils/local.js"
let ignoreTab = false
const clipboard = navigator.clipboard
const mod = isMac ? 4 : 2
/**
* Sets whether editors should ignore tab or use it for indentation.
* Users can always toggle this using Ctrl+M / Ctrl+Shift+M (Mac).
*/
const setIgnoreTab = (newState: boolean) => (ignoreTab = newState)
const whitespaceEnd = (str: string) => str.search(/\S|$/)
/**
* Extension that will add automatic indentation, closing of brackets,
* quotes and tags along with the following commands:
*
* - Alt+ArrowUp/Down: Move line up/down
* - Shift+Alt+ArrowUp/Down: Copy line up/down
* - Ctrl+Enter (Cmd+Enter on MacOS): Insert blank line
* - Ctrl+[ (Cmd+[ on MacOS): Outdent line
* - Ctrl+] (Cmd+] on MacOS): Indent line
* - Shift+Ctrl+K (Shift+Cmd + K on MacOS): Delete line
* - Ctrl+/ (Cmd+/ on MacOS): Toggle comment
* - Shift+Alt+A: Toggle block comment
* - Ctrl+M (Ctrl+Shift+M on MacOS): Toggle Tab capturing
* @param selfClosePairs Pairs of self-closing brackets and quotes.
* Must be an array of strings with 2 characters each.
* Defaults to `['""', "''", '``', '()', '[]', '{}']`.
* @param selfCloseRegex Regex controlling whether or not a bracket/quote should
* automatically close based on the character before and after the cursor.
* Defaults to ``/([^$\w'"`]["'`]|.[[({])[.,:;\])}>\s]|.[[({]`/s``.
*/
const defaultCommands =
(
selfClosePairs = ['""', "''", "``", "()", "[]", "{}"],
selfCloseRegex = /([^$\w'"`]["'`]|.[[({])[.,:;\])}>\s]|.[[({]`/s,
): BasicExtension =>
(editor, options) => {
let prevCopy: string
const { keyCommandMap, inputCommandMap, getSelection } = editor
const getIndent = ({ insertSpaces = true, tabSize }: EditorOptions) =>
[insertSpaces ? " " : "\t", insertSpaces ? tabSize || 2 : 1] as const
const scroll = (): true => !editor.extensions.cursor?.scrollIntoView()
/**
* Automatically closes quotes and brackets if text is selected,
* or if the character before and after the cursor matches a regex
* @param wrapOnly If true, the character will only be closed if text is selected.
*/
const selfClose = (
[start, end]: InputSelection,
[open, close]: string,
value: string,
wrapOnly?: boolean,
) =>
(start < end ||
(!wrapOnly && selfCloseRegex.test((value[end - 1] || " ") + open + (value[end] || " ")))) &&
!insertText(editor, open + value.slice(start, end) + close, null, null, start + 1, end + 1)!
const skipIfEqual = ([start, end]: InputSelection, char: string, value: string) =>
start == end && value[end] == char && !editor.setSelection(start + 1)!
/**
* Inserts slightly altered lines while keeping the same selection.
* Used when toggling comments and indenting.
*/
const insertLines = (
old: string[],
newL: string[],
start: number,
end: number,
selectionStart: number,
selectionEnd: number,
) => {
let newLines = newL.join("\n")
if (newLines != old.join("\n")) {
const last = old.length - 1
const lastLine = newL[last]
const oldLastLine = old[last]
const lastDiff = oldLastLine.length - lastLine.length
const firstDiff = newL[0].length - old[0].length
const firstInsersion = start + whitespaceEnd((firstDiff < 0 ? newL : old)[0])
const lastInsersion =
end - oldLastLine.length + whitespaceEnd(lastDiff > 0 ? lastLine : oldLastLine)
const offset = start - end + newLines.length + lastDiff
const newCursorStart =
firstInsersion > selectionStart
? selectionStart
: Math.max(firstInsersion, selectionStart + firstDiff)
const newCursorEnd = selectionEnd + start - end + newLines.length
insertText(
editor,
newLines,
start,
end,
newCursorStart,
selectionEnd < lastInsersion
? newCursorEnd + lastDiff
: Math.max(lastInsersion + offset, newCursorEnd),
)
}
}
const indent = (
outdent: boolean,
lines: string[],
start1: number,
end1: number,
start: number,
end: number,
indentChar: string,
tabSize: number,
) => {
insertLines(
lines,
lines.map(
outdent
? str => str.slice(whitespaceEnd(str) ? tabSize - (whitespaceEnd(str) % tabSize) : 0)
: str => str && indentChar.repeat(tabSize - (whitespaceEnd(str) % tabSize)) + str,
),
start1,
end1,
start,
end,
)
}
inputCommandMap["<"] = (_e, selection, value) => selfClose(selection, "<>", value, true)
selfClosePairs.forEach(([open, close]) => {
const isQuote = open == close
inputCommandMap[open] = (_e, selection, value) =>
((isQuote && skipIfEqual(selection, close, value)) ||
selfClose(selection, open + close, value)) &&
scroll()
if (!isQuote)
inputCommandMap[close] = (_e, selection, value) =>
skipIfEqual(selection, close, value) && scroll()
})
inputCommandMap[">"] = (e, selection, value) => {
const closingTag = languageMap[getLanguage(editor)]?.autoCloseTags?.(selection, value, editor)
if (closingTag) {
insertText(editor, ">" + closingTag, null, null, selection[0] + 1)
preventDefault(e)
}
}
keyCommandMap.Tab = (e, [start, end], value) => {
if (ignoreTab || options.readOnly || getModifierCode(e) & 6) return
const [indentChar, tabSize] = getIndent(options)
const shiftKey = e.shiftKey
const [lines, start1, end1] = getLines(value, start, end)
if (start < end || shiftKey) {
indent(shiftKey, lines, start1, end1, start, end, indentChar, tabSize)
} else insertText(editor, indentChar.repeat(tabSize - ((start - start1) % tabSize)))
return scroll()
}
keyCommandMap.Enter = (e, selection, value) => {
const code = getModifierCode(e) & 7
if (!code || code == mod) {
if (code) selection[0] = selection[1] = getLines(value, selection[1])[2]
const [indentChar, tabSize] = getIndent(options),
[start, end] = selection,
autoIndent = languageMap[getLanguage(editor)]?.autoIndent,
indenationCount =
Math.floor(whitespaceEnd(getLineBefore(value, start)) / tabSize) * tabSize,
extraIndent = autoIndent?.[0]?.(selection, value, editor) ? tabSize : 0,
extraLine = autoIndent?.[1]?.(selection, value, editor),
newText =
"\n" +
indentChar.repeat(indenationCount + extraIndent) +
(extraLine ? "\n" + indentChar.repeat(indenationCount) : "")
if (newText[1] || value[end]) {
insertText(editor, newText, start, end, start + indenationCount + extraIndent + 1)
return scroll()
}
}
}
keyCommandMap.Backspace = (_e, [start, end], value) => {
if (start == end) {
const currentLine = getLineBefore(value, start),
[, tabSize] = getIndent(options),
isPair = selfClosePairs.includes(value.slice(start - 1, start + 1)),
indenationCount = currentLine.length % tabSize || tabSize
if (isPair || (indenationCount != 1 && !/\S|^$/.test(currentLine))) {
insertText(editor, "", start - (isPair ? 1 : indenationCount), start + +isPair)
return scroll()
}
}
}
for (let i = 0; i < 2; i++)
keyCommandMap[i ? "ArrowDown" : "ArrowUp"] = (e, [start, end], value) => {
const code = getModifierCode(e)
if ((code & 0b111) == 1) {
if (code == 1) {
// Moving lines
const newStart = i ? start : getLineStart(value, start) - 1
const newEnd = i ? value.indexOf("\n", end) + 1 : end
if (newStart > -1 && newEnd > 0) {
const [lines, start1, end1] = getLines(value, newStart, newEnd),
line = lines[i ? "pop" : "shift"]()!,
offset = (line.length + 1) * (i ? 1 : -1)
lines[i ? "unshift" : "push"](line)
insertText(editor, lines.join("\n"), start1, end1, start + offset, end + offset)
}
} else {
// Copying lines
const [lines, start1, end1] = getLines(value, start, end)
const str = lines.join("\n"),
offset = i ? str.length + 1 : 0
insertText(editor, str + "\n" + str, start1, end1, start + offset, end + offset)
}
return scroll()
}
}
addTextareaListener(editor, "keydown", e => {
const code = getModifierCode(e),
keyCode = e.keyCode,
[start, end, dir] = getSelection()
if (code == mod && (keyCode == 221 || keyCode == 219)) {
indent(
keyCode == 219,
...getLines(editor.value, start, end),
start,
end,
...getIndent(options),
)
return scroll()
}
if (code == (isMac ? 0b1010 : 0b0010) && keyCode == 77) {
setIgnoreTab(!ignoreTab)
preventDefault(e)
} else if ((keyCode == 191 && code == mod) || (keyCode == 65 && code == 9)) {
const value = editor.value,
isBlock = code == 9,
position = isBlock ? start : getLineStart(value, start),
language = languageMap[getLanguage(editor, position)] || {},
{ line, block } =
language.getComments?.(editor, position, value) || language.comments || {},
[lines, start1, end1] = getLines(value, start, end),
last = lines.length - 1
if (isBlock) {
if (block) {
const [open, close] = block,
text = value.slice(start, end),
pos = value.slice(0, start).search(regexEscape(open) + " ?$"),
matches = RegExp("^ ?" + regexEscape(close)).test(value.slice(end))
if (pos + 1 && matches)
insertText(
editor,
text,
pos,
end + +(value[end] == " ") + close.length,
pos,
pos + end - start,
)
else
insertText(
editor,
`${open} ${text} ${close}`,
start,
end,
start + open.length + 1,
end + open.length + 1,
)
scroll()
}
} else {
if (line) {
const escaped = regexEscape(line),
regex = RegExp(`^\\s*(${escaped} ?|$)`),
regex2 = RegExp(escaped + " ?"),
allWhiteSpace = !/\S/.test(value.slice(start1, end1)),
newLines = lines.map(
lines.every(line => regex.test(line)) && !allWhiteSpace
? str => str.replace(regex2, "")
: str =>
allWhiteSpace || /\S/.test(str) ? str.replace(/^\s*/, `$&${line} `) : str,
)
insertLines(lines, newLines, start1, end1, start, end)
scroll()
} else if (block) {
const [open, close] = block,
insertionPoint = whitespaceEnd(lines[0]),
hasComment = lines[0].startsWith(open, insertionPoint) && lines[last].endsWith(close),
newLines = lines.slice()
newLines[0] = lines[0].replace(
hasComment ? RegExp(regexEscape(open) + " ?") : /(?=\S)|$/,
hasComment ? "" : open + " ",
)
let diff = newLines[0].length - lines[0].length
newLines[last] = hasComment
? newLines[last].replace(RegExp(`( ?${regexEscape(close)})?$`), "")
: newLines[last] + " " + close
let newText = newLines.join("\n")
let firstInsersion = insertionPoint + start1
let newStart = firstInsersion > start ? start : Math.max(start + diff, firstInsersion)
let newEnd =
firstInsersion > end - <any>(start != end)
? end
: Math.min(Math.max(firstInsersion, end + diff), start1 + newText.length)
insertText(editor, newText, start1, end1, newStart, Math.max(newStart, newEnd))
scroll()
}
}
} else if (code == 8 + mod && keyCode == 75) {
const value = editor.value,
[lines, start1, end1] = getLines(value, start, end),
column = dir == "forward" ? end - end1 + lines.pop()!.length : start - start1,
newLineLen = getLines(value, end1 + 1)[0][0].length
insertText(
editor,
"",
start1 - <any>!!start1,
end1 + <any>!start1,
start1 + Math.min(column, newLineLen),
)
return scroll()
}
})
;(["copy", "cut", "paste"] as const).forEach(type =>
addTextareaListener(editor, type, e => {
const [start, end] = getSelection()
if (start == end && clipboard) {
const [[line], start1, end1] = getLines(editor.value, start, end)
if (type == "paste") {
if (e.clipboardData!.getData("text/plain") == prevCopy) {
insertText(editor, prevCopy + "\n", start1, start1, start + prevCopy.length + 1)
scroll()
preventDefault(e)
}
} else {
clipboard.writeText((prevCopy = line))
if (type == "cut") insertText(editor, "", start1, end1 + 1), scroll()
preventDefault(e)
}
}
}),
)
}
export interface EditHistory extends BasicExtension {
/** Clears the history stack. Probably necessary after changing the value of the editor. */
clear(): void
/**
* Sets the active entry relative to the current entry.
*
* @param offset The position you want to move to relative to the current entry.
*
* `EditHistory.go(-1)` would be equivalent to an undo while `EditHistory.go(1)` would
* be equivalent to a redo.
*
* If there's no entry at the specified position, the call does nothing.
*/
go(offset: number): void
/**
* Returns whether or not there exists a history entry at the specified offset relative
* to the current entry.
*
* This method can be used to determine whether a call to {@link EditHistory.go} with the
* same offset will succeed or do nothing.
*/
has(offset: number): boolean
}
/**
* History extension that overrides the undo/redo behavior of the browser.
*
* Without this extension, the browser's native undo/redo is used, which can be sufficient
* in some cases.
*
* It should be noted that the history stack is not automatically cleared when the editors
* value is changed programmatically using `editor.setOptions` Instead you can clear the
* stack any time using {@link EditHistory.clear}.
*
* Once added to an editor, this extension can be accessed from `editor.extensions.history`.
*
* If you want to create a new editor with different extensions while keeping the undo/redo
* history of an old editor, you can! Just add the old editor's history extension instance
* to the new editor. Keep in mind that this will fully break the undo/redo behavior of the
* old editor.
*
* @param historyLimit The maximum size of the history stack. Defaults to 999.
*/
const editHistory = (historyLimit = 999) => {
let sp = 0
let currentEditor: PrismEditor
let allowMerge: boolean
let isTyping = false
let prevInputType: string
let prevData: string | null
let isMerge: boolean
let textarea: HTMLTextAreaElement
let getSelection: PrismEditor["getSelection"]
const stack: [string, InputSelection, InputSelection][] = []
const update = (index: number) => {
if (index >= historyLimit) {
index--
stack.shift()
}
stack.splice((sp = index), historyLimit, [currentEditor.value, getSelection(), getSelection()])
}
const setEditorState = (index: number) => {
if (stack[index]) {
textarea.value = stack[index][0]
textarea.setSelectionRange(...stack[index][index < sp ? 2 : 1])
currentEditor.update()
currentEditor.extensions.cursor?.scrollIntoView()
sp = index
allowMerge = false
}
}
const self: EditHistory = (editor, options) => {
editor.extensions.history = self
currentEditor = editor
getSelection = editor.getSelection
textarea || update(0)
textarea = editor.textarea
editor.addListener("selectionChange", () => {
allowMerge = isTyping
isTyping = false
})
addTextareaListener(editor, "beforeinput", e => {
let data = e.data
let inputType = e.inputType
if (/history/.test(inputType)) {
setEditorState(sp + (inputType[7] == "U" ? -1 : 1))
preventDefault(e)
} else if (
!(isMerge =
allowMerge &&
prevInputType == inputType &&
e.isTrusted &&
!data?.includes("\n") &&
(data != " " || prevData == data))
) {
stack[sp][2] = prevSelection || getSelection()
}
isTyping = true
prevData = data
prevInputType = inputType
})
addTextareaListener(editor, "input", () => update(sp + <any>!isMerge))
addTextareaListener(editor, "keydown", e => {
if (!options.readOnly) {
const code = getModifierCode(e)
const keyCode = e.keyCode
const isUndo = code == mod && keyCode == 90
const isRedo =
(code == mod + 8 && keyCode == 90) || (!isMac && code == mod && keyCode == 89)
if (isUndo) {
setEditorState(sp - 1)
preventDefault(e)
} else if (isRedo) {
setEditorState(sp + 1)
preventDefault(e)
}
}
})
}
self.clear = () => {
update(0)
allowMerge = false
}
self.has = offset => sp + offset in stack
self.go = offset => setEditorState(sp + offset)
return self
}
export { defaultCommands, setIgnoreTab, ignoreTab, editHistory }