debug both in app

This commit is contained in:
Ryan Di 2024-12-06 19:00:58 +08:00
parent e49db1dd3c
commit 1c55160e3f

View File

@ -26,6 +26,7 @@ import {
StoreAction, StoreAction,
reconcileElements, reconcileElements,
exportToCanvas, exportToCanvas,
exportToSvg,
} from "../packages/excalidraw"; } from "../packages/excalidraw";
import { import {
exportToBlob, exportToBlob,
@ -79,10 +80,7 @@ import {
} from "./components/ExportToExcalidrawPlus"; } from "./components/ExportToExcalidrawPlus";
import { updateStaleImageStatuses } from "./data/FileManager"; import { updateStaleImageStatuses } from "./data/FileManager";
import { newElementWith } from "../packages/excalidraw/element/mutateElement"; import { newElementWith } from "../packages/excalidraw/element/mutateElement";
import { import { isInitializedImageElement } from "../packages/excalidraw/element/typeChecks";
isInitializedImageElement,
isRectangularElement,
} from "../packages/excalidraw/element/typeChecks";
import { loadFilesFromFirebase } from "./data/firebase"; import { loadFilesFromFirebase } from "./data/firebase";
import { import {
LibraryIndexedDBAdapter, LibraryIndexedDBAdapter,
@ -136,7 +134,8 @@ import DebugCanvas, {
import { AIComponents } from "./components/AI"; import { AIComponents } from "./components/AI";
import { ExcalidrawPlusIframeExport } from "./ExcalidrawPlusIframeExport"; import { ExcalidrawPlusIframeExport } from "./ExcalidrawPlusIframeExport";
import { fileSave } from "../packages/excalidraw/data/filesystem"; import { fileSave } from "../packages/excalidraw/data/filesystem";
import type { ExportToCanvasConfig } from "../packages/excalidraw/scene/export"; import { type ExportSceneConfig } from "../packages/excalidraw/scene/export";
import { round } from "../packages/math";
polyfill(); polyfill();
@ -618,18 +617,13 @@ const ExcalidrawWrapper = () => {
}, [excalidrawAPI]); }, [excalidrawAPI]);
const canvasPreviewContainerRef = useRef<HTMLDivElement>(null); const canvasPreviewContainerRef = useRef<HTMLDivElement>(null);
const svgPreviewContainerRef = useRef<HTMLDivElement>(null);
const [config, setConfig] = useState<ExportToCanvasConfig>( const [config, setConfig] = useState<ExportSceneConfig>({
JSON.parse(localStorage.getItem("_exportConfig") || "null") || { scale: 1,
width: 300, position: "center",
height: 100, fit: "contain",
padding: 2, });
scale: 1,
position: "none",
fit: "contain",
canvasBackgroundColor: "yellow",
},
);
useEffect(() => { useEffect(() => {
localStorage.setItem("_exportConfig", JSON.stringify(config)); localStorage.setItem("_exportConfig", JSON.stringify(config));
@ -676,25 +670,6 @@ const ExcalidrawWrapper = () => {
files, files,
}, },
config: { config: {
// // light yellow
// // canvasBackgroundColor: "#fff9c4",
// // width,
// // maxWidthOrHeight: 120,
// // scale: 0.01,
// // scale: 2,
// // origin: "content",
// // scale: 2,
// // x: 0,
// // y: 0,
// padding: 20,
// ...config,
// width: config.width,
// height: config.height,
// maxWidthOrHeight: config.maxWidthOrHeight,
// widthOrHeight: config.widthOrHeight,
// padding: config.padding,
...(frame ...(frame
? { ? {
...config, ...config,
@ -704,25 +679,40 @@ const ExcalidrawWrapper = () => {
y: frame.y, y: frame.y,
} }
: config), : config),
// // height: 140,
// // x: -appState.scrollX,
// // y: -appState.scrollY,
// // height: 150,
// // height: appState.height,
// // scale,
// // zoom: { value: appState.zoom.value },
// // getDimensions(width,height) {
// // setCanvasSize({ width, height })
// // return {width: 300, height: 150}
// // }
}, },
}).then((canvas) => { }).then((canvas) => {
if (canvasPreviewContainerRef.current) { if (canvasPreviewContainerRef.current) {
canvasPreviewContainerRef.current.replaceChildren(canvas); canvasPreviewContainerRef.current.replaceChildren(canvas);
document.querySelector( document.querySelector(
".dims", ".canvas_dims",
)!.innerHTML = `${canvas.width}x${canvas.height}`; )!.innerHTML = `${canvas.width}x${canvas.height} (canvas)`;
// canvas.style.width = "100%"; }
});
exportToSvg({
data: {
elements: nonDeletedElements.filter((x) => x.id !== frame?.id),
appState,
files,
},
config: {
...(frame
? {
...config,
width: frame.width,
height: frame.height,
x: frame.x,
y: frame.y,
}
: config),
},
}).then((svg) => {
if (svgPreviewContainerRef.current) {
svgPreviewContainerRef.current.replaceChildren(svg);
document.querySelector(".svg_dims")!.innerHTML = `${round(
parseFloat(svg.getAttribute("width") ?? ""),
0,
)}x${round(parseFloat(svg.getAttribute("height") ?? ""), 0)} (svg)`;
} }
}); });
@ -1430,7 +1420,7 @@ const ExcalidrawWrapper = () => {
</label> </label>
</div> </div>
</div> </div>
<div className="dims">0x0</div> <div className="canvas_dims">0x0</div>
<div <div
ref={canvasPreviewContainerRef} ref={canvasPreviewContainerRef}
onClick={() => { onClick={() => {
@ -1456,6 +1446,32 @@ const ExcalidrawWrapper = () => {
backgroundColor: "pink", backgroundColor: "pink",
}} }}
/> />
<div className="svg_dims">0x0</div>
<div
ref={svgPreviewContainerRef}
onClick={() => {
exportToBlob({
data: {
elements: excalidrawAPI!.getSceneElements(),
files: excalidrawAPI?.getFiles() || null,
},
config,
}).then((blob) => {
fileSave(blob, {
name: "xx",
extension: "png",
description: "xxx",
});
});
}}
style={{
borderRadius: 12,
border: "1px solid #777",
overflow: "hidden",
padding: 10,
backgroundColor: "pink",
}}
/>
</div> </div>
</div> </div>
); );