-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory.ts
65 lines (55 loc) · 1.43 KB
/
factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import * as React from "react";
import { Grip, Point } from "./geometry";
export interface ShapeContent {
content: JSX.Element;
settings?: JSX.Element;
grips: Grip[];
outline: Point[];
offset(delta: { x: number, y: number }): void;
commit(keep: boolean): void;
dispose(): void;
}
export function placeholder(type: string): ShapeContent {
type; // unused
return {
get content() {
return React.createElement("g");
},
get settings() {
return React.createElement("div");
},
offset() {},
commit() {},
grips: [],
outline: [],
json: undefined,
dispose() {}
} as ShapeContent;
};
const types: {
[type: string]: {
displayName: string;
alloc: () => ShapeContent;
}
} = {};
export const factory = {
register(type: string, displayName: string, alloc: () => ShapeContent) {
if (types[type]) {
throw new Error(`Duplicate type: ${type}`);
}
types[type] = { displayName, alloc };
},
alloc(type: string) {
const registration = types[type];
if (!registration) {
return placeholder(type);
}
return registration.alloc();
},
types(): string[] {
return Object.keys(types);
},
displayName(type: string) {
return (types[type] || { displayName: `Unknown: ${type}` }).displayName;
}
};