You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

types.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
  2. /* -------------------------------------------------- */
  3. /* Client State */
  4. /* -------------------------------------------------- */
  5. export interface Data {
  6. isReadOnly: boolean
  7. settings: {
  8. fontSize: number
  9. isDarkMode: boolean
  10. isCodeOpen: boolean
  11. isStyleOpen: boolean
  12. nudgeDistanceSmall: number
  13. nudgeDistanceLarge: number
  14. isToolLocked: boolean
  15. isPenLocked: boolean
  16. }
  17. currentStyle: ShapeStyles
  18. activeTool: ShapeType | 'select'
  19. brush?: Bounds
  20. boundsRotation: number
  21. pointedId?: string
  22. hoveredId?: string
  23. editingId?: string
  24. currentPageId: string
  25. currentParentId: string
  26. currentCodeFileId: string
  27. codeControls: Record<string, CodeControl>
  28. document: TLDocument
  29. pageStates: Record<string, PageState>
  30. }
  31. /* -------------------------------------------------- */
  32. /* Document */
  33. /* -------------------------------------------------- */
  34. export interface TLDocument {
  35. id: string
  36. name: string
  37. pages: Record<string, Page>
  38. code: Record<string, CodeFile>
  39. }
  40. export interface Page {
  41. id: string
  42. type: 'page'
  43. childIndex: number
  44. name: string
  45. shapes: Record<string, Shape>
  46. }
  47. export interface PageState {
  48. id: string
  49. selectedIds: Set<string>
  50. camera: {
  51. point: number[]
  52. zoom: number
  53. }
  54. }
  55. export enum ShapeType {
  56. Dot = 'dot',
  57. Circle = 'circle',
  58. Ellipse = 'ellipse',
  59. Line = 'line',
  60. Ray = 'ray',
  61. Polyline = 'polyline',
  62. Rectangle = 'rectangle',
  63. Draw = 'draw',
  64. Arrow = 'arrow',
  65. Text = 'text',
  66. Group = 'group',
  67. }
  68. export enum ColorStyle {
  69. White = 'White',
  70. LightGray = 'LightGray',
  71. Gray = 'Gray',
  72. Black = 'Black',
  73. Green = 'Green',
  74. Cyan = 'Cyan',
  75. Blue = 'Blue',
  76. Indigo = 'Indigo',
  77. Violet = 'Violet',
  78. Red = 'Red',
  79. Orange = 'Orange',
  80. Yellow = 'Yellow',
  81. }
  82. export enum SizeStyle {
  83. Small = 'Small',
  84. Medium = 'Medium',
  85. Large = 'Large',
  86. }
  87. export enum DashStyle {
  88. Solid = 'Solid',
  89. Dashed = 'Dashed',
  90. Dotted = 'Dotted',
  91. }
  92. export enum FontSize {
  93. Small = 'Small',
  94. Medium = 'Medium',
  95. Large = 'Large',
  96. ExtraLarge = 'ExtraLarge',
  97. }
  98. export type ShapeStyles = {
  99. color: ColorStyle
  100. size: SizeStyle
  101. dash: DashStyle
  102. isFilled: boolean
  103. }
  104. export interface BaseShape {
  105. id: string
  106. seed: number
  107. type: ShapeType
  108. parentId: string
  109. childIndex: number
  110. isGenerated: boolean
  111. name: string
  112. point: number[]
  113. style: ShapeStyles
  114. rotation: number
  115. children?: string[]
  116. bindings?: Record<string, ShapeBinding>
  117. handles?: Record<string, ShapeHandle>
  118. isLocked: boolean
  119. isHidden: boolean
  120. isAspectRatioLocked: boolean
  121. }
  122. export interface DotShape extends BaseShape {
  123. type: ShapeType.Dot
  124. }
  125. export interface CircleShape extends BaseShape {
  126. type: ShapeType.Circle
  127. radius: number
  128. }
  129. export interface EllipseShape extends BaseShape {
  130. type: ShapeType.Ellipse
  131. radiusX: number
  132. radiusY: number
  133. }
  134. export interface LineShape extends BaseShape {
  135. type: ShapeType.Line
  136. direction: number[]
  137. }
  138. export interface RayShape extends BaseShape {
  139. type: ShapeType.Ray
  140. direction: number[]
  141. }
  142. export interface PolylineShape extends BaseShape {
  143. type: ShapeType.Polyline
  144. points: number[][]
  145. }
  146. export interface RectangleShape extends BaseShape {
  147. type: ShapeType.Rectangle
  148. size: number[]
  149. radius: number
  150. }
  151. export interface DrawShape extends BaseShape {
  152. type: ShapeType.Draw
  153. points: number[][]
  154. }
  155. export interface ArrowShape extends BaseShape {
  156. type: ShapeType.Arrow
  157. handles: Record<string, ShapeHandle>
  158. bend: number
  159. decorations?: {
  160. start: Decoration
  161. end: Decoration
  162. middle: Decoration
  163. }
  164. }
  165. export interface TextShape extends BaseShape {
  166. type: ShapeType.Text
  167. text: string
  168. size: number[] | 'auto'
  169. scale: number
  170. fontSize: FontSize
  171. }
  172. export interface GroupShape extends BaseShape {
  173. type: ShapeType.Group
  174. children: string[]
  175. size: number[]
  176. }
  177. export type MutableShape =
  178. | DotShape
  179. | CircleShape
  180. | EllipseShape
  181. | LineShape
  182. | RayShape
  183. | PolylineShape
  184. | DrawShape
  185. | RectangleShape
  186. | ArrowShape
  187. | TextShape
  188. | GroupShape
  189. export interface Shapes {
  190. [ShapeType.Dot]: Readonly<DotShape>
  191. [ShapeType.Circle]: Readonly<CircleShape>
  192. [ShapeType.Ellipse]: Readonly<EllipseShape>
  193. [ShapeType.Line]: Readonly<LineShape>
  194. [ShapeType.Ray]: Readonly<RayShape>
  195. [ShapeType.Polyline]: Readonly<PolylineShape>
  196. [ShapeType.Draw]: Readonly<DrawShape>
  197. [ShapeType.Rectangle]: Readonly<RectangleShape>
  198. [ShapeType.Arrow]: Readonly<ArrowShape>
  199. [ShapeType.Text]: Readonly<TextShape>
  200. [ShapeType.Group]: Readonly<GroupShape>
  201. }
  202. export type Shape = Readonly<MutableShape>
  203. export type ShapeByType<T extends ShapeType> = Shapes[T]
  204. export interface CodeFile {
  205. id: string
  206. name: string
  207. code: string
  208. }
  209. export enum Decoration {
  210. Arrow = 'Arrow',
  211. }
  212. export interface ShapeBinding {
  213. id: string
  214. index: number
  215. point: number[]
  216. }
  217. export interface ShapeHandle {
  218. id: string
  219. index: number
  220. point: number[]
  221. }
  222. /* -------------------------------------------------- */
  223. /* Editor UI */
  224. /* -------------------------------------------------- */
  225. export interface PointerInfo {
  226. target: string
  227. pointerId: number
  228. origin: number[]
  229. point: number[]
  230. pressure: number
  231. shiftKey: boolean
  232. ctrlKey: boolean
  233. metaKey: boolean
  234. altKey: boolean
  235. }
  236. export enum Edge {
  237. Top = 'top_edge',
  238. Right = 'right_edge',
  239. Bottom = 'bottom_edge',
  240. Left = 'left_edge',
  241. }
  242. export enum Corner {
  243. TopLeft = 'top_left_corner',
  244. TopRight = 'top_right_corner',
  245. BottomRight = 'bottom_right_corner',
  246. BottomLeft = 'bottom_left_corner',
  247. }
  248. export interface Bounds {
  249. minX: number
  250. minY: number
  251. maxX: number
  252. maxY: number
  253. width: number
  254. height: number
  255. rotation?: number
  256. }
  257. export interface RotatedBounds extends Bounds {
  258. rotation: number
  259. }
  260. export interface ShapeBounds extends Bounds {
  261. id: string
  262. }
  263. export interface PointSnapshot extends Bounds {
  264. nx: number
  265. nmx: number
  266. ny: number
  267. nmy: number
  268. }
  269. export interface BoundsSnapshot extends PointSnapshot {
  270. nw: number
  271. nh: number
  272. }
  273. export type Difference<A, B> = A extends B ? never : A
  274. export type ShapeSpecificProps<T extends Shape> = Pick<
  275. T,
  276. Difference<keyof T, keyof BaseShape>
  277. >
  278. export type ShapeIndicatorProps<T extends Shape> = ShapeSpecificProps<T>
  279. export type ShapeUtil<K extends Shape> = {
  280. create(props: Partial<K>): K
  281. getBounds(shape: K): Bounds
  282. hitTest(shape: K, test: number[]): boolean
  283. hitTestBounds(shape: K, bounds: Bounds): boolean
  284. rotate(shape: K): K
  285. translate(shape: K, delta: number[]): K
  286. scale(shape: K, scale: number): K
  287. stretch(shape: K, scaleX: number, scaleY: number): K
  288. render(shape: K): JSX.Element
  289. }
  290. export enum MoveType {
  291. Backward,
  292. Forward,
  293. ToFront,
  294. ToBack,
  295. }
  296. export enum AlignType {
  297. Top,
  298. CenterVertical,
  299. Bottom,
  300. Left,
  301. CenterHorizontal,
  302. Right,
  303. }
  304. export enum StretchType {
  305. Horizontal,
  306. Vertical,
  307. }
  308. export enum DistributeType {
  309. Horizontal,
  310. Vertical,
  311. }
  312. /* -------------------------------------------------- */
  313. /* Code Editor */
  314. /* -------------------------------------------------- */
  315. export type IMonaco = typeof monaco
  316. export type IMonacoEditor = monaco.editor.IStandaloneCodeEditor
  317. export enum ControlType {
  318. Number = 'number',
  319. Vector = 'vector',
  320. Text = 'text',
  321. Select = 'select',
  322. }
  323. export interface BaseCodeControl {
  324. id: string
  325. type: ControlType
  326. label: string
  327. }
  328. export interface NumberCodeControl extends BaseCodeControl {
  329. type: ControlType.Number
  330. min?: number
  331. max?: number
  332. value: number
  333. step: number
  334. format?: (value: number) => number
  335. }
  336. export interface VectorCodeControl extends BaseCodeControl {
  337. type: ControlType.Vector
  338. value: number[]
  339. isNormalized: boolean
  340. format?: (value: number[]) => number[]
  341. }
  342. export interface TextCodeControl extends BaseCodeControl {
  343. type: ControlType.Text
  344. value: string
  345. format?: (value: string) => string
  346. }
  347. export interface SelectCodeControl<T extends string = ''>
  348. extends BaseCodeControl {
  349. type: ControlType.Select
  350. value: T
  351. options: T[]
  352. format?: (string: T) => string
  353. }
  354. export type CodeControl =
  355. | NumberCodeControl
  356. | VectorCodeControl
  357. | TextCodeControl
  358. | SelectCodeControl
  359. export type PropsOfType<T extends Record<string, unknown>> = {
  360. [K in keyof T]: T[K] extends boolean ? K : never
  361. }[keyof T]
  362. export type Mutable<T extends Shape> = { -readonly [K in keyof T]: T[K] }
  363. export interface ShapeUtility<K extends Shape> {
  364. // A cache for the computed bounds of this kind of shape.
  365. boundsCache: WeakMap<K, Bounds>
  366. // Whether to show transform controls when this shape is selected.
  367. canTransform: boolean
  368. // Whether the shape's aspect ratio can change.
  369. canChangeAspectRatio: boolean
  370. // Whether the shape's style can be filled.
  371. canStyleFill: boolean
  372. // Whether the shape may be edited in an editing mode
  373. canEdit: boolean
  374. // Whether the shape is a foreign object.
  375. isForeignObject: boolean
  376. // Whether the shape can contain other shapes.
  377. isParent: boolean
  378. // Whether the shape is only shown when on hovered.
  379. isShy: boolean
  380. // Create a new shape.
  381. create(props: Partial<K>): K
  382. // Update a shape's styles
  383. applyStyles(
  384. this: ShapeUtility<K>,
  385. shape: Mutable<K>,
  386. style: Partial<ShapeStyles>
  387. ): ShapeUtility<K>
  388. translateBy(
  389. this: ShapeUtility<K>,
  390. shape: Mutable<K>,
  391. point: number[]
  392. ): ShapeUtility<K>
  393. translateTo(
  394. this: ShapeUtility<K>,
  395. shape: Mutable<K>,
  396. point: number[]
  397. ): ShapeUtility<K>
  398. rotateBy(
  399. this: ShapeUtility<K>,
  400. shape: Mutable<K>,
  401. rotation: number
  402. ): ShapeUtility<K>
  403. rotateTo(
  404. this: ShapeUtility<K>,
  405. shape: Mutable<K>,
  406. rotation: number,
  407. delta: number
  408. ): ShapeUtility<K>
  409. // Transform to fit a new bounding box when more than one shape is selected.
  410. transform(
  411. this: ShapeUtility<K>,
  412. shape: Mutable<K>,
  413. bounds: Bounds,
  414. info: {
  415. type: Edge | Corner
  416. initialShape: K
  417. scaleX: number
  418. scaleY: number
  419. transformOrigin: number[]
  420. }
  421. ): ShapeUtility<K>
  422. // Transform a single shape to fit a new bounding box.
  423. transformSingle(
  424. this: ShapeUtility<K>,
  425. shape: Mutable<K>,
  426. bounds: Bounds,
  427. info: {
  428. type: Edge | Corner
  429. initialShape: K
  430. scaleX: number
  431. scaleY: number
  432. transformOrigin: number[]
  433. }
  434. ): ShapeUtility<K>
  435. setProperty<P extends keyof K>(
  436. this: ShapeUtility<K>,
  437. shape: Mutable<K>,
  438. prop: P,
  439. value: K[P]
  440. ): ShapeUtility<K>
  441. // Respond when any child of this shape changes.
  442. onChildrenChange(
  443. this: ShapeUtility<K>,
  444. shape: Mutable<K>,
  445. children: Shape[]
  446. ): ShapeUtility<K>
  447. // Respond when a user moves one of the shape's bound elements.
  448. onBindingChange(
  449. this: ShapeUtility<K>,
  450. shape: Mutable<K>,
  451. bindings: Record<string, ShapeBinding>
  452. ): ShapeUtility<K>
  453. // Respond when a user moves one of the shape's handles.
  454. onHandleChange(
  455. this: ShapeUtility<K>,
  456. shape: Mutable<K>,
  457. handle: Partial<K['handles']>
  458. ): ShapeUtility<K>
  459. // Respond when a user double clicks the shape's bounds.
  460. onBoundsReset(this: ShapeUtility<K>, shape: Mutable<K>): ShapeUtility<K>
  461. // Respond when a user double clicks the center of the shape.
  462. onDoubleFocus(this: ShapeUtility<K>, shape: Mutable<K>): ShapeUtility<K>
  463. // Clean up changes when a session ends.
  464. onSessionComplete(this: ShapeUtility<K>, shape: Mutable<K>): ShapeUtility<K>
  465. // Render a shape to JSX.
  466. render(
  467. this: ShapeUtility<K>,
  468. shape: K,
  469. info: {
  470. isEditing: boolean
  471. ref?: React.MutableRefObject<HTMLTextAreaElement>
  472. }
  473. ): JSX.Element
  474. invalidate(this: ShapeUtility<K>, shape: K): ShapeUtility<K>
  475. // Get the bounds of the a shape.
  476. getBounds(this: ShapeUtility<K>, shape: K): Bounds
  477. // Get the routated bounds of the a shape.
  478. getRotatedBounds(this: ShapeUtility<K>, shape: K): Bounds
  479. // Get the center of the shape
  480. getCenter(this: ShapeUtility<K>, shape: K): number[]
  481. // Test whether a point lies within a shape.
  482. hitTest(this: ShapeUtility<K>, shape: K, test: number[]): boolean
  483. // Test whether bounds collide with or contain a shape.
  484. hitTestBounds(this: ShapeUtility<K>, shape: K, bounds: Bounds): boolean
  485. shouldDelete(this: ShapeUtility<K>, shape: K): boolean
  486. }