1- import { Toolbar , ToolbarGroup , ToolbarSeparator } from '@/components/tiptap-ui-primitive/toolbar'
1+ import {
2+ Toolbar ,
3+ ToolbarGroup ,
4+ ToolbarSeparator ,
5+ } from '@/components/tiptap-ui-primitive/toolbar'
26import { Button } from '@/components/tiptap-ui-primitive/button'
3- import { BoldIcon , ItalicIcon , Code , CodeIcon } from 'lucide-react'
7+
8+ import {
9+ BoldIcon ,
10+ ItalicIcon ,
11+ UnderlineIcon ,
12+ StrikethroughIcon ,
13+ CodeIcon ,
14+ HighlighterIcon ,
15+ Heading1 ,
16+ Heading2 ,
17+ Heading3 ,
18+ ListIcon ,
19+ ListOrderedIcon ,
20+ CheckSquareIcon ,
21+ QuoteIcon ,
22+ CodeSquareIcon ,
23+ MinusIcon ,
24+ CornerDownLeftIcon ,
25+ LinkIcon ,
26+ UnlinkIcon ,
27+ ImageIcon ,
28+ TableIcon ,
29+ AlignLeftIcon ,
30+ AlignCenterIcon ,
31+ AlignRightIcon ,
32+ AlignJustifyIcon ,
33+ UndoIcon ,
34+ RedoIcon ,
35+ EraserIcon ,
36+ TrashIcon ,
37+ } from 'lucide-react'
38+
439import type { Editor } from '@tiptap/react'
540import { useEditorState } from '@tiptap/react'
641
7- // import { BoldIcon } from '@/components/icons/bold-icon'
8- // import { ItalicIcon } from '@/components/icons/italic-icon'
9- // import { Spacer } from '@/components/tiptap-ui-primitive/spacer'
1042interface Props {
11- editor : Editor
43+ editor : Editor
44+ }
45+
46+ export default function TiptapToolbar ( { editor } : Props ) {
47+ const state = useEditorState ( {
48+ editor,
49+ selector : ( { editor } ) => ( {
50+ bold : editor . isActive ( 'bold' ) ,
51+ italic : editor . isActive ( 'italic' ) ,
52+ underline : editor . isActive ( 'underline' ) ,
53+ strike : editor . isActive ( 'strike' ) ,
54+ code : editor . isActive ( 'code' ) ,
55+ highlight : editor . isActive ( 'highlight' ) ,
56+ bulletList : editor . isActive ( 'bulletList' ) ,
57+ orderedList : editor . isActive ( 'orderedList' ) ,
58+ taskList : editor . isActive ( 'taskList' ) ,
59+ blockquote : editor . isActive ( 'blockquote' ) ,
60+ codeBlock : editor . isActive ( 'codeBlock' ) ,
61+ h1 : editor . isActive ( 'heading' , { level : 1 } ) ,
62+ h2 : editor . isActive ( 'heading' , { level : 2 } ) ,
63+ h3 : editor . isActive ( 'heading' , { level : 3 } ) ,
64+ alignLeft : editor . isActive ( { textAlign : 'left' } ) ,
65+ alignCenter : editor . isActive ( { textAlign : 'center' } ) ,
66+ alignRight : editor . isActive ( { textAlign : 'right' } ) ,
67+ alignJustify : editor . isActive ( { textAlign : 'justify' } ) ,
68+ link : editor . isActive ( 'link' ) ,
69+ canUndo : editor . can ( ) . undo ( ) ,
70+ canRedo : editor . can ( ) . redo ( ) ,
71+ } ) ,
72+ } )
73+
74+ return (
75+ < Toolbar variant = "floating" className = "flex-wrap gap-1 border rounded" >
76+ { /* TEXT MARKS */ }
77+ < ToolbarGroup >
78+ < Button className = " "
79+ data-active-state = { state . bold ? 'on' : 'off' }
80+ onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleBold ( ) . run ( ) } >
81+ < BoldIcon />
82+ </ Button >
83+ < Button data-active-state = { state . italic ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleItalic ( ) . run ( ) } > < ItalicIcon /> </ Button >
84+ < Button data-active-state = { state . underline ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleUnderline ( ) . run ( ) } > < UnderlineIcon /> </ Button >
85+ < Button data-active-state = { state . strike ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleStrike ( ) . run ( ) } > < StrikethroughIcon /> </ Button >
86+ < Button data-active-state = { state . code ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleCode ( ) . run ( ) } > < CodeIcon /> </ Button >
87+ < Button data-active-state = { state . highlight ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleHighlight ( ) . run ( ) } > < HighlighterIcon /> </ Button >
88+ { /*
89+ </ToolbarGroup>
90+ */ }
91+ < ToolbarSeparator />
92+
93+ { /* HEADINGS */ }
94+ { /*
95+ <ToolbarGroup>
96+ */ }
97+ < Button data-active-state = { state . h1 ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleHeading ( { level : 1 } ) . run ( ) } > < Heading1 /> </ Button >
98+ < Button data-active-state = { state . h2 ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleHeading ( { level : 2 } ) . run ( ) } > < Heading2 /> </ Button >
99+ < Button data-active-state = { state . h3 ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleHeading ( { level : 3 } ) . run ( ) } > < Heading3 /> </ Button >
100+ < Button onClick = { ( ) => editor . chain ( ) . focus ( ) . setParagraph ( ) . run ( ) } > P</ Button >
101+ { /*
102+ */ }
103+ </ ToolbarGroup >
104+ < ToolbarSeparator />
105+
106+ { /* LISTS */ }
107+ { /*
108+ */ }
109+ < ToolbarGroup >
110+ < Button data-active-state = { state . bulletList ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleBulletList ( ) . run ( ) } > < ListIcon /> </ Button >
111+ < Button data-active-state = { state . orderedList ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleOrderedList ( ) . run ( ) } > < ListOrderedIcon /> </ Button >
112+ < Button data-active-state = { state . taskList ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleTaskList ( ) . run ( ) } > < CheckSquareIcon /> </ Button >
113+ < Button onClick = { ( ) => editor . chain ( ) . focus ( ) . sinkListItem ( 'listItem' ) . run ( ) } > →</ Button >
114+ < Button onClick = { ( ) => editor . chain ( ) . focus ( ) . liftListItem ( 'listItem' ) . run ( ) } > ←</ Button >
115+ { /*
116+ </ToolbarGroup>
117+ */ }
118+
119+ < ToolbarSeparator />
120+
121+ { /* BLOCKS */ }
122+ { /*
123+ <ToolbarGroup>
124+ */ }
125+ < Button data-active-state = { state . blockquote ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleBlockquote ( ) . run ( ) } > < QuoteIcon /> </ Button >
126+ < Button data-active-state = { state . codeBlock ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleCodeBlock ( ) . run ( ) } > < CodeSquareIcon /> </ Button >
127+ < Button onClick = { ( ) => editor . chain ( ) . focus ( ) . setHorizontalRule ( ) . run ( ) } > < MinusIcon /> </ Button >
128+ < Button onClick = { ( ) => editor . chain ( ) . focus ( ) . setHardBreak ( ) . run ( ) } > < CornerDownLeftIcon /> </ Button >
129+ { /*
130+ */ }
131+ </ ToolbarGroup >
132+
133+ { /*
134+ <ToolbarSeparator />
135+ */ }
136+
137+ { /* ALIGNMENT */ }
138+ { /*
139+ */ }
140+ < ToolbarGroup >
141+ < Button data-active-state = { state . alignLeft ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . setTextAlign ( 'left' ) . run ( ) } > < AlignLeftIcon /> </ Button >
142+ < Button data-active-state = { state . alignCenter ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . setTextAlign ( 'center' ) . run ( ) } > < AlignCenterIcon /> </ Button >
143+ < Button data-active-state = { state . alignRight ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . setTextAlign ( 'right' ) . run ( ) } > < AlignRightIcon /> </ Button >
144+ < Button data-active-state = { state . alignJustify ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . setTextAlign ( 'justify' ) . run ( ) } > < AlignJustifyIcon /> </ Button >
145+ </ ToolbarGroup >
146+
147+ < ToolbarSeparator />
148+
149+ { /* LINK / MEDIA */ }
150+ < ToolbarGroup >
151+ < Button data-active-state = { state . link ? 'on' : 'off' } onClick = { ( ) => editor . chain ( ) . focus ( ) . setLink ( { href : prompt ( 'URL' ) || '' } ) . run ( ) } > < LinkIcon /> </ Button >
152+ < Button onClick = { ( ) => editor . chain ( ) . focus ( ) . unsetLink ( ) . run ( ) } > < UnlinkIcon /> </ Button >
153+ < Button onClick = { ( ) => editor . chain ( ) . focus ( ) . setImage ( { src : prompt ( 'Image URL' ) || '' } ) . run ( ) } > < ImageIcon /> </ Button >
154+ < Button onClick = { ( ) => editor . commands . insertTable ( { rows : 3 , cols : 3 , withHeaderRow : true } ) } > < TableIcon /> </ Button >
155+ </ ToolbarGroup >
156+
157+ < ToolbarSeparator />
158+
159+ { /* HISTORY / CLEANUP */ }
160+ < ToolbarGroup >
161+ < Button disabled = { ! state . canUndo } onClick = { ( ) => editor . chain ( ) . focus ( ) . undo ( ) . run ( ) } > < UndoIcon /> </ Button >
162+ < Button disabled = { ! state . canRedo } onClick = { ( ) => editor . chain ( ) . focus ( ) . redo ( ) . run ( ) } > < RedoIcon /> </ Button >
163+ < Button onClick = { ( ) => editor . chain ( ) . focus ( ) . unsetAllMarks ( ) . clearNodes ( ) . run ( ) } > < EraserIcon /> </ Button >
164+ < Button onClick = { ( ) => editor . chain ( ) . focus ( ) . clearContent ( ) . run ( ) } > < TrashIcon /> </ Button >
165+ </ ToolbarGroup >
166+
167+ </ Toolbar >
168+ )
12169}
13- export default function TiptapToolbar ( props : Props ) {
14- let { editor } = props
15- const editorState = useEditorState ( {
16- editor,
17- selector : ( { editor } ) => ( {
18- isBold : editor . isActive ( 'bold' ) ,
19- isItalic : editor . isActive ( 'italic' ) ,
20- canBold : editor . can ( ) . chain ( ) . focus ( ) . toggleBold ( ) . run ( ) ,
21- canItalic : editor . can ( ) . chain ( ) . focus ( ) . toggleItalic ( ) . run ( ) ,
22- canCode : editor . can ( ) . chain ( ) . focus ( ) . toggleCode ( ) . run ( ) , //
23- } ) ,
24- } )
25-
26- return (
27- < Toolbar variant = 'floating' className = 'my-2 rounded; border border-primary/20' >
28- < ToolbarGroup >
29-
30- < Button
31- data-style = "ghost"
32- data-active-state = { editorState . isBold ? 'on' : 'off' }
33- disabled = { ! editorState . canBold }
34- onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleBold ( ) . run ( ) }
35- >
36- < BoldIcon className = "tiptap-button-icon" />
37- </ Button >
38-
39- < Button
40- data-style = "ghost"
41- data-active-state = { editorState . isItalic ? 'on' : 'off' }
42- disabled = { ! editorState . canItalic }
43- onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleItalic ( ) . run ( ) }
44- >
45- < ItalicIcon className = "tiptap-button-icon" />
46- </ Button >
47-
48- < Button
49- data-style = "ghost"
50- data-active-state = { editorState . canCode ? 'on' : 'off' }
51- disabled = { ! editorState . canCode }
52- onClick = { ( ) => editor . chain ( ) . focus ( ) . toggleCode ( ) . run ( ) }
53- >
54- < CodeIcon className = "tiptap-button-icon" />
55- </ Button >
56-
57- </ ToolbarGroup >
58-
59- < ToolbarSeparator />
60- </ Toolbar >
61- )
62- }
0 commit comments