diff --git a/public/assets/js/bridge/blocks-api.js b/public/assets/js/bridge/blocks-api.js index 93070f9..8e2585b 100644 --- a/public/assets/js/bridge/blocks-api.js +++ b/public/assets/js/bridge/blocks-api.js @@ -129,32 +129,17 @@ log(`Starte Registrierung des Komponententyps '${REFERENCE_COMPONENT_TYPE}'.`, '#1E90FF'); - setTimeout(() => { - const ReferenceModel = defaultType.model.extend({ - defaults: { - ...defaultType.model.prototype.defaults, - components: [], - editable: false, - removable: true, - draggable: true, - copyable: true, - droppable: false, - traits: [ - { type: 'text', name: 'lib-id', label: 'Library ID', changeProp: true }, - { type: 'text', name: 'lib-kind', label: 'Library Kind', changeProp: true }, - ], - 'lib-id': '', - 'lib-kind': '', - startContent: '', - rawHtml: '', - }, + setTimeout(() => { + const ReferenceModel = defaultType.model.extend({ - initialize(props = {}, opts = {}) { - defaultType.model.prototype.initialize.apply(this, [props, opts]); + initialize(props = {}, opts = {}) { + defaultType.model.prototype.initialize.apply(this, [props, opts]); - this.on('change:lib-kind change:lib-id', () => { - this.ensureReferenceMetadata(); - this.reloadComponentContent(); + this.applyReferenceDefaults(); + + this.on('change:lib-kind change:lib-id', () => { + this.ensureReferenceMetadata(); + this.reloadComponentContent(); }); this.ensureReferenceMetadata(); @@ -167,10 +152,43 @@ if (startContent) { this.setPreviewHtml(startContent); this.unset('startContent'); - } else if (kind && id) { - this.reloadComponentContent({ forced: true, reason: 'INIT_LOAD' }); - } - }, + } else if (kind && id) { + this.reloadComponentContent({ forced: true, reason: 'INIT_LOAD' }); + } + }, + + applyReferenceDefaults() { + const enforced = { + editable: false, + removable: true, + draggable: true, + copyable: true, + droppable: false, + 'lib-id': this.get('lib-id') || '', + 'lib-kind': this.get('lib-kind') || '', + rawHtml: this.get('rawHtml') || '', + }; + + Object.entries(enforced).forEach(([key, value]) => { + if (typeof this.get(key) === 'undefined') { + this.set(key, value, { silent: true }); + } + }); + + const traits = this.get('traits'); + const hasLibTraits = Array.isArray(traits) && traits.some(t => t?.name === 'lib-id' || t?.name === 'lib-kind'); + if (!hasLibTraits) { + this.set('traits', [ + { type: 'text', name: 'lib-id', label: 'Library ID', changeProp: true }, + { type: 'text', name: 'lib-kind', label: 'Library Kind', changeProp: true }, + ], { silent: true }); + } + + const comps = this.components?.(); + if (comps && typeof comps.reset === 'function' && comps.length) { + comps.reset([]); + } + }, ensureReferenceMetadata() { const attrsCurrent = this.get('attributes') || {};