ShortcutsDialog.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import React from "react";
  2. import oc from "open-color";
  3. import { t } from "../i18n";
  4. import { isDarwin } from "../keys";
  5. import { Dialog } from "./Dialog";
  6. import { getShortcutKey } from "../utils";
  7. const Columns = (props: { children: React.ReactNode }) => (
  8. <div
  9. style={{
  10. display: "flex",
  11. flexDirection: "row",
  12. flexWrap: "wrap",
  13. justifyContent: "space-between",
  14. }}
  15. >
  16. {props.children}
  17. </div>
  18. );
  19. const Column = (props: { children: React.ReactNode }) => (
  20. <div
  21. style={{
  22. width: "49%",
  23. }}
  24. >
  25. {props.children}
  26. </div>
  27. );
  28. const ShortcutIsland = (props: {
  29. caption: string;
  30. children: React.ReactNode;
  31. }) => (
  32. <div
  33. style={{
  34. border: `1px solid ${oc.gray[4]}`,
  35. marginBottom: "16px",
  36. }}
  37. >
  38. <h3
  39. style={{
  40. margin: "0",
  41. padding: "4px",
  42. backgroundColor: oc.gray[2],
  43. textAlign: "center",
  44. }}
  45. >
  46. {props.caption}
  47. </h3>
  48. {props.children}
  49. </div>
  50. );
  51. const Shortcut = (props: {
  52. label: string;
  53. shortcuts: string[];
  54. isOr: boolean;
  55. }) => {
  56. const isRTL = document.documentElement.getAttribute("dir") === "rtl";
  57. return (
  58. <div
  59. style={{
  60. borderTop: `1px solid ${oc.gray[4]}`,
  61. }}
  62. >
  63. <div
  64. style={{
  65. display: "flex",
  66. margin: "0",
  67. padding: "4px 8px",
  68. alignItems: "center",
  69. }}
  70. >
  71. <div
  72. style={{
  73. lineHeight: 1.4,
  74. }}
  75. >
  76. {props.label}
  77. </div>
  78. <div
  79. style={{
  80. display: "flex",
  81. flex: "0 0 auto",
  82. justifyContent: "flex-end",
  83. marginLeft: isRTL ? "0em" : "auto",
  84. marginRight: isRTL ? "auto" : "0em",
  85. minWidth: "30%",
  86. }}
  87. >
  88. {props.shortcuts.map((shortcut, index) => (
  89. <React.Fragment key={index}>
  90. <ShortcutKey>{shortcut}</ShortcutKey>
  91. {props.isOr &&
  92. index !== props.shortcuts.length - 1 &&
  93. t("shortcutsDialog.or")}
  94. </React.Fragment>
  95. ))}
  96. </div>
  97. </div>
  98. </div>
  99. );
  100. };
  101. Shortcut.defaultProps = {
  102. isOr: true,
  103. };
  104. const ShortcutKey = (props: { children: React.ReactNode }) => (
  105. <span
  106. style={{
  107. wordBreak: "keep-all",
  108. border: `1px solid ${oc.gray[4]}`,
  109. padding: "2px 8px",
  110. margin: "auto 4px",
  111. backgroundColor: oc.gray[2],
  112. borderRadius: "2px",
  113. fontSize: "0.8em",
  114. minHeight: "26px",
  115. boxSizing: "border-box",
  116. display: "flex",
  117. alignItems: "center",
  118. }}
  119. {...props}
  120. />
  121. );
  122. const Footer = () => (
  123. <div
  124. style={{
  125. display: "flex",
  126. flexDirection: "row",
  127. justifyContent: "space-evenly",
  128. borderTop: `1px solid ${oc.gray[4]}`,
  129. marginTop: 8,
  130. paddingTop: 16,
  131. }}
  132. >
  133. <a
  134. href="https://blog.excalidraw.com"
  135. target="_blank"
  136. rel="noopener noreferrer"
  137. >
  138. {t("shortcutsDialog.blog")}
  139. </a>
  140. <a
  141. href="https://howto.excalidraw.com"
  142. target="_blank"
  143. rel="noopener noreferrer"
  144. >
  145. {t("shortcutsDialog.howto")}
  146. </a>
  147. <a
  148. href="https://github.com/excalidraw/excalidraw/issues"
  149. target="_blank"
  150. rel="noopener noreferrer"
  151. >
  152. {t("shortcutsDialog.github")}
  153. </a>
  154. </div>
  155. );
  156. export const ShortcutsDialog = ({ onClose }: { onClose?: () => void }) => {
  157. const handleClose = React.useCallback(() => {
  158. if (onClose) {
  159. onClose();
  160. }
  161. }, [onClose]);
  162. return (
  163. <>
  164. <Dialog
  165. maxWidth={900}
  166. onCloseRequest={handleClose}
  167. title={t("shortcutsDialog.title")}
  168. >
  169. <Columns>
  170. <Column>
  171. <ShortcutIsland caption={t("shortcutsDialog.shapes")}>
  172. <Shortcut label={t("toolBar.selection")} shortcuts={["S", "1"]} />
  173. <Shortcut label={t("toolBar.rectangle")} shortcuts={["R", "2"]} />
  174. <Shortcut label={t("toolBar.diamond")} shortcuts={["D", "3"]} />
  175. <Shortcut label={t("toolBar.ellipse")} shortcuts={["E", "4"]} />
  176. <Shortcut label={t("toolBar.arrow")} shortcuts={["A", "5"]} />
  177. <Shortcut label={t("toolBar.line")} shortcuts={["L", "6"]} />
  178. <Shortcut label={t("toolBar.draw")} shortcuts={["X", "7"]} />
  179. <Shortcut label={t("toolBar.text")} shortcuts={["T", "8"]} />
  180. <Shortcut
  181. label={t("shortcutsDialog.textNewLine")}
  182. shortcuts={[
  183. getShortcutKey("Enter"),
  184. getShortcutKey("Shift+Enter"),
  185. ]}
  186. />
  187. <Shortcut
  188. label={t("shortcutsDialog.textFinish")}
  189. shortcuts={[
  190. getShortcutKey("Esc"),
  191. getShortcutKey("CtrlOrCmd+Enter"),
  192. ]}
  193. />
  194. <Shortcut
  195. label={t("shortcutsDialog.curvedArrow")}
  196. shortcuts={[
  197. "A",
  198. t("shortcutsDialog.click"),
  199. t("shortcutsDialog.click"),
  200. t("shortcutsDialog.click"),
  201. ]}
  202. isOr={false}
  203. />
  204. <Shortcut
  205. label={t("shortcutsDialog.curvedLine")}
  206. shortcuts={[
  207. "L",
  208. t("shortcutsDialog.click"),
  209. t("shortcutsDialog.click"),
  210. t("shortcutsDialog.click"),
  211. ]}
  212. isOr={false}
  213. />
  214. <Shortcut label={t("toolBar.lock")} shortcuts={["Q"]} />
  215. </ShortcutIsland>
  216. <ShortcutIsland caption={t("shortcutsDialog.view")}>
  217. <Shortcut
  218. label={t("buttons.zoomIn")}
  219. shortcuts={[getShortcutKey("CtrlOrCmd++")]}
  220. />
  221. <Shortcut
  222. label={t("buttons.zoomOut")}
  223. shortcuts={[getShortcutKey("CtrlOrCmd+-")]}
  224. />
  225. <Shortcut
  226. label={t("buttons.resetZoom")}
  227. shortcuts={[getShortcutKey("CtrlOrCmd+0")]}
  228. />
  229. <Shortcut
  230. label={t("shortcutsDialog.zoomToFit")}
  231. shortcuts={["Shift+1"]}
  232. />
  233. <Shortcut
  234. label={t("buttons.toggleFullScreen")}
  235. shortcuts={["F"]}
  236. />
  237. <Shortcut
  238. label={t("buttons.toggleZenMode")}
  239. shortcuts={[getShortcutKey("Alt+Z")]}
  240. />
  241. <Shortcut
  242. label={t("labels.toggleGridMode")}
  243. shortcuts={[getShortcutKey("CtrlOrCmd+'")]}
  244. />
  245. </ShortcutIsland>
  246. </Column>
  247. <Column>
  248. <ShortcutIsland caption={t("shortcutsDialog.editor")}>
  249. <Shortcut
  250. label={t("labels.selectAll")}
  251. shortcuts={[getShortcutKey("CtrlOrCmd+A")]}
  252. />
  253. <Shortcut
  254. label={t("labels.multiSelect")}
  255. shortcuts={[
  256. getShortcutKey(`Shift+${t("shortcutsDialog.click")}`),
  257. ]}
  258. />
  259. <Shortcut
  260. label={t("labels.moveCanvas")}
  261. shortcuts={[
  262. getShortcutKey(`Space+${t("shortcutsDialog.drag")}`),
  263. getShortcutKey(`Wheel+${t("shortcutsDialog.drag")}`),
  264. ]}
  265. isOr={true}
  266. />
  267. <Shortcut
  268. label={t("labels.copy")}
  269. shortcuts={[getShortcutKey("CtrlOrCmd+C")]}
  270. />
  271. <Shortcut
  272. label={t("labels.paste")}
  273. shortcuts={[getShortcutKey("CtrlOrCmd+V")]}
  274. />
  275. <Shortcut
  276. label={t("labels.copyAsPng")}
  277. shortcuts={[getShortcutKey("Shift+Alt+C")]}
  278. />
  279. <Shortcut
  280. label={t("labels.copyStyles")}
  281. shortcuts={[getShortcutKey("CtrlOrCmd+Shift+C")]}
  282. />
  283. <Shortcut
  284. label={t("labels.pasteStyles")}
  285. shortcuts={[getShortcutKey("CtrlOrCmd+Shift+V")]}
  286. />
  287. <Shortcut
  288. label={t("labels.delete")}
  289. shortcuts={[getShortcutKey("Del")]}
  290. />
  291. <Shortcut
  292. label={t("labels.sendToBack")}
  293. shortcuts={[
  294. isDarwin
  295. ? getShortcutKey("CtrlOrCmd+Alt+[")
  296. : getShortcutKey("CtrlOrCmd+Shift+["),
  297. ]}
  298. />
  299. <Shortcut
  300. label={t("labels.bringToFront")}
  301. shortcuts={[
  302. isDarwin
  303. ? getShortcutKey("CtrlOrCmd+Alt+]")
  304. : getShortcutKey("CtrlOrCmd+Shift+]"),
  305. ]}
  306. />
  307. <Shortcut
  308. label={t("labels.sendBackward")}
  309. shortcuts={[getShortcutKey("CtrlOrCmd+[")]}
  310. />
  311. <Shortcut
  312. label={t("labels.bringForward")}
  313. shortcuts={[getShortcutKey("CtrlOrCmd+]")]}
  314. />
  315. <Shortcut
  316. label={t("labels.duplicateSelection")}
  317. shortcuts={[
  318. getShortcutKey("CtrlOrCmd+D"),
  319. getShortcutKey(`Alt+${t("shortcutsDialog.drag")}`),
  320. ]}
  321. />
  322. <Shortcut
  323. label={t("buttons.undo")}
  324. shortcuts={[getShortcutKey("CtrlOrCmd+Z")]}
  325. />
  326. <Shortcut
  327. label={t("buttons.redo")}
  328. shortcuts={[getShortcutKey("CtrlOrCmd+Shift+Z")]}
  329. />
  330. <Shortcut
  331. label={t("labels.group")}
  332. shortcuts={[getShortcutKey("CtrlOrCmd+G")]}
  333. />
  334. <Shortcut
  335. label={t("labels.ungroup")}
  336. shortcuts={[getShortcutKey("CtrlOrCmd+Shift+G")]}
  337. />
  338. </ShortcutIsland>
  339. </Column>
  340. </Columns>
  341. <Footer />
  342. </Dialog>
  343. </>
  344. );
  345. };