Files
ndsolutionswebsite/public/js/editor-bridge.js

57 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// public/js/editor-bridge.js
// Bridge between the editor UI (parent) and the live preview iframe.
// Listens for messages from the parent and applies updates to the preview.
(function () {
// Store current config state for quick reference
let siteConfig = {};
let themeConfig = {};
// Helper: apply theme preset CSS variables (assumes theme presets expose a JSON of variables)
function applyTheme(presetKey) {
fetch(`/theme/${presetKey}.json`)
.then((r) => r.json())
.then((vars) => {
const root = document.documentElement;
Object.entries(vars).forEach(([k, v]) => {
root.style.setProperty(`--${k}`, v);
});
})
.catch(() => console.warn('Theme preset not found:', presetKey));
}
// Helper: re-render the preview by injecting the updated HTML (simple approach).
function reloadPreview() {
// For SSR Astro we cannot rerender inside the iframe without a full reload.
// Trigger a reload the parent will send the latest config on load.
window.location.reload();
}
// Message dispatcher
window.addEventListener('message', (event) => {
const { type, payload } = event.data || {};
switch (type) {
case 'load':
siteConfig = payload || {};
// initial load could trigger a render hook if needed
break;
case 'add':
// For simplicity we just reload real implementation would manipulate DOM.
reloadPreview();
break;
case 'preset':
themeConfig.activePreset = payload;
applyTheme(payload);
break;
case 'update':
// payload contains partial siteConfig changes
Object.assign(siteConfig, payload);
reloadPreview();
break;
default:
// ignore unknown messages
break;
}
});
})();