diff --git a/authorship.js b/authorship.js index 0d0d6ce..623d9f2 100644 --- a/authorship.js +++ b/authorship.js @@ -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) { diff --git a/demo/index.js b/demo/index.js index 4bef431..5f333d4 100644 --- a/demo/index.js +++ b/demo/index.js @@ -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{ @@ -161,4 +163,4 @@ doc.fetch((err) => { quill.setContents(del); document.querySelector(".content").innerHTML = quill.root.innerHTML; }) -}); \ No newline at end of file +});