mention then insert HTML to editor

i am using ckeditor, how can i mention a product and insert HTML information related to that product (including photo and name) instead of just @productname, i mean insert a snippet of html code that i can customize, or any editor which can perform the same operation

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function MentionCustomization(editor) {
editor.conversion.for("upcast").elementToAttribute({
view: {
name: "a",
key: "data-mention",
classes: "mention",
attributes: {
href: true,
"data-user-id": true,
},
},
model: {
key: "mention",
value: (viewItem) => {
const mentionAttribute = editor.plugins
.get("Mention")
.toMentionAttribute(viewItem, {
link: viewItem.getAttribute("href"),
userId: viewItem.getAttribute("data-user-id"),
});
return mentionAttribute;
},
},
converterPriority: "high",
});
editor.conversion.for("downcast").attributeToElement({
model: "mention",
view: (modelAttributeValue, { writer }) => {
if (!modelAttributeValue) {
return;
}
return writer.createAttributeElement(
"a",
{
class: "mention",
"data-mention": modelAttributeValue.id,
"data-user-id": modelAttributeValue.userId,
href: modelAttributeValue.link,
target: "_blank",
},
{
priority: 20,
id: modelAttributeValue.uid,
}
);
},
converterPriority: "high",
});
}
</code>
<code>function MentionCustomization(editor) { editor.conversion.for("upcast").elementToAttribute({ view: { name: "a", key: "data-mention", classes: "mention", attributes: { href: true, "data-user-id": true, }, }, model: { key: "mention", value: (viewItem) => { const mentionAttribute = editor.plugins .get("Mention") .toMentionAttribute(viewItem, { link: viewItem.getAttribute("href"), userId: viewItem.getAttribute("data-user-id"), }); return mentionAttribute; }, }, converterPriority: "high", }); editor.conversion.for("downcast").attributeToElement({ model: "mention", view: (modelAttributeValue, { writer }) => { if (!modelAttributeValue) { return; } return writer.createAttributeElement( "a", { class: "mention", "data-mention": modelAttributeValue.id, "data-user-id": modelAttributeValue.userId, href: modelAttributeValue.link, target: "_blank", }, { priority: 20, id: modelAttributeValue.uid, } ); }, converterPriority: "high", }); } </code>
function MentionCustomization(editor) {
  editor.conversion.for("upcast").elementToAttribute({
    view: {
      name: "a",
      key: "data-mention",
      classes: "mention",
      attributes: {
        href: true,
        "data-user-id": true,
      },
    },
    model: {
      key: "mention",
      value: (viewItem) => {
        const mentionAttribute = editor.plugins
          .get("Mention")
          .toMentionAttribute(viewItem, {
           
            link: viewItem.getAttribute("href"),
            userId: viewItem.getAttribute("data-user-id"),
          });

        return mentionAttribute;
      },
    },
    converterPriority: "high",
  });

  editor.conversion.for("downcast").attributeToElement({
    model: "mention",
    view: (modelAttributeValue, { writer }) => {
      if (!modelAttributeValue) {
        return;
      }
      return writer.createAttributeElement(
        "a",
        {
          class: "mention",
          "data-mention": modelAttributeValue.id,
          "data-user-id": modelAttributeValue.userId,
          href: modelAttributeValue.link,
          target: "_blank",
        },
        {
          priority: 20,
          id: modelAttributeValue.uid,
        }
      );
    },
    converterPriority: "high",
  });
}

i am adding this MentionCustomization to config plugins, in downcast i can only convert to “a” tag, every attempt when I create a container as a div tag to wrap the img and span doesn’t work.
Thanks for help !!

1

