Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix[hotkeys]: remove scope stack (#36)

Peter Chinman2da0822c1ac1
diff --git a/js/app/packages/app/component/command/useCommandItems.ts b/js/app/packages/app/component/command/useCommandItems.ts
index 9f31067dac..2ff22da0a4 100644
--- a/js/app/packages/app/component/command/useCommandItems.ts
+++ b/js/app/packages/app/component/command/useCommandItems.ts
@@ -1,6 +1,6 @@
import { useChannelsContext } from '@core/component/ChannelsProvider';
import { getActiveCommandsFromScope } from '@core/hotkey/getCommands';
-import { activeScopeStack } from '@core/hotkey/state';
+import { activeScope } from '@core/hotkey/state';
import { useEmailContacts } from '@core/user';
import { mapFromListsByKey } from '@core/util/compareUtils';
import type { Channel } from '@service-comms/generated/models/channel';
@@ -31,8 +31,7 @@ export function useCommandItems() {
const channelsContext = useChannelsContext();
const channels = () => channelsContext.channels();
const contactItems = useEmailContacts();
- const activeScope = activeScopeStack().at(-1) ?? 'global';
- const activeCommands = getActiveCommandsFromScope(activeScope, {
+ const activeCommands = getActiveCommandsFromScope(activeScope(), {
sortByScopeLevel: false,
hideShadowedCommands: false,
hideCommandsWithoutHotkeys: false,
diff --git a/js/app/packages/app/component/dock/Dock.tsx b/js/app/packages/app/component/dock/Dock.tsx
index 631a1dd3cd..d06a413bc4 100644
--- a/js/app/packages/app/component/dock/Dock.tsx
+++ b/js/app/packages/app/component/dock/Dock.tsx
@@ -17,7 +17,7 @@ import {
useSettingsState,
} from '@core/constant/SettingsState';
import { runCommand } from '@core/hotkey/hotkeys';
-import { activeScopeStack, hotkeyScopeTree } from '@core/hotkey/state';
+import { activeScope, hotkeyScopeTree } from '@core/hotkey/state';
import { TOKENS } from '@core/hotkey/tokens';
import { isMobileWidth } from '@core/mobile/mobileWidth';
import { isRightPanelOpen, useToggleRightPanel } from '@core/signal/layout';
@@ -49,9 +49,9 @@ export function Dock() {
});
// This method of opening the correct help drawer is disgusting, but it works and doesn't require changing anything else.
const activeSoupDrawerCommand = () => {
- const activeScope = activeScopeStack().at(-1);
- if (!activeScope) return undefined;
- let activeScopeNode = hotkeyScopeTree.get(activeScope);
+ const currentActiveScope = activeScope();
+ if (!currentActiveScope) return undefined;
+ let activeScopeNode = hotkeyScopeTree.get(currentActiveScope);
if (!activeScopeNode) return undefined;
if (activeScopeNode?.type !== 'dom') return;
const dom = activeScopeNode.element;
@@ -101,17 +101,13 @@ export function Dock() {
}}
onClick={() => setDebugOpen((p) => !p)}
>
- <h3 class="right-full bottom-[14ch] absolute bg-ink px-1 py-px whitespace-nowrap -rotate-90 origin-bottom-right">
- Active Scopes
+ <h3 class="right-full bottom-[7ch] absolute bg-ink px-1 py-px whitespace-nowrap -rotate-90 origin-bottom-right">
+ Debug
</h3>
<ol class="flex flex-col bg-ink/50 px-[1ch] py-[1lh] list-decimal list-inside">
- <For each={activeScopeStack()}>
- {(scope) => (
<li class="whitespace-nowrap list-item">
- <span class="bg-ink px-1 py-px">{scope}</span>
+ <span class="bg-ink px-1 py-px">{activeScope()}</span>
</li>
- )}
- </For>
</ol>
</div>
</Show>
diff --git a/js/app/packages/core/hotkey/getCommands.ts b/js/app/packages/core/hotkey/getCommands.ts
index 421b08d917..739f874b73 100644
--- a/js/app/packages/core/hotkey/getCommands.ts
+++ b/js/app/packages/core/hotkey/getCommands.ts
@@ -1,7 +1,7 @@
import { isEditableInput } from '@core/util/isEditableInput';
import { activeElement } from 'app/signal/focus';
import { createMemo } from 'solid-js';
-import { activeScopeStack, hotkeyScopeTree } from './state';
+import { activeScope, hotkeyScopeTree } from './state';
import type { HotkeyCommand, ValidHotkey } from './types';
type sortAndFilterOptions = {
@@ -21,7 +21,7 @@ export function useActiveCommands(
scope?: string
) {
return createMemo(() => {
- const scopeId = scope ?? activeScopeStack().at(-1) ?? '';
+ const scopeId = scope ?? activeScope() ?? '';
return getActiveCommandsFromScope(scopeId, displayOptions ?? {});
});
}
diff --git a/js/app/packages/core/hotkey/hotkeys.ts b/js/app/packages/core/hotkey/hotkeys.ts
index a2b31b95ce..a083fb3cfe 100644
--- a/js/app/packages/core/hotkey/hotkeys.ts
+++ b/js/app/packages/core/hotkey/hotkeys.ts
@@ -14,7 +14,7 @@ import {
MODIFIER_LIST_NON_MAC,
} from './constants';
import {
- activeScopeStack,
+ activeScope,
hotkeyScopeTree,
hotkeysAwaitingKeyUp,
hotkeyTokenMap,
@@ -22,6 +22,7 @@ import {
setExecutedTokens,
setHotkeyTokenMap,
setPressedKeys,
+ setActiveScope,
} from './state';
import type { HotkeyToken } from './tokens';
import {
@@ -40,7 +41,6 @@ import {
getScopeId,
normalizeEventKeyPress,
prettyPrintHotkeyString,
- pushActiveScope,
registerScope,
removeScope,
} from './utils';
@@ -305,7 +305,7 @@ export function useHotkeyDOMScope(
const scopeNode = hotkeyScopeTree.get(scopeId);
if (scopeNode && !scopeActivatedByFocusIn) {
- pushActiveScope(scopeId);
+ setActiveScope(scopeId);
if (e.currentTarget instanceof Element) {
repairScopeBranch(scopeNode, e.currentTarget);
}
@@ -384,7 +384,7 @@ export function attachGlobalDOMScope(el: Element) {
});
if (!scopeActivatedByFocusIn) {
- pushActiveScope('global');
+ setActiveScope('global');
}
scopeActivatedByFocusIn = false;
};
@@ -485,7 +485,7 @@ export function useHotKeyRoot() {
const checkHotKeys = (e: KeyboardEvent) => {
const scopeTree = hotkeyScopeTree;
- const currentScopeId = activeScopeStack().at(-1);
+ const currentScopeId = activeScope();
const currentPressedKeys = pressedKeys();
if (currentPressedKeys.size === 0) {
return;
@@ -540,7 +540,7 @@ export function useHotKeyRoot() {
const newScope = hotkeyScopeTree.get(command.activateCommandScopeId);
if (newScope) {
setPressedKeys(new Set<string>());
- pushActiveScope(newScope.id);
+ setActiveScope(newScope.id);
if (!transitionOrCommandFound) {
setExecutedTokens((prev) =>
prev.includes(command.hotkeyToken ?? '')
@@ -604,7 +604,7 @@ export function runCommand({
if (activateCommandScopeId) {
const newScope = hotkeyScopeTree.get(activateCommandScopeId);
if (newScope) {
- pushActiveScope(newScope.id);
+ setActiveScope(newScope.id);
}
}
}
@@ -618,7 +618,7 @@ export function runCommandByToken(token: HotkeyToken) {
if (command.activateCommandScopeId) {
const newScope = hotkeyScopeTree.get(command.activateCommandScopeId);
if (newScope) {
- pushActiveScope(newScope.id);
+ setActiveScope(newScope.id);
}
}
}
diff --git a/js/app/packages/core/hotkey/state.ts b/js/app/packages/core/hotkey/state.ts
index 1ff736475d..49d7c6b447 100644
--- a/js/app/packages/core/hotkey/state.ts
+++ b/js/app/packages/core/hotkey/state.ts
@@ -20,9 +20,7 @@ const initialTree = new Map<string, ScopeNode>([
export const hotkeyScopeTree = initialTree;
-export const [activeScopeStack, setActiveScopeStack] = createSignal<string[]>([
- 'global',
-]);
+export const [activeScope, setActiveScope] = createSignal<string>('global');
export const [pressedKeys, setPressedKeys] = createSignal<Set<string>>(
new Set()
diff --git a/js/app/packages/core/hotkey/utils.ts b/js/app/packages/core/hotkey/utils.ts
index 53308b070c..0fbee1c064 100644
--- a/js/app/packages/core/hotkey/utils.ts
+++ b/js/app/packages/core/hotkey/utils.ts
@@ -5,21 +5,13 @@ import {
shiftPunctuationReverseMap,
} from './constants';
import {
- activeScopeStack,
+ activeScope,
hotkeyScopeTree,
hotkeysAwaitingKeyUp,
- setActiveScopeStack,
+ setActiveScope,
} from './state';
import type { ScopeNode, ValidHotkey } from './types';
-// Pushes a scope to the active scope stack, if it is not already the active scope.
-export function pushActiveScope(scopeId: string) {
- setActiveScopeStack((prev) => {
- if (prev.at(-1) === scopeId) return prev;
- return [...prev, scopeId];
- });
-}
-
let scopeCounter = 0;
export function getScopeId(prefix: string = 'scope'): string {
const scopeId = `${prefix}_${scopeCounter++}`;
@@ -89,7 +81,7 @@ export function removeScope(scopeId: string) {
// if scope is in currently active scope branch, we want to "snip just above it", i.e. set active scope to closest DOM scope parent.
if (scope.type === 'dom') {
- let currentScope = scopeTree.get(activeScopeStack().at(-1) ?? '');
+ let currentScope = scopeTree.get(activeScope() ?? '');
while (currentScope) {
if (currentScope.id === scopeId) {
let parentScope = hotkeyScopeTree.get(currentScope.parentScopeId ?? '');
@@ -100,14 +92,14 @@ export function removeScope(scopeId: string) {
parentScope.element instanceof HTMLElement
) {
parentScope.element.focus();
- pushActiveScope(parentScope.id);
+ setActiveScope(parentScope.id);
foundDOMScopeParent = true;
break;
}
parentScope = hotkeyScopeTree.get(parentScope.parentScopeId ?? '');
}
if (!foundDOMScopeParent) {
- pushActiveScope('global');
+ setActiveScope('global');
}
break;
}
@@ -142,7 +134,7 @@ export function removeScope(scopeId: string) {
* Used to 'exit' a command scope.
*/
export function activateClosestDOMScope() {
- let currentScope = hotkeyScopeTree.get(activeScopeStack().at(-1) ?? '');
+ let currentScope = hotkeyScopeTree.get(activeScope() ?? '');
let activeScopeId = 'global';
// find the closest active DOM scope
while (currentScope) {
@@ -159,7 +151,7 @@ export function activateClosestDOMScope() {
currentScope = hotkeyScopeTree.get(currentScope.parentScopeId);
}
- pushActiveScope(activeScopeId);
+ setActiveScope(activeScopeId);
}
export function normalizeEventKeyPress(e: KeyboardEvent): string {