fix api and support individual shapes and text element

This commit is contained in:
Aakansha Doshi 2023-05-15 16:32:56 +05:30
parent acde193a64
commit 32aaa8b95d
5 changed files with 53 additions and 24 deletions

View file

@ -1546,7 +1546,6 @@ class App extends React.Component<AppProps, AppState> {
let file = event?.clipboardData?.files[0]; let file = event?.clipboardData?.files[0];
const data = await parseClipboard(event, isPlainPaste); const data = await parseClipboard(event, isPlainPaste);
if (!file && data.text && !isPlainPaste) { if (!file && data.text && !isPlainPaste) {
const string = data.text.trim(); const string = data.text.trim();
if (string.startsWith("<svg") && string.endsWith("</svg>")) { if (string.startsWith("<svg") && string.endsWith("</svg>")) {

View file

@ -122,8 +122,8 @@ const restoreElementWithProperties = <
y: extra.y ?? element.y ?? 0, y: extra.y ?? element.y ?? 0,
strokeColor: element.strokeColor || oc.black, strokeColor: element.strokeColor || oc.black,
backgroundColor: element.backgroundColor || "transparent", backgroundColor: element.backgroundColor || "transparent",
width: element.width || 100, width: element.width,
height: element.height || 100, height: element.height,
seed: element.seed ?? 1, seed: element.seed ?? 1,
groupIds: element.groupIds ?? [], groupIds: element.groupIds ?? [],
roundness: element.roundness roundness: element.roundness

View file

@ -38,21 +38,17 @@ export interface ImportedDataState {
elements?: elements?:
| readonly ( | readonly (
| (ExcalidrawElement & { | (ExcalidrawElement & {
label?: [ label?: { text: string } & MarkOptional<
{ text: string } & MarkOptional< ElementConstructorOpts,
ElementConstructorOpts, "x" | "y"
"x" | "y" >;
>,
];
}) })
| { | {
type: Exclude<ExcalidrawGenericElement["type"], "selection">; type: Exclude<ExcalidrawGenericElement["type"], "selection">;
label?: [ label?: { text: string } & MarkOptional<
{ text: string } & MarkOptional< ElementConstructorOpts,
ElementConstructorOpts, "x" | "y"
"x" | "y" > &
>,
] &
MarkOptional<ElementConstructorOpts, "x" | "y">; MarkOptional<ElementConstructorOpts, "x" | "y">;
} }
)[] )[]

View file

@ -50,6 +50,12 @@ import { isArrowElement } from "./typeChecks";
import { MarkOptional, Merge, Mutable } from "../utility-types"; import { MarkOptional, Merge, Mutable } from "../utility-types";
import { ImportedDataState } from "../data/types"; import { ImportedDataState } from "../data/types";
const ELEMENTS_SUPPORTING_PROGRAMMATIC_API = [
"rectangle",
"ellipse",
"diamond",
"text",
];
export type ElementConstructorOpts = MarkOptional< export type ElementConstructorOpts = MarkOptional<
Omit<ExcalidrawGenericElement, "id" | "type" | "isDeleted" | "updated">, Omit<ExcalidrawGenericElement, "id" | "type" | "isDeleted" | "updated">,
| "width" | "width"
@ -95,6 +101,7 @@ const _newElementBase = <T extends ExcalidrawElement>(
...rest ...rest
}: ElementConstructorOpts & Omit<Partial<ExcalidrawGenericElement>, "type">, }: ElementConstructorOpts & Omit<Partial<ExcalidrawGenericElement>, "type">,
) => { ) => {
console.log("width", width);
// assign type to guard against excess properties // assign type to guard against excess properties
const element: Merge<ExcalidrawGenericElement, { type: T["type"] }> = { const element: Merge<ExcalidrawGenericElement, { type: T["type"] }> = {
id: rest.id || randomId(), id: rest.id || randomId(),
@ -143,7 +150,6 @@ const getTextElementPositionOffsets = (
height: number; height: number;
}, },
) => { ) => {
console.log("metrics", metrics);
return { return {
x: x:
opts.textAlign === "center" opts.textAlign === "center"
@ -655,25 +661,56 @@ export const convertToExcalidrawElements = (
if (!elements) { if (!elements) {
return []; return [];
} }
console.log(elements, "ele ");
elements.forEach((element) => { elements.forEach((element) => {
if (!element) { if (!element) {
return; return;
} }
const textElement = element.label?.find((child) => child.text !== null);
if ( if (
isValidTextContainer(element) && isValidTextContainer(element) &&
textElement && element?.label?.text &&
(element.type === "rectangle" || (element.type === "rectangle" ||
element.type === "ellipse" || element.type === "ellipse" ||
element.type === "diamond") element.type === "diamond")
) { ) {
const elements = bindTextToContainer(element, textElement); const elements = bindTextToContainer(element, element.label);
res.push(...elements); res.push(...elements);
} else { } else {
let excalidrawElement;
if (element.type === "text") {
//@ts-ignore
excalidrawElement = newTextElement({
//@ts-ignore
x: 0,
//@ts-ignore
y: 0,
...element,
});
} else {
//@ts-ignore
excalidrawElement = newElement({
x: 0,
y: 0,
...element,
width:
//@ts-ignore
element?.width ||
(ELEMENTS_SUPPORTING_PROGRAMMATIC_API.includes(element.type)
? 100
: 0),
height:
//@ts-ignore
element?.height ||
(ELEMENTS_SUPPORTING_PROGRAMMATIC_API.includes(element.type)
? 100
: 0),
});
}
//@ts-ignore //@ts-ignore
res.push(element);
res.push(excalidrawElement);
} }
}); });
return res; return res;
}; };

View file

@ -1,10 +1,7 @@
import { getFontString, arrayToMap, isTestEnv } from "../utils"; import { getFontString, arrayToMap, isTestEnv } from "../utils";
import { import {
ExcalidrawDiamondElement,
ExcalidrawElement, ExcalidrawElement,
ExcalidrawEllipseElement,
ExcalidrawGenericElement, ExcalidrawGenericElement,
ExcalidrawRectangleElement,
ExcalidrawTextContainer, ExcalidrawTextContainer,
ExcalidrawTextElement, ExcalidrawTextElement,
ExcalidrawTextElementWithContainer, ExcalidrawTextElementWithContainer,