grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

Changing asset via command

Open Amir2828 opened this issue 7 years ago • 5 comments

Hello All,

I would like to emulate the assets manager image replace behavior. for example I created a view on the side with all the images that are present on the canvas. I would like that changes on the view will be reflected on the canvas, just like the way the assets manager is replacing images on the spot.

Any help will be appreciated.

Amir2828 avatar Jun 25 '19 12:06 Amir2828

Hi @Amir2828, I would like to connect with you on your work on GrapeJs if you are interested in a side project. Let me know

hananint avatar Jun 25 '19 13:06 hananint

@Amir2828 on select you have to change the src property of the Image component editor.getSelected().set({ src: 'https://.../image3.jpg' })

artf avatar Jul 03 '19 21:07 artf

Hi @artf,

I use a custom file manager and use the command below to define the image:

editor.getSelected().set({src: 'https: //.../image3.jpg'})

However, in Style Manager, when selecting a background image through the manager, it does not work since it is not an image and does not have the src attribute.

How can I set the src and work in the background image?

hitaloramon avatar Nov 28 '20 01:11 hitaloramon

Hi @hitaloramon. I have the same issue, did you solve it?

mmotov avatar Apr 26 '21 15:04 mmotov

Hi @mmotov,

I'll show you how I did it, it's easier to understand.

When capturing the value of the image I define it in the same way, but below I do some checks to know if the element is a background or not.

editor.Commands.add("open-assets", { run(editor, sender, opts = {}) { var image = $("#img-file").val();

    opts.target.set("src", image);

    if (opts.target.attributes.type == "image") {
        if (image) {
            opts.target.set("src", image);
        }
    } else {
        if (opts.target.getStyle()["background-image"] == undefined) {
            var background = `url("${image}")`;
        } else {
            var background = opts.target.getStyle()["background-image"] + `url("${image}")`;
        }

        opts.target.addStyle({
            "background-image": background
        });

        editor.trigger("component:toggled");
    }

    editor.Commands.stop("open-assets");
}

});

hitaloramon avatar Apr 26 '21 17:04 hitaloramon