[Question] Custom request on "src" attributes
Question
I implemented a small GrapesJS editor that allows to edit Django/Jinja2 templates. GrapesJS by default is trying to GET the image preview of my element:
<a href="{{product_url}}" target="_blank"><img src="{{image_url}}" width="130"/></a>
The request happens everytime I load the template into the GrapesJS editor:
http://localhost:8000/%7B%7Bimage_url%7D%7D 404 (Not Found)
GrapesJS editor though works fine even with this 404 call. But I would like to do handle the request to the image. So instead of GrapesJS making a default call to http://localhost:8000/%7B%7Bimage_url%7D%7D I would like to render a placeholder image let's say: https://placekitten.com/200/300
My current configuration:
editor = grapesjs.init({
container: '#gjs',
assetManager: {},
});
editor.setComponents('<a href="{{product_url}}" target="_blank"><img src="{{image_url}}" width="130"/></a>');
// querySelector is returning an empty Array of Nodes
editor.on('load', () => {
const body = editor.Canvas.getBody().ownerDocument;
body.querySelectorAll('a').forEach(function(el) {
let link = el;
link.setAttribute('src', "http://via.placeholder.com/350x150");
});
editor.store();
});
How can this be achieved with Grapes ?
Answers (3)
Hi @mathiasbc by using the latest https://github.com/artf/grapesjs/releases/tag/v0.14.43 It should work with something like this
editor.on('component:mount', model => {
if (model.is('image')) {
if (model.get('src') == '{{image_url}}') {
model.getEl().src = "http://via.placeholder.com/350x150";
}
}
});
Hi Mathias, currently the only possibility I see now is to extend the image component and its render() method. Probably I'll add some new component event to make such a case easier to manage
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #418
Can't replace image if it's inside a link
Hi, If import this code inside the editor: And double click the image to change it, when I choose a new image from the modal, nothing happe...
Issue #1812
[Bug]: 404 when trying to add background image (reproducible in demo)
Visit https://grapesjs.com/demo.html in Chrome IncognitoClick the "Header" elementClick Decorations > BackgroundDelete Layer 2Click Layer 1...
Issue #1816
Not allowing to upload same image on same the position after delete that image
Ajax call which is uploading image file in assests folder It's working fine to add an image from the local. But in another scenario, It's n...
Issue #1615
[Feature] support edit credentials request attribute on Remote Storage
The credentials options its forced to be 'include' on Remote Storage. https://github.com/artf/grapesjs/blob/master/src/storage_manager/mode...
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.