You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cryptpad/www/pad/comment.js

195 lines
7.4 KiB
JavaScript

5 years ago
(function() {
var CKEDITOR = window.CKEDITOR;
5 years ago
5 years ago
function isUnstylable(el) {
if (el.hasClass('cke_widget_mathjax')) {
return false;
}
if (el.hasClass('cke_widget_mediatag')) {
return false;
}
5 years ago
var b = el.getAttribute('contentEditable') === 'false' ||
el.getAttribute('data-nostyle');
5 years ago
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;
}
};
5 years ago
var color1 = 'rgba(249, 230, 65, 1.0)';
var color2 = 'rgba(252, 181, 0, 1.0)';
5 years ago
CKEDITOR.plugins.add('comments', {
5 years ago
onLoad: function() {
CKEDITOR.addCss('comment { background-color: ' + color1 + '; color:#333; }' +
5 years ago
'@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; }' +
'comment * { background-color: transparent !important; }');
5 years ago
},
5 years ago
init: function(editor) {
var Messages = CKEDITOR._commentsTranslations;
5 years ago
var styleDef = {
element: 'comment',
attributes: {
'data-uid': '#(uid)',
5 years ago
},
5 years ago
overrides: [{
5 years ago
element: 'comment'
5 years ago
}],
5 years ago
childRule: isUnstylable
};
var removeStyle = new CKEDITOR.style(styleDef, { 'uid': '' });
5 years ago
var isApplicable = editor.plugins.comments.isApplicable = function(path, sel) {
path = path || editor.elementPath();
sel = sel || editor.getSelection();
var applicable = removeStyle.checkApplicable(path, editor);
var selectedHtml = editor.getSelectedHtml();
if (selectedHtml === null) { return; }
var comments = selectedHtml.$.querySelectorAll('comment');
var hasComments = comments && comments.length;
var isComment = removeStyle.checkActive(path, editor);
var empty = !sel.getSelectedText();
return applicable && !empty && !hasComments && !isComment;
};
5 years ago
// Register the command.
4 years ago
editor.plugins.comments.command = editor.addCommand('comment', {
5 years ago
exec: function(editor) {
5 years ago
if (editor.readOnly) { return; }
editor.focus();
5 years ago
var uid = CKEDITOR.tools.getUniqueId();
5 years ago
editor.plugins.comments.addComment(uid, function() {
5 years ago
// Make an undo spnashot
editor.fire('saveSnapshot');
5 years ago
// Make sure comments won't overlap
editor.removeStyle(removeStyle);
5 years ago
// Add the comment marker
var s = new CKEDITOR.style(styleDef, { 'uid': uid });
editor.applyStyle(s);
// Save the undo snapshot after all changes are affected.
5 years ago
setTimeout(function() {
editor.fire('saveSnapshot');
5 years ago
}, 0);
});
5 years ago
}
});
// Uncomment provided element
5 years ago
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;
5 years ago
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
try {
style.removeFromRange(range, editor);
} catch (e) {
console.error(e);
}
});
5 years ago
setTimeout(function() {
editor.fire('saveSnapshot');
5 years ago
}, 0);
};
// Uncomment from context menu, disabled for now...
editor.addCommand('uncomment', {
5 years ago
exec: function(editor, data) {
if (editor.readOnly) { return; }
editor.fire('saveSnapshot');
5 years ago
if (!data ||  !data.id) {
5 years ago
editor.focus();
editor.removeStyle(removeStyle);
5 years ago
setTimeout(function() {
5 years ago
editor.fire('saveSnapshot');
5 years ago
}, 0);
5 years ago
return;
}
5 years ago
}
});
// Register the toolbar button.
5 years ago
if (editor.ui.addButton) {
editor.ui.addButton('Comment', {
label: Messages.comment,
command: 'comment',
5 years ago
icon: '/pad/icons/comment.png',
5 years ago
toolbar: 'insert,10'
});
}
if (editor.addMenuItems) {
editor.addMenuGroup('comments');
editor.addMenuItem('comment', {
label: Messages.comment,
5 years ago
icon: '/pad/icons/comment.png',
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,
};
});
*/
5 years ago
editor.contextMenu.addListener(function(element, sel, path) {
var applicable = isApplicable(path, sel);
if (!applicable || isReadOnly(element.$)) { return; }
return {
comment: CKEDITOR.TRISTATE_OFF,
};
});
}
5 years ago
}
});
})();