|
| 1 | +import { useCallback, useEffect, useState } from 'react'; |
| 2 | + |
| 3 | +import { BubbleMenu } from '@tiptap/react/menus'; |
| 4 | + |
| 5 | +import { ActionButton } from '@/components/ActionButton'; |
| 6 | +import { Button, Input, Label } from '@/components/ui'; |
| 7 | +import { Dialog, DialogContent, DialogFooter, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; |
| 8 | +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; |
| 9 | +import { Callout } from '@/extensions/Callout'; |
| 10 | +import { useAttributes } from '@/hooks/useAttributes'; |
| 11 | +import { useLocale } from '@/locales'; |
| 12 | +import { useEditorInstance } from '@/store/editor'; |
| 13 | +import { useEditableEditor } from '@/store/store'; |
| 14 | +import { deleteNode } from '@/utils/delete-node'; |
| 15 | + |
| 16 | +const CALLOUT_TYPES = [ |
| 17 | + { value: 'note', label: 'Note', icon: 'Info' }, |
| 18 | + { value: 'tip', label: 'Tip', icon: 'Lightbulb' }, |
| 19 | + { value: 'important', label: 'Important', icon: 'AlertCircle' }, |
| 20 | + { value: 'warning', label: 'Warning', icon: 'TriangleAlert' }, |
| 21 | + { value: 'caution', label: 'Caution', icon: 'OctagonAlert' }, |
| 22 | +] as const; |
| 23 | + |
| 24 | +interface ICalloutAttrs { |
| 25 | + type?: string |
| 26 | + title?: string |
| 27 | + body?: string |
| 28 | +} |
| 29 | + |
| 30 | +export function RichTextBubbleCallout() { |
| 31 | + const editable = useEditableEditor(); |
| 32 | + const editor = useEditorInstance(); |
| 33 | + const { t } = useLocale(); |
| 34 | + |
| 35 | + const { type, title, body } = useAttributes<ICalloutAttrs>(editor, Callout.name, { |
| 36 | + type: 'note', |
| 37 | + title: '', |
| 38 | + body: '' |
| 39 | + }); |
| 40 | + |
| 41 | + const [visible, setVisible] = useState(false); |
| 42 | + const [calloutType, setCalloutType] = useState('note'); |
| 43 | + const [calloutTitle, setCalloutTitle] = useState(''); |
| 44 | + const [calloutBody, setCalloutBody] = useState(''); |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + if (visible) { |
| 48 | + setCalloutType(type || 'note'); |
| 49 | + setCalloutTitle(title || ''); |
| 50 | + setCalloutBody(body || ''); |
| 51 | + } |
| 52 | + }, [visible, type, title, body]); |
| 53 | + |
| 54 | + const handleUpdate = useCallback(() => { |
| 55 | + if (!editor) return; |
| 56 | + |
| 57 | + // Update attributes |
| 58 | + editor |
| 59 | + .chain() |
| 60 | + .updateAttributes(Callout.name, { |
| 61 | + type: calloutType, |
| 62 | + title: calloutTitle, |
| 63 | + body: calloutBody, |
| 64 | + }) |
| 65 | + .focus() |
| 66 | + .run(); |
| 67 | + |
| 68 | + setVisible(false); |
| 69 | + }, [editor, calloutType, calloutTitle, calloutBody]); |
| 70 | + |
| 71 | + const handleCancel = useCallback(() => { |
| 72 | + setVisible(false); |
| 73 | + }, []); |
| 74 | + |
| 75 | + const handleDelete = useCallback(() => { |
| 76 | + deleteNode(Callout.name, editor); |
| 77 | + }, [editor]); |
| 78 | + |
| 79 | + const shouldShow = useCallback(() => { |
| 80 | + return editor.isActive(Callout.name); |
| 81 | + }, [editor]); |
| 82 | + |
| 83 | + if (!editable) { |
| 84 | + return <></>; |
| 85 | + } |
| 86 | + |
| 87 | + return ( |
| 88 | + <BubbleMenu |
| 89 | + editor={editor} |
| 90 | + options={{ placement: 'top', offset: 8, flip: true }} |
| 91 | + pluginKey="RichTextBubbleCallout" |
| 92 | + shouldShow={shouldShow} |
| 93 | + > |
| 94 | + <div className="richtext-flex richtext-items-center richtext-gap-2 richtext-rounded-md !richtext-border !richtext-border-solid !richtext-border-border richtext-bg-popover richtext-p-1 richtext-text-popover-foreground richtext-shadow-md richtext-outline-none"> |
| 95 | + <Dialog onOpenChange={setVisible} |
| 96 | + open={visible} |
| 97 | + > |
| 98 | + <DialogTrigger asChild> |
| 99 | + <ActionButton icon="Pencil" |
| 100 | + tooltip={t('editor.callout.edit.title')} |
| 101 | + /> |
| 102 | + </DialogTrigger> |
| 103 | + |
| 104 | + <DialogContent> |
| 105 | + <DialogTitle> |
| 106 | + {t('editor.callout.edit.title')} |
| 107 | + </DialogTitle> |
| 108 | + |
| 109 | + <div className="richtext-space-y-4 richtext-py-4"> |
| 110 | + <div className="richtext-space-y-2"> |
| 111 | + <Label> |
| 112 | + {t('editor.callout.dialog.type')} |
| 113 | + </Label> |
| 114 | + |
| 115 | + <Select onValueChange={setCalloutType} |
| 116 | + value={calloutType} |
| 117 | + > |
| 118 | + <SelectTrigger> |
| 119 | + <SelectValue className='richtext-text-accent' |
| 120 | +placeholder={t('editor.callout.dialog.type.placeholder')} |
| 121 | + /> |
| 122 | + </SelectTrigger> |
| 123 | + |
| 124 | + <SelectContent> |
| 125 | + {CALLOUT_TYPES.map((type) => ( |
| 126 | + <SelectItem key={type.value} |
| 127 | + value={type.value} |
| 128 | + > |
| 129 | + {t(`editor.callout.type.${type.value}`)} |
| 130 | + </SelectItem> |
| 131 | + ))} |
| 132 | + </SelectContent> |
| 133 | + </Select> |
| 134 | + </div> |
| 135 | + |
| 136 | + <div className="richtext-space-y-2"> |
| 137 | + <Label> |
| 138 | + {t('editor.callout.dialog.title.label')} |
| 139 | + </Label> |
| 140 | + |
| 141 | + <Input |
| 142 | + onChange={(e) => setCalloutTitle(e.target.value)} |
| 143 | + placeholder={t('editor.callout.dialog.title.placeholder')} |
| 144 | + type="text" |
| 145 | + value={calloutTitle} |
| 146 | + /> |
| 147 | + </div> |
| 148 | + |
| 149 | + <div className="richtext-space-y-2"> |
| 150 | + <Label> |
| 151 | + {t('editor.callout.dialog.body.label')} |
| 152 | + </Label> |
| 153 | + |
| 154 | + <Input |
| 155 | + onChange={(e) => setCalloutBody(e.target.value)} |
| 156 | + placeholder={t('editor.callout.dialog.body.placeholder')} |
| 157 | + type="text" |
| 158 | + value={calloutBody} |
| 159 | + /> |
| 160 | + </div> |
| 161 | + </div> |
| 162 | + |
| 163 | + <DialogFooter> |
| 164 | + <Button onClick={handleCancel} |
| 165 | + variant="outline" |
| 166 | + > |
| 167 | + {t('editor.callout.dialog.button.cancel')} |
| 168 | + </Button> |
| 169 | + |
| 170 | + <Button onClick={handleUpdate}> |
| 171 | + {t('editor.callout.dialog.button.apply')} |
| 172 | + </Button> |
| 173 | + </DialogFooter> |
| 174 | + </DialogContent> |
| 175 | + </Dialog> |
| 176 | + |
| 177 | + <ActionButton action={handleDelete} |
| 178 | + icon="Trash2" |
| 179 | + tooltip={t('editor.delete')} |
| 180 | + /> |
| 181 | + </div> |
| 182 | + </BubbleMenu> |
| 183 | + ); |
| 184 | +} |
0 commit comments