fix text constructor groupIds & improve type safety (#1715)

This commit is contained in:
David Luzar 2020-06-06 13:32:43 +02:00 committed by GitHub
parent 4eb6c3e8a4
commit d1be2a5481
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 15 deletions

View File

@ -14,20 +14,16 @@ import { newElementWith } from "./mutateElement";
import { getNewGroupIdsForDuplication } from "../groups"; import { getNewGroupIdsForDuplication } from "../groups";
import { AppState } from "../types"; import { AppState } from "../types";
type ElementConstructorOpts = { type ElementConstructorOpts = MarkOptional<
x: ExcalidrawGenericElement["x"]; Omit<ExcalidrawGenericElement, "id" | "type" | "isDeleted">,
y: ExcalidrawGenericElement["y"]; | "width"
strokeColor: ExcalidrawGenericElement["strokeColor"]; | "height"
backgroundColor: ExcalidrawGenericElement["backgroundColor"]; | "angle"
fillStyle: ExcalidrawGenericElement["fillStyle"]; | "groupIds"
strokeWidth: ExcalidrawGenericElement["strokeWidth"]; | "seed"
strokeStyle: ExcalidrawGenericElement["strokeStyle"]; | "version"
roughness: ExcalidrawGenericElement["roughness"]; | "versionNonce"
opacity: ExcalidrawGenericElement["opacity"]; >;
width?: ExcalidrawGenericElement["width"];
height?: ExcalidrawGenericElement["height"];
angle?: ExcalidrawGenericElement["angle"];
};
const _newElementBase = <T extends ExcalidrawElement>( const _newElementBase = <T extends ExcalidrawElement>(
type: T["type"], type: T["type"],
@ -44,6 +40,7 @@ const _newElementBase = <T extends ExcalidrawElement>(
width = 0, width = 0,
height = 0, height = 0,
angle = 0, angle = 0,
groupIds = [],
...rest ...rest
}: ElementConstructorOpts & Omit<Partial<ExcalidrawGenericElement>, "type">, }: ElementConstructorOpts & Omit<Partial<ExcalidrawGenericElement>, "type">,
) => ({ ) => ({
@ -61,11 +58,11 @@ const _newElementBase = <T extends ExcalidrawElement>(
strokeStyle, strokeStyle,
roughness, roughness,
opacity, opacity,
groupIds,
seed: rest.seed ?? randomInteger(), seed: rest.seed ?? randomInteger(),
version: rest.version || 1, version: rest.version || 1,
versionNonce: rest.versionNonce ?? 0, versionNonce: rest.versionNonce ?? 0,
isDeleted: false as false, isDeleted: false as false,
groupIds: [],
}); });
export const newElement = ( export const newElement = (

3
src/global.d.ts vendored
View File

@ -25,3 +25,6 @@ type ResolutionType<T extends (...args: any) => any> = T extends (
) => Promise<infer R> ) => Promise<infer R>
? R ? R
: any; : any;
// https://github.com/krzkaczor/ts-essentials
type MarkOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;