1- /* eslint-disable react/no-duplicate-key */
21import React , { useMemo } from 'react'
32import type { Editor } from '@tiptap/core'
3+ import type { ToolbarItemProps , ToolbarProps } from '@/types'
44
55import { Separator } from '@/components'
66import { useLocale } from '@/locales'
77import { isFunction } from '@/utils/utils'
88
9- export interface ToolbarProps {
9+ export interface ToolbarComponentProps {
1010 editor : Editor
1111 disabled ?: boolean
12+ toolbar ?: ToolbarProps
1213}
1314
14- interface ToolbarItemProps {
15- button : {
16- component : React . ComponentType < any >
17- componentProps : Record < string , any >
18- }
19- divider : boolean
20- spacer : boolean
21- }
22-
23- function Toolbar ( { editor, disabled } : ToolbarProps ) {
15+ function Toolbar ( { editor, disabled, toolbar } : ToolbarComponentProps ) {
2416 const { t, lang } = useLocale ( )
2517
2618 const items = useMemo ( ( ) => {
@@ -34,7 +26,12 @@ function Toolbar({ editor, disabled }: ToolbarProps) {
3426 let menus : ToolbarItemProps [ ] = [ ]
3527
3628 for ( const extension of sortExtensions ) {
37- const { button, divider = false , spacer = false , toolbar = true } = extension . options as any
29+ const {
30+ button,
31+ divider = false ,
32+ spacer = false ,
33+ toolbar = true ,
34+ } = extension . options
3835 if ( ! button || ! isFunction ( button ) || ! toolbar ) {
3936 continue
4037 }
@@ -50,44 +47,62 @@ function Toolbar({ editor, disabled }: ToolbarProps) {
5047 button : k ,
5148 divider : i === _button . length - 1 ? divider : false ,
5249 spacer : i === 0 ? spacer : false ,
50+ type : extension . type ,
51+ name : extension . name ,
5352 } ) )
5453 menus = [ ...menus , ...menu ]
5554 continue
5655 }
5756
58- menus . push ( { button : _button , divider, spacer } )
57+ menus . push ( {
58+ button : _button ,
59+ divider,
60+ spacer,
61+ type : extension . type ,
62+ name : extension . name ,
63+ } )
5964 }
6065 return menus
6166 } , [ editor , t , lang ] )
6267
63- return (
64- < div
65- className = "richtext-px-1 richtext-py-2 !richtext-border-b"
66- style = { {
67- pointerEvents : disabled ? 'none' : 'auto' ,
68- opacity : disabled ? 0.5 : 1 ,
69- } }
70- >
71- < div className = "richtext-relative richtext-flex richtext-flex-wrap richtext-h-auto richtext-gap-y-1 richtext-gap-x-1" >
72- { items . map ( ( item : ToolbarItemProps , key ) => {
73- const ButtonComponent = item . button . component
74-
75- return (
76- < div className = "richtext-flex richtext-items-center" key = { `toolbar-item-${ key } ` } >
77- { item ?. spacer && < Separator orientation = "vertical" className = "!richtext-h-[16px] !richtext-mx-[10px]" /> }
78-
79- < ButtonComponent
80- { ...item . button . componentProps }
81- disabled = { disabled || item ?. button ?. componentProps ?. disabled }
82- />
83-
84- { item ?. divider && < Separator orientation = "vertical" className = "!richtext-h-auto !richtext-mx-2" /> }
85- </ div >
86- )
87- } ) }
68+ const containerDom = ( innerContent : React . ReactNode ) => {
69+ return (
70+ < div
71+ className = "richtext-px-1 richtext-py-2 !richtext-border-b"
72+ style = { {
73+ pointerEvents : disabled ? 'none' : 'auto' ,
74+ opacity : disabled ? 0.5 : 1 ,
75+ } }
76+ >
77+ < div className = "richtext-relative richtext-flex richtext-flex-wrap richtext-h-auto richtext-gap-y-1 richtext-gap-x-1" >
78+ { innerContent }
79+ </ div >
8880 </ div >
89- </ div >
90- )
81+ )
82+ }
83+
84+ const domContent = items . map ( ( item : ToolbarItemProps , key ) => {
85+ const ButtonComponent = item . button . component
86+
87+ return (
88+ < div className = "richtext-flex richtext-items-center" key = { `toolbar-item-${ key } ` } >
89+ { item ?. spacer && < Separator orientation = "vertical" className = "!richtext-h-[16px] !richtext-mx-[10px]" /> }
90+
91+ < ButtonComponent
92+ { ...item . button . componentProps }
93+ disabled = { disabled || item ?. button ?. componentProps ?. disabled }
94+ />
95+
96+ { item ?. divider && < Separator orientation = "vertical" className = "!richtext-h-auto !richtext-mx-2" /> }
97+ </ div >
98+ )
99+ } )
100+
101+ if ( toolbar && toolbar ?. render ) {
102+ return toolbar . render ( { editor, disabled : disabled || false } , items , domContent , containerDom )
103+ }
104+
105+ return containerDom ( domContent )
91106}
92107
93108export { Toolbar }
0 commit comments