@tldraw/tlschema
Version:
tldraw infinite canvas SDK (schema).
8 lines (7 loc) • 4.52 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/misc/TLCursor.ts"],
"sourcesContent": ["import { T } from '@tldraw/validate'\nimport { SetValue } from '../util-types'\n\n/**\n * All available cursor types used throughout the tldraw editor.\n *\n * These cursor types correspond to CSS cursor values and are used to indicate\n * different interaction modes and states within the editor. The cursor types\n * cover selection, resizing, rotation, text editing, and various other editor\n * interactions.\n *\n * @example\n * ```ts\n * // Check if a cursor type is valid\n * if (TL_CURSOR_TYPES.has('resize-corner')) {\n * console.log('Valid cursor type')\n * }\n *\n * // Get all available cursor types\n * const allCursors = Array.from(TL_CURSOR_TYPES)\n * ```\n *\n * @public\n */\nexport const TL_CURSOR_TYPES = new Set([\n\t'none',\n\t'default',\n\t'pointer',\n\t'cross',\n\t'grab',\n\t'rotate',\n\t'grabbing',\n\t'resize-edge',\n\t'resize-corner',\n\t'text',\n\t'move',\n\t'ew-resize',\n\t'ns-resize',\n\t'nesw-resize',\n\t'nwse-resize',\n\t'nesw-rotate',\n\t'nwse-rotate',\n\t'swne-rotate',\n\t'senw-rotate',\n\t'zoom-in',\n\t'zoom-out',\n])\n\n/**\n * A union type representing all available cursor types in the tldraw editor.\n *\n * Each cursor type corresponds to a different interaction mode or state,\n * helping users understand what action they can perform at any given moment.\n *\n * @example\n * ```ts\n * const defaultCursor: TLCursorType = 'default'\n * const textCursor: TLCursorType = 'text'\n * const resizeCursor: TLCursorType = 'resize-corner'\n * const rotateCursor: TLCursorType = 'nesw-rotate'\n * ```\n *\n * @public\n */\nexport type TLCursorType = SetValue<typeof TL_CURSOR_TYPES>\n\n/**\n * A validator for cursor types.\n *\n * This validator ensures that cursor type values are one of the valid types\n * defined in {@link TL_CURSOR_TYPES}. It provides runtime type checking for\n * cursor properties throughout the editor.\n *\n * @example\n * ```ts\n * import { cursorTypeValidator } from '@tldraw/tlschema'\n *\n * // Validate a cursor type\n * try {\n * const validCursor = cursorTypeValidator.validate('pointer')\n * console.log('Valid cursor:', validCursor)\n * } catch (error) {\n * console.error('Invalid cursor:', error.message)\n * }\n * ```\n *\n * @public\n */\nexport const cursorTypeValidator = T.setEnum(TL_CURSOR_TYPES)\n\n/**\n * A cursor object used throughout the tldraw editor.\n *\n * Represents both the cursor type (which determines the visual appearance)\n * and its rotation angle. The rotation is particularly useful for resize\n * and rotation cursors that need to align with the current interaction angle.\n *\n * @example\n * ```ts\n * // Default cursor\n * const defaultCursor: TLCursor = {\n * type: 'default',\n * rotation: 0\n * }\n *\n * // Rotated resize cursor\n * const rotatedResizeCursor: TLCursor = {\n * type: 'resize-corner',\n * rotation: Math.PI / 4 // 45 degrees\n * }\n *\n * // Text editing cursor\n * const textCursor: TLCursor = {\n * type: 'text',\n * rotation: 0\n * }\n * ```\n *\n * @public\n */\nexport interface TLCursor {\n\t/** The cursor type, determining the visual appearance and interaction mode */\n\ttype: TLCursorType\n\t/** The rotation angle in radians, used for rotated cursors like resize handles */\n\trotation: number\n}\n\n/**\n * A validator for TLCursor objects.\n *\n * This validator ensures that cursor objects have valid cursor types and\n * numeric rotation values. It provides runtime validation for cursor\n * properties used throughout the editor.\n *\n * @example\n * ```ts\n * import { cursorValidator } from '@tldraw/tlschema'\n *\n * // Validate a cursor object\n * try {\n * const validCursor = cursorValidator.validate({\n * type: 'pointer',\n * rotation: 0.5\n * })\n * console.log('Valid cursor:', validCursor)\n * } catch (error) {\n * console.error('Invalid cursor:', error.message)\n * }\n * ```\n *\n * @public\n */\nexport const cursorValidator: T.ObjectValidator<TLCursor> = T.object<TLCursor>({\n\ttype: cursorTypeValidator,\n\trotation: T.number,\n})\n"],
"mappings": "AAAA,SAAS,SAAS;AAwBX,MAAM,kBAAkB,oBAAI,IAAI;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,CAAC;AA0CM,MAAM,sBAAsB,EAAE,QAAQ,eAAe;AAgErD,MAAM,kBAA+C,EAAE,OAAiB;AAAA,EAC9E,MAAM;AAAA,EACN,UAAU,EAAE;AACb,CAAC;",
"names": []
}