electron-findbar 0.5.0 → 0.6.4

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 CHANGED
@@ -47,21 +47,21 @@ All public methods are documented with JSDoc and can be referenced during import
47
47
  To import the Findbar class:
48
48
 
49
49
  ```js
50
- const { Findbar } = require('electron-findbar');
50
+ const { Findbar } = require('electron-findbar')
51
51
  ```
52
52
 
53
53
  ### Creating the Findbar Instance
54
54
 
55
- You can pass a `BrowserWindow` instance as a single parameter to use it as the parent window. The `BrowserWindow.WebContents` will be used as the searchable content:
55
+ You can pass a `BrowserWindow` instance as a single parameter to use it as the parent window. The `BrowserWindow.WebContents` will be used as the findable content:
56
56
 
57
57
  ```js
58
- const findbar = new Findbar(browserWindow);
58
+ const findbar = new Findbar(browserWindow)
59
59
  ```
60
60
 
61
- Alternatively, you can provide a custom `WebContents` as the second parameter. In this case, the first parameter can be any `BaseWindow`, and the second parameter will be the searchable content:
61
+ Alternatively, you can provide a custom `WebContents` as the second parameter. In this case, the first parameter can be any `BaseWindow`, and the second parameter will be the findable content:
62
62
 
63
63
  ```js
64
- const findbar = new Findbar(baseWindow, customWebContents);
64
+ const findbar = new Findbar(baseWindow, customWebContents)
65
65
  ```
66
66
 
67
67
  ### Configuring the Findbar
@@ -69,16 +69,24 @@ const findbar = new Findbar(baseWindow, customWebContents);
69
69
  You can customize the Findbar window options using the `setWindowOptions` method:
70
70
 
71
71
  ```js
72
- findbar.setWindowOptions({ movable: true, resizable: true, alwaysOnTop: true });
72
+ findbar.setWindowOptions({ movable: true, resizable: true, alwaysOnTop: true })
73
73
  ```
74
74
 
75
- The findbar has a default position handler which moves the findbar to the top-right corner. To change the position handler, use the `setPositionHandler`. The position handler is called when the parent window moves or resizes and provides both the parent and findbar bounds as parameters.
75
+ To handle the Findbar window directly after it is opened, use the `setWindowHandler` method:
76
+
77
+ ```js
78
+ findbar.setWindowHandler(win => {
79
+ win.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true })
80
+ });
81
+ ```
82
+
83
+ The findbar has a default position handler which moves the findbar to the top-right corner. To change the position handler, use the `setPositionHandler` method. The position handler is called when the parent window moves or resizes and provides both the parent and findbar bounds as parameters.
76
84
 
77
85
  ```js
78
86
  findbar.setPositionHandler((parentBounds, findbarBounds) => ({
79
87
  x: parentBounds.x + parentBounds.width - findbarBounds.width - 20,
80
88
  y: parentBounds.y - ((findbarBounds.height / 4) | 0)
81
- }));
89
+ }))
82
90
  ```
83
91
 
84
92
  ### Opening the Findbar
