@tldraw/editor 3.8.0-canary.ab23d1d97da1 → 3.8.0-canary.ab782583b3ba

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/dist-cjs/index.d.ts +168 -13
  2. package/dist-cjs/index.js +7 -1
  3. package/dist-cjs/index.js.map +2 -2
  4. package/dist-cjs/lib/TldrawEditor.js +4 -2
  5. package/dist-cjs/lib/TldrawEditor.js.map +2 -2
  6. package/dist-cjs/lib/components/default-components/DefaultCanvas.js +2 -5
  7. package/dist-cjs/lib/components/default-components/DefaultCanvas.js.map +2 -2
  8. package/dist-cjs/lib/config/createTLStore.js +4 -2
  9. package/dist-cjs/lib/config/createTLStore.js.map +2 -2
  10. package/dist-cjs/lib/editor/Editor.js +95 -21
  11. package/dist-cjs/lib/editor/Editor.js.map +2 -2
  12. package/dist-cjs/lib/editor/shapes/ShapeUtil.js.map +2 -2
  13. package/dist-cjs/lib/editor/shapes/shared/resizeScaled.js +66 -0
  14. package/dist-cjs/lib/editor/shapes/shared/resizeScaled.js.map +7 -0
  15. package/dist-cjs/lib/editor/types/SvgExportContext.js.map +2 -2
  16. package/dist-cjs/lib/editor/types/misc-types.js.map +1 -1
  17. package/dist-cjs/lib/exports/exportToSvg.js.map +2 -2
  18. package/dist-cjs/lib/exports/getSvgAsImage.js +83 -0
  19. package/dist-cjs/lib/exports/getSvgAsImage.js.map +7 -0
  20. package/dist-cjs/lib/exports/getSvgJsx.js +16 -3
  21. package/dist-cjs/lib/exports/getSvgJsx.js.map +2 -2
  22. package/dist-cjs/lib/hooks/useLocalStore.js +1 -1
  23. package/dist-cjs/lib/hooks/useLocalStore.js.map +2 -2
  24. package/dist-cjs/lib/options.js +2 -1
  25. package/dist-cjs/lib/options.js.map +2 -2
  26. package/dist-cjs/lib/utils/browserCanvasMaxSize.js +75 -0
  27. package/dist-cjs/lib/utils/browserCanvasMaxSize.js.map +7 -0
  28. package/dist-cjs/lib/utils/sync/TLLocalSyncClient.js +3 -1
  29. package/dist-cjs/lib/utils/sync/TLLocalSyncClient.js.map +2 -2
  30. package/dist-cjs/version.js +3 -3
  31. package/dist-cjs/version.js.map +1 -1
  32. package/dist-esm/index.d.mts +168 -13
  33. package/dist-esm/index.mjs +7 -1
  34. package/dist-esm/index.mjs.map +2 -2
  35. package/dist-esm/lib/TldrawEditor.mjs +4 -2
  36. package/dist-esm/lib/TldrawEditor.mjs.map +2 -2
  37. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs +2 -5
  38. package/dist-esm/lib/components/default-components/DefaultCanvas.mjs.map +2 -2
  39. package/dist-esm/lib/config/createTLStore.mjs +4 -2
  40. package/dist-esm/lib/config/createTLStore.mjs.map +2 -2
  41. package/dist-esm/lib/editor/Editor.mjs +95 -21
  42. package/dist-esm/lib/editor/Editor.mjs.map +2 -2
  43. package/dist-esm/lib/editor/shapes/ShapeUtil.mjs.map +2 -2
  44. package/dist-esm/lib/editor/shapes/shared/resizeScaled.mjs +46 -0
  45. package/dist-esm/lib/editor/shapes/shared/resizeScaled.mjs.map +7 -0
  46. package/dist-esm/lib/editor/types/SvgExportContext.mjs.map +2 -2
  47. package/dist-esm/lib/exports/exportToSvg.mjs.map +2 -2
  48. package/dist-esm/lib/exports/getSvgAsImage.mjs +63 -0
  49. package/dist-esm/lib/exports/getSvgAsImage.mjs.map +7 -0
  50. package/dist-esm/lib/exports/getSvgJsx.mjs +16 -3
  51. package/dist-esm/lib/exports/getSvgJsx.mjs.map +2 -2
  52. package/dist-esm/lib/hooks/useLocalStore.mjs +1 -1
  53. package/dist-esm/lib/hooks/useLocalStore.mjs.map +2 -2
  54. package/dist-esm/lib/options.mjs +2 -1
  55. package/dist-esm/lib/options.mjs.map +2 -2
  56. package/dist-esm/lib/utils/browserCanvasMaxSize.mjs +45 -0
  57. package/dist-esm/lib/utils/browserCanvasMaxSize.mjs.map +7 -0
  58. package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs +3 -1
  59. package/dist-esm/lib/utils/sync/TLLocalSyncClient.mjs.map +2 -2
  60. package/dist-esm/version.mjs +3 -3
  61. package/dist-esm/version.mjs.map +1 -1
  62. package/editor.css +2 -1
  63. package/package.json +9 -7
  64. package/src/index.ts +6 -0
  65. package/src/lib/TldrawEditor.tsx +2 -0
  66. package/src/lib/components/default-components/DefaultCanvas.tsx +2 -5
  67. package/src/lib/config/createTLStore.ts +4 -2
  68. package/src/lib/editor/Editor.ts +129 -39
  69. package/src/lib/editor/shapes/ShapeUtil.ts +30 -1
  70. package/src/lib/editor/shapes/shared/resizeScaled.ts +61 -0
  71. package/src/lib/editor/types/SvgExportContext.tsx +21 -0
  72. package/src/lib/editor/types/misc-types.ts +55 -2
  73. package/src/lib/exports/exportToSvg.tsx +2 -2
  74. package/src/lib/exports/getSvgAsImage.ts +92 -0
  75. package/src/lib/exports/getSvgJsx.tsx +17 -2
  76. package/src/lib/hooks/useLocalStore.ts +1 -1
  77. package/src/lib/options.ts +6 -0
  78. package/src/lib/utils/browserCanvasMaxSize.ts +65 -0
  79. package/src/lib/utils/sync/TLLocalSyncClient.ts +3 -1
  80. package/src/version.ts +3 -3
@@ -131,7 +131,9 @@ class TLLocalSyncClient {
131
131
  });
132
132
  }
133
133
  if (sessionStateSnapshot) {
134
- loadSessionStateSnapshotIntoStore(this.store, sessionStateSnapshot);
134
+ loadSessionStateSnapshotIntoStore(this.store, sessionStateSnapshot, {
135
+ forceOverwrite: true
136
+ });
135
137
  }
136
138
  }
