/* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ import * as Popover from '@radix-ui/react-popover' import * as Tooltip from '@radix-ui/react-tooltip' import React from 'react' import { activeEditor$, editorRootElementRef$, iconComponentFor$, cancelLinkEdit$, linkDialogState$, onWindowChange$, removeLink$, switchFromPreviewToLinkEdit$, updateLink$, ClickLinkCallback } from '@mdxeditor/editor' import { useForm } from 'react-hook-form' import { Cell, useCellValues, usePublisher } from '@mdxeditor/gurx' import styles from './Dialog.module.scss' interface LinkEditFormProps { url: string title: string onSubmit: (link: { url: string; title: string }) => void onCancel: () => void } interface LinkFormFields { url: string title: string } export function LinkEditForm({ url, title, onSubmit, onCancel }: LinkEditFormProps) { const { register, handleSubmit, setValue } = useForm({ values: { url, title } }) return (
{ void handleSubmit(onSubmit)(e) e.stopPropagation() e.preventDefault() }} onReset={(e) => { e.stopPropagation() onCancel() }} >
setValue('url', e.currentTarget.value)} placeholder={'Paste an URL'} />
) } export const onClickLinkCallback$ = Cell(null) /** @internal */ export const LinkDialog = () => { const [ editorRootElementRef, activeEditor, iconComponentFor, linkDialogState, onClickLinkCallback ] = useCellValues( editorRootElementRef$, activeEditor$, iconComponentFor$, linkDialogState$, onClickLinkCallback$ ) const publishWindowChange = usePublisher(onWindowChange$) const updateLink = usePublisher(updateLink$) const cancelLinkEdit = usePublisher(cancelLinkEdit$) const switchFromPreviewToLinkEdit = usePublisher(switchFromPreviewToLinkEdit$) const removeLink = usePublisher(removeLink$) React.useEffect(() => { const update = () => { activeEditor?.getEditorState().read(() => { publishWindowChange(true) }) } window.addEventListener('resize', update) window.addEventListener('scroll', update) return () => { window.removeEventListener('resize', update) window.removeEventListener('scroll', update) } }, [activeEditor, publishWindowChange]) const [copyUrlTooltipOpen, setCopyUrlTooltipOpen] = React.useState(false) const theRect = linkDialogState.rectangle const urlIsExternal = linkDialogState.type === 'preview' && linkDialogState.url.startsWith('http') return ( { e.preventDefault() }} key={linkDialogState.linkNodeKey} className={[ 'popUpMainCard', ...(linkDialogState.type === 'edit' ? [styles.wrapper] : []) ].join(' ')} > {linkDialogState.type === 'edit' && ( )} {linkDialogState.type === 'preview' && ( <>
{ switchFromPreviewToLinkEdit() }} >
{ void window.navigator.clipboard .writeText(linkDialogState.url) .then(() => { setCopyUrlTooltipOpen(true) setTimeout(() => { setCopyUrlTooltipOpen(false) }, 1000) }) }} >
{'Copied!'}
{ removeLink() }} >
)}
) }