Skip to content

Commit

Permalink
feat: change shortcuts without reloading tabs (AutomaApp#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kholid060 committed Oct 6, 2022
1 parent 9f60bb2 commit 236fc2c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/background/BackgroundEventsListeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ class BackgroundEventsListeners {
isFirstTime: true,
visitWebTriggers: [],
});
await browser.tabs.create({
active: true,
await browser.windows.create({
type: 'popup',
state: 'maximized',
url: browser.runtime.getURL('newtab.html#/welcome'),
});

Expand Down
9 changes: 6 additions & 3 deletions src/content/commandPalette/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,10 @@ function onKeydown(event) {
return;
}
if (state.shortcutKeys.length < 1) return;
const shortcuts = window._automaShortcuts;
if (shortcuts.length < 1) return;
const automaShortcut = state.shortcutKeys.every((shortcutKey) => {
const automaShortcut = shortcuts.every((shortcutKey) => {
if (shortcutKey === 'mod') return ctrlKey || metaKey;
if (shortcutKey === 'shift') return shiftKey;
if (shortcutKey === 'option') return altKey;
Expand All @@ -260,6 +261,7 @@ function onKeydown(event) {
if (automaShortcut) {
event.preventDefault();
state.active = true;
state.shortcutKeys = shortcuts;
}
}
function onInputKeydown(event) {
Expand Down Expand Up @@ -385,10 +387,11 @@ onMounted(() => {
browser.storage.local.get('automaShortcut').then(({ automaShortcut }) => {
if (Array.isArray(automaShortcut) && automaShortcut.length < 1) return;
let keys = ['mod', 'shift', 'a'];
let keys = ['mod', 'shift', 'e'];
if (automaShortcut) keys = automaShortcut.split('+');
state.shortcutKeys = keys;
window._automaShortcuts = keys;
});
window.addEventListener('keydown', onKeydown);
Expand Down
56 changes: 42 additions & 14 deletions src/content/services/shortcutListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,28 @@ function workflowShortcutsListener(findWorkflow, shortcutsObj) {
return true;
});
}
async function getWorkflows() {
const { workflows, workflowHosts } = await browser.storage.local.get([
'workflows',
'workflowHosts',
]);
const localWorkflows = Array.isArray(workflows)
? workflows
: Object.values(workflows);

return {
local: localWorkflows,
hosted: Object.values(workflowHosts || {}),
};
}

export default async function () {
try {
const { shortcuts, workflows, workflowHosts } =
await browser.storage.local.get([
'shortcuts',
'workflows',
'workflowHosts',
]);
const workflowsArr = Array.isArray(workflows)
? workflows
: Object.values(workflows);
const storage = await browser.storage.local.get('shortcuts');
let workflows = await getWorkflows();

const findWorkflow = (id, publicId = false) => {
let workflow = workflowsArr.find((item) => {
let workflow = workflows.local.find((item) => {
if (publicId) {
return item.settings.publicId === id;
}
Expand All @@ -85,18 +93,38 @@ export default async function () {
});

if (!workflow) {
workflow = Object.values(workflowHosts || {}).find(
({ hostId }) => hostId === id
);
workflow = workflows.hosted.find(({ hostId }) => hostId === id);

if (workflow) workflow.id = workflow.hostId;
}

return workflow;
};

browser.storage.onChanged.addListener(({ automaShortcut, shortcuts }) => {
if (automaShortcut) {
if (
Array.isArray(automaShortcut.newValue) &&
automaShortcut.newValue.length < 1
) {
window._automaShortcuts = [];
} else {
const automaShortcutArr = automaShortcut.newValue.split('+');

window._automaShortcuts = automaShortcutArr;
}
}
if (shortcuts) {
Mousetrap.reset();
getWorkflows().then((updatedWorkflows) => {
workflows = updatedWorkflows;
workflowShortcutsListener(findWorkflow, shortcuts.newValue || {});
});
}
});

automaCustomEventListener(findWorkflow);
workflowShortcutsListener(findWorkflow, shortcuts || {});
workflowShortcutsListener(findWorkflow, storage.shortcuts || {});
} catch (error) {
console.error(error);
}
Expand Down
4 changes: 2 additions & 2 deletions src/manifest.chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"commands": {
"open-dashboard": {
"suggested_key": {
"default": "Ctrl+Shift+A",
"mac": "MacCtrl+Shift+A"
"default": "Alt+A",
"mac": "Alt+A"
},
"description": "Open the Automa dashboard"
}
Expand Down
2 changes: 1 addition & 1 deletion src/newtab/pages/settings/SettingsShortcuts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const { t } = useI18n();
const toast = useToast();
const shortcuts = ref(mapShortcuts);
const automaShortcut = ref(getReadableShortcut('mod+shift+a'));
const automaShortcut = ref(getReadableShortcut('mod+shift+e'));
const recording = reactive({
id: '',
keys: [],
Expand Down

0 comments on commit 236fc2c

Please sign in to comment.