Files
iTop/js/ckeditor.on-init.js
2024-04-02 10:13:28 +02:00

79 lines
2.8 KiB
JavaScript

/*
* @copyright Copyright (C) 2010-2023 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
// WARNING: This code cannot be placed directly within the page as CKEditor could not be loaded yet.
// As it can be loaded from an XHR call (several times), we need to ensure it will be called when necessary (see PHP WebResourcesHelper)
CKEDITOR.on('instanceReady', function (oEvent) {
// Keyboard listener
oEvent.editor.on('key', function(oKeyEvent){
const oKeyboardEvent = oKeyEvent.data.domEvent.$;
// Submit value on "Ctrl + Enter" or "Meta (Cmd) + Enter" keyboard shortcut
if ((oKeyboardEvent.ctrlKey || oKeyboardEvent.metaKey) && oKeyboardEvent.key === 'Enter') {
$('#'+ oEvent.editor.name).closest('form').trigger('submit');
}
});
// N°4631 - Add a custom class to the CKEditor container when it is in fullscreen mode
// so elements know they should take into account intersecting with the editor
oEvent.editor.on('maximize', function() {
const sFullscreenClass = 'ibo-is-fullscreen';
let container = this.container.getFirst( function( node ) {
return node.type === CKEDITOR.NODE_ELEMENT && node.hasClass( 'cke_inner' );
} );
if (this.commands.maximize.state === CKEDITOR.TRISTATE_ON) {
// The editor is in fullscreen mode, add the custom class
container.addClass(sFullscreenClass);
} else {
// The editor is not in fullscreen mode, remove the custom class
container.removeClass(sFullscreenClass);
}
});
});
// For disabling the CKEditor at init time when the corresponding textarea is disabled !
if ((CKEDITOR !== undefined) && (CKEDITOR.plugins.registered['disabler'] === undefined)) {
CKEDITOR.plugins.add( 'disabler',
{
init : function( editor )
{
editor.on( 'instanceReady', function(e)
{
e.removeListener();
$('#'+ editor.name).trigger('update');
});
}
});
}
// Rewrite the CKEditor Mentions plugin regexp to make it suitable for all Unicode alphabets.
if (CKEDITOR !== undefined && CKEDITOR.plugins.registered['mentions']) {
// From https://github.com/ckeditor/ckeditor4/blob/a3786007fb979d7d7bff3d10c34a2d422935baed/plugins/mentions/plugin.js#L147
function createPattern(marker, minChars) {
// Escape marker if it's a regex token
// https://github.com/tc39/proposal-regex-escaping/blob/main/EscapedChars.md#syntaxcharacter-proposal
const regexTokens = ['^', '$', '\\', '.', '*', '+', '?', '(', ')', '[', ']', '{', '}', '|'];
if (regexTokens.indexOf(marker) >= 0) {
marker = '\\' + marker;
}
let pattern = marker + '[\\p{L}\\p{N}_-]';
if ( minChars ) {
pattern += '{' + minChars + ',}';
} else {
pattern += '*';
}
pattern += '$';
return new RegExp(pattern, 'u');
}
CKEDITOR.on('instanceLoaded', event => {
event.editor.config.mentions.forEach(config => {
config.pattern = createPattern(config.marker, config.minChars);
});
});
}