100 lines
2.7 KiB
TypeScript
100 lines
2.7 KiB
TypeScript
"use client";
|
|
|
|
import * as React from "react";
|
|
import type { Editor } from "@tiptap/react";
|
|
import { ToolbarButton } from "./ToolbarButton";
|
|
import { ToolbarPipe } from "./ToolbarPipe";
|
|
import { TableMenu } from "./TableMenu";
|
|
import { alignItems, listItems, markItems, headingItems, linkItem, imageItem } from "./toolbarConfig";
|
|
|
|
type Props = {
|
|
editor: Editor;
|
|
canImageUpload: boolean;
|
|
onPickImage: () => void;
|
|
};
|
|
|
|
function promptForUrl(prev?: string) {
|
|
return window.prompt("링크 URL을 입력하세요", prev ?? "");
|
|
}
|
|
|
|
export function Toolbar({ editor, canImageUpload, onPickImage }: Props) {
|
|
const link = React.useMemo(() => linkItem({ promptForUrl }), []);
|
|
const image = React.useMemo(() => imageItem({ onPick: onPickImage, disabled: !canImageUpload }), [
|
|
onPickImage,
|
|
canImageUpload,
|
|
]);
|
|
|
|
return (
|
|
<div className="flex flex-wrap items-center gap-1 text-foreground">
|
|
{/* Headings (H1/H2/H3) */}
|
|
{headingItems.map((item) => (
|
|
<ToolbarButton
|
|
key={item.key}
|
|
title={item.title}
|
|
active={item.isActive ? item.isActive(editor) : false}
|
|
onClick={() => item.onClick(editor)}
|
|
>
|
|
{item.label}
|
|
</ToolbarButton>
|
|
))}
|
|
|
|
<ToolbarPipe />
|
|
|
|
{/* Marks */}
|
|
{markItems.map((item) => (
|
|
<ToolbarButton
|
|
key={item.key}
|
|
icon={item.icon}
|
|
title={item.title}
|
|
active={item.isActive ? item.isActive(editor) : false}
|
|
disabled={item.isDisabled ? item.isDisabled(editor) : false}
|
|
onClick={() => item.onClick(editor)}
|
|
/>
|
|
))}
|
|
|
|
<ToolbarPipe />
|
|
|
|
{/* Align */}
|
|
{alignItems.map((item) => (
|
|
<ToolbarButton
|
|
key={item.key}
|
|
icon={item.icon}
|
|
title={item.title}
|
|
active={item.isActive ? item.isActive(editor) : false}
|
|
onClick={() => item.onClick(editor)}
|
|
/>
|
|
))}
|
|
|
|
<ToolbarPipe />
|
|
|
|
{/* Lists */}
|
|
{listItems.map((item) => (
|
|
<ToolbarButton
|
|
key={item.key}
|
|
icon={item.icon}
|
|
title={item.title}
|
|
active={item.isActive ? item.isActive(editor) : false}
|
|
onClick={() => item.onClick(editor)}
|
|
/>
|
|
))}
|
|
|
|
<ToolbarPipe />
|
|
|
|
{/* Media / Link / Table */}
|
|
<ToolbarButton
|
|
icon={image.icon}
|
|
title={image.title}
|
|
disabled={image.isDisabled ? image.isDisabled(editor) : false}
|
|
onClick={() => image.onClick(editor)}
|
|
/>
|
|
<ToolbarButton
|
|
icon={link.icon}
|
|
title={link.title}
|
|
active={link.isActive ? link.isActive(editor) : false}
|
|
onClick={() => link.onClick(editor)}
|
|
/>
|
|
<TableMenu editor={editor} />
|
|
</div>
|
|
);
|
|
}
|