diff --git a/src/static/js/changesettracker.js b/src/static/js/changesettracker.js index 94bc5071b..0a0ef9332 100644 --- a/src/static/js/changesettracker.js +++ b/src/static/js/changesettracker.js @@ -142,43 +142,42 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => { // Sanitize authorship // We need to replace all author attribs with thisSession.author, // in case they copy/pasted or otherwise inserted other peoples changes - if (apool.numToAttrib) { - let authorAttr; - for (const attr in apool.numToAttrib) { - if (apool.numToAttrib[attr][0] === 'author' && - apool.numToAttrib[attr][1] === authorId) { - authorAttr = Number(attr).toString(36); - } + let authorAttr; + for (const attr in apool.numToAttrib) { + if (apool.numToAttrib[attr][0] === 'author' && + apool.numToAttrib[attr][1] === authorId) { + authorAttr = Number(attr).toString(36); } - - // Replace all added 'author' attribs with the value of the current user - const cs = Changeset.unpack(userChangeset); - const iterator = Changeset.opIterator(cs.ops); - let op; - const assem = Changeset.mergingOpAssembler(); - - while (iterator.hasNext()) { - op = iterator.next(); - if (op.opcode === '+') { - let newAttrs = ''; - - op.attribs.split('*').forEach((attrNum) => { - if (!attrNum) return; - const attr = apool.getAttrib(parseInt(attrNum, 36)); - if (!attr) return; - if ('author' === attr[0]) { - // replace that author with the current one - newAttrs += `*${authorAttr}`; - } else { newAttrs += `*${attrNum}`; } // overtake all other attribs as is - }); - op.attribs = newAttrs; - } - assem.append(op); - } - assem.endDocument(); - userChangeset = Changeset.pack(cs.oldLen, cs.newLen, assem.toString(), cs.charBank); - Changeset.checkRep(userChangeset); } + + // Replace all added 'author' attribs with the value of the current user + const cs = Changeset.unpack(userChangeset); + const iterator = Changeset.opIterator(cs.ops); + let op; + const assem = Changeset.mergingOpAssembler(); + + while (iterator.hasNext()) { + op = iterator.next(); + if (op.opcode === '+') { + let newAttrs = ''; + + op.attribs.split('*').forEach((attrNum) => { + if (!attrNum) return; + const attr = apool.getAttrib(parseInt(attrNum, 36)); + if (!attr) return; + if ('author' === attr[0]) { + // replace that author with the current one + newAttrs += `*${authorAttr}`; + } else { newAttrs += `*${attrNum}`; } // overtake all other attribs as is + }); + op.attribs = newAttrs; + } + assem.append(op); + } + assem.endDocument(); + userChangeset = Changeset.pack(cs.oldLen, cs.newLen, assem.toString(), cs.charBank); + Changeset.checkRep(userChangeset); + if (Changeset.isIdentity(userChangeset)) toSubmit = null; else toSubmit = userChangeset; }