import { Editor } from "@tiptap/react"
import { useCallback } from "react"

export const useTextmenuCommands = (editor: Editor) => {
	const onBold = useCallback(() => editor.chain().focus().toggleBold().run(), [editor])
	const onItalic = useCallback(() => editor.chain().focus().toggleItalic().run(), [editor])
	const onStrike = useCallback(() => editor.chain().focus().toggleStrike().run(), [editor])
	const onCode = useCallback(() => editor.chain().focus().toggleCode().run(), [editor])
	const onCodeBlock = useCallback(() => editor.chain().focus().toggleCodeBlock().run(), [editor])
	const onQuote = useCallback(() => editor.chain().focus().toggleBlockquote().run(), [editor])
	const onLink = useCallback(
		(url: string, inNewTab?: boolean) =>
			editor
				.chain()
				.focus()
				.setLink({ href: url, target: inNewTab ? "_blank" : "" })
				.run(),
		[editor]
	)

	return {
		onBold,
		onItalic,
		onStrike,
		onCode,
		onCodeBlock,
		onQuote,
		onLink
	}
}
