| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | type InfographicOptions, |
| 5 | type ParsedInfographicOptions, |
| 6 | } from "@antv/infographic"; |
| 7 | |
| 8 | export function cloneSerializableOptions( |
| 9 | options: Partial<InfographicOptions>, |
| 10 | ): Partial<InfographicOptions> { |
| 11 | try { |
| 12 | return JSON.parse(JSON.stringify(options)) as Partial<InfographicOptions>; |
| 13 | } catch { |
| 14 | return { |
| 15 | data: options.data, |
| 16 | elements: options.elements, |
| 17 | height: options.height, |
| 18 | padding: options.padding, |
| 19 | svg: options.svg, |
| 20 | template: options.template, |
| 21 | theme: options.theme, |
| 22 | themeConfig: options.themeConfig, |
| 23 | width: options.width, |
| 24 | }; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | export function pickSerializableOptions( |
| 29 | options: Partial<InfographicOptions>, |
| 30 | ): Partial<InfographicOptions> { |
| 31 | const { |
| 32 | container: _container, |
| 33 | plugins: _plugins, |
| 34 | interactions: _interactions, |
| 35 | ...rest |
| 36 | } = options; |
| 37 | |
| 38 | return cloneSerializableOptions(rest); |
| 39 | } |
| 40 | |
| 41 | export function toSerializableOptionsFromParsed( |
| 42 | parsed: Partial<ParsedInfographicOptions>, |
| 43 | ): Partial<InfographicOptions> { |
| 44 | return cloneSerializableOptions({ |
| 45 | data: parsed.data, |
| 46 | elements: parsed.shapes, |
| 47 | height: parsed.height, |
| 48 | padding: parsed.padding, |
| 49 | svg: parsed.svg, |
| 50 | template: parsed.template, |
| 51 | theme: parsed.theme, |
| 52 | themeConfig: parsed.themeConfig, |
| 53 | width: parsed.width, |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | function normalizeSerializableValue(value: unknown): unknown { |
| 58 | if (Array.isArray(value)) { |
| 59 | return value.map(normalizeSerializableValue); |
| 60 | } |
| 61 | |
| 62 | if (value && typeof value === "object") { |
| 63 | const record = value as Record<string, unknown>; |
| 64 | |
| 65 | return Object.fromEntries( |
| 66 | Object.keys(record) |
| 67 | .sort() |
| 68 | .filter((key) => record[key] !== undefined) |
| 69 | .map((key) => [key, normalizeSerializableValue(record[key])]), |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | return value; |
| 74 | } |
| 75 | |
| 76 | export function getSerializableOptionsKey(value: unknown): string { |
| 77 | return JSON.stringify(normalizeSerializableValue(value)) ?? "undefined"; |
| 78 | } |
| 79 |