From 5c56ede7223b631450a75001dbad5f8742d3a1d3 Mon Sep 17 00:00:00 2001 From: Van Pham <64197333+Van-QA@users.noreply.github.com> Date: Tue, 14 May 2024 11:14:33 +0700 Subject: [PATCH] Revert "chore: remove nutjs (#2860)" This reverts commit ce2d8e540582a73423efed02dbe484e213cdf0fe. --- electron/package.json | 3 ++- electron/utils/selectedText.ts | 27 +++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/electron/package.json b/electron/package.json index fc81fcb197..20675a37bb 100644 --- a/electron/package.json +++ b/electron/package.json @@ -103,7 +103,8 @@ "pacote": "^17.0.4", "request": "^2.88.2", "request-progress": "^3.0.0", - "ulidx": "^2.3.0" + "ulidx": "^2.3.0", + "@nut-tree/nut-js": "^4.0.0" }, "devDependencies": { "@electron/notarize": "^2.1.0", diff --git a/electron/utils/selectedText.ts b/electron/utils/selectedText.ts index f76146d133..25d31fab59 100644 --- a/electron/utils/selectedText.ts +++ b/electron/utils/selectedText.ts @@ -1,24 +1,23 @@ import { clipboard, globalShortcut } from 'electron' +import { keyboard, Key } from '@nut-tree/nut-js' /** * Gets selected text by synthesizing the keyboard shortcut * "CommandOrControl+c" then reading text from the clipboard */ export const getSelectedText = async () => { - // TODO: Implement this function - // const currentClipboardContent = clipboard.readText() // preserve clipboard content - // clipboard.clear() - // const hotkeys: Key[] = [ - // process.platform === 'darwin' ? Key.LeftCmd : Key.LeftControl, - // Key.C, - // ] - // await keyboard.pressKey(...hotkeys) - // await keyboard.releaseKey(...hotkeys) - // await new Promise((resolve) => setTimeout(resolve, 200)) // add a delay before checking clipboard - // const selectedText = clipboard.readText() - // clipboard.writeText(currentClipboardContent) - // return selectedText - return '' + const currentClipboardContent = clipboard.readText() // preserve clipboard content + clipboard.clear() + const hotkeys: Key[] = [ + process.platform === 'darwin' ? Key.LeftCmd : Key.LeftControl, + Key.C, + ] + await keyboard.pressKey(...hotkeys) + await keyboard.releaseKey(...hotkeys) + await new Promise((resolve) => setTimeout(resolve, 200)) // add a delay before checking clipboard + const selectedText = clipboard.readText() + clipboard.writeText(currentClipboardContent) + return selectedText } /**