{
  "version": 3,
  "sources": ["../../src/records/TLDocument.ts"],
  "sourcesContent": ["import {\n\tBaseRecord,\n\tcreateMigrationIds,\n\tcreateRecordMigrationSequence,\n\tcreateRecordType,\n\tRecordId,\n\tUnknownRecord,\n} from '@tldraw/store'\nimport { JsonObject } from '@tldraw/utils'\nimport { T } from '@tldraw/validate'\n\n/**\n * Document record containing global settings and metadata for a tldraw document.\n * There is exactly one document record per tldraw instance with a fixed ID.\n *\n * @example\n * ```ts\n * const document: TLDocument = {\n *   id: 'document:document',\n *   typeName: 'document',\n *   gridSize: 20,        // Grid snap size in pixels\n *   name: 'My Drawing',  // Document name\n *   meta: {\n *     createdAt: Date.now(),\n *     author: 'user123',\n *     version: '1.0.0'\n *   }\n * }\n *\n * // Update document settings\n * editor.updateDocumentSettings({\n *   name: 'Updated Drawing',\n *   gridSize: 25\n * })\n * ```\n *\n * @public\n */\nexport interface TLDocument extends BaseRecord<'document', RecordId<TLDocument>> {\n\t/** Grid snap size in pixels. Used for shape positioning and alignment */\n\tgridSize: number\n\t/** Human-readable name of the document */\n\tname: string\n\t/** User-defined metadata for the document */\n\tmeta: JsonObject\n}\n\n/**\n * Validator for TLDocument records that ensures runtime type safety.\n * Enforces the fixed document ID and validates all document properties.\n *\n * @example\n * ```ts\n * // Validation happens automatically when document is stored\n * try {\n *   const validatedDocument = documentValidator.validate(documentData)\n *   store.put([validatedDocument])\n * } catch (error) {\n *   console.error('Document validation failed:', error.message)\n * }\n * ```\n *\n * @public\n */\nexport const documentValidator: T.Validator<TLDocument> = T.model(\n\t'document',\n\tT.object({\n\t\ttypeName: T.literal('document'),\n\t\tid: T.literal('document:document' as RecordId<TLDocument>),\n\t\tgridSize: T.number,\n\t\tname: T.string,\n\t\tmeta: T.jsonValue as T.ObjectValidator<JsonObject>,\n\t})\n)\n\n/**\n * Type guard to check if a record is a TLDocument.\n * Useful for filtering or type narrowing when working with mixed record types.\n *\n * @param record - The record to check\n * @returns True if the record is a document, false otherwise\n *\n * @example\n * ```ts\n * // Type guard usage\n * function processRecord(record: UnknownRecord) {\n *   if (isDocument(record)) {\n *     // record is now typed as TLDocument\n *     console.log(`Document: ${record.name}, Grid: ${record.gridSize}px`)\n *   }\n * }\n *\n * // Filter documents from mixed records\n * const allRecords = store.allRecords()\n * const documents = allRecords.filter(isDocument) // Should be exactly one\n * ```\n *\n * @public\n */\nexport function isDocument(record?: UnknownRecord): record is TLDocument {\n\tif (!record) return false\n\treturn record.typeName === 'document'\n}\n\n/**\n * Migration version identifiers for document record schema evolution.\n * Each version represents a breaking change that requires data migration.\n *\n * @example\n * ```ts\n * // Check if document needs migration\n * const needsNameMigration = currentVersion < documentVersions.AddName\n * const needsMetaMigration = currentVersion < documentVersions.AddMeta\n * ```\n *\n * @public\n */\nexport const documentVersions = createMigrationIds('com.tldraw.document', {\n\tAddName: 1,\n\tAddMeta: 2,\n} as const)\n\n/**\n * Migration sequence for evolving document record structure over time.\n * Handles converting document records from older schema versions to current format.\n *\n * @example\n * ```ts\n * // Migration is applied automatically when loading old documents\n * const migratedStore = migrator.migrateStoreSnapshot({\n *   schema: oldSchema,\n *   store: oldStoreSnapshot\n * })\n *\n * // The migrations:\n * // v1: Added 'name' property with empty string default\n * // v2: Added 'meta' property with empty object default\n * ```\n *\n * @public\n */\nexport const documentMigrations = createRecordMigrationSequence({\n\tsequenceId: 'com.tldraw.document',\n\trecordType: 'document',\n\tsequence: [\n\t\t{\n\t\t\tid: documentVersions.AddName,\n\t\t\tup: (document) => {\n\t\t\t\t;(document as any).name = ''\n\t\t\t},\n\t\t\tdown: (document) => {\n\t\t\t\tdelete (document as any).name\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tid: documentVersions.AddMeta,\n\t\t\tup: (record) => {\n\t\t\t\t;(record as any).meta = {}\n\t\t\t},\n\t\t},\n\t],\n})\n\n/**\n * Record type definition for TLDocument with validation and default properties.\n * Configures the document as a document-scoped record that persists across sessions.\n *\n * @example\n * ```ts\n * // Create a document record (usually done automatically)\n * const documentRecord = DocumentRecordType.create({\n *   id: TLDOCUMENT_ID,\n *   name: 'My Drawing',\n *   gridSize: 20,\n *   meta: { createdAt: Date.now() }\n * })\n *\n * // Create with defaults\n * const defaultDocument = DocumentRecordType.create({\n *   id: TLDOCUMENT_ID\n *   // gridSize: 10, name: '', meta: {} are applied as defaults\n * })\n *\n * // Store the document\n * store.put([documentRecord])\n * ```\n *\n * @public\n */\nexport const DocumentRecordType = createRecordType<TLDocument>('document', {\n\tvalidator: documentValidator,\n\tscope: 'document',\n}).withDefaultProperties(\n\t(): Omit<TLDocument, 'id' | 'typeName'> => ({\n\t\tgridSize: 10,\n\t\tname: '',\n\t\tmeta: {},\n\t})\n)\n\n/**\n * The fixed ID for the singleton document record in every tldraw store.\n * All document records use this same ID: 'document:document'\n *\n * @example\n * ```ts\n * // Get the document from store\n * const document = store.get(TLDOCUMENT_ID)\n *\n * // Update document settings\n * store.put([{\n *   ...document,\n *   name: 'Updated Name',\n *   gridSize: 25\n * }])\n *\n * // Access via editor\n * const documentSettings = editor.getDocumentSettings()\n * editor.updateDocumentSettings({ name: 'New Name' })\n * ```\n *\n * @public\n */\nexport const TLDOCUMENT_ID: RecordId<TLDocument> = DocumentRecordType.createId('document')\n"],
  "mappings": "AAAA;AAAA,EAEC;AAAA,EACA;AAAA,EACA;AAAA,OAGM;AAEP,SAAS,SAAS;AAuDX,MAAM,oBAA6C,EAAE;AAAA,EAC3D;AAAA,EACA,EAAE,OAAO;AAAA,IACR,UAAU,EAAE,QAAQ,UAAU;AAAA,IAC9B,IAAI,EAAE,QAAQ,mBAA2C;AAAA,IACzD,UAAU,EAAE;AAAA,IACZ,MAAM,EAAE;AAAA,IACR,MAAM,EAAE;AAAA,EACT,CAAC;AACF;AA0BO,SAAS,WAAW,QAA8C;AACxE,MAAI,CAAC,OAAQ,QAAO;AACpB,SAAO,OAAO,aAAa;AAC5B;AAeO,MAAM,mBAAmB,mBAAmB,uBAAuB;AAAA,EACzE,SAAS;AAAA,EACT,SAAS;AACV,CAAU;AAqBH,MAAM,qBAAqB,8BAA8B;AAAA,EAC/D,YAAY;AAAA,EACZ,YAAY;AAAA,EACZ,UAAU;AAAA,IACT;AAAA,MACC,IAAI,iBAAiB;AAAA,MACrB,IAAI,CAAC,aAAa;AACjB;AAAC,QAAC,SAAiB,OAAO;AAAA,MAC3B;AAAA,MACA,MAAM,CAAC,aAAa;AACnB,eAAQ,SAAiB;AAAA,MAC1B;AAAA,IACD;AAAA,IACA;AAAA,MACC,IAAI,iBAAiB;AAAA,MACrB,IAAI,CAAC,WAAW;AACf;AAAC,QAAC,OAAe,OAAO,CAAC;AAAA,MAC1B;AAAA,IACD;AAAA,EACD;AACD,CAAC;AA4BM,MAAM,qBAAqB,iBAA6B,YAAY;AAAA,EAC1E,WAAW;AAAA,EACX,OAAO;AACR,CAAC,EAAE;AAAA,EACF,OAA4C;AAAA,IAC3C,UAAU;AAAA,IACV,MAAM;AAAA,IACN,MAAM,CAAC;AAAA,EACR;AACD;AAyBO,MAAM,gBAAsC,mBAAmB,SAAS,UAAU;",
  "names": []
}
