forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor.js
156 lines (140 loc) · 4.28 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { EmbedBlot, Scope } from 'parchment';
import TextBlot from './text';
class Cursor extends EmbedBlot {
static value() {
return undefined;
}
constructor(scroll, domNode, selection) {
super(scroll, domNode);
this.selection = selection;
this.textNode = document.createTextNode(Cursor.CONTENTS);
this.domNode.appendChild(this.textNode);
this.savedLength = 0;
}
detach() {
// super.detach() will also clear domNode.__blot
if (this.parent != null) this.parent.removeChild(this);
}
format(name, value) {
if (this.savedLength !== 0) {
super.format(name, value);
return;
}
let target = this;
let index = 0;
while (target != null && target.statics.scope !== Scope.BLOCK_BLOT) {
index += target.offset(target.parent);
target = target.parent;
}
if (target != null) {
this.savedLength = Cursor.CONTENTS.length;
target.optimize();
target.formatAt(index, Cursor.CONTENTS.length, name, value);
this.savedLength = 0;
}
}
index(node, offset) {
if (node === this.textNode) return 0;
return super.index(node, offset);
}
length() {
return this.savedLength;
}
position() {
return [this.textNode, this.textNode.data.length];
}
remove() {
super.remove();
this.parent = null;
}
restore() {
if (this.selection.composing || this.parent == null) return null;
const range = this.selection.getNativeRange();
// 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,
);
}
const prevTextBlot = this.prev instanceof TextBlot ? this.prev : null;
const prevTextLength = prevTextBlot ? prevTextBlot.length() : 0;
const nextTextBlot = this.next instanceof TextBlot ? this.next : null;
const nextText = nextTextBlot ? nextTextBlot.text : '';
const { textNode } = this;
// take text from inside this blot and reset it
const newText = textNode.data.split(Cursor.CONTENTS).join('');
textNode.data = Cursor.CONTENTS;
// proactively merge TextBlots around cursor so that optimization
// doesn't lose the cursor. the reason we are here in cursor.restore
// could be that the user clicked in prevTextBlot or nextTextBlot, or
// the user typed something.
let mergedTextBlot;
if (prevTextBlot) {
mergedTextBlot = prevTextBlot;
if (newText || nextTextBlot) {
prevTextBlot.insertAt(prevTextBlot.length(), newText + nextText);
if (nextTextBlot) {
nextTextBlot.remove();
}
}
} else if (nextTextBlot) {
mergedTextBlot = nextTextBlot;
nextTextBlot.insertAt(0, newText);
} else {
const newTextNode = document.createTextNode(newText);
mergedTextBlot = this.scroll.create(newTextNode);
this.parent.insertBefore(mergedTextBlot, this);
}
this.remove();
if (range) {
// calculate selection to restore
const remapOffset = (node, offset) => {
if (prevTextBlot && node === prevTextBlot.domNode) {
return offset;
}
if (node === textNode) {
return prevTextLength + offset - 1;
}
if (nextTextBlot && node === nextTextBlot.domNode) {
return prevTextLength + newText.length + offset;
}
return null;
};
const start = remapOffset(range.start.node, range.start.offset);
const end = remapOffset(range.end.node, range.end.offset);
if (start !== null && end !== null) {
return {
startNode: mergedTextBlot.domNode,
startOffset: start,
endNode: mergedTextBlot.domNode,
endOffset: end,
};
}
}
return null;
}
update(mutations, context) {
if (
mutations.some(mutation => {
return (
mutation.type === 'characterData' && mutation.target === this.textNode
);
})
) {
const range = this.restore();
if (range) context.range = range;
}
}
value() {
return '';
}
}
Cursor.blotName = 'cursor';
Cursor.className = 'ql-cursor';
Cursor.tagName = 'span';
Cursor.CONTENTS = '\uFEFF'; // Zero width no break space
export default Cursor;