electron-findbar 3.0.0 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -5
- package/index.js +21 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
## Installation
|
|
15
15
|
|
|
16
|
-
Install the `electron-findbar` package via npm:
|
|
16
|
+
Install the `electron-findbar` package via [npm](https://www.npmjs.com/package/electron-findbar):
|
|
17
17
|
|
|
18
18
|
```sh
|
|
19
19
|
npm install electron-findbar
|
|
@@ -21,7 +21,7 @@ npm install electron-findbar
|
|
|
21
21
|
|
|
22
22
|
## Overview
|
|
23
23
|
|
|
24
|
-
The `electron-findbar`
|
|
24
|
+
The `electron-findbar` package creates a `BrowserWindow`-based component designed to emulate the Chrome findbar layout, leveraging the `webContents.findInPage` method to navigate through matches. Inter-process communication (IPC) is used for interaction between the `main` and `renderer` processes.
|
|
25
25
|
|
|
26
26
|
### Memory Usage
|
|
27
27
|
|
|
@@ -55,14 +55,14 @@ Alternatively, you can provide a custom `WebContents` as the second parameter. I
|
|
|
55
55
|
const findbar = Findbar.from(baseWindow, webContents)
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
It is also possible to create a findbar providing only the web contents. The BaseWindow.getAllWindows() will be used to query for the parent window:
|
|
59
59
|
|
|
60
60
|
```js
|
|
61
61
|
// Create or retrieve the findbar associated to the webContents.
|
|
62
62
|
const findbar = Findbar.from(webContents)
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
-
**Note:** The findbar is ALWAYS linked to the webContents not the window. The parent is only the window to connect the events and stay on top. If the `.from(webContents)` is used to retrieve an existing findbar previously created with a parent, the findbar will stay connected to the parent.
|
|
65
|
+
**Note:** The findbar is ALWAYS linked to the webContents, not the window. The parent is only the window to connect the events and stay on top. If the `.from(webContents)` is used to retrieve an existing findbar previously created with a parent, the findbar will stay connected to the parent.
|
|
66
66
|
|
|
67
67
|
#### Retrieve if exists
|
|
68
68
|
|
|
@@ -305,10 +305,11 @@ If the `contextIsolation` is enabled, the `electron-findbar/remote` will not be
|
|
|
305
305
|
```js
|
|
306
306
|
const $remote = (ipc => ({
|
|
307
307
|
getLastState: async () => ipc.invoke('electron-findbar/last-state'),
|
|
308
|
-
inputChange: (
|
|
308
|
+
inputChange: (text) => { ipc.send('electron-findbar/input-change', text) },
|
|
309
309
|
matchCase: (value) => { ipc.send('electron-findbar/match-case', value) },
|
|
310
310
|
previous: () => { ipc.send('electron-findbar/previous') },
|
|
311
311
|
next: () => { ipc.send('electron-findbar/next') },
|
|
312
|
+
open: () => { ipc.send('electron-findbar/open') },
|
|
312
313
|
close: () => { ipc.send('electron-findbar/close') }
|
|
313
314
|
})) (require('electron').ipcRenderer)
|
|
314
315
|
|
|
@@ -327,6 +328,49 @@ FindbarRemote.open()
|
|
|
327
328
|
FindbarRemote.inputChange('findIt')
|
|
328
329
|
```
|
|
329
330
|
|
|
331
|
+
## Changing the Parent Window
|
|
332
|
+
|
|
333
|
+
There are scenarios where you might need to change the parent window.
|
|
334
|
+
|
|
335
|
+
### Using updateParentWindow
|
|
336
|
+
|
|
337
|
+
The `updateParentWindow` method allows you to change the parent window while preserving the findbar instance and its state:
|
|
338
|
+
|
|
339
|
+
```javascript
|
|
340
|
+
// Create a findbar for the initial window
|
|
341
|
+
const findbar = Findbar.from(oldWindow, webContents)
|
|
342
|
+
|
|
343
|
+
// Later, when you need to change the parent:
|
|
344
|
+
findbar.updateParentWindow(newWindow)
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
This approach keeps the same findbar instance connected to the same webContents, but changes which window it's attached to. The findbar will close immediately.
|
|
348
|
+
|
|
349
|
+
### Using detach
|
|
350
|
+
|
|
351
|
+
Alternatively, the `detach` method disconnects a findbar instance from its webContents, allowing you to create a new instance in the next `Findbar.from` call:
|
|
352
|
+
|
|
353
|
+
```javascript
|
|
354
|
+
// Get the existing findbar
|
|
355
|
+
const oldFindbar = Findbar.fromIfExists(webContents)
|
|
356
|
+
|
|
357
|
+
// Detach it to free the association
|
|
358
|
+
if (oldFindbar) {
|
|
359
|
+
oldFindbar.detach()
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Now create a new findbar with a different parent
|
|
363
|
+
const newFindbar = Findbar.from([newWindow, ]webContents)
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
This approach is useful when you want to completely reset the findbar's configuration or when moving between very different window configurations.
|
|
367
|
+
|
|
368
|
+
### Important Considerations
|
|
369
|
+
|
|
370
|
+
- If the findbar is currently open when you change the parent window, it will automatically close.
|
|
371
|
+
- Window options and handlers will be preserved when using `updateParentWindow`.
|
|
372
|
+
- After calling `detach`, the old findbar instance can no longer be used.
|
|
373
|
+
|
|
330
374
|
## Author
|
|
331
375
|
|
|
332
376
|
Created by [Emerson Capuchi Romaneli](https://github.com/ECRomaneli) (@ECRomaneli).
|
package/index.js
CHANGED
|
@@ -14,7 +14,7 @@ class Findbar {
|
|
|
14
14
|
#findableContents
|
|
15
15
|
|
|
16
16
|
/** @type { { active: number, total: number } } */
|
|
17
|
-
#matches
|
|
17
|
+
#matches = { active: 0, total: 0 }
|
|
18
18
|
|
|
19
19
|
/** @type {(findbarWindow: BrowserWindow) => void} */
|
|
20
20
|
#windowHandler
|
|
@@ -74,6 +74,8 @@ class Findbar {
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
this.#findableContents._findbar = this
|
|
77
|
+
|
|
78
|
+
this.#findableContents.once('destroyed', () => { this.detach() })
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
/**
|
|
@@ -116,10 +118,15 @@ class Findbar {
|
|
|
116
118
|
if (this.#window) { this.#window.webContents._findbar = void 0 }
|
|
117
119
|
}
|
|
118
120
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
121
|
+
/**
|
|
122
|
+
* Update the parent window of the findbar.
|
|
123
|
+
* @param {BaseWindow} [newParent] - The new parent window. If not provided, the parent will be set to the window containing the web contents.
|
|
124
|
+
* @returns {void}
|
|
125
|
+
*/
|
|
126
|
+
updateParentWindow(newParent) {
|
|
127
|
+
if (this.#parent === newParent) { return }
|
|
128
|
+
this.close()
|
|
129
|
+
this.#parent = newParent ?? Findbar.#getBaseWindowFromWebContents(this.#findableContents)
|
|
123
130
|
}
|
|
124
131
|
|
|
125
132
|
/**
|
|
@@ -164,6 +171,7 @@ class Findbar {
|
|
|
164
171
|
* @returns {void}
|
|
165
172
|
*/
|
|
166
173
|
findPrevious() {
|
|
174
|
+
if (this.#matches.total < 2) { return }
|
|
167
175
|
this.#matches.active === 1 && (this.#fixMove = false)
|
|
168
176
|
this.isOpen() && this.#findInContent({ forward: false })
|
|
169
177
|
}
|
|
@@ -173,6 +181,7 @@ class Findbar {
|
|
|
173
181
|
* @returns {void}
|
|
174
182
|
*/
|
|
175
183
|
findNext() {
|
|
184
|
+
if (this.#matches.total < 2) { return }
|
|
176
185
|
this.#matches.active === this.#matches.total && (this.#fixMove = true)
|
|
177
186
|
this.isOpen() && this.#findInContent({ forward: true })
|
|
178
187
|
}
|
|
@@ -310,7 +319,7 @@ class Findbar {
|
|
|
310
319
|
this.#parent.prependListener('move', boundsHandler)
|
|
311
320
|
}
|
|
312
321
|
|
|
313
|
-
this.#window.once('
|
|
322
|
+
this.#window.once('closed', () => {
|
|
314
323
|
if (this.#parent && !this.#parent.isDestroyed()) {
|
|
315
324
|
this.#parent.off('show', showCascade)
|
|
316
325
|
this.#parent.off('hide', hideCascade)
|
|
@@ -339,7 +348,8 @@ class Findbar {
|
|
|
339
348
|
this.#fixMove = null
|
|
340
349
|
}
|
|
341
350
|
|
|
342
|
-
this.#matches =
|
|
351
|
+
this.#matches.active = active
|
|
352
|
+
this.#matches.total = total
|
|
343
353
|
|
|
344
354
|
this.#window.webContents.send('electron-findbar/matches', this.#matches)
|
|
345
355
|
}
|
|
@@ -358,16 +368,16 @@ class Findbar {
|
|
|
358
368
|
* @returns {WebContents | undefined} The web contents if any.
|
|
359
369
|
*/
|
|
360
370
|
static #retrieveWebContents(window) {
|
|
361
|
-
return window.webContents ?? window.contentView?.children[0]
|
|
371
|
+
return window.webContents ?? window.contentView?.children[0]?.webContents
|
|
362
372
|
}
|
|
363
373
|
|
|
364
374
|
/**
|
|
365
375
|
* Get the parent window from web contents.
|
|
366
|
-
* @param {WebContents}
|
|
376
|
+
* @param {WebContents} w
|
|
367
377
|
* @returns {BaseWindow | undefined} Parent window if any.
|
|
368
378
|
*/
|
|
369
|
-
static #getBaseWindowFromWebContents(
|
|
370
|
-
return BaseWindow.getAllWindows().find(win => win.webContents ===
|
|
379
|
+
static #getBaseWindowFromWebContents(w) {
|
|
380
|
+
return BaseWindow.getAllWindows().find(win => win.webContents === w || win.contentView.children.some(child => child.webContents === w))
|
|
371
381
|
}
|
|
372
382
|
|
|
373
383
|
/**
|