fix history (#1009)

* fix history

* tweak withBatchedUpdates typing
This commit is contained in:
David Luzar 2020-03-19 14:51:05 +01:00 committed by GitHub
parent ca5f37850e
commit 82ce068972
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 86 additions and 80 deletions

View file

@ -105,11 +105,14 @@ import { isLinearElement } from "../element/typeChecks";
import { rescalePoints } from "../points";
import { actionFinalize } from "../actions";
/**
* @param func handler taking at most single parameter (event).
*/
function withBatchedUpdates<
TFunction extends ((event: any) => void) | (() => void)
>(func: TFunction) {
>(func: Parameters<TFunction>["length"] extends 0 | 1 ? TFunction : never) {
return (event => {
unstable_batchedUpdates(func, event);
unstable_batchedUpdates(func as TFunction, event);
}) as TFunction;
}
@ -164,30 +167,28 @@ export class App extends React.Component<any, AppState> {
this.actionManager.registerAction(createRedoAction(history));
}
private syncActionResult = withBatchedUpdates(
(res: ActionResult, commitToHistory: boolean = true) => {
if (this.unmounted) {
return;
}
if (res.elements) {
globalSceneState.replaceAllElements(res.elements);
if (commitToHistory) {
history.resumeRecording();
}
private syncActionResult = withBatchedUpdates((res: ActionResult) => {
if (this.unmounted) {
return;
}
if (res.elements) {
globalSceneState.replaceAllElements(res.elements);
if (res.commitToHistory) {
history.resumeRecording();
}
}
if (res.appState) {
if (commitToHistory) {
history.resumeRecording();
}
this.setState(state => ({
...res.appState,
isCollaborating: state.isCollaborating,
collaborators: state.collaborators,
}));
if (res.appState) {
if (res.commitToHistory) {
history.resumeRecording();
}
},
);
this.setState(state => ({
...res.appState,
isCollaborating: state.isCollaborating,
collaborators: state.collaborators,
}));
}
});
private onCut = withBatchedUpdates((event: ClipboardEvent) => {
if (isWritableElement(event.target)) {
@ -917,7 +918,11 @@ export class App extends React.Component<any, AppState> {
) {
loadFromBlob(file)
.then(({ elements, appState }) =>
this.syncActionResult({ elements, appState }),
this.syncActionResult({
elements,
appState,
commitToHistory: false,
}),
)
.catch(error => console.error(error));
}