import type * as React from "react";
import {
useMemo,
useState,
useCallback,
useDeferredValue,
memo,
Fragment,
} from "react";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import { s } from "@shared/styles";
import { isMac } from "@shared/utils/browser";
import Flex from "~/components/Flex";
import InputSearch from "~/components/InputSearch";
import { KeyboardShortcut } from "~/components/KeyboardShortcut";
type Props = {
/** Initial search query to filter shortcuts */
defaultQuery?: string;
};
function KeyboardShortcuts({ defaultQuery = "" }: Props) {
const { t } = useTranslation();
const categories = useMemo(
() => [
{
title: t("Navigation"),
items: [
{
shortcut: ,
label: t("Open command menu"),
},
{
shortcut: (
),
label: t("Back"),
},
{
shortcut: (
),
label: t("Forward"),
},
{
shortcut: ,
label: t("Previous document in sidebar"),
},
{
shortcut: ,
label: t("Next document in sidebar"),
},
{
shortcut: ,
label: t("New document"),
},
{
shortcut: ,
label: t("Edit current document"),
},
{
shortcut: ,
label: t("Move current document"),
},
{
shortcut: ,
label: t("Open document history"),
},
{
shortcut: (
),
label: t("Jump to search"),
},
{
shortcut: ,
label: t("Jump to home"),
},
{
shortcut: ,
label: t("Table of contents"),
},
{
shortcut: ,
label: t("Toggle sidebar"),
},
{
shortcut: ,
label: t("Toggle theme"),
},
{
shortcut: ,
label: t("Toggle statistics"),
},
{
shortcut: ,
label: t("Focus search input"),
},
{
shortcut: ,
label: t("Open this guide"),
},
{
shortcut: ,
label: t("Go to link"),
},
{
shortcut: ,
label: t("Present document"),
},
{
shortcut: ,
label: t("Publish document and exit"),
},
{
shortcut: (
),
label: t("Cancel editing"),
},
],
},
{
title: t("Collaboration"),
items: [
{
shortcut: ,
label: t("Comment"),
},
],
},
{
title: t("Formatting"),
items: [
{
shortcut: ,
label: t("Paragraph"),
},
{
shortcut: ,
label: t("Big heading"),
},
{
shortcut: ,
label: t("Medium heading"),
},
{
shortcut: ,
label: t("Small heading"),
},
{
shortcut: ,
label: t("Code block"),
},
{
shortcut: ,
label: t("Bold"),
},
{
shortcut: ,
label: t("Code"),
},
{
shortcut: ,
label: t("Highlight"),
},
{
shortcut: ,
label: t("Italic"),
},
{
shortcut: ,
label: t("Link"),
},
{
shortcut: ,
label: t("Strikethrough"),
},
{
shortcut: ,
label: t("Underline"),
},
{
shortcut: ,
label: t("Undo"),
},
{
shortcut: ,
label: t("Redo"),
},
{
shortcut: ,
label: t("Move block up"),
},
{
shortcut: ,
label: t("Move block down"),
},
],
},
{
title: t("Lists"),
items: [
{
shortcut: ,
label: t("Todo list"),
},
{
shortcut: ,
label: t("Bulleted list"),
},
{
shortcut: ,
label: t("Ordered list"),
},
{
shortcut: ,
label: t("Toggle task list item"),
},
{
shortcut: ,
label: t("Indent list item"),
},
{
shortcut: ,
label: t("Outdent list item"),
},
{
shortcut: ,
label: t("Move list item up"),
},
{
shortcut: ,
label: t("Move list item down"),
},
],
},
{
title: t("Toggle blocks"),
items: [
{
shortcut: ,
label: t("Open / close"),
},
{
shortcut: ,
label: t("Indent item"),
},
{
shortcut: ,
label: t("Outdent item"),
},
],
},
{
title: t("Tables"),
items: [
{
shortcut: ,
label: t("Insert row"),
},
{
shortcut: ,
label: t("Next cell"),
},
{
shortcut: ,
label: t("Previous cell"),
},
],
},
{
title: t("Markdown"),
items: [
{
shortcut: (
),
label: t("Big heading"),
},
{
shortcut: (
),
label: t("Medium heading"),
},
{
shortcut: (
),
label: t("Small heading"),
},
{
shortcut: (
),
label: t("Numbered list"),
},
{
shortcut: (
),
label: t("Bulleted list"),
},
{
shortcut: (
),
label: t("Todo list"),
},
{
shortcut: (
", "space"]} combination="sequence" />
),
label: t("Blockquote"),
},
{
shortcut: ,
label: t("Horizontal divider"),
},
{
shortcut: ,
label: t("Table"),
},
{
shortcut: (
),
label: t("Code block"),
},
{
shortcut: (
),
label: t("LaTeX block"),
},
{
shortcut: (
),
label: t("Toggle block"),
},
{
shortcut: ,
label: t("Info notice"),
},
{
shortcut: "_italic_",
label: t("Italic"),
},
{
shortcut: "**bold**",
label: t("Bold"),
},
{
shortcut: "~~strikethrough~~",
label: t("Strikethrough"),
},
{
shortcut: "`code`",
label: t("Inline code"),
},
{
shortcut: "$$latex$$",
label: t("Inline LaTeX"),
},
{
shortcut: "==highlight==",
label: t("Highlight"),
},
],
},
{
title: t("Triggers"),
items: [
{
shortcut: "@",
label: t("Mention users and more"),
},
{
shortcut: ":",
label: t("Emoji"),
},
{
shortcut: "/",
label: t("Insert block"),
},
],
},
],
[t]
);
const [searchTerm, setSearchTerm] = useState(defaultQuery);
// Defer filtering so typing in the search input stays responsive.
const deferredSearchTerm = useDeferredValue(searchTerm);
const normalizedSearchTerm = deferredSearchTerm.toLocaleLowerCase();
const handleChange = useCallback(
(event: React.ChangeEvent) => {
setSearchTerm(event.target.value);
},
[]
);
const handleKeyDown = useCallback(
(event: React.KeyboardEvent) => {
if (event.currentTarget.value && event.key === "Escape") {
event.preventDefault();
event.stopPropagation();
setSearchTerm("");
}
},
[]
);
return (
{categories.map((category, x) => {
const titleMatches = category.title
.toLocaleLowerCase()
.includes(normalizedSearchTerm);
const filtered = deferredSearchTerm
? titleMatches
? category.items
: category.items.filter((item) =>
item.label.toLocaleLowerCase().includes(normalizedSearchTerm)
)
: category.items;
if (!filtered.length) {
return null;
}
return (
{filtered.map((item) => (
{item.shortcut}
))}
);
})}
);
}
const StickySearch = styled.div`
position: sticky;
top: -16px;
z-index: 1;
padding: 16px;
margin: -16px;
background: ${s("background")};
border-radius: 8px;
`;
const Header = styled.h2`
font-size: 15px;
font-weight: 500;
margin-top: 2em;
`;
const List = styled.dl`
font-size: 14px;
width: 100%;
overflow: hidden;
padding: 0;
margin: 0;
user-select: none;
`;
const Keys = styled.dt`
float: inline-end;
width: 45%;
margin: 0 0 10px;
clear: inline-start;
text-align: end;
font-size: 12px;
color: ${s("textSecondary")};
display: flex;
align-items: center;
justify-content: flex-end;
`;
const Label = styled.dd`
float: inline-start;
width: 55%;
margin: 0 0 10px;
display: flex;
align-items: center;
color: ${s("textSecondary")};
`;
export default memo(KeyboardShortcuts);