From 6ec259a0e71174651bae95d4628138bf6fd68742 Mon Sep 17 00:00:00 2001 From: kj_sh604 Date: Sun, 15 Mar 2026 16:19:35 -0400 Subject: refactor: packages/ --- .../excalidraw/components/PasteChartDialog.tsx | 136 +++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 packages/excalidraw/components/PasteChartDialog.tsx (limited to 'packages/excalidraw/components/PasteChartDialog.tsx') diff --git a/packages/excalidraw/components/PasteChartDialog.tsx b/packages/excalidraw/components/PasteChartDialog.tsx new file mode 100644 index 0000000..08a5634 --- /dev/null +++ b/packages/excalidraw/components/PasteChartDialog.tsx @@ -0,0 +1,136 @@ +import oc from "open-color"; +import React, { useLayoutEffect, useRef, useState } from "react"; +import { trackEvent } from "../analytics"; +import type { ChartElements, Spreadsheet } from "../charts"; +import { renderSpreadsheet } from "../charts"; +import type { ChartType } from "../element/types"; +import { t } from "../i18n"; +import { exportToSvg } from "../scene/export"; +import type { UIAppState } from "../types"; +import { useApp } from "./App"; +import { Dialog } from "./Dialog"; + +import "./PasteChartDialog.scss"; + +type OnInsertChart = (chartType: ChartType, elements: ChartElements) => void; + +const ChartPreviewBtn = (props: { + spreadsheet: Spreadsheet | null; + chartType: ChartType; + selected: boolean; + onClick: OnInsertChart; +}) => { + const previewRef = useRef(null); + const [chartElements, setChartElements] = useState( + null, + ); + + useLayoutEffect(() => { + if (!props.spreadsheet) { + return; + } + + const elements = renderSpreadsheet( + props.chartType, + props.spreadsheet, + 0, + 0, + ); + setChartElements(elements); + let svg: SVGSVGElement; + const previewNode = previewRef.current!; + + (async () => { + svg = await exportToSvg( + elements, + { + exportBackground: false, + viewBackgroundColor: oc.white, + }, + null, // files + { + skipInliningFonts: true, + }, + ); + svg.querySelector(".style-fonts")?.remove(); + previewNode.replaceChildren(); + previewNode.appendChild(svg); + + if (props.selected) { + (previewNode.parentNode as HTMLDivElement).focus(); + } + })(); + + return () => { + previewNode.replaceChildren(); + }; + }, [props.spreadsheet, props.chartType, props.selected]); + + return ( + + ); +}; + +export const PasteChartDialog = ({ + setAppState, + appState, + onClose, +}: { + appState: UIAppState; + onClose: () => void; + setAppState: React.Component["setState"]; +}) => { + const { onInsertElements } = useApp(); + const handleClose = React.useCallback(() => { + if (onClose) { + onClose(); + } + }, [onClose]); + + const handleChartClick = (chartType: ChartType, elements: ChartElements) => { + onInsertElements(elements); + trackEvent("paste", "chart", chartType); + setAppState({ + currentChartType: chartType, + pasteDialog: { + shown: false, + data: null, + }, + }); + }; + + return ( + +
+ + +
+
+ ); +}; -- cgit v1.2.3