Grapesjs: Find images links in the editor

Created on 4 Jan 2019  Â·  4Comments  Â·  Source: artf/grapesjs

I am trying to integrate GrapesJs in reactJs.

Can you please help me out with the below question w.r.t GrapesJs-React,

• Is there a way to find the images link used inside the editor ??

example: when I drag and drop and image inside grapesjs editor it will create one image tag with image src path. So I want all all the images path used inside the editor which and all I drag and dropped inside editor.

outdated

Most helpful comment

Hi @hemanthmc
I can suggest subscribing to sorter:drag:end event and pass a React method as a callback.
In the method check if image is dropped and store its src into React state or do whatever needed.

A more detailed example:
this.grapesjsInstance.on('sorter:drag:end', this.onDragEnd);

onDragEnd(DataTransfer, modelToDrop) { if (modelToDrop && modelToDrop.attributes && modelToDrop.attributes.type === 'image') { this.setState({ /* data to save */ }); } }

All 4 comments

Hi @hemanthmc
I can suggest subscribing to sorter:drag:end event and pass a React method as a callback.
In the method check if image is dropped and store its src into React state or do whatever needed.

A more detailed example:
this.grapesjsInstance.on('sorter:drag:end', this.onDragEnd);

onDragEnd(DataTransfer, modelToDrop) { if (modelToDrop && modelToDrop.attributes && modelToDrop.attributes.type === 'image') { this.setState({ /* data to save */ }); } }

Thank you for your suggestion @maryia-kabash

editor.getWrapper() // Get the wrapper Component 
    .find('img') // Find all inner components by a query string
    .map(comp => comp.getAttributes()['src']) // map found components

Component API: https://grapesjs.com/docs/api/component.html

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tribulant picture tribulant  Â·  3Comments

Snarkly picture Snarkly  Â·  3Comments

kosirm picture kosirm  Â·  3Comments

alibouaziz picture alibouaziz  Â·  3Comments

nojacko picture nojacko  Â·  3Comments