Closed throne1986 closed 3 years ago
I solved the issue for future reference
first I removed this function :
editor.on("component:add", (model, argument) => {
if (model.getName() === "Image") {
modal.setContent(ModalImages);
}
});
I replaced with this function
editor.Commands.add('open-assets', {
run(editor, sender, opts = {}) {
dispatch(setIsOpen(true)); // open my custom asset manager
opts.target.set('src', customImgUrl) // set image url
}
})
This replaced the default asset manager with my custom react modal asset manager
@artf I have one more problem , if I do the following its works
editor.Commands.add("open-assets", {
run(editor, sender, opts = {}) {
dispatch(setIsOpen(true));
console.log("imageUrl", imageUrl);
opts.target.set('src', "https://via.placeholder.com/150"); // passing URL manually
},
});
But if I pass the URL dynamically it's not working, here is how I am passing URL dynamically
editor.Commands.add("open-assets", {
run(editor, sender, opts = {}) {
dispatch(setIsOpen(true));
console.log("imageUrl", imageUrl);
opts.target.set('src', imageUrl);
},
});
Check the image below what I see on the console, and tell me what is wrong here?
when you run editor.command.run('open-assets')
you have to pass options, eg.
editor.command.run('open-assets', {
target: editor.getSelected(),
})
when you run
editor.command.run('open-assets')
you have to pass options, eg.editor.command.run('open-assets', { target: editor.getSelected(), })
Thanks that works as expected:
But now when I click once my button for setting image URL its not setting the URL , it setts the URL only if I double click the button for setting the URL here is my solution
import React, { useState, useEffect } from "react";
import grapesjs from "grapesjs";
import { setIsOpen, setImageUrl } from "../reducers/modalSlice";
import ModalImages from "../components/ModalImages";
const Editor = () => {
const [editor, setEditor] = useState(null);
const imageUrl = useSelector((state) => state.modal.imageUrl)
useEffect(() => {
const editor = grapesjs.init({
container: "#editor",
storageManager: false,
blockManager: true,
plugins: [block, imageBlock],
});
setEditor(editor);
}, []);
if (editor) {
editor.Commands.add("open-assets", {
run(editor, sender, opts = {}) {
dispatch(setIsOpen(true));
opts.target.set("src", imageUrl);
},
});
}
const handleUseImage = (image) => {
dispatch(setImageUrl(image.attributes.path_thumb_url));
editor.Commands.run("open-assets", {
target: editor.getSelected(),
});
};
return (
<>
<ModalImages handleUseImage={handleUseImage} />
<div id="editor"></div>
</>
);
};
export default Editor;
Why do I need to double-click my button for setting the targeted image URL ? what is wrong here? thanks for the help though
Here is minal example demo : https://codesandbox.io/s/react-redux-toolkit-application-forked-l5rhp?file=/src/pages/Editor.js
@artf I solved the issue it was about closure problem , the solution just passing the URL as options as follows
editor.Commands.run("open-assets", {
target: editor.getSelected()
url: imageUrl
});
editor.Commands.add("open-assets", {
run(editor, sender, opts = {}) {
dispatch(setIsOpen(true));
console.log("imageUrl", opts.url);
opts.target.set("src", opts.url);
}
});
U can close the issue , again thank you so much for this library god bless ya
GrapesJS version
What browser are you using?
chrome new version
Reproducible demo link
https://codesandbox.io/s/grapejs-reactjs-forked-zqnqo?file=/src/Editor.js
Describe the bug
How to reproduce the bug?
Live demo : https://codesandbox.io/s/grapejs-reactjs-forked-zqnqo?file=/src/Editor.js
What is the expected behavior?
When the user drops custom image block, default image block, or double-clicking any image on canvas instead of opening the default modal image uploader I want to open my custom react-modal
What is the current behavior?
Now when the user drops the image on canvas or doubles click the image for replacing the image it opens up the default image uploader,
If is necessary to execute some code in order to reproduce the bug, paste it here below:
Code of Conduct