tldraw
Version:
A tiny little drawing editor.
8 lines (7 loc) • 2.08 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/lib/utils/test-helpers.ts"],
"sourcesContent": ["import type { TLDrawShapeSegment, VecModel } from '@tldraw/editor'\nimport { b64Vecs, compressLegacySegments } from '@tldraw/editor'\n\n/**\n * Helper function to convert draw shape points from VecModel[] to base64 string.\n * Uses delta encoding for improved Float16 precision.\n * This is useful for tests that create draw shapes with the legacy array format.\n *\n * @example\n * ```ts\n * const segments = [{ type: 'free', path: pointsToBase64([{x: 0, y: 0, z: 0.5}]) }]\n * ```\n *\n * @public\n */\nexport function pointsToBase64(points: VecModel[]): string {\n\treturn b64Vecs.encodePoints(points)\n}\n\n/**\n * Helper function to convert base64 string back to VecModel[] points.\n * Decodes delta-encoded points to absolute coordinates.\n * This is useful for tests that need to inspect draw shape points.\n *\n * @example\n * ```ts\n * const points = base64ToPoints(shape.props.segments[0].path)\n * expect(points[0].x).toBe(0)\n * ```\n *\n * @public\n */\nexport function base64ToPoints(base64: string): VecModel[] {\n\treturn b64Vecs.decodePoints(base64)\n}\n\n/**\n * Helper function to create draw shape segments from legacy array format.\n * This allows tests to use the old format while the shape uses the new base64 format.\n *\n * @example\n * ```ts\n * editor.createShapes([{\n * type: 'draw',\n * props: {\n * segments: createDrawSegments([[{x: 0, y: 0}, {x: 10, y: 10}]])\n * }\n * }])\n * ```\n * @public\n */\nexport function createDrawSegments(\n\tpointArrays: VecModel[][],\n\ttype: 'free' | 'straight' = 'free'\n): TLDrawShapeSegment[] {\n\treturn compressLegacySegments(\n\t\tpointArrays.map((points) => ({\n\t\t\ttype,\n\t\t\tpoints,\n\t\t}))\n\t)\n}\n"],
"mappings": "AACA,SAAS,SAAS,8BAA8B;AAczC,SAAS,eAAe,QAA4B;AAC1D,SAAO,QAAQ,aAAa,MAAM;AACnC;AAeO,SAAS,eAAe,QAA4B;AAC1D,SAAO,QAAQ,aAAa,MAAM;AACnC;AAiBO,SAAS,mBACf,aACA,OAA4B,QACL;AACvB,SAAO;AAAA,IACN,YAAY,IAAI,CAAC,YAAY;AAAA,MAC5B;AAAA,MACA;AAAA,IACD,EAAE;AAAA,EACH;AACD;",
"names": []
}