Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undo/redo in new canvas implementation #3191

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { styled } from '@mui/material';
import { styled, useEventCallback } from '@mui/material';
import { NodeHashes, RuntimeEvents } from '@mui/toolpad-core';
import createCache from '@emotion/cache';
import { CacheProvider } from '@emotion/react';
Expand All @@ -20,6 +20,7 @@ import { rectContainsPoint } from '../../../utils/geometry';
import { queryClient } from '../../../runtime/api';
import { PageViewState } from '../../../types';
import { updateNodeInfo } from '../../../canvas';
import { useAppStateApi } from '../../AppState';

interface OverlayProps {
children?: React.ReactNode;
Expand Down Expand Up @@ -90,15 +91,38 @@ export default function EditorCanvasHost({

const [portal, setPortal] = React.useState<HTMLElement | null>(null);

const handleIframeLoad = React.useCallback<React.ReactEventHandler<HTMLIFrameElement>>(
(event) => {
invariant(event.currentTarget.contentDocument, 'iframe contentDocument is not available');
const root = event.currentTarget.contentDocument.getElementById('root');
invariant(root, 'root element not found');
setPortal(root);
},
[],
);
const appStateApi = useAppStateApi();

const handleIframeLoad = useEventCallback<React.ReactEventHandler<HTMLIFrameElement>>((event) => {
invariant(event.currentTarget.contentDocument, 'iframe contentDocument is not available');
const root = event.currentTarget.contentDocument.getElementById('root');
invariant(root, 'root element not found');

const iframeWindow = event.currentTarget.contentWindow;
invariant(iframeWindow, 'Iframe not attached');

const handleKeyDown = (keyDownEvent: KeyboardEvent) => {
const isZ = !!keyDownEvent.key && keyDownEvent.key.toLowerCase() === 'z';

const undoShortcut = isZ && (keyDownEvent.metaKey || keyDownEvent.ctrlKey);
const redoShortcut = undoShortcut && keyDownEvent.shiftKey;

if (redoShortcut) {
keyDownEvent.preventDefault();
appStateApi.redo();
} else if (undoShortcut) {
keyDownEvent.preventDefault();
appStateApi.undo();
}
};

iframeWindow.addEventListener('keydown', handleKeyDown);
iframeWindow.addEventListener('unload', () => {
iframeWindow.removeEventListener('keydown', handleKeyDown);
});

setPortal(root);
});

const viewState = React.useRef<PageViewState>({ nodes: {} });

Expand Down
Loading