Extend Image Traits
See original GitHub issueHello,
i would like to extend the image type with some options, specially with a redirect option on click.
I tried this solution after the editor initialization (i precise that i work in full javascript and non Node.js environment) :
editor.DomComponents.addType(‘image’, {
isComponent: el => el.tagName == ‘img’,
model: {
defaults: {
traits: [
// Strings are automatically converted to text types
‘alt’,
‘title’, // Same as: { type: ‘text’, name: ‘name’ }
{ type: ‘url’, name: ‘href’, placeholder:‘https://google.com’, changeProp: 1 }
],
// As by default, traits are binded to attributes, so to define
// their initial value we can use attributes
attributes: { type: ‘title’, required: false },
},
init() {
// Also the listener changes from change:attributes:*
to change:*
this.on(‘change:url’, this.handlePlhChange);
},
handlePlhChange() {
console.log(‘URL updated’);
},
},
});
So, with that code, i saw my URL input but i can’t trigger the update. My point is to intercept the input update and get the input value to use it to put my “img” DOM into a “a href” DOM
Any help would be great 😃
Best regards
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (3 by maintainers)
Hi again,
here is the final solution to create a a href image :
Now i have to found how to check the URL integrity when you want to update the href variable
Best regards
Here is a solution i found :
editor.DomComponents.addType(‘imagelink’, { // Model definition model: { // Default properties defaults: { tagName: ‘a’, //resizable: true, attributes: { // Default attributes name: ‘Default name’, placeholder: ‘Insert text here’ }, components: ‘
’,
traits: [
{ type: ‘text’, label: ‘Name’, name: ‘name’ },
{ type: ‘text’, label: ‘Placeholder’, name: ‘placeholder’ },
{ type: ‘href’, label: ‘Source’, name: ‘href’ }
],
},
init() {
this.on(‘change:attributes’, this.handleAttrChange);
},
handleAttrChange() {
console.log('Attributes updated: ', this.getAttributes());
},
},
view: {
events: {
dblclick: ‘innerElDblClick’,
click: ‘innerElClick’,
},
innerElClick(ev) {
ev.stopPropagation();
editor.select( this.model );
return false;
},
innerElDblClick(ev) {
ev.stopPropagation();
editor.select( this.model );
return false;
},
}
});
With functions innerElClick and innerElDblClick, when you click throught the editor, it will select the properties of the new type imagelink and allow you to change the source link. By the way, you can also double click on the image to open asset and change the source file image.
I have actually another issue with my new type, i can’t figure out how to resize the component with the mouse drag method… I tried to add the property resizable: true on the model definition (commented in my sample) but it didn’t work…
Could you please tell me if there is a solution to change the size of the whole component (img and href dom) ?
Best regards