-
Notifications
You must be signed in to change notification settings - Fork 4
/
editable.js
139 lines (117 loc) · 4.41 KB
/
editable.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
'use strict';
import Combokeys from 'combokeys'
import UndoManager from 'undo-manager'
import { getSections, setSelection } from './utils/selection'
import { defineNewLine } from './behaviors/utils'
export default class Editable {
constructor(elem, { autoIndent = true,
autoOpen = true,
autoStrip = true,
overwrite = true,
softTabs = 2,
replaceTab = true,
pairs = [['(', ')'], ['[', ']'], ['{', '}'], ['"'], ["'"]],
oninput = () => {},
undoLimit = 0,
behavior,
store,
} = {}) {
const editable = this
const handler = behavior(defineNewLine(), softTabs ? ' '.repeat(softTabs) : '\t')
const undoMgr = new UndoManager()
undoMgr.setLimit(undoLimit)
const setDom = (value) => {
var content = value.prefix + value.selected + value.suffix
elem.textContent = content
oninput(content, value)
setSelection(elem, value.prefix.length, value.prefix.length + value.selected.length)
}
const update = (content) => {
let previous = store()
undoMgr.add({
undo : () => { setDom(previous) },
redo : () => { setDom(content) }
})
store(content)
setDom(content)
}
const keys = new Combokeys(elem)
keys.stopCallback = () => false // work without needing to set combokeys class on elements
keys.bind('mod+z', () => { undoMgr.undo(); return false })
keys.bind('shift+mod+z', () => { undoMgr.redo(); return false })
if (autoIndent) {
keys.bind('enter', () => getSections(elem, ({ prefix, selected, suffix}) => {
update(handler.autoIndent(prefix, selected, suffix))
return false
}))
}
if (autoStrip) {
keys.bind('backspace', () => getSections(elem, ({ prefix, selected, suffix }, selection) => {
if (selection.isCollapsed && handler.testAutoStrip(pairs, prefix, selected, suffix)) {
update(handler.autoStrip(prefix, selected, suffix))
return false
}
}))
}
const fnAutoOpen = (opening, closing) => () => getSections(elem, ({ prefix, selected, suffix }) => {
update(handler.autoOpen(opening, closing, prefix, selected, suffix))
return false
})
const fnOverwrite = (closing) => () => getSections(elem, ({ prefix, selected, suffix }, selection) => {
if (selection.isCollapsed && handler.testOverwrite(closing, prefix, selected, suffix)) {
update(handler.overwrite(closing, prefix, selected, suffix))
return false
}
})
pairs.forEach(([opening, closing]) => {
if (closing) {
if (autoOpen) keys.bind(opening, fnAutoOpen(opening, closing))
if (overwrite) keys.bind(closing, fnOverwrite(closing))
} else {
if (autoOpen && overwrite) {
keys.bind(opening, () => getSections(elem, ({ prefix, selected, suffix }, selection) => {
if (selection.isCollapsed && handler.testOverwrite(opening, prefix, selected, suffix))
update(handler.overwrite(opening, prefix, selected, suffix))
else
update(handler.autoOpen(opening, opening, prefix, selected, suffix))
return false
}))
} else {
if (autoOpen) keys.bind(opening, fnAutoOpen(opening, opening))
if (overwrite) keys.bind(opening, fnOverwrite(opening))
}
}
})
if (replaceTab) {
keys.bind('tab', () => getSections(elem, ({ prefix, selected, suffix }) => {
update(handler.tabIndent(prefix, selected, suffix))
return false
}))
keys.bind('shift+tab', () => getSections(elem, ({ prefix, selected, suffix }) => {
update(handler.tabUnindent(prefix, selected, suffix))
return false
}))
}
editable.inputListener = elem.addEventListener('input', () => getSections(elem, update))
oninput(elem.textContent, store())
// expose for haxxoers
editable.elem = elem
editable.handler = handler
editable.undoMgr = undoMgr
editable.store = store
editable.setDom = setDom
editable.update = update
editable.keys = keys
}
destroy() {
this.elem.removeEventListener('input', this.inputListener)
this.keys.detach()
this.undoMgr.clear()
}
focus() {
this.elem.focus()
}
blur() {
this.elem.blur()
}
}