26 lines
819 B
TypeScript
26 lines
819 B
TypeScript
import { EditorState } from "@codemirror/state";
|
|
import { SearchQuery } from "@codemirror/search";
|
|
|
|
/** Commands for the shared document search controls. */
|
|
export interface FindTarget {
|
|
query: SearchQuery;
|
|
readOnly: boolean;
|
|
canSelectAll?: boolean;
|
|
setQuery(query: SearchQuery): void;
|
|
next(backward?: boolean): void;
|
|
selectAll(): void;
|
|
replace(all?: boolean): void;
|
|
close(): void;
|
|
keydown?(event: KeyboardEvent): boolean;
|
|
}
|
|
|
|
export function documentMatches(source: string, query: SearchQuery) {
|
|
const state = EditorState.create({ doc: source });
|
|
const matches: { from: number; to: number }[] = [];
|
|
if (query.valid) {
|
|
const cursor = query.getCursor(state);
|
|
for (let match = cursor.next(); !match.done; match = cursor.next()) matches.push(match.value);
|
|
}
|
|
return { state, matches };
|
|
}
|