ShortcutsDialog.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.text")} shortcuts={["T", "7"]} />
  179. <Shortcut
  180. label={t("shortcutsDialog.textNewLine")}
  181. shortcuts={[
  182. getShortcutKey("Enter"),
  183. getShortcutKey("Shift+Enter"),
  184. ]}
  185. />
  186. <Shortcut
  187. label={t("shortcutsDialog.textFinish")}
  188. shortcuts={[
  189. getShortcutKey("Esc"),
  190. getShortcutKey("CtrlOrCmd+Enter"),
  191. ]}
  192. />
  193. <Shortcut
  194. label={t("shortcutsDialog.curvedArrow")}
  195. shortcuts={[
  196. "A",
  197. t("shortcutsDialog.click"),
  198. t("shortcutsDialog.click"),
  199. t("shortcutsDialog.click"),
  200. ]}
  201. isOr={false}
  202. />
  203. <Shortcut
  204. label={t("shortcutsDialog.curvedLine")}
  205. shortcuts={[
  206. "L",
  207. t("shortcutsDialog.click"),
  208. t("shortcutsDialog.click"),
  209. t("shortcutsDialog.click"),
  210. ]}
  211. isOr={false}
  212. />
  213. <Shortcut label={t("toolBar.lock")} shortcuts={["Q"]} />
  214. </ShortcutIsland>
  215. <ShortcutIsland caption={t("shortcutsDialog.view")}>
  216. <Shortcut
  217. label={t("buttons.zoomIn")}
  218. shortcuts={[getShortcutKey("CtrlOrCmd++")]}
  219. />
  220. <Shortcut
  221. label={t("buttons.zoomOut")}
  222. shortcuts={[getShortcutKey("CtrlOrCmd+-")]}
  223. />
  224. <Shortcut
  225. label={t("buttons.resetZoom")}
  226. shortcuts={[getShortcutKey("CtrlOrCmd+0")]}
  227. />
  228. <Shortcut
  229. label={t("shortcutsDialog.zoomToFit")}
  230. shortcuts={["Shift+1"]}
  231. />
  232. <Shortcut
  233. label={t("buttons.toggleFullScreen")}
  234. shortcuts={["F"]}
  235. />
  236. <Shortcut
  237. label={t("buttons.toggleZenMode")}
  238. shortcuts={["Alt+Z"]}
  239. />
  240. </ShortcutIsland>
  241. </Column>
  242. <Column>
  243. <ShortcutIsland caption={t("shortcutsDialog.editor")}>
  244. <Shortcut
  245. label={t("labels.selectAll")}
  246. shortcuts={[getShortcutKey("CtrlOrCmd+A")]}
  247. />
  248. <Shortcut
  249. label={t("labels.copy")}
  250. shortcuts={[getShortcutKey("CtrlOrCmd+C")]}
  251. />
  252. <Shortcut
  253. label={t("labels.paste")}
  254. shortcuts={[getShortcutKey("CtrlOrCmd+V")]}
  255. />
  256. <Shortcut
  257. label={t("labels.copyAsPng")}
  258. shortcuts={[getShortcutKey("Shift+Alt+C")]}
  259. />
  260. <Shortcut
  261. label={t("labels.copyStyles")}
  262. shortcuts={[getShortcutKey("CtrlOrCmd+Shift+C")]}
  263. />
  264. <Shortcut
  265. label={t("labels.pasteStyles")}
  266. shortcuts={[getShortcutKey("CtrlOrCmd+Shift+V")]}
  267. />
  268. <Shortcut
  269. label={t("labels.delete")}
  270. shortcuts={[getShortcutKey("Del")]}
  271. />
  272. <Shortcut
  273. label={t("labels.sendToBack")}
  274. shortcuts={[
  275. isDarwin
  276. ? getShortcutKey("CtrlOrCmd+Alt+[")
  277. : getShortcutKey("CtrlOrCmd+Shift+["),
  278. ]}
  279. />
  280. <Shortcut
  281. label={t("labels.bringToFront")}
  282. shortcuts={[
  283. isDarwin
  284. ? getShortcutKey("CtrlOrCmd+Alt+]")
  285. : getShortcutKey("CtrlOrCmd+Shift+]"),
  286. ]}
  287. />
  288. <Shortcut
  289. label={t("labels.sendBackward")}
  290. shortcuts={[getShortcutKey("CtrlOrCmd+[")]}
  291. />
  292. <Shortcut
  293. label={t("labels.bringForward")}
  294. shortcuts={[getShortcutKey("CtrlOrCmd+]")]}
  295. />
  296. <Shortcut
  297. label={t("labels.duplicateSelection")}
  298. shortcuts={[
  299. getShortcutKey("CtrlOrCmd+D"),
  300. getShortcutKey(`Alt+${t("shortcutsDialog.drag")}`),
  301. ]}
  302. />
  303. <Shortcut
  304. label={t("buttons.undo")}
  305. shortcuts={[getShortcutKey("CtrlOrCmd+Z")]}
  306. />
  307. <Shortcut
  308. label={t("buttons.redo")}
  309. shortcuts={[getShortcutKey("CtrlOrCmd+Shift+Z")]}
  310. />
  311. </ShortcutIsland>
  312. </Column>
  313. </Columns>
  314. <Footer />
  315. </Dialog>
  316. </>
  317. );
  318. };