/
editor.js
78 lines (64 loc) · 2.22 KB
/
editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* This module can be used to bind CKEditor to a textarea
* <code>
* require(['elgg-ckeditor'], function(editor) {
* editor.bind('textarea');
* });
* </code>
*/
define(['jquery', 'elgg', 'elgg/hooks', 'ckeditor/ckeditor'], function ($, elgg, hooks, CKEDITOR) {
if (!document.getElementById('ckeditor-css')) {
$('head').append('<link rel="stylesheet" type="text/css" id="ckeditor-css" href="' + elgg.get_simplecache_url('ckeditor/editor.css') + '">');
}
$(document).on('submit', 'form', function() {
$(window).off('beforeunload.ckeditor');
});
return {
init: function (selector, editor_type) {
var $input = $(selector);
if (!$input.length) {
return;
}
editor_type = editor_type || $input.data().editorType || 'default';
// store used editor type
$input.data('editorType', editor_type);
require(['ckeditor/config/' + editor_type], function (config) {
config = hooks.trigger('config', 'ckeditor', {'editor': editor_type, 'selector': selector}, config);
CKEDITOR.create(document.querySelector(selector), config)
.then(editor => {
window.editor = editor;
// set classname based on type
$(editor.ui.view.element).addClass('elgg-ckeditor-' + editor_type);
editor.model.document.on('change:data', () => {
editor.updateSourceElement();
$(editor.sourceElement).data('dirty', true);
$(editor.sourceElement).trigger('change');
});
editor.keystrokes.set('Ctrl+Enter', (event, cancel ) => {
$submit_button = $(editor.sourceElement).closest('form').find('button[type="submit"]').eq(0);
if ($submit_button.length) {
$submit_button.trigger('click');
}
cancel();
});
$(window).on('beforeunload.ckeditor', function(event) {
if ($(editor.sourceElement).data('dirty') && $(editor.sourceElement).closest('form').is(':visible')) {
return true;
}
});
});
});
},
destroy: function (selector) {
var $input = $(selector);
if (!$input.length) {
return;
}
var $editable = $input.next().find('.ck-editor__editable');
if (!$editable.length) {
return;
}
$editable[0].ckeditorInstance.destroy();
}
};
});