/
OtherKey.js
76 lines (61 loc) · 1.82 KB
/
OtherKey.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
import ChangeText from '../Manipulation/ChangeText';
import CombineBlocks from '../Manipulation/CombineBlocks';
const KEY_CODES = {
'shift': 16, 'ctrl': 17, 'alt': 18, 'meta': 91,
'left': 37, 'up': 38, 'right': 39, 'down': 40
};
class OtherKey {
constructor(content, selection) {
this.content = content;
this.selection = selection;
}
static getName() {
return 'other';
}
// catch-all for all content keys
matches(event) {
const ignores = Object.keys(KEY_CODES).map(key => KEY_CODES[key]);
return ignores.indexOf(event.keyCode) === -1;
}
down(callback) {
const guids = this.selection.guids();
const offsets = this.selection.offsets();
let results;
if (this.selection.crossBlock()) {
results = this._combineBlocks().execute(guids, offsets);
}
this._complete(results, callback);
}
up(callback) {
const guids = this.selection.guids();
const offsets = this.selection.offsets();
const node = document.getElementsByName(guids.anchor)[0];
let text;
if (false && node && node.getAttribute('data-figure')) {
const caption = node.getElementsByTagName("figcaption")[0];
text = caption.textContent;
} else {
text = node && node.textContent;
}
const results = this._changeText().execute(guids, offsets, { node: node, text: text });
this._complete(results, callback);
}
_complete(results, callback) {
const content = results ? results.content : this.content;
const position = results ? results.position : null;
callback({
content: content,
position: position,
stopPropagation: false,
preventDefault: false,
emit: false
});
}
_changeText() {
return new ChangeText(this.content);
}
_combineBlocks() {
return new CombineBlocks(this.content);
}
}
export default OtherKey;