summaryrefslogtreecommitdiff
path: root/apps/web/src/masks/index.ts
blob: be5faef70f577a5d6d2f9cca1e3554483503de27 (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import type { Mask, MaskDefaultContext, MaskType } from "@/masks/types";
import {
	BASE_MASK_PARAM_DEFINITIONS,
	builtinMasksRegistry,
	type RegisteredBuiltinMaskDefinition,
} from "./registry";
import { freeformMaskDefinition } from "./freeform/definition";
import { generateUUID } from "@/utils/id";
import { SquareIcon } from "@hugeicons/core-free-icons";

export { builtinMasksRegistry } from "./registry";
export { registerBuiltinMasks as registerDefaultMasks } from "./builtin/definitions";

type MaskWithoutId = Mask extends infer TMask
	? TMask extends Mask
		? Omit<TMask, "id">
		: never
	: never;

function withMaskId({ mask, id }: { mask: MaskWithoutId; id: string }): Mask {
	switch (mask.type) {
		case "split":
			return { ...mask, id };
		case "cinematic-bars":
			return { ...mask, id };
		case "rectangle":
			return { ...mask, id };
		case "ellipse":
			return { ...mask, id };
		case "heart":
			return { ...mask, id };
		case "diamond":
			return { ...mask, id };
		case "star":
			return { ...mask, id };
		case "text":
			return { ...mask, id };
		case "freeform":
			return { ...mask, id };
	}
}

export function getMaskDefinition(maskType: MaskType): RegisteredBuiltinMaskDefinition {
	if (maskType === "freeform") {
		return {
			...freeformMaskDefinition,
			params: [...freeformMaskDefinition.params, ...BASE_MASK_PARAM_DEFINITIONS],
			icon: { icon: SquareIcon },
		} as RegisteredBuiltinMaskDefinition;
	}

	return builtinMasksRegistry.get(maskType);
}

export function getMaskDefinitionsForMenu() {
	return [
		...builtinMasksRegistry.getAll(),
		{
			...freeformMaskDefinition,
			name: "Pen tool",
			params: [...freeformMaskDefinition.params, ...BASE_MASK_PARAM_DEFINITIONS],
			icon: { icon: SquareIcon },
		},
	];
}

export function buildDefaultMaskInstance({
	maskType,
	elementSize,
}: {
	maskType: MaskType;
	elementSize?: { width: number; height: number };
}): Mask {
	const definition = getMaskDefinition(maskType);
	const context: MaskDefaultContext = { elementSize };
	return withMaskId({
		mask: definition.buildDefault(context),
		id: generateUUID(),
	});
}