| 1 | export interface EditModeLayoutIslandChild { |
| 2 | index: number |
| 3 | x: number |
| 4 | y: number |
| 5 | width: number |
| 6 | height: number |
| 7 | } |
| 8 | |
| 9 | export interface EditModeLayoutIsland { |
| 10 | selector: string |
| 11 | width: number |
| 12 | height: number |
| 13 | children: EditModeLayoutIslandChild[] |
| 14 | } |
| 15 | |
| 16 | const MAX_LAYOUT_CHILDREN = 80 |
| 17 | |
| 18 | function normalizeFinite(value: unknown, min: number, max: number): number | null { |
| 19 | const numeric = Number(value) |
| 20 | if (!Number.isFinite(numeric) || numeric < min || numeric > max) return null |
| 21 | return Math.round(numeric * 10) / 10 |
| 22 | } |
| 23 | |
| 24 | export function normalizeEditModeLayoutIsland(value: unknown): EditModeLayoutIsland | undefined { |
| 25 | if (!value || typeof value !== 'object') return undefined |
| 26 | const record = value as { |
| 27 | selector?: unknown |
| 28 | width?: unknown |
| 29 | height?: unknown |
| 30 | children?: unknown |
| 31 | } |
| 32 | const selector = typeof record.selector === 'string' ? record.selector.trim() : '' |
| 33 | const width = normalizeFinite(record.width, 1, 3200) |
| 34 | const height = normalizeFinite(record.height, 1, 3200) |
| 35 | if (!selector || selector.length > 1000 || width === null || height === null) return undefined |
| 36 | if (!Array.isArray(record.children)) return undefined |
| 37 | const children = record.children |
| 38 | .map((value): EditModeLayoutIsland['children'][number] | null => { |
| 39 | if (!value || typeof value !== 'object') return null |
| 40 | const child = value as { |
| 41 | index?: unknown |
| 42 | x?: unknown |
| 43 | y?: unknown |
| 44 | width?: unknown |
| 45 | height?: unknown |
| 46 | } |
| 47 | const index = Number(child.index) |
| 48 | const x = normalizeFinite(child.x, -3200, 3200) |
| 49 | const y = normalizeFinite(child.y, -3200, 3200) |
| 50 | const childWidth = normalizeFinite(child.width, 1, 3200) |
| 51 | const childHeight = normalizeFinite(child.height, 1, 3200) |
| 52 | if ( |
| 53 | !Number.isInteger(index) || |
| 54 | index < 0 || |
| 55 | index > 200 || |
| 56 | x === null || |
| 57 | y === null || |
| 58 | childWidth === null || |
| 59 | childHeight === null |
| 60 | ) { |
| 61 | return null |
| 62 | } |
| 63 | return { index, x, y, width: childWidth, height: childHeight } |
| 64 | }) |
| 65 | .filter((child): child is EditModeLayoutIsland['children'][number] => child !== null) |
| 66 | .slice(0, MAX_LAYOUT_CHILDREN) |
| 67 | return children.length > 0 ? { selector, width, height, children } : undefined |
| 68 | } |
| 69 |