This commit is contained in:
2025-12-04 22:33:05 +01:00
parent 316175e158
commit 9dee06cdd6
145 changed files with 16865 additions and 88 deletions

View File

@@ -0,0 +1,143 @@
/* /assets/js/bridge/custom-blocks-plugin.js (FINALE VERSION 2.0: Erzwungene Sortierung) */
(function(gjs, B){
if (!gjs || !B || !B.CATEGORY_CONFIG) return;
// --- 1. Block-Sortierlogik (Wieder logische Gewichte: 1 < 2 < 3) -----------------------
const getSortWeight = (id) => {
// Logisch: Section (1) < Block (2) < Snippet (3)
if (id.startsWith('custom-section-') || id.startsWith('lib-sec-')) return 1;
if (id.startsWith('custom-block-') || id.startsWith('lib-blk-')) return 2;
if (id.startsWith('custom-snippet-') || id.startsWith('snip-')) return 3;
return 99;
};
const sortBlocksByPrefixAndLabel = (blocks) => {
blocks.sort((a, b) => {
const aId = String((a.get ? a.get('id') : a.id) || '');
const bId = String((b.get ? b.get('id') : b.id) || '');
// Hier nutzen wir die Rohdaten (aSnippet, bSnippet)
const aLabel = String((a.get ? a.get('label') : a.label) || '').toLowerCase();
const bLabel = String((b.get ? b.get('label') : b.label) || '').toLowerCase();
const aWeight = getSortWeight(aId);
const bWeight = getSortWeight(bId);
// 1. Sortierung nach Gewicht (1, 2, 3)
if (aWeight !== bWeight) return aWeight - bWeight;
// 2. Sortierung alphabetisch (a vor b)
if (aLabel < bLabel) return -1;
if (aLabel > bLabel) return 1;
return 0;
});
};
// -----------------------------------------------------------------------------------------
gjs.plugins.add('bridge-custom-blocks', (editor, opts = {}) => {
const config = B.CATEGORY_CONFIG;
const validCatIds = Object.keys(config);
// IDs zur internen Zuweisung
const CAT_CUSTOM_MAIN_ID = 'custom';
const CAT_BAUSTEINE_ID = 'bausteine';
const CAT_BIBLIOTHEK_ID = 'mysnips';
const CAT_LIB_TEMPLATES_ID = 'lib-templates';
const normalizeAndSort = (ed) => {
try {
const bm = ed.BlockManager;
const categories = bm.getCategories ? bm.getCategories() : null;
let allBlocks = bm.getAll().models || bm.getAll();
let customBlocksArray = [];
let otherBlocksArray = [];
// 1. Blöcke neu kategorisieren & trennen
(allBlocks || []).forEach(b => {
const id = String((b.get ? b.get('id') : b.id) || '');
let targetCatId = null;
if (id.startsWith('lib-tpl-ref-')) {
targetCatId = CAT_LIB_TEMPLATES_ID;
} else if (id.startsWith('custom-') || id.startsWith('lib-sec-') || id.startsWith('lib-blk-') || id.startsWith('snip-')) {
targetCatId = CAT_CUSTOM_MAIN_ID;
} else if (id.startsWith('blk-') || id.startsWith('std-')) {
targetCatId = CAT_BAUSTEINE_ID;
} else if (!b.get('category')) {
targetCatId = CAT_BIBLIOTHEK_ID;
}
if (targetCatId) {
b.set('category', targetCatId);
if (targetCatId === CAT_CUSTOM_MAIN_ID) {
customBlocksArray.push(b);
} else {
otherBlocksArray.push(b);
}
} else {
// Blöcke, die nicht zugewiesen wurden (z.B. basic/extra), behalten
otherBlocksArray.push(b);
}
});
// 2. Block-Sortierung INNERHALB der "custom" Kategorie
if (customBlocksArray.length > 0) {
sortBlocksByPrefixAndLabel(customBlocksArray);
console.log('[DEBUG PLUGIN] Custom Blocks intern sortiert.');
}
// NEU: Gesamte BlockManager-Kollektion mit sortierten Custom-Blöcken überschreiben
// Wir nehmen alle sortierten Custom-Blöcke und fügen die anderen Blöcke danach an.
const newBlockOrder = customBlocksArray.concat(otherBlocksArray);
// Dieser Hack sollte die Reihenfolge in der Seitenleiste erzwingen.
if (bm.getAll().reset) {
bm.getAll().reset(newBlockOrder);
console.log('[DEBUG PLUGIN] Gesamte Block-Kollektion mit neuer Sortierung überschrieben.');
}
// 3. Kategorien Aufräumen & Sortieren (wie zuvor)
if (categories && categories.models) {
// Aufräumen
categories.models.slice().forEach(cat => {
const catId = (cat.get('id') || cat.id || '').toLowerCase();
if (!validCatIds.includes(catId) && catId !== 'basic' && catId !== 'extra') {
categories.remove(cat);
}
});
// Labels korrigieren und Kategorie-Sortierung erzwingen
categories.models.forEach(cat => {
const catId = (cat.get('id') || cat.id || '').toLowerCase();
if (config[catId]) {
cat.set('label', config[catId].label);
cat.set('open', config[catId].open ?? true);
}
});
const catOrder = (m) => config[String((m.get('id') || m.id)).toLowerCase()]?.ord || 99;
const arr = categories.models.slice().sort((a,b) => catOrder(a) - catOrder(b));
categories.reset(arr);
}
// 4. Finaler DOM-Sweep
B.enforceCategoryOrder && B.enforceCategoryOrder(ed);
B.renderBlocks && B.renderBlocks(ed);
} catch(e) {
console.error('[CustomPlugin] Error during normalize:', e);
}
};
// 5. Listener
editor.on('block:add block:remove block:reset', () => normalizeAndSort(editor));
editor.on('load', () => {
normalizeAndSort(editor);
setTimeout(() => normalizeAndSort(editor), 100);
setTimeout(() => normalizeAndSort(editor), 800);
setTimeout(() => normalizeAndSort(editor), 1500);
});
});
})(window.grapesjs, window.BridgeParts);