From 778e4b08af13a0e1a4a2a4c8052d51af40c87da3 Mon Sep 17 00:00:00 2001 From: David Luzar Date: Wed, 6 Jan 2021 13:36:55 +0100 Subject: [PATCH] feat: Add cmd+o shortcut to load scene (#2732) --- src/actions/actionExport.tsx | 47 ++++++++++++++-------------- src/keys.ts | 1 + src/packages/excalidraw/CHANGELOG.md | 1 + 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/actions/actionExport.tsx b/src/actions/actionExport.tsx index ac60fdd3..e341d98f 100644 --- a/src/actions/actionExport.tsx +++ b/src/actions/actionExport.tsx @@ -9,7 +9,6 @@ import { loadFromJSON, saveAsJSON } from "../data"; import { t } from "../i18n"; import useIsMobile from "../is-mobile"; import { KEYS } from "../keys"; -import { muteFSAbortError } from "../utils"; import { register } from "./register"; export const actionChangeProjectName = register({ @@ -156,18 +155,29 @@ export const actionSaveAsScene = register({ export const actionLoadScene = register({ name: "loadScene", - perform: ( - elements, - appState, - { elements: loadedElements, appState: loadedAppState, error }, - ) => ({ - elements: loadedElements, - appState: { - ...loadedAppState, - errorMessage: error, - }, - commitToHistory: true, - }), + perform: async (elements, appState) => { + try { + const { + elements: loadedElements, + appState: loadedAppState, + } = await loadFromJSON(appState); + return { + elements: loadedElements, + appState: loadedAppState, + commitToHistory: true, + }; + } catch (error) { + if (error?.name === "AbortError") { + return false; + } + return { + elements, + appState: { ...appState, errorMessage: error.message }, + commitToHistory: false, + }; + } + }, + keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.key === KEYS.O, PanelComponent: ({ updateData, appState }) => ( { - loadFromJSON(appState) - .then(({ elements, appState }) => { - updateData({ elements, appState }); - }) - .catch(muteFSAbortError) - .catch((error) => { - updateData({ error: error.message }); - }); - }} + onClick={updateData} /> ), }); diff --git a/src/keys.ts b/src/keys.ts index d072ca93..9e0cd2ac 100644 --- a/src/keys.ts +++ b/src/keys.ts @@ -40,6 +40,7 @@ export const KEYS = { D: "d", E: "e", L: "l", + O: "o", P: "p", Q: "q", R: "r", diff --git a/src/packages/excalidraw/CHANGELOG.md b/src/packages/excalidraw/CHANGELOG.md index 42173549..4d6774a8 100644 --- a/src/packages/excalidraw/CHANGELOG.md +++ b/src/packages/excalidraw/CHANGELOG.md @@ -16,6 +16,7 @@ Please add the latest change on the top under the correct section. ### Features +- Add `cmd+o` shortcut to load scene [#2732](https://github.com/excalidraw/excalidraw/pull/2732) - Remove language picker, and add `langCode`, `renderFooter` [#2644](https://github.com/excalidraw/excalidraw/pull/2644): - BREAKING: removed the language picker from UI. It is now the host app's responsibility to implement a language picker if desirable, using the newly added [`renderFooter`](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#renderFooter) prop. The reasoning is that the i18n should be controlled by the app itself, not by the nested Excalidraw component. - Added [`langCode`](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#langCode) prop to control the UI language.