Skip to content

Commit

Permalink
refactor: use options argument
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Apr 17, 2023
1 parent fb0d32a commit ff41c53
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
14 changes: 7 additions & 7 deletions docs/api/web-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -1428,11 +1428,11 @@ Scrolls to the top of the current `webContents`.

Scrolls to the bottom of the current `webContents`.

#### `contents.adjustSelection(start, end, showMenu)`
#### `contents.adjustSelection(options)`

* `start` Number - Index at which to begin the adjustment.
* `end` Number - Index at which to end the adjustment.
* `showMenu` Boolean - Whether to show the selection menu.
* `options` Object
* `start` Number - Index at which to begin the adjustment.
* `end` Number - Index at which to end the adjustment.

Adjusts the current text selection starting and ending points in the focused frame by the given amounts. A negative amount moves the selection towards the beginning of the document, and a positive amount moves the selection towards the end of the document.

Expand All @@ -1443,14 +1443,14 @@ const win = new BrowserWindow()

// Adjusts the beginning of the selection 1 letter forward,
// and the end of the selection 5 letters forward.
win.webContents.adjustSelection(1, 5, false)
win.webContents.adjustSelection({ start: 1, end: 5 })

// Adjusts the beginning of the selection 2 letters forward,
// and the end of the selection 3 letters backward.
win.webContents.adjustSelection(2, -3, false)
win.webContents.adjustSelection({ start: 2, end: -3 })
```

For a call of `win.webContents.adjustSelection(1, 5, false)`
For a call of `win.webContents.adjustSelection({ start: 1, end: 5 })`

Before:

Expand Down
10 changes: 5 additions & 5 deletions docs/api/webview-tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,15 @@ Scrolls to the top of the current `<webview>`.

Scrolls to the bottom of the current `<webview>`.

#### `<webview>.adjustSelection(start, end, showMenu)`
#### `<webview>.adjustSelection(options)`

* `start` Number - Index at which to begin the adjustment.
* `end` Number - Index at which to end the adjustment.
* `showMenu` Boolean - Whether to show the selection menu.
* `options` Object
* `start` Number - Index at which to begin the adjustment.
* `end` Number - Index at which to end the adjustment.

Adjusts the current text selection starting and ending points in the focused frame by the given amounts. A negative amount moves the selection towards the beginning of the document, and a positive amount moves the selection towards the end of the document.

See [`webContents.adjustSelection`](web-contents.md#contentsadjustselectionstart-end-showmenu) for
See [`webContents.adjustSelection`](web-contents.md#contentsadjustselectionoptions) for
examples.

### `<webview>.replace(text)`
Expand Down
19 changes: 14 additions & 5 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3127,11 +3127,20 @@ void WebContents::ScrollToBottomOfDocument() {
web_contents()->ScrollToBottomOfDocument();
}

void WebContents::AdjustSelectionByCharacterOffset(int start_adjust,
int end_adjust,
bool show_selection_menu) {
web_contents()->AdjustSelectionByCharacterOffset(start_adjust, end_adjust,
show_selection_menu);
void WebContents::AdjustSelectionByCharacterOffset(gin::Arguments* args) {
int start_adjust = 0;
int end_adjust = 0;

gin_helper::Dictionary dict;
if (args->GetNext(&dict)) {
dict.Get("start", &start_adjust);
dict.Get("matchCase", &end_adjust);
}

// The selection menu is a Chrome-specific piece of UI.
// TODO(codebytere): maybe surface as an event in the future?
web_contents()->AdjustSelectionByCharacterOffset(
start_adjust, end_adjust, false /* show_selection_menu */);
}

void WebContents::Replace(const std::u16string& word) {
Expand Down
4 changes: 1 addition & 3 deletions shell/browser/api/electron_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,7 @@ class WebContents : public ExclusiveAccessContext,
void Unselect();
void ScrollToTopOfDocument();
void ScrollToBottomOfDocument();
void AdjustSelectionByCharacterOffset(int start_adjust,
int end_adjust,
bool show_selection_menu);
void AdjustSelectionByCharacterOffset(gin::Arguments* args);
void Replace(const std::u16string& word);
void ReplaceMisspelling(const std::u16string& word);
uint32_t FindInPage(gin::Arguments* args);
Expand Down

0 comments on commit ff41c53

Please sign in to comment.