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', 'applyChangesToBase',
'applyPreparedChangesetToBase', 'applyPreparedChangesetToBase',
'setUserChangeNotificationCallback', 'setUserChangeNotificationCallback',
'setAuthorId',
'setAuthorInfo', 'setAuthorInfo',
'callWithAce', 'callWithAce',
'execCommand', 'execCommand',

View file

@ -684,6 +684,7 @@ function Ace2Inner(editorInfo, cssManagers) {
editorInfo.ace_setUserChangeNotificationCallback = (f) => { editorInfo.ace_setUserChangeNotificationCallback = (f) => {
changesetTracker.setUserChangeNotificationCallback(f); changesetTracker.setUserChangeNotificationCallback(f);
}; };
editorInfo.ace_setAuthorId = (authorId) => changesetTracker.setAuthorId(authorId);
editorInfo.ace_setAuthorInfo = (author, info) => { editorInfo.ace_setAuthorInfo = (author, info) => {
setAuthorInfo(author, info); setAuthorInfo(author, info);
}; };

View file

@ -64,6 +64,10 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => {
let self; let self;
return self = { return self = {
_authorId: null,
setAuthorId(authorId) {
this._authorId = authorId;
},
isTracking: () => tracking, isTracking: () => tracking,
setBaseText: (text) => { setBaseText: (text) => {
self.setBaseAttributedText(Changeset.makeAText(text), null); 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 // If there are user changes to submit, 'changeset' will be the
// changeset, else it will be null. // changeset, else it will be null.
let toSubmit; let toSubmit;
@ -137,8 +141,7 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => {
// that includes old submittedChangeset // that includes old submittedChangeset
toSubmit = Changeset.compose(submittedChangeset, userChangeset, apool); toSubmit = Changeset.compose(submittedChangeset, userChangeset, apool);
} else { } else {
// Get my authorID if (!this._authorId) throw new Error('setAuthorId() not yet called');
const authorId = parent.parent.pad.myUserInfo.userId;
// Sanitize authorship: Replace all author attributes with this user's author ID in case the // Sanitize authorship: Replace all author attributes with this user's author ID in case the
// text was copied from another author. // text was copied from another author.
@ -149,8 +152,8 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => {
if (op.opcode === '+') { if (op.opcode === '+') {
const attribs = AttributeMap.fromString(op.attribs, apool); const attribs = AttributeMap.fromString(op.attribs, apool);
const oldAuthorId = attribs.get('author'); const oldAuthorId = attribs.get('author');
if (oldAuthorId != null && oldAuthorId !== authorId) { if (oldAuthorId != null && oldAuthorId !== this._authorId) {
attribs.set('author', authorId); attribs.set('author', this._authorId);
op.attribs = attribs.toString(); op.attribs = attribs.toString();
} }
} }

View file

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