|
| 1 | +/** |
| 2 | + * Formula reference highlights. |
| 3 | + * |
| 4 | + * Manages DOM-level highlights for cells/ranges referenced in the formula |
| 5 | + * being edited. Applied directly via setAttribute to avoid React re-renders. |
| 6 | + */ |
| 7 | + |
| 8 | +const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 9 | + |
| 10 | +const colLabelToIndex = (label: string, headerValues?: string[]): number => { |
| 11 | + const values = headerValues && headerValues.length ? headerValues : alphabet; |
| 12 | + if (typeof values === "string") { |
| 13 | + let n = 0; |
| 14 | + for (const ch of label.toUpperCase()) { |
| 15 | + n = n * 26 + (alphabet.indexOf(ch) + 1); |
| 16 | + } |
| 17 | + return n - 1; |
| 18 | + } |
| 19 | + return (values as string[]).indexOf(label); |
| 20 | +}; |
| 21 | + |
| 22 | +const extractRefs = ( |
| 23 | + formula: string, |
| 24 | + headerValues?: string[], |
| 25 | + gridRows = 0, |
| 26 | + gridCols = 0, |
| 27 | +): Array<[number, number]> => { |
| 28 | + const cells: Array<[number, number]> = []; |
| 29 | + const rangeRe = /([A-Z]+)(\d+):([A-Z]+)(\d+)/gi; |
| 30 | + const cellRe = /\b([A-Z]+)(\d+)\b/gi; |
| 31 | + const rangeMatches: Array<{ start: number; end: number }> = []; |
| 32 | + let m: RegExpExecArray | null; |
| 33 | + |
| 34 | + rangeRe.lastIndex = 0; |
| 35 | + while ((m = rangeRe.exec(formula)) !== null) { |
| 36 | + rangeMatches.push({ start: m.index, end: m.index + m[0].length }); |
| 37 | + const c1 = colLabelToIndex(m[1], headerValues); |
| 38 | + const r1 = parseInt(m[2], 10) - 1; |
| 39 | + const c2 = colLabelToIndex(m[3], headerValues); |
| 40 | + const r2 = parseInt(m[4], 10) - 1; |
| 41 | + const rMin = Math.min(r1, r2); |
| 42 | + const rMax = Math.max(r1, r2); |
| 43 | + const cMin = Math.min(c1, c2); |
| 44 | + const cMax = Math.max(c1, c2); |
| 45 | + for (let r = rMin; r <= rMax; r++) |
| 46 | + for (let c = cMin; c <= cMax; c++) |
| 47 | + if (gridRows === 0 || (r < gridRows && c < gridCols)) cells.push([r, c]); |
| 48 | + } |
| 49 | + |
| 50 | + cellRe.lastIndex = 0; |
| 51 | + while ((m = cellRe.exec(formula)) !== null) { |
| 52 | + if (!rangeMatches.some((rng) => m!.index >= rng.start && m!.index < rng.end)) { |
| 53 | + const c = colLabelToIndex(m[1], headerValues); |
| 54 | + const r = parseInt(m[2], 10) - 1; |
| 55 | + if (gridRows === 0 || (r < gridRows && c < gridCols)) cells.push([r, c]); |
| 56 | + } |
| 57 | + } |
| 58 | + return cells; |
| 59 | +}; |
| 60 | + |
| 61 | +let _highlightedIds: string[] = []; |
| 62 | +let _highlightedAxes: { el: Element; attr: string }[] = []; |
| 63 | + |
| 64 | +/** |
| 65 | + * Parse the formula and set `data-formula-ref` on referenced cell DOM elements. |
| 66 | + * Also highlights column <th> and row axis <td> headers. |
| 67 | + * Pure DOM — zero React re-renders. |
| 68 | + */ |
| 69 | +export const updateFormulaHighlights = ( |
| 70 | + formulaValue: string, |
| 71 | + formulaRow: number, |
| 72 | + formulaCol: number, |
| 73 | + headerValues?: string[], |
| 74 | + gridRows = 0, |
| 75 | + gridCols = 0, |
| 76 | +): void => { |
| 77 | + for (const id of _highlightedIds) { |
| 78 | + const el = document.getElementById(id); |
| 79 | + if (el) { |
| 80 | + el.removeAttribute("data-formula-ref"); |
| 81 | + } |
| 82 | + } |
| 83 | + _highlightedIds = []; |
| 84 | + for (const { el, attr } of _highlightedAxes) el.removeAttribute(attr); |
| 85 | + _highlightedAxes = []; |
| 86 | + |
| 87 | + if (!formulaValue.startsWith("=")) return; |
| 88 | + |
| 89 | + const refs = extractRefs(formulaValue.slice(1), headerValues, gridRows, gridCols); |
| 90 | + const highlightedCols = new Set<number>(); |
| 91 | + const highlightedRows = new Set<number>(); |
| 92 | + |
| 93 | + for (const [r, c] of refs) { |
| 94 | + if (r === formulaRow && c === formulaCol) continue; |
| 95 | + const el = document.getElementById(`${r}-${c}`); |
| 96 | + if (el) { |
| 97 | + el.setAttribute("data-formula-ref", "1"); |
| 98 | + _highlightedIds.push(`${r}-${c}`); |
| 99 | + } |
| 100 | + highlightedCols.add(c); |
| 101 | + highlightedRows.add(r); |
| 102 | + } |
| 103 | + |
| 104 | + const headerThs = document.querySelectorAll(".sheet-table thead th"); |
| 105 | + for (const col of highlightedCols) { |
| 106 | + const th = headerThs[col + 1] as HTMLElement | undefined; |
| 107 | + if (th) { |
| 108 | + th.setAttribute("data-formula-col", "1"); |
| 109 | + _highlightedAxes.push({ el: th, attr: "data-formula-col" }); |
| 110 | + } |
| 111 | + } |
| 112 | + for (const row of highlightedRows) { |
| 113 | + const tr = document.querySelector(`.sheet-table tbody tr:nth-child(${row + 1})`); |
| 114 | + const axisTd = tr?.querySelector("td:first-child"); |
| 115 | + if (axisTd) { |
| 116 | + axisTd.setAttribute("data-formula-row", "1"); |
| 117 | + _highlightedAxes.push({ el: axisTd, attr: "data-formula-row" }); |
| 118 | + } |
| 119 | + } |
| 120 | +}; |
| 121 | + |
| 122 | +export const clearFormulaHighlights = (): void => { |
| 123 | + for (const id of _highlightedIds) { |
| 124 | + document.getElementById(id)?.removeAttribute("data-formula-ref"); |
| 125 | + } |
| 126 | + _highlightedIds = []; |
| 127 | + for (const { el, attr } of _highlightedAxes) el.removeAttribute(attr); |
| 128 | + _highlightedAxes = []; |
| 129 | +}; |
0 commit comments