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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
  2. import React from 'react'
  3. /* -------------------------------------------------- */
  4. /* Client State */
  5. /* -------------------------------------------------- */
  6. export interface Data {
  7. isReadOnly: boolean
  8. settings: {
  9. fontSize: number
  10. isDarkMode: boolean
  11. isCodeOpen: boolean
  12. isStyleOpen: boolean
  13. nudgeDistanceSmall: number
  14. nudgeDistanceLarge: number
  15. isToolLocked: boolean
  16. isPenLocked: boolean
  17. }
  18. currentStyle: ShapeStyles
  19. activeTool: ShapeType | 'select'
  20. brush?: Bounds
  21. boundsRotation: number
  22. selectedIds: Set<string>
  23. pointedId?: string
  24. hoveredId?: string
  25. currentPageId: string
  26. currentParentId: string
  27. currentCodeFileId: string
  28. codeControls: Record<string, CodeControl>
  29. document: {
  30. pages: Record<string, Page>
  31. code: Record<string, CodeFile>
  32. }
  33. pageStates: Record<
  34. string,
  35. {
  36. camera: {
  37. point: number[]
  38. zoom: number
  39. }
  40. }
  41. >
  42. }
  43. /* -------------------------------------------------- */
  44. /* Document */
  45. /* -------------------------------------------------- */
  46. export interface Page {
  47. id: string
  48. type: 'page'
  49. childIndex: number
  50. name: string
  51. shapes: Record<string, Shape>
  52. }
  53. export enum ShapeType {
  54. Dot = 'dot',
  55. Circle = 'circle',
  56. Ellipse = 'ellipse',
  57. Line = 'line',
  58. Ray = 'ray',
  59. Polyline = 'polyline',
  60. Rectangle = 'rectangle',
  61. Draw = 'draw',
  62. Arrow = 'arrow',
  63. Text = 'text',
  64. Group = 'group',
  65. }
  66. export enum ColorStyle {
  67. White = 'White',
  68. LightGray = 'LightGray',
  69. Gray = 'Gray',
  70. Black = 'Black',
  71. Green = 'Green',
  72. Cyan = 'Cyan',
  73. Blue = 'Blue',
  74. Indigo = 'Indigo',
  75. Violet = 'Violet',
  76. Red = 'Red',
  77. Orange = 'Orange',
  78. Yellow = 'Yellow',
  79. }
  80. export enum SizeStyle {
  81. Small = 'Small',
  82. Medium = 'Medium',
  83. Large = 'Large',
  84. }
  85. export enum DashStyle {
  86. Solid = 'Solid',
  87. Dashed = 'Dashed',
  88. Dotted = 'Dotted',
  89. }
  90. export type ShapeStyles = {
  91. color: ColorStyle
  92. size: SizeStyle
  93. dash: DashStyle
  94. isFilled: boolean
  95. }
  96. export interface BaseShape {
  97. id: string
  98. type: ShapeType
  99. parentId: string
  100. childIndex: number
  101. isGenerated: boolean
  102. name: string
  103. point: number[]
  104. style: ShapeStyles
  105. rotation: number
  106. children?: string[]
  107. bindings?: Record<string, ShapeBinding>
  108. handles?: Record<string, ShapeHandle>
  109. isLocked: boolean
  110. isHidden: boolean
  111. isAspectRatioLocked: boolean
  112. }
  113. export interface DotShape extends BaseShape {
  114. type: ShapeType.Dot
  115. }
  116. export interface CircleShape extends BaseShape {
  117. type: ShapeType.Circle
  118. radius: number
  119. }
  120. export interface EllipseShape extends BaseShape {
  121. type: ShapeType.Ellipse
  122. radiusX: number
  123. radiusY: number
  124. }
  125. export interface LineShape extends BaseShape {
  126. type: ShapeType.Line
  127. direction: number[]
  128. }
  129. export interface RayShape extends BaseShape {
  130. type: ShapeType.Ray
  131. direction: number[]
  132. }
  133. export interface PolylineShape extends BaseShape {
  134. type: ShapeType.Polyline
  135. points: number[][]
  136. }
  137. export interface RectangleShape extends BaseShape {
  138. type: ShapeType.Rectangle
  139. size: number[]
  140. radius: number
  141. }
  142. export interface DrawShape extends BaseShape {
  143. type: ShapeType.Draw
  144. points: number[][]
  145. }
  146. export interface ArrowShape extends BaseShape {
  147. type: ShapeType.Arrow
  148. points: number[][]
  149. handles: Record<string, ShapeHandle>
  150. bend: number
  151. decorations?: {
  152. start: Decoration
  153. end: Decoration
  154. middle: Decoration
  155. }
  156. }
  157. export interface TextShape extends BaseShape {
  158. type: ShapeType.Text
  159. text: string
  160. }
  161. export interface GroupShape extends BaseShape {
  162. type: ShapeType.Group
  163. children: string[]
  164. size: number[]
  165. }
  166. export type MutableShape =
  167. | DotShape
  168. | CircleShape
  169. | EllipseShape
  170. | LineShape
  171. | RayShape
  172. | PolylineShape
  173. | DrawShape
  174. | RectangleShape
  175. | ArrowShape
  176. | TextShape
  177. | GroupShape
  178. export interface Shapes {
  179. [ShapeType.Dot]: Readonly<DotShape>
  180. [ShapeType.Circle]: Readonly<CircleShape>
  181. [ShapeType.Ellipse]: Readonly<EllipseShape>
  182. [ShapeType.Line]: Readonly<LineShape>
  183. [ShapeType.Ray]: Readonly<RayShape>
  184. [ShapeType.Polyline]: Readonly<PolylineShape>
  185. [ShapeType.Draw]: Readonly<DrawShape>
  186. [ShapeType.Rectangle]: Readonly<RectangleShape>
  187. [ShapeType.Arrow]: Readonly<ArrowShape>
  188. [ShapeType.Text]: Readonly<TextShape>
  189. [ShapeType.Group]: Readonly<GroupShape>
  190. }
  191. export type Shape = Readonly<MutableShape>
  192. export type ShapeByType<T extends ShapeType> = Shapes[T]
  193. export interface CodeFile {
  194. id: string
  195. name: string
  196. code: string
  197. }
  198. export enum Decoration {
  199. Arrow = 'Arrow',
  200. }
  201. export interface ShapeBinding {
  202. id: string
  203. index: number
  204. point: number[]
  205. }
  206. export interface ShapeHandle {
  207. id: string
  208. index: number
  209. point: number[]
  210. }
  211. /* -------------------------------------------------- */
  212. /* Editor UI */
  213. /* -------------------------------------------------- */
  214. export interface PointerInfo {
  215. target: string
  216. pointerId: number
  217. origin: number[]
  218. point: number[]
  219. shiftKey: boolean
  220. ctrlKey: boolean
  221. metaKey: boolean
  222. altKey: boolean
  223. }
  224. export enum Edge {
  225. Top = 'top_edge',
  226. Right = 'right_edge',
  227. Bottom = 'bottom_edge',
  228. Left = 'left_edge',
  229. }
  230. export enum Corner {
  231. TopLeft = 'top_left_corner',
  232. TopRight = 'top_right_corner',
  233. BottomRight = 'bottom_right_corner',
  234. BottomLeft = 'bottom_left_corner',
  235. }
  236. export interface Bounds {
  237. minX: number
  238. minY: number
  239. maxX: number
  240. maxY: number
  241. width: number
  242. height: number
  243. rotation?: number
  244. }
  245. export interface RotatedBounds extends Bounds {
  246. rotation: number
  247. }
  248. export interface ShapeBounds extends Bounds {
  249. id: string
  250. }
  251. export interface PointSnapshot extends Bounds {
  252. nx: number
  253. nmx: number
  254. ny: number
  255. nmy: number
  256. }
  257. export interface BoundsSnapshot extends PointSnapshot {
  258. nw: number
  259. nh: number
  260. }
  261. export type Difference<A, B> = A extends B ? never : A
  262. export type ShapeSpecificProps<T extends Shape> = Pick<
  263. T,
  264. Difference<keyof T, keyof BaseShape>
  265. >
  266. export type ShapeIndicatorProps<T extends Shape> = ShapeSpecificProps<T>
  267. export type ShapeUtil<K extends Shape> = {
  268. create(props: Partial<K>): K
  269. getBounds(shape: K): Bounds
  270. hitTest(shape: K, test: number[]): boolean
  271. hitTestBounds(shape: K, bounds: Bounds): boolean
  272. rotate(shape: K): K
  273. translate(shape: K, delta: number[]): K
  274. scale(shape: K, scale: number): K
  275. stretch(shape: K, scaleX: number, scaleY: number): K
  276. render(shape: K): JSX.Element
  277. }
  278. export enum MoveType {
  279. Backward,
  280. Forward,
  281. ToFront,
  282. ToBack,
  283. }
  284. export enum AlignType {
  285. Top,
  286. CenterVertical,
  287. Bottom,
  288. Left,
  289. CenterHorizontal,
  290. Right,
  291. }
  292. export enum StretchType {
  293. Horizontal,
  294. Vertical,
  295. }
  296. export enum DistributeType {
  297. Horizontal,
  298. Vertical,
  299. }
  300. /* -------------------------------------------------- */
  301. /* Code Editor */
  302. /* -------------------------------------------------- */
  303. export type IMonaco = typeof monaco
  304. export type IMonacoEditor = monaco.editor.IStandaloneCodeEditor
  305. export enum ControlType {
  306. Number = 'number',
  307. Vector = 'vector',
  308. Text = 'text',
  309. Select = 'select',
  310. }
  311. export interface BaseCodeControl {
  312. id: string
  313. type: ControlType
  314. label: string
  315. }
  316. export interface NumberCodeControl extends BaseCodeControl {
  317. type: ControlType.Number
  318. min?: number
  319. max?: number
  320. value: number
  321. step: number
  322. format?: (value: number) => number
  323. }
  324. export interface VectorCodeControl extends BaseCodeControl {
  325. type: ControlType.Vector
  326. value: number[]
  327. isNormalized: boolean
  328. format?: (value: number[]) => number[]
  329. }
  330. export interface TextCodeControl extends BaseCodeControl {
  331. type: ControlType.Text
  332. value: string
  333. format?: (value: string) => string
  334. }
  335. export interface SelectCodeControl<T extends string = ''>
  336. extends BaseCodeControl {
  337. type: ControlType.Select
  338. value: T
  339. options: T[]
  340. format?: (string: T) => string
  341. }
  342. export type CodeControl =
  343. | NumberCodeControl
  344. | VectorCodeControl
  345. | TextCodeControl
  346. | SelectCodeControl
  347. export type PropsOfType<T extends object, K> = {
  348. [K in keyof T]: T[K] extends boolean ? K : never
  349. }[keyof T]
  350. export type Mutable<T extends Shape> = { -readonly [K in keyof T]: T[K] }