fix elements insert position and viewport centering
This commit is contained in:
parent
d070760b2f
commit
9b1c88eed9
@ -2244,11 +2244,12 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
private addElementsFromPasteOrLibrary = (opts: {
|
addElementsFromPasteOrLibrary = (opts: {
|
||||||
elements: readonly ExcalidrawElement[];
|
elements: readonly ExcalidrawElement[];
|
||||||
files: BinaryFiles | null;
|
files: BinaryFiles | null;
|
||||||
position: { clientX: number; clientY: number } | "cursor" | "center";
|
position: { clientX: number; clientY: number } | "cursor" | "center";
|
||||||
retainSeed?: boolean;
|
retainSeed?: boolean;
|
||||||
|
fitToContent?: boolean;
|
||||||
}) => {
|
}) => {
|
||||||
const elements = restoreElements(opts.elements, null, undefined);
|
const elements = restoreElements(opts.elements, null, undefined);
|
||||||
const [minX, minY, maxX, maxY] = getCommonBounds(elements);
|
const [minX, minY, maxX, maxY] = getCommonBounds(elements);
|
||||||
@ -2353,6 +2354,12 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
this.setActiveTool({ type: "selection" });
|
this.setActiveTool({ type: "selection" });
|
||||||
|
|
||||||
|
if (opts.fitToContent) {
|
||||||
|
this.scrollToContent(newElements, {
|
||||||
|
fitToContent: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private addTextFromPaste(text: string, isPlainPaste = false) {
|
private addTextFromPaste(text: string, isPlainPaste = false) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { useState, useRef, useEffect } from "react";
|
import { useState, useRef, useEffect } from "react";
|
||||||
import { AppState, BinaryFiles, NormalizedZoomValue } from "../types";
|
import { AppState, BinaryFiles } 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,17 +140,9 @@ const MermaidToExcalidraw = ({
|
|||||||
mermaidToExcalidrawLib.current.graphToExcalidraw(mermaidGraphData);
|
mermaidToExcalidrawLib.current.graphToExcalidraw(mermaidGraphData);
|
||||||
|
|
||||||
data.current = {
|
data.current = {
|
||||||
elements: convertToExcalidrawElements(
|
elements: convertToExcalidrawElements(elements, {
|
||||||
elements,
|
regenerateIds: true,
|
||||||
{
|
}),
|
||||||
...appState,
|
|
||||||
zoom: { ...appState.zoom, value: 1 as NormalizedZoomValue },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
regenerateIds: true,
|
|
||||||
transformViewportToSceneCoords: true,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
files,
|
files,
|
||||||
};
|
};
|
||||||
const parent = canvasNode.parentElement!;
|
const parent = canvasNode.parentElement!;
|
||||||
@ -186,12 +178,12 @@ const MermaidToExcalidraw = ({
|
|||||||
|
|
||||||
const onSelect = () => {
|
const onSelect = () => {
|
||||||
const { elements: newElements, files } = data.current;
|
const { elements: newElements, files } = data.current;
|
||||||
app.scene.replaceAllElements([...elements, ...newElements]);
|
app.addElementsFromPasteOrLibrary({
|
||||||
app.addFiles(Object.values(files || []));
|
elements: newElements,
|
||||||
app.scrollToContent(newElements);
|
files,
|
||||||
|
position: "center",
|
||||||
app.setSelection(newElements);
|
fitToContent: true,
|
||||||
|
});
|
||||||
onClose();
|
onClose();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -38,14 +38,9 @@ import {
|
|||||||
VerticalAlign,
|
VerticalAlign,
|
||||||
} from "../element/types";
|
} from "../element/types";
|
||||||
import { MarkOptional } from "../utility-types";
|
import { MarkOptional } from "../utility-types";
|
||||||
import {
|
import { assertNever, getFontString } from "../utils";
|
||||||
assertNever,
|
|
||||||
getFontString,
|
|
||||||
viewportCoordsToSceneCoords,
|
|
||||||
} from "../utils";
|
|
||||||
import { getSizeFromPoints } from "../points";
|
import { getSizeFromPoints } from "../points";
|
||||||
import { nanoid } from "nanoid";
|
import { nanoid } from "nanoid";
|
||||||
import { AppState } from "../types";
|
|
||||||
|
|
||||||
export type ValidLinearElement = {
|
export type ValidLinearElement = {
|
||||||
type: "arrow" | "line";
|
type: "arrow" | "line";
|
||||||
@ -392,8 +387,7 @@ class ElementStore {
|
|||||||
|
|
||||||
export const convertToExcalidrawElements = (
|
export const convertToExcalidrawElements = (
|
||||||
elements: ExcalidrawElementSkeleton[] | null,
|
elements: ExcalidrawElementSkeleton[] | null,
|
||||||
appState?: AppState,
|
opts?: { regenerateIds: boolean },
|
||||||
opts?: { regenerateIds: boolean; transformViewportToSceneCoords: boolean },
|
|
||||||
) => {
|
) => {
|
||||||
if (!elements) {
|
if (!elements) {
|
||||||
return [];
|
return [];
|
||||||
@ -411,19 +405,6 @@ export const convertToExcalidrawElements = (
|
|||||||
Object.assign(element, { id: nanoid() });
|
Object.assign(element, { id: nanoid() });
|
||||||
}
|
}
|
||||||
|
|
||||||
// transform viewport coords to scene coordinates
|
|
||||||
if (opts?.transformViewportToSceneCoords && appState) {
|
|
||||||
const { x, y } = viewportCoordsToSceneCoords(
|
|
||||||
{
|
|
||||||
clientX: element.x,
|
|
||||||
clientY: element.y,
|
|
||||||
},
|
|
||||||
appState,
|
|
||||||
);
|
|
||||||
|
|
||||||
Object.assign(element, { x, y });
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (element.type) {
|
switch (element.type) {
|
||||||
case "rectangle":
|
case "rectangle":
|
||||||
case "ellipse":
|
case "ellipse":
|
||||||
|
@ -528,6 +528,7 @@ export type AppClassProperties = {
|
|||||||
scrollToContent: App["scrollToContent"];
|
scrollToContent: App["scrollToContent"];
|
||||||
addFiles: App["addFiles"];
|
addFiles: App["addFiles"];
|
||||||
setSelection: App["setSelection"];
|
setSelection: App["setSelection"];
|
||||||
|
addElementsFromPasteOrLibrary: App["addElementsFromPasteOrLibrary"];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PointerDownState = Readonly<{
|
export type PointerDownState = Readonly<{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user