UNPKG

@tldraw/tlschema

Version:

A tiny little drawing app (schema).

124 lines (123 loc) 3.16 kB
import { createMigrationIds, createRecordMigrationSequence, createRecordType } from "@tldraw/store"; import { mapObjectMapValues, uniqueId } from "@tldraw/utils"; import { T } from "@tldraw/validate"; import { createShapeValidator } from "../shapes/TLBaseShape.mjs"; import { StyleProp } from "../styles/StyleProp.mjs"; const rootShapeVersions = createMigrationIds("com.tldraw.shape", { AddIsLocked: 1, HoistOpacity: 2, AddMeta: 3, AddWhite: 4 }); const rootShapeMigrations = createRecordMigrationSequence({ sequenceId: "com.tldraw.shape", recordType: "shape", sequence: [ { id: rootShapeVersions.AddIsLocked, up: (record) => { record.isLocked = false; }, down: (record) => { delete record.isLocked; } }, { id: rootShapeVersions.HoistOpacity, up: (record) => { record.opacity = Number(record.props.opacity ?? "1"); delete record.props.opacity; }, down: (record) => { const opacity = record.opacity; delete record.opacity; record.props.opacity = opacity < 0.175 ? "0.1" : opacity < 0.375 ? "0.25" : opacity < 0.625 ? "0.5" : opacity < 0.875 ? "0.75" : "1"; } }, { id: rootShapeVersions.AddMeta, up: (record) => { record.meta = {}; } }, { id: rootShapeVersions.AddWhite, up: (_record) => { }, down: (record) => { if (record.props.color === "white") { record.props.color = "black"; } } } ] }); function isShape(record) { if (!record) return false; return record.typeName === "shape"; } function isShapeId(id) { if (!id) return false; return id.startsWith("shape:"); } function createShapeId(id) { return `shape:${id ?? uniqueId()}`; } function getShapePropKeysByStyle(props) { const propKeysByStyle = /* @__PURE__ */ new Map(); for (const [key, prop] of Object.entries(props)) { if (prop instanceof StyleProp) { if (propKeysByStyle.has(prop)) { throw new Error( `Duplicate style prop ${prop.id}. Each style prop can only be used once within a shape.` ); } propKeysByStyle.set(prop, key); } } return propKeysByStyle; } function createShapePropsMigrationSequence(migrations) { return migrations; } function createShapePropsMigrationIds(shapeType, ids) { return mapObjectMapValues(ids, (_k, v) => `com.tldraw.shape.${shapeType}/${v}`); } function createShapeRecordType(shapes) { return createRecordType("shape", { scope: "document", validator: T.model( "shape", T.union( "type", mapObjectMapValues( shapes, (type, { props, meta }) => createShapeValidator(type, props, meta) ) ) ) }).withDefaultProperties(() => ({ x: 0, y: 0, rotation: 0, isLocked: false, opacity: 1, meta: {} })); } export { createShapeId, createShapePropsMigrationIds, createShapePropsMigrationSequence, createShapeRecordType, getShapePropKeysByStyle, isShape, isShapeId, rootShapeMigrations, rootShapeVersions }; //# sourceMappingURL=TLShape.mjs.map