GrapesJS设置链接“ src”占位符onLoad

时间:2018-11-14 01:49:23

标签: django jinja2 grapesjs

我实现了一个小GrapesJS编辑器,该编辑器允许编辑Django / Jinja2模板。默认情况下,GrapesJS试图获取我元素的图像src

<a href="{{product_url}}" target="_blank"><img src="{{image_url}}" width="130"/></a>

每次我将HTML模板加载到GrapesJS编辑器中时,都会发生请求: http://localhost:8000/%7B%7Bimage_url%7D%7D 404 (Not Found)

即使此404调用,GrapesJS编辑器也可以正常工作。但是我想处理对图像的请求。因此,我不想渲染GrapesJS的http://localhost:8000/%7B%7Bimage_url%7D%7D默认调用,而是渲染一个占位符图像:https://placekitten.com/200/300

我目前拥有的东西:

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();
});

0 个答案:

没有答案