Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: search next with pressing enter #631

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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