|
| 1 | +import type { BorderSpec, CellBorders, Run, TableBorders, TableBorderValue } from '@superdoc/contracts'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Hash helpers for block version computation. |
| 5 | + * |
| 6 | + * Duplicated from painters/dom/src/paragraph-hash-utils.ts to avoid a circular |
| 7 | + * dependency (painter-dom -> layout-resolved is not allowed). Keep the two |
| 8 | + * copies in sync. |
| 9 | + */ |
| 10 | + |
| 11 | +// --------------------------------------------------------------------------- |
| 12 | +// Table/Cell border hashing |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | + |
| 15 | +const isNoneBorder = (value: TableBorderValue): value is { none: true } => { |
| 16 | + return typeof value === 'object' && value !== null && 'none' in value && (value as { none: true }).none === true; |
| 17 | +}; |
| 18 | + |
| 19 | +const isBorderSpec = (value: unknown): value is BorderSpec => { |
| 20 | + return typeof value === 'object' && value !== null && !('none' in value); |
| 21 | +}; |
| 22 | + |
| 23 | +export const hashBorderSpec = (border: BorderSpec): string => { |
| 24 | + const parts: string[] = []; |
| 25 | + if (border.style !== undefined) parts.push(`s:${border.style}`); |
| 26 | + if (border.width !== undefined) parts.push(`w:${border.width}`); |
| 27 | + if (border.color !== undefined) parts.push(`c:${border.color}`); |
| 28 | + if (border.space !== undefined) parts.push(`sp:${border.space}`); |
| 29 | + return parts.join(','); |
| 30 | +}; |
| 31 | + |
| 32 | +const hashTableBorderValue = (borderValue: TableBorderValue | undefined): string => { |
| 33 | + if (borderValue === undefined) return ''; |
| 34 | + if (borderValue === null) return 'null'; |
| 35 | + if (isNoneBorder(borderValue)) return 'none'; |
| 36 | + if (isBorderSpec(borderValue)) { |
| 37 | + return hashBorderSpec(borderValue); |
| 38 | + } |
| 39 | + return ''; |
| 40 | +}; |
| 41 | + |
| 42 | +export const hashTableBorders = (borders: TableBorders | undefined): string => { |
| 43 | + if (!borders) return ''; |
| 44 | + const parts: string[] = []; |
| 45 | + if (borders.top !== undefined) parts.push(`t:[${hashTableBorderValue(borders.top)}]`); |
| 46 | + if (borders.right !== undefined) parts.push(`r:[${hashTableBorderValue(borders.right)}]`); |
| 47 | + if (borders.bottom !== undefined) parts.push(`b:[${hashTableBorderValue(borders.bottom)}]`); |
| 48 | + if (borders.left !== undefined) parts.push(`l:[${hashTableBorderValue(borders.left)}]`); |
| 49 | + if (borders.insideH !== undefined) parts.push(`ih:[${hashTableBorderValue(borders.insideH)}]`); |
| 50 | + if (borders.insideV !== undefined) parts.push(`iv:[${hashTableBorderValue(borders.insideV)}]`); |
| 51 | + return parts.join(';'); |
| 52 | +}; |
| 53 | + |
| 54 | +export const hashCellBorders = (borders: CellBorders | undefined): string => { |
| 55 | + if (!borders) return ''; |
| 56 | + const parts: string[] = []; |
| 57 | + if (borders.top) parts.push(`t:[${hashBorderSpec(borders.top)}]`); |
| 58 | + if (borders.right) parts.push(`r:[${hashBorderSpec(borders.right)}]`); |
| 59 | + if (borders.bottom) parts.push(`b:[${hashBorderSpec(borders.bottom)}]`); |
| 60 | + if (borders.left) parts.push(`l:[${hashBorderSpec(borders.left)}]`); |
| 61 | + return parts.join(';'); |
| 62 | +}; |
| 63 | + |
| 64 | +// --------------------------------------------------------------------------- |
| 65 | +// Run property accessors |
| 66 | +// --------------------------------------------------------------------------- |
| 67 | + |
| 68 | +const hasStringProp = (run: Run, prop: string): run is Run & Record<string, string> => { |
| 69 | + return prop in run && typeof (run as Record<string, unknown>)[prop] === 'string'; |
| 70 | +}; |
| 71 | + |
| 72 | +const hasNumberProp = (run: Run, prop: string): run is Run & Record<string, number> => { |
| 73 | + return prop in run && typeof (run as Record<string, unknown>)[prop] === 'number'; |
| 74 | +}; |
| 75 | + |
| 76 | +const hasBooleanProp = (run: Run, prop: string): run is Run & Record<string, boolean> => { |
| 77 | + return prop in run && typeof (run as Record<string, unknown>)[prop] === 'boolean'; |
| 78 | +}; |
| 79 | + |
| 80 | +export const getRunStringProp = (run: Run, prop: string): string => { |
| 81 | + if (hasStringProp(run, prop)) { |
| 82 | + return run[prop]; |
| 83 | + } |
| 84 | + return ''; |
| 85 | +}; |
| 86 | + |
| 87 | +export const getRunNumberProp = (run: Run, prop: string): number => { |
| 88 | + if (hasNumberProp(run, prop)) { |
| 89 | + return run[prop]; |
| 90 | + } |
| 91 | + return 0; |
| 92 | +}; |
| 93 | + |
| 94 | +export const getRunBooleanProp = (run: Run, prop: string): boolean => { |
| 95 | + if (hasBooleanProp(run, prop)) { |
| 96 | + return run[prop]; |
| 97 | + } |
| 98 | + return false; |
| 99 | +}; |
| 100 | + |
| 101 | +export const getRunUnderlineStyle = (run: Run): string => { |
| 102 | + if ('underline' in run && typeof run.underline === 'boolean') { |
| 103 | + return run.underline ? 'single' : ''; |
| 104 | + } |
| 105 | + if ('underline' in run && run.underline && typeof run.underline === 'object') { |
| 106 | + return (run.underline as { style?: string }).style ?? ''; |
| 107 | + } |
| 108 | + return ''; |
| 109 | +}; |
| 110 | + |
| 111 | +export const getRunUnderlineColor = (run: Run): string => { |
| 112 | + if ('underline' in run && run.underline && typeof run.underline === 'object') { |
| 113 | + return (run.underline as { color?: string }).color ?? ''; |
| 114 | + } |
| 115 | + return ''; |
| 116 | +}; |
0 commit comments