Skip to content

Commit

Permalink
Improve navigation experience for select next
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong committed Jan 18, 2025
1 parent 7a881f6 commit e900acf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
6 changes: 3 additions & 3 deletions CoreEditor/src/bridge/web/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface WebModuleSearch extends WebModule {
replaceNext(): void;
replaceAll(): void;
selectAllOccurrences(): void;
selectNextOccurrence(): void;
selectNextOccurrence(): boolean;
numberOfMatches(): CodeGen_Int;
}

Expand Down Expand Up @@ -78,8 +78,8 @@ export class WebModuleSearchImpl implements WebModuleSearch {
selectAllOccurrences();
}

selectNextOccurrence(): void {
selectNextOccurrence();
selectNextOccurrence(): boolean {
return selectNextOccurrence();
}

numberOfMatches(): CodeGen_Int {
Expand Down
13 changes: 12 additions & 1 deletion CoreEditor/src/modules/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,18 @@ export function selectAllOccurrences() {
}

export function selectNextOccurrence() {
selectNextOccurrenceCommand(window.editor);
const editor = window.editor;
const oldRanges = editor.state.selection.ranges;
const foundNext = selectNextOccurrenceCommand(editor);

const newRanges = editor.state.selection.ranges;
newRanges.forEach(range => {
if (!oldRanges.includes(range)) {
scrollIntoView(range, 'center');
}
});

return foundNext;
}

export function numberOfMatches(): CodeGen_Int {
Expand Down
10 changes: 8 additions & 2 deletions MarkEditKit/Sources/Bridge/Web/Generated/WebBridgeSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,14 @@ public final class WebBridgeSearch {
webView?.invoke(path: "webModules.search.selectAllOccurrences", completion: completion)
}

public func selectNextOccurrence(completion: ((Result<Void, WKWebView.InvokeError>) -> Void)? = nil) {
webView?.invoke(path: "webModules.search.selectNextOccurrence", completion: completion)
public func selectNextOccurrence() async throws -> Bool {
return try await withCheckedThrowingContinuation { continuation in
webView?.invoke(path: "webModules.search.selectNextOccurrence") { result in
Task { @MainActor in
continuation.resume(with: result)
}
}
}
}

public func numberOfMatches() async throws -> Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ extension EditorViewController {
}

func selectNextOccurrence() {
bridge.search.selectNextOccurrence()
Task {
if let foundNext = try? await bridge.search.selectNextOccurrence(), !foundNext {
NSSound.beep()
}
}
}

func performSearchOperation(_ operation: SearchOperation) {
Expand Down

0 comments on commit e900acf

Please sign in to comment.