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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
import { buildGraphicPreviewUrl, buildDefaultGraphicInstance, graphicsRegistry, registerDefaultGraphics } from "@/lib/graphics";
import type { ParamValues } from "@/lib/params";
import { buildStickerId, parseStickerId } from "../sticker-id";
import type {
StickerBrowseResult,
StickerItem,
StickerProvider,
StickerSearchResult,
} from "../types";
const SHAPES_PROVIDER_ID = "shapes";
type ShapeGraphicPreset = {
shapeKey: string;
name: string;
definitionId: string;
params?: ParamValues;
};
const LEGACY_SHAPE_PRESETS: Record<string, ShapeGraphicPreset> = {
square: { shapeKey: "square", name: "Square", definitionId: "rectangle" },
circle: { shapeKey: "circle", name: "Circle", definitionId: "ellipse" },
triangle: {
shapeKey: "triangle",
name: "Triangle",
definitionId: "polygon",
params: { sides: 3 },
},
hexagon: {
shapeKey: "hexagon",
name: "Hexagon",
definitionId: "polygon",
params: { sides: 6 },
},
diamond: {
shapeKey: "diamond",
name: "Diamond",
definitionId: "polygon",
params: { sides: 4 },
},
star: { shapeKey: "star", name: "Star", definitionId: "star" },
};
function getShapePresets(): ShapeGraphicPreset[] {
registerDefaultGraphics();
return graphicsRegistry.getAll().map((definition) => ({
shapeKey: definition.id,
name: definition.name,
definitionId: definition.id,
}));
}
function getShapePreset({
shapeKey,
}: {
shapeKey: string;
}): ShapeGraphicPreset | null {
return (
getShapePresets().find((preset) => preset.shapeKey === shapeKey) ??
LEGACY_SHAPE_PRESETS[shapeKey] ??
null
);
}
function getShapeParams({
preset,
}: {
preset: ShapeGraphicPreset;
}): ParamValues {
return {
...buildDefaultGraphicInstance({ definitionId: preset.definitionId }).params,
...preset.params,
};
}
export function parseShapeStickerId({
stickerId,
}: {
stickerId: string;
}): ShapeGraphicPreset | null {
try {
const { providerValue } = parseStickerId({ stickerId });
return getShapePreset({ shapeKey: providerValue });
} catch {
return null;
}
}
function buildShapeUrl({ shapeKey }: { shapeKey: string }): string {
const preset = getShapePreset({ shapeKey });
if (!preset) {
return buildGraphicPreviewUrl({ definitionId: "rectangle" });
}
return buildGraphicPreviewUrl({
definitionId: preset.definitionId,
params: getShapeParams({ preset }),
});
}
function toStickerItem({
preset,
}: {
preset: ShapeGraphicPreset;
}): StickerItem {
return {
id: buildStickerId({
providerId: SHAPES_PROVIDER_ID,
providerValue: preset.shapeKey,
}),
provider: SHAPES_PROVIDER_ID,
name: preset.name,
previewUrl: buildShapeUrl({ shapeKey: preset.shapeKey }),
metadata: {
definitionId: preset.definitionId,
params: preset.params ?? {},
},
};
}
function filterShapesByQuery({ query }: { query: string }): ShapeGraphicPreset[] {
const normalizedQuery = query.trim().toLowerCase();
const presets = getShapePresets();
if (!normalizedQuery) {
return presets;
}
return presets.filter((preset) => {
const definition = graphicsRegistry.get(preset.definitionId);
return (
preset.name.toLowerCase().includes(normalizedQuery) ||
definition.keywords.some((keyword) =>
keyword.toLowerCase().includes(normalizedQuery),
)
);
});
}
function paginateShapes({
shapes,
options,
}: {
shapes: ShapeGraphicPreset[];
options?: { page?: number; limit?: number };
}): { items: ShapeGraphicPreset[]; hasMore: boolean; total: number } {
const page = Math.max(1, options?.page ?? 1);
const limit = Math.max(1, options?.limit ?? getShapePresets().length);
const startIndex = (page - 1) * limit;
const endIndex = startIndex + limit;
const pagedItems = shapes.slice(startIndex, endIndex);
return {
items: pagedItems,
hasMore: endIndex < shapes.length,
total: shapes.length,
};
}
export const shapesProvider: StickerProvider = {
id: SHAPES_PROVIDER_ID,
async search({
query,
options,
}: {
query: string;
options?: { limit?: number };
}): Promise<StickerSearchResult> {
const filteredShapes = filterShapesByQuery({ query });
const paged = paginateShapes({
shapes: filteredShapes,
options: { page: 1, limit: options?.limit ?? getShapePresets().length },
});
return {
items: paged.items.map((preset) => toStickerItem({ preset })),
total: paged.total,
hasMore: paged.hasMore,
};
},
async browse({
options,
}: {
options?: { page?: number; limit?: number };
}): Promise<StickerBrowseResult> {
const paged = paginateShapes({
shapes: getShapePresets(),
options,
});
return {
sections: [
{
id: "all",
items: paged.items.map((preset) => toStickerItem({ preset })),
hasMore: paged.hasMore,
layout: "grid",
},
],
};
},
resolveUrl({
stickerId,
}: {
stickerId: string;
options?: { width?: number; height?: number };
}): string {
const preset = parseShapeStickerId({ stickerId });
return buildShapeUrl({ shapeKey: preset?.shapeKey ?? "rectangle" });
},
};
|