Skip to content
Snippets Groups Projects
Commit 126538b7 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

use dataset for id storage not element attributes and optimize textarea replacement, fixes #3698

Closes #3698

Merge request studip/studip!2567
parent 34b47010
No related branches found
No related tags found
No related merge requests found
...@@ -20,7 +20,10 @@ STUDIP.domReady(() => { ...@@ -20,7 +20,10 @@ STUDIP.domReady(() => {
// hidden, the editor does not function properly; therefore attach to // hidden, the editor does not function properly; therefore attach to
// visible textareas only // visible textareas only
function replaceVisibleTextareas() { function replaceVisibleTextareas() {
const textareas = document.querySelectorAll('textarea.wysiwyg'); const textareas = document.querySelectorAll('textarea.wysiwyg:not(.has-editor)');
textareas.forEach(STUDIP.wysiwyg.replace); textareas.forEach(node => {
STUDIP.wysiwyg.replace(node);
node.classList.add('has-editor');
});
} }
}); });
...@@ -59,44 +59,26 @@ async function replaceTextarea(textarea) { ...@@ -59,44 +59,26 @@ async function replaceTextarea(textarea) {
return editor; return editor;
} }
function destroyTextarea(textarea) {
if (!hasEditor(textarea)) {
throw new Error('Trying to destroy a non-existing editor instance.');
}
const editor = getEditor(textarea);
editor.destroy(true);
unsetEditor(textarea);
}
// create an unused id
function createNewId(prefix) {
var i = 0;
while ($('#' + prefix + i).length > 0) {
i++;
}
return prefix + i;
}
const instances = new Map(); const instances = new Map();
let internalIdCounter = 0;
function getEditor(textarea) { function getEditor(textarea) {
return textarea?.id !== '' ? instances.get(textarea.id) : null; return textarea.dataset.editorId !== undefined ? instances.get(textarea.dataset.editorId) : null;
} }
function hasEditor(textarea) { function hasEditor(textarea) {
return textarea.id !== '' && instances.has(textarea.id); return textarea.dataset.editorId !== undefined && instances.has(textarea.dataset.editorId);
} }
function setEditor(textarea, editor) { function setEditor(textarea, editor) {
if (textarea.id === '') { if (textarea.dataset.editorId === undefined) {
textarea.id = createNewId('wysiwyg'); textarea.dataset.editorId = String(internalIdCounter++);
} }
instances.set(textarea.id, editor); instances.set(textarea.dataset.editorId, editor);
} }
function unsetEditor(textarea) { function unsetEditor(textarea) {
instances.delete(textarea.id); instances.delete(textarea.dataset.editorId);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment