mirror of https://github.com/keeweb/keeweb.git
148 lines
4.5 KiB
JavaScript
148 lines
4.5 KiB
JavaScript
|
const FeatureDetector = require('../util/feature-detector');
|
||
|
const Keys = require('../const/keys');
|
||
|
const Format = require('../util/format');
|
||
|
const AppSettingsModel = require('../models/app-settings-model');
|
||
|
const Launcher = require('./launcher');
|
||
|
|
||
|
let allowedKeys;
|
||
|
|
||
|
function getAllowedKeys() {
|
||
|
if (!allowedKeys) {
|
||
|
allowedKeys = {};
|
||
|
for (const [name, code] of Object.entries(Keys)) {
|
||
|
const keyName = name.replace('DOM_VK_', '');
|
||
|
if (/^([0-9A-Z]|F\d{1,2})$/.test(keyName)) {
|
||
|
allowedKeys[code] = keyName;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return allowedKeys;
|
||
|
}
|
||
|
|
||
|
const globalShortcuts = {
|
||
|
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' },
|
||
|
autoType: { mac: 'Ctrl+Alt+T', all: 'Shift+Alt+T' }
|
||
|
};
|
||
|
|
||
|
const Shortcuts = {
|
||
|
keyEventToShortcut(event) {
|
||
|
const modifiers = [];
|
||
|
if (event.ctrlKey) {
|
||
|
modifiers.push('Ctrl');
|
||
|
}
|
||
|
if (event.altKey) {
|
||
|
modifiers.push('Alt');
|
||
|
}
|
||
|
if (event.shiftKey) {
|
||
|
modifiers.push('Shift');
|
||
|
}
|
||
|
if (FeatureDetector.isMac && event.metaKey) {
|
||
|
modifiers.push('Meta');
|
||
|
}
|
||
|
const keyName = getAllowedKeys()[event.which];
|
||
|
return {
|
||
|
value: modifiers.join('+') + '+' + (keyName || '…'),
|
||
|
valid: modifiers.length > 0 && !!keyName
|
||
|
};
|
||
|
},
|
||
|
presentShortcut(shortcutValue, formatting) {
|
||
|
return shortcutValue
|
||
|
.split(/\+/g)
|
||
|
.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('');
|
||
|
},
|
||
|
actionShortcutSymbol(formatting) {
|
||
|
return FeatureDetector.isMac
|
||
|
? '⌘'
|
||
|
: formatting
|
||
|
? '<span class="thin">ctrl + </span>'
|
||
|
: 'ctrl+';
|
||
|
},
|
||
|
altShortcutSymbol(formatting) {
|
||
|
return FeatureDetector.isMac
|
||
|
? '⌥'
|
||
|
: formatting
|
||
|
? '<span class="thin">alt + </span>'
|
||
|
: 'alt+';
|
||
|
},
|
||
|
shiftShortcutSymbol(formatting) {
|
||
|
return FeatureDetector.isMac
|
||
|
? '⇧'
|
||
|
: formatting
|
||
|
? '<span class="thin">shift + </span>'
|
||
|
: 'shift+';
|
||
|
},
|
||
|
ctrlShortcutSymbol(formatting) {
|
||
|
return FeatureDetector.isMac
|
||
|
? '⌃'
|
||
|
: formatting
|
||
|
? '<span class="thin">ctrl + </span>'
|
||
|
: 'ctrl+';
|
||
|
},
|
||
|
globalShortcutText(type, formatting) {
|
||
|
return this.presentShortcut(this.globalShortcut(type), formatting);
|
||
|
},
|
||
|
globalShortcut(type) {
|
||
|
const appSettingsShortcut = AppSettingsModel.instance.get(
|
||
|
this.globalShortcutAppSettingsKey(type)
|
||
|
);
|
||
|
if (appSettingsShortcut) {
|
||
|
return appSettingsShortcut;
|
||
|
}
|
||
|
const globalShortcut = globalShortcuts[type];
|
||
|
if (globalShortcut) {
|
||
|
if (FeatureDetector.isMac && globalShortcut.mac) {
|
||
|
return globalShortcut.mac;
|
||
|
}
|
||
|
return globalShortcut.all;
|
||
|
}
|
||
|
return undefined;
|
||
|
},
|
||
|
setGlobalShortcut(type, value) {
|
||
|
if (!globalShortcuts[type]) {
|
||
|
throw new Error('Bad shortcut: ' + type);
|
||
|
}
|
||
|
if (value) {
|
||
|
AppSettingsModel.instance.set(this.globalShortcutAppSettingsKey(type), value);
|
||
|
} else {
|
||
|
AppSettingsModel.instance.unset(this.globalShortcutAppSettingsKey(type));
|
||
|
}
|
||
|
Launcher.setGlobalShortcuts(AppSettingsModel.instance.attributes);
|
||
|
},
|
||
|
globalShortcutAppSettingsKey(type) {
|
||
|
return 'globalShortcut' + Format.capFirst(type);
|
||
|
},
|
||
|
screenshotToClipboardShortcut() {
|
||
|
if (FeatureDetector.isiOS) {
|
||
|
return 'Sleep+Home';
|
||
|
}
|
||
|
if (FeatureDetector.isMobile) {
|
||
|
return '';
|
||
|
}
|
||
|
if (FeatureDetector.isMac) {
|
||
|
return 'Command-Shift-Control-4';
|
||
|
}
|
||
|
if (FeatureDetector.isWindows) {
|
||
|
return 'Alt+PrintScreen';
|
||
|
}
|
||
|
return '';
|
||
|
}
|
||
|
};
|
||
|
|
||
|
module.exports = Shortcuts;
|