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

Add findSelected and findSelectedBackwards commands #4502

Merged
merged 2 commits into from
Jan 12, 2025
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
12 changes: 11 additions & 1 deletion background_scripts/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,13 @@ const Commands = {
"Vomnibar.activateEditUrl",
"Vomnibar.activateEditUrlInNewTab",
],
findCommands: ["enterFindMode", "performFind", "performBackwardsFind"],
findCommands: [
"enterFindMode",
"performFind",
"performBackwardsFind",
"findSelected",
"findSelectedBackwards",
],
historyNavigation: ["goBack", "goForward"],
tabManipulation: [
"createTab",
Expand Down Expand Up @@ -423,6 +429,8 @@ const defaultKeyMappings = {
"/": "enterFindMode",
"n": "performFind",
"N": "performBackwardsFind",
"*": "findSelected",
"#": "findSelectedBackwards",

// Vomnibar
"o": "Vomnibar.activate",
Expand Down Expand Up @@ -509,6 +517,8 @@ const commandDescriptions = {
enterFindMode: ["Enter find mode", { noRepeat: true }],
performFind: ["Cycle forward to the next find match"],
performBackwardsFind: ["Cycle backward to the previous find match"],
findSelected: ["Find the selected text"],
findSelectedBackwards: ["Find the selected text backwards"],

goPrevious: ["Follow the link labeled previous or <", { noRepeat: true }],
goNext: ["Follow the link labeled next or >", { noRepeat: true }],
Expand Down
3 changes: 3 additions & 0 deletions content_scripts/mode_find.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ class FindMode extends Mode {

static updateQuery(query) {
let pattern;
if (!this.query) {
this.query = {};
}
this.query.rawQuery = query;
// the query can be treated differently (e.g. as a plain string versus regex depending on the
// presence of escape sequences. '\' is the escape character and needs to be escaped itself to
Expand Down
16 changes: 16 additions & 0 deletions content_scripts/mode_normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,22 @@ const NormalModeCommands = {
}
},

findSelected() {
let selection = window.getSelection().toString();
if (!selection) return;
FindMode.updateQuery(selection);
FindMode.saveQuery();
return FindMode.findNext(false);
},

findSelectedBackwards() {
let selection = window.getSelection().toString();
if (!selection) return;
FindMode.updateQuery(selection);
FindMode.saveQuery();
return FindMode.findNext(true);
Comment on lines +225 to +229
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since both the forward and backward implementation use the same code except for the forward/backward boolean, it might make sense to use a helper function that accepts a boolean. It just depends what @philc prefers for maintainability.

},

// Misc.
mainFrame() {
return focusThisFrame({ highlight: true, forceFocusThisFrame: true });
Expand Down