137
139
  this.channel.onmessage = ({ data: data2 }) => {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/lib/utils/sync/TLLocalSyncClient.ts"],
4
- "sourcesContent": ["import { Signal, transact } from '@tldraw/state'\nimport { RecordsDiff, SerializedSchema, UnknownRecord, squashRecordDiffs } from '@tldraw/store'\nimport { TLStore } from '@tldraw/tlschema'\nimport { assert } from '@tldraw/utils'\nimport {\n\tTAB_ID,\n\tTLSessionStateSnapshot,\n\tcreateSessionStateSnapshotSignal,\n\textractSessionStateFromLegacySnapshot,\n\tloadSessionStateSnapshotIntoStore,\n} from '../../config/TLSessionStateSnapshot'\nimport { LocalIndexedDb } from './LocalIndexedDb'\nimport { showCantReadFromIndexDbAlert, showCantWriteToIndexDbAlert } from './alerts'\n\n/** How should we debounce persists? */\nconst PERSIST_THROTTLE_MS = 350\n/** If we're in an error state, how long should we wait before retrying a write? */\nconst PERSIST_RETRY_THROTTLE_MS = 10_000\n\nconst UPDATE_INSTANCE_STATE = Symbol('UPDATE_INSTANCE_STATE')\n\n/**\n * IMPORTANT!!!\n *\n * This is just a quick-n-dirty temporary solution that will be replaced with the remote sync client\n * once it has the db integrated\n */\n\ninterface SyncMessage {\n\ttype: 'diff'\n\tstoreId: string\n\tchanges: RecordsDiff<UnknownRecord>\n\tschema: SerializedSchema\n}\n\n// Sent by new clients when they connect\n// If another client is on the channel with a newer schema version\n// It will\ninterface AnnounceMessage {\n\ttype: 'announce'\n\tschema: SerializedSchema\n}\n\ntype Message = SyncMessage | AnnounceMessage\n\ntype UnpackPromise<T> = T extends Promise<infer U> ? U : T\n\nconst msg = (msg: Message) => msg\n\n/** @internal */\nexport class BroadcastChannelMock {\n\tonmessage?: (e: MessageEvent) => void\n\tconstructor(_name: string) {\n\t\t// noop\n\t}\n\tpostMessage(_msg: Message) {\n\t\t// noop\n\t}\n\tclose() {\n\t\t// noop\n\t}\n}\n\nconst BC = typeof BroadcastChannel === 'undefined' ? BroadcastChannelMock : BroadcastChannel\n\n/** @internal */\nexport class TLLocalSyncClient {\n\tprivate disposables = new Set<() => void>()\n\tprivate diffQueue: Array<RecordsDiff<UnknownRecord> | typeof UPDATE_INSTANCE_STATE> = []\n\tprivate didDispose = false\n\tprivate shouldDoFullDBWrite = true\n\tprivate isReloading = false\n\treadonly persistenceKey: string\n\treadonly sessionId: string\n\treadonly serializedSchema: SerializedSchema\n\tprivate isDebugging = false\n\tprivate readonly documentTypes: ReadonlySet<string>\n\tprivate readonly $sessionStateSnapshot: Signal<TLSessionStateSnapshot | null>\n\t/** @internal */\n\treadonly db: LocalIndexedDb\n\n\tinitTime = Date.now()\n\tprivate debug(...args: any[]) {\n\t\tif (this.isDebugging) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.debug(...args)\n\t\t}\n\t}\n\tconstructor(\n\t\tpublic readonly store: TLStore,\n\t\t{\n\t\t\tpersistenceKey,\n\t\t\tsessionId = TAB_ID,\n\t\t\tonLoad,\n\t\t\tonLoadError,\n\t\t}: {\n\t\t\tpersistenceKey: string\n\t\t\tsessionId?: string\n\t\t\tonLoad(self: TLLocalSyncClient): void\n\t\t\tonLoadError(error: Error): void\n\t\t},\n\t\tpublic readonly channel = new BC(`tldraw-tab-sync-${persistenceKey}`)\n\t) {\n\t\tif (typeof window !== 'undefined') {\n\t\t\t;(window as any).tlsync = this\n\t\t}\n\t\tthis.persistenceKey = persistenceKey\n\t\tthis.sessionId = sessionId\n\t\tthis.db = new LocalIndexedDb(persistenceKey)\n\t\tthis.disposables.add(() => this.db.close())\n\n\t\tthis.serializedSchema = this.store.schema.serialize()\n\t\tthis.$sessionStateSnapshot = createSessionStateSnapshotSignal(this.store)\n\n\t\tthis.disposables.add(\n\t\t\t// Set up a subscription to changes from the store: When\n\t\t\t// the store changes (and if the change was made by the user)\n\t\t\t// then immediately send the diff to other tabs via postMessage\n\t\t\t// and schedule a persist.\n\t\t\tstore.listen(\n\t\t\t\t({ changes }) => {\n\t\t\t\t\tthis.diffQueue.push(changes)\n\t\t\t\t\tthis.channel.postMessage(\n\t\t\t\t\t\tmsg({\n\t\t\t\t\t\t\ttype: 'diff',\n\t\t\t\t\t\t\tstoreId: this.store.id,\n\t\t\t\t\t\t\tchanges,\n\t\t\t\t\t\t\tschema: this.serializedSchema,\n\t\t\t\t\t\t})\n\t\t\t\t\t)\n\t\t\t\t\tthis.schedulePersist()\n\t\t\t\t},\n\t\t\t\t{ source: 'user', scope: 'document' }\n\t\t\t)\n\t\t)\n\t\tthis.disposables.add(\n\t\t\tstore.listen(\n\t\t\t\t() => {\n\t\t\t\t\tthis.diffQueue.push(UPDATE_INSTANCE_STATE)\n\t\t\t\t\tthis.schedulePersist()\n\t\t\t\t},\n\t\t\t\t{ scope: 'session' }\n\t\t\t)\n\t\t)\n\n\t\tthis.connect(onLoad, onLoadError)\n\n\t\tthis.documentTypes = new Set(\n\t\t\tObject.values(this.store.schema.types)\n\t\t\t\t.filter((t) => t.scope === 'document')\n\t\t\t\t.map((t) => t.typeName)\n\t\t)\n\t}\n\n\tprivate async connect(onLoad: (client: this) => void, onLoadError: (error: Error) => void) {\n\t\tthis.debug('connecting')\n\t\tlet data: UnpackPromise<ReturnType<LocalIndexedDb['load']>> | undefined\n\n\t\ttry {\n\t\t\tdata = await this.db.load({ sessionId: this.sessionId })\n\t\t} catch (error: any) {\n\t\t\tonLoadError(error)\n\t\t\tshowCantReadFromIndexDbAlert()\n\t\t\treturn\n\t\t}\n\n\t\tthis.debug('loaded data from store', data, 'didDispose', this.didDispose)\n\t\tif (this.didDispose) return\n\n\t\ttry {\n\t\t\tif (data) {\n\t\t\t\tconst documentSnapshot = Object.fromEntries(data.records.map((r) => [r.id, r]))\n\t\t\t\tconst sessionStateSnapshot =\n\t\t\t\t\tdata.sessionStateSnapshot ?? extractSessionStateFromLegacySnapshot(documentSnapshot)\n\t\t\t\tconst migrationResult = this.store.schema.migrateStoreSnapshot({\n\t\t\t\t\tstore: documentSnapshot,\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-deprecated\n\t\t\t\t\tschema: data.schema ?? this.store.schema.serializeEarliestVersion(),\n\t\t\t\t})\n\n\t\t\t\tif (migrationResult.type === 'error') {\n\t\t\t\t\tconsole.error('failed to migrate store', migrationResult)\n\t\t\t\t\tonLoadError(new Error(`Failed to migrate store: ${migrationResult.reason}`))\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tconst records = Object.values(migrationResult.value).filter((r) =>\n\t\t\t\t\tthis.documentTypes.has(r.typeName)\n\t\t\t\t)\n\t\t\t\tif (records.length > 0) {\n\t\t\t\t\t// 3. Merge the changes into the REAL STORE\n\t\t\t\t\tthis.store.mergeRemoteChanges(() => {\n\t\t\t\t\t\t// Calling put will validate the records!\n\t\t\t\t\t\tthis.store.put(records, 'initialize')\n\t\t\t\t\t})\n\t\t\t\t}\n\n\t\t\t\tif (sessionStateSnapshot) {\n\t\t\t\t\tloadSessionStateSnapshotIntoStore(this.store, sessionStateSnapshot)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthis.channel.onmessage = ({ data }) => {\n\t\t\t\tthis.debug('got message', data)\n\t\t\t\tconst msg = data as Message\n\t\t\t\t// if their schema is earlier than ours, we need to tell them so they can refresh\n\t\t\t\t// if their schema is later than ours, we need to refresh\n\t\t\t\tconst res = this.store.schema.getMigrationsSince(msg.schema)\n\n\t\t\t\tif (!res.ok) {\n\t\t\t\t\t// we are older, refresh\n\t\t\t\t\t// but add a safety check to make sure we don't get in an infinite loop\n\t\t\t\t\tconst timeSinceInit = Date.now() - this.initTime\n\t\t\t\t\tif (timeSinceInit < 5000) {\n\t\t\t\t\t\t// This tab was just reloaded, but is out of date compared to other tabs.\n\t\t\t\t\t\t// Not expecting this to ever happen. It should only happen if we roll back a release that incremented\n\t\t\t\t\t\t// the schema version (which we should never do)\n\t\t\t\t\t\t// Or maybe during development if you have multiple local tabs open running the app on prod mode and you\n\t\t\t\t\t\t// check out an older commit. Dev server should be fine.\n\t\t\t\t\t\tonLoadError(new Error('Schema mismatch, please close other tabs and reload the page'))\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\t\t\t\t\tthis.debug('reloading')\n\t\t\t\t\tthis.isReloading = true\n\t\t\t\t\twindow?.location?.reload?.()\n\t\t\t\t\treturn\n\t\t\t\t} else if (res.value.length > 0) {\n\t\t\t\t\t// they are older, tell them to refresh and not write any more data\n\t\t\t\t\tthis.debug('telling them to reload')\n\t\t\t\t\tthis.channel.postMessage({ type: 'announce', schema: this.serializedSchema })\n\t\t\t\t\t// schedule a full db write in case they wrote data anyway\n\t\t\t\t\tthis.shouldDoFullDBWrite = true\n\t\t\t\t\tthis.persistIfNeeded()\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\t// otherwise, all good, same version :)\n\t\t\t\tif (msg.type === 'diff') {\n\t\t\t\t\tthis.debug('applying diff')\n\t\t\t\t\ttransact(() => {\n\t\t\t\t\t\tthis.store.mergeRemoteChanges(() => {\n\t\t\t\t\t\t\tthis.store.applyDiff(msg.changes as any)\n\t\t\t\t\t\t})\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\t\t\tthis.channel.postMessage({ type: 'announce', schema: this.serializedSchema })\n\t\t\tthis.disposables.add(() => {\n\t\t\t\tthis.channel.close()\n\t\t\t})\n\t\t\tonLoad(this)\n\t\t} catch (e: any) {\n\t\t\tthis.debug('error loading data from store', e)\n\t\t\tif (this.didDispose) return\n\t\t\tonLoadError(e)\n\t\t\treturn\n\t\t}\n\t}\n\n\tclose() {\n\t\tthis.debug('closing')\n\t\tthis.didDispose = true\n\t\tthis.disposables.forEach((d) => d())\n\t}\n\n\tprivate isPersisting = false\n\tprivate didLastWriteError = false\n\t// eslint-disable-next-line no-restricted-globals\n\tprivate scheduledPersistTimeout: ReturnType<typeof setTimeout> | null = null\n\n\t/**\n\t * Schedule a persist. Persists don't happen immediately: they are throttled to avoid writing too\n\t * often, and will retry if failed.\n\t *\n\t * @internal\n\t */\n\tprivate schedulePersist() {\n\t\tthis.debug('schedulePersist', this.scheduledPersistTimeout)\n\t\tif (this.scheduledPersistTimeout) return\n\t\t// eslint-disable-next-line no-restricted-globals\n\t\tthis.scheduledPersistTimeout = setTimeout(\n\t\t\t() => {\n\t\t\t\tthis.scheduledPersistTimeout = null\n\t\t\t\tthis.persistIfNeeded()\n\t\t\t},\n\t\t\tthis.didLastWriteError ? PERSIST_RETRY_THROTTLE_MS : PERSIST_THROTTLE_MS\n\t\t)\n\t}\n\n\t/**\n\t * Persist to IndexedDB only under certain circumstances:\n\t *\n\t * - If we're not already persisting\n\t * - If we're not reloading the page\n\t * - And we have something to persist (a full db write scheduled or changes in the diff queue)\n\t *\n\t * @internal\n\t */\n\tprivate persistIfNeeded() {\n\t\tthis.debug('persistIfNeeded', {\n\t\t\tisPersisting: this.isPersisting,\n\t\t\tisReloading: this.isReloading,\n\t\t\tshouldDoFullDBWrite: this.shouldDoFullDBWrite,\n\t\t\tdiffQueueLength: this.diffQueue.length,\n\t\t\tstoreIsPossiblyCorrupt: this.store.isPossiblyCorrupted(),\n\t\t})\n\n\t\t// if we've scheduled a persist for the future, that's no longer needed\n\t\tif (this.scheduledPersistTimeout) {\n\t\t\tclearTimeout(this.scheduledPersistTimeout)\n\t\t\tthis.scheduledPersistTimeout = null\n\t\t}\n\n\t\t// if a persist is already in progress, we don't need to do anything -\n\t\t// if there are still outstanding changes once it's finished, it'll\n\t\t// schedule another persist\n\t\tif (this.isPersisting) return\n\n\t\t// if we're reloading the page, it's because there's a newer client\n\t\t// present so lets not overwrite their changes\n\t\tif (this.isReloading) return\n\n\t\t// if the store is possibly corrupted, we don't want to persist\n\t\tif (this.store.isPossiblyCorrupted()) return\n\n\t\t// if we're scheduled for a full write or if we have changes outstanding, let's persist them!\n\t\tif (this.shouldDoFullDBWrite || this.diffQueue.length > 0) {\n\t\t\tthis.doPersist()\n\t\t}\n\t}\n\n\t/**\n\t * Actually persist to IndexedDB. If the write fails, then we'll retry with a full db write after\n\t * a short delay.\n\t */\n\tprivate async doPersist() {\n\t\tassert(!this.isPersisting, 'persist already in progress')\n\t\tif (this.didDispose) return\n\t\tthis.isPersisting = true\n\n\t\tthis.debug('doPersist start')\n\n\t\t// instantly empty the diff queue, but keep our own copy of it. this way\n\t\t// diffs that come in during the persist will still get tracked\n\t\tconst diffQueue = this.diffQueue\n\t\tthis.diffQueue = []\n\n\t\ttry {\n\t\t\tif (this.shouldDoFullDBWrite) {\n\t\t\t\tthis.shouldDoFullDBWrite = false\n\t\t\t\tawait this.db.storeSnapshot({\n\t\t\t\t\tschema: this.store.schema,\n\t\t\t\t\tsnapshot: this.store.serialize(),\n\t\t\t\t\tsessionId: this.sessionId,\n\t\t\t\t\tsessionStateSnapshot: this.$sessionStateSnapshot.get(),\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tconst diffs = squashRecordDiffs(\n\t\t\t\t\tdiffQueue.filter((d): d is RecordsDiff<UnknownRecord> => d !== UPDATE_INSTANCE_STATE)\n\t\t\t\t)\n\t\t\t\tawait this.db.storeChanges({\n\t\t\t\t\tchanges: diffs,\n\t\t\t\t\tschema: this.store.schema,\n\t\t\t\t\tsessionId: this.sessionId,\n\t\t\t\t\tsessionStateSnapshot: this.$sessionStateSnapshot.get(),\n\t\t\t\t})\n\t\t\t}\n\t\t\tthis.didLastWriteError = false\n\t\t} catch (e) {\n\t\t\t// set this.shouldDoFullDBWrite because we clear the diffQueue no matter what,\n\t\t\t// so if this is just a temporary error, we will still persist all changes\n\t\t\tthis.shouldDoFullDBWrite = true\n\t\t\tthis.didLastWriteError = true\n\t\t\tconsole.error('failed to store changes in indexed db', e)\n\n\t\t\tshowCantWriteToIndexDbAlert()\n\t\t\tif (typeof window !== 'undefined') {\n\t\t\t\t// adios\n\t\t\t\twindow.location.reload()\n\t\t\t}\n\t\t}\n\n\t\tthis.isPersisting = false\n\t\tthis.debug('doPersist end')\n\n\t\t// changes might have come in between when we started the persist and\n\t\t// now. we request another persist so any new changes can get written\n\t\tthis.schedulePersist()\n\t}\n}\n"],
5
- "mappings": "AAAA,SAAiB,gBAAgB;AACjC,SAAuD,yBAAyB;AAEhF,SAAS,cAAc;AACvB;AAAA,EACC;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,sBAAsB;AAC/B,SAAS,8BAA8B,mCAAmC;AAG1E,MAAM,sBAAsB;AAE5B,MAAM,4BAA4B;AAElC,MAAM,wBAAwB,OAAO,uBAAuB;AA4B5D,MAAM,M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D,WAAK,UAAU;AAAA,IAChB;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAc,YAAY;AACzB,WAAO,CAAC,KAAK,cAAc,6BAA6B;AACxD,QAAI,KAAK,WAAY;AACrB,SAAK,eAAe;AAEpB,SAAK,MAAM,iBAAiB;AAI5B,UAAM,YAAY,KAAK;AACvB,SAAK,YAAY,CAAC;AAElB,QAAI;AACH,UAAI,KAAK,qBAAqB;AAC7B,aAAK,sBAAsB;AAC3B,cAAM,KAAK,GAAG,cAAc;AAAA,UAC3B,QAAQ,KAAK,MAAM;AAAA,UACnB,UAAU,KAAK,MAAM,UAAU;AAAA,UAC/B,WAAW,KAAK;AAAA,UAChB,sBAAsB,KAAK,sBAAsB,IAAI;AAAA,QACtD,CAAC;AAAA,MACF,OAAO;AACN,cAAM,QAAQ;AAAA,UACb,UAAU,OAAO,CAAC,MAAuC,MAAM,qBAAqB;AAAA,QACrF;AACA,cAAM,KAAK,GAAG,aAAa;AAAA,UAC1B,SAAS;AAAA,UACT,QAAQ,KAAK,MAAM;AAAA,UACnB,WAAW,KAAK;AAAA,UAChB,sBAAsB,KAAK,sBAAsB,IAAI;AAAA,QACtD,CAAC;AAAA,MACF;AACA,WAAK,oBAAoB;AAAA,IAC1B,SAAS,GAAG;AAGX,WAAK,sBAAsB;AAC3B,WAAK,oBAAoB;AACzB,cAAQ,MAAM,yCAAyC,CAAC;AAExD,kCAA4B;AAC5B,UAAI,OAAO,WAAW,aAAa;AAElC,eAAO,SAAS,OAAO;AAAA,MACxB;AAAA,IACD;AAEA,SAAK,eAAe;AACpB,SAAK,MAAM,eAAe;AAI1B,SAAK,gBAAgB;AAAA,EACtB;AACD;",
4
+ "sourcesContent": ["import { Signal, transact } from '@tldraw/state'\nimport { RecordsDiff, SerializedSchema, UnknownRecord, squashRecordDiffs } from '@tldraw/store'\nimport { TLStore } from '@tldraw/tlschema'\nimport { assert } from '@tldraw/utils'\nimport {\n\tTAB_ID,\n\tTLSessionStateSnapshot,\n\tcreateSessionStateSnapshotSignal,\n\textractSessionStateFromLegacySnapshot,\n\tloadSessionStateSnapshotIntoStore,\n} from '../../config/TLSessionStateSnapshot'\nimport { LocalIndexedDb } from './LocalIndexedDb'\nimport { showCantReadFromIndexDbAlert, showCantWriteToIndexDbAlert } from './alerts'\n\n/** How should we debounce persists? */\nconst PERSIST_THROTTLE_MS = 350\n/** If we're in an error state, how long should we wait before retrying a write? */\nconst PERSIST_RETRY_THROTTLE_MS = 10_000\n\nconst UPDATE_INSTANCE_STATE = Symbol('UPDATE_INSTANCE_STATE')\n\n/**\n * IMPORTANT!!!\n *\n * This is just a quick-n-dirty temporary solution that will be replaced with the remote sync client\n * once it has the db integrated\n */\n\ninterface SyncMessage {\n\ttype: 'diff'\n\tstoreId: string\n\tchanges: RecordsDiff<UnknownRecord>\n\tschema: SerializedSchema\n}\n\n// Sent by new clients when they connect\n// If another client is on the channel with a newer schema version\n// It will\ninterface AnnounceMessage {\n\ttype: 'announce'\n\tschema: SerializedSchema\n}\n\ntype Message = SyncMessage | AnnounceMessage\n\ntype UnpackPromise<T> = T extends Promise<infer U> ? U : T\n\nconst msg = (msg: Message) => msg\n\n/** @internal */\nexport class BroadcastChannelMock {\n\tonmessage?: (e: MessageEvent) => void\n\tconstructor(_name: string) {\n\t\t// noop\n\t}\n\tpostMessage(_msg: Message) {\n\t\t// noop\n\t}\n\tclose() {\n\t\t// noop\n\t}\n}\n\nconst BC = typeof BroadcastChannel === 'undefined' ? BroadcastChannelMock : BroadcastChannel\n\n/** @internal */\nexport class TLLocalSyncClient {\n\tprivate disposables = new Set<() => void>()\n\tprivate diffQueue: Array<RecordsDiff<UnknownRecord> | typeof UPDATE_INSTANCE_STATE> = []\n\tprivate didDispose = false\n\tprivate shouldDoFullDBWrite = true\n\tprivate isReloading = false\n\treadonly persistenceKey: string\n\treadonly sessionId: string\n\treadonly serializedSchema: SerializedSchema\n\tprivate isDebugging = false\n\tprivate readonly documentTypes: ReadonlySet<string>\n\tprivate readonly $sessionStateSnapshot: Signal<TLSessionStateSnapshot | null>\n\t/** @internal */\n\treadonly db: LocalIndexedDb\n\n\tinitTime = Date.now()\n\tprivate debug(...args: any[]) {\n\t\tif (this.isDebugging) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.debug(...args)\n\t\t}\n\t}\n\tconstructor(\n\t\tpublic readonly store: TLStore,\n\t\t{\n\t\t\tpersistenceKey,\n\t\t\tsessionId = TAB_ID,\n\t\t\tonLoad,\n\t\t\tonLoadError,\n\t\t}: {\n\t\t\tpersistenceKey: string\n\t\t\tsessionId?: string\n\t\t\tonLoad(self: TLLocalSyncClient): void\n\t\t\tonLoadError(error: Error): void\n\t\t},\n\t\tpublic readonly channel = new BC(`tldraw-tab-sync-${persistenceKey}`)\n\t) {\n\t\tif (typeof window !== 'undefined') {\n\t\t\t;(window as any).tlsync = this\n\t\t}\n\t\tthis.persistenceKey = persistenceKey\n\t\tthis.sessionId = sessionId\n\t\tthis.db = new LocalIndexedDb(persistenceKey)\n\t\tthis.disposables.add(() => this.db.close())\n\n\t\tthis.serializedSchema = this.store.schema.serialize()\n\t\tthis.$sessionStateSnapshot = createSessionStateSnapshotSignal(this.store)\n\n\t\tthis.disposables.add(\n\t\t\t// Set up a subscription to changes from the store: When\n\t\t\t// the store changes (and if the change was made by the user)\n\t\t\t// then immediately send the diff to other tabs via postMessage\n\t\t\t// and schedule a persist.\n\t\t\tstore.listen(\n\t\t\t\t({ changes }) => {\n\t\t\t\t\tthis.diffQueue.push(changes)\n\t\t\t\t\tthis.channel.postMessage(\n\t\t\t\t\t\tmsg({\n\t\t\t\t\t\t\ttype: 'diff',\n\t\t\t\t\t\t\tstoreId: this.store.id,\n\t\t\t\t\t\t\tchanges,\n\t\t\t\t\t\t\tschema: this.serializedSchema,\n\t\t\t\t\t\t})\n\t\t\t\t\t)\n\t\t\t\t\tthis.schedulePersist()\n\t\t\t\t},\n\t\t\t\t{ source: 'user', scope: 'document' }\n\t\t\t)\n\t\t)\n\t\tthis.disposables.add(\n\t\t\tstore.listen(\n\t\t\t\t() => {\n\t\t\t\t\tthis.diffQueue.push(UPDATE_INSTANCE_STATE)\n\t\t\t\t\tthis.schedulePersist()\n\t\t\t\t},\n\t\t\t\t{ scope: 'session' }\n\t\t\t)\n\t\t)\n\n\t\tthis.connect(onLoad, onLoadError)\n\n\t\tthis.documentTypes = new Set(\n\t\t\tObject.values(this.store.schema.types)\n\t\t\t\t.filter((t) => t.scope === 'document')\n\t\t\t\t.map((t) => t.typeName)\n\t\t)\n\t}\n\n\tprivate async connect(onLoad: (client: this) => void, onLoadError: (error: Error) => void) {\n\t\tthis.debug('connecting')\n\t\tlet data: UnpackPromise<ReturnType<LocalIndexedDb['load']>> | undefined\n\n\t\ttry {\n\t\t\tdata = await this.db.load({ sessionId: this.sessionId })\n\t\t} catch (error: any) {\n\t\t\tonLoadError(error)\n\t\t\tshowCantReadFromIndexDbAlert()\n\t\t\treturn\n\t\t}\n\n\t\tthis.debug('loaded data from store', data, 'didDispose', this.didDispose)\n\t\tif (this.didDispose) return\n\n\t\ttry {\n\t\t\tif (data) {\n\t\t\t\tconst documentSnapshot = Object.fromEntries(data.records.map((r) => [r.id, r]))\n\t\t\t\tconst sessionStateSnapshot =\n\t\t\t\t\tdata.sessionStateSnapshot ?? extractSessionStateFromLegacySnapshot(documentSnapshot)\n\t\t\t\tconst migrationResult = this.store.schema.migrateStoreSnapshot({\n\t\t\t\t\tstore: documentSnapshot,\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-deprecated\n\t\t\t\t\tschema: data.schema ?? this.store.schema.serializeEarliestVersion(),\n\t\t\t\t})\n\n\t\t\t\tif (migrationResult.type === 'error') {\n\t\t\t\t\tconsole.error('failed to migrate store', migrationResult)\n\t\t\t\t\tonLoadError(new Error(`Failed to migrate store: ${migrationResult.reason}`))\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tconst records = Object.values(migrationResult.value).filter((r) =>\n\t\t\t\t\tthis.documentTypes.has(r.typeName)\n\t\t\t\t)\n\t\t\t\tif (records.length > 0) {\n\t\t\t\t\t// 3. Merge the changes into the REAL STORE\n\t\t\t\t\tthis.store.mergeRemoteChanges(() => {\n\t\t\t\t\t\t// Calling put will validate the records!\n\t\t\t\t\t\tthis.store.put(records, 'initialize')\n\t\t\t\t\t})\n\t\t\t\t}\n\n\t\t\t\tif (sessionStateSnapshot) {\n\t\t\t\t\tloadSessionStateSnapshotIntoStore(this.store, sessionStateSnapshot, {\n\t\t\t\t\t\tforceOverwrite: true,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthis.channel.onmessage = ({ data }) => {\n\t\t\t\tthis.debug('got message', data)\n\t\t\t\tconst msg = data as Message\n\t\t\t\t// if their schema is earlier than ours, we need to tell them so they can refresh\n\t\t\t\t// if their schema is later than ours, we need to refresh\n\t\t\t\tconst res = this.store.schema.getMigrationsSince(msg.schema)\n\n\t\t\t\tif (!res.ok) {\n\t\t\t\t\t// we are older, refresh\n\t\t\t\t\t// but add a safety check to make sure we don't get in an infinite loop\n\t\t\t\t\tconst timeSinceInit = Date.now() - this.initTime\n\t\t\t\t\tif (timeSinceInit < 5000) {\n\t\t\t\t\t\t// This tab was just reloaded, but is out of date compared to other tabs.\n\t\t\t\t\t\t// Not expecting this to ever happen. It should only happen if we roll back a release that incremented\n\t\t\t\t\t\t// the schema version (which we should never do)\n\t\t\t\t\t\t// Or maybe during development if you have multiple local tabs open running the app on prod mode and you\n\t\t\t\t\t\t// check out an older commit. Dev server should be fine.\n\t\t\t\t\t\tonLoadError(new Error('Schema mismatch, please close other tabs and reload the page'))\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\t\t\t\t\tthis.debug('reloading')\n\t\t\t\t\tthis.isReloading = true\n\t\t\t\t\twindow?.location?.reload?.()\n\t\t\t\t\treturn\n\t\t\t\t} else if (res.value.length > 0) {\n\t\t\t\t\t// they are older, tell them to refresh and not write any more data\n\t\t\t\t\tthis.debug('telling them to reload')\n\t\t\t\t\tthis.channel.postMessage({ type: 'announce', schema: this.serializedSchema })\n\t\t\t\t\t// schedule a full db write in case they wrote data anyway\n\t\t\t\t\tthis.shouldDoFullDBWrite = true\n\t\t\t\t\tthis.persistIfNeeded()\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\t// otherwise, all good, same version :)\n\t\t\t\tif (msg.type === 'diff') {\n\t\t\t\t\tthis.debug('applying diff')\n\t\t\t\t\ttransact(() => {\n\t\t\t\t\t\tthis.store.mergeRemoteChanges(() => {\n\t\t\t\t\t\t\tthis.store.applyDiff(msg.changes as any)\n\t\t\t\t\t\t})\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\t\t\tthis.channel.postMessage({ type: 'announce', schema: this.serializedSchema })\n\t\t\tthis.disposables.add(() => {\n\t\t\t\tthis.channel.close()\n\t\t\t})\n\t\t\tonLoad(this)\n\t\t} catch (e: any) {\n\t\t\tthis.debug('error loading data from store', e)\n\t\t\tif (this.didDispose) return\n\t\t\tonLoadError(e)\n\t\t\treturn\n\t\t}\n\t}\n\n\tclose() {\n\t\tthis.debug('closing')\n\t\tthis.didDispose = true\n\t\tthis.disposables.forEach((d) => d())\n\t}\n\n\tprivate isPersisting = false\n\tprivate didLastWriteError = false\n\t// eslint-disable-next-line no-restricted-globals\n\tprivate scheduledPersistTimeout: ReturnType<typeof setTimeout> | null = null\n\n\t/**\n\t * Schedule a persist. Persists don't happen immediately: they are throttled to avoid writing too\n\t * often, and will retry if failed.\n\t *\n\t * @internal\n\t */\n\tprivate schedulePersist() {\n\t\tthis.debug('schedulePersist', this.scheduledPersistTimeout)\n\t\tif (this.scheduledPersistTimeout) return\n\t\t// eslint-disable-next-line no-restricted-globals\n\t\tthis.scheduledPersistTimeout = setTimeout(\n\t\t\t() => {\n\t\t\t\tthis.scheduledPersistTimeout = null\n\t\t\t\tthis.persistIfNeeded()\n\t\t\t},\n\t\t\tthis.didLastWriteError ? PERSIST_RETRY_THROTTLE_MS : PERSIST_THROTTLE_MS\n\t\t)\n\t}\n\n\t/**\n\t * Persist to IndexedDB only under certain circumstances:\n\t *\n\t * - If we're not already persisting\n\t * - If we're not reloading the page\n\t * - And we have something to persist (a full db write scheduled or changes in the diff queue)\n\t *\n\t * @internal\n\t */\n\tprivate persistIfNeeded() {\n\t\tthis.debug('persistIfNeeded', {\n\t\t\tisPersisting: this.isPersisting,\n\t\t\tisReloading: this.isReloading,\n\t\t\tshouldDoFullDBWrite: this.shouldDoFullDBWrite,\n\t\t\tdiffQueueLength: this.diffQueue.length,\n\t\t\tstoreIsPossiblyCorrupt: this.store.isPossiblyCorrupted(),\n\t\t})\n\n\t\t// if we've scheduled a persist for the future, that's no longer needed\n\t\tif (this.scheduledPersistTimeout) {\n\t\t\tclearTimeout(this.scheduledPersistTimeout)\n\t\t\tthis.scheduledPersistTimeout = null\n\t\t}\n\n\t\t// if a persist is already in progress, we don't need to do anything -\n\t\t// if there are still outstanding changes once it's finished, it'll\n\t\t// schedule another persist\n\t\tif (this.isPersisting) return\n\n\t\t// if we're reloading the page, it's because there's a newer client\n\t\t// present so lets not overwrite their changes\n\t\tif (this.isReloading) return\n\n\t\t// if the store is possibly corrupted, we don't want to persist\n\t\tif (this.store.isPossiblyCorrupted()) return\n\n\t\t// if we're scheduled for a full write or if we have changes outstanding, let's persist them!\n\t\tif (this.shouldDoFullDBWrite || this.diffQueue.length > 0) {\n\t\t\tthis.doPersist()\n\t\t}\n\t}\n\n\t/**\n\t * Actually persist to IndexedDB. If the write fails, then we'll retry with a full db write after\n\t * a short delay.\n\t */\n\tprivate async doPersist() {\n\t\tassert(!this.isPersisting, 'persist already in progress')\n\t\tif (this.didDispose) return\n\t\tthis.isPersisting = true\n\n\t\tthis.debug('doPersist start')\n\n\t\t// instantly empty the diff queue, but keep our own copy of it. this way\n\t\t// diffs that come in during the persist will still get tracked\n\t\tconst diffQueue = this.diffQueue\n\t\tthis.diffQueue = []\n\n\t\ttry {\n\t\t\tif (this.shouldDoFullDBWrite) {\n\t\t\t\tthis.shouldDoFullDBWrite = false\n\t\t\t\tawait this.db.storeSnapshot({\n\t\t\t\t\tschema: this.store.schema,\n\t\t\t\t\tsnapshot: this.store.serialize(),\n\t\t\t\t\tsessionId: this.sessionId,\n\t\t\t\t\tsessionStateSnapshot: this.$sessionStateSnapshot.get(),\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tconst diffs = squashRecordDiffs(\n\t\t\t\t\tdiffQueue.filter((d): d is RecordsDiff<UnknownRecord> => d !== UPDATE_INSTANCE_STATE)\n\t\t\t\t)\n\t\t\t\tawait this.db.storeChanges({\n\t\t\t\t\tchanges: diffs,\n\t\t\t\t\tschema: this.store.schema,\n\t\t\t\t\tsessionId: this.sessionId,\n\t\t\t\t\tsessionStateSnapshot: this.$sessionStateSnapshot.get(),\n\t\t\t\t})\n\t\t\t}\n\t\t\tthis.didLastWriteError = false\n\t\t} catch (e) {\n\t\t\t// set this.shouldDoFullDBWrite because we clear the diffQueue no matter what,\n\t\t\t// so if this is just a temporary error, we will still persist all changes\n\t\t\tthis.shouldDoFullDBWrite = true\n\t\t\tthis.didLastWriteError = true\n\t\t\tconsole.error('failed to store changes in indexed db', e)\n\n\t\t\tshowCantWriteToIndexDbAlert()\n\t\t\tif (typeof window !== 'undefined') {\n\t\t\t\t// adios\n\t\t\t\twindow.location.reload()\n\t\t\t}\n\t\t}\n\n\t\tthis.isPersisting = false\n\t\tthis.debug('doPersist end')\n\n\t\t// changes might have come in between when we started the persist and\n\t\t// now. we request another persist so any new changes can get written\n\t\tthis.schedulePersist()\n\t}\n}\n"],
5
+ "mappings": "AAAA,SAAiB,gBAAgB;AACjC,SAAuD,yBAAyB;AAEhF,SAAS,cAAc;AACvB;AAAA,EACC;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,sBAAsB;AAC/B,SAAS,8BAA8B,mCAAmC;AAG1E,MAAM,sBAAsB;AAE5B,MAAM,4BAA4B;AAElC,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canary.ab782583b3ba";
2
2
  const publishDates = {
3
3
  major: "2024-09-13T14:36:29.063Z",
4
- minor: "2025-01-09T08:29:21.762Z",
5
- patch: "2025-01-09T08:29:21.762Z"
4
+ minor: "2025-01-28T18:43:07.534Z",
5
+ patch: "2025-01-28T18:43:07.534Z"
6
6
  };
7
7
  export {
8
8
  publishDates,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/version.ts"],
4
- "sourcesContent": ["// This file is automatically generated by internal/scripts/refresh-assets.ts.\n// Do not edit manually. Or do, I'm a comment, not a cop.\n\nexport const version = '3.8.0-canary.ab23d1d97da1'\nexport const publishDates = {\n\tmajor: '2024-09-13T14:36:29.063Z',\n\tminor: '2025-01-09T08:29:21.762Z',\n\tpatch: '2025-01-09T08:29:21.762Z',\n}\n"],
4
+ "sourcesContent": ["// This file is automatically generated by internal/scripts/refresh-assets.ts.\n// Do not edit manually. Or do, I'm a comment, not a cop.\n\nexport const version = '3.8.0-canary.ab782583b3ba'\nexport const publishDates = {\n\tmajor: '2024-09-13T14:36:29.063Z',\n\tminor: '2025-01-28T18:43:07.534Z',\n\tpatch: '2025-01-28T18:43:07.534Z',\n}\n"],
5
5
  "mappings": "AAGO,MAAM,UAAU;AAChB,MAAM,eAAe;AAAA,EAC3B,OAAO;AAAA,EACP,OAAO;AAAA,EACP,OAAO;AACR;",
6
6
  "names": []
7
7
  }
package/editor.css CHANGED
@@ -39,9 +39,10 @@
39
39
  --layer-overlays-user-brush: 50;
40
40
  --layer-overlays-user-indicator-selected: 60;
41
41
  --layer-overlays-user-indicator-hovered: 70;
42
- --layer-overlays-user-handles: 80;
43
42
  --layer-overlays-user-snapline: 90;
44
43
  --layer-overlays-selection-fg: 100;
44
+ /* User handles need to be above selection edges / corners, matters for sticky note clone handles */
45
+ --layer-overlays-user-handles: 105;
45
46
  --layer-overlays-user-indicator-hint: 110;
46
47
  --layer-overlays-collaborator-cursor-hint: 120;
47
48
  --layer-overlays-collaborator-cursor: 130;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tldraw/editor",
3
3
  "description": "A tiny little drawing app (editor).",
4
- "version": "3.8.0-canary.ab23d1d97da1",
4
+ "version": "3.8.0-canary.ab782583b3ba",
5
5
  "author": {
6
6
  "name": "tldraw Inc.",
7
7
  "email": "hello@tldraw.com"
@@ -45,14 +45,15 @@
45
45
  "lint": "yarn run -T tsx ../../internal/scripts/lint.ts"
46
46
  },
47
47
  "dependencies": {
48
- "@tldraw/state": "3.8.0-canary.ab23d1d97da1",
49
- "@tldraw/state-react": "3.8.0-canary.ab23d1d97da1",
50
- "@tldraw/store": "3.8.0-canary.ab23d1d97da1",
51
- "@tldraw/tlschema": "3.8.0-canary.ab23d1d97da1",
52
- "@tldraw/utils": "3.8.0-canary.ab23d1d97da1",
53
- "@tldraw/validate": "3.8.0-canary.ab23d1d97da1",
48
+ "@tldraw/state": "3.8.0-canary.ab782583b3ba",
49
+ "@tldraw/state-react": "3.8.0-canary.ab782583b3ba",
50
+ "@tldraw/store": "3.8.0-canary.ab782583b3ba",
51
+ "@tldraw/tlschema": "3.8.0-canary.ab782583b3ba",
52
+ "@tldraw/utils": "3.8.0-canary.ab782583b3ba",
53
+ "@tldraw/validate": "3.8.0-canary.ab782583b3ba",
54
54
  "@types/core-js": "^2.5.5",
55
55
  "@use-gesture/react": "^10.2.27",
56
+ "canvas-size": "~2.0.0",
56
57
  "classnames": "^2.3.2",
57
58
  "core-js": "^3.31.1",
58
59
  "eventemitter3": "^4.0.7",
@@ -69,6 +70,7 @@
69
70
  "@testing-library/jest-dom": "^5.16.5",
70
71
  "@testing-library/react": "^15.0.6",
71
72
  "@types/benchmark": "^2.1.2",
73
+ "@types/canvas-size": "^1.2.2",
72
74
  "@types/wicg-file-system-access": "^2020.9.5",
73
75
  "benchmark": "^2.1.4",
74
76
  "fake-indexeddb": "^4.0.0",
package/src/index.ts CHANGED
@@ -25,6 +25,7 @@ export {
25
25
  useStateTracking,
26
26
  useValue,
27
27
  } from '@tldraw/state-react'
28
+ export { resizeScaled } from './lib/editor/shapes/shared/resizeScaled'
28
29
  export { LocalIndexedDb, Table, type StoreName } from './lib/utils/sync/LocalIndexedDb'
29
30
  // eslint-disable-next-line local/no-export-star
30
31
  export * from '@tldraw/store'
@@ -182,6 +183,7 @@ export { UserPreferencesManager } from './lib/editor/managers/UserPreferencesMan
182
183
  export { BaseBoxShapeUtil, type TLBaseBoxShape } from './lib/editor/shapes/BaseBoxShapeUtil'
183
184
  export {
184
185
  ShapeUtil,
186
+ type TLCropInfo,
185
187
  type TLHandleDragInfo,
186
188
  type TLResizeInfo,
187
189
  type TLResizeMode,
@@ -254,10 +256,13 @@ export {
254
256
  type TLCameraConstraints,
255
257
  type TLCameraMoveOptions,
256
258
  type TLCameraOptions,
259
+ type TLExportType,
257
260
  type TLImageExportOptions,
261
+ type TLSvgExportOptions,
258
262
  type TLSvgOptions,
259
263
  } from './lib/editor/types/misc-types'
260
264
  export { type TLResizeHandle, type TLSelectionHandle } from './lib/editor/types/selection-types'
265
+ export { getSvgAsImage } from './lib/exports/getSvgAsImage'
261
266
  export { tlenv } from './lib/globals/environment'
262
267
  export { tlmenus } from './lib/globals/menus'
263
268
  export { tltime } from './lib/globals/time'
@@ -381,6 +386,7 @@ export {
381
386
  type SharedStyle,
382
387
  } from './lib/utils/SharedStylesMap'
383
388
  export { dataUrlToFile, getDefaultCdnBaseUrl } from './lib/utils/assets'
389
+ export { clampToBrowserMaxCanvasSize, type CanvasMaxSize } from './lib/utils/browserCanvasMaxSize'
384
390
  export {
385
391
  debugFlags,
386
392
  featureFlags,
@@ -297,6 +297,7 @@ function TldrawEditorWithOwnStore(
297
297
  sessionId,
298
298
  user,
299
299
  assets,
300
+ migrations,
300
301
  } = props
301
302
 
302
303
  const syncedStore = useLocalStore({
@@ -308,6 +309,7 @@ function TldrawEditorWithOwnStore(
308
309
  defaultName,
309
310
  snapshot,
310
311
  assets,
312
+ migrations,
311
313
  })
312
314
 
313
315
  return <TldrawEditorWithLoadingStore {...props} store={syncedStore} user={user} />
@@ -160,7 +160,6 @@ export function DefaultCanvas({ className }: TLCanvasComponentProps) {
160
160
  <div className="tl-overlays">
161
161
  <div ref={rHtmlLayer2} className="tl-html-layer">
162
162
  {debugGeometry ? <GeometryDebuggingView /> : null}
163
- <HandlesWrapper />
164
163
  <BrushWrapper />
165
164
  <ScribbleWrapper />
166
165
  <ZoomBrushWrapper />
@@ -168,6 +167,7 @@ export function DefaultCanvas({ className }: TLCanvasComponentProps) {
168
167
  <HintedShapeIndicator />
169
168
  <SnapIndicatorWrapper />
170
169
  <SelectionForegroundWrapper />
170
+ <HandlesWrapper />
171
171
  <LiveCollaborators />
172
172
  </div>
173
173
  </div>
@@ -485,10 +485,7 @@ function DebugSvgCopy({ id, mode }: { id: TLShapeId; mode: 'img' | 'iframe' }) {
485
485
  if (!bounds) return
486
486
  bounds = bounds.clone().expandBy(padding)
487
487
 
488
- const result = await editor.getSvgString([id], {
489
- padding,
490
- background: editor.getInstanceState().exportBackground,
491
- })
488
+ const result = await editor.getSvgString([id], { padding })
492
489
 
493
490
  if (latest !== renderId || !result) return
494
491
 
@@ -61,7 +61,9 @@ const defaultAssetResolve: NonNullable<TLAssetStore['resolve']> = (asset) => ass
61
61
 
62
62
  /** @public */
63
63
  export const inlineBase64AssetStore: TLAssetStore = {
64
- upload: (_, file) => FileHelpers.blobToDataUrl(file),
64
+ upload: async (_, file) => {
65
+ return { src: await FileHelpers.blobToDataUrl(file) }
66
+ },
65
67
  }
66
68
 
67
69
  /**
@@ -128,7 +130,7 @@ export function createTLStore({
128
130
 
129
131
  if (rest.snapshot) {
130
132
  if (initialData) throw new Error('Cannot provide both initialData and snapshot')
131
- loadSnapshot(store, rest.snapshot)
133
+ loadSnapshot(store, rest.snapshot, { forceOverwriteSessionState: true })
132
134
  }
133
135
 
134
136
  return store
@@ -104,6 +104,7 @@ import {
104
104
  ZOOM_TO_FIT_PADDING,
105
105
  } from '../constants'
106
106
  import { exportToSvg } from '../exports/exportToSvg'
107
+ import { getSvgAsImage } from '../exports/getSvgAsImage'
107
108
  import { tlenv } from '../globals/environment'
108
109
  import { tlmenus } from '../globals/menus'
109
110
  import { tltime } from '../globals/time'
@@ -162,6 +163,7 @@ import {
162
163
  TLCameraMoveOptions,
163
164
  TLCameraOptions,
164
165
  TLImageExportOptions,
166
+ TLSvgExportOptions,
165
167
  } from './types/misc-types'
166
168
  import { TLResizeHandle } from './types/selection-types'
167
169
 
@@ -927,6 +929,21 @@ export class Editor extends EventEmitter<TLEventMap> {
927
929
  return shapeUtil
928
930
  }
929
931
 
932
+ /**
933
+ * Returns true if the editor has a shape util for the given shape / shape type.
934
+ *
935
+ * @param shape - A shape, shape partial, or shape type.
936
+ */
937
+ hasShapeUtil<S extends TLUnknownShape>(shape: S | TLShapePartial<S>): boolean
938
+ hasShapeUtil<S extends TLUnknownShape>(type: S['type']): boolean
939
+ hasShapeUtil<T extends ShapeUtil>(
940
+ type: T extends ShapeUtil<infer R> ? R['type'] : string
941
+ ): boolean
942
+ hasShapeUtil(arg: string | { type: string }): boolean {
943
+ const type = typeof arg === 'string' ? arg : arg.type
944
+ return hasOwnProperty(this.shapeUtils, type)
945
+ }
946
+
930
947
  /* ------------------- Binding Utils ------------------ */
931
948
  /**
932
949
  * A map of shape utility classes (TLShapeUtils) by shape type.
@@ -1218,18 +1235,7 @@ export class Editor extends EventEmitter<TLEventMap> {
1218
1235
  }
1219
1236
 
1220
1237
  /** @internal */
1221
- createErrorAnnotations(
1222
- origin: string,
1223
- willCrashApp: boolean | 'unknown'
1224
- ): {
1225
- tags: { origin: string; willCrashApp: boolean | 'unknown' }
1226
- extras: {
1227
- activeStateNode?: string
1228
- selectedShapes?: TLUnknownShape[]
1229
- editingShape?: TLUnknownShape
1230
- inputs?: Record<string, unknown>
1231
- }
1232
- } {
1238
+ createErrorAnnotations(origin: string, willCrashApp: boolean | 'unknown') {
1233
1239
  try {
1234
1240
  const editingShapeId = this.getEditingShapeId()
1235
1241
  return {
@@ -1239,9 +1245,20 @@ export class Editor extends EventEmitter<TLEventMap> {
1239
1245
  },
1240
1246
  extras: {
1241
1247
  activeStateNode: this.root.getPath(),
1242
- selectedShapes: this.getSelectedShapes(),
1248
+ selectedShapes: this.getSelectedShapes().map((s) => {
1249
+ const { props, ...rest } = s
1250
+ const { text: _text, richText: _richText, ...restProps } = props as any
1251
+ return {
1252
+ ...rest,
1253
+ props: restProps,
1254
+ }
1255
+ }),
1256
+ selectionCount: this.getSelectedShapes().length,
1243
1257
  editingShape: editingShapeId ? this.getShape(editingShapeId) : undefined,
1244
1258
  inputs: this.inputs,
1259
+ pageState: this.getCurrentPageState(),
1260
+ instanceState: this.getInstanceState(),
1261
+ collaboratorCount: this.getCollaboratorsOnCurrentPage().length,
1245
1262
  },
1246
1263
  }
1247
1264
  } catch {
@@ -1458,10 +1475,10 @@ export class Editor extends EventEmitter<TLEventMap> {
1458
1475
  if (partial.isChangingStyle !== undefined) {
1459
1476
  clearTimeout(this._isChangingStyleTimeout)
1460
1477
  if (partial.isChangingStyle === true) {
1461
- // If we've set to true, set a new reset timeout to change the value back to false after 2 seconds
1478
+ // If we've set to true, set a new reset timeout to change the value back to false after 1 seconds
1462
1479
  this._isChangingStyleTimeout = this.timers.setTimeout(() => {
1463
1480
  this._updateInstanceState({ isChangingStyle: false }, { history: 'ignore' })
1464
- }, 2000)
1481
+ }, 1000)
1465
1482
  }
1466
1483
  }
1467
1484
 
@@ -4145,20 +4162,24 @@ export class Editor extends EventEmitter<TLEventMap> {
4145
4162
  context: {
4146
4163
  screenScale?: number
4147
4164
  shouldResolveToOriginal?: boolean
4165
+ dpr?: number
4148
4166
  }
4149
4167
  ): Promise<string | null> {
4150
4168
  if (!assetId) return null
4151
4169
  const asset = this.getAsset(assetId)
4152
4170
  if (!asset) return null
4153
4171
 
4154
- const { screenScale = 1, shouldResolveToOriginal = false } = context
4172
+ const {
4173
+ screenScale = 1,
4174
+ shouldResolveToOriginal = false,
4175
+ dpr = this.getInstanceState().devicePixelRatio,
4176
+ } = context
4155
4177
 
4156
4178
  // We only look at the zoom level at powers of 2.
4157
4179
  const zoomStepFunction = (zoom: number) => Math.pow(2, Math.ceil(Math.log2(zoom)))
4158
- const steppedScreenScale = Math.max(0.125, zoomStepFunction(screenScale))
4180
+ const steppedScreenScale = zoomStepFunction(screenScale)
4159
4181
  const networkEffectiveType: string | null =
4160
4182
  'connection' in navigator ? (navigator as any).connection.effectiveType : null
4161
- const dpr = this.getInstanceState().devicePixelRatio
4162
4183
 
4163
4184
  return await this.store.props.assets.resolve(asset, {
4164
4185
  screenScale: screenScale || 1,
@@ -4172,7 +4193,11 @@ export class Editor extends EventEmitter<TLEventMap> {
4172
4193
  * Upload an asset to the store's asset service, returning a URL that can be used to resolve the
4173
4194
  * asset.
4174
4195
  */
4175
- async uploadAsset(asset: TLAsset, file: File, abortSignal?: AbortSignal): Promise<string> {
4196
+ async uploadAsset(
4197
+ asset: TLAsset,
4198
+ file: File,
4199
+ abortSignal?: AbortSignal
4200
+ ): Promise<{ src: string; meta?: JsonObject }> {
4176
4201
  return await this.store.props.assets.upload(asset, file, abortSignal)
4177
4202
  }
4178
4203
 
@@ -6750,6 +6775,8 @@ export class Editor extends EventEmitter<TLEventMap> {
6750
6775
  }
6751
6776
  }
6752
6777
 
6778
+ let didResize = false
6779
+
6753
6780
  if (util.onResize && util.canResize(initialShape)) {
6754
6781
  // get the model changes from the shape util
6755
6782
  const newPagePoint = this._scalePagePoint(
@@ -6788,24 +6815,30 @@ export class Editor extends EventEmitter<TLEventMap> {
6788
6815
  )
6789
6816
  }
6790
6817
 
6818
+ const resizedShape = util.onResize(
6819
+ { ...initialShape, x, y },
6820
+ {
6821
+ newPoint: newLocalPoint,
6822
+ handle: opts.dragHandle ?? 'bottom_right',
6823
+ // don't set isSingle to true for children
6824
+ mode: opts.mode ?? 'scale_shape',
6825
+ scaleX: myScale.x,
6826
+ scaleY: myScale.y,
6827
+ initialBounds,
6828
+ initialShape,
6829
+ }
6830
+ )
6831
+
6832
+ if (resizedShape) {
6833
+ didResize = true
6834
+ }
6835
+
6791
6836
  workingShape = applyPartialToRecordWithProps(workingShape, {
6792
6837
  id,
6793
6838
  type: initialShape.type as any,
6794
6839
  x: newLocalPoint.x,
6795
6840
  y: newLocalPoint.y,
6796
- ...util.onResize(
6797
- { ...initialShape, x, y },
6798
- {
6799
- newPoint: newLocalPoint,
6800
- handle: opts.dragHandle ?? 'bottom_right',
6801
- // don't set isSingle to true for children
6802
- mode: opts.mode ?? 'scale_shape',
6803
- scaleX: myScale.x,
6804
- scaleY: myScale.y,
6805
- initialBounds,
6806
- initialShape,
6807
- }
6808
- ),
6841
+ ...resizedShape,
6809
6842
  })
6810
6843
 
6811
6844
  if (!opts.skipStartAndEndCallbacks) {
@@ -6816,7 +6849,11 @@ export class Editor extends EventEmitter<TLEventMap> {
6816
6849
  }
6817
6850
 
6818
6851
  this.updateShapes([workingShape])
6819
- } else {
6852
+ }
6853
+
6854
+ if (!didResize) {
6855
+ // reposition shape (rather than resizing it) based on where its resized center would be
6856
+
6820
6857
  const initialPageCenter = Mat.applyToPoint(pageTransform, initialBounds.center)
6821
6858
  // get the model changes from the shape util
6822
6859
  const newPageCenter = this._scalePagePoint(
@@ -8564,11 +8601,13 @@ export class Editor extends EventEmitter<TLEventMap> {
8564
8601
  *
8565
8602
  * @public
8566
8603
  */
8567
- async getSvgElement(shapes: TLShapeId[] | TLShape[], opts: TLImageExportOptions = {}) {
8604
+ async getSvgElement(shapes: TLShapeId[] | TLShape[], opts: TLSvgExportOptions = {}) {
8568
8605
  const ids =
8569
- typeof shapes[0] === 'string'
8570
- ? (shapes as TLShapeId[])
8571
- : (shapes as TLShape[]).map((s) => s.id)
8606
+ shapes.length === 0
8607
+ ? this.getCurrentPageShapeIdsSorted()
8608
+ : typeof shapes[0] === 'string'
8609
+ ? (shapes as TLShapeId[])
8610
+ : (shapes as TLShape[]).map((s) => s.id)
8572
8611
 
8573
8612
  if (ids.length === 0) return undefined
8574
8613
 
@@ -8585,7 +8624,7 @@ export class Editor extends EventEmitter<TLEventMap> {
8585
8624
  *
8586
8625
  * @public
8587
8626
  */
8588
- async getSvgString(shapes: TLShapeId[] | TLShape[], opts: TLImageExportOptions = {}) {
8627
+ async getSvgString(shapes: TLShapeId[] | TLShape[], opts: TLSvgExportOptions = {}) {
8589
8628
  const result = await this.getSvgElement(shapes, opts)
8590
8629
  if (!result) return undefined
8591
8630
 
@@ -8598,12 +8637,63 @@ export class Editor extends EventEmitter<TLEventMap> {
8598
8637
  }
8599
8638
 
8600
8639
  /** @deprecated Use {@link Editor.getSvgString} or {@link Editor.getSvgElement} instead. */
8601
- async getSvg(shapes: TLShapeId[] | TLShape[], opts: TLImageExportOptions = {}) {
8640
+ async getSvg(shapes: TLShapeId[] | TLShape[], opts: TLSvgExportOptions = {}) {
8602
8641
  const result = await this.getSvgElement(shapes, opts)
8603
8642
  if (!result) return undefined
8604
8643
  return result.svg
8605
8644
  }
8606
8645
 
8646
+ /**
8647
+ * Get an exported image of the given shapes.
8648
+ *
8649
+ * @param shapes - The shapes (or shape ids) to export.
8650
+ * @param opts - Options for the export.
8651
+ *
8652
+ * @returns A blob of the image.
8653
+ * @public
8654
+ */
8655
+ async toImage(shapes: TLShapeId[] | TLShape[], opts: TLImageExportOptions = {}) {
8656
+ const withDefaults = {
8657
+ format: 'png',
8658
+ scale: 1,
8659
+ pixelRatio: opts.format === 'svg' ? undefined : 2,
8660
+ ...opts,
8661
+ } satisfies TLImageExportOptions
8662
+ const result = await this.getSvgString(shapes, withDefaults)
8663
+ if (!result) throw new Error('Could not create SVG')
8664
+
8665
+ switch (withDefaults.format) {
8666
+ case 'svg':
8667
+ return {
8668
+ blob: new Blob([result.svg], { type: 'text/plain' }),
8669
+ width: result.width,
8670
+ height: result.height,
8671
+ }
8672
+ case 'jpeg':
8673
+ case 'png':
8674
+ case 'webp': {
8675
+ const blob = await getSvgAsImage(result.svg, {
8676
+ type: withDefaults.format,
8677
+ quality: withDefaults.quality,
8678
+ pixelRatio: withDefaults.pixelRatio,
8679
+ width: result.width,
8680
+ height: result.height,
8681
+ })
8682
+ if (!blob) {
8683
+ throw new Error('Could not construct image.')
8684
+ }
8685
+ return {
8686
+ blob,
8687
+ width: result.width,
8688
+ height: result.height,
8689
+ }
8690
+ }
8691
+ default: {
8692
+ exhaustiveSwitchError(withDefaults.format)
8693
+ }
8694
+ }
8695
+ }
8696
+
8607
8697
  /* --------------------- Events --------------------- */
8608
8698
 
8609
8699
  /**
@@ -5,11 +5,12 @@ import {
5
5
  TLHandle,
6
6
  TLPropsMigrations,
7
7
  TLShape,
8
+ TLShapeCrop,
8
9
  TLShapePartial,
9
10
  TLUnknownShape,
10
11
  } from '@tldraw/tlschema'
11
12
  import { ReactElement } from 'react'
12
- import { Box } from '../../primitives/Box'
13
+ import { Box, SelectionHandle } from '../../primitives/Box'
13
14
  import { Vec } from '../../primitives/Vec'
14
15
  import { Geometry2d } from '../../primitives/geometry/Geometry2d'
15
16
  import type { Editor } from '../Editor'
@@ -419,6 +420,19 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
419
420
  */
420
421
  onBeforeUpdate?(prev: Shape, next: Shape): Shape | void
421
422
 
423
+ /**
424
+ * A callback called when a shape changes from a crop.
425
+ *
426
+ * @param shape - The shape at the start of the crop.
427
+ * @param info - Info about the crop.
428
+ * @returns A change to apply to the shape, or void.
429
+ * @public
430
+ */
431
+ onCrop?(
432
+ shape: Shape,
433
+ info: TLCropInfo<Shape>
434
+ ): Omit<TLShapePartial<Shape>, 'id' | 'type'> | undefined | void
435
+
422
436
  /**
423
437
  * A callback called when some other shapes are dragged over this one.
424
438
  *
@@ -616,6 +630,21 @@ export abstract class ShapeUtil<Shape extends TLUnknownShape = TLUnknownShape> {
616
630
  onEditEnd?(shape: Shape): void
617
631
  }
618
632
 
633
+ /**
634
+ * Info about a crop.
635
+ * @param handle - The handle being dragged.
636
+ * @param change - The distance the handle is moved.
637
+ * @param initialShape - The shape at the start of the resize.
638
+ * @public
639
+ */
640
+ export interface TLCropInfo<T extends TLShape> {
641
+ handle: SelectionHandle
642
+ change: Vec
643
+ crop: TLShapeCrop
644
+ uncroppedSize: { w: number; h: number }
645
+ initialShape: T
646
+ }
647
+
619
648
  /**
620
649
  * The type of resize.
621
650
  *