Skip to content

Commit

Permalink
feat: support key map
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Jun 22, 2024
1 parent 57c61b7 commit 143ba50
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"editor.renderWhitespace": "all",
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
}
}
407 changes: 280 additions & 127 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "google-translate",
"version": "4.1.31",
"version": "4.1.32",
"description": "google translate, work in any app",
"main": "./dist/index.js",
"scripts": {
Expand Down Expand Up @@ -44,7 +44,7 @@
"@typescript-eslint/eslint-plugin": "^4.25.0",
"@typescript-eslint/parser": "^4.25.0",
"cross-env": "^6.0.3",
"electron": "^7.1.2",
"electron": "^7.3.3",
"electron-builder": "^22.9.1",
"electron-rebuild": "^1.8.8",
"eslint": "^7.27.0",
Expand Down
2 changes: 1 addition & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ app.on('ready', () => {
if (window.isVisible()) {
window.fadeOut();
} else {
window.webContents.send(CUSTOM_EVENT.TRANSLATE, await getSelectionText());
window.webContents.send(CUSTOM_EVENT.TRANSLATE, await getSelectionText(settings.copyModifier));
window.fadeIn();
}
});
Expand Down
2 changes: 2 additions & 0 deletions src/main/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import { CUSTOM_EVENT } from '../consts';
export interface Settings {
enableUpdateCheck: 'on' | 'off';
translateShortcut: string;
copyModifier: 'auto' | 'control' | 'command' | 'alt';
}

const defaultSettings: Settings = {
enableUpdateCheck: 'off',
translateShortcut: 'CommandOrControl+Q',
copyModifier: 'auto',
};

const settingsPath = path.join(app.getPath('userData'), 'settings.json');
Expand Down
2 changes: 1 addition & 1 deletion src/main/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const settingsClickHandle = () => {
} else {
settingsWindow = new BrowserWindow({
width: 360,
height: 216,
height: 232,
webPreferences: {
nodeIntegration: true,
},
Expand Down
6 changes: 3 additions & 3 deletions src/main/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function delay(time: number) {
await new Promise(resolve => setTimeout(resolve, time));
}

export async function getSelectionText() {
export async function getSelectionText(modifier: string) {
function restoreContent() {
const oldString = clipboard.readHTML();
const oldImage = clipboard.readImage();
Expand All @@ -26,10 +26,10 @@ export async function getSelectionText() {
// window 平台下,ctrl+q 调用此函数时如果 ctrl 弹起的早则会输入 'c'
if (config.platform === 'win32') {
await delay(200);
robotjs.keyTap('c', 'control');
robotjs.keyTap('c', modifier === 'auto' ? 'control' : modifier);
await delay(100);
} else {
robotjs.keyTap('c', 'command');
robotjs.keyTap('c', modifier === 'auto' ? 'command' : modifier);
await delay(300);
}
const newString = clipboard.readText();
Expand Down
13 changes: 12 additions & 1 deletion src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Settings } from '../main/settings';
import { version } from '../../package.json';

const translateShortcutList = ['CommandOrControl+Q', 'CommandOrControl+T', 'CommandOrControl+F'];
const copyModifierList = ['auto', 'control', 'command', 'alt'];

document.title = 'Settings';

Expand Down Expand Up @@ -40,7 +41,7 @@ render(
check update
</label>
<fieldset>
<legend>Shortcut</legend>
<legend>Keys</legend>
<label>
translate selection text:
<select name="translateShortcut">
Expand All @@ -51,6 +52,16 @@ render(
)}
</select>
</label>
<label>
copy modifier:
<select name="copyModifier">
${copyModifierList.map(
key => html`
<option value=${key} ?selected=${settings.copyModifier === key}>${key}</option>
`,
)}
</select>
</label>
</fieldset>
<div class="action"><button type="submit">Save</button></div>
</form>
Expand Down

0 comments on commit 143ba50

Please sign in to comment.