forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor.js
114 lines (100 loc) · 3.18 KB
/
cursor.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
import Parchment from 'parchment';
import Embed from './embed';
import TextBlot from './text';
import Emitter from '../core/emitter';
class Cursor extends Embed {
static value() {
return undefined;
}
constructor(domNode, selection) {
super(domNode);
this.selection = selection;
this.textNode = document.createTextNode(Cursor.CONTENTS);
this.domNode.appendChild(this.textNode);
this._length = 0;
}
detach() {
// super.detach() will also clear domNode.__blot
if (this.parent != null) this.parent.removeChild(this);
}
format(name, value) {
if (this._length !== 0) {
return super.format(name, value);
}
let target = this, index = 0;
while (target != null && target.statics.scope !== Parchment.Scope.BLOCK_BLOT) {
index += target.offset(target.parent);
target = target.parent;
}
if (target != null) {
this._length = Cursor.CONTENTS.length;
target.optimize();
target.formatAt(index, Cursor.CONTENTS.length, name, value);
this._length = 0;
}
}
index(node, offset) {
if (node === this.textNode) return 0;
return super.index(node, offset);
}
length() {
return this._length;
}
position() {
return [this.textNode, this.textNode.data.length];
}
remove() {
super.remove();
this.parent = null;
}
restore() {
if (this.selection.composing) return;
if (this.parent == null) return;
let textNode = this.textNode;
let range = this.selection.getNativeRange();
let restoreText, start, end;
if (range != null && range.start.node === textNode && range.end.node === textNode) {
[restoreText, start, end] = [textNode, range.start.offset, range.end.offset];
}
// Link format will insert text outside of anchor tag
while (this.domNode.lastChild != null && this.domNode.lastChild !== this.textNode) {
this.domNode.parentNode.insertBefore(this.domNode.lastChild, this.domNode);
}
if (this.textNode.data !== Cursor.CONTENTS) {
let text = this.textNode.data.split(Cursor.CONTENTS).join('');
if (this.next instanceof TextBlot) {
restoreText = this.next.domNode;
this.next.insertAt(0, text);
this.textNode.data = Cursor.CONTENTS;
} else {
this.textNode.data = text;
this.parent.insertBefore(Parchment.create(this.textNode), this);
this.textNode = document.createTextNode(Cursor.CONTENTS);
this.domNode.appendChild(this.textNode);
}
}
this.remove();
if (start == null) return;
this.selection.emitter.once(Emitter.events.SCROLL_OPTIMIZE, () => {
[start, end] = [start, end].map(function(offset) {
return Math.max(0, Math.min(restoreText.data.length, offset - 1));
});
this.selection.setNativeRange(restoreText, start, restoreText, end);
});
}
update(mutations) {
mutations.forEach((mutation) => {
if (mutation.type === 'characterData' && mutation.target === this.textNode) {
this.restore();
}
});
}
value() {
return '';
}
}
Cursor.blotName = 'cursor';
Cursor.className = 'ql-cursor';
Cursor.tagName = 'span';
Cursor.CONTENTS = "\uFEFF"; // Zero width no break space
export default Cursor;