@@ -86,17 +94,9 @@ findbar.setPositionHandler((parentBounds, findbarBounds) => ({
86
94
  The Findbar is a child window of the `BaseWindow` passed during construction. To open it use:
87
95
 
88
96
  ```js
89
- findbar.open();
90
- ```
91
-
92
- On MacOS, the findbar will follow the relative movement of the parent window by default and there is no way to change it. On Windows and Linux, this behavior is not default and is simulated by using the `move` event of the parent and can be disabled by using:
93
-
94
- ```js
95
- findbar.followParentWindow(false)
97
+ findbar.open()
96
98
  ```
97
99
 
98
- > Enabled by default.
99
-
100
100
  ### Finding Text in the Page
101
101
 
102
102
  Once open, the Findbar appears by default in the top-right corner of the parent window and can be used without additional coding. Alternatively, you can use the following methods to trigger `findInPage` and navigate through matches in the main process:
@@ -105,28 +105,28 @@ Once open, the Findbar appears by default in the top-right corner of the parent
105
105
  /**
106
106
  * Retrieve the last queried value.
107
107
  */
108
- getLastValue();
108
+ getLastValue()
109
109
 
110
110
  /**
111
111
  * Initiate a request to find all matches for the specified text on the page.
112
112
  * @param {string} text - The text to search for.
113
113
  */
114
- startFind(text);
114
+ startFind(text)
115
115
 
116
116
  /**
117
117
  * Select the previous match, if available.
118
118
  */
119
- findPrevious();
119
+ findPrevious()
120
120
 
121
121
  /**
122
122
  * Select the next match, if available.
123
123
  */
124
- findNext();
124
+ findNext()
125
125
 
126
126
  /**
127
127
  * Stop the find request.
128
128
  */
129
- stopFind();
129
+ stopFind()
130
130
  ```
131
131
 
132
132
  ### Closing the Findbar
@@ -134,7 +134,7 @@ stopFind();
134
134
  When the Findbar is closed, its window is destroyed to free memory resources. Use the following method to close the Findbar:
135
135
 
136
136
  ```js
137
- findbar.close();
137
+ findbar.close()
138
138
  ```
139
139
 
140
140
  A new internal window will be created the next time the `open` method is called. There is no need to instantiate another Findbar for the same parent window.
@@ -144,32 +144,36 @@ A new internal window will be created the next time the `open` method is called.
144
144
  Here is a quick example demonstrating how to use the `electron-findbar`:
145
145
 
146
146
  ```js
147
- const { app, BrowserWindow } = require('electron');
148
- const { Findbar } = require('electron-findbar');
147
+ const { app, BrowserWindow } = require('electron')
148
+ const { Findbar } = require('electron-findbar')
149
149
 
150
150
  app.whenReady().then(() => {
151
- const window = new BrowserWindow();
152
- window.loadURL('https://github.com/ECRomaneli/electron-findbar');
151
+ const window = new BrowserWindow()
152
+ window.loadURL('https://github.com/ECRomaneli/electron-findbar')
153
153
 
154
154
  // Create and configure the Findbar object
155
- const findbar = new Findbar(window);
155
+ const findbar = new Findbar(window)
156
156
 
157
157
  // [OPTIONAL] Customize window options
158
- findbar.setWindowOptions({ movable: true, resizable: true });
158
+ findbar.setWindowOptions({ movable: true, resizable: true })
159
159
 
160
160
  // [OPTIONAL] Handle the window object when the Findbar is opened
161
161
  findbar.setWindowHandler(win => {
162
- win.webContents.openDevTools();
163
- });
162
+ win.webContents.openDevTools()
163
+ })
164
164
 
165
165
  // Open the Findbar
166
- findbar.open();
167
- });
166
+ findbar.open()
167
+ })
168
168
  ```
169
169
 
170
170
  ## IPC Events
171
171
 
172
- As an alternative, the findbar can be controlled using IPC events in the `renderer` process of the `WebContents` provided during the findbar construction. Example:
172
+ As an alternative, the findbar can be controlled using IPC events in the `renderer` process of the `WebContents` provided during the findbar construction.
173
+
174
+ ### ipcRenderer
175
+
176
+ If the `contextIsolation` is enabled, the `electron-findbar/remote` will not be available, but the IPC events can be used directly through the preload script:
173
177
 
174
178
  ```js
