-
Notifications
You must be signed in to change notification settings - Fork 589
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app-file-manager): add support for shift-selecting files (#3675)
- Loading branch information
Showing
22 changed files
with
416 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export * from "./useConfirmationDialog"; | ||
export * from "./useDialog"; | ||
export * from "./useSnackbar"; | ||
export * from "./useKeyHandler"; | ||
export * from "./useShiftKey"; | ||
export * from "./useModKey"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React, { SyntheticEvent } from "react"; | ||
import isHotkey from "is-hotkey"; | ||
import { generateId } from "@webiny/utils"; | ||
|
||
interface KeyHandler { | ||
id: string; | ||
handler: any; | ||
} | ||
const keyStack: Record<string, KeyHandler[]> = {}; | ||
|
||
let listener = false; | ||
const filter = ["TEXTAREA", "INPUT"]; | ||
|
||
const isContentEditable = (value: any) => { | ||
return ["true", true].includes(value); | ||
}; | ||
|
||
const setupListener = (): void => { | ||
if (listener || !document.body) { | ||
return; | ||
} | ||
|
||
const eventListener = (ev: KeyboardEvent) => { | ||
if (!ev.target) { | ||
return; | ||
} | ||
const target = ev.target as HTMLElement; | ||
// We ignore all keyboard events coming from within contentEditable element and inputs. | ||
if (filter.includes(target.nodeName) || isContentEditable(target.contentEditable)) { | ||
return; | ||
} | ||
|
||
const matchedKey = Object.keys(keyStack).find(key => isHotkey(key, ev)); | ||
|
||
if (matchedKey && keyStack[matchedKey].length > 0) { | ||
const item = keyStack[matchedKey][0]; | ||
item.handler(ev); | ||
ev.stopPropagation(); | ||
} | ||
}; | ||
|
||
document.body.addEventListener("keydown", eventListener); | ||
|
||
listener = true; | ||
}; | ||
|
||
const addKeyHandler = ( | ||
id: string, | ||
key: string, | ||
handler: (e: SyntheticEvent<HTMLElement>) => void | ||
) => { | ||
setupListener(); | ||
keyStack[key] = keyStack[key] || []; | ||
if (!keyStack[key].find(item => item.id === id)) { | ||
keyStack[key].unshift({ id, handler }); | ||
} | ||
}; | ||
|
||
const removeKeyHandler = (id: string, key: string): void => { | ||
if (!keyStack[key]) { | ||
return; | ||
} | ||
|
||
const index = keyStack[key].findIndex(item => item.id === id); | ||
if (index >= 0) { | ||
keyStack[key].splice(index, 1); | ||
} | ||
}; | ||
|
||
type AddKeyHandlerType = (key: string, handler: (e: SyntheticEvent<HTMLElement>) => void) => void; | ||
|
||
type RemoveKeyHandlerType = (key: string) => void; | ||
|
||
export function useKeyHandler(): { | ||
addKeyHandler: AddKeyHandlerType; | ||
removeKeyHandler: RemoveKeyHandlerType; | ||
} { | ||
const [id] = React.useState(generateId()); | ||
|
||
return React.useMemo( | ||
() => ({ | ||
addKeyHandler(key, handler) { | ||
addKeyHandler(id, key, handler); | ||
}, | ||
removeKeyHandler(key) { | ||
removeKeyHandler(id, key); | ||
} | ||
}), | ||
[] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useState, useEffect, useCallback } from "react"; | ||
import isHotkey from "is-hotkey"; | ||
|
||
export function useModKey() { | ||
const [pressed, setPressed] = useState(false); | ||
|
||
const onKeyDown = useCallback((event: KeyboardEvent) => { | ||
if (isHotkey("Control", event) || isHotkey("Meta", event)) { | ||
setPressed(true); | ||
} | ||
}, []); | ||
|
||
const onKeyUp = useCallback((event: KeyboardEvent) => { | ||
if (event.key === "Control" && event.ctrlKey === false) { | ||
setPressed(false); | ||
} | ||
|
||
if (event.key === "Meta" && event.metaKey === false) { | ||
setPressed(false); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
document.addEventListener("keydown", onKeyDown); | ||
document.addEventListener("keyup", onKeyUp); | ||
|
||
return () => { | ||
document.removeEventListener("keydown", onKeyDown); | ||
document.removeEventListener("keyup", onKeyUp); | ||
}; | ||
}, []); | ||
|
||
console.log("pressed", pressed); | ||
|
||
return pressed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useState, useEffect, useCallback, useRef } from "react"; | ||
import isHotkey from "is-hotkey"; | ||
|
||
export function useShiftKey() { | ||
const [pressed, setPressed] = useState(false); | ||
const onselectstartRef = useRef(document.onselectstart); | ||
|
||
const onKeyDown = useCallback((event: KeyboardEvent) => { | ||
if (isHotkey("shift", event)) { | ||
setPressed(true); | ||
} | ||
}, []); | ||
|
||
const onKeyUp = useCallback((event: KeyboardEvent) => { | ||
if (event.key === "Shift" && event.shiftKey === false) { | ||
setPressed(false); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
// @ts-ignore | ||
document.onselectstart = () => !pressed; | ||
|
||
return () => { | ||
document.onselectstart = onselectstartRef.current; | ||
}; | ||
}, [pressed]); | ||
|
||
useEffect(() => { | ||
document.addEventListener("keydown", onKeyDown); | ||
document.addEventListener("keyup", onKeyUp); | ||
|
||
return () => { | ||
document.removeEventListener("keydown", onKeyDown); | ||
document.removeEventListener("keyup", onKeyUp); | ||
}; | ||
}, []); | ||
|
||
return pressed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.