Skip to content

Commit

Permalink
feat: search next with pressing enter
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Aug 1, 2023
1 parent d37837e commit 4a98e5b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/provider/ADataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ abstract class ADataProvider extends AEventDispatcher implements IDataProvider {
* @param search
* @param col
*/
abstract searchAndJump(search: string | RegExp, col: Column): void;
abstract searchAndJump(search: string | RegExp, col: Column, first?: boolean): number[] | void;

jumpToNearest(indices: number[]) {
if (indices.length === 0) {
Expand Down
14 changes: 12 additions & 2 deletions src/provider/LocalDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,18 @@ export default class LocalDataProvider extends ACommonDataProvider {
return lazySeq(Array.from(indices)).map((i) => col.getRawNumber(this._dataRows[i]));
}

searchAndJump(search: string | RegExp, col: Column) {
searchAndJump(search: string | RegExp, col: Column, first?: boolean) {
//case insensitive search
const indices = this.search(search, col);
if (first && indices.length > 0) {
this.jumpToNearest(indices.slice(0, 1));
} else {
this.jumpToNearest(indices);
}
return indices;
}

search(search: string | RegExp, col: Column): number[] {
search = typeof search === 'string' ? search.toLowerCase() : search;
const f =
typeof search === 'string'
Expand All @@ -625,7 +635,7 @@ export default class LocalDataProvider extends ACommonDataProvider {
indices.push(i);
}
}
this.jumpToNearest(indices);
return indices;
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/provider/RemoteDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,13 @@ export default class RemoteDataProvider extends ACommonDataProvider {
return this.server.mappingSample((col as any).desc.column);
}

searchAndJump(search: string | RegExp, col: Column) {
searchAndJump(search: string | RegExp, col: Column, first?: boolean) {
this.server.search(search, (col as any).desc.column).then((indices) => {
this.jumpToNearest(indices);
if (indices.length > 0 && first) {
this.jumpToNearest(indices.slice(0, 1));
} else {
this.jumpToNearest(indices);
}
});
}
}
4 changes: 3 additions & 1 deletion src/provider/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ export interface IDataProvider extends AEventDispatcher {

mappingSample(col: Column): Promise<ISequence<number>> | ISequence<number>;

searchAndJump(search: string | RegExp, col: Column): void;
searchAndJump(search: string | RegExp, col: Column, first?: boolean): number[] | void;

jumpToNearest(indices: number[]);

getRankings(): Ranking[];

Expand Down
31 changes: 27 additions & 4 deletions src/ui/dialogs/SearchDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import { debounce } from '../../internal';

/** @internal */
export default class SearchDialog extends ADialog {
private current: {
isRegex: boolean;
search: string;
indices: number[];
index: number;
} | null = null;

constructor(private readonly column: Column, dialog: IDialogContext, private readonly provider: IDataProvider) {
super(dialog, {
livePreview: 'search',
Expand Down Expand Up @@ -57,12 +64,28 @@ export default class SearchDialog extends ADialog {
const input = this.findInput('input[type="text"]')!;
const checkbox = this.findInput('input[type="checkbox"]')!;

let search: any = input.value;
let search: string = input.value;
const isRegex = checkbox.checked;
if (isRegex) {
search = new RegExp(search);
if (this.current && this.current.search === search && this.current.isRegex === isRegex) {
const next = (this.current.index + 1) % this.current.indices.length;
this.current.index = next;
this.provider.jumpToNearest([this.current.indices[next]]);
return false;
} else {
const indices = this.provider.searchAndJump(isRegex ? new RegExp(search) : search, this.column, true);
if (indices) {
this.current = {
search,
isRegex,
indices,
index: 0,
};
return indices.length > 1;
} else {
this.current = null;
}
}
this.provider.searchAndJump(search, this.column);

return true;
}

Expand Down

0 comments on commit 4a98e5b

Please sign in to comment.