Extend Image Traits
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
Read full answer below ↓Question
Hello,
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 regardsAnswers (3)
Hi again,
here is the final solution to create a a href image :
editor.DomComponents.addType('imagelink', {
model: {
defaults: {
tagName: 'div',
resizable: {keepAutoHeight: true},
droppable: false,
traits: [
{
type: 'href', label: 'Source', name: 'href'
},
{
type: 'select',
label: 'Target',
name: 'target',
options: [
{ value: '', name: 'This window' },
{ value: '_blank', name: 'New window' }
]
}
],
components: [
{
resizable: false,
draggable: false,
droppable: false,
selectable: false,
hoverable: false,
type: 'link',
components: [{
type: 'image',
attributes: {
src: 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIHZpZXdCb3g9IjAgMCAyNCAyNCIgc3R5bGU9ImZpbGw6IHJnYmEoMCwwLDAsMC4xNSk7IHRyYW5zZm9ybTogc2NhbGUoMC43NSkiPgogICAgICAgIDxwYXRoIGQ9Ik04LjUgMTMuNWwyLjUgMyAzLjUtNC41IDQuNSA2SDVtMTYgMVY1YTIgMiAwIDAgMC0yLTJINWMtMS4xIDAtMiAuOS0yIDJ2MTRjMCAxLjEuOSAyIDIgMmgxNGMxLjEgMCAyLS45IDItMnoiPjwvcGF0aD4KICAgICAgPC9zdmc+',
style:'width:100%;'
},
resizable: false,
draggable: false,
droppable: false,
hoverable: false,
selectable: false,
}]
}
]
},
init() {
this.on('change:attributes:href', this.handleHrefChange);
this.on('change:attributes:target', this.handleTargetChange);
},
handleHrefChange: function ( component, value ) {
delete component.attributes.attributes;
component.attributes.components.models[0].attributes.attributes.href = value;
return false;
},
handleTargetChange: function ( component, value ) {
delete component.attributes.attributes;
component.attributes.components.models[0].attributes.attributes.target = value;
return false;
},
},
});
Now i have to found how to check the URL integrity when you want to update the href variable
Best regards
I have the same need in my project. I tried creating a block, but I couldn't get the initial popup to "pop".
I tried creating a component with a plugin, but there's too much work that I can't understand how it can connect to each other.
Any hints or clues on what we both can do would be great!
Thanks!
You've added this listener change:url but the name of the trait is name: 'href', so just change it with change:href
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1605
Can we implement Paste as Rich text editor options in Rte?
hi @artf I've gone through this Drag and Drop Editor and what an amazing work. I also read your docs and I tried to extend rich text editor...
Issue #1522
[Question] is it possible to add some text to content body where the cursor is ?
I'd like to have a button that is outside the editor and if i click this button, some text should be generated in the body where my cursor...
Issue #3194
How do I repurpose the Spectrum Color Picker?
I would like to repurpose the color picker used in GrapesJS. I have to use a color picker for some project level settings. These settings a...
Issue #2381
[QUESTION] Is it possible to add info modal on the editor
Hi, I would like to add on the grapejs editor a clickable questionmark we can click so we could give information on what does this specific...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.
Browse premium plugins →Related tutorials
In-depth guides on the same topic.
Tutorial
How to Build a Production GrapesJS Editor: The Complete Walkthrough of Brief, Preset, Plugins, and Services
A complete walkthrough of building a production GrapesJS editor: how to choose a preset, pick plugins, and scope setup services without burning a sprint.
Tutorial
Why use grapesjs shadcn for your template builder
Skip months of editor plumbing and start with a fully working visual builder — polished UI included.
Tutorial
Preset DevFuture 2.0 — New Update Released
Preset continues evolving as a clean, production-ready starting point for building visual editors powered by GrapesJS
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.