(function () {
"use strict";
const ASSET_VERSION = document.currentScript
? new URL(document.currentScript.src, location.href).search
: "";
const TOOLBAR = [
{ cmd: "toggleBold", icon: "format_bold", title: "Bold", active: "bold" },
{ cmd: "toggleItalic", icon: "format_italic", title: "Italic", active: "italic" },
{ cmd: "toggleStrike", icon: "strikethrough_s", title: "Strikethrough", active: "strike" },
{ cmd: "toggleCode", icon: "code", title: "Inline code", active: "code" },
{ sep: true },
{ cmd: "toggleHeading", arg: { level: 2 }, icon: "title", title: "Heading 2", active: "heading", activeArg: { level: 2 } },
{ cmd: "toggleHeading", arg: { level: 3 }, label: "H3", title: "Heading 3", active: "heading", activeArg: { level: 3 } },
{ cmd: "toggleHeading", arg: { level: 4 }, label: "H4", title: "Heading 4", active: "heading", activeArg: { level: 4 } },
{ sep: true },
{ cmd: "toggleBulletList", icon: "format_list_bulleted", title: "Bullet list", active: "bulletList" },
{ cmd: "toggleOrderedList", icon: "format_list_numbered", title: "Numbered list", active: "orderedList" },
{ cmd: "toggleBlockquote", icon: "format_quote", title: "Quote", active: "blockquote" },
{ cmd: "toggleCodeBlock", icon: "code_blocks", title: "Code block", active: "codeBlock" },
{ cmd: "setHorizontalRule", icon: "horizontal_rule", title: "Divider" },
{ sep: true },
{ fn: "setLink", icon: "link", title: "Insert / edit external link" },
{ fn: "linkInternal", kind: "page", linktype: "page", icon: "article", title: "Link to a page" },
{ fn: "linkInternal", kind: "document", linktype: "media", icon: "attachment", title: "Link to a document" },
{ fn: "linkEmail", icon: "mail", title: "Insert email link (mailto:)" },
{ fn: "linkAnchor", icon: "tag", title: "Link to an on-page anchor (#id)" },
{ fn: "unsetLink", icon: "link_off", title: "Remove link" },
{ sep: true },
{ cmd: "insertTable", arg: { rows: 3, cols: 3, withHeaderRow: true }, icon: "grid_on", title: "Insert table" },
{ cmd: "addRowAfter", icon: "add_row_below", title: "Add row" },
{ cmd: "addColumnAfter", icon: "add_column_right", title: "Add column" },
{ cmd: "deleteRow", label: "−R", title: "Delete row" },
{ cmd: "deleteColumn", label: "−C", title: "Delete column" },
{ cmd: "deleteTable", icon: "grid_off", title: "Delete table" },
{ sep: true },
{ cmd: "undo", icon: "undo", title: "Undo" },
{ cmd: "redo", icon: "redo", title: "Redo" },
];
function makeButton(spec, editor) {
const b = document.createElement("button");
b.type = "button";
b.className = "rcms-richtext-tool";
b.title = spec.title;
b.setAttribute("aria-label", spec.title);
if (spec.icon) {
const i = document.createElement("span");
i.className = "material-symbols-rounded";
i.textContent = spec.icon;
b.appendChild(i);
} else {
b.textContent = spec.label;
}
b.addEventListener("click", function (e) {
e.preventDefault();
const chain = editor.chain().focus();
if (spec.fn === "setLink") {
const prev = editor.getAttributes("link").href || "";
const url = window.prompt("Link URL", prev);
if (url === null) return;
if (url === "") { chain.unsetLink().run(); return; }
chain.extendMarkRange("link").setLink({ href: url }).run();
return;
}
if (spec.fn === "linkInternal") {
if (typeof window.rcmsOpenChooser !== "function") {
window.alert("The chooser isn't available on this page.");
return;
}
window.rcmsOpenChooser(spec.kind).then(function (item) {
if (!item || !item.id) return;
editor
.chain()
.focus()
.extendMarkRange("link")
.setLink({ href: null, linktype: spec.linktype, id: String(item.id) })
.run();
});
return;
}
if (spec.fn === "linkEmail") {
const prev = (editor.getAttributes("link").href || "").replace(/^mailto:/, "");
const email = window.prompt("Email address", prev);
if (email === null) return;
const trimmed = email.trim();
if (trimmed === "") { chain.unsetLink().run(); return; }
chain.extendMarkRange("link").setLink({ href: "mailto:" + trimmed }).run();
return;
}
if (spec.fn === "linkAnchor") {
const prev = (editor.getAttributes("link").href || "").replace(/^#/, "");
const name = window.prompt("Anchor name (target element id, without #)", prev);
if (name === null) return;
const trimmed = name.trim().replace(/^#/, "");
if (trimmed === "") { chain.unsetLink().run(); return; }
chain.extendMarkRange("link").setLink({ href: "#" + trimmed }).run();
return;
}
if (spec.fn === "unsetLink") { chain.unsetLink().run(); return; }
if (spec.arg) chain[spec.cmd](spec.arg).run();
else chain[spec.cmd]().run();
});
return b;
}
function syncActive(editor, buttons) {
buttons.forEach(function (entry) {
if (!entry.spec.active) return;
const on = entry.spec.activeArg
? editor.isActive(entry.spec.active, entry.spec.activeArg)
: editor.isActive(entry.spec.active);
entry.el.classList.toggle("is-active", on);
entry.el.setAttribute("aria-pressed", String(on));
});
}
function enhance(textarea) {
if (textarea.dataset.rcmsTiptap === "1") return;
if (!window.RcmsRichtext || typeof window.RcmsRichtext.create !== "function") return;
textarea.dataset.rcmsTiptap = "1";
const wrap = document.createElement("div");
wrap.className = "rcms-richtext";
const toolbar = document.createElement("div");
toolbar.className = "rcms-richtext-toolbar";
toolbar.setAttribute("role", "toolbar");
toolbar.setAttribute("aria-label", "Rich text formatting");
const surface = document.createElement("div");
surface.className = "rcms-richtext-surface";
wrap.appendChild(toolbar);
wrap.appendChild(surface);
textarea.hidden = true;
textarea.parentNode.insertBefore(wrap, textarea.nextSibling);
const editor = window.RcmsRichtext.create({
element: surface,
content: textarea.value || "",
onUpdate: function (html) {
textarea.value = html === "<p></p>" ? "" : html;
textarea.dispatchEvent(new Event("input", { bubbles: true }));
},
});
textarea.__rcmsEditor = editor;
wrap.dataset.rcmsRichtextWrap = "1";
const buttons = [];
TOOLBAR.forEach(function (spec) {
if (spec.sep) {
const s = document.createElement("span");
s.className = "rcms-richtext-sep";
s.setAttribute("aria-hidden", "true");
toolbar.appendChild(s);
return;
}
const el = makeButton(spec, editor);
toolbar.appendChild(el);
buttons.push({ spec: spec, el: el });
});
editor.on("selectionUpdate", function () { syncActive(editor, buttons); });
editor.on("transaction", function () { syncActive(editor, buttons); });
syncActive(editor, buttons);
}
var bundlePromise = null;
function ensureBundle() {
if (window.RcmsRichtext && typeof window.RcmsRichtext.create === "function") {
return Promise.resolve();
}
if (bundlePromise) return bundlePromise;
bundlePromise = new Promise(function (resolve, reject) {
var s = document.createElement("script");
s.src = "/cms-admin/static/vendor/tiptap.bundle.js" + ASSET_VERSION;
s.onload = resolve;
s.onerror = reject;
document.head.appendChild(s);
});
return bundlePromise;
}
function enhanceAll(root) {
var pending = (root || document).querySelectorAll(
'textarea[data-widget-mode="richtext"]:not([data-rcms-tiptap])'
);
if (!pending.length) return;
ensureBundle()
.then(function () { pending.forEach(enhance); })
.catch(function () {
});
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", function () { enhanceAll(document); });
} else {
enhanceAll(document);
}
window.rcmsEnhanceRichtext = enhanceAll;
window.rcmsDestroyRichtext = function (root) {
(root || document)
.querySelectorAll('textarea[data-rcms-tiptap="1"]')
.forEach(function (ta) {
if (ta.__rcmsEditor) {
try { ta.__rcmsEditor.destroy(); } catch (e) { }
ta.__rcmsEditor = null;
}
});
};
window.rcmsReenhanceRichtext = function (root) {
(root || document)
.querySelectorAll('[data-rcms-richtext-wrap="1"]')
.forEach(function (wrap) {
var ta = wrap.previousElementSibling;
if (ta && ta.matches && ta.matches('textarea[data-widget-mode="richtext"]')) {
var ce = wrap.querySelector("[contenteditable]");
if (ce) {
var html = ce.innerHTML;
ta.value = html === "<p></p>" || html === "<p><br></p>" ? "" : html;
}
ta.hidden = false;
delete ta.dataset.rcmsTiptap;
ta.__rcmsEditor = null;
}
wrap.remove();
});
enhanceAll(root);
};
})();