refactor: find in page (#7086)

- refactor rxjs data flow
- use canvas text to mitigate searchable search box input text issue
This commit is contained in:
pengx17
2024-05-28 06:19:53 +00:00
parent bd9c929d05
commit 2ca77d9170
12 changed files with 276 additions and 192 deletions
@@ -1,17 +1,19 @@
import type { NamespaceHandlers } from '../type';
export const findInPageHandlers = {
findInPage: async (
event: Electron.IpcMainInvokeEvent,
text: string,
options?: Electron.FindInPageOptions
) => {
find: async (event, text: string, options?: Electron.FindInPageOptions) => {
const { promise, resolve } =
Promise.withResolvers<Electron.Result | null>();
const webContents = event.sender;
return webContents.findInPage(text, options);
let requestId: number = -1;
webContents.once('found-in-page', (_, result) => {
resolve(result.requestId === requestId ? result : null);
});
requestId = webContents.findInPage(text, options);
return promise;
},
stopFindInPage: async (
event: Electron.IpcMainInvokeEvent,
action: 'clearSelection' | 'keepSelection' | 'activateSelection'
) => {
clear: async event => {
const webContents = event.sender;
return webContents.stopFindInPage(action);
webContents.stopFindInPage('keepSelection');
},
};
} satisfies NamespaceHandlers;
@@ -1,7 +1,7 @@
import assert from 'node:assert';
import { join } from 'node:path';
import type { CookiesSetDetails } from 'electron';
import { type CookiesSetDetails } from 'electron';
import { BrowserWindow, nativeTheme } from 'electron';
import electronWindowState from 'electron-window-state';
@@ -169,16 +169,6 @@ async function createWindow(additionalArguments: string[]) {
uiSubjects.onFullScreen$.next(false);
});
browserWindow.webContents.on('found-in-page', (_event, result) => {
const { requestId, activeMatchOrdinal, matches, finalUpdate } = result;
browserWindow.webContents.send('found-in-page-result', {
requestId,
activeMatchOrdinal,
matches,
finalUpdate,
});
});
/**
* URL for main window.
*/