From 27c572b2f5bcbc4a0d027e9b1a6b88dc8a9147d2 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Mon, 21 Sep 2020 17:31:25 +0100 Subject: [PATCH] Desktop: Fixes #3700: Disable editor shortcuts when a dialog, such as GotoAnything, is visible --- ElectronClient/app.js | 11 +++++++++ ElectronClient/gui/MainScreen/MainScreen.tsx | 23 ++++++++++++++++++- .../utils/useWindowCommandHandler.ts | 3 ++- ElectronClient/plugins/GotoAnything.jsx | 10 ++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/ElectronClient/app.js b/ElectronClient/app.js index 130f5a4961..9a53d832ec 100644 --- a/ElectronClient/app.js +++ b/ElectronClient/app.js @@ -99,6 +99,7 @@ const appDefaultState = Object.assign({}, defaultState, { watchedNoteFiles: [], lastEditorScrollPercents: {}, devToolsVisible: false, + visibleDialogs: {}, // empty object if no dialog is visible. Otherwise contains the list of visible dialogs. }); class Application extends BaseApplication { @@ -280,6 +281,16 @@ class Application extends BaseApplication { newState.devToolsVisible = action.value; break; + case 'VISIBLE_DIALOGS_ADD': + newState = Object.assign({}, state); + newState.visibleDialogs[state.name] = true; + break; + + case 'VISIBLE_DIALOGS_REMOVE': + newState = Object.assign({}, state); + delete newState.visibleDialogs[state.name]; + break; + } } catch (error) { error.message = `In reducer: ${error.message} Action: ${JSON.stringify(action)}`; diff --git a/ElectronClient/gui/MainScreen/MainScreen.tsx b/ElectronClient/gui/MainScreen/MainScreen.tsx index 695472a7ee..ee0eb8c1d9 100644 --- a/ElectronClient/gui/MainScreen/MainScreen.tsx +++ b/ElectronClient/gui/MainScreen/MainScreen.tsx @@ -224,7 +224,7 @@ class MainScreenComponent extends React.Component { }) }); } - componentDidUpdate(prevProps:any) { + componentDidUpdate(prevProps:any, prevState:any) { if (this.props.noteListVisibility !== prevProps.noteListVisibility || this.props.sidebarVisibility !== prevProps.sidebarVisibility) { this.setState({ layout: produce(this.state.layout, (draftState:any) => { const noteListColumn = findItemByKey(draftState, 'noteListColumn'); @@ -238,6 +238,27 @@ class MainScreenComponent extends React.Component { if (prevProps.style.width !== this.props.style.width || prevProps.style.height !== this.props.style.height) { this.updateRootLayoutSize(); } + + if (this.state.notePropertiesDialogOptions !== prevState.notePropertiesDialogOptions) { + this.props.dispatch({ + type: this.state.notePropertiesDialogOptions && this.state.notePropertiesDialogOptions.visible ? 'VISIBLE_DIALOGS_ADD' : 'VISIBLE_DIALOGS_REMOVE', + name: 'noteProperties', + }); + } + + if (this.state.noteContentPropertiesDialogOptions !== prevState.noteContentPropertiesDialogOptions) { + this.props.dispatch({ + type: this.state.noteContentPropertiesDialogOptions && this.state.noteContentPropertiesDialogOptions.visible ? 'VISIBLE_DIALOGS_ADD' : 'VISIBLE_DIALOGS_REMOVE', + name: 'noteContentProperties', + }); + } + + if (this.state.shareNoteDialogOptions !== prevState.shareNoteDialogOptions) { + this.props.dispatch({ + type: this.state.shareNoteDialogOptions && this.state.shareNoteDialogOptions.visible ? 'VISIBLE_DIALOGS_ADD' : 'VISIBLE_DIALOGS_REMOVE', + name: 'shareNote', + }); + } } componentDidMount() { diff --git a/ElectronClient/gui/NoteEditor/utils/useWindowCommandHandler.ts b/ElectronClient/gui/NoteEditor/utils/useWindowCommandHandler.ts index 4f8b97ab44..918f5999ac 100644 --- a/ElectronClient/gui/NoteEditor/utils/useWindowCommandHandler.ts +++ b/ElectronClient/gui/NoteEditor/utils/useWindowCommandHandler.ts @@ -44,7 +44,7 @@ function editorCommandRuntime(declaration:CommandDeclaration, editorRef:any):Com } }, isEnabled: (props:any) => { - if (props.routeName !== 'Main') return false; + if (props.routeName !== 'Main' || props.isDialogVisible) return false; if (props.markdownEditorViewerOnly) return false; if (!props.noteId) return false; const note = BaseModel.byId(props.notes, props.noteId); @@ -60,6 +60,7 @@ function editorCommandRuntime(declaration:CommandDeclaration, editorRef:any):Com notes: state.notes, noteId: state.selectedNoteIds.length === 1 ? state.selectedNoteIds[0] : null, routeName: state.route.routeName, + isDialogVisible: !!Object.keys(state.visibleDialogs).length, }; }, }; diff --git a/ElectronClient/plugins/GotoAnything.jsx b/ElectronClient/plugins/GotoAnything.jsx index 4e613eaed2..e1fc379c4f 100644 --- a/ElectronClient/plugins/GotoAnything.jsx +++ b/ElectronClient/plugins/GotoAnything.jsx @@ -120,11 +120,21 @@ class Dialog extends React.PureComponent { componentDidMount() { document.addEventListener('keydown', this.onKeyDown); + + this.props.dispatch({ + type: 'VISIBLE_DIALOGS_ADD', + name: 'gotoAnything', + }); } componentWillUnmount() { if (this.listUpdateIID_) clearTimeout(this.listUpdateIID_); document.removeEventListener('keydown', this.onKeyDown); + + this.props.dispatch({ + type: 'VISIBLE_DIALOGS_REMOVE', + name: 'gotoAnything', + }); } onKeyDown(event) {