jan/electron/utils/selectedText.ts
NamH ce2d8e5405
chore: remove nutjs (#2860)
Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>
2024-05-02 21:02:05 +07:00

46 lines
1.5 KiB
TypeScript

import { clipboard, globalShortcut } from 'electron'
/**
* 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 ''
}
/**
* Registers a global shortcut of `accelerator`. The `callback` is called
* with the selected text when the registered shortcut is pressed by the user
*
* Returns `true` if the shortcut was registered successfully
*/
export const registerShortcut = (
accelerator: Electron.Accelerator,
callback: (selectedText: string) => void
) => {
return globalShortcut.register(accelerator, async () => {
callback(await getSelectedText())
})
}
/**
* Unregisters a global shortcut of `accelerator` and
* is equivalent to electron.globalShortcut.unregister
*/
export const unregisterShortcut = (accelerator: Electron.Accelerator) => {
globalShortcut.unregister(accelerator)
}