Skip to content

Commit

Permalink
perf: use local ref to save flow editor focus state (microsoft#4474)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeze322 committed Oct 22, 2020
1 parent d2fabd8 commit c5715d6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
42 changes: 31 additions & 11 deletions Composer/packages/client/src/hooks/useElectronFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { useEffect } from 'react';
import { useEffect, useRef } from 'react';
import get from 'lodash/get';
import { getEditorAPI } from '@bfc/shared';

export const useElectronFeatures = (
actionSelected: boolean,
flowFocused: boolean,
canUndo: boolean,
canRedo: boolean
) => {
// Sync selection state to Electron main process
useEffect(() => {
export const useElectronFeatures = (actionSelected: boolean, canUndo: boolean, canRedo: boolean) => {
const flowEditorFocused = useRef(false);

const sendStatus = () => {
if (!window.__IS_ELECTRON__) return;
if (!window.ipcRenderer || typeof window.ipcRenderer.send !== 'function') return;
window.ipcRenderer.send('composer-state-change', {
actionSelected,
flowFocused: flowEditorFocused.current,
canUndo,
canRedo,
});
};

const onFocusFlowEditor = () => {
flowEditorFocused.current = true;
sendStatus();
};

window.ipcRenderer.send('composer-state-change', { actionSelected, flowFocused, canUndo, canRedo });
}, [actionSelected, flowFocused, canUndo, canRedo]);
const onBlurFlowEditor = () => {
flowEditorFocused.current = false;
sendStatus();
};

// Sync selection state to Electron main process
useEffect(() => {
sendStatus();
}, [actionSelected, canUndo, canRedo]);

// Subscribe Electron app menu events (copy/cut/del/undo/redo)
useEffect(() => {
Expand All @@ -41,4 +56,9 @@ export const useElectronFeatures = (
}
});
}, []);

return {
onFocusFlowEditor,
onBlurFlowEditor,
};
};
7 changes: 3 additions & 4 deletions Composer/packages/client/src/pages/design/DesignPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ const DesignPage: React.FC<RouteComponentProps<{ dialogId: string; projectId: st
}
};

const [flowEditorFocused, setFlowEditorFocused] = useState(false);
const { actionSelected, showDisableBtn, showEnableBtn } = useMemo(() => {
const actionSelected = Array.isArray(visualEditorSelection) && visualEditorSelection.length > 0;
if (!actionSelected) {
Expand All @@ -292,7 +291,7 @@ const DesignPage: React.FC<RouteComponentProps<{ dialogId: string; projectId: st
return { actionSelected, showDisableBtn, showEnableBtn };
}, [visualEditorSelection]);

useElectronFeatures(actionSelected, flowEditorFocused, canUndo(), canRedo());
const { onFocusFlowEditor, onBlurFlowEditor } = useElectronFeatures(actionSelected, canUndo(), canRedo());

const EditorAPI = getEditorAPI();
const toolbarItems: IToolbarItem[] = [
Expand Down Expand Up @@ -640,8 +639,8 @@ const DesignPage: React.FC<RouteComponentProps<{ dialogId: string; projectId: st
<EditorExtension plugins={pluginConfig} projectId={projectId} shell={shellForFlowEditor}>
<VisualEditor
openNewTriggerModal={openNewTriggerModal}
onBlur={() => setFlowEditorFocused(false)}
onFocus={() => setFlowEditorFocused(true)}
onBlur={() => onBlurFlowEditor()}
onFocus={() => onFocusFlowEditor()}
/>
</EditorExtension>
)}
Expand Down

0 comments on commit c5715d6

Please sign in to comment.