Slightly improve history performance

This commit is contained in:
Siklos 2022-08-12 19:07:32 +02:00
parent 704dab7307
commit ebde73bf10
3 changed files with 32 additions and 34 deletions

View file

@ -27,14 +27,15 @@ export function SelectContainer(
throw new Error('[SelectContainer] Cannot find container among children of main container!'); throw new Error('[SelectContainer] Cannot find container among children of main container!');
} }
setHistory(history.concat([{ history.push({
LastAction: `Select container ${selectedContainer.properties.id}`, LastAction: `Select container ${selectedContainer.properties.id}`,
MainContainer: mainContainerClone, MainContainer: mainContainerClone,
SelectedContainer: selectedContainer, SelectedContainer: selectedContainer,
SelectedContainerId: selectedContainer.properties.id, SelectedContainerId: selectedContainer.properties.id,
TypeCounters: Object.assign({}, current.TypeCounters) TypeCounters: Object.assign({}, current.TypeCounters)
}])); });
setHistoryCurrentStep(history.length); setHistory(history);
setHistoryCurrentStep(history.length - 1);
} }
/** /**
@ -87,14 +88,15 @@ export function DeleteContainer(
container.parent; container.parent;
const SelectedContainerId = SelectedContainer.properties.id; const SelectedContainerId = SelectedContainer.properties.id;
setHistory(history.concat([{ history.push({
LastAction: `Delete container ${containerId}`, LastAction: `Delete container ${containerId}`,
MainContainer: mainContainerClone, MainContainer: mainContainerClone,
SelectedContainer, SelectedContainer,
SelectedContainerId, SelectedContainerId,
TypeCounters: Object.assign({}, current.TypeCounters) TypeCounters: Object.assign({}, current.TypeCounters)
}])); });
setHistoryCurrentStep(history.length); setHistory(history);
setHistoryCurrentStep(history.length - 1);
} }
/** /**
@ -235,12 +237,13 @@ export function AddContainer(
} }
// Update the state // Update the state
setHistory(history.concat([{ history.push({
LastAction: 'Add container', LastAction: 'Add container',
MainContainer: clone, MainContainer: clone,
SelectedContainer: parentClone, SelectedContainer: parentClone,
SelectedContainerId: parentClone.properties.id, SelectedContainerId: parentClone.properties.id,
TypeCounters: newCounters TypeCounters: newCounters
}])); });
setHistoryCurrentStep(history.length); setHistory(history);
setHistoryCurrentStep(history.length - 1);
} }

View file

@ -51,14 +51,15 @@ export function OnPropertyChange(
RecalculatePhysics(container); RecalculatePhysics(container);
} }
setHistory(history.concat([{ history.push({
LastAction: `Change property of container ${container.properties.id}`, LastAction: `Change ${key} of ${container.properties.id}`,
MainContainer: mainContainerClone, MainContainer: mainContainerClone,
SelectedContainer: container, SelectedContainer: container,
SelectedContainerId: container.properties.id, SelectedContainerId: container.properties.id,
TypeCounters: Object.assign({}, current.TypeCounters) TypeCounters: Object.assign({}, current.TypeCounters)
}])); });
setHistoryCurrentStep(history.length); setHistory(history);
setHistoryCurrentStep(history.length - 1);
} }
/** /**
@ -107,12 +108,13 @@ export function OnPropertiesSubmit(
RecalculatePhysics(container); RecalculatePhysics(container);
} }
setHistory(history.concat([{ history.push({
LastAction: `Change property of container ${container.properties.id}`, LastAction: `Change properties of ${container.properties.id}`,
MainContainer: mainContainerClone, MainContainer: mainContainerClone,
SelectedContainer: container, SelectedContainer: container,
SelectedContainerId: container.properties.id, SelectedContainerId: container.properties.id,
TypeCounters: Object.assign({}, current.TypeCounters) TypeCounters: Object.assign({}, current.TypeCounters)
}])); });
setHistoryCurrentStep(history.length); setHistory(history);
setHistoryCurrentStep(history.length - 1);
} }

View file

@ -1,5 +1,5 @@
import * as React from 'react'; import * as React from 'react';
import { IHistoryState } from "../../Interfaces/IHistoryState"; import { IHistoryState } from '../../Interfaces/IHistoryState';
interface IHistoryProps { interface IHistoryProps {
history: IHistoryState[] history: IHistoryState[]
@ -10,38 +10,31 @@ interface IHistoryProps {
export const History: React.FC<IHistoryProps> = (props: IHistoryProps) => { export const History: React.FC<IHistoryProps> = (props: IHistoryProps) => {
const isOpenClasses = props.isOpen ? 'right-0' : '-right-64'; const isOpenClasses = props.isOpen ? 'right-0' : '-right-64';
const states: JSX.Element[] = [];
const states = props.history.map((step, move) => { for (let move = props.history.length - 1; move >= 0; move--) {
const step = props.history[move];
const desc = move > 0 const desc = move > 0
? `Go to modification n°${move}` ? `${move}: ${step.LastAction}`
: 'Go to the beginning'; : 'Go to the beginning';
const isCurrent = move === props.historyCurrentStep; const selectedClass = move === props.historyCurrentStep
const selectedClass = isCurrent
? 'bg-blue-500 hover:bg-blue-600' ? 'bg-blue-500 hover:bg-blue-600'
: 'bg-slate-500 hover:bg-slate-700'; : 'bg-slate-500 hover:bg-slate-700';
const isCurrentText = isCurrent states.push(
? ' (current)'
: '';
return (
<button <button
key={move} key={move}
onClick={() => props.jumpTo(move)} onClick={() => props.jumpTo(move)}
title={step.LastAction}
className={ className={
`w-full elements-sidebar-row whitespace-pre `w-full elements-sidebar-row whitespace-pre
text-left text-sm font-medium transition-all ${selectedClass}` text-left text-sm font-medium transition-all ${selectedClass}`
} }
> >
{desc}{isCurrentText} {desc}
</button> </button>
); );
}); }
// recent first
states.reverse();
return ( return (
<div className={`fixed flex flex-col bg-slate-300 text-white transition-all h-screen w-64 overflow-y-auto z-20 ${isOpenClasses}`}> <div className={`fixed flex flex-col bg-slate-300 text-white transition-all h-screen w-64 overflow-y-auto z-20 ${isOpenClasses}`}>