This commit is contained in:
2025-12-06 01:22:42 +01:00
parent 756a079b5c
commit a8aa34baee
2 changed files with 48 additions and 43 deletions

View File

@@ -207,16 +207,16 @@ export function initEditor() {
  }
  // ---------- Initialen HTML-Inhalt in Editor pushen (mit Token/Race-Schutz) ----------
  async function pushInitialHtmlToEditor({ mode, html, snippets, ref, token }) {
    if (token !== reqToken) return; // veraltete Anfrage ignorieren
async function pushInitialHtmlToEditor({ mode, html, snippets, ref, token, hasJson }) {
if (token !== reqToken) return; // veraltete Anfrage ignorieren
    const win = iframe?.contentWindow;
const win = iframe?.contentWindow;
    const doc = iframe?.contentDocument;
    // NEU: HTML wird NUR über postMessage gesendet. Die Bridge im iFrame ist verantwortlich
    // dafür, das HTML in GrapesJS zu setzen, NACHDEM ihre Plugins fertig sind.
    try {
      win?.postMessage({ source:'admin', type:'init', mode, html: html || '', snippets: snippets || [], ref: ref || {} }, '*');
win?.postMessage({ source:'admin', type:'init', mode, html: html || '', snippets: snippets || [], ref: ref || {}, hasJson: !!hasJson }, '*');
    } catch {}
    try {
@@ -235,7 +235,7 @@ export function initEditor() {
  }
  // ---------- Öffnen ----------
  async function open(item, resource) {
async function open(item, resource) {
    current = {
      resource: String(resource || activeMode() || 'templates').toLowerCase(),
      id: Number(item?.id || 0),
@@ -259,15 +259,17 @@ export function initEditor() {
    // Daten parallel laden (fresh HTML + kontextgefilterte Snippets + Referenzen)
    let fresh = '';
    let snippets = [];
    let refLib = { sections: [], blocks: [] };
let refLib = { sections: [], blocks: [] };
let hasJson = false;
    await Promise.all([
      (async() => {
        try {
          const row = await apiGet(current.resource, current.id);
          // API liefert jetzt top-level html/content; fallback auf item.*
          fresh = row?.html ?? row?.content ?? row?.item?.html ?? row?.item?.content ?? '';
        } catch {}
try {
const row = await apiGet(current.resource, current.id);
hasJson = !!(row?.content);
// API liefert jetzt top-level html/content; fallback auf item.*
fresh = row?.html ?? row?.content ?? row?.item?.html ?? row?.item?.content ?? '';
} catch {}
      })(),
      (async() => { snippets = await buildSnippetsForContext(current); })(),
      (async() => { refLib   = await buildRefLibForContext(current); })()
@@ -303,29 +305,31 @@ export function initEditor() {
            mode: current.resource,
            html: fresh,
            snippets,
            ref: {
              sections: (refLib.sections || []).map(r => ({ id:r.id, name:r.name, html:r.html || '' })),
              blocks:   (refLib.blocks   || []).map(r => ({ id:r.id, name:r.name, html:r.html || '' }))
            },
            token: myToken
          });
        }
      };
ref: {
sections: (refLib.sections || []).map(r => ({ id:r.id, name:r.name, html:r.html || '' })),
blocks: (refLib.blocks || []).map(r => ({ id:r.id, name:r.name, html:r.html || '' }))
},
token: myToken,
hasJson
});
}
};
      window.addEventListener('message', bridgeListener);
      // Fallback, falls kein Ready ankommt
      setTimeout(() => {
        pushInitialHtmlToEditor({
          mode: current.resource,
          html: fresh,
          snippets,
          ref: {
            sections: (refLib.sections || []).map(r => ({ id:r.id, name:r.name, html:r.html || '' })),
            blocks:   (refLib.blocks   || []).map(r => ({ id:r.id, name:r.name, html:r.html || '' }))
          },
          token: myToken
        });
      }, 1200);
pushInitialHtmlToEditor({
mode: current.resource,
html: fresh,
snippets,
ref: {
sections: (refLib.sections || []).map(r => ({ id:r.id, name:r.name, html:r.html || '' })),
blocks: (refLib.blocks || []).map(r => ({ id:r.id, name:r.name, html:r.html || '' }))
},
token: myToken,
hasJson
});
}, 1200);
    };
    // Jetzt den Editor-Core laden (erst NACH about:blank)