1
0
mirror of https://github.com/keeweb/keeweb.git synced 2024-06-21 07:06:39 +02:00
keeweb/app/scripts/comp/browser/shortcuts.ts

161 lines
5.2 KiB
TypeScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import { Launcher } from 'comp/launcher';
import { Keys } from 'const/keys';
2021-05-24 10:01:25 +02:00
import { AppSettingsFieldName, AppSettings } from 'models/app-settings';
2019-09-15 14:16:32 +02:00
import { Features } from 'util/features';
import { Locale } from 'util/locale';
2021-05-19 14:07:29 +02:00
let allowedKeys: Map<number, string>;
2021-05-19 14:07:29 +02:00
function getAllowedKeys(): Map<number, string> {
if (!allowedKeys) {
2021-05-19 14:07:29 +02:00
allowedKeys = new Map<number, string>();
for (const [name, code] of Object.entries(Keys)) {
2021-05-19 14:07:29 +02:00
if (typeof code === 'string') {
continue;
}
const keyName = name.replace('DOM_VK_', '');
if (/^([0-9A-Z]|F\d{1,2})$/.test(keyName)) {
2021-05-19 14:07:29 +02:00
allowedKeys.set(code, keyName);
}
}
}
return allowedKeys;
}
2021-05-19 14:07:29 +02:00
const AllGlobalShortcuts = {
copyPassword: { mac: 'Ctrl+Alt+C', all: 'Shift+Alt+C' },
copyUser: { mac: 'Ctrl+Alt+B', all: 'Shift+Alt+B' },
copyUrl: { mac: 'Ctrl+Alt+U', all: 'Shift+Alt+U' },
2021-05-19 14:07:29 +02:00
copyOtp: undefined,
autoType: { mac: 'Ctrl+Alt+T', all: 'Shift+Alt+T' },
2021-05-19 14:07:29 +02:00
restoreApp: undefined
};
2021-07-04 17:12:28 +02:00
export type GlobalShortcutType = keyof typeof AllGlobalShortcuts;
const GlobalShortcutAppSettingsFields: Record<GlobalShortcutType, AppSettingsFieldName> = {
2021-05-19 14:07:29 +02:00
copyPassword: 'globalShortcutCopyPassword',
copyUser: 'globalShortcutCopyUser',
copyUrl: 'globalShortcutCopyUrl',
copyOtp: 'globalShortcutCopyOtp',
autoType: 'globalShortcutAutoType',
restoreApp: 'globalShortcutRestoreApp'
};
const Shortcuts = {
2021-05-19 14:07:29 +02:00
keyEventToShortcut(event: KeyboardEvent): { valid: boolean; value: string } {
const modifiers = [];
if (event.ctrlKey) {
modifiers.push('Ctrl');
}
if (event.altKey) {
modifiers.push('Alt');
}
if (event.shiftKey) {
modifiers.push('Shift');
}
2019-09-15 08:11:11 +02:00
if (Features.isMac && event.metaKey) {
modifiers.push('Meta');
}
2021-05-19 14:07:29 +02:00
const keyName = getAllowedKeys().get(event.which);
return {
value: modifiers.join('+') + '+' + (keyName || '…'),
valid: modifiers.length > 0 && !!keyName
};
},
2021-05-19 14:07:29 +02:00
presentShortcut(shortcutValue: string | undefined, formatting?: boolean): string {
if (!shortcutValue) {
return '-';
}
return shortcutValue
.split(/\+/g)
2020-06-01 16:53:51 +02:00
.map((part) => {
switch (part) {
case 'Ctrl':
return this.ctrlShortcutSymbol(formatting);
case 'Alt':
return this.altShortcutSymbol(formatting);
case 'Shift':
return this.shiftShortcutSymbol(formatting);
case 'Meta':
return this.actionShortcutSymbol(formatting);
default:
return part;
}
})
.join('');
},
2021-05-19 14:07:29 +02:00
actionShortcutSymbol(formatting?: boolean): string {
return Features.isMac ? '⌘' : this.formatShortcut(Locale.ctrlKey, formatting);
},
2021-05-19 14:07:29 +02:00
altShortcutSymbol(formatting?: boolean): string {
return Features.isMac ? '⌥' : this.formatShortcut(Locale.altKey, formatting);
},
2021-05-19 14:07:29 +02:00
shiftShortcutSymbol(formatting?: boolean): string {
return Features.isMac ? '⇧' : this.formatShortcut(Locale.shiftKey, formatting);
},
2021-05-19 14:07:29 +02:00
ctrlShortcutSymbol(formatting?: boolean): string {
return Features.isMac ? '⌃' : this.formatShortcut(Locale.ctrlKey, formatting);
},
2021-05-19 14:07:29 +02:00
formatShortcut(shortcut: string, formatting?: boolean): string {
2020-05-09 16:35:11 +02:00
return formatting ? `${shortcut} + ` : `${shortcut}+`;
},
2021-05-19 14:07:29 +02:00
2021-07-04 17:12:28 +02:00
globalShortcutText(type: GlobalShortcutType, formatting?: boolean): string {
return this.presentShortcut(this.globalShortcut(type), formatting);
},
2021-05-19 14:07:29 +02:00
2021-07-04 17:12:28 +02:00
globalShortcut(type: GlobalShortcutType): string | undefined {
2021-05-19 14:07:29 +02:00
const setting = GlobalShortcutAppSettingsFields[type];
2021-05-24 10:01:25 +02:00
const appSettingsShortcut = AppSettings[setting];
2021-05-19 14:07:29 +02:00
if (typeof appSettingsShortcut === 'string') {
return appSettingsShortcut;
}
2021-05-19 14:07:29 +02:00
const globalShortcut = AllGlobalShortcuts[type];
if (globalShortcut) {
2019-09-15 08:11:11 +02:00
if (Features.isMac && globalShortcut.mac) {
return globalShortcut.mac;
}
return globalShortcut.all;
}
return undefined;
},
2021-05-19 14:07:29 +02:00
2021-07-04 17:12:28 +02:00
setGlobalShortcut(type: GlobalShortcutType, value: string | null): void {
2021-05-19 14:07:29 +02:00
if (!AllGlobalShortcuts[type]) {
throw new Error('Bad shortcut: ' + type);
}
2021-05-19 14:07:29 +02:00
const setting = GlobalShortcutAppSettingsFields[type];
if (value) {
2021-05-24 10:01:25 +02:00
AppSettings.set(setting, value);
} else {
2021-05-24 10:01:25 +02:00
AppSettings.delete(setting);
}
2021-05-19 14:07:29 +02:00
Launcher?.ipcRenderer.invoke('set-global-shortcuts', { [setting]: value });
},
2021-05-19 14:07:29 +02:00
screenshotToClipboardShortcut(): string {
2019-09-15 08:11:11 +02:00
if (Features.isiOS) {
return 'Sleep+Home';
}
2019-09-15 08:11:11 +02:00
if (Features.isMobile) {
return '';
}
2019-09-15 08:11:11 +02:00
if (Features.isMac) {
return 'Command-Shift-Control-4';
}
2019-09-15 08:11:11 +02:00
if (Features.isWindows) {
return 'Alt+PrintScreen';
}
return '';
}
};
2019-09-15 14:16:32 +02:00
export { Shortcuts };