|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | + |
| 3 | +/** Legacy single entry or multi-link array — XLSX round-trip uses the first link only. */ |
| 4 | +export function getFirstHyperlinkEntry( |
| 5 | + raw: unknown, |
| 6 | +): { linkType: string; linkAddress: string } | undefined { |
| 7 | + if (Array.isArray(raw)) { |
| 8 | + const first = raw[0]; |
| 9 | + if ( |
| 10 | + first && |
| 11 | + typeof first === 'object' && |
| 12 | + typeof (first as any).linkType === 'string' && |
| 13 | + typeof (first as any).linkAddress === 'string' |
| 14 | + ) { |
| 15 | + return first as { linkType: string; linkAddress: string }; |
| 16 | + } |
| 17 | + return undefined; |
| 18 | + } |
| 19 | + if ( |
| 20 | + raw && |
| 21 | + typeof raw === 'object' && |
| 22 | + typeof (raw as any).linkType === 'string' && |
| 23 | + typeof (raw as any).linkAddress === 'string' |
| 24 | + ) { |
| 25 | + return raw as { linkType: string; linkAddress: string }; |
| 26 | + } |
| 27 | + return undefined; |
| 28 | +} |
| 29 | + |
| 30 | +/** Plain text from `ct.s` — fast path when a single run (e.g. post-import normalization). */ |
| 31 | +export function concatInlineStrRunsText(runs: unknown[]): string { |
| 32 | + if (!Array.isArray(runs) || runs.length === 0) return ''; |
| 33 | + if (runs.length === 1) return String((runs[0] as any)?.v ?? ''); |
| 34 | + let out = ''; |
| 35 | + for (let i = 0; i < runs.length; i += 1) { |
| 36 | + out += String((runs[i] as any)?.v ?? ''); |
| 37 | + } |
| 38 | + return out; |
| 39 | +} |
| 40 | + |
| 41 | +/** Copy `ct` fields except text container keys (fc/un/s/t) — avoids spreading large `s` arrays. */ |
| 42 | +function pickCtPreservedForHyperlinkInline(ct: unknown): Record<string, unknown> { |
| 43 | + if (!ct || typeof ct !== 'object' || Array.isArray(ct)) return {}; |
| 44 | + const src = ct as Record<string, unknown>; |
| 45 | + const out: Record<string, unknown> = {}; |
| 46 | + for (const k in src) { |
| 47 | + if (!Object.prototype.hasOwnProperty.call(src, k)) continue; |
| 48 | + if (k === 'fc' || k === 'un' || k === 's' || k === 't') continue; |
| 49 | + out[k] = src[k]; |
| 50 | + } |
| 51 | + return out; |
| 52 | +} |
| 53 | + |
| 54 | +export type HyperlinkEntryLite = { linkType: string; linkAddress: string }; |
| 55 | + |
| 56 | +/** |
| 57 | + * After Excel import: one `ct.s` run with link + typography; strip root/ct fc/un so the grid |
| 58 | + * reads styles from segments only (matches native hyperlink cells). |
| 59 | + */ |
| 60 | +export function normalizeImportedHyperlinkCellV( |
| 61 | + cellV: Record<string, unknown>, |
| 62 | + hyperlink: HyperlinkEntryLite, |
| 63 | +): void { |
| 64 | + const fallbackInlineText = String(cellV.m ?? cellV.v ?? '').replace(/\r\n/g, '\n'); |
| 65 | + |
| 66 | + let mergedText = ''; |
| 67 | + let baseRun: Record<string, unknown> | null = null; |
| 68 | + const ctPrev = cellV.ct as { t?: string; s?: unknown[] } | undefined; |
| 69 | + |
| 70 | + if ( |
| 71 | + ctPrev?.t === 'inlineStr' && |
| 72 | + Array.isArray(ctPrev.s) && |
| 73 | + ctPrev.s.length > 0 |
| 74 | + ) { |
| 75 | + const runs = ctPrev.s as Record<string, unknown>[]; |
| 76 | + if (runs.length === 1) { |
| 77 | + const seg0 = runs[0]; |
| 78 | + mergedText = String(seg0?.v ?? ''); |
| 79 | + if (mergedText.length > 0) baseRun = seg0; |
| 80 | + } else { |
| 81 | + for (let i = 0; i < runs.length; i += 1) { |
| 82 | + const seg = runs[i]; |
| 83 | + const txt = String(seg?.v ?? ''); |
| 84 | + if (txt.length > 0 && baseRun == null) baseRun = seg; |
| 85 | + mergedText += txt; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + if (mergedText.length === 0) { |
| 90 | + mergedText = fallbackInlineText; |
| 91 | + } |
| 92 | + |
| 93 | + const singleRun: Record<string, unknown> = { |
| 94 | + v: mergedText, |
| 95 | + fs: baseRun?.fs ?? cellV.fs ?? 10, |
| 96 | + ff: baseRun?.ff ?? cellV.ff ?? 0, |
| 97 | + bl: baseRun?.bl ?? cellV.bl ?? 0, |
| 98 | + it: baseRun?.it ?? cellV.it ?? 0, |
| 99 | + cl: baseRun?.cl ?? cellV.cl ?? 0, |
| 100 | + fc: |
| 101 | + baseRun?.fc != null && String(baseRun.fc).length > 0 |
| 102 | + ? baseRun.fc |
| 103 | + : 'rgb(0, 0, 255)', |
| 104 | + un: |
| 105 | + baseRun?.un != null && baseRun.un !== 0 ? baseRun.un : 1, |
| 106 | + link: { |
| 107 | + linkType: hyperlink.linkType, |
| 108 | + linkAddress: hyperlink.linkAddress, |
| 109 | + }, |
| 110 | + }; |
| 111 | + |
| 112 | + const prevCt = pickCtPreservedForHyperlinkInline(cellV.ct); |
| 113 | + cellV.ct = { |
| 114 | + ...prevCt, |
| 115 | + t: 'inlineStr', |
| 116 | + s: [singleRun], |
| 117 | + }; |
| 118 | + cellV.m = mergedText; |
| 119 | + cellV.v = mergedText; |
| 120 | + |
| 121 | + delete cellV.fc; |
| 122 | + delete cellV.un; |
| 123 | + const ct = cellV.ct; |
| 124 | + if (ct && typeof ct === 'object' && !Array.isArray(ct)) { |
| 125 | + delete (ct as { fc?: unknown }).fc; |
| 126 | + delete (ct as { un?: unknown }).un; |
| 127 | + } |
| 128 | +} |
0 commit comments