@@ -2,6 +2,7 @@ import log from 'electron-log';
2
2
import fs from 'fs-extra' ;
3
3
import path from 'path' ;
4
4
import upath from 'upath' ;
5
+ import prompt from 'electron-prompt' ;
5
6
import { servicesHelpers } from '../helpers/services' ;
6
7
7
8
const {
@@ -43,6 +44,75 @@ function saveFileToFolder(browserWin) {
43
44
shell . showItemInFolder ( targetFile ) ;
44
45
}
45
46
47
+ let lastSearchText ;
48
+ let lastSearchIndex ;
49
+ let lastSearchResults ;
50
+ const findNextLabel = 'Find Next' ;
51
+ const findNextShortcut = 'F3' ;
52
+ const findBackShortcut = 'Shift+F3' ;
53
+
54
+ function findNextInPageMenuItem ( browserWin , searchText , forward = true ) {
55
+ const findNextInfo = lastSearchResults
56
+ ? ` (${ lastSearchIndex } /${ lastSearchResults } )`
57
+ : '' ;
58
+ return findInPageMenuItem (
59
+ browserWin ,
60
+ searchText ,
61
+ false ,
62
+ `${ forward ? findNextLabel : 'Find Backward' } ${ findNextInfo } ` ,
63
+ forward ? findNextShortcut : findBackShortcut ,
64
+ forward
65
+ ) ;
66
+ }
67
+
68
+ function findInPageMenuItem (
69
+ browserWin ,
70
+ selectionText ,
71
+ openPrompt = true ,
72
+ label = 'Find' ,
73
+ accelerator = 'CmdOrCtrl+F' ,
74
+ forward = true
75
+ ) {
76
+ return {
77
+ label,
78
+ accelerator,
79
+ click : async ( ) => {
80
+ const { webContents } = browserWin ;
81
+ webContents . unselect ( ) ;
82
+ if ( openPrompt ) {
83
+ try {
84
+ const promptResult = await prompt (
85
+ {
86
+ title : label ,
87
+ label : '' ,
88
+ resizable : true ,
89
+ value : selectionText ,
90
+ inputAttrs : {
91
+ type : 'text'
92
+ }
93
+ } ,
94
+ browserWin
95
+ ) ;
96
+ if ( ! promptResult || promptResult . length === 0 ) {
97
+ return ;
98
+ }
99
+ lastSearchText = promptResult ;
100
+ lastSearchIndex = undefined ;
101
+ lastSearchResults = undefined ;
102
+ webContents . findInPage ( promptResult ) ;
103
+ } catch ( error ) {
104
+ log . error ( error ) ;
105
+ }
106
+ } else {
107
+ if ( ! selectionText || selectionText . length === 0 ) {
108
+ return ;
109
+ }
110
+ webContents . findInPage ( selectionText , { forward } ) ;
111
+ }
112
+ }
113
+ } ;
114
+ }
115
+
46
116
function buildBrowserTemplate ( browserWin ) {
47
117
// console.log('menu/buildDefaultTemplate');
48
118
// console.log(loginLabel);
@@ -66,6 +136,11 @@ function buildBrowserTemplate(browserWin) {
66
136
{
67
137
role : 'copy'
68
138
} ,
139
+ { type : 'separator' } ,
140
+ findInPageMenuItem ( browserWin , lastSearchText ) ,
141
+ findNextInPageMenuItem ( browserWin , lastSearchText , true ) ,
142
+ findNextInPageMenuItem ( browserWin , lastSearchText , false ) ,
143
+ { type : 'separator' } ,
69
144
{
70
145
role : 'selectAll'
71
146
}
@@ -139,9 +214,25 @@ function openFileInChromeBrowser(
139
214
}
140
215
} ) ;
141
216
}
142
-
143
- browserWin . webContents . on ( 'context-menu' , ( ) => {
144
- Menu . buildFromTemplate ( [ { role : 'copy' } ] ) . popup ( browserWin ) ;
217
+ browserWin . webContents . on ( 'found-in-page' , ( event , result ) => {
218
+ const { activeMatchOrdinal, matches } = result ;
219
+ lastSearchResults = matches ;
220
+ lastSearchIndex = activeMatchOrdinal ;
221
+ buildBrowserMenu ( browserWin ) ;
222
+ } ) ;
223
+ browserWin . webContents . on ( 'context-menu' , ( event , params ) => {
224
+ const { selectionText } = params ;
225
+ const searchText =
226
+ selectionText && selectionText . length > 0
227
+ ? selectionText
228
+ : lastSearchText ;
229
+ Menu . buildFromTemplate ( [
230
+ { role : 'copy' } ,
231
+ { type : 'separator' } ,
232
+ findInPageMenuItem ( browserWin , selectionText ) ,
233
+ findNextInPageMenuItem ( browserWin , searchText ) ,
234
+ findNextInPageMenuItem ( browserWin , searchText , false )
235
+ ] ) . popup ( browserWin ) ;
145
236
} ) ;
146
237
return browserWin ;
147
238
}
0 commit comments