From c5c5681cfd2f2865a9f4ca1b85e09771106f30d3 Mon Sep 17 00:00:00 2001 From: fullex <106392080+0xfullex@users.noreply.github.com> Date: Thu, 29 May 2025 10:10:55 +0800 Subject: [PATCH] feat(SelectionAssistant): support Shift+Click & enhance Ctrl key mode (#6566) * feat: add filter mode and list functionality to selection assistant - Introduced new filter mode options (default, whitelist, blacklist) for the selection assistant. - Added methods to set and get filter mode and filter list in ConfigManager. - Enhanced SelectionService to manage filter mode and list, affecting text selection processing. - Updated UI components to allow users to configure filter settings. - Localized new filter settings in multiple languages. * feat: support Shift+Click & enhance Ctrl key method * fix: remove comments --- package.json | 2 +- src/main/services/SelectionService.ts | 35 ++++++++++++++++++--------- yarn.lock | 10 ++++---- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 2a83a93284..bb003f29ad 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,7 @@ "officeparser": "^4.1.1", "os-proxy-config": "^1.1.2", "proxy-agent": "^6.5.0", - "selection-hook": "^0.9.15", + "selection-hook": "^0.9.17", "tar": "^7.4.3", "turndown": "^7.2.0", "webdav": "^5.8.0", diff --git a/src/main/services/SelectionService.ts b/src/main/services/SelectionService.ts index 2a33a05376..ebe17b3e0e 100644 --- a/src/main/services/SelectionService.ts +++ b/src/main/services/SelectionService.ts @@ -143,7 +143,7 @@ export class SelectionService { this.filterMode = configManager.getSelectionAssistantFilterMode() this.filterList = configManager.getSelectionAssistantFilterList() - this.setHookClipboardMode(this.filterMode, this.filterList) + this.setHookGlobalFilterMode(this.filterMode, this.filterList) configManager.subscribe(ConfigKeys.SelectionAssistantTriggerMode, (triggerMode: string) => { this.triggerMode = triggerMode @@ -156,21 +156,21 @@ export class SelectionService { configManager.subscribe(ConfigKeys.SelectionAssistantFilterMode, (filterMode: string) => { this.filterMode = filterMode - this.setHookClipboardMode(this.filterMode, this.filterList) + this.setHookGlobalFilterMode(this.filterMode, this.filterList) }) configManager.subscribe(ConfigKeys.SelectionAssistantFilterList, (filterList: string[]) => { this.filterList = filterList - this.setHookClipboardMode(this.filterMode, this.filterList) + this.setHookGlobalFilterMode(this.filterMode, this.filterList) }) } /** - * Set the clipboard mode for the selection-hook + * Set the global filter mode for the selection-hook * @param mode - The mode to set, either 'default', 'whitelist', or 'blacklist' * @param list - An array of strings representing the list of items to include or exclude */ - private setHookClipboardMode(mode: string, list: string[]) { + private setHookGlobalFilterMode(mode: string, list: string[]) { if (!this.selectionHook) return const modeMap = { @@ -178,8 +178,8 @@ export class SelectionService { whitelist: 1, blacklist: 2 } - if (!this.selectionHook.setClipboardMode(modeMap[mode], list)) { - this.logError(new Error('Failed to set selection-hook clipboard mode')) + if (!this.selectionHook.setGlobalFilterMode(modeMap[mode], list)) { + this.logError(new Error('Failed to set selection-hook global filter mode')) } } @@ -194,8 +194,6 @@ export class SelectionService { } try { - //init basic configs - this.initConfig() //make sure the toolbar window is ready this.createToolbarWindow() // Initialize preloaded windows @@ -209,6 +207,9 @@ export class SelectionService { // Start the hook if (this.selectionHook.start({ debug: isDev })) { + //init basic configs + this.initConfig() + //init trigger mode configs this.processTriggerMode() @@ -613,12 +614,16 @@ export class SelectionService { selectionData.endBottom ) + // Note: shift key + mouse click == DoubleClick + + //double click to select a word if (isDoubleClick && isSameLine) { refOrientation = 'bottomMiddle' refPoint = { x: selectionData.mousePosEnd.x, y: selectionData.endBottom.y + 4 } break } + // below: isDoubleClick || isSameLine if (isSameLine) { const direction = selectionData.mousePosEnd.x - selectionData.mousePosStart.x @@ -632,6 +637,7 @@ export class SelectionService { break } + // below: !isDoubleClick && !isSameLine const direction = selectionData.mousePosEnd.y - selectionData.mousePosStart.y if (direction > 0) { @@ -732,6 +738,10 @@ export class SelectionService { if (this.triggerMode === 'ctrlkey' && this.isCtrlkey(data.vkCode)) { return } + //dont hide toolbar when shiftkey is pressed, because it's used for selection + if (this.isShiftkey(data.vkCode)) { + return + } this.hideToolbar() } @@ -788,6 +798,11 @@ export class SelectionService { return vkCode === 162 || vkCode === 163 } + //check if the key is shift key + private isShiftkey(vkCode: number) { + return vkCode === 160 || vkCode === 161 + } + /** * Create a preloaded action window for quick response * Action windows handle specific operations on selected text @@ -958,7 +973,6 @@ export class SelectionService { this.isCtrlkeyListenerActive = false } - this.selectionHook!.enableClipboard() this.selectionHook!.setSelectionPassiveMode(false) } else if (this.triggerMode === 'ctrlkey') { if (!this.isCtrlkeyListenerActive) { @@ -968,7 +982,6 @@ export class SelectionService { this.isCtrlkeyListenerActive = true } - this.selectionHook!.disableClipboard() this.selectionHook!.setSelectionPassiveMode(true) } } diff --git a/yarn.lock b/yarn.lock index be491bd4b9..c71023fb1a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5666,7 +5666,7 @@ __metadata: remark-math: "npm:^6.0.0" rollup-plugin-visualizer: "npm:^5.12.0" sass: "npm:^1.88.0" - selection-hook: "npm:^0.9.15" + selection-hook: "npm:^0.9.17" shiki: "npm:^3.4.2" string-width: "npm:^7.2.0" styled-components: "npm:^6.1.11" @@ -16376,13 +16376,13 @@ __metadata: languageName: node linkType: hard -"selection-hook@npm:^0.9.15": - version: 0.9.15 - resolution: "selection-hook@npm:0.9.15" +"selection-hook@npm:^0.9.17": + version: 0.9.17 + resolution: "selection-hook@npm:0.9.17" dependencies: node-gyp: "npm:latest" node-gyp-build: "npm:^4.8.4" - checksum: 10c0/e1b5fdfc9135f9d1a37a99d46414517ff63c11250e7510005adbaa144f35f8ae72871effd26245abb657cd85a8539d9b25df92c5850f46a0aa95224e17d7f0a1 + checksum: 10c0/1dbd1234e06bb6fe53d2fe9cb0de9736177af0b66e824c4dd3fb2d90413c30235c999826893f999b1d8809bfaf33968108c609b5ac80ea4b946f6a4891f5d1b9 languageName: node linkType: hard