add option to transform viewport to scene coords in transform api

This commit is contained in:
Aakansha Doshi 2023-08-31 11:08:10 +05:30
parent c8fd157914
commit d203794f70
2 changed files with 35 additions and 14 deletions

View file

@ -38,9 +38,14 @@ import {
VerticalAlign,
} from "../element/types";
import { MarkOptional } from "../utility-types";
import { assertNever, getFontString } from "../utils";
import {
assertNever,
getFontString,
viewportCoordsToSceneCoords,
} from "../utils";
import { getSizeFromPoints } from "../points";
import { nanoid } from "nanoid";
import { AppState } from "../types";
export type ValidLinearElement = {
type: "arrow" | "line";
@ -387,7 +392,8 @@ class ElementStore {
export const convertToExcalidrawElements = (
elements: ExcalidrawElementSkeleton[] | null,
regenerateIds = false,
appState: AppState,
{ regenerateIds = false, transformViewportToSceneCoords = false },
) => {
if (!elements) {
return [];
@ -404,6 +410,20 @@ export const convertToExcalidrawElements = (
if (regenerateIds) {
Object.assign(element, { id: nanoid() });
}
// transform viewport coords to scene coordinates
if (transformViewportToSceneCoords) {
const { x, y } = viewportCoordsToSceneCoords(
{
clientX: element.x,
clientY: element.y,
},
appState,
);
Object.assign(element, { x, y });
}
switch (element.type) {
case "rectangle":
case "ellipse":