Fix race condition when adding a new comment

pull/1/head
yflory 5 years ago
parent a71d75bad3
commit 3722725462

@ -125,7 +125,7 @@
if (editor.ui.addButton) { if (editor.ui.addButton) {
editor.ui.addButton('Comment', { editor.ui.addButton('Comment', {
label: 'COMMENT', label: 'COMMENT',
command: pluginName, command: 'comment',
icon : '/pad/icons/comment.png', icon : '/pad/icons/comment.png',
toolbar: 'insert,10' toolbar: 'insert,10'
}); });

@ -73,7 +73,6 @@ define([
data.avatar = userData.avatar; data.avatar = userData.avatar;
data.profile = userData.profile; data.profile = userData.profile;
data.curvePublic = userData.curvePublic; data.curvePublic = userData.curvePublic;
console.log(data);
return myAuthorId; return myAuthorId;
}; };
@ -143,7 +142,8 @@ define([
var redrawComments = function (Env) { var redrawComments = function (Env) {
// Don't redraw if there were no change // Don't redraw if there were no change
var str = Sortify(Env.comments || {}); var str = Sortify((Env.comments || {}).data || {});
if (str === Env.oldComments) { return; } if (str === Env.oldComments) { return; }
Env.oldComments = str; Env.oldComments = str;
@ -164,7 +164,6 @@ define([
}).toArray(); }).toArray();
var done = []; var done = [];
order.forEach(function (key) { order.forEach(function (key) {
// Avoir duplicates // Avoir duplicates
if (done.indexOf(key) !== -1) { return; } if (done.indexOf(key) !== -1) { return; }
@ -414,11 +413,18 @@ define([
v: canonicalize(Env.editor.getSelection().getSelectedText()) v: canonicalize(Env.editor.getSelection().getSelectedText())
}] }]
}; };
// There may be a race condition between updateMetadata and addMark that causes
// * updateMetadata first: comment not rendered (redrawComments called
// before addMark)
// * addMark first: comment deleted (checkDeleted called before updateMetadata)
// ==> we're going to call updateMetadata first, and we'll invalidate the cache
// of rendered comments to display them properly in redrawComments
updateMetadata(Env); updateMetadata(Env);
addMark(); addMark();
Env.framework.localChange(); Env.framework.localChange();
Env.oldComments = undefined;
}); });
Env.$container.prepend(form).show(); Env.$container.prepend(form).show();
}; };
@ -430,11 +436,6 @@ define([
onChange(Env); onChange(Env);
checkDeleted(Env); checkDeleted(Env);
}; };
var localChange = function (Env) {
if (!Env.ready) { return; }
// Check deleted
checkDeleted(Env);
};
var ready = function (Env) { var ready = function (Env) {
Env.ready = 0; Env.ready = 0;
@ -453,14 +454,12 @@ define([
} }
Env.$container.find('.cp-comment-active').removeClass('cp-comment-active'); Env.$container.find('.cp-comment-active').removeClass('cp-comment-active');
Env.$inner.find('comment.active').removeClass('active'); Env.$inner.find('comment.active').removeClass('active');
Env.$container.find('.cp-comment-form').remove();
}); });
// Unselect comment when clicking on another part of the doc // Unselect comment when clicking on another part of the doc
Env.$inner.on('click', function (e) { Env.$inner.on('click', function (e) {
if ($(e.target).closest('comment').length) { return; } if ($(e.target).closest('comment').length) { return; }
Env.$container.find('.cp-comment-active').removeClass('cp-comment-active'); Env.$container.find('.cp-comment-active').removeClass('cp-comment-active');
Env.$inner.find('comment.active').removeClass('active'); Env.$inner.find('comment.active').removeClass('active');
Env.$container.find('.cp-comment-form').remove();
}); });
Env.$inner.on('click', 'comment', function (e) { Env.$inner.on('click', 'comment', function (e) {
var $comment = $(e.target); var $comment = $(e.target);
@ -484,7 +483,6 @@ define([
return { return {
onContentUpdate: call(onContentUpdate), onContentUpdate: call(onContentUpdate),
localChange: call(localChange),
ready: call(ready) ready: call(ready)
}; };
}; };

Loading…
Cancel
Save