175
179
  const $remote = (ipc => ({
@@ -185,9 +189,16 @@ $remote.open()
185
189
  $remote.inputChange('findIt')
186
190
  ```
187
191
 
188
- ## Notes
192
+ ### Remote module
189
193
 
190
- There are some intentional differences from the Chrome findbar, such as the horizontal margins of the divider and the input text, which has been replaced by a search input to include a clear button (the "x" on the right side).
194
+ With the `contextIsolation` disabled, the remote library is available to use:
195
+
196
+ ```js
197
+ const FindbarRemote = require('electron-findbar/remote')
198
+
199
+ FindbarRemote.open()
200
+ FindbarRemote.inputChange('findIt')
201
+ ```
191
202
 
192
203
  ## Author
193
204
 
package/index.js CHANGED
@@ -40,6 +40,7 @@ class Findbar {
40
40
  constructor (parent, webContents) {
41
41
  this.#parent = parent
42
42
  this.#findableContents = webContents ?? parent.webContents
43
+ this.#findableContents._findbar = this
43
44
 
44
45
  if (!this.#findableContents) {
45
46
  throw new Error('There are no searchable web contents.')
@@ -56,7 +57,6 @@ class Findbar {
56
57
  }
57
58
  this.#window = new BrowserWindow(Findbar.#mergeStandardOptions(this.#customOptions, this.#parent))
58
59
  this.#window.webContents._findbar = this
59
- this.#findableContents._findbar = this
60
60
 
61
61
  this.#registerListeners()
62
62
 
@@ -90,7 +90,7 @@ class Findbar {
90
90
  startFind(text, skipInputUpdate) {
91
91
  skipInputUpdate || this.#window?.webContents.send('electron-findbar/text-change', text)
92
92
  if (this.#lastText = text) {
93
- this.#findableContents.findInPage(this.#lastText, { findNext: true })
93
+ this.isOpen() && this.#findableContents.findInPage(this.#lastText, { findNext: true })
94
94
  } else {
95
95
  this.stopFind()
96
96
  }
@@ -101,7 +101,7 @@ class Findbar {
101
101
  */
102
102
  findPrevious() {
103
103
  this.#matches.active === 1 && (this.#fixMove = false)
104
- this.#findableContents.findInPage(this.#lastText, { forward: false })
104
+ this.isOpen() && this.#findableContents.findInPage(this.#lastText, { forward: false })
105
105
  }
106
106
 
107
107
  /**
@@ -109,7 +109,7 @@ class Findbar {
109
109
  */
110
110
  findNext() {
111
111
  this.#matches.active === this.#matches.total && (this.#fixMove = true)
112
- this.#findableContents.findInPage(this.#lastText, { forward: true })
112
+ this.isOpen() && this.#findableContents.findInPage(this.#lastText, { forward: true })
113
113
  }
114
114
 
115
115
  /**
@@ -121,13 +121,29 @@ class Findbar {
121
121
  }
122
122
 
123
123
  /**
124
- * Return if the findbar is open or not.
124
+ * Whether the findbar is opened.
125
125
  * @returns {boolean} True, if the findbar is open. Otherwise, false.
126
126
  */
127
127
  isOpen() {
128
128
  return !!this.#window
129
129
  }
130
130
 
131
+ /**
132
+ * Whether the findbar is focused. If the findbar is closed, false will be returned.
133
+ * @returns {boolean} True, if the findbar is focused. Otherwise, false.
134
+ */
135
+ isFocused() {
136
+ return !!this.#window?.isFocused()
137
+ }
138
+
139
+ /**
140
+ * Whether the findbar is visible to the user in the foreground of the app. If the findbar is closed, false will be returned.
141
+ * @returns {boolean} True, if the findbar is visible. Otherwise, false.
142
+ */
143
+ isVisible() {
144
+ return !!this.#window?.isVisible()
145
+ }
146
+
131
147
  /**
132
148
  * Provides a customized set of options to findbar window before open. Note
133
149
  * that the options below are necessary for the correct functioning and cannot
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electron-findbar",
3
- "version": "0.5.0",
3
+ "version": "0.6.4",
4
4
  "description": "Chrome-like findbar for your Electron app.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -13,10 +13,15 @@
13
13
  },
14
14
  "keywords": [
15
15
  "findbar",
16
+ "find-bar",
17
+ "find bar",
16
18
  "electron-findbar",
19
+ "electron-find-bar",
17
20
  "electron",
18
21
  "chrome-findbar",
19
- "search"
22
+ "chrome-find-bar",
23
+ "search",
24
+ "search-bar"
20
25
  ],
21
26
  "author": "ECRomaneli",
22
27
  "license": "MIT",
package/remote.js ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Remote IPC events to control the findbar through the renderer.
3
+ */
4
+ const Remote = (ipc => ({
5
+ /**
6
+ * Get last queried text.
7
+ * @returns {string}
8
+ */
9
+ getLastText: async () => ipc.invoke('electron-findbar/last-text'),
10
+
11
+ /**
12
+ * Change the input value and find it.
13
+ * @param {string} text
14
+ */
15
+ inputChange: (text) => { ipc.send('electron-findbar/input-change', text) },
16
+ previous: () => { ipc.send('electron-findbar/previous') },
17
+ next: () => { ipc.send('electron-findbar/next') },
18
+ open: () => { ipc.send('electron-findbar/open') },
19
+ close: () => { ipc.send('electron-findbar/close') },
20
+ })) (require('electron').ipcRenderer)
21
+
22
+ module.exports = Remote
package/test/sample.html CHANGED
@@ -22,14 +22,7 @@ Suspendisse vel euismod ante. Nunc sagittis quam ut gravida pulvinar. Sed at sem
22
22
  Aliquam ut pellentesque tellus, quis vulputate nisl. Phasellus vitae blandit nunc, eu sodales velit. Duis enim tellus, faucibus id arcu vitae, consectetur tempus mauris. Praesent commodo commodo dolor non malesuada. Donec sed dolor eget arcu tincidunt efficitur sit amet vel nisi. Sed consectetur tincidunt molestie. Nam at magna a odio rhoncus convallis id eu nunc. Nam luctus ut leo et viverra. Proin tempor libero vitae arcu laoreet, sed pharetra sapien rutrum. Nam nunc orci, aliquet sit amet dignissim nec, aliquet quis quam. Nulla facilisi.
23
23
  </span>
24
24
  <script>
25
- const $remote = (ipc => ({
26
- getLastText: async () => ipc.invoke('electron-findbar/last-text'),
27
- inputChange: (value) => { ipc.send('electron-findbar/input-change', value) },
28
- previous: () => { ipc.send('electron-findbar/previous') },
29
- next: () => { ipc.send('electron-findbar/next') },
30
- open: () => { ipc.send('electron-findbar/open') },
31
- close: () => { ipc.send('electron-findbar/close') },
32
- })) (require('electron').ipcRenderer)
25
+ const $remote = require('../remote')
33
26
  </script>
34
27
  </body>
35
28
  </html>
package/test/sample.js CHANGED
@@ -22,7 +22,6 @@ function setupFindbar(window) {
22
22
  const findbar = new Findbar(window)
23
23
  findbar.setWindowOptions({ movable: true, resizable: true })
24
24
  findbar.setWindowHandler(win => { /* handle the findbar window */ })
25
- findbar.open()
26
25
  return findbar
27
26
  }
28
27