Issue #4796Opened December 21, 2022by RakulAgn0 reactions

Custom Traits not Updating if using mjml

Question

setTimeout(() => {
    editor.DomComponents.addType("mj-image", {
      isComponent: (el: any) => el.tagName === "MJ-IMAGE",
      model: {
        defaults: {
          traits: [
            {
              type: "mjchange",
              label: " ",
              name: "mjchange",
            },
            {
              type: "href",
              label: "Link",
              name: "href",
            },
            {
              type: "alt",
              label: "Alt",
              name: "alt",
            },
          ],
        },
      },
    });

  editor.TraitManager.addType("mjchange", {
    noLabel: true,
    createInput({}) {
      let selectedSrc = editor.getSelected();

      let src = selectedSrc!.attributes.attributes!.src;

      const toggleModal = () => {
        editor.runCommand("open-assets", {
          target: editor.getSelected(),
        });
      };
      const el = document.createElement("div");
      el.setAttribute("class", "image-trait-preview");
      el.innerHTML = `<img src="${src}" style="width: 100%; height:auto;background:#f9f9f9;" id="gjs_img_preview_logo_rtl"/>
                <button type="submit" class="btn btn-primary btn-block btn-sm" id="chg-img-trait-btn">Change</button>`;

      const inputType = el.querySelector("#chg-img-trait-btn");
      const imgBox = el.querySelector("#gjs_img_preview_logo_rtl");

      imgBox!.addEventListener("click", toggleModal);
      inputType!.addEventListener("click", toggleModal);

      return el;
    },
  });

  editor.on("modal:open", (component) => {
    const $ = editor.$;
    editor.AssetManager.open({
      types: ["mj-image"],
      select(assets, complete) {
        const selected = editor.getSelected();    
        if (selected && selected.is("mj-image")) {
          selected.addAttributes({ src: assets.getSrc() });
          $("#gjs_img_preview_logo_rtl").attr("src", assets.getSrc());
          complete && editor.AssetManager.close();
        }
      },
    });

This Method Works Fine for Normal HTML. But When I Tried With MJML For MJ-IMAGE Tag The Src is Updated in TR Tag but not updated in img tag inside the TR tag The Below Image Screenshot from 2022-12-21 13-32-13(1)

Originally posted by @RakulAgn in https://github.com/artf/grapesjs/discussions/4795

Answers (1)

artfJanuary 2, 20230 reactions

@RakulAgn please avoid creating issues from discussions.

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.