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

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