Files
contact-front/src/components/kri-editor/toolbar/toolbarConfig.ts

201 lines
5.4 KiB
TypeScript

import type { Editor } from "@tiptap/react";
import type { LucideIcon } from "lucide-react";
import {
Bold,
Italic,
Underline,
Strikethrough,
AlignLeft,
AlignCenter,
AlignRight,
AlignJustify,
List,
ListOrdered,
Link2,
Image as ImageIcon,
Table2,
Rows3,
Columns3,
Trash2,
} from "lucide-react";
export type ToolbarItem = {
key: string;
icon?: LucideIcon;
label?: string; // text buttons (H1/H2/H3)
title: string;
isActive?: (editor: Editor) => boolean;
isDisabled?: (editor: Editor) => boolean;
onClick: (editor: Editor) => void;
};
export const headingItems: ToolbarItem[] = [
{
key: "h1",
label: "H1",
title: "Heading 1",
isActive: (e) => e.isActive("heading", { level: 1 }),
onClick: (e) => e.chain().focus().toggleHeading({ level: 1 }).run(),
},
{
key: "h2",
label: "H2",
title: "Heading 2",
isActive: (e) => e.isActive("heading", { level: 2 }),
onClick: (e) => e.chain().focus().toggleHeading({ level: 2 }).run(),
},
{
key: "h3",
label: "H3",
title: "Heading 3",
isActive: (e) => e.isActive("heading", { level: 3 }),
onClick: (e) => e.chain().focus().toggleHeading({ level: 3 }).run(),
},
];
export const markItems: ToolbarItem[] = [
{
key: "bold",
icon: Bold,
title: "Bold",
isActive: (e) => e.isActive("bold"),
isDisabled: (e) => !e.can().chain().focus().toggleBold().run(),
onClick: (e) => e.chain().focus().toggleBold().run(),
},
{
key: "italic",
icon: Italic,
title: "Italic",
isActive: (e) => e.isActive("italic"),
isDisabled: (e) => !e.can().chain().focus().toggleItalic().run(),
onClick: (e) => e.chain().focus().toggleItalic().run(),
},
{
key: "underline",
icon: Underline,
title: "Underline",
isActive: (e) => e.isActive("underline"),
onClick: (e) => e.chain().focus().toggleUnderline().run(),
},
{
key: "strike",
icon: Strikethrough,
title: "Strikethrough",
isActive: (e) => e.isActive("strike"),
onClick: (e) => e.chain().focus().toggleStrike().run(),
},
];
export const alignItems: ToolbarItem[] = [
{
key: "alignLeft",
icon: AlignLeft,
title: "Align Left",
isActive: (e) => e.isActive({ textAlign: "left" }),
onClick: (e) => e.chain().focus().setTextAlign("left").run(),
},
{
key: "alignCenter",
icon: AlignCenter,
title: "Align Center",
isActive: (e) => e.isActive({ textAlign: "center" }),
onClick: (e) => e.chain().focus().setTextAlign("center").run(),
},
{
key: "alignRight",
icon: AlignRight,
title: "Align Right",
isActive: (e) => e.isActive({ textAlign: "right" }),
onClick: (e) => e.chain().focus().setTextAlign("right").run(),
},
{
key: "alignJustify",
icon: AlignJustify,
title: "Justify",
isActive: (e) => e.isActive({ textAlign: "justify" }),
onClick: (e) => e.chain().focus().setTextAlign("justify").run(),
},
];
export const listItems: ToolbarItem[] = [
{
key: "bulletList",
icon: List,
title: "Bullet List",
isActive: (e) => e.isActive("bulletList"),
onClick: (e) => e.chain().focus().toggleBulletList().run(),
},
{
key: "orderedList",
icon: ListOrdered,
title: "Ordered List",
isActive: (e) => e.isActive("orderedList"),
onClick: (e) => e.chain().focus().toggleOrderedList().run(),
},
];
export const linkItem = (opts: { promptForUrl: (prev?: string) => string | null }): ToolbarItem => ({
key: "link",
icon: Link2,
title: "Link / Unlink",
isActive: (e) => e.isActive("link"),
onClick: (e) => {
const isLinked = e.isActive("link");
if (isLinked) {
e.chain().focus().extendMarkRange("link").unsetLink().run();
return;
}
const prev = e.getAttributes("link")?.href as string | undefined;
const url = opts.promptForUrl(prev);
if (url == null) return;
const trimmed = url.trim();
if (!trimmed) return;
e.chain().focus().extendMarkRange("link").setLink({ href: trimmed }).run();
},
});
export const imageItem = (opts: { onPick: () => void; disabled: boolean }): ToolbarItem => ({
key: "image",
icon: ImageIcon,
title: opts.disabled ? "onImageUpload 미설정으로 비활성" : "Image Upload",
isDisabled: () => opts.disabled,
onClick: () => opts.onPick(),
});
export const tableMenuItems = {
icon: Table2,
title: "Table",
actions: [
{
key: "insertTable",
icon: Table2,
title: "Insert Table",
isDisabled: (e: Editor) =>
!e.can().chain().focus().insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run(),
onClick: (e: Editor) =>
e.chain().focus().insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run(),
},
{
key: "addRowAfter",
icon: Rows3,
title: "Add Row",
isDisabled: (e: Editor) => !e.can().chain().focus().addRowAfter().run(),
onClick: (e: Editor) => e.chain().focus().addRowAfter().run(),
},
{
key: "addColumnAfter",
icon: Columns3,
title: "Add Column",
isDisabled: (e: Editor) => !e.can().chain().focus().addColumnAfter().run(),
onClick: (e: Editor) => e.chain().focus().addColumnAfter().run(),
},
{
key: "deleteTable",
icon: Trash2,
title: "Delete Table",
isDisabled: (e: Editor) => !e.can().chain().focus().deleteTable().run(),
onClick: (e: Editor) => e.chain().focus().deleteTable().run(),
},
] as ToolbarItem[],
};