change selection/line/draw shortcut defaults (#1953)

This commit is contained in:
David Luzar 2020-07-24 15:47:46 +02:00 committed by GitHub
parent c5d37a07c8
commit dc1f6c4d4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 22 deletions

View File

@ -104,9 +104,11 @@ export const ShapesSwitcher = ({
<> <>
{SHAPES.map(({ value, icon, key }, index) => { {SHAPES.map(({ value, icon, key }, index) => {
const label = t(`toolBar.${value}`); const label = t(`toolBar.${value}`);
const shortcut = `${capitalizeString(key)} ${t("shortcutsDialog.or")} ${ const letter = typeof key === "string" ? key : key[0];
index + 1 const letterShortcut = /[a-z]/.test(letter) ? letter : `Shift+${letter}`;
}`; const shortcut = `${capitalizeString(letterShortcut)} ${t(
"shortcutsDialog.or",
)} ${index + 1}`;
return ( return (
<ToolButton <ToolButton
className="Shape" className="Shape"

View File

@ -85,7 +85,7 @@ import {
getRotateWithDiscreteAngleKey, getRotateWithDiscreteAngleKey,
} from "../keys"; } from "../keys";
import { findShapeByKey, shapesShortcutKeys } from "../shapes"; import { findShapeByKey } from "../shapes";
import { createHistory, SceneHistory } from "../history"; import { createHistory, SceneHistory } from "../history";
import ContextMenu from "./ContextMenu"; import ContextMenu from "./ContextMenu";
@ -1379,8 +1379,6 @@ class App extends React.Component<ExcalidrawProps, AppState> {
this.setState({ isLibraryOpen: !this.state.isLibraryOpen }); this.setState({ isLibraryOpen: !this.state.isLibraryOpen });
} }
const shape = findShapeByKey(event.key);
if (isArrowKey(event.key)) { if (isArrowKey(event.key)) {
const step = const step =
(this.state.gridSize && (this.state.gridSize &&
@ -1444,7 +1442,8 @@ class App extends React.Component<ExcalidrawProps, AppState> {
!event.metaKey && !event.metaKey &&
this.state.draggingElement === null this.state.draggingElement === null
) { ) {
if (shapesShortcutKeys.includes(event.key.toLowerCase())) { const shape = findShapeByKey(event.key);
if (shape) {
this.selectShapeTool(shape); this.selectShapeTool(shape);
} else if (event.key === "q") { } else if (event.key === "q") {
this.toggleLock(); this.toggleLock();

View File

@ -178,13 +178,16 @@ export const ShortcutsDialog = ({ onClose }: { onClose?: () => void }) => {
<Columns> <Columns>
<Column> <Column>
<ShortcutIsland caption={t("shortcutsDialog.shapes")}> <ShortcutIsland caption={t("shortcutsDialog.shapes")}>
<Shortcut label={t("toolBar.selection")} shortcuts={["S", "1"]} /> <Shortcut label={t("toolBar.selection")} shortcuts={["V", "1"]} />
<Shortcut label={t("toolBar.rectangle")} shortcuts={["R", "2"]} /> <Shortcut label={t("toolBar.rectangle")} shortcuts={["R", "2"]} />
<Shortcut label={t("toolBar.diamond")} shortcuts={["D", "3"]} /> <Shortcut label={t("toolBar.diamond")} shortcuts={["D", "3"]} />
<Shortcut label={t("toolBar.ellipse")} shortcuts={["E", "4"]} /> <Shortcut label={t("toolBar.ellipse")} shortcuts={["E", "4"]} />
<Shortcut label={t("toolBar.arrow")} shortcuts={["A", "5"]} /> <Shortcut label={t("toolBar.arrow")} shortcuts={["A", "5"]} />
<Shortcut label={t("toolBar.line")} shortcuts={["L", "6"]} /> <Shortcut label={t("toolBar.line")} shortcuts={["P", "6"]} />
<Shortcut label={t("toolBar.draw")} shortcuts={["X", "7"]} /> <Shortcut
label={t("toolBar.draw")}
shortcuts={["Shift+P", "7"]}
/>
<Shortcut label={t("toolBar.text")} shortcuts={["T", "8"]} /> <Shortcut label={t("toolBar.text")} shortcuts={["T", "8"]} />
<Shortcut <Shortcut
label={t("shortcutsDialog.textNewLine")} label={t("shortcutsDialog.textNewLine")}

View File

@ -11,7 +11,7 @@ export const SHAPES = [
</svg> </svg>
), ),
value: "selection", value: "selection",
key: "s", key: ["v", "s"],
}, },
{ {
icon: ( icon: (
@ -68,7 +68,7 @@ export const SHAPES = [
</svg> </svg>
), ),
value: "line", value: "line",
key: "l", key: ["p", "l"],
}, },
{ {
icon: ( icon: (
@ -81,7 +81,7 @@ export const SHAPES = [
</svg> </svg>
), ),
value: "draw", value: "draw",
key: "x", key: ["P", "x"],
}, },
{ {
icon: ( icon: (
@ -95,12 +95,14 @@ export const SHAPES = [
}, },
] as const; ] as const;
export const shapesShortcutKeys = SHAPES.map((shape, index) => [ export const findShapeByKey = (key: string) => {
shape.key, const shape = SHAPES.find((shape, index) => {
(index + 1).toString(), return (
]).flat(1); key === (index + 1).toString() ||
(typeof shape.key === "string"
export const findShapeByKey = (key: string) => ? shape.key === key
SHAPES.find((shape, index) => { : (shape.key as readonly string[]).includes(key))
return shape.key === key.toLowerCase() || key === (index + 1).toString(); );
})?.value || "selection"; });
return shape?.value || null;
};