changesettracker: Plumb author ID to avoid global pad variable

This commit is contained in:
Richard Hansen 2021-08-14 05:16:22 -04:00
parent 2dfb1c494b
commit b9bea963e4
4 changed files with 11 additions and 5 deletions

View file

@ -119,6 +119,7 @@ const Ace2Editor = function () {
'applyChangesToBase',
'applyPreparedChangesetToBase',
'setUserChangeNotificationCallback',
'setAuthorId',
'setAuthorInfo',
'callWithAce',
'execCommand',

View file

@ -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);
};

View file

@ -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();
}
}

View file

@ -408,6 +408,7 @@ const pad = {
};
const postAceInit = () => {
padeditor.ace.setAuthorId(clientVars.userId);
padeditbar.init();
setTimeout(() => {
padeditor.ace.focus();