mirror of
https://github.com/outline/outline.git
synced 2026-11-24 23:00:18 +03:00
fix: useKeyDown binding blocks browser defaults (#13098)
* fix: useKeyDown binding blocks browser defaults * feedback
This commit is contained in:
1 parent
55fa306838
commit
2d91a13647
5 files changed
+95
-54
No files matched your search
@@ -15,7 +15,6 @@ import usePolicy from "~/hooks/usePolicy";
|
||||
import useStores from "~/hooks/useStores";
|
||||
import Logger from "~/utils/Logger";
|
||||
import history from "~/utils/history";
|
||||
import { isModKey } from "@shared/utils/keyboard";
|
||||
import lazyWithRetry from "~/utils/lazyWithRetry";
|
||||
import {
|
||||
searchPath,
|
||||
@@ -45,11 +44,7 @@ const AuthenticatedLayout: React.FC = ({ children }: Props) => {
|
||||
const team = useCurrentTeam();
|
||||
const [spendPostLoginPath] = usePostLoginPath();
|
||||
|
||||
useKeyDown(".", (event) => {
|
||||
if (isModKey(event)) {
|
||||
ui.toggleCollapsedSidebar();
|
||||
}
|
||||
});
|
||||
useKeyDown(".", () => ui.toggleCollapsedSidebar(), { metaKey: true });
|
||||
|
||||
const goToSearch = (ev: KeyboardEvent) => {
|
||||
if (!ev.metaKey && !ev.ctrlKey) {
|
||||
|
||||
@@ -6,11 +6,7 @@ import { useHistory } from "react-router-dom";
|
||||
import styled, { useTheme } from "styled-components";
|
||||
import breakpoint from "styled-components-breakpoint";
|
||||
import { s } from "@shared/styles";
|
||||
import {
|
||||
isModKey,
|
||||
metaDisplay,
|
||||
shortcutSeparator,
|
||||
} from "@shared/utils/keyboard";
|
||||
import { metaDisplay, shortcutSeparator } from "@shared/utils/keyboard";
|
||||
import useBoolean from "~/hooks/useBoolean";
|
||||
import useKeyDown from "~/hooks/useKeyDown";
|
||||
import useMobile from "~/hooks/useMobile";
|
||||
@@ -52,12 +48,16 @@ function InputSearchPage({
|
||||
const isMobile = useMobile();
|
||||
const [isFocused, setFocused, setUnfocused] = useBoolean(false);
|
||||
|
||||
useKeyDown("f", (ev: KeyboardEvent) => {
|
||||
if (isModKey(ev) && document.activeElement !== inputRef.current) {
|
||||
ev.preventDefault();
|
||||
inputRef.current?.focus();
|
||||
}
|
||||
});
|
||||
useKeyDown(
|
||||
"f",
|
||||
(ev: KeyboardEvent) => {
|
||||
if (document.activeElement !== inputRef.current) {
|
||||
ev.preventDefault();
|
||||
inputRef.current?.focus();
|
||||
}
|
||||
},
|
||||
{ metaKey: true }
|
||||
);
|
||||
|
||||
const handleKeyDown = React.useCallback(
|
||||
(ev: React.KeyboardEvent<HTMLInputElement>) => {
|
||||
|
||||
@@ -4,6 +4,12 @@ import useKeyDown from "~/hooks/useKeyDown";
|
||||
type Props = {
|
||||
trigger: KeyFilter;
|
||||
handler: (event: KeyboardEvent) => void;
|
||||
/** Require the platform modifier key (Cmd on macOS, Ctrl elsewhere). */
|
||||
metaKey?: boolean;
|
||||
/** Require the Alt/Option key. */
|
||||
altKey?: boolean;
|
||||
/** Require the Shift key. */
|
||||
shiftKey?: boolean;
|
||||
options?: Options;
|
||||
};
|
||||
|
||||
@@ -12,7 +18,19 @@ type Props = {
|
||||
* class components that have not yet been converted to functions. Do not use
|
||||
* this method in functional components.
|
||||
*/
|
||||
export default function RegisterKeyDown({ trigger, handler, options }: Props) {
|
||||
useKeyDown(trigger, handler, options);
|
||||
export default function RegisterKeyDown({
|
||||
trigger,
|
||||
handler,
|
||||
metaKey,
|
||||
altKey,
|
||||
shiftKey,
|
||||
options,
|
||||
}: Props) {
|
||||
useKeyDown(trigger, handler, {
|
||||
...options,
|
||||
metaKey: metaKey ?? options?.metaKey,
|
||||
altKey: altKey ?? options?.altKey,
|
||||
shiftKey: shiftKey ?? options?.shiftKey,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
+42
-3
@@ -10,6 +10,12 @@ export type KeyFilter = ((event: KeyboardEvent) => boolean) | string;
|
||||
|
||||
export type Options = {
|
||||
allowInInput?: boolean;
|
||||
/** Require the platform modifier key (Cmd on macOS, Ctrl elsewhere) to be held. */
|
||||
metaKey?: boolean;
|
||||
/** Require the Alt/Option key to be held. */
|
||||
altKey?: boolean;
|
||||
/** Require the Shift key to be held. */
|
||||
shiftKey?: boolean;
|
||||
};
|
||||
|
||||
type RegisteredCallback = {
|
||||
@@ -24,24 +30,57 @@ let callbacks: RegisteredCallback[] = [];
|
||||
// in this case, they should never be triggered from mobile keyboards.
|
||||
let imeOpen = false;
|
||||
|
||||
// Returns true when the event's modifier keys satisfy the given options.
|
||||
const matchesModifiers = (event: KeyboardEvent, options?: Options) => {
|
||||
if (options?.metaKey ? !isModKey(event) : isModKey(event)) {
|
||||
return false;
|
||||
}
|
||||
if (options?.altKey && !event.altKey) {
|
||||
return false;
|
||||
}
|
||||
if (options?.shiftKey && !event.shiftKey) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// Based on implementation in react-use
|
||||
// https://github.com/streamich/react-use/blob/master/src/useKey.ts#L15-L22
|
||||
const createKeyPredicate = (keyFilter: KeyFilter) =>
|
||||
// A string filter matches the bare key with the modifiers described by options;
|
||||
// use a function filter for anything the options cannot express.
|
||||
const createKeyPredicate = (keyFilter: KeyFilter, options?: Options) =>
|
||||
typeof keyFilter === "function"
|
||||
? keyFilter
|
||||
: typeof keyFilter === "string"
|
||||
? (event: KeyboardEvent) => event.key === keyFilter
|
||||
? (event: KeyboardEvent) => {
|
||||
// Match case-insensitively only when Shift is required, since a
|
||||
// shifted letter reports an uppercase event.key (e.g. Cmd+Shift+P).
|
||||
// Otherwise match exactly so a bare shortcut does not fire when Shift
|
||||
// is held (e.g. "n" must not trigger on Shift+N).
|
||||
const keyMatches = options?.shiftKey
|
||||
? event.key.toLowerCase() === keyFilter.toLowerCase()
|
||||
: event.key === keyFilter;
|
||||
return keyMatches && matchesModifiers(event, options);
|
||||
}
|
||||
: keyFilter
|
||||
? (_event: KeyboardEvent) => true
|
||||
: (_event: KeyboardEvent) => false;
|
||||
|
||||
/**
|
||||
* Registers a global keyboard shortcut for the lifetime of the component.
|
||||
*
|
||||
* @param key The key to match, either a string for a single key or a predicate
|
||||
* function for full control over the event.
|
||||
* @param fn The callback to invoke when the shortcut matches.
|
||||
* @param options Behavioral options, including the required modifier keys.
|
||||
*/
|
||||
export default function useKeyDown(
|
||||
key: KeyFilter,
|
||||
fn: Callback,
|
||||
options?: Options
|
||||
): void {
|
||||
const { pane, isSplitView } = useContext(SplitViewContext);
|
||||
const predicate = createKeyPredicate(key);
|
||||
const predicate = createKeyPredicate(key, options);
|
||||
|
||||
useEffect(() => {
|
||||
const handler = (event: KeyboardEvent) => {
|
||||
|
||||
@@ -12,7 +12,6 @@ import { s } from "@shared/styles";
|
||||
import type { NavigationNode } from "@shared/types";
|
||||
import { IconType, TOCPosition, TeamPreference } from "@shared/types";
|
||||
import { determineIconType } from "@shared/utils/icon";
|
||||
import { isModKey } from "@shared/utils/keyboard";
|
||||
import type Document from "~/models/Document";
|
||||
import type Revision from "~/models/Revision";
|
||||
import DocumentMove from "~/components/DocumentExplorer/DocumentMove";
|
||||
@@ -151,29 +150,26 @@ function DocumentScene({
|
||||
|
||||
const onUndoRedo = useCallback(
|
||||
(event: KeyboardEvent) => {
|
||||
if (isModKey(event)) {
|
||||
const target =
|
||||
event.target instanceof Element ? event.target : undefined;
|
||||
const target = event.target instanceof Element ? event.target : undefined;
|
||||
|
||||
// The editor handles undo/redo through its own keymap when focused
|
||||
if (
|
||||
editorRef.current?.view?.hasFocus() ||
|
||||
(target && (isTextInput(target) || !!target.closest(".ProseMirror")))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
// The editor handles undo/redo through its own keymap when focused
|
||||
if (
|
||||
editorRef.current?.view?.hasFocus() ||
|
||||
(target && (isTextInput(target) || !!target.closest(".ProseMirror")))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
event.preventDefault();
|
||||
|
||||
if (event.shiftKey) {
|
||||
if (!readOnly) {
|
||||
editorRef.current?.commands.redo?.();
|
||||
}
|
||||
} else {
|
||||
if (!readOnly) {
|
||||
editorRef.current?.commands.undo?.();
|
||||
}
|
||||
}
|
||||
if (readOnly) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.shiftKey) {
|
||||
editorRef.current?.commands.redo?.();
|
||||
} else {
|
||||
editorRef.current?.commands.undo?.();
|
||||
}
|
||||
},
|
||||
[readOnly]
|
||||
@@ -259,15 +255,6 @@ function DocumentScene({
|
||||
[document, dialogs, t, onSave]
|
||||
);
|
||||
|
||||
const handlePublishShortcut = useCallback(
|
||||
(event: KeyboardEvent) => {
|
||||
if (isModKey(event) && event.shiftKey) {
|
||||
onPublish(event);
|
||||
}
|
||||
},
|
||||
[onPublish]
|
||||
);
|
||||
|
||||
const goBack = useCallback(() => {
|
||||
if (!readOnly) {
|
||||
history.push({
|
||||
@@ -309,16 +296,18 @@ function DocumentScene({
|
||||
return (
|
||||
<ErrorBoundary showTitle>
|
||||
<RegisterKeyDown trigger="m" handler={onMove} />
|
||||
<RegisterKeyDown trigger="z" handler={onUndoRedo} />
|
||||
<RegisterKeyDown trigger="z" metaKey handler={onUndoRedo} />
|
||||
<RegisterKeyDown trigger="e" handler={goToEdit} />
|
||||
<RegisterKeyDown trigger="Escape" handler={goBack} />
|
||||
<RegisterKeyDown trigger="h" handler={goToHistory} />
|
||||
<RegisterKeyDown
|
||||
trigger="p"
|
||||
metaKey
|
||||
shiftKey
|
||||
options={{
|
||||
allowInInput: true,
|
||||
}}
|
||||
handler={handlePublishShortcut}
|
||||
handler={onPublish}
|
||||
/>
|
||||
<MeasuredContainer
|
||||
as={Background}
|
||||
|
||||
Reference in new issue
Block a user