|
1 | 1 | /* |
2 | 2 | * Copyright (c) 2024 Certinia Inc. All rights reserved. |
3 | 3 | */ |
4 | | -import { |
5 | | - Module, |
6 | | - type CellComponent, |
7 | | - type GroupComponent, |
8 | | - type RowComponent, |
9 | | - type Tabulator, |
10 | | -} from 'tabulator-tables'; |
| 4 | +import { Module, type GroupComponent, type RowComponent, type Tabulator } from 'tabulator-tables'; |
11 | 5 |
|
12 | 6 | export class Find extends Module { |
13 | 7 | static moduleName = 'FindModule'; |
@@ -53,51 +47,50 @@ export class Find extends Module { |
53 | 47 | const regex = new RegExp(searchString, `g${findArgs.options.matchCase ? '' : 'i'}`); |
54 | 48 |
|
55 | 49 | tbl.blockRedraw(); |
56 | | - let totalMatches = 0; |
57 | | - const rowsToReformat = []; |
58 | | - const len = flattenedRows.length; |
59 | | - for (let i = 0; i < len; i++) { |
60 | | - const row = flattenedRows[i]; |
61 | | - if (!row) { |
62 | | - continue; |
63 | | - } |
64 | | - |
65 | | - let clearHighlight = false; |
| 50 | + for (const row of flattenedRows) { |
66 | 51 | const data = row.getData(); |
67 | | - if (data.highlightIndexes?.length) { |
68 | | - clearHighlight = true; |
69 | | - rowsToReformat.push(row); |
| 52 | + if (data.highlightIndexes?.length > 0) { |
| 53 | + data.highlightIndexes.length = 0; |
| 54 | + row.reformat(); |
| 55 | + } else if (!data.highlightIndexes) { |
| 56 | + data.highlightIndexes = []; |
70 | 57 | } |
| 58 | + } |
| 59 | + tbl.restoreRedraw(); |
71 | 60 |
|
72 | | - data.highlightIndexes = []; |
73 | | - |
74 | | - if (!searchString) { |
75 | | - continue; |
76 | | - } |
77 | | - let reformat = false; |
78 | | - |
79 | | - row.getCells().forEach((cell: CellComponent) => { |
80 | | - const elem = cell.getElement(); |
81 | | - const matchCount = this._countMatches(elem, findArgs, regex); |
82 | | - if (matchCount) { |
83 | | - const kLen = matchCount; |
84 | | - for (let k = 0; k < kLen; k++) { |
85 | | - totalMatches++; |
86 | | - data.highlightIndexes.push(totalMatches); |
87 | | - result.matchIndexes[totalMatches] = row; |
88 | | - } |
89 | | - reformat = true; |
| 61 | + await new Promise((resolve) => requestAnimationFrame(resolve)); |
| 62 | + let totalMatches = 0; |
| 63 | + if (searchString) { |
| 64 | + const rowsToReformat = new Set<RowComponent>(); |
| 65 | + const len = flattenedRows.length; |
| 66 | + for (let i = 0; i < len; i++) { |
| 67 | + const row = flattenedRows[i]; |
| 68 | + if (!row) { |
| 69 | + continue; |
90 | 70 | } |
91 | | - }); |
92 | 71 |
|
93 | | - if (reformat && !clearHighlight) { |
94 | | - rowsToReformat.push(row); |
| 72 | + const data = row.getData(); |
| 73 | + data.highlightIndexes = []; |
| 74 | + row.getCells().forEach((cell) => { |
| 75 | + const elem = cell.getElement(); |
| 76 | + const matchCount = this._countMatches(elem, findArgs, regex); |
| 77 | + if (matchCount) { |
| 78 | + const kLen = matchCount; |
| 79 | + for (let k = 0; k < kLen; k++) { |
| 80 | + totalMatches++; |
| 81 | + data.highlightIndexes.push(totalMatches); |
| 82 | + result.matchIndexes[totalMatches] = row; |
| 83 | + } |
| 84 | + rowsToReformat.add(row); |
| 85 | + } |
| 86 | + }); |
95 | 87 | } |
| 88 | + tbl.blockRedraw(); |
| 89 | + rowsToReformat.forEach((row) => { |
| 90 | + row?.reformat(); |
| 91 | + }); |
| 92 | + tbl.restoreRedraw(); |
96 | 93 | } |
97 | | - rowsToReformat.forEach((row) => { |
98 | | - row?.reformat(); |
99 | | - }); |
100 | | - tbl.restoreRedraw(); |
101 | 94 |
|
102 | 95 | result.totalMatches = totalMatches; |
103 | 96 | return result; |
|
0 commit comments