[tiny] getSnapshot and loadSnapshot on Editor class (#3912)

I originally didn't want to add these methods to the Editor class, to
avoid muddying the API with multiple ways to do one thing, but I've
found myself reaching for these on a number of occasions so I think
maybe it would be better to have them?

### Change Type

<!--  Please select a 'Scope' label ️ -->

- [x] `sdk` — Changes the tldraw SDK
- [ ] `dotcom` — Changes the tldraw.com web app
- [ ] `docs` — Changes to the documentation, examples, or templates.
- [ ] `vs code` — Changes to the vscode plugin
- [ ] `internal` — Does not affect user-facing stuff

<!--  Please select a 'Type' label ️ -->

- [ ] `bugfix` — Bug fix
- [ ] `feature` — New feature
- [x] `improvement` — Improving existing features
- [ ] `chore` — Updating dependencies, other boring stuff
- [ ] `galaxy brain` — Architectural changes
- [ ] `tests` — Changes to any test code
- [ ] `tools` — Changes to infrastructure, CI, internal scripts,
debugging tools, etc.
- [ ] `dunno` — I don't know


### Test Plan

1. Add a step-by-step description of how to test your PR here.
2.

- [ ] Unit Tests
- [ ] End to end tests

### Release Notes

- Add a brief release note for your PR here.
This commit is contained in:
David Sheldrick 2024-06-16 18:12:37 +03:00 committed by GitHub
parent 764f26007f
commit 6d2f963f91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 0 deletions

View file

@ -1028,6 +1028,8 @@ export class Editor extends EventEmitter<TLEventMap> {
getShapeUtil<T extends ShapeUtil>(type: T extends ShapeUtil<infer R> ? R['type'] : string): T;
getSharedOpacity(): SharedStyle<number>;
getSharedStyles(): ReadonlySharedStyleMap;
// (undocumented)
getSnapshot(): TLEditorSnapshot;
getSortedChildIdsForParent(parent: TLPage | TLParentId | TLShape): TLShapeId[];
getStateDescendant<T extends StateNode>(path: string): T | undefined;
getStyleForNextShape<T>(style: StyleProp<T>): T;
@ -1105,6 +1107,7 @@ export class Editor extends EventEmitter<TLEventMap> {
isShapeOrAncestorLocked(shape?: TLShape): boolean;
// (undocumented)
isShapeOrAncestorLocked(id?: TLShapeId): boolean;
loadSnapshot(snapshot: Partial<TLEditorSnapshot> | TLStoreSnapshot): this;
mark(markId?: string): this;
moveShapesToPage(shapes: TLShape[] | TLShapeId[], pageId: TLPageId): this;
nudgeShapes(shapes: TLShape[] | TLShapeId[], offset: VecLike): this;

View file

@ -43,6 +43,7 @@ import {
TLShapeId,
TLShapePartial,
TLStore,
TLStoreSnapshot,
TLUnknownBinding,
TLUnknownShape,
TLVideoAsset,
@ -82,6 +83,7 @@ import {
import EventEmitter from 'eventemitter3'
import { flushSync } from 'react-dom'
import { createRoot } from 'react-dom/client'
import { TLEditorSnapshot, getSnapshot, loadSnapshot } from '../config/TLEditorSnapshot'
import { TLUser, createTLUser } from '../config/createTLUser'
import { checkBindings } from '../config/defaultBindings'
import { checkShapesAndAddCore } from '../config/defaultShapes'
@ -8481,6 +8483,24 @@ export class Editor extends EventEmitter<TLEventMap> {
return this.getInstanceState().isFocused
}
/**
* @public
* @returns a snapshot of the store's UI and document state
*/
getSnapshot() {
return getSnapshot(this.store)
}
/**
* Loads a snapshot into the editor.
* @param snapshot - the snapshot to load
* @returns
*/
loadSnapshot(snapshot: Partial<TLEditorSnapshot> | TLStoreSnapshot) {
loadSnapshot(this.store, snapshot)
return this
}
/**
* A manager for recording multiple click events.
*