@tldraw/editor
Version:
tldraw infinite canvas SDK (editor).
8 lines (7 loc) • 4.8 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/lib/config/TLEditorSnapshot.ts"],
"sourcesContent": ["import { Signal } from '@tldraw/state'\nimport { TLINSTANCE_ID, TLStore, TLStoreSnapshot, pluckPreservingValues } from '@tldraw/tlschema'\nimport { WeakCache, filterEntries } from '@tldraw/utils'\nimport {\n\tTLSessionStateSnapshot,\n\tcreateSessionStateSnapshotSignal,\n\tloadSessionStateSnapshotIntoStore,\n} from './TLSessionStateSnapshot'\n\n/** @public */\nexport interface TLEditorSnapshot {\n\tdocument: TLStoreSnapshot\n\tsession: TLSessionStateSnapshot\n}\n\n/**\n * Options for {@link loadSnapshot}\n * @public\n */\nexport interface TLLoadSnapshotOptions {\n\t/**\n\t * By default, some session state flags like `isDebugMode` are not overwritten when loading a snapshot.\n\t * These are usually considered \"sticky\" by users while the document data is not.\n\t * If you want to overwrite these flags, set this to `true`.\n\t */\n\tforceOverwriteSessionState?: boolean\n}\n\n/**\n * Loads a snapshot into a store.\n * @public\n */\nexport function loadSnapshot(\n\tstore: TLStore,\n\t_snapshot: Partial<TLEditorSnapshot> | TLStoreSnapshot,\n\topts?: TLLoadSnapshotOptions\n) {\n\tlet snapshot: Partial<TLEditorSnapshot> = {}\n\tif ('store' in _snapshot) {\n\t\t// regular old TLStoreSnapshot\n\t\t// let's migrate it and then filter out the non-doc state to help folks out\n\t\tconst migrationResult = store.schema.migrateStoreSnapshot(_snapshot)\n\t\tif (migrationResult.type !== 'success') {\n\t\t\tthrow new Error('Failed to migrate store snapshot: ' + migrationResult.reason)\n\t\t}\n\n\t\tsnapshot.document = {\n\t\t\tschema: store.schema.serialize(),\n\t\t\tstore: filterEntries(migrationResult.value, (_, { typeName }) =>\n\t\t\t\tstore.scopedTypes.document.has(typeName)\n\t\t\t),\n\t\t}\n\t} else {\n\t\t// TLEditorSnapshot\n\t\tsnapshot = _snapshot\n\t}\n\n\t// We need to preserve a bunch of instance state properties that the Editor sets\n\t// to avoid breaking the editor or causing jarring changes when loading a snapshot.\n\tconst preservingInstanceState = pluckPreservingValues(store.get(TLINSTANCE_ID))\n\tconst preservingSessionState = sessionStateCache\n\t\t.get(store, createSessionStateSnapshotSignal)\n\t\t.get()\n\n\tstore.atomic(() => {\n\t\t// first load the document state (this will wipe the store if it happens)\n\t\tif (snapshot.document) {\n\t\t\tstore.loadStoreSnapshot(snapshot.document)\n\t\t}\n\n\t\t// then make sure we preserve those instance state properties that must be preserved\n\t\t// this is a noop if the document state wasn't loaded above\n\t\tif (preservingInstanceState) {\n\t\t\tstore.update(TLINSTANCE_ID, (r) => ({ ...r, ...preservingInstanceState }))\n\t\t}\n\t\tif (preservingSessionState) {\n\t\t\t// there's some duplication here with the instanceState but it's fine\n\t\t\tloadSessionStateSnapshotIntoStore(store, preservingSessionState)\n\t\t}\n\n\t\t// finally reinstate the UI state\n\t\tif (snapshot.session) {\n\t\t\tloadSessionStateSnapshotIntoStore(store, snapshot.session, {\n\t\t\t\tforceOverwrite: opts?.forceOverwriteSessionState,\n\t\t\t})\n\t\t}\n\t})\n}\n\nconst sessionStateCache = new WeakCache<\n\tTLStore,\n\tSignal<TLSessionStateSnapshot | undefined | null>\n>()\n\n/** @public */\nexport function getSnapshot(store: TLStore): TLEditorSnapshot {\n\tconst sessionState$ = sessionStateCache.get(store, createSessionStateSnapshotSignal)\n\tconst session = sessionState$.get()\n\tif (!session) {\n\t\tthrow new Error('Session state is not ready yet')\n\t}\n\n\treturn {\n\t\tdocument: store.getStoreSnapshot(),\n\t\tsession,\n\t}\n}\n"],
"mappings": "AACA,SAAS,eAAyC,6BAA6B;AAC/E,SAAS,WAAW,qBAAqB;AACzC;AAAA,EAEC;AAAA,EACA;AAAA,OACM;AAyBA,SAAS,aACf,OACA,WACA,MACC;AACD,MAAI,WAAsC,CAAC;AAC3C,MAAI,WAAW,WAAW;AAGzB,UAAM,kBAAkB,MAAM,OAAO,qBAAqB,SAAS;A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