109 lines
4.0 KiB
JavaScript
109 lines
4.0 KiB
JavaScript
/* /assets/js/bridge/blocks-custom.js (DYNAMIC ELEMENT LOADER) */
|
|
(function () {
|
|
const PluginName = 'blocks-custom';
|
|
const B = window.BridgeParts || (window.BridgeParts = {});
|
|
|
|
if (B.LOG_CONFIG && B.LOG_CONFIG.PLUGINS) {
|
|
B.LOG_CONFIG.PLUGINS[PluginName] = false;
|
|
}
|
|
|
|
const log = (type, message, color = '#FFD700', logType = 'info', force = false) => {
|
|
if (typeof B.log === 'function') {
|
|
B.log(PluginName, `[${type}] ${message}`, color, logType, force);
|
|
} else if (logType === 'error') {
|
|
console.error(`%c[${PluginName} - ${type}] %c${message}`, `color:red; font-weight:bold;`, 'color:inherit;');
|
|
}
|
|
};
|
|
|
|
if (window.__CUSTOM_BLOCKS_LOADED) return;
|
|
window.__CUSTOM_BLOCKS_LOADED = true;
|
|
|
|
const TARGET_CAT_ID = 'bausteine';
|
|
const ALL_CUSTOM_BLOCK_IDS = [];
|
|
|
|
function addOnce(bm, id, def, category = TARGET_CAT_ID) {
|
|
try {
|
|
bm.add(id, { ...def, category });
|
|
ALL_CUSTOM_BLOCK_IDS.push(id);
|
|
log('BLOCK ADD', `Block '${id}' erfolgreich hinzugefügt.`, '#B8860B');
|
|
} catch (e) {
|
|
log('BLOCK ERROR', `Fehler beim Hinzufügen von Block '${id}': ${e.message}`, 'red', 'error');
|
|
}
|
|
}
|
|
|
|
const css = o => Object.entries(o).map(([k, v]) => `${k}:${v}`).join(';');
|
|
|
|
const loadScript = (src) => new Promise((resolve, reject) => {
|
|
const s = document.createElement('script');
|
|
s.src = src;
|
|
s.async = true;
|
|
s.onload = () => resolve();
|
|
s.onerror = (e) => reject(e);
|
|
document.head.appendChild(s);
|
|
});
|
|
|
|
const defaultElementFiles = [
|
|
'text.js',
|
|
'image.js',
|
|
'button.js',
|
|
'table-2xn.js',
|
|
'image-text.js',
|
|
'divider.js',
|
|
'spacer.js',
|
|
'hero.js',
|
|
'footer.js',
|
|
];
|
|
|
|
async function loadElementFiles() {
|
|
const base = B.API_KERNEL_URL || '/api.php';
|
|
const sep = base.includes('?') ? '&' : '?';
|
|
const url = `${base}${sep}action=blocks_custom.list`;
|
|
try {
|
|
const res = await fetch(url, { credentials: 'include' });
|
|
const data = await res.json();
|
|
if (data && data.ok && Array.isArray(data.files) && data.files.length) {
|
|
return data.files;
|
|
}
|
|
} catch {}
|
|
return defaultElementFiles;
|
|
}
|
|
|
|
function register(editor) {
|
|
log('EXECUTION', `Starte Block-Registrierung für ${TARGET_CAT_ID}.`, '#DAA520');
|
|
const bm = editor.BlockManager;
|
|
const basePath = (B.BASE_PATH_BRIDGE || '/assets/js/bridge/') + 'blocks-custom/elements/';
|
|
|
|
loadElementFiles().then(async (files) => {
|
|
const unique = Array.from(new Set(files.filter(Boolean)));
|
|
for (const file of unique) {
|
|
await loadScript(basePath + file).catch(() => {
|
|
log('LOAD ERROR', `Element-Datei konnte nicht geladen werden: ${file}`, 'red', 'error');
|
|
});
|
|
}
|
|
|
|
const fns = Array.isArray(window.BridgeBlocksCustomElements) ? window.BridgeBlocksCustomElements : [];
|
|
fns.forEach((fn) => {
|
|
try {
|
|
fn({ editor, bm, addOnce, css, category: TARGET_CAT_ID, log });
|
|
} catch (e) {
|
|
log('ELEMENT ERROR', e?.message || String(e), 'red', 'error');
|
|
}
|
|
});
|
|
|
|
log('SUCCESS', `Registrierung abgeschlossen. ${ALL_CUSTOM_BLOCK_IDS.length} Blöcke erstellt.`, '#008000', 'info');
|
|
});
|
|
}
|
|
|
|
window.BridgeBlocksCustom = {
|
|
IDS: ALL_CUSTOM_BLOCK_IDS,
|
|
register: register
|
|
};
|
|
|
|
if (B && B.registerGrapesJSPlugin && typeof register === 'function') {
|
|
B.registerGrapesJSPlugin('bridge-blocks-custom', register);
|
|
log('PLUGIN REGISTER', `'bridge-blocks-custom' erfolgreich zur Bridge Plugin Registry hinzugefügt.`, '#008000');
|
|
} else {
|
|
log('CRITICAL ERROR', `BridgeParts oder registerGrapesJSPlugin fehlt! Plugin-Registrierung gescheitert.`, 'red', 'error');
|
|
}
|
|
})();
|