"use client" import "./toolbar.css" import { useRef, type PointerEvent as ReactPointerEvent } from "react" import { cn } from "@/lib/utils" import type { EditorToolbarAction } from "./types" type EditorToolbarProps = { actions: ReadonlyArray } function isSeparatorAction( action: EditorToolbarAction ): action is Extract { return "type" in action && action.type === "separator" } function isCustomAction( action: EditorToolbarAction ): action is Extract { return "type" in action && action.type === "custom" } export function EditorToolbar({ actions }: EditorToolbarProps) { const containerRef = useRef(null) const dragStateRef = useRef<{ pointerId: number startX: number startScrollLeft: number moved: boolean } | null>(null) const handlePointerDown = (event: ReactPointerEvent) => { if (event.pointerType === "mouse" && event.button !== 0) { return } const container = containerRef.current if (!container || container.scrollWidth <= container.clientWidth) { return } dragStateRef.current = { pointerId: event.pointerId, startX: event.clientX, startScrollLeft: container.scrollLeft, moved: false, } container.setPointerCapture(event.pointerId) } const handlePointerMove = (event: ReactPointerEvent) => { const container = containerRef.current const dragState = dragStateRef.current if (!container || !dragState || dragState.pointerId !== event.pointerId) { return } const deltaX = event.clientX - dragState.startX if (Math.abs(deltaX) > 3) { dragState.moved = true } container.scrollLeft = dragState.startScrollLeft - deltaX } const handlePointerEnd = (event: ReactPointerEvent) => { const container = containerRef.current const dragState = dragStateRef.current if (!container || !dragState || dragState.pointerId !== event.pointerId) { return } if (container.hasPointerCapture(event.pointerId)) { container.releasePointerCapture(event.pointerId) } window.setTimeout(() => { dragStateRef.current = null }, 0) } return (
{actions.map((action) => { if (isSeparatorAction(action)) { return (
) }