Update editor.js
This commit is contained in:
+12
-11
@@ -1,16 +1,13 @@
|
|||||||
console.log("Die JavaScript-Datei wurde erfolgreich geladen!");
|
console.log("Die JavaScript-Datei wurde erfolgreich geladen!");
|
||||||
/**
|
|
||||||
* Editor-Steuerung für dynamische Inhaltsblöcke
|
function initEditor() {
|
||||||
*/
|
|
||||||
document.addEventListener("DOMContentLoaded", function() {
|
|
||||||
const form = document.getElementById("editor-form");
|
const form = document.getElementById("editor-form");
|
||||||
|
|
||||||
// Falls das Formular auf der aktuellen Seite nicht existiert, Skript abbrechen
|
|
||||||
if (!form) {
|
if (!form) {
|
||||||
console.error("Skript abgebrochen: Formular nicht gefunden!");
|
console.error("Skript abgebrochen: Formular nicht gefunden!");
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
console.log("Formular gefunden:", form);
|
console.log("Formular gefunden und Editor initialisiert:", form);
|
||||||
}
|
}
|
||||||
|
|
||||||
const container = document.getElementById("block-container");
|
const container = document.getElementById("block-container");
|
||||||
@@ -63,7 +60,6 @@ document.addEventListener("DOMContentLoaded", function() {
|
|||||||
imgPreview.style.display = "block";
|
imgPreview.style.display = "block";
|
||||||
imgPreview.style.marginTop = "10px";
|
imgPreview.style.marginTop = "10px";
|
||||||
|
|
||||||
// Falls bereits ein Bildpfad existiert (z.B. nach einem Reload bei Validierungsfehlern)
|
|
||||||
if (value && typeof value === 'string' && value.startsWith('uploads/')) {
|
if (value && typeof value === 'string' && value.startsWith('uploads/')) {
|
||||||
imgPreview.src = value;
|
imgPreview.src = value;
|
||||||
blockDiv.setAttribute("data-value", value);
|
blockDiv.setAttribute("data-value", value);
|
||||||
@@ -74,7 +70,6 @@ document.addEventListener("DOMContentLoaded", function() {
|
|||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.onload = function(e) {
|
reader.onload = function(e) {
|
||||||
imgPreview.src = e.target.result;
|
imgPreview.src = e.target.result;
|
||||||
// Temporäre Speicherung des Base64-Strings im HTML-Attribut
|
|
||||||
blockDiv.setAttribute("data-value", e.target.result);
|
blockDiv.setAttribute("data-value", e.target.result);
|
||||||
}
|
}
|
||||||
reader.readAsDataURL(this.files[0]);
|
reader.readAsDataURL(this.files[0]);
|
||||||
@@ -104,7 +99,6 @@ document.addEventListener("DOMContentLoaded", function() {
|
|||||||
blocks.push({ type: type, value: value });
|
blocks.push({ type: type, value: value });
|
||||||
});
|
});
|
||||||
|
|
||||||
// JSON-Daten in das unsichtbare Formularfeld schreiben
|
|
||||||
hiddenContentInput.value = JSON.stringify(blocks);
|
hiddenContentInput.value = JSON.stringify(blocks);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -115,9 +109,16 @@ document.addEventListener("DOMContentLoaded", function() {
|
|||||||
initialBlocks.forEach(b => addBlockElement(b.type, b.value));
|
initialBlocks.forEach(b => addBlockElement(b.type, b.value));
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
// Fallback für alten Reintext aus Altbeständen
|
|
||||||
if (hiddenContentInput.value.trim() !== "") {
|
if (hiddenContentInput.value.trim() !== "") {
|
||||||
addBlockElement("text", hiddenContentInput.value);
|
addBlockElement("text", hiddenContentInput.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
// SICHERER START: Prüft, ob das HTML bereits bereit ist
|
||||||
|
if (document.readyState === "loading") {
|
||||||
|
document.addEventListener("DOMContentLoaded", initEditor);
|
||||||
|
} else {
|
||||||
|
// Falls das DOM schon fertig geladen ist, führen wir es direkt aus
|
||||||
|
initEditor();
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user