Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
import { useAppSelector } from 'app/store/storeHooks';
import { adjustPromptAttention } from 'common/util/promptAttention';
import { useRegisteredHotkeys } from 'features/system/components/HotkeysModal/useHotkeyData';
import { selectSystemPrefersNumericAttentionWeights } from 'features/system/store/systemSlice';
import type { RefObject } from 'react';
import { useCallback } from 'react';
type UsePromptAttentionHotkeysArgs = {
textareaRef: RefObject<HTMLTextAreaElement | null>;
onPromptChange: (prompt: string) => void;
};
/**
* Registers hotkeys for adjusting prompt attention weights (Ctrl+Up/Down).
* Handles both increment and decrement operations with proper selection restoration.
* Uses execCommand to integrate with browser's native undo stack.
*/
export const usePromptAttentionHotkeys = ({
textareaRef,
onPromptChange: _onPromptChange,
}: UsePromptAttentionHotkeysArgs) => {
const isPromptFocused = useCallback(() => document.activeElement === textareaRef.current, [textareaRef]);
const prefersNumericWeights = useAppSelector(selectSystemPrefersNumericAttentionWeights);
const handleAttentionAdjustment = useCallback(
(direction: 'increment' | 'decrement') => {
const textarea = textareaRef.current;
if (!textarea) {
return;
}
const result = adjustPromptAttention(
textarea.value,
textarea.selectionStart,
textarea.selectionEnd,
direction,
prefersNumericWeights
);
// Use execCommand to make the change undo-able by the browser.
// This triggers the textarea's native onChange, which syncs React state.
textarea.focus();
textarea.setSelectionRange(0, textarea.value.length);
document.execCommand('insertText', false, result.prompt);
// Restore the selection to cover the adjusted portion
textarea.setSelectionRange(result.selectionStart, result.selectionEnd);
},
[textareaRef, prefersNumericWeights]
);
useRegisteredHotkeys({
id: 'promptWeightUp',
category: 'app',
callback: (e) => {
if (isPromptFocused() && textareaRef.current) {
e.preventDefault();
handleAttentionAdjustment('increment');
}
},
options: { preventDefault: true, enableOnFormTags: ['TEXTAREA'] },
dependencies: [isPromptFocused, handleAttentionAdjustment, prefersNumericWeights],
});
useRegisteredHotkeys({
id: 'promptWeightDown',
category: 'app',
callback: (e) => {
if (isPromptFocused() && textareaRef.current) {
e.preventDefault();
handleAttentionAdjustment('decrement');
}
},
options: { preventDefault: true, enableOnFormTags: ['TEXTAREA'] },
dependencies: [isPromptFocused, handleAttentionAdjustment, prefersNumericWeights],
});
};