From b9bea963e4defddec1b0f5ddb13a3f2a9a8fb22e Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 14 Aug 2021 05:16:22 -0400 Subject: [PATCH] changesettracker: Plumb author ID to avoid global `pad` variable --- src/static/js/ace.js | 1 + src/static/js/ace2_inner.js | 1 + src/static/js/changesettracker.js | 13 ++++++++----- src/static/js/pad.js | 1 + 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/static/js/ace.js b/src/static/js/ace.js index 1ebd16505..4aab7d057 100644 --- a/src/static/js/ace.js +++ b/src/static/js/ace.js @@ -119,6 +119,7 @@ const Ace2Editor = function () { 'applyChangesToBase', 'applyPreparedChangesetToBase', 'setUserChangeNotificationCallback', + 'setAuthorId', 'setAuthorInfo', 'callWithAce', 'execCommand', diff --git a/src/static/js/ace2_inner.js b/src/static/js/ace2_inner.js index 77912057e..d876ea0dc 100644 --- a/src/static/js/ace2_inner.js +++ b/src/static/js/ace2_inner.js @@ -684,6 +684,7 @@ function Ace2Inner(editorInfo, cssManagers) { editorInfo.ace_setUserChangeNotificationCallback = (f) => { changesetTracker.setUserChangeNotificationCallback(f); }; + editorInfo.ace_setAuthorId = (authorId) => changesetTracker.setAuthorId(authorId); editorInfo.ace_setAuthorInfo = (author, info) => { setAuthorInfo(author, info); }; diff --git a/src/static/js/changesettracker.js b/src/static/js/changesettracker.js index 30c70aa74..30c32c4e3 100644 --- a/src/static/js/changesettracker.js +++ b/src/static/js/changesettracker.js @@ -64,6 +64,10 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => { let self; return self = { + _authorId: null, + setAuthorId(authorId) { + this._authorId = authorId; + }, isTracking: () => tracking, setBaseText: (text) => { self.setBaseAttributedText(Changeset.makeAText(text), null); @@ -128,7 +132,7 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => { } }); }, - prepareUserChangeset: () => { + prepareUserChangeset() { // If there are user changes to submit, 'changeset' will be the // changeset, else it will be null. let toSubmit; @@ -137,8 +141,7 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => { // that includes old submittedChangeset toSubmit = Changeset.compose(submittedChangeset, userChangeset, apool); } else { - // Get my authorID - const authorId = parent.parent.pad.myUserInfo.userId; + if (!this._authorId) throw new Error('setAuthorId() not yet called'); // Sanitize authorship: Replace all author attributes with this user's author ID in case the // text was copied from another author. @@ -149,8 +152,8 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => { if (op.opcode === '+') { const attribs = AttributeMap.fromString(op.attribs, apool); const oldAuthorId = attribs.get('author'); - if (oldAuthorId != null && oldAuthorId !== authorId) { - attribs.set('author', authorId); + if (oldAuthorId != null && oldAuthorId !== this._authorId) { + attribs.set('author', this._authorId); op.attribs = attribs.toString(); } } diff --git a/src/static/js/pad.js b/src/static/js/pad.js index e831454de..c0696a2bd 100644 --- a/src/static/js/pad.js +++ b/src/static/js/pad.js @@ -408,6 +408,7 @@ const pad = { }; const postAceInit = () => { + padeditor.ace.setAuthorId(clientVars.userId); padeditbar.init(); setTimeout(() => { padeditor.ace.focus();