Skip to content

Commit

Permalink
[bug-fix] stop fetching the same user info from server multiple times…
Browse files Browse the repository at this point in the history
… at editor startup
  • Loading branch information
lencyforce committed Feb 24, 2021
1 parent beea44d commit 76dd820
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 50 deletions.
118 changes: 69 additions & 49 deletions authorship.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,71 +281,91 @@ class AuthorSidebar {
}
}

affectedLines.forEach((line) => {
let lineIndex = allLines.indexOf(line);
self.updateLineAuthor(line, lineIndex);
self.adjustSidebarItemPosition(line, lineIndex);
});
this.updateAffectedLineRecursively(0, affectedLines, allLines);
}

updateLineAuthor(line, lineIndex) {
updateAffectedLineRecursively(current, affectedLines, allLines) {
if (current >= affectedLines.length)
return;

let maxLengthAuthor = 0;
let maxLength = 0;
let line = affectedLines[current];
let lineIndex = allLines.indexOf(line);

let authorLength = {};
let self = this;

let current = line.children.head;
this.updateLineAuthor(line, lineIndex)
.then(() => {
self.adjustSidebarItemPosition(line, lineIndex);
self.updateAffectedLineRecursively(current+1, affectedLines, allLines);
})
.catch((err) => {
console.error(err);
});
}

let sidebarItem = this.sidebarItems[lineIndex];
this.updateAuthorInfoOnSidebarItem(sidebarItem, lineIndex); // Clear previous author info
updateLineAuthor(line, lineIndex) {

if(current) {
while(current) {
let length = current.length();
return new Promise((resolve, reject) => {
let maxLengthAuthor = 0;
let maxLength = 0;

if(!current.domNode.getAttribute) {
// Text node
} else {
let authorId = this.authorAttribute.value(current.domNode);
let authorLength = {};

if(typeof(authorLength[authorId]) === 'undefined') {
authorLength[authorId] = length;
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 {
authorLength[authorId] += length;
}
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;
if(authorLength[authorId] > maxLength) {
maxLength = authorLength[authorId];
maxLengthAuthor = authorId;
}
}
}

current = current.next;
}
current = current.next;
}

if(maxLengthAuthor === 0) {
return;
}
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);
})
.catch((err) => {
console.log(err);
});
} else {
this.updateAuthorInfoOnSidebarItem(sidebarItem, lineIndex, lineAuthorId);
// 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) {
Expand Down
4 changes: 3 additions & 1 deletion demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ let editorOptions = {

let author = authors.find((a) => a.id + '' === authorId);

console.log("user info retrieved from server: " + authorId);

if(author) {
resolve(author);
}else{
Expand Down Expand Up @@ -161,4 +163,4 @@ doc.fetch((err) => {
quill.setContents(del);
document.querySelector(".content").innerHTML = quill.root.innerHTML;
})
});
});

0 comments on commit 76dd820

Please sign in to comment.