CKEditor’s Mention feature is primarily designed to insert inline content (e.g., hyperlinks or text spans) rather than complex HTML structures like div tags with nested elements.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function MentionCustomization(editor) {
editor.conversion.for("upcast").elementToElement({
view: {
name: "div",
classes: "product-mention",
},
model: (viewElement, { writer }) => {
const productName = viewElement.getChild(1).data;
const productId = viewElement.getAttribute("data-product-id");
const productImage = viewElement.getAttribute("data-product-image");
return writer.createElement("productMention", {
name: productName,
id: productId,
image: productImage,
});
},
converterPriority: "high",
});
editor.conversion.for("downcast").elementToElement({
model: "productMention",
view: (modelItem, { writer }) => {
const productName = modelItem.getAttribute("name");
const productId = modelItem.getAttribute("id");
const productImage = modelItem.getAttribute("image");
// Create the wrapper div
const container = writer.createContainerElement("div", {
class: "product-mention",
"data-product-id": productId,
"data-product-image": productImage,
});
// Add the image element
const imageElement = writer.createEmptyElement("img", {
src: productImage,
alt: productName,
class: "product-image",
});
writer.insert(writer.createPositionAt(container, 0), imageElement);
// Add the span element for the name
const nameElement = writer.createText(productName);
writer.insert(writer.createPositionAt(container, "end"), nameElement);
return container;
},
converterPriority: "high",
});
// Add the schema for the custom mention type
editor.model.schema.register("productMention", {
allowWhere: "$block",
allowAttributes: ["name", "id", "image"],
isObject: true,
});
</code>
<code>function MentionCustomization(editor) { editor.conversion.for("upcast").elementToElement({ view: { name: "div", classes: "product-mention", }, model: (viewElement, { writer }) => { const productName = viewElement.getChild(1).data; const productId = viewElement.getAttribute("data-product-id"); const productImage = viewElement.getAttribute("data-product-image"); return writer.createElement("productMention", { name: productName, id: productId, image: productImage, }); }, converterPriority: "high", }); editor.conversion.for("downcast").elementToElement({ model: "productMention", view: (modelItem, { writer }) => { const productName = modelItem.getAttribute("name"); const productId = modelItem.getAttribute("id"); const productImage = modelItem.getAttribute("image"); // Create the wrapper div const container = writer.createContainerElement("div", { class: "product-mention", "data-product-id": productId, "data-product-image": productImage, }); // Add the image element const imageElement = writer.createEmptyElement("img", { src: productImage, alt: productName, class: "product-image", }); writer.insert(writer.createPositionAt(container, 0), imageElement); // Add the span element for the name const nameElement = writer.createText(productName); writer.insert(writer.createPositionAt(container, "end"), nameElement); return container; }, converterPriority: "high", }); // Add the schema for the custom mention type editor.model.schema.register("productMention", { allowWhere: "$block", allowAttributes: ["name", "id", "image"], isObject: true, }); </code>
function MentionCustomization(editor) {
editor.conversion.for("upcast").elementToElement({
view: {
  name: "div",
  classes: "product-mention",
},
model: (viewElement, { writer }) => {
  const productName = viewElement.getChild(1).data;
  const productId = viewElement.getAttribute("data-product-id");
  const productImage = viewElement.getAttribute("data-product-image");

    return writer.createElement("productMention", {
      name: productName,
      id: productId,
    image: productImage,
    });
    },
   converterPriority: "high",
 });
 editor.conversion.for("downcast").elementToElement({
model: "productMention",
view: (modelItem, { writer }) => {
  const productName = modelItem.getAttribute("name");
  const productId = modelItem.getAttribute("id");
  const productImage = modelItem.getAttribute("image");

  // Create the wrapper div
  const container = writer.createContainerElement("div", {
    class: "product-mention",
    "data-product-id": productId,
    "data-product-image": productImage,
  });

  // Add the image element
  const imageElement = writer.createEmptyElement("img", {
    src: productImage,
    alt: productName,
    class: "product-image",
  });
  writer.insert(writer.createPositionAt(container, 0), imageElement);

  // Add the span element for the name
  const nameElement = writer.createText(productName);
  writer.insert(writer.createPositionAt(container, "end"), nameElement);

    return container;
  },
  converterPriority: "high",
});

// Add the schema for the custom mention type
 editor.model.schema.register("productMention", {
   allowWhere: "$block",
  allowAttributes: ["name", "id", "image"],
  isObject: true,
 });

}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật