FEAT: No example shown for extending a component like image type
Question
There is no examples for components like extending image types , the docs seems a little bit difficult to understand and comprehend , if there is example for customizing an image type or the image uploader for a separate component then it would be nice, because we have I have a use case in my project.
Answers (3)
You extend the image as any other component, it's explained here. If you have a particular use case, it's up to you looking at the source of the component implementation
I am facing the same difficulty as @revdfdev - when following the documentation example to extend an existing component, I am struggling to comprehend. For example the documentation says:
comps.addType('my-new-component', {
isComponent: el => {/* ... */},
extend: 'other-defined-component',
model: { ... }, // Will extend the model from 'other-defined-component'
view: { ... }, // Will extend the view from 'other-defined-component'
});
what does one put into the { ... }? This is never explained - just a bunch of dots. If I define model as
model: {
defaults: {
traits: [
'another',
'fred',
],
}
}
then the existing traits id and title are destroyed, nothing is 'extended'. For example, I want to preserve the nice default feature of id being auto allocated, unless I explicitly set its value. If I re-implement 'id' then I'd have to figure how to do this again myself.
There are very few if any examples of using extend - all the examples I can find use this sort of technique e.g. this example:
var domComps = editor.DomComponents;
dType = domComps.getType('default');
dModel = dType.model;
dView = dType.view;
domComps.addType('input', {
model: dModel.extend({
defaults: Object.assign({}, dModel.prototype.defaults, {
traits: dModel.prototype.defaults.traits.concat([
'test'
])
})
},
{
isComponent: function (el) {
if (el.tagName == 'INPUT') {
return { type: 'input' };
}
},
}),
view: dView
});
is this perhaps an older technique that precedes the extend idea? Interesting, in the above example, I get the attribute test without explicitly declaring it?
The only other example of using extend I could find was in #3135 and running here and looks very simple and zen - however it merely shows adding a dblclick handler to the video component and does not show adding additional component attributes or properties and traits.
I've also tried studying existing component plugins but cannot find any that use extend. Incidentally, studying published plugins is an additional level of complexity since they are structured differently than a plugin that I define on the fly within my own project. I'd like a simple example of calling addType that e.g. extends an input or text component with additional attributes and traits - and IMO it should be in the doco.
When you extend the default properties it works like that:
// Component A
defaults: {
propA: 'A',
propB: [1, 2],
}
// Component B which extends Component A
defaults: {
// Component B will have propA as Component A
propB: [3, 4], // this overrides entirely the propB
}
If you need to have propB merged with the one from Component A, you would need something like that
const parentProps = domc.getType('parent-type').model.prototype.defaults;
// ...
defaults: {
propB: [ ...parentProps.traits, 3, 4 ]
}
Interesting, in the above example, I get the attribute test without explicitly declaring it?
Strings fallbacks to a simple text trait type 'attr-name' => { type: 'text', name: 'attr-name' }
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1428
[Question] Define or hide properties for specific components
Hi, I'm wondering if there is the possibility to define properties for specific components. I mean, when I select the component Image I wou...
Issue #3541
[QUESTION] What is the idiomatic way to append and remove classes from components?
Hey there 👋 Love the editor! We're using traits to modify what classes a component has, but that feels a bit hacky as you have to redeclar...
Issue #2404
Question: how to observe children changes in component
Is there an easy way to subscribe/listen to changes in the components/children of my custom component type? Perhaps an event I can subscrib...
Issue #362
How to create custom link components that prompts users to type link/choose link similar to Images Component Modal
Image Components have logic to call Modal and prompt user's interaction for options/configuration of image url. Would like to do similar to...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.