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

Add undo/redo #129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions packages/editor-sample/src/App/TemplatePanel/Redo/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, { useEffect } from 'react';

import { RedoOutlined as RedoIcon } from '@mui/icons-material';
import { IconButton, Tooltip } from '@mui/material';

import { redo, useCanRedo } from '../../../documents/editor/EditorContext';

export default function Redo() {
const canRedo = useCanRedo();
useEffect(() => {
function handle(event: KeyboardEvent) {
if (event.ctrlKey && event.key.toLowerCase() === 'y') {
event.preventDefault();
redo();
}
}

document.addEventListener('keydown', handle);
return () => document.removeEventListener('keydown', handle);
}, []);

return (
<>
<Tooltip title="Redo (Ctrl + Y)">
<span>
<IconButton onClick={redo} disabled={canRedo === false}>
<RedoIcon fontSize="small" />
</IconButton>
</span>
</Tooltip>
</>
);
}
33 changes: 33 additions & 0 deletions packages/editor-sample/src/App/TemplatePanel/Undo/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, { useEffect } from 'react';

import { UndoOutlined as UndoIcon } from '@mui/icons-material';
import { IconButton, Tooltip } from '@mui/material';

import { undo, useCanUndo } from '../../../documents/editor/EditorContext';

export default function Undo() {
const canUndo = useCanUndo();
useEffect(() => {
function handle(event: KeyboardEvent) {
if (event.ctrlKey && event.key.toLowerCase() === 'z') {
event.preventDefault();
undo();
}
}

document.addEventListener('keydown', handle);
return () => document.removeEventListener('keydown', handle);
}, []);

return (
<>
<Tooltip title="Undo (Ctrl + Z)">
<span>
<IconButton onClick={undo} disabled={canUndo === false}>
<UndoIcon fontSize="small" />
</IconButton>
</span>
</Tooltip>
</>
);
}
4 changes: 4 additions & 0 deletions packages/editor-sample/src/App/TemplatePanel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';

Check failure on line 1 in packages/editor-sample/src/App/TemplatePanel/index.tsx

View workflow job for this annotation

GitHub Actions / build

Run autofix to sort these imports!

import { MonitorOutlined, PhoneIphoneOutlined } from '@mui/icons-material';
import { Box, Stack, SxProps, ToggleButton, ToggleButtonGroup, Tooltip } from '@mui/material';
Expand All @@ -20,6 +20,8 @@
import JsonPanel from './JsonPanel';
import MainTabsGroup from './MainTabsGroup';
import ShareButton from './ShareButton';
import Undo from './Undo';
import Redo from './Redo';

export default function TemplatePanel() {
const document = useDocument();
Expand Down Expand Up @@ -97,6 +99,8 @@
<Stack direction="row" spacing={2}>
<DownloadJson />
<ImportJson />
<Undo />
<Redo />
<ToggleButtonGroup value={selectedScreenSize} exclusive size="small" onChange={handleScreenSizeChange}>
<ToggleButton value="desktop">
<Tooltip title="Desktop view">
Expand Down
69 changes: 64 additions & 5 deletions packages/editor-sample/src/documents/editor/EditorContext.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { create } from 'zustand';

Check failure on line 1 in packages/editor-sample/src/documents/editor/EditorContext.tsx

View workflow job for this annotation

GitHub Actions / build

Run autofix to sort these imports!

import debounce from '@mui/material/utils/debounce';
import getConfiguration from '../../getConfiguration';

import { TEditorConfiguration } from './core';

type TValue = {
document: TEditorConfiguration;

documentHistory: Array<TEditorConfiguration>,
documentHistoryIndex: number,

selectedBlockId: string | null;
selectedSidebarTab: 'block-configuration' | 'styles';
selectedMainTab: 'editor' | 'preview' | 'json' | 'html';
Expand All @@ -18,6 +21,8 @@

const editorStateStore = create<TValue>(() => ({
document: getConfiguration(window.location.hash),
documentHistory: [],
documentHistoryIndex: 0,
selectedBlockId: null,
selectedSidebarTab: 'styles',
selectedMainTab: 'editor',
Expand Down Expand Up @@ -76,7 +81,25 @@
return editorStateStore.setState({ selectedSidebarTab });
}

const addDocumentToHistory = debounce(function (document: TValue['document']) {
let documentHistory = editorStateStore.getState().documentHistory;
const currentIndex = editorStateStore.getState().documentHistoryIndex;
if (currentIndex < documentHistory.length - 1) {
documentHistory = documentHistory.slice(0, currentIndex + 1);
}
documentHistory.push(document);
if (documentHistory.length > 254) {
documentHistory = documentHistory.slice(1);
}

editorStateStore.setState({
documentHistory,
documentHistoryIndex: documentHistory.length - 1,
});
}, 500);

export function resetDocument(document: TValue['document']) {
addDocumentToHistory(document);
return editorStateStore.setState({
document,
selectedSidebarTab: 'styles',
Expand All @@ -86,11 +109,47 @@

export function setDocument(document: TValue['document']) {
const originalDocument = editorStateStore.getState().document;
const mergedDocument = {
...originalDocument,
...document,
};
addDocumentToHistory(mergedDocument);
return editorStateStore.setState({
document: {
...originalDocument,
...document,
},
document: mergedDocument,
});
}

export function useCanUndo() {
return editorStateStore(s => s.documentHistory.length > 0 && s.documentHistoryIndex > 0);
}

export function useCanRedo() {
return editorStateStore(s => s.documentHistory.length > 0 && s.documentHistoryIndex < s.documentHistory.length - 1);
}

export function undo() {
const documentHistory = editorStateStore.getState().documentHistory;
const currentIndex = editorStateStore.getState().documentHistoryIndex;
if (documentHistory.length <= 0 || currentIndex <= 0) {
return;
}
const changeToIndex = currentIndex - 1;
editorStateStore.setState({
document: documentHistory[changeToIndex],
documentHistoryIndex: changeToIndex,
});
}

export function redo() {
const documentHistory = editorStateStore.getState().documentHistory;
const currentIndex = editorStateStore.getState().documentHistoryIndex;
if (documentHistory.length <= 0 || currentIndex >= documentHistory.length - 1) {
return;
}
const changeToIndex = currentIndex + 1;
editorStateStore.setState({
document: documentHistory[changeToIndex],
documentHistoryIndex: changeToIndex,
});
}

Expand Down
Loading