import { Editor } from "@tiptap/core";
import StarterKit from "@tiptap/starter-kit";
import Link from "@tiptap/extension-link";
import Image from "@tiptap/extension-image";
import Table from "@tiptap/extension-table";
import TableRow from "@tiptap/extension-table-row";
import TableHeader from "@tiptap/extension-table-header";
import TableCell from "@tiptap/extension-table-cell";
const RcmsLink = Link.extend({
addAttributes() {
return {
...this.parent?.(),
linktype: {
default: null,
parseHTML: (el) => el.getAttribute("linktype"),
renderHTML: (attrs) => (attrs.linktype ? { linktype: attrs.linktype } : {}),
},
id: {
default: null,
parseHTML: (el) => el.getAttribute("id"),
renderHTML: (attrs) =>
attrs.id !== null && attrs.id !== undefined && attrs.id !== ""
? { id: String(attrs.id) }
: {},
},
};
},
parseHTML() {
return [
{ tag: 'a[href]:not([href *= "javascript:" i])' },
{ tag: "a[linktype]" },
];
},
});
window.RcmsRichtext = {
create({ element, content, onUpdate }) {
return new Editor({
element,
content: content || "",
extensions: [
StarterKit.configure({ heading: { levels: [2, 3, 4] } }),
RcmsLink.configure({
openOnClick: false,
HTMLAttributes: { rel: "noopener", target: null },
}),
Image,
Table,
TableRow,
TableHeader,
TableCell,
],
onUpdate: ({ editor }) => {
if (onUpdate) onUpdate(editor.getHTML());
},
});
},
};