mirror of
https://github.com/linsa-io/linsa.git
synced 2026-03-23 01:29:23 +01:00
* wip * wip page * chore: style * wip pages * wip pages * chore: toggle * chore: link * feat: topic search * chore: page section * refactor: apply tailwind class ordering * fix: handle loggedIn user for guest route * feat: folder & image schema * chore: move utils to shared * refactor: tailwind class ordering * feat: img ext for editor * refactor: remove qa * fix: tanstack start * fix: wrong import * chore: use toast * chore: schema
62 lines
1.6 KiB
TypeScript
62 lines
1.6 KiB
TypeScript
import * as React from "react"
|
|
import type { Editor } from "@tiptap/core"
|
|
import type { Node } from "@tiptap/pm/model"
|
|
import { isUrl } from "@shared/editor/lib/utils"
|
|
|
|
interface UseImageActionsProps {
|
|
editor: Editor
|
|
node: Node
|
|
src: string
|
|
onViewClick: (value: boolean) => void
|
|
}
|
|
|
|
export type ImageActionHandlers = {
|
|
onView?: () => void
|
|
onDownload?: () => void
|
|
onCopy?: () => void
|
|
onCopyLink?: () => void
|
|
onRemoveImg?: () => void
|
|
}
|
|
|
|
export const useImageActions = ({
|
|
editor,
|
|
node,
|
|
src,
|
|
onViewClick,
|
|
}: UseImageActionsProps) => {
|
|
const isLink = React.useMemo(() => isUrl(src), [src])
|
|
|
|
const onView = React.useCallback(() => {
|
|
onViewClick(true)
|
|
}, [onViewClick])
|
|
|
|
const onDownload = React.useCallback(() => {
|
|
editor.commands.downloadImage({ src: node.attrs.src, alt: node.attrs.alt })
|
|
}, [editor.commands, node.attrs.alt, node.attrs.src])
|
|
|
|
const onCopy = React.useCallback(() => {
|
|
editor.commands.copyImage({ src: node.attrs.src })
|
|
}, [editor.commands, node.attrs.src])
|
|
|
|
const onCopyLink = React.useCallback(() => {
|
|
editor.commands.copyLink({ src: node.attrs.src })
|
|
}, [editor.commands, node.attrs.src])
|
|
|
|
const onRemoveImg = React.useCallback(() => {
|
|
editor.commands.command(({ tr, dispatch }) => {
|
|
const { selection } = tr
|
|
const nodeAtSelection = tr.doc.nodeAt(selection.from)
|
|
|
|
if (nodeAtSelection && nodeAtSelection.type.name === "image") {
|
|
if (dispatch) {
|
|
tr.deleteSelection()
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
})
|
|
}, [editor.commands])
|
|
|
|
return { isLink, onView, onDownload, onCopy, onCopyLink, onRemoveImg }
|
|
}
|