This commit is contained in:
2025-12-10 22:04:45 +01:00
parent 5ddb152acb
commit d978b64d34

View File

@@ -568,28 +568,29 @@
return null; return null;
}; };
const ensurePlaceholderComponent = (editor) => { const cloneValue = (value) => {
const domc = editor.DomComponents; if (Array.isArray(value)) {
if (domc.getType(PLACEHOLDER_COMPONENT)) return; return value.map(cloneValue);
const baseType = domc.getType('text') || domc.getType('default') || {};
const BaseModel = baseType.model || editor.DomComponents.Component;
const BaseView = baseType.view || editor.DomComponents.View;
const placeholderDefaults = {};
if (BaseModel.prototype && BaseModel.prototype.defaults) {
for (const key in BaseModel.prototype.defaults) {
placeholderDefaults[key] = BaseModel.prototype.defaults[key];
}
} }
placeholderDefaults.name = 'Placeholder'; if (value && typeof value === 'object') {
placeholderDefaults.tagName = 'span'; const copy = {};
placeholderDefaults.droppable = false; Object.keys(value).forEach(key => {
placeholderDefaults.attributes = { copy[key] = cloneValue(value[key]);
});
return copy;
}
return value;
};
const defaultPlaceholderProps = {
name: 'Placeholder',
tagName: 'span',
droppable: false,
attributes: {
'data-placeholder-type': 'custom', 'data-placeholder-type': 'custom',
'data-placeholder-key': 'UEBERSCHRIFT', 'data-placeholder-key': 'UEBERSCHRIFT',
}; },
placeholderDefaults.traits = [ traits: [
{ {
type: 'select', type: 'select',
name: 'data-placeholder-type', name: 'data-placeholder-type',
@@ -621,17 +622,37 @@
options: [], options: [],
changeProp: true, changeProp: true,
}, },
]; ],
const baseToolbar = Array.isArray(placeholderDefaults.toolbar) ? placeholderDefaults.toolbar.slice() : []; toolbar: [
baseToolbar.push({ {
attributes: { class: 'fa fa-edit', title: 'Placeholder bearbeiten' }, attributes: { class: 'fa fa-edit', title: 'Placeholder bearbeiten' },
command: 'bridge-placeholder:edit', command: 'bridge-placeholder:edit',
},
],
};
const applyPlaceholderDefaults = (model) => {
if (!model || typeof model.get !== 'function' || typeof model.set !== 'function') {
return;
}
Object.entries(defaultPlaceholderProps).forEach(([key, value]) => {
if (typeof model.get(key) === 'undefined') {
model.set(key, cloneValue(value));
}
}); });
placeholderDefaults.toolbar = baseToolbar; };
const ensurePlaceholderComponent = (editor) => {
const domc = editor.DomComponents;
if (domc.getType(PLACEHOLDER_COMPONENT)) return;
const baseType = domc.getType('text') || domc.getType('default') || {};
const BaseModel = baseType.model || editor.DomComponents.Component;
const BaseView = baseType.view || editor.DomComponents.View;
const PlaceholderModel = BaseModel.extend({ const PlaceholderModel = BaseModel.extend({
defaults: placeholderDefaults,
init() { init() {
applyPlaceholderDefaults(this);
this.listenTo(this, 'change:attributes', this.updatePlaceholderState); this.listenTo(this, 'change:attributes', this.updatePlaceholderState);
this.updatePlaceholderState(); this.updatePlaceholderState();
fetchPlaceholderSchema() fetchPlaceholderSchema()
@@ -720,13 +741,6 @@
} }
}); });
PlaceholderModel.isComponent = function (el) {
if (el && el.hasAttribute && el.hasAttribute('data-placeholder-type')) {
return { type: PLACEHOLDER_COMPONENT };
}
return false;
};
const PlaceholderView = BaseView.extend({ const PlaceholderView = BaseView.extend({
render() { render() {
BaseView.prototype.render.apply(this, arguments); BaseView.prototype.render.apply(this, arguments);
@@ -745,44 +759,49 @@
domc.addType(PLACEHOLDER_COMPONENT, { domc.addType(PLACEHOLDER_COMPONENT, {
model: PlaceholderModel, model: PlaceholderModel,
view: PlaceholderView, view: PlaceholderView,
isComponent(el) {
if (el && el.hasAttribute && el.hasAttribute('data-placeholder-type')) {
return { type: PLACEHOLDER_COMPONENT };
}
return false;
},
}); });
}; };
const patchTextComponentDroppable = (component) => {
if (!component || !component.is || !component.is('text')) {
return;
}
if (component.__bridgePlaceholderDroppable) {
return;
}
component.__bridgePlaceholderDroppable = true;
const originalDroppable = component.get('droppable');
const allowFn = function (source, cmp) {
if (cmp && cmp.get && cmp.get('type') === PLACEHOLDER_COMPONENT) {
return true;
}
if (typeof originalDroppable === 'function') {
return originalDroppable.call(this, source, cmp);
}
if (typeof originalDroppable === 'undefined') {
return true;
}
return originalDroppable;
};
component.set('droppable', allowFn);
};
const ensureTextSupportsPlaceholders = (editor) => { const ensureTextSupportsPlaceholders = (editor) => {
if (editor.__bridgeTextPlaceholderExtended) return; const wrapper = editor.getWrapper && editor.getWrapper();
const domc = editor.DomComponents; if (wrapper) {
const textType = domc.getType('text'); if (typeof wrapper.findType === 'function') {
if (!textType || !textType.model) return; wrapper.findType('text').forEach(patchTextComponentDroppable);
} else if (typeof wrapper.find === 'function') {
const BaseModel = textType.model; wrapper.find('[data-gjs-type="text"]').forEach(patchTextComponentDroppable);
const BaseView = textType.view; }
const baseDefaults = (BaseModel.prototype && BaseModel.prototype.defaults) ? BaseModel.prototype.defaults : {}; }
const originalDroppable = baseDefaults.droppable; editor.on('component:add', (cmp) => patchTextComponentDroppable(cmp));
const baseIsComponent = typeof textType.isComponent === 'function' ? textType.isComponent : null;
const TextModel = BaseModel.extend({
defaults: {
...baseDefaults,
droppable(source, component) {
if (component && component.get && component.get('type') === PLACEHOLDER_COMPONENT) {
return true;
}
if (typeof originalDroppable === 'function') {
return originalDroppable.call(this, source, component);
}
return typeof originalDroppable === 'undefined' ? false : originalDroppable;
},
},
}, {});
domc.addType('text', {
model: TextModel,
isComponent: baseIsComponent || textType.isComponent,
view: BaseView,
});
editor.__bridgeTextPlaceholderExtended = true;
log('TEXT EXTEND', 'Text-Komponenten erlauben jetzt Placeholder als Inline-Drop.', '#DAA520');
}; };
function setTraitOptions(trait, options) { function setTraitOptions(trait, options) {