From da35e0da27b9b9cce00b812f009bf40889e2d60a Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Fri, 2 Jun 2023 10:38:13 +0100 Subject: [PATCH] move v1 migration code into file-format (#1499) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move v1 migration code out of editor ### Change Type - [x] `minor` — New Feature --- packages/editor/api-report.md | 19 ------------------- packages/editor/src/index.ts | 1 - packages/file-format/api-report.md | 19 +++++++++++++++++++ packages/file-format/package.json | 1 + packages/file-format/src/index.ts | 1 + .../src/lib}/buildFromV1Document.ts | 15 +++++++-------- packages/file-format/src/lib/file.ts | 2 +- packages/file-format/tsconfig.json | 1 + public-yarn.lock | 1 + 9 files changed, 31 insertions(+), 29 deletions(-) rename packages/{editor/src/lib/utils => file-format/src/lib}/buildFromV1Document.ts (99%) diff --git a/packages/editor/api-report.md b/packages/editor/api-report.md index 8a863097b..51279b3b4 100644 --- a/packages/editor/api-report.md +++ b/packages/editor/api-report.md @@ -569,9 +569,6 @@ export function blobAsString(blob: Blob): Promise; // @internal (undocumented) export const BOUND_ARROW_OFFSET = 10; -// @internal (undocumented) -export function buildFromV1Document(app: App, document: LegacyTldrawDocument): void; - // @public (undocumented) export const Canvas: React_3.MemoExoticComponent<({ onDropOverride, }: { onDropOverride?: ((defaultOnDrop: (e: React_3.DragEvent) => Promise) => (e: React_3.DragEvent) => Promise) | undefined; @@ -923,22 +920,6 @@ export const isValidHttpURL: (url: string) => boolean; // @public (undocumented) export const LABEL_FONT_SIZES: Record; -// @internal (undocumented) -export interface LegacyTldrawDocument { - // (undocumented) - assets: TDAssets; - // (undocumented) - id: string; - // (undocumented) - name: string; - // (undocumented) - pages: Record; - // (undocumented) - pageStates: Record; - // (undocumented) - version: number; -} - // @public (undocumented) export function LoadingScreen({ children }: { children: any; diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts index b91acd2ea..6124be3b5 100644 --- a/packages/editor/src/index.ts +++ b/packages/editor/src/index.ts @@ -204,7 +204,6 @@ export { isSvgText, isValidHttpURL, } from './lib/utils/assets' -export { buildFromV1Document, type LegacyTldrawDocument } from './lib/utils/buildFromV1Document' export { checkFlag, fileToBase64, diff --git a/packages/file-format/api-report.md b/packages/file-format/api-report.md index 3d0b607fe..5f5d3c242 100644 --- a/packages/file-format/api-report.md +++ b/packages/file-format/api-report.md @@ -14,9 +14,28 @@ import { TLTranslationKey } from '@tldraw/ui'; import { ToastsContextType } from '@tldraw/ui'; import { UnknownRecord } from '@tldraw/tlstore'; +// @internal (undocumented) +export function buildFromV1Document(app: App, document: LegacyTldrawDocument): void; + // @public (undocumented) export function isV1File(data: any): boolean; +// @internal (undocumented) +export interface LegacyTldrawDocument { + // (undocumented) + assets: TDAssets; + // (undocumented) + id: string; + // (undocumented) + name: string; + // (undocumented) + pages: Record; + // (undocumented) + pageStates: Record; + // (undocumented) + version: number; +} + // @internal (undocumented) export function parseAndLoadDocument(app: App, document: string, msg: (id: TLTranslationKey) => string, addToast: ToastsContextType['addToast'], onV1FileLoad?: () => void, forceDarkMode?: boolean): Promise; diff --git a/packages/file-format/package.json b/packages/file-format/package.json index c5e64e9e1..09ebd1667 100644 --- a/packages/file-format/package.json +++ b/packages/file-format/package.json @@ -42,6 +42,7 @@ }, "dependencies": { "@tldraw/editor": "workspace:*", + "@tldraw/primitives": "workspace:*", "@tldraw/tlstore": "workspace:*", "@tldraw/tlvalidate": "workspace:*", "@tldraw/ui": "workspace:*", diff --git a/packages/file-format/src/index.ts b/packages/file-format/src/index.ts index c8b4a5cb5..9cb2c43e9 100644 --- a/packages/file-format/src/index.ts +++ b/packages/file-format/src/index.ts @@ -1,3 +1,4 @@ +export { buildFromV1Document, type LegacyTldrawDocument } from './lib/buildFromV1Document' export { TLDRAW_FILE_EXTENSION, TLDRAW_FILE_MIMETYPE, diff --git a/packages/editor/src/lib/utils/buildFromV1Document.ts b/packages/file-format/src/lib/buildFromV1Document.ts similarity index 99% rename from packages/editor/src/lib/utils/buildFromV1Document.ts rename to packages/file-format/src/lib/buildFromV1Document.ts index 007b3bfaa..b896e357e 100644 --- a/packages/editor/src/lib/utils/buildFromV1Document.ts +++ b/packages/file-format/src/lib/buildFromV1Document.ts @@ -1,11 +1,13 @@ -import { clamp, Vec2d } from '@tldraw/primitives' import { + App, AssetRecordType, + MAX_SHAPES_PER_PAGE, PageRecordType, TLAlignType, - TLArrowheadType, TLArrowShape, TLArrowTerminal, + TLArrowUtil, + TLArrowheadType, TLAsset, TLAssetId, TLColorType, @@ -22,17 +24,14 @@ import { TLTextShape, TLVideoShape, Vec2dModel, -} from '@tldraw/tlschema' -import { transact } from 'signia' -import { App } from '../app/App' -import { TLArrowUtil } from '../app/shapeutils/TLArrowUtil/TLArrowUtil' -import { MAX_SHAPES_PER_PAGE } from '../constants' +} from '@tldraw/editor' +import { Vec2d, clamp } from '@tldraw/primitives' const TLDRAW_V1_VERSION = 15.5 /** @internal */ export function buildFromV1Document(app: App, document: LegacyTldrawDocument) { - transact(() => { + app.batch(() => { document = migrate(document, TLDRAW_V1_VERSION) // Cancel any interactions / states app.cancel().cancel().cancel().cancel() diff --git a/packages/file-format/src/lib/file.ts b/packages/file-format/src/lib/file.ts index f4f368abb..7ee031ea9 100644 --- a/packages/file-format/src/lib/file.ts +++ b/packages/file-format/src/lib/file.ts @@ -1,6 +1,5 @@ import { App, - buildFromV1Document, createTLStore, fileToBase64, TLAsset, @@ -19,6 +18,7 @@ import { import { T } from '@tldraw/tlvalidate' import { TLTranslationKey, ToastsContextType } from '@tldraw/ui' import { exhaustiveSwitchError, Result } from '@tldraw/utils' +import { buildFromV1Document } from './buildFromV1Document' /** @public */ export const TLDRAW_FILE_MIMETYPE = 'application/vnd.tldraw+json' as const diff --git a/packages/file-format/tsconfig.json b/packages/file-format/tsconfig.json index 965b92760..f2d4b975b 100644 --- a/packages/file-format/tsconfig.json +++ b/packages/file-format/tsconfig.json @@ -10,6 +10,7 @@ { "path": "../editor" }, { "path": "../tlstore" }, { "path": "../tlvalidate" }, + { "path": "../primitives" }, { "path": "../utils" }, { "path": "../ui" } ] diff --git a/public-yarn.lock b/public-yarn.lock index 9a4919692..e1dc26153 100644 --- a/public-yarn.lock +++ b/public-yarn.lock @@ -4373,6 +4373,7 @@ __metadata: resolution: "@tldraw/file-format@workspace:packages/file-format" dependencies: "@tldraw/editor": "workspace:*" + "@tldraw/primitives": "workspace:*" "@tldraw/tlstore": "workspace:*" "@tldraw/tlvalidate": "workspace:*" "@tldraw/ui": "workspace:*"