forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll.ts
252 lines (223 loc) · 6.68 KB
/
scroll.ts
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import {
Scope,
ScrollBlot,
ContainerBlot,
LeafBlot,
ParentBlot,
Registry,
} from 'parchment';
import { Blot, Parent } from 'parchment/dist/typings/blot/abstract/blot';
import Emitter, { EmitterSource } from '../core/emitter';
import Block, { BlockEmbed } from './block';
import Break from './break';
import Container from './container';
function isLine(blot: unknown): blot is Block | BlockEmbed {
return blot instanceof Block || blot instanceof BlockEmbed;
}
interface UpdatableEmbed {
updateContent(change: unknown): void;
}
function isUpdatable(blot: Blot): blot is Blot & UpdatableEmbed {
return typeof (blot as unknown as any).updateContent === 'function';
}
class Scroll extends ScrollBlot {
static blotName = 'scroll';
static className = 'ql-editor';
static tagName = 'DIV';
static defaultChild = Block;
static allowedChildren = [Block, BlockEmbed, Container];
emitter: Emitter;
batch: false | MutationRecord[];
constructor(
registry: Registry,
domNode: HTMLDivElement,
{ emitter }: { emitter: Emitter },
) {
super(registry, domNode);
this.emitter = emitter;
this.batch = false;
this.optimize();
this.enable();
this.domNode.addEventListener('dragstart', e => this.handleDragStart(e));
}
batchStart() {
if (!Array.isArray(this.batch)) {
this.batch = [];
}
}
batchEnd() {
if (!this.batch) return;
const mutations = this.batch;
this.batch = false;
this.update(mutations);
}
emitMount(blot) {
this.emitter.emit(Emitter.events.SCROLL_BLOT_MOUNT, blot);
}
emitUnmount(blot) {
this.emitter.emit(Emitter.events.SCROLL_BLOT_UNMOUNT, blot);
}
emitEmbedUpdate(blot, change) {
this.emitter.emit(Emitter.events.SCROLL_EMBED_UPDATE, blot, change);
}
deleteAt(index, length) {
const [first, offset] = this.line(index);
const [last] = this.line(index + length);
super.deleteAt(index, length);
if (last != null && first !== last && offset > 0) {
if (first instanceof BlockEmbed || last instanceof BlockEmbed) {
this.optimize();
return;
}
const ref =
last.children.head instanceof Break ? null : last.children.head;
first.moveChildren(last, ref);
first.remove();
}
this.optimize();
}
enable(enabled = true) {
this.domNode.setAttribute('contenteditable', enabled ? 'true' : 'false');
}
formatAt(index, length, format, value) {
super.formatAt(index, length, format, value);
this.optimize();
}
handleDragStart(event) {
event.preventDefault();
}
insertAt(index: number, value: string, def?: unknown) {
if (index >= this.length()) {
if (def == null || this.scroll.query(value, Scope.BLOCK) == null) {
const blot = this.scroll.create(this.statics.defaultChild.blotName);
this.appendChild(blot);
if (def == null && value.endsWith('\n')) {
blot.insertAt(0, value.slice(0, -1), def);
} else {
blot.insertAt(0, value, def);
}
} else {
const embed = this.scroll.create(value, def);
this.appendChild(embed);
}
} else {
super.insertAt(index, value, def);
}
this.optimize();
}
insertBefore(blot: Blot, ref?: Blot) {
if (blot.statics.scope === Scope.INLINE_BLOT) {
const wrapper = this.scroll.create(
this.statics.defaultChild.blotName,
) as Parent;
wrapper.appendChild(blot);
super.insertBefore(wrapper, ref);
} else {
super.insertBefore(blot, ref);
}
}
isEnabled() {
return this.domNode.getAttribute('contenteditable') === 'true';
}
leaf(index: number): [LeafBlot | null, number] {
const last = this.path(index).pop();
if (!last) {
return [null, -1];
}
const [blot, offset] = last;
return blot instanceof LeafBlot ? [blot, offset] : [null, -1];
}
line(index: number): [Block | BlockEmbed | null, number] {
if (index === this.length()) {
return this.line(index - 1);
}
// @ts-expect-error TODO: make descendant() generic
return this.descendant(isLine, index);
}
lines(index = 0, length = Number.MAX_VALUE): (Block | BlockEmbed)[] {
const getLines = (
blot: ParentBlot,
blotIndex: number,
blotLength: number,
) => {
let lines = [];
let lengthLeft = blotLength;
blot.children.forEachAt(
blotIndex,
blotLength,
(child, childIndex, childLength) => {
if (isLine(child)) {
lines.push(child);
} else if (child instanceof ContainerBlot) {
lines = lines.concat(getLines(child, childIndex, lengthLeft));
}
lengthLeft -= childLength;
},
);
return lines;
};
return getLines(this, index, length);
}
optimize(context: { [key: string]: any }): void;
optimize(
mutations?: MutationRecord[],
context?: { [key: string]: any },
): void;
optimize(mutations = [], context = {}) {
if (this.batch) return;
super.optimize(mutations, context);
if (mutations.length > 0) {
this.emitter.emit(Emitter.events.SCROLL_OPTIMIZE, mutations, context);
}
}
path(index: number) {
return super.path(index).slice(1); // Exclude self
}
remove() {
// Never remove self
}
update(source?: EmitterSource): void;
update(mutations?: MutationRecord[]): void;
update(mutations?: MutationRecord[] | EmitterSource): void {
if (this.batch) {
if (Array.isArray(mutations)) {
this.batch = this.batch.concat(mutations);
}
return;
}
let source: EmitterSource = Emitter.sources.USER;
if (typeof mutations === 'string') {
source = mutations;
}
if (!Array.isArray(mutations)) {
mutations = this.observer.takeRecords();
}
mutations = mutations.filter(({ target }) => {
const blot = this.find(target, true);
return blot && !isUpdatable(blot);
});
if (mutations.length > 0) {
this.emitter.emit(Emitter.events.SCROLL_BEFORE_UPDATE, source, mutations);
}
super.update(mutations.concat([])); // pass copy
if (mutations.length > 0) {
this.emitter.emit(Emitter.events.SCROLL_UPDATE, source, mutations);
}
}
updateEmbedAt(index: number, key: string, change: unknown) {
// Currently it only supports top-level embeds (BlockEmbed).
// We can update `ParentBlot` in parchment to support inline embeds.
const [blot] = this.descendant(b => b instanceof BlockEmbed, index);
if (blot && blot.statics.blotName === key && isUpdatable(blot)) {
blot.updateContent(change);
}
}
}
export interface ScrollConstructor {
new (
registry: Registry,
domNode: HTMLDivElement,
options: { emitter: Emitter },
): Scroll;
}
export default Scroll;