Restore a comment with undo

pull/1/head
yflory 5 years ago
parent 8e4ede3bb8
commit 45d3c1304f

@ -14,9 +14,6 @@
var color2 = 'rgba(252, 231, 3, 0.8)'; var color2 = 'rgba(252, 231, 3, 0.8)';
CKEDITOR.plugins.add('comments', { CKEDITOR.plugins.add('comments', {
//requires: 'dialog,widget',
//icons: 'image',
//hidpi: true,
onLoad: function () { onLoad: function () {
CKEDITOR.addCss('comment { background-color: '+color1+'; }' + CKEDITOR.addCss('comment { background-color: '+color1+'; }' +
'@keyframes color { 0% { background-color: '+color2+'; } 50% { background-color: '+color1+'; } 100% { background-color: '+color2+'; } }' + '@keyframes color { 0% { background-color: '+color2+'; } 50% { background-color: '+color1+'; } 100% { background-color: '+color2+'; } }' +
@ -94,7 +91,12 @@
range.setStart(node, 0); range.setStart(node, 0);
range.setEnd(node, Number.MAX_SAFE_INTEGER); range.setEnd(node, Number.MAX_SAFE_INTEGER);
// Remove style for the comment // Remove style for the comment
console.log(range);
try {
style.removeFromRange(range, editor); style.removeFromRange(range, editor);
} catch (e) {
console.error(e);
}
}); });
setTimeout( function() { setTimeout( function() {

@ -270,8 +270,6 @@ define([
}, function () { }, function () {
// Delete the comment // Delete the comment
delete Env.comments.data[key]; delete Env.comments.data[key];
var els = Env.$inner.find('comment[data-uid="'+key+'"]').toArray();
Env.editor.plugins.comments.uncomment(key, els);
// Send to chainpad // Send to chainpad
updateMetadata(Env); updateMetadata(Env);
@ -354,6 +352,7 @@ define([
var changed = false; var changed = false;
// Get the comments from the document // Get the comments from the document
var toUncomment = {};
var uids = Env.$inner.find('comment').map(function (i, el) { var uids = Env.$inner.find('comment').map(function (i, el) {
var id = el.getAttribute('data-uid'); var id = el.getAttribute('data-uid');
// Empty comment: remove from dom // Empty comment: remove from dom
@ -363,22 +362,36 @@ define([
return; return;
} }
// Comment not in the metadata: uncomment (probably an undo) // Comment not in the metadata: uncomment (probably an undo)
if (comments.indexOf(id) === -1) { var obj = Env.comments.data[id];
Env.editor.plugins.comments.uncomment(id, [el]); if (!obj) {
toUncomment[id] = toUncomment[id] || [];
toUncomment[id].push(el);
changed = true; changed = true;
return; return;
} }
// If this comment was deleted, we're probably using "undo" to restore it:
// remove the "deleted" state and continue
if (obj.deleted) {
delete obj.deleted;
changed = true;
}
return id; return id;
}).toArray(); }).toArray();
if (Object.keys(toUncomment).length) {
Object.keys(toUncomment).forEach(function (id) {
Env.editor.plugins.comments.uncomment(id, toUncomment[id]);
});
}
// Check if a comment has been deleted // Check if a comment has been deleted
comments.forEach(function (uid) { comments.forEach(function (uid) {
if (uids.indexOf(uid) !== -1) { return; } if (uids.indexOf(uid) !== -1) { return; }
// comment has been deleted // comment has been deleted
var data = Env.comments.data[uid]; var data = Env.comments.data[uid];
if (!data) { return; } if (!data) { return; }
//data.deleted = true; data.deleted = true;
delete Env.comments.data[uid]; //delete Env.comments.data[uid];
changed = true; changed = true;
}); });

Loading…
Cancel
Save