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

View File

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