mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-11-12 00:34:21 +01:00
feat: apply deltas API (#9869)
This commit is contained in:
@@ -233,6 +233,8 @@ import {
|
||||
hitElementBoundingBox,
|
||||
isLineElement,
|
||||
isSimpleArrow,
|
||||
StoreDelta,
|
||||
type ApplyToOptions,
|
||||
} from "@excalidraw/element";
|
||||
|
||||
import type { LocalPoint, Radians } from "@excalidraw/math";
|
||||
@@ -259,6 +261,7 @@ import type {
|
||||
MagicGenerationData,
|
||||
ExcalidrawArrowElement,
|
||||
ExcalidrawElbowArrowElement,
|
||||
SceneElementsMap,
|
||||
} from "@excalidraw/element/types";
|
||||
|
||||
import type { Mutable, ValueOf } from "@excalidraw/common/utility-types";
|
||||
@@ -697,6 +700,7 @@ class App extends React.Component<AppProps, AppState> {
|
||||
if (excalidrawAPI) {
|
||||
const api: ExcalidrawImperativeAPI = {
|
||||
updateScene: this.updateScene,
|
||||
applyDeltas: this.applyDeltas,
|
||||
mutateElement: this.mutateElement,
|
||||
updateLibrary: this.library.updateLibrary,
|
||||
addFiles: this.addFiles,
|
||||
@@ -3938,6 +3942,27 @@ class App extends React.Component<AppProps, AppState> {
|
||||
},
|
||||
);
|
||||
|
||||
public applyDeltas = (
|
||||
deltas: StoreDelta[],
|
||||
options?: ApplyToOptions,
|
||||
): [SceneElementsMap, AppState, boolean] => {
|
||||
// squash all deltas together, starting with a fresh new delta instance
|
||||
const aggregatedDelta = StoreDelta.squash(...deltas);
|
||||
|
||||
// create new instance of elements map & appState, so we don't accidentaly mutate existing ones
|
||||
const nextAppState = { ...this.state };
|
||||
const nextElements = new Map(
|
||||
this.scene.getElementsMapIncludingDeleted(),
|
||||
) as SceneElementsMap;
|
||||
|
||||
return StoreDelta.applyTo(
|
||||
aggregatedDelta,
|
||||
nextElements,
|
||||
nextAppState,
|
||||
options,
|
||||
);
|
||||
};
|
||||
|
||||
public mutateElement = <TElement extends Mutable<ExcalidrawElement>>(
|
||||
element: TElement,
|
||||
updates: ElementUpdate<TElement>,
|
||||
|
||||
Reference in New Issue
Block a user