Skip to content

Commit 7447900

Browse files
committed
refactor: split clear highlight + search logic
1 parent a694b69 commit 7447900

1 file changed

Lines changed: 39 additions & 45 deletions

File tree

  • log-viewer/modules/components/calltree-view/module

log-viewer/modules/components/calltree-view/module/Find.ts

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
/*
22
* Copyright (c) 2024 Certinia Inc. All rights reserved.
33
*/
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';
115

126
export class Find extends Module {
137
static moduleName = 'FindModule';
@@ -30,6 +24,7 @@ export class Find extends Module {
3024

3125
this._clearMatches();
3226

27+
// We only do this when groups exist to get row order
3328
const flattenFromGrps = (row: GroupComponent): RowComponent[] => {
3429
const mergedArray: RowComponent[] = [];
3530
Array.prototype.push.apply(mergedArray, row.getRows());
@@ -52,51 +47,50 @@ export class Find extends Module {
5247
const regex = new RegExp(searchString, `g${findArgs.options.matchCase ? '' : 'i'}`);
5348

5449
tbl.blockRedraw();
55-
let totalMatches = 0;
56-
const rowsToReformat = [];
57-
const len = flattenedRows.length;
58-
for (let i = 0; i < len; i++) {
59-
const row = flattenedRows[i];
60-
if (!row) {
61-
continue;
62-
}
63-
64-
let clearHighlight = false;
50+
for (const row of flattenedRows) {
6551
const data = row.getData();
66-
if (data.highlightIndexes?.length) {
67-
clearHighlight = true;
68-
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 = [];
6957
}
58+
}
59+
tbl.restoreRedraw();
7060

71-
data.highlightIndexes = [];
72-
73-
if (!searchString) {
74-
continue;
75-
}
76-
let reformat = false;
77-
78-
row.getCells().forEach((cell: CellComponent) => {
79-
const elem = cell.getElement();
80-
const matchCount = this._countMatches(elem, findArgs, regex);
81-
if (matchCount) {
82-
const kLen = matchCount;
83-
for (let k = 0; k < kLen; k++) {
84-
totalMatches++;
85-
data.highlightIndexes.push(totalMatches);
86-
result.matchIndexes[totalMatches] = row;
87-
}
88-
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;
8970
}
90-
});
9171

92-
if (reformat && !clearHighlight) {
93-
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+
});
9487
}
88+
tbl.blockRedraw();
89+
rowsToReformat.forEach((row) => {
90+
row?.reformat();
91+
});
92+
tbl.restoreRedraw();
9593
}
96-
rowsToReformat.forEach((row) => {
97-
row?.reformat();
98-
});
99-
tbl.restoreRedraw();
10094

10195
result.totalMatches = totalMatches;
10296
return result;

0 commit comments

Comments
 (0)