-
Notifications
You must be signed in to change notification settings - Fork 30
/
authorship.js
520 lines (389 loc) · 16.7 KB
/
authorship.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import Quill from "quill";
import EditorEvents from "./editor-events";
const Parchment = Quill.import("parchment");
const Delta = Quill.import("delta");
const AuthorAttribute = new Parchment.Attributor.Class('author', 'ql-author', {
scope: Parchment.Scope.INLINE
});
Quill.register(AuthorAttribute);
import './authorship.css';
class Authorship {
constructor(editor, composition, options) {
this.editor = editor;
this.composition = composition;
this.author = options.author;
this.authorSidebar = new AuthorSidebar(editor.quill, AuthorAttribute, options);
let self = this;
this.editor.on(EditorEvents.beforeSync, () => {
self.authorSidebar.reset();
});
this.editor.on(EditorEvents.beforeSubmitToUpstream, (delta) => {
self.applyLocalFixingDelta(delta);
});
this.editor.on(EditorEvents.editorTextChanged, ({delta, oldDelta}) => {
self.authorSidebar.update(delta, oldDelta);
});
this.editor.on(EditorEvents.undo, ({delta, oldDelta}) => {
self.authorSidebar.update(delta, oldDelta);
});
this.editor.on(EditorEvents.redo, ({delta, oldDelta}) => {
self.authorSidebar.update(delta, oldDelta);
});
}
applyLocalFixingDelta(delta) {
let authorDelta = new Delta();
let authorFormat = { author: this.author.id };
let self = this;
delta.ops.forEach(op => {
if (op.delete) {
return;
}
if (op.insert || (op.retain && op.attributes)) {
// Add authorship to insert/format
op.attributes = op.attributes || {};
op.attributes.author = self.author.id;
// Apply authorship to our own editor
authorDelta.retain(
op.retain || op.insert.length || 1,
authorFormat,
);
} else {
authorDelta.retain(op.retain);
}
});
if (authorDelta.ops.length !== 0) {
self.composition.submitLocalFixingDelta(authorDelta);
}
}
}
class AuthorSidebar {
constructor (quill, authorAttribute, options) {
this.quill = quill;
this.author = options.author;
this.authorAttribute = authorAttribute;
this.options = options;
// Add sidebar container to editor
this.sidebarNode = this.quill.addContainer("ql-author-sidebar");
this.sidebarItems = [];
this.authorsInfo = {};
this.colors = options.colors;
this.identifiedAuthorIds = {};
this.identifiedAuthorCount = 0;
this.styleElement = null;
let self = this;
// On image load, update sidebar position
quill.root.addEventListener(
'load',
function(event){
let domNode = event.target;
if( domNode.tagName === 'IMG'){
let imageBlot = Quill.find(domNode);
if(!imageBlot) {
// Could also be image placeholder
let imagePlaceholderDomNode = domNode.closest(".image-placeholder");
if(imagePlaceholderDomNode) {
imageBlot = Quill.find(imagePlaceholderDomNode);
}
}
if(imageBlot) {
let [lineBlot, offset] = quill.getLine(quill.getIndex(imageBlot));
let lineNumber = quill.getLines().indexOf(lineBlot);
self.adjustSidebarItemPosition(lineBlot, lineNumber);
}
}
},
true // useCapture
);
}
reset () {
this.sidebarNode.classList.add("single-author");
this.sidebarItems.forEach((item) => {
item.remove();
});
this.sidebarItems.length = 0;
this.identifiedAuthorIds = {};
this.identifiedAuthorCount = 0;
// Add the local editor's info into the authorsInfo
this.authorsInfo = {};
this.authorsInfo[this.author.id] = this.author;
// User main color for local editor
this.addStyleForAuthor(this.author.id, this.options.authorColor);
this.createSidebarItem(-1);
}
update (delta, oldDelta) {
this.searchAuthorFromDelta(delta);
// In deletion situations where we cannot determine which lines has been deleted
// we calculate a parallel Delta with each step of the target delta so that we can
// get a revert op of the deletion to find out exactly if there're any new lines been deleted.
let parallelDelta = oldDelta;
let allLines = this.quill.getLines();
let affectedLines = [];
let index = 0;
let latestLineIndex = 0;
let self = this;
for(let i=0; i<delta.ops.length; i++) {
let op = delta.ops[i];
if(op.insert) {
// For embed elements such as image, op.insert is an object and length is 1;
let length = op.insert.length || 1;
let lines = self.quill.getLines(index, length);
if(typeof(op.insert) === 'string') {
let regex = /\n/g;
while ( (regex.exec(op.insert)) ) {
let author = null;
if(op.attributes && op.attributes.author)
author = op.attributes.author;
self.createSidebarItem(latestLineIndex);
}
}
lines.forEach((line) => {
self.addAffectedLine(affectedLines, line);
let lineIndex = allLines.indexOf(line);
if (lineIndex > latestLineIndex) {
latestLineIndex = lineIndex;
}
});
if((latestLineIndex + 1) < allLines.length) {
// Edge case for the next line of line break
self.addAffectedLine(affectedLines, allLines[latestLineIndex + 1]);
}
let dt = new Delta();
dt = dt.retain(index).insert(op.insert, op.attributes);
parallelDelta = parallelDelta.compose(dt);
index = index + length;
} else if (op.retain) {
let lines = this.quill.getLines(index, op.retain);
if(lines.length !== 0) {
lines.forEach((line) => {
if(op.attributes) {
self.addAffectedLine(affectedLines, line);
}
let lineIndex = allLines.indexOf(line);
if (lineIndex > latestLineIndex) {
latestLineIndex = lineIndex;
}
});
}
let dt = new Delta();
dt = dt.retain(op.retain, op.attributes);
parallelDelta = parallelDelta.compose(dt);
index = index + op.retain;
} else if (op.delete) {
let [currentLine, offset] = self.quill.getLine(index);
let currentLineIndex = -1;
if(currentLine) {
currentLineIndex = allLines.indexOf(currentLine);
self.addAffectedLine(affectedLines, currentLine);
}
// A more complicated situation where even if the total line number is not changed, it is likely
// to be a deletion of one line followed by an insertion of new line.
// So we use the parallel delta to invert current delta to find out what exactly has been deleted.
let afterDelete = parallelDelta.compose(new Delta().retain(index).delete(op.delete));
// Edge case for the last line break in the editor
if(currentLineIndex !== -1) {
let diffDelta = afterDelete.diff(parallelDelta, index);
diffDelta.ops.forEach((dop) => {
if(dop.insert) {
let regex = /\n/g;
while ( (regex.exec(dop.insert)) ) {
// A line is deleted.
self.deleteSidebarItem(currentLineIndex, 1);
}
}
});
}
parallelDelta = afterDelete;
// Index doesn't need to be changed since deletion has already been done on quill editor.
} else {
console.log("exceptional behavior");
}
}
this.updateAffectedLineRecursively(0, affectedLines, allLines);
}
updateAffectedLineRecursively(current, affectedLines, allLines) {
if (current >= affectedLines.length)
return;
let line = affectedLines[current];
let lineIndex = allLines.indexOf(line);
let self = this;
this.updateLineAuthor(line, lineIndex)
.then(() => {
self.adjustSidebarItemPosition(line, lineIndex);
self.updateAffectedLineRecursively(current+1, affectedLines, allLines);
})
.catch((err) => {
console.error(err);
});
}
updateLineAuthor(line, lineIndex) {
return new Promise((resolve, reject) => {
let maxLengthAuthor = 0;
let maxLength = 0;
let authorLength = {};
let current = line.children.head;
let sidebarItem = this.sidebarItems[lineIndex];
this.updateAuthorInfoOnSidebarItem(sidebarItem, lineIndex); // Clear previous author info
if(current) {
while(current) {
let length = current.length();
if(!current.domNode.getAttribute) {
// Text node
} else {
let authorId = this.authorAttribute.value(current.domNode);
if(typeof(authorLength[authorId]) === 'undefined') {
authorLength[authorId] = length;
} else {
authorLength[authorId] += length;
}
if(authorLength[authorId] > maxLength) {
maxLength = authorLength[authorId];
maxLengthAuthor = authorId;
}
}
current = current.next;
}
if(maxLengthAuthor === 0) {
resolve();
return;
}
// Update author's name inside sidebar item
let lineAuthorId = maxLengthAuthor;
let self = this;
if(!this.authorsInfo[lineAuthorId]) {
// Author info must be retrieved from the store
self.options.handlers.getAuthorInfoById(lineAuthorId)
.then((author) => {
self.authorsInfo[lineAuthorId] = author;
self.updateAuthorInfoOnSidebarItem(sidebarItem, lineIndex, lineAuthorId);
resolve();
})
.catch((err) => {
reject(err);
});
} else {
this.updateAuthorInfoOnSidebarItem(sidebarItem, lineIndex, lineAuthorId);
resolve();
}
}
});
}
updateAuthorInfoOnSidebarItem(sidebarItem, itemIndex, authorId) {
sidebarItem.className = 'ql-author-item';
sidebarItem.removeAttribute("data-author-id");
let authorNameNode = sidebarItem.querySelector(".author-name");
authorNameNode.innerHTML = "";
if(authorId) {
let authorName = this.authorsInfo[authorId].name;
sidebarItem.className = 'ql-author-item ql-author-' + authorId;
sidebarItem.setAttribute("data-author-id", authorId);
authorNameNode.innerHTML = authorName;
// Check if we're the first item in a same color range
if(itemIndex === 0) {
sidebarItem.classList.add("first");
} else {
let previousSidebarItem = this.sidebarItems[itemIndex - 1];
if(!previousSidebarItem.classList.contains("ql-author-" + authorId)) {
sidebarItem.classList.add("first");
}
}
}
// Check if we're breaking an existing range
if(itemIndex !== this.sidebarItems.length - 1) {
let nextSidebarItem = this.sidebarItems[itemIndex + 1];
if(nextSidebarItem.classList.contains("ql-author-" + authorId)) {
// Next is the same as us
nextSidebarItem.classList.remove("first");
} else {
nextSidebarItem.classList.add("first");
}
}
}
adjustSidebarItemPosition(line, lineIndex) {
let height = line.domNode.offsetHeight;
this.sidebarItems[lineIndex].style.height = height + "px";
let styles = window.getComputedStyle(line.domNode);
this.sidebarItems[lineIndex].style.marginTop = styles.getPropertyValue("margin-top");
this.sidebarItems[lineIndex].style.marginBottom = styles.getPropertyValue("margin-bottom");
}
createSidebarItem(afterIndex) {
let index = (afterIndex + 1);
let item = document.createElement('div');
item.id = 'ql-author-item-' + Math.ceil(Math.random() * 1000000);
item.className = 'ql-author-item';
item.innerHTML = '<div class="color-bar"></div><div class="author-name"></div>';
if(index === 0 || index === this.sidebarItems.length) {
this.sidebarNode.append(item);
this.sidebarItems.push(item);
} else {
this.sidebarNode.insertBefore(item, this.sidebarItems[index]);
this.sidebarItems.splice(index, 0, item);
}
}
deleteSidebarItem(afterIndex, count) {
let elements = this.sidebarItems.splice(afterIndex + 1, count);
elements.forEach((elem) => {
elem.remove();
});
if(afterIndex >= this.sidebarItems.length - 1) {
return;
}
let previousItem = this.sidebarItems[afterIndex];
let nextItem = this.sidebarItems[afterIndex + 1];
if(previousItem.getAttribute("data-author-id") !== nextItem.getAttribute("data-author-id")) {
nextItem.classList.add("first");
}
}
addAffectedLine(affectedLines, line) {
if(affectedLines.indexOf(line) === -1) {
affectedLines.push(line);
}
}
searchAuthorFromDelta(delta) {
let self = this;
delta.ops.forEach((op) => {
if(op.attributes && op.attributes.author) {
let authorId = op.attributes.author;
if(authorId !== self.author.id && typeof(self.identifiedAuthorIds[authorId]) === 'undefined') {
// New author identified.
// Assign a color to this author.
self.identifiedAuthorIds[authorId] = self.identifiedAuthorCount++ % self.colors.length;
// Add css rules to display the color
this.addStyleForAuthor(authorId);
}
}
});
if(self.identifiedAuthorCount >= 1) {
this.sidebarNode.classList.remove("single-author");
}
}
addStyleForAuthor(authorId, preferredColor) {
if (!this.styleElement) {
this.styleElement = document.createElement('style');
this.styleElement.type = 'text/css';
document.documentElement
.getElementsByTagName('head')[0]
.appendChild(this.styleElement);
}
let color = preferredColor || this.findColor(authorId);
if(color) {
let rule = `.ql-author-sidebar .ql-author-${authorId} .color-bar { ` + `background-color:${color}; }\n`;
this.styleElement.sheet.insertRule(rule, 0);
let rule2 = `.ql-author-sidebar .ql-author-${authorId} .author-name { ` + `color:${color}; }\n`;
this.styleElement.sheet.insertRule(rule2, 0);
}
}
findColor(authorId) {
if(typeof(this.identifiedAuthorIds[authorId]) === 'undefined')
return null;
return this.colors[this.identifiedAuthorIds[authorId]];
}
}
const DEBUG = false;
function log(msg){
if(!DEBUG)
{
return;
}
console.log(msg);
}
export default Authorship;