This repository was archived by the owner on Mar 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorLink.js
More file actions
69 lines (59 loc) · 1.59 KB
/
EditorLink.js
File metadata and controls
69 lines (59 loc) · 1.59 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
class EditorLink extends EditorSublistBase {
constructor(id, entries) {
super(id, '↓', 'link', [
[EditorNode, [2, 2]],
[EditorLabel, [0, 1]],
]);
this.nodes_.addNodeAfter(
entries && entries[0] ? entries[0].getLabel() : null);
this.nodes_.addNodeAfter(
entries && entries[1] ? entries[1].getLabel() : null);
}
serialize() {
return super.serialize({
type: 'link',
from: this.getFrom().serialize(),
to: this.getTo().serialize(),
});
}
getFrom() {
return this.nodes_.getEntries(EditorNode)[0];
}
getTo() {
return this.nodes_.getEntries(EditorNode)[1];
}
flip() {
let entries = this.nodes_.getEntries(EditorNode);
let fromElem = entries[0].getElement();
let toElem = entries[1].getElement();
let fromHasFocus = document.activeElement == fromElem;
let toHasFocus = document.activeElement == toElem;
toElem.parentElement.insertBefore(toElem, fromElem);
if (fromHasFocus) {
fromElem.focus();
} else if (toHasFocus) {
toElem.focus();
}
}
onKeyDown(e) {
super.onKeyDown(e);
switch (e.key) {
case 'f':
this.flip();
e.stopPropagation();
e.preventDefault();
break;
}
}
static unserialize(ser) {
let link = new EditorLink(ser.id);
link.nodes_.clear();
if (ser.label != null) {
link.setLabel(ser.label, ser.labelObj.id);
link.getLabelObj().setHighlight(ser.labelObj.highlight);
}
link.setHighlight(ser.highlight);
link.nodes_.unserialize([ser.from, ser.to]);
return link.getElement();
}
}