aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+studip@gmail.com>2024-02-16 13:48:46 +0000
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2024-02-16 13:48:46 +0000
commit126538b703bcb78d79f1af810481125c65b62e3d (patch)
treeace5a3044fb44bb5fb87a4768c7df5025f86c131 /resources/assets/javascripts
parent34b470106f8980ed95b3e48231ff8a81d047c68c (diff)
use dataset for id storage not element attributes and optimize textarea replacement, fixes #3698
Closes #3698 Merge request studip/studip!2567
Diffstat (limited to 'resources/assets/javascripts')
-rw-r--r--resources/assets/javascripts/bootstrap/wysiwyg.js7
-rw-r--r--resources/assets/javascripts/lib/wysiwyg.js32
2 files changed, 12 insertions, 27 deletions
diff --git a/resources/assets/javascripts/bootstrap/wysiwyg.js b/resources/assets/javascripts/bootstrap/wysiwyg.js
index 92479d2..fb158bb 100644
--- a/resources/assets/javascripts/bootstrap/wysiwyg.js
+++ b/resources/assets/javascripts/bootstrap/wysiwyg.js
@@ -20,7 +20,10 @@ STUDIP.domReady(() => {
// hidden, the editor does not function properly; therefore attach to
// visible textareas only
function replaceVisibleTextareas() {
- const textareas = document.querySelectorAll('textarea.wysiwyg');
- textareas.forEach(STUDIP.wysiwyg.replace);
+ const textareas = document.querySelectorAll('textarea.wysiwyg:not(.has-editor)');
+ textareas.forEach(node => {
+ STUDIP.wysiwyg.replace(node);
+ node.classList.add('has-editor');
+ });
}
});
diff --git a/resources/assets/javascripts/lib/wysiwyg.js b/resources/assets/javascripts/lib/wysiwyg.js
index 806abaf..f9acb81 100644
--- a/resources/assets/javascripts/lib/wysiwyg.js
+++ b/resources/assets/javascripts/lib/wysiwyg.js
@@ -59,44 +59,26 @@ async function replaceTextarea(textarea) {
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();
+let internalIdCounter = 0;
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) {
- return textarea.id !== '' && instances.has(textarea.id);
+ return textarea.dataset.editorId !== undefined && instances.has(textarea.dataset.editorId);
}
function setEditor(textarea, editor) {
- if (textarea.id === '') {
- textarea.id = createNewId('wysiwyg');
+ if (textarea.dataset.editorId === undefined) {
+ textarea.dataset.editorId = String(internalIdCounter++);
}
- instances.set(textarea.id, editor);
+ instances.set(textarea.dataset.editorId, editor);
}
function unsetEditor(textarea) {
- instances.delete(textarea.id);
+ instances.delete(textarea.dataset.editorId);
}
////////////////////////////////////////////////////////////////////////////////