This commit is contained in:
2025-12-10 22:50:27 +01:00
parent 3af929b6b5
commit b6e947d401

View File

@@ -197,6 +197,33 @@ const buildPlaceholderHTML = (payload) => {
}
};
const captureRteSelection = (rteInstance) => {
const doc = rteInstance && rteInstance.doc
? rteInstance.doc
: (rteInstance && rteInstance.el && rteInstance.el.ownerDocument) || document;
if (!doc || !doc.getSelection) return null;
const sel = doc.getSelection();
if (!sel || !sel.rangeCount) return null;
return {
doc,
range: sel.getRangeAt(0).cloneRange()
};
};
const restoreRteSelection = (snapshot) => {
if (!snapshot || !snapshot.doc || !snapshot.range) return false;
const sel = snapshot.doc.getSelection && snapshot.doc.getSelection();
if (!sel) return false;
try {
sel.removeAllRanges();
sel.addRange(snapshot.range);
return true;
} catch (err) {
log('PLACEHOLDER WARN', `Auswahl konnte nicht wiederhergestellt werden: ${err && err.message ? err.message : err}`, '#b45309');
return false;
}
};
const insertPlaceholderIntoSelection = (rteInstance, html) => {
const doc = rteInstance && rteInstance.doc
? rteInstance.doc
@@ -586,14 +613,31 @@ const buildPlaceholderHTML = (payload) => {
log('PLACEHOLDER INFO', 'Bitte zuerst ein Text-Element auswählen.', '#888');
return;
}
const markerId = `bridge-placeholder-marker-${Date.now()}-${Math.random().toString(36).slice(2)}`;
const markerInserted = insertCaretMarker(rteInstance, markerId);
if (!markerInserted) {
log('PLACEHOLDER ERROR', 'Marker konnte nicht gesetzt werden. Bitte erneut versuchen.', 'red', 'error');
const selectionSnapshot = captureRteSelection(rteInstance);
if (!selectionSnapshot) {
log('PLACEHOLDER ERROR', 'Keine Text-Selektion gefunden. Bitte Cursor setzen und erneut versuchen.', 'red', 'error');
return;
}
setTimeout(() => reparseTextComponent(target, editor), 0);
const cleanupMarker = () => removeMarkerFromComponent(target, markerId, editor);
let markerId = null;
const restoreSelection = () => restoreRteSelection(selectionSnapshot);
const ensureMarker = () => {
if (markerId) return true;
if (!restoreSelection()) return false;
markerId = `bridge-placeholder-marker-${Date.now()}-${Math.random().toString(36).slice(2)}`;
const inserted = insertPlaceholderIntoSelection(rteInstance, `<span ${PLACEHOLDER_MARKER_ATTR}="${markerId}"></span>`);
if (inserted) {
setTimeout(() => reparseTextComponent(target, editor), 0);
return true;
}
markerId = null;
return false;
};
const cleanupMarker = () => {
if (markerId) {
removeMarkerFromComponent(target, markerId, editor);
markerId = null;
}
};
openPlaceholderModal(editor, null, {
onCancel: () => {
cleanupMarker();
@@ -602,6 +646,18 @@ const buildPlaceholderHTML = (payload) => {
}
},
onSubmit: (payload) => {
if (!ensureMarker()) {
cleanupMarker();
if (restoreSelection()) {
const html = buildPlaceholderHTML(payload);
if (insertPlaceholderIntoSelection(rteInstance, html)) {
setTimeout(() => reparseTextComponent(target, editor), 0);
return true;
}
}
log('PLACEHOLDER ERROR', 'Placeholder konnte nicht eingefügt werden (Marker fehlte).', 'red', 'error');
return false;
}
const replaced = replaceMarkerWithPlaceholder(target, markerId, payload, editor);
if (!replaced) {
cleanupMarker();