Skip to content

Commit a0c92cb

Browse files
committed
Improve undo/redo for theme edit
1 parent 432540e commit a0c92cb

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

demo/src/App.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ function App() {
9393

9494
const { liveData, loading, updateLiveData } = useDatabase()
9595

96-
const [{ present: data }, { set: setData, reset, undo, redo, canUndo, canRedo }] = useUndo(
97-
dataDefinition.data
98-
)
96+
const [
97+
{ present: data, past, future },
98+
{ set: setData, reset, undo: undoData, redo: redoData, canUndo, canRedo },
99+
] = useUndo(selectedDataSet === 'editTheme' ? themes.default : dataDefinition.data)
100+
// Provides a named version of these methods (i.e undo.name = "undo")
101+
const undo = () => undoData()
102+
const redo = () => redoData()
99103

100104
useEffect(() => {
101105
if (selectedDataSet === 'liveData' && !loading && liveData) reset(liveData)
@@ -182,6 +186,14 @@ function App() {
182186
}
183187
}
184188

189+
const handleHistory = (method: () => void) => {
190+
if (selectedDataSet === 'editTheme') {
191+
const theme = (method.name === 'undo' ? past.slice(-1)[0] : future[0]) as Theme
192+
updateState({ theme })
193+
}
194+
method()
195+
}
196+
185197
const handleReset = async () => {
186198
const newState = { ...state }
187199
newState.searchText = ''
@@ -296,8 +308,7 @@ function App() {
296308
if (result) return result
297309
else {
298310
const { newData } = nodeData
299-
if (selectedDataSet === 'editTheme')
300-
updateState({ theme: newData as ThemeName | Theme })
311+
if (selectedDataSet === 'editTheme') updateState({ theme: newData as Theme })
301312
}
302313
}}
303314
onEdit={dataDefinition?.onEdit ?? undefined}
@@ -358,7 +369,7 @@ function App() {
358369
<Button
359370
colorScheme="primaryScheme"
360371
leftIcon={<ArrowBackIcon />}
361-
onClick={() => undo()}
372+
onClick={() => handleHistory(undo)}
362373
isDisabled={!canUndo}
363374
>
364375
Undo
@@ -367,7 +378,7 @@ function App() {
367378
<Button
368379
colorScheme="primaryScheme"
369380
rightIcon={<ArrowForwardIcon />}
370-
onClick={() => redo()}
381+
onClick={() => handleHistory(redo)}
371382
isDisabled={!canRedo}
372383
>
373384
Redo

0 commit comments

Comments
 (0)