sdsad
This commit is contained in:
@@ -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 insertPlaceholderIntoSelection = (rteInstance, html) => {
|
||||||
const doc = rteInstance && rteInstance.doc
|
const doc = rteInstance && rteInstance.doc
|
||||||
? rteInstance.doc
|
? rteInstance.doc
|
||||||
@@ -586,14 +613,31 @@ const buildPlaceholderHTML = (payload) => {
|
|||||||
log('PLACEHOLDER INFO', 'Bitte zuerst ein Text-Element auswählen.', '#888');
|
log('PLACEHOLDER INFO', 'Bitte zuerst ein Text-Element auswählen.', '#888');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const markerId = `bridge-placeholder-marker-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
const selectionSnapshot = captureRteSelection(rteInstance);
|
||||||
const markerInserted = insertCaretMarker(rteInstance, markerId);
|
if (!selectionSnapshot) {
|
||||||
if (!markerInserted) {
|
log('PLACEHOLDER ERROR', 'Keine Text-Selektion gefunden. Bitte Cursor setzen und erneut versuchen.', 'red', 'error');
|
||||||
log('PLACEHOLDER ERROR', 'Marker konnte nicht gesetzt werden. Bitte erneut versuchen.', 'red', 'error');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
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);
|
setTimeout(() => reparseTextComponent(target, editor), 0);
|
||||||
const cleanupMarker = () => removeMarkerFromComponent(target, markerId, editor);
|
return true;
|
||||||
|
}
|
||||||
|
markerId = null;
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
const cleanupMarker = () => {
|
||||||
|
if (markerId) {
|
||||||
|
removeMarkerFromComponent(target, markerId, editor);
|
||||||
|
markerId = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
openPlaceholderModal(editor, null, {
|
openPlaceholderModal(editor, null, {
|
||||||
onCancel: () => {
|
onCancel: () => {
|
||||||
cleanupMarker();
|
cleanupMarker();
|
||||||
@@ -602,6 +646,18 @@ const buildPlaceholderHTML = (payload) => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
onSubmit: (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);
|
const replaced = replaceMarkerWithPlaceholder(target, markerId, payload, editor);
|
||||||
if (!replaced) {
|
if (!replaced) {
|
||||||
cleanupMarker();
|
cleanupMarker();
|
||||||
|
|||||||
Reference in New Issue
Block a user