diff --git a/src/MarkdownEditor/editor/elements/chart.tsx b/src/MarkdownEditor/editor/elements/chart.tsx index 44e515d..ccc56d3 100644 --- a/src/MarkdownEditor/editor/elements/chart.tsx +++ b/src/MarkdownEditor/editor/elements/chart.tsx @@ -452,176 +452,214 @@ export const Chart: React.FC = (props) => { const columns = (node as TableNode).otherProps?.columns || []; + const [columnLength, setColumnLength] = React.useState(2); + const config = [node.otherProps?.config].flat(1); const htmlRef = React.useRef(null); /** * 图表配置 */ - const getChartPopover = (index: number) => [ - { - return { - key, - label: ChartMap[key as 'pie'].title, - onClick: () => { - const path = EditorUtils.findPath(editor, node); - const config = [ - JSON.parse(JSON.stringify(node.otherProps?.config || [])), - ].flat(1); - config[index] = { - ...config?.at(index), - chartType: key, - }; + const getChartPopover = (index: number, isChartList: boolean) => + [ + { + return { + key, + label: ChartMap[key as 'pie'].title, + onClick: () => { + const path = EditorUtils.findPath(editor, node); + const config = [ + JSON.parse(JSON.stringify(node.otherProps?.config || [])), + ].flat(1); + config[index] = { + ...config?.at(index), + chartType: key, + }; - Transforms.setNodes( - editor, - { - otherProps: { - ...node.otherProps, - config: config, + Transforms.setNodes( + editor, + { + otherProps: { + ...node.otherProps, + config: config, + }, }, - }, - { - at: path, - }, - ); - }, - }; - }, - ) || [], - }} - > - - {ChartMap[(config.at(index)?.chartType as 'bar') || 'bar']?.title} - - - , - - + {ChartMap[(config.at(index)?.chartType as 'bar') || 'bar']?.title} + + + , + isChartList ? ( + { + return { + key: i + 1, + label: i + 1, + onClick: () => { + setColumnLength(i + 1); + }, + }; + }), + }} + > + + {columnLength} 列 + + + + ) : null, + + { - const path = EditorUtils.findPath(editor, node); - const config = JSON.parse( - JSON.stringify(node.otherProps?.config || []), - ); + onFinish={(values) => { + const path = EditorUtils.findPath(editor, node); + const config = JSON.parse( + JSON.stringify(node.otherProps?.config || []), + ); - config[index] = { - ...config.at(index), - ...values, - }; + config[index] = { + ...config.at(index), + ...values, + }; - Transforms.setNodes( - editor, - { - otherProps: { - ...node.otherProps, - config: config, + Transforms.setNodes( + editor, + { + otherProps: { + ...node.otherProps, + config: config, + }, }, - }, - { - at: path, - }, - ); - }} - > -
- { - e.stopPropagation(); - }, - }} - options={columns - ?.filter((item) => item.title) - ?.map((item) => { - return { - label: item.title, - value: item.dataIndex, - }; - })} - /> - { - e.stopPropagation(); - }, +
item.title) - ?.map((item) => { - return { - label: item.title, - value: item.dataIndex, - }; - })} - /> + > + { + e.stopPropagation(); + }, + }} + options={columns + ?.filter((item) => item.title) + ?.map((item) => { + return { + label: item.title, + value: item.dataIndex, + }; + })} + /> + { + e.stopPropagation(); + }, + }} + options={columns + ?.filter((item) => item.title) + ?.map((item) => { + return { + label: item.title, + value: item.dataIndex, + }; + })} + /> +
-
-
- - } - > - + + } > - - -
, - ]; + + + + , + ].filter((item) => !!item) as JSX.Element[]; return useMemo( () => ( @@ -780,7 +818,7 @@ export const Chart: React.FC = (props) => { ); }) .map((itemList, index) => { - const toolBar = getChartPopover(index); + const toolBar = getChartPopover(index, config.length > 1); if (Array.isArray(itemList)) { return itemList?.map((item, subIndex) => { if (!item) return null; @@ -788,15 +826,10 @@ export const Chart: React.FC = (props) => {
= (props) => { contentEditable={false} style={{ userSelect: 'none', - border: - // 只有一个图表时不显示边框,用消息框自己的 - config.length < 2 && - store?.editor?.children?.length < 2 - ? 'none' - : '1px solid #eee', + border: '1px solid #eee', borderRadius: 18, margin: 'auto', - minWidth: 300, + minWidth: `calc(${100 / columnLength}% - 16px)`, flex: 1, }} onClick={(e) => { @@ -889,6 +917,7 @@ export const Chart: React.FC = (props) => { attributes, JSON.stringify((node as TableNode).otherProps), editor, + columnLength, readonly, ], );