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-06 19:34:22 +04:00
|
|
|
|
2020-03-17 20:55:40 +01:00
|
|
|
import {
|
|
|
|
ExcalidrawElement,
|
|
|
|
ExcalidrawTextElement,
|
|
|
|
ExcalidrawLinearElement,
|
|
|
|
ExcalidrawGenericElement,
|
|
|
|
} from "../element/types";
|
2020-01-21 00:16:22 +01:00
|
|
|
import { measureText } from "../utils";
|
|
|
|
|
2020-03-17 20:55:40 +01:00
|
|
|
type ElementConstructorOpts = {
|
|
|
|
x: ExcalidrawGenericElement["x"];
|
|
|
|
y: ExcalidrawGenericElement["y"];
|
|
|
|
strokeColor: ExcalidrawGenericElement["strokeColor"];
|
|
|
|
backgroundColor: ExcalidrawGenericElement["backgroundColor"];
|
|
|
|
fillStyle: ExcalidrawGenericElement["fillStyle"];
|
|
|
|
strokeWidth: ExcalidrawGenericElement["strokeWidth"];
|
|
|
|
roughness: ExcalidrawGenericElement["roughness"];
|
|
|
|
opacity: ExcalidrawGenericElement["opacity"];
|
|
|
|
width?: ExcalidrawGenericElement["width"];
|
|
|
|
height?: ExcalidrawGenericElement["height"];
|
|
|
|
};
|
|
|
|
|
|
|
|
function _newElementBase<T extends ExcalidrawElement>(
|
|
|
|
type: T["type"],
|
|
|
|
{
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
strokeColor,
|
|
|
|
backgroundColor,
|
|
|
|
fillStyle,
|
|
|
|
strokeWidth,
|
|
|
|
roughness,
|
|
|
|
opacity,
|
|
|
|
width = 0,
|
|
|
|
height = 0,
|
|
|
|
...rest
|
|
|
|
}: ElementConstructorOpts & Partial<ExcalidrawGenericElement>,
|
2020-01-06 19:34:22 +04:00
|
|
|
) {
|
2020-03-17 20:55:40 +01:00
|
|
|
return {
|
|
|
|
id: rest.id || nanoid(),
|
2020-01-07 19:04:52 +04:00
|
|
|
type,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
strokeColor,
|
|
|
|
backgroundColor,
|
|
|
|
fillStyle,
|
|
|
|
strokeWidth,
|
|
|
|
roughness,
|
|
|
|
opacity,
|
2020-03-17 20:55:40 +01:00
|
|
|
seed: rest.seed ?? randomSeed(),
|
|
|
|
version: rest.version || 1,
|
|
|
|
versionNonce: rest.versionNonce ?? 0,
|
|
|
|
isDeleted: rest.isDeleted ?? false,
|
2020-01-06 19:34:22 +04:00
|
|
|
};
|
2020-03-17 20:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function newElement(
|
|
|
|
opts: {
|
|
|
|
type: ExcalidrawGenericElement["type"];
|
|
|
|
} & ElementConstructorOpts,
|
|
|
|
): ExcalidrawGenericElement {
|
|
|
|
return _newElementBase<ExcalidrawGenericElement>(opts.type, opts);
|
2020-01-06 19:34:22 +04:00
|
|
|
}
|
2020-01-08 19:54:42 +01:00
|
|
|
|
2020-01-21 00:16:22 +01:00
|
|
|
export function newTextElement(
|
2020-03-17 20:55:40 +01:00
|
|
|
opts: {
|
|
|
|
text: string;
|
|
|
|
font: string;
|
|
|
|
} & ElementConstructorOpts,
|
|
|
|
): ExcalidrawTextElement {
|
|
|
|
const { text, font } = opts;
|
2020-01-21 00:16:22 +01:00
|
|
|
const metrics = measureText(text, font);
|
2020-03-17 20:55:40 +01:00
|
|
|
const textElement = {
|
|
|
|
..._newElementBase<ExcalidrawTextElement>("text", opts),
|
2020-01-21 00:16:22 +01:00
|
|
|
text: text,
|
|
|
|
font: font,
|
|
|
|
// Center the text
|
2020-03-17 20:55:40 +01:00
|
|
|
x: opts.x - metrics.width / 2,
|
|
|
|
y: opts.y - metrics.height / 2,
|
2020-01-21 00:16:22 +01:00
|
|
|
width: metrics.width,
|
|
|
|
height: metrics.height,
|
2020-01-24 12:04:54 +02:00
|
|
|
baseline: metrics.baseline,
|
2020-01-21 00:16:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return textElement;
|
|
|
|
}
|
|
|
|
|
2020-03-17 20:55:40 +01:00
|
|
|
export function newLinearElement(
|
|
|
|
opts: {
|
|
|
|
type: "arrow" | "line";
|
|
|
|
} & ElementConstructorOpts,
|
|
|
|
): ExcalidrawLinearElement {
|
|
|
|
return {
|
|
|
|
..._newElementBase<ExcalidrawLinearElement>(opts.type, opts),
|
|
|
|
points: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-19 22:28:11 +01:00
|
|
|
// Simplified deep clone for the purpose of cloning ExcalidrawElement only
|
|
|
|
// (doesn't clone Date, RegExp, Map, Set, Typed arrays etc.)
|
|
|
|
//
|
|
|
|
// Adapted from https://github.com/lukeed/klona
|
|
|
|
function _duplicateElement(val: any, depth: number = 0) {
|
|
|
|
if (val == null || typeof val !== "object") {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.prototype.toString.call(val) === "[object Object]") {
|
|
|
|
const tmp =
|
|
|
|
typeof val.constructor === "function"
|
|
|
|
? Object.create(Object.getPrototypeOf(val))
|
|
|
|
: {};
|
2020-03-07 10:20:38 -05:00
|
|
|
for (const key in val) {
|
|
|
|
if (val.hasOwnProperty(key)) {
|
2020-02-19 22:28:11 +01:00
|
|
|
// don't copy top-level shape property, which we want to regenerate
|
2020-03-07 10:20:38 -05:00
|
|
|
if (depth === 0 && (key === "shape" || key === "canvas")) {
|
2020-02-19 22:28:11 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-03-07 10:20:38 -05:00
|
|
|
tmp[key] = _duplicateElement(val[key], depth + 1);
|
2020-02-19 22:28:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tmp;
|
2020-02-09 23:57:14 +01:00
|
|
|
}
|
|
|
|
|
2020-02-19 22:28:11 +01:00
|
|
|
if (Array.isArray(val)) {
|
|
|
|
let k = val.length;
|
|
|
|
const arr = new Array(k);
|
|
|
|
while (k--) {
|
|
|
|
arr[k] = _duplicateElement(val[k], depth + 1);
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2020-03-17 20:55:40 +01:00
|
|
|
export function duplicateElement<TElement extends Mutable<ExcalidrawElement>>(
|
|
|
|
element: TElement,
|
|
|
|
overrides?: Partial<TElement>,
|
|
|
|
): TElement {
|
|
|
|
let copy: TElement = _duplicateElement(element);
|
2020-01-08 19:54:42 +01:00
|
|
|
copy.id = nanoid();
|
|
|
|
copy.seed = randomSeed();
|
2020-03-17 20:55:40 +01:00
|
|
|
if (overrides) {
|
|
|
|
copy = Object.assign(copy, overrides);
|
|
|
|
}
|
2020-01-08 19:54:42 +01:00
|
|
|
return copy;
|
|
|
|
}
|