import React, { Fragment } from 'react'; import { type ICommand, defaultCommands } from '../../commands/index'; import { IMarkdownEditor, ToolBarProps } from '../..'; import './index.less'; export type Commands = keyof typeof defaultCommands | ICommand; export interface IToolBarProps extends ToolBarProps { className?: string; editorProps: IMarkdownEditor; mode?: boolean; prefixCls?: string; toolbars?: T[]; onClick?: (type: string) => void; } export default function ToolBar(props: IToolBarProps) { const { prefixCls = 'md-editor', className, onClick, toolbars = [], editor, mode, preview, container, containerEditor, editorProps = {}, ...htmlProps } = props; if (!toolbars || toolbars.length === 0) return null; function handleClick(execute: ICommand['execute']) { if (execute && editor && editor) { execute(editor.current!); } } editorProps.prefixCls = prefixCls; return (
{[...toolbars].map((command, key) => { let buttonProps: React.ButtonHTMLAttributes = { type: 'button', }; const obj = typeof command === 'string' ? defaultCommands[command] : command; if (!obj) return null; buttonProps.children = obj.icon; buttonProps.onClick = () => handleClick(obj.execute); if (obj.button && typeof obj.button === 'object') { const btn: React.ButtonHTMLAttributes = obj.button; (Object.keys(btn) as (keyof React.ButtonHTMLAttributes)[]).forEach((key) => { buttonProps[key] = btn[key]; }); } else if (typeof obj.button === 'function') { const CustomButton = obj.button(obj, editorProps, { preview, container, containerEditor, editor, editorProps, }); return {CustomButton}; } return
); }