From c19ffed5e423b6c6ef3c3774d9a212de83c33ca8 Mon Sep 17 00:00:00 2001 From: Luke Cotter <4013877+lukecotter@users.noreply.github.com> Date: Fri, 16 May 2025 18:06:40 +0100 Subject: [PATCH] fix: stop search from occuring multiple times --- .../components/analysis-view/AnalysisView.ts | 18 +++++----- .../components/calltree-view/CalltreeView.ts | 19 +++++----- .../components/calltree-view/module/Find.ts | 36 +++++++++++-------- .../components/database-view/DMLView.ts | 33 +++++++++++------ .../components/database-view/SOQLView.ts | 34 +++++++++++------- 5 files changed, 84 insertions(+), 56 deletions(-) diff --git a/log-viewer/modules/components/analysis-view/AnalysisView.ts b/log-viewer/modules/components/analysis-view/AnalysisView.ts index f11d4862..7607fe42 100644 --- a/log-viewer/modules/components/analysis-view/AnalysisView.ts +++ b/log-viewer/modules/components/analysis-view/AnalysisView.ts @@ -110,7 +110,7 @@ export class AnalysisView extends LitElement { options: { matchCase: false }, }; totalMatches = 0; - blockClearHighlights = false; + blockClearHighlights = true; constructor() { super(); @@ -239,8 +239,7 @@ export class AnalysisView extends LitElement { } } - const hasHighlights = Object.keys(this.findMap).length !== 0; - if (!hasHighlights) { + if (this.totalMatches <= 0) { return; } this.blockClearHighlights = true; @@ -420,7 +419,7 @@ export class AnalysisView extends LitElement { }); this.analysisTable.on('renderStarted', () => { - if (!this.blockClearHighlights) { + if (!this.blockClearHighlights && this.totalMatches > 0) { this._resetFindWidget(); this._clearSearchHighlights(); } @@ -432,11 +431,12 @@ export class AnalysisView extends LitElement { } _clearSearchHighlights() { - this._find( - new CustomEvent('lv-find-close', { - detail: { text: '', count: 0, options: { matchCase: false } }, - }), - ); + this.findArgs.text = ''; + this.findArgs.count = 0; + //@ts-expect-error This is a custom function added in by Find custom module + this.analysisTable.clearFindHighlights(Object.values(this.findMap)); + this.findMap = {}; + this.totalMatches = 0; } } export class Metric { diff --git a/log-viewer/modules/components/calltree-view/CalltreeView.ts b/log-viewer/modules/components/calltree-view/CalltreeView.ts index c26a9c37..85e210c3 100644 --- a/log-viewer/modules/components/calltree-view/CalltreeView.ts +++ b/log-viewer/modules/components/calltree-view/CalltreeView.ts @@ -57,7 +57,7 @@ export class CalltreeView extends LitElement { findMap: { [key: number]: RowComponent } = {}; totalMatches = 0; - blockClearHighlights = false; + blockClearHighlights = true; searchString = ''; findArgs: { text: string; count: number; options: { matchCase: boolean } } = { text: '', @@ -361,6 +361,7 @@ export class CalltreeView extends LitElement { if (clearHighlights) { newFindArgs.text = ''; } + if (newSearch || clearHighlights) { this.blockClearHighlights = true; //@ts-expect-error This is a custom function added in by Find custom module @@ -377,8 +378,7 @@ export class CalltreeView extends LitElement { } // Highlight the current row and reset the previous or next depending on whether we are stepping forward or back. - const hasHighlights = Object.keys(this.findMap).length !== 0; - if (!hasHighlights) { + if (this.totalMatches <= 0) { return; } this.blockClearHighlights = true; @@ -793,7 +793,7 @@ export class CalltreeView extends LitElement { }); this.calltreeTable.on('renderStarted', () => { - if (!this.blockClearHighlights) { + if (!this.blockClearHighlights && this.totalMatches > 0) { this._resetFindWidget(); this._clearSearchHighlights(); } @@ -810,11 +810,12 @@ export class CalltreeView extends LitElement { } private _clearSearchHighlights() { - this._find( - new CustomEvent('lv-find-close', { - detail: { text: '', count: 0, options: { matchCase: false } }, - }), - ); + this.findArgs.text = ''; + this.findArgs.count = 0; + //@ts-expect-error This is a custom function added in by Find custom module + this.calltreeTable.clearFindHighlights(Object.values(this.findMap)); + this.findMap = {}; + this.totalMatches = 0; } private _expandCollapseAll(rows: RowComponent[], expand: boolean = true) { diff --git a/log-viewer/modules/components/calltree-view/module/Find.ts b/log-viewer/modules/components/calltree-view/module/Find.ts index 9de4fbac..bb7e72b2 100644 --- a/log-viewer/modules/components/calltree-view/module/Find.ts +++ b/log-viewer/modules/components/calltree-view/module/Find.ts @@ -16,6 +16,8 @@ export class Find extends Module { super(table); // @ts-expect-error registerTableFunction() needs adding to tabulator types this.registerTableFunction('find', this._find.bind(this)); + // @ts-expect-error registerTableFunction() needs adding to tabulator types + this.registerTableFunction('clearFindHighlights', this._clearFindHighlights.bind(this)); } initialize() {} @@ -100,6 +102,16 @@ export class Find extends Module { return result; } + _clearFindHighlights(rows: RowComponent[]) { + this.table.blockRedraw(); + for (const row of rows) { + const data = row.getData(); + data.highlightIndexes = []; + row.reformat(); + } + this.table.restoreRedraw(); + } + _countMatches(elem: Node, findArgs: FindArgs, regex: RegExp) { let count = 0; @@ -201,25 +213,19 @@ export class Find extends Module { export function formatter(row: RowComponent, findArgs: FindArgs) { const { text, count } = findArgs; - if (!text || !count || !row.getData()) { + if (!text || !count || !row.getData() || !row.getData().highlightIndexes?.length) { return; } - requestAnimationFrame(() => { - const data = row.getData(); - const highlights = { - indexes: data.highlightIndexes, - currentMatch: 0, - }; - row.getCells().forEach((cell) => { - const cellElem = cell.getElement(); - _highlightText(cellElem, findArgs, highlights); - }); + const data = row.getData(); + const highlights = { + indexes: data.highlightIndexes, + currentMatch: 0, + }; - //@ts-expect-error This is private to tabulator, but we have no other choice atm. - if (row._getSelf().type === 'row') { - row.normalizeHeight(); - } + row.getCells().forEach((cell) => { + const cellElem = cell.getElement(); + _highlightText(cellElem, findArgs, highlights); }); } diff --git a/log-viewer/modules/components/database-view/DMLView.ts b/log-viewer/modules/components/database-view/DMLView.ts index f99b47df..a994ab8f 100644 --- a/log-viewer/modules/components/database-view/DMLView.ts +++ b/log-viewer/modules/components/database-view/DMLView.ts @@ -62,7 +62,7 @@ export class DMLView extends LitElement { }; findMap: { [key: number]: RowComponent } = {}; totalMatches = 0; - blockClearHighlights = false; + blockClearHighlights = true; constructor() { super(); @@ -196,6 +196,7 @@ export class DMLView extends LitElement { }); } + // todo: fix search on grouped data _highlightMatches(highlightIndex: number) { if (!this.dmlTable?.element?.clientHeight) { return; @@ -304,7 +305,7 @@ export class DMLView extends LitElement { selectableRows: 'highlight', dataTree: true, dataTreeBranchElement: false, - dataTreeStartExpanded: true, + dataTreeStartExpanded: false, columnDefaults: { title: 'default', resizable: true, @@ -381,7 +382,9 @@ export class DMLView extends LitElement { row.normalizeHeight(); } - formatter(row, this.findArgs); + requestAnimationFrame(() => { + formatter(row, this.findArgs); + }); }, }); @@ -391,9 +394,16 @@ export class DMLView extends LitElement { return; } - this.dmlTable?.blockRedraw(); group.toggle(); - this.dmlTable?.restoreRedraw(); + if (this.dmlTable && group.isVisible()) { + this.dmlTable.blockRedraw(); + for (const row of group.getRows()) { + if (row.getTreeChildren() && !row.isTreeExpanded()) { + row.treeExpand(); + } + } + this.dmlTable.restoreRedraw(); + } }); this.dmlTable.on('rowClick', function (e, row) { @@ -413,7 +423,7 @@ export class DMLView extends LitElement { }); this.dmlTable.on('renderStarted', () => { - if (!this.blockClearHighlights) { + if (!this.blockClearHighlights && this.totalMatches > 0) { this._resetFindWidget(); this._clearSearchHighlights(); } @@ -439,11 +449,12 @@ export class DMLView extends LitElement { } private _clearSearchHighlights() { - this._find( - new CustomEvent('lv-find-close', { - detail: { text: '', count: 0, options: { matchCase: false } }, - }), - ); + this.findArgs.text = ''; + this.findArgs.count = 0; + //@ts-expect-error This is a custom function added in by Find custom module + this.dmlTable.clearFindHighlights(Object.values(this.findMap)); + this.findMap = {}; + this.totalMatches = 0; } _getTable() { diff --git a/log-viewer/modules/components/database-view/SOQLView.ts b/log-viewer/modules/components/database-view/SOQLView.ts index bca63642..fa3affc0 100644 --- a/log-viewer/modules/components/database-view/SOQLView.ts +++ b/log-viewer/modules/components/database-view/SOQLView.ts @@ -73,7 +73,7 @@ export class SOQLView extends LitElement { }; findMap: { [key: number]: RowComponent } = {}; totalMatches = 0; - blockClearHighlights = false; + blockClearHighlights = true; get _soqlTableWrapper(): HTMLDivElement | null { return this.renderRoot?.querySelector('#db-soql-table') ?? null; @@ -346,7 +346,7 @@ export class SOQLView extends LitElement { }, dataTree: true, dataTreeBranchElement: false, - dataTreeStartExpanded: true, + dataTreeStartExpanded: false, columnDefaults: { title: 'default', resizable: true, @@ -510,7 +510,9 @@ export class SOQLView extends LitElement { row.normalizeHeight(); } - formatter(row, this.findArgs); + requestAnimationFrame(() => { + formatter(row, this.findArgs); + }); }, }); @@ -519,10 +521,17 @@ export class SOQLView extends LitElement { if (type === 'Range') { return; } - - this.soqlTable?.blockRedraw(); group.toggle(); - this.soqlTable?.restoreRedraw(); + + if (this.soqlTable && group.isVisible()) { + this.soqlTable.blockRedraw(); + for (const row of group.getRows()) { + if (row.getTreeChildren() && !row.isTreeExpanded()) { + row.treeExpand(); + } + } + this.soqlTable.restoreRedraw(); + } }); this.soqlTable.on('rowClick', function (e, row) { @@ -542,7 +551,7 @@ export class SOQLView extends LitElement { }); this.soqlTable.on('renderStarted', () => { - if (!this.blockClearHighlights) { + if (!this.blockClearHighlights && this.totalMatches > 0) { this._resetFindWidget(); this._clearSearchHighlights(); } @@ -568,11 +577,12 @@ export class SOQLView extends LitElement { } _clearSearchHighlights() { - this._find( - new CustomEvent('lv-find-close', { - detail: { text: '', count: 0, options: { matchCase: false } }, - }), - ); + this.findArgs.text = ''; + this.findArgs.count = 0; + //@ts-expect-error This is a custom function added in by Find custom module + this.soqlTable.clearFindHighlights(Object.values(this.findMap)); + this.findMap = {}; + this.totalMatches = 0; } _getTable() {