refactor(app): centralize line snapline state sync
This commit is contained in:
@@ -784,6 +784,29 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
return api;
|
return api;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private withStableSnapLines<T extends { snapLines: AppState["snapLines"] }>(
|
||||||
|
state: T,
|
||||||
|
): T {
|
||||||
|
const snapLines = updateStable(this.state.snapLines, state.snapLines);
|
||||||
|
|
||||||
|
return snapLines === state.snapLines
|
||||||
|
? state
|
||||||
|
: {
|
||||||
|
...state,
|
||||||
|
snapLines,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private shouldUpdateSelectedLinearElementState(
|
||||||
|
selectedLinearElement: AppState["selectedLinearElement"],
|
||||||
|
snapLines: AppState["snapLines"],
|
||||||
|
) {
|
||||||
|
return (
|
||||||
|
selectedLinearElement !== this.state.selectedLinearElement ||
|
||||||
|
snapLines !== this.state.snapLines
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
constructor(props: AppProps) {
|
constructor(props: AppProps) {
|
||||||
super(props);
|
super(props);
|
||||||
const defaultAppState = getDefaultAppState();
|
const defaultAppState = getDefaultAppState();
|
||||||
@@ -6848,21 +6871,23 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
const { editingLinearElement, snapLines } = result;
|
const { editingLinearElement, snapLines } = result;
|
||||||
const nextSnapLines = updateStable(this.state.snapLines, snapLines);
|
const nextState = this.withStableSnapLines({
|
||||||
|
selectedLinearElement: editingLinearElement,
|
||||||
|
snapLines,
|
||||||
|
});
|
||||||
|
|
||||||
if (
|
if (
|
||||||
editingLinearElement &&
|
editingLinearElement &&
|
||||||
(editingLinearElement !== this.state.selectedLinearElement ||
|
this.shouldUpdateSelectedLinearElementState(
|
||||||
nextSnapLines !== this.state.snapLines)
|
nextState.selectedLinearElement,
|
||||||
|
nextState.snapLines,
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
// Since we are reading from previous state which is not possible with
|
// Since we are reading from previous state which is not possible with
|
||||||
// automatic batching in React 18 hence using flush sync to synchronously
|
// automatic batching in React 18 hence using flush sync to synchronously
|
||||||
// update the state. Check https://github.com/excalidraw/excalidraw/pull/5508 for more details.
|
// update the state. Check https://github.com/excalidraw/excalidraw/pull/5508 for more details.
|
||||||
flushSync(() => {
|
flushSync(() => {
|
||||||
this.setState({
|
this.setState(nextState);
|
||||||
selectedLinearElement: editingLinearElement,
|
|
||||||
snapLines: nextSnapLines,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
@@ -9728,17 +9753,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
pointerDownState.lastCoords.x = pointerCoords.x;
|
pointerDownState.lastCoords.x = pointerCoords.x;
|
||||||
pointerDownState.lastCoords.y = pointerCoords.y;
|
pointerDownState.lastCoords.y = pointerCoords.y;
|
||||||
pointerDownState.drag.hasOccurred = true;
|
pointerDownState.drag.hasOccurred = true;
|
||||||
const nextSnapLines = updateStable(
|
const nextState = this.withStableSnapLines(newState);
|
||||||
this.state.snapLines,
|
|
||||||
newState.snapLines,
|
|
||||||
);
|
|
||||||
const nextState =
|
|
||||||
nextSnapLines === newState.snapLines
|
|
||||||
? newState
|
|
||||||
: {
|
|
||||||
...newState,
|
|
||||||
snapLines: nextSnapLines,
|
|
||||||
};
|
|
||||||
|
|
||||||
// NOTE: Optimize setState calls because it
|
// NOTE: Optimize setState calls because it
|
||||||
// affects history and performance
|
// affects history and performance
|
||||||
|
|||||||
Reference in New Issue
Block a user