MCPcopy Create free account
hub / github.com/pgadmin-org/pgadmin4 / useKeyboardShortcuts

Function useKeyboardShortcuts

web/pgadmin/static/js/custom_hooks.js:184–223  ·  view source on GitHub ↗
(shortcuts, eleRef)

Source from the content-addressed store, hash-verified

182 ]
183*/
184export function useKeyboardShortcuts(shortcuts, eleRef) {
185 const shortcutsRef = useRef(shortcuts);
186
187 const matchFound = (shortcut, e)=>{
188 if(!shortcut) return false;
189 let keyCode = getCode(e);
190 const ctrlKey = (isMac() && shortcut.ctrl_is_meta) ? e.metaKey : e.ctrlKey;
191
192 return Boolean(shortcut.alt) == e.altKey &&
193 Boolean(shortcut.shift) == e.shiftKey &&
194 Boolean(shortcut.control) == ctrlKey &&
195 shortcut.key.key_code == keyCode;
196 };
197
198 useEffect(()=>{
199 let ele = eleRef.current ?? document;
200 const dispatch = (e)=>{
201 Promise.resolve(0).then(()=>{
202 let allListeners = _.filter(shortcutsRef.current, (s)=>matchFound(s.shortcut, e));
203 for(const {options} of allListeners) {
204 Promise.resolve(0).then(()=>{
205 if(options.callback && (options.enabled ?? true)) {
206 e.preventDefault();
207 e.stopPropagation();
208 options.callback(e);
209 }
210 });
211 }
212 });
213 };
214 ele.addEventListener('keydown', dispatch);
215 return ()=>{
216 ele.removeEventListener('keydown', dispatch);
217 };
218 }, [eleRef.current]);
219
220 useEffect(()=>{
221 shortcutsRef.current = shortcuts;
222 }, [shortcuts]);
223}
224
225export function useWindowSize() {
226 const [size, setSize] = useState([999999, 999999]);

Callers 4

ToolBarFunction · 0.90
MainToolBarFunction · 0.90
ConnectionBarFunction · 0.90
ResultSetToolbarFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected