-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathshortkeys.ts
69 lines (56 loc) · 2.4 KB
/
shortkeys.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
import { wrapCode } from "../transformer/inline-collection";
import { findUpward, breakElement, setCaretAfter, getContextDocument } from "../dom";
import { handleKeyboardEvent } from "./table-keys";
function handleFormattingKeys(ev: KeyboardEvent): boolean {
const { ctrlKey: ctrl, shiftKey: shift, altKey: alt, which } = ev
const document = getContextDocument()
if (false) { }
else if (ctrl && !shift && !alt && which == 73) /* Ctrl+I */ document.execCommand("italic")
else if (ctrl && !shift && !alt && which == 66) /* Ctrl+B */ document.execCommand("bold")
else if (ctrl && !shift && !alt && which == 85) /* Ctrl+U */ document.execCommand("underline")
else if (ctrl && shift && !alt && which == 61) /* Ctrl+Shift+= */ document.execCommand("superscript")
else if (ctrl && !shift && !alt && which == 61) /* Ctrl+= */ document.execCommand("subscript")
else if (ctrl && !shift && !alt && which == 76) /* Ctrl+L */ document.execCommand("justifyLeft")
else if (ctrl && !shift && !alt && which == 69) /* Ctrl+E */ document.execCommand("justifyCenter")
else if (ctrl && !shift && !alt && which == 82) /* Ctrl+R */ document.execCommand("justifyRight")
else return false
return true
}
function toggleInlineElement(ev: KeyboardEvent): boolean {
const { ctrlKey: ctrl, shiftKey: shift, altKey: alt, which } = ev
if (ctrl || shift || alt) return false
const document = getContextDocument()
const sel = document.getSelection()
if (sel.isCollapsed) return false
let caret = sel.focusNode
if (caret.nodeType === Node.ELEMENT_NODE) caret = caret.childNodes[sel.focusOffset] || caret
const range = sel.getRangeAt(0)
if (which === 192) /* ` */ {
let code = findUpward(caret, el => el.nodeName.toLowerCase() === 'code') as HTMLElement
if (code) {
const newCaretAfter = code.lastChild
breakElement(code)
setCaretAfter(newCaretAfter)
} else {
code = wrapCode(range)
setCaretAfter(code)
}
}
else return false
return true
}
export default function bindShortkeys(editor: HTMLElement) {
editor.addEventListener('keydown', ev => {
try {
let handled = false /* add a `false` to align the following methods */
|| handleFormattingKeys(ev)
|| toggleInlineElement(ev)
|| handleKeyboardEvent(ev) // table keys
if (!handled) return
} catch (err) {
return
}
ev.preventDefault()
ev.stopPropagation()
}, false)
}