2020-01-13 11:04:28 -08:00
|
|
|
import { randomSeed } from "roughjs/bin/math";
|
2020-01-07 23:49:39 +04:00
|
|
|
import nanoid from "nanoid";
|
2020-01-12 04:00:00 +04:00
|
|
|
import { Drawable } from "roughjs/bin/core";
|
2020-01-06 19:34:22 +04:00
|
|
|
|
2020-01-21 00:16:22 +01:00
|
|
|
import { ExcalidrawElement, ExcalidrawTextElement } from "../element/types";
|
|
|
|
import { measureText } from "../utils";
|
|
|
|
|
2020-01-06 19:34:22 +04:00
|
|
|
export function newElement(
|
|
|
|
type: string,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
strokeColor: string,
|
|
|
|
backgroundColor: string,
|
|
|
|
fillStyle: string,
|
|
|
|
strokeWidth: number,
|
|
|
|
roughness: number,
|
|
|
|
opacity: number,
|
|
|
|
width = 0,
|
|
|
|
height = 0
|
|
|
|
) {
|
|
|
|
const element = {
|
2020-01-07 23:49:39 +04:00
|
|
|
id: nanoid(),
|
2020-01-07 19:04:52 +04:00
|
|
|
type,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
strokeColor,
|
|
|
|
backgroundColor,
|
|
|
|
fillStyle,
|
|
|
|
strokeWidth,
|
|
|
|
roughness,
|
|
|
|
opacity,
|
2020-01-06 19:34:22 +04:00
|
|
|
isSelected: false,
|
2020-01-12 04:00:00 +04:00
|
|
|
seed: randomSeed(),
|
|
|
|
shape: null as Drawable | Drawable[] | null
|
2020-01-06 19:34:22 +04:00
|
|
|
};
|
|
|
|
return element;
|
|
|
|
}
|
2020-01-08 19:54:42 +01:00
|
|
|
|
2020-01-21 00:16:22 +01:00
|
|
|
export function newTextElement(
|
|
|
|
element: ExcalidrawElement,
|
|
|
|
text: string,
|
|
|
|
font: string
|
|
|
|
) {
|
|
|
|
const metrics = measureText(text, font);
|
|
|
|
const textElement: ExcalidrawTextElement = {
|
|
|
|
...element,
|
|
|
|
type: "text",
|
|
|
|
text: text,
|
|
|
|
font: font,
|
|
|
|
// Center the text
|
|
|
|
x: element.x - metrics.width / 2,
|
|
|
|
y: element.y - metrics.height / 2,
|
|
|
|
width: metrics.width,
|
|
|
|
height: metrics.height,
|
|
|
|
baseline: metrics.baseline
|
|
|
|
};
|
|
|
|
|
|
|
|
return textElement;
|
|
|
|
}
|
|
|
|
|
2020-01-08 19:54:42 +01:00
|
|
|
export function duplicateElement(element: ReturnType<typeof newElement>) {
|
|
|
|
const copy = { ...element };
|
2020-01-11 19:35:06 -08:00
|
|
|
delete copy.shape;
|
2020-01-08 19:54:42 +01:00
|
|
|
copy.id = nanoid();
|
|
|
|
copy.seed = randomSeed();
|
|
|
|
return copy;
|
|
|
|
}
|