Newer
Older
/**
* wysiwyg.js - Replace HTML textareas with WYSIWYG editor.
*/
import parseOptions from './parse_options.js';
import FindAndReplace from '@ckeditor/ckeditor5-find-and-replace/src/findandreplace';
import StudipBlockQuote from '../cke/studip-quote/StudipBlockQuote.js';
import WikiLink from '../cke/wiki-link/wiki-link.js';
const wysiwyg = {
// NOTE keep this function in sync with Markup class
htmlMarker: '<!--HTML-->',
htmlMarkerRegExp: /^\s*<!--\s*HTML.*?-->/i,
isHtml: function isHtml(text) {
// NOTE keep this function in sync with
// Markup::isHtml in Markup.class.php
return this.hasHtmlMarker(text);
},
hasHtmlMarker: function hasHtmlMarker(text) {
// NOTE keep this function in sync with
// Markup::hasHtmlMarker in Markup.class.php
return this.htmlMarkerRegExp.test(text);
},
markAsHtml: function markAsHtml(text) {
// NOTE keep this function in sync with
// Markup::markAsHtml in Markup.class.php
if (this.hasHtmlMarker(text) || text.trim() == '') {
return text; // marker already set, don't set twice
}
return this.htmlMarker + text;
getEditor,
hasEditor,
replace: replaceTextarea,
};
export default wysiwyg;
async function replaceTextarea(textarea) {
if (hasEditor(textarea)) {
return getEditor(textarea);
}
setEditor(textarea, {});
await loadMathJax();
const chunk = await STUDIP.loadChunk('wysiwyg');
const $textarea = textarea instanceof jQuery ? textarea : $(textarea);
const { options, editorType } = parseEditorOptions($textarea.attr('data-editor'));
const editor = await createEditor(chunk, textarea, editorType, options);
enhanceEditor($textarea, editor);
setEditor(textarea, editor);
$textarea.trigger('load.wysiwyg');
return editor;
const instances = new Map();

Jan-Hendrik Willms
committed
let internalIdCounter = 0;
function getEditor(textarea) {

Jan-Hendrik Willms
committed
return textarea.dataset.editorId !== undefined ? instances.get(textarea.dataset.editorId) : null;
}
function hasEditor(textarea) {

Jan-Hendrik Willms
committed
return textarea.dataset.editorId !== undefined && instances.has(textarea.dataset.editorId);
}
function setEditor(textarea, editor) {

Jan-Hendrik Willms
committed
if (textarea.dataset.editorId === undefined) {
textarea.dataset.editorId = String(internalIdCounter++);

Jan-Hendrik Willms
committed
instances.set(textarea.dataset.editorId, editor);
}
function unsetEditor(textarea) {

Jan-Hendrik Willms
committed
instances.delete(textarea.dataset.editorId);
////////////////////////////////////////////////////////////////////////////////
function parseEditorOptions(data) {
const result = { options: {}, editorType: 'classic' };
if (data) {
const parsed = parseOptions(data);
const toolbar = getToolbarOptions(parsed);
if (toolbar) {
result.options.toolbar = toolbar;
}
if (parsed.removePlugins) {
result.options.removePlugins = parsed.removePlugins.split(',');
}
if (parsed.extraPlugins) {
const pluginMap = { FindAndReplace, StudipBlockQuote, WikiLink };
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
result.options.extraPlugins = parsed.extraPlugins.split(',').reduce((memo, plugin) => {
return plugin in pluginMap ? [...memo, pluginMap[plugin]] : memo;
}, []);
}
if (parsed.type) {
if (['balloon', 'classic'].includes(parsed.type)) {
result.editorType = parsed.type;
}
}
}
return result;
}
function getToolbarOptions(parsed) {
if (parsed.toolbar === 'small') {
return {
removeItems: [
'undo',
'redo',
'strikethrough',
'horizontalLine',
],
};
} else if (parsed.toolbar === 'minimal') {
return {
items: [
'bold',
'italic',
'underline',
'subscript',
'superscript',
'fontColor',
'fontBackgroundColor',
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'|',
'removeFormat',
'|',
'bulletedList',
'numberedList',
'|',
'link',
'math',
'specialCharacters',
],
};
}
return null;
}
function loadMathJax() {
if (window.MathJax && window.MathJax.Hub) {
return Promise.resolve(window.MathJax);
} else if (window.STUDIP && window.STUDIP.loadChunk) {
return window.STUDIP.loadChunk('mathjax');
}
return Promise.reject(new Error('Could not load MathJax'));
}
function createEditor(chunk, textarea, editorType, options) {
switch (editorType) {
case 'classic':
return chunk.createClassicEditorFromTextarea(textarea, options);
case 'balloon':
return chunk.createBalloonEditorFromTextarea(textarea, options);
}
throw new Error('No such type of WYSIWYG editor.');
}
function enhanceEditor($textarea, ckeditor) {
// make sure HTML marker is always set, in
// case contents are cut-off by the backend
$textarea.closest('form').submit(() => {
// only trigger if the editor is still attached to the textarea
if (getEditor($textarea[0]) === ckeditor) {
ckeditor.setData(wysiwyg.markAsHtml(ckeditor.getData()));
ckeditor.updateSourceElement();
}
});
// focus the editor if requested
if ($textarea.is('[autofocus]')) {
ckeditor.focus();
}
ckeditor.ui.focusTracker.on('change:isFocused', (evt, name, isFocused) => {
if (!isFocused) {
ckeditor.sourceElement.value = wysiwyg.markAsHtml(ckeditor.getData());
}
});
// Tell MathJax v2.7 to leave the editor alone
ckeditor.ui.element.classList.add('tex2jax_ignore');
return ckeditor;
}