@@ -2,6 +2,7 @@ import log from 'electron-log';
22import fs from 'fs-extra' ;
33import path from 'path' ;
44import upath from 'upath' ;
5+ import prompt from 'electron-prompt' ;
56import { servicesHelpers } from '../helpers/services' ;
67
78const {
@@ -43,6 +44,75 @@ function saveFileToFolder(browserWin) {
4344 shell . showItemInFolder ( targetFile ) ;
4445}
4546
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+
46116function buildBrowserTemplate ( browserWin ) {
47117 // console.log('menu/buildDefaultTemplate');
48118 // console.log(loginLabel);
@@ -66,6 +136,11 @@ function buildBrowserTemplate(browserWin) {
66136 {
67137 role : 'copy'
68138 } ,
139+ { type : 'separator' } ,
140+ findInPageMenuItem ( browserWin , lastSearchText ) ,
141+ findNextInPageMenuItem ( browserWin , lastSearchText , true ) ,
142+ findNextInPageMenuItem ( browserWin , lastSearchText , false ) ,
143+ { type : 'separator' } ,
69144 {
70145 role : 'selectAll'
71146 }
@@ -139,9 +214,25 @@ function openFileInChromeBrowser(
139214 }
140215 } ) ;
141216 }
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 ) ;
145236 } ) ;
146237 return browserWin ;
147238}
0 commit comments