|
| 1 | +import { useEditorState } from '@tiptap/react'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { CheckIcon, UnlinkIcon } from '../icons'; |
| 4 | +import { useBubbleMenuContext } from './context'; |
| 5 | +import { focusEditor, getUrlFromString } from './utils'; |
| 6 | + |
| 7 | +export interface BubbleMenuImageFormProps { |
| 8 | + className?: string; |
| 9 | + validateUrl?: (value: string) => string | null; |
| 10 | + onLinkApply?: (href: string) => void; |
| 11 | + onLinkRemove?: () => void; |
| 12 | +} |
| 13 | + |
| 14 | +export function BubbleMenuImageForm({ |
| 15 | + className, |
| 16 | + validateUrl, |
| 17 | + onLinkApply, |
| 18 | + onLinkRemove, |
| 19 | +}: BubbleMenuImageFormProps) { |
| 20 | + const { editor, isEditing, setIsEditing } = useBubbleMenuContext(); |
| 21 | + const inputRef = React.useRef<HTMLInputElement>(null); |
| 22 | + const formRef = React.useRef<HTMLFormElement>(null); |
| 23 | + |
| 24 | + const imageHref = useEditorState({ |
| 25 | + editor, |
| 26 | + selector: ({ editor: e }) => |
| 27 | + (e?.getAttributes('image').href as string | null) ?? '', |
| 28 | + }); |
| 29 | + |
| 30 | + const [inputValue, setInputValue] = React.useState(imageHref ?? ''); |
| 31 | + |
| 32 | + React.useEffect(() => { |
| 33 | + if (!isEditing) { |
| 34 | + return; |
| 35 | + } |
| 36 | + setInputValue(imageHref ?? ''); |
| 37 | + const timeoutId = setTimeout(() => { |
| 38 | + inputRef.current?.focus(); |
| 39 | + }, 0); |
| 40 | + return () => clearTimeout(timeoutId); |
| 41 | + }, [isEditing, imageHref]); |
| 42 | + |
| 43 | + React.useEffect(() => { |
| 44 | + if (!isEditing) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const handleKeyDown = (event: KeyboardEvent) => { |
| 49 | + if (event.key === 'Escape') { |
| 50 | + setIsEditing(false); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + const handleClickOutside = (event: MouseEvent) => { |
| 55 | + if (formRef.current && !formRef.current.contains(event.target as Node)) { |
| 56 | + const form = formRef.current; |
| 57 | + const submitEvent = new Event('submit', { |
| 58 | + bubbles: true, |
| 59 | + cancelable: true, |
| 60 | + }); |
| 61 | + form.dispatchEvent(submitEvent); |
| 62 | + setIsEditing(false); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + document.addEventListener('mousedown', handleClickOutside); |
| 67 | + window.addEventListener('keydown', handleKeyDown); |
| 68 | + |
| 69 | + return () => { |
| 70 | + window.removeEventListener('keydown', handleKeyDown); |
| 71 | + document.removeEventListener('mousedown', handleClickOutside); |
| 72 | + }; |
| 73 | + }, [isEditing, setIsEditing]); |
| 74 | + |
| 75 | + if (!isEditing) { |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + function handleSubmit(e: React.FormEvent<HTMLFormElement>) { |
| 80 | + e.preventDefault(); |
| 81 | + |
| 82 | + const value = inputValue.trim(); |
| 83 | + |
| 84 | + if (value === '') { |
| 85 | + editor.chain().focus().updateAttributes('image', { href: null }).run(); |
| 86 | + setIsEditing(false); |
| 87 | + focusEditor(editor); |
| 88 | + onLinkRemove?.(); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const validate = validateUrl ?? getUrlFromString; |
| 93 | + const finalValue = validate(value); |
| 94 | + |
| 95 | + if (!finalValue) { |
| 96 | + editor.chain().focus().updateAttributes('image', { href: null }).run(); |
| 97 | + setIsEditing(false); |
| 98 | + focusEditor(editor); |
| 99 | + onLinkRemove?.(); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + editor |
| 104 | + .chain() |
| 105 | + .focus() |
| 106 | + .updateAttributes('image', { href: finalValue }) |
| 107 | + .run(); |
| 108 | + setIsEditing(false); |
| 109 | + focusEditor(editor); |
| 110 | + onLinkApply?.(finalValue); |
| 111 | + } |
| 112 | + |
| 113 | + function handleUnlink(e: React.MouseEvent) { |
| 114 | + e.stopPropagation(); |
| 115 | + editor.chain().focus().updateAttributes('image', { href: null }).run(); |
| 116 | + setIsEditing(false); |
| 117 | + focusEditor(editor); |
| 118 | + onLinkRemove?.(); |
| 119 | + } |
| 120 | + |
| 121 | + const hasLink = (imageHref ?? '') !== ''; |
| 122 | + |
| 123 | + return ( |
| 124 | + <form |
| 125 | + ref={formRef} |
| 126 | + data-re-img-bm-form="" |
| 127 | + className={className} |
| 128 | + onMouseDown={(e) => e.stopPropagation()} |
| 129 | + onClick={(e) => e.stopPropagation()} |
| 130 | + onKeyDown={(e) => e.stopPropagation()} |
| 131 | + onSubmit={handleSubmit} |
| 132 | + > |
| 133 | + <input |
| 134 | + ref={inputRef} |
| 135 | + data-re-img-bm-input="" |
| 136 | + value={inputValue} |
| 137 | + onFocus={(e) => e.stopPropagation()} |
| 138 | + onChange={(e) => setInputValue(e.target.value)} |
| 139 | + placeholder="Paste a link" |
| 140 | + type="text" |
| 141 | + /> |
| 142 | + |
| 143 | + {hasLink ? ( |
| 144 | + <button |
| 145 | + type="button" |
| 146 | + aria-label="Remove link" |
| 147 | + data-re-img-bm-unlink="" |
| 148 | + onClick={handleUnlink} |
| 149 | + > |
| 150 | + <UnlinkIcon /> |
| 151 | + </button> |
| 152 | + ) : ( |
| 153 | + <button |
| 154 | + type="submit" |
| 155 | + aria-label="Apply link" |
| 156 | + data-re-img-bm-apply="" |
| 157 | + onMouseDown={(e) => e.stopPropagation()} |
| 158 | + > |
| 159 | + <CheckIcon /> |
| 160 | + </button> |
| 161 | + )} |
| 162 | + </form> |
| 163 | + ); |
| 164 | +} |
0 commit comments