make opts optional and use 100% zoom when inserting to canvas

This commit is contained in:
Aakansha Doshi 2023-08-31 13:34:28 +05:30
parent d203794f70
commit acd6db5304
2 changed files with 16 additions and 9 deletions

View File

@ -1,5 +1,5 @@
import { useState, useRef, useEffect } from "react"; import { useState, useRef, useEffect } from "react";
import { AppState, BinaryFiles } from "../types"; import { AppState, BinaryFiles, NormalizedZoomValue } from "../types";
import { updateActiveTool } from "../utils"; import { updateActiveTool } from "../utils";
import { useApp, useExcalidrawSetAppState } from "./App"; import { useApp, useExcalidrawSetAppState } from "./App";
import { Button } from "./Button"; import { Button } from "./Button";
@ -140,10 +140,17 @@ const MermaidToExcalidraw = ({
mermaidToExcalidrawLib.current.graphToExcalidraw(mermaidGraphData); mermaidToExcalidrawLib.current.graphToExcalidraw(mermaidGraphData);
data.current = { data.current = {
elements: convertToExcalidrawElements(elements, appState, { elements: convertToExcalidrawElements(
elements,
{
...appState,
zoom: { ...appState.zoom, value: 1 as NormalizedZoomValue },
},
{
regenerateIds: true, regenerateIds: true,
transformViewportToSceneCoords: true, transformViewportToSceneCoords: true,
}), },
),
files, files,
}; };
const parent = canvasNode.parentElement!; const parent = canvasNode.parentElement!;

View File

@ -392,8 +392,8 @@ class ElementStore {
export const convertToExcalidrawElements = ( export const convertToExcalidrawElements = (
elements: ExcalidrawElementSkeleton[] | null, elements: ExcalidrawElementSkeleton[] | null,
appState: AppState, appState?: AppState,
{ regenerateIds = false, transformViewportToSceneCoords = false }, opts?: { regenerateIds: boolean; transformViewportToSceneCoords: boolean },
) => { ) => {
if (!elements) { if (!elements) {
return []; return [];
@ -407,12 +407,12 @@ export const convertToExcalidrawElements = (
for (const element of elements) { for (const element of elements) {
let excalidrawElement: ExcalidrawElement; let excalidrawElement: ExcalidrawElement;
const originalId = element.id; const originalId = element.id;
if (regenerateIds) { if (opts?.regenerateIds) {
Object.assign(element, { id: nanoid() }); Object.assign(element, { id: nanoid() });
} }
// transform viewport coords to scene coordinates // transform viewport coords to scene coordinates
if (transformViewportToSceneCoords) { if (opts?.transformViewportToSceneCoords && appState) {
const { x, y } = viewportCoordsToSceneCoords( const { x, y } = viewportCoordsToSceneCoords(
{ {
clientX: element.x, clientX: element.x,