cryptpad/www/pad/comment.js

195 lines
7.4 KiB
JavaScript
Raw Normal View History

2020-04-29 15:53:36 +00:00
(function() {
var CKEDITOR = window.CKEDITOR;
2020-04-08 14:16:04 +00:00
2020-04-29 15:53:36 +00:00
function isUnstylable(el) {
2020-05-04 11:30:08 +00:00
if (el.hasClass('cke_widget_mathjax')) {
return false;
}
2020-04-24 08:50:07 +00:00
if (el.hasClass('cke_widget_mediatag')) {
return false;
}
2020-04-29 15:53:36 +00:00
var b = el.getAttribute('contentEditable') === 'false' ||
el.getAttribute('data-nostyle');
2020-04-08 14:16:04 +00:00
return b;
}
// COPYPASTED from mediatag-plugin-dialog.js
var isReadOnly = function (el) {
if (!el) { return; }
var parent = el;
while (parent) {
if (parent.nodeName.toUpperCase() === 'BODY') {
return parent.getAttribute("contenteditable") === 'false';
}
parent = parent.parentElement;
}
};
2020-04-29 15:53:36 +00:00
var color1 = 'rgba(249, 230, 65, 1.0)';
var color2 = 'rgba(252, 181, 0, 1.0)';
2020-04-08 14:16:04 +00:00
CKEDITOR.plugins.add('comments', {
2020-04-29 15:53:36 +00:00
onLoad: function() {
CKEDITOR.addCss('comment { background-color: ' + color1 + '; }' +
'@keyframes color { 0% { background-color: ' + color2 + '; } 50% { background-color: ' + color1 + '; } 100% { background-color: ' + color2 + '; } }' +
'comment.active { animation-name: color; animation-duration: 1s; animation-iteration-count: 2; background-color: ' + color2 + '; outline: none;}' +
'comment media-tag { border: 2px solid ' + color1 + ' !important; }' +
'comment.active media-tag { border: 2px solid ' + color2 + ' !important; }' +
2020-04-20 13:22:45 +00:00
'comment * { background-color: transparent !important; }');
2020-04-08 14:16:04 +00:00
},
2020-04-29 15:53:36 +00:00
init: function(editor) {
2020-04-24 10:49:21 +00:00
var Messages = CKEDITOR._commentsTranslations;
2020-04-08 14:16:04 +00:00
var styleDef = {
element: 'comment',
attributes: {
'data-uid': '#(uid)',
2020-04-08 14:16:04 +00:00
},
2020-04-29 15:53:36 +00:00
overrides: [{
2020-04-08 14:16:04 +00:00
element: 'comment'
2020-04-29 15:53:36 +00:00
}],
2020-04-08 14:16:04 +00:00
childRule: isUnstylable
};
2020-04-24 15:53:33 +00:00
var removeStyle = new CKEDITOR.style(styleDef, { 'uid': '' });
2020-04-29 15:53:36 +00:00
var isApplicable = editor.plugins.comments.isApplicable = function(path, sel) {
2020-04-24 15:53:33 +00:00
path = path || editor.elementPath();
sel = sel || editor.getSelection();
var applicable = removeStyle.checkApplicable(path, editor);
2020-06-26 12:03:24 +00:00
var selectedHtml = editor.getSelectedHtml();
if (selectedHtml === null) { return; }
var comments = selectedHtml.$.querySelectorAll('comment');
var hasComments = comments && comments.length;
2020-06-26 12:03:24 +00:00
2020-04-24 15:53:33 +00:00
var isComment = removeStyle.checkActive(path, editor);
var empty = !sel.getSelectedText();
return applicable && !empty && !hasComments && !isComment;
};
2020-04-08 14:16:04 +00:00
// Register the command.
var command = editor.plugins.comments.command = editor.addCommand('comment', {
2020-04-29 15:53:36 +00:00
exec: function(editor) {
2020-04-08 14:16:04 +00:00
if (editor.readOnly) { return; }
editor.focus();
2020-04-20 13:22:45 +00:00
2020-04-08 14:16:04 +00:00
var uid = CKEDITOR.tools.getUniqueId();
2020-04-29 15:53:36 +00:00
editor.plugins.comments.addComment(uid, function() {
2020-04-22 14:23:45 +00:00
// Make an undo spnashot
2020-04-20 13:22:45 +00:00
editor.fire('saveSnapshot');
2020-04-22 14:23:45 +00:00
// Make sure comments won't overlap
2020-04-20 13:22:45 +00:00
editor.removeStyle(removeStyle);
2020-04-22 14:23:45 +00:00
// Add the comment marker
var s = new CKEDITOR.style(styleDef, { 'uid': uid });
editor.applyStyle(s);
2020-04-20 13:22:45 +00:00
// Save the undo snapshot after all changes are affected.
2020-04-29 15:53:36 +00:00
setTimeout(function() {
2020-04-20 13:22:45 +00:00
editor.fire('saveSnapshot');
2020-04-29 15:53:36 +00:00
}, 0);
2020-04-20 13:22:45 +00:00
});
2020-04-08 14:16:04 +00:00
2020-04-20 13:22:45 +00:00
}
});
// Uncomment provided element
2020-04-29 15:53:36 +00:00
editor.plugins.comments.uncomment = function(id, els) {
if (editor.readOnly) { return; }
editor.fire('saveSnapshot');
//Create style for this id
var style = new CKEDITOR.style({
element: 'comment',
attributes: {
'data-uid': id,
},
});
style.alwaysRemoveElement = true;
2020-04-29 15:53:36 +00:00
els.forEach(function(el) {
// Create range for this element
el.removeAttribute('class');
var node = new CKEDITOR.dom.node(el);
var range = editor.createRange();
range.setStart(node, 0);
range.setEnd(node, Number.MAX_SAFE_INTEGER);
// Remove style for the comment
2020-04-24 09:16:56 +00:00
try {
style.removeFromRange(range, editor);
} catch (e) {
console.error(e);
}
});
2020-04-29 15:53:36 +00:00
setTimeout(function() {
editor.fire('saveSnapshot');
2020-04-29 15:53:36 +00:00
}, 0);
};
2020-04-24 10:49:21 +00:00
// Uncomment from context menu, disabled for now...
2020-04-20 13:22:45 +00:00
editor.addCommand('uncomment', {
2020-04-29 15:53:36 +00:00
exec: function(editor, data) {
2020-04-20 13:22:45 +00:00
if (editor.readOnly) { return; }
editor.fire('saveSnapshot');
2020-04-29 15:53:36 +00:00
if (!data ||  !data.id) {
2020-04-22 14:23:45 +00:00
editor.focus();
editor.removeStyle(removeStyle);
2020-04-29 15:53:36 +00:00
setTimeout(function() {
2020-04-22 14:23:45 +00:00
editor.fire('saveSnapshot');
2020-04-29 15:53:36 +00:00
}, 0);
2020-04-22 14:23:45 +00:00
return;
}
2020-04-08 14:16:04 +00:00
}
});
// Register the toolbar button.
2020-04-23 11:52:21 +00:00
if (editor.ui.addButton) {
editor.ui.addButton('Comment', {
2020-04-24 10:49:21 +00:00
label: Messages.comment,
command: 'comment',
2020-04-29 15:53:36 +00:00
icon: '/pad/icons/comment.png',
2020-04-23 11:52:21 +00:00
toolbar: 'insert,10'
});
}
2020-04-24 10:49:21 +00:00
if (editor.addMenuItems) {
editor.addMenuGroup('comments');
editor.addMenuItem('comment', {
label: Messages.comment,
2020-04-29 15:53:36 +00:00
icon: '/pad/icons/comment.png',
2020-04-24 10:49:21 +00:00
command: 'comment',
group: 'comments'
});
/*
editor.addMenuItem('uncomment', {
label: Messages.uncomment,
icon : '/pad/icons/uncomment.png',
command: 'uncomment',
group: 'comments'
});
*/
}
if (editor.contextMenu) {
/*
editor.contextMenu.addListener(function (element, sel, path) {
var isComment = removeStyle.checkActive(path, editor);
if (!isComment) { return; }
return {
uncomment: CKEDITOR.TRISTATE_OFF,
};
});
*/
2020-04-29 15:53:36 +00:00
editor.contextMenu.addListener(function(element, sel, path) {
2020-04-24 15:53:33 +00:00
var applicable = isApplicable(path, sel);
if (!applicable || isReadOnly(element.$)) { return; }
2020-04-24 10:49:21 +00:00
return {
comment: CKEDITOR.TRISTATE_OFF,
};
});
}
2020-04-08 14:16:04 +00:00
}
});
2020-04-30 12:11:50 +00:00
})();