您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

types.ts 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. }
  14. currentStyle: ShapeStyles
  15. camera: {
  16. point: number[]
  17. zoom: number
  18. }
  19. brush?: Bounds
  20. boundsRotation: number
  21. selectedIds: Set<string>
  22. pointedId?: string
  23. hoveredId?: string
  24. currentPageId: string
  25. currentCodeFileId: string
  26. codeControls: Record<string, CodeControl>
  27. document: {
  28. pages: Record<string, Page>
  29. code: Record<string, CodeFile>
  30. }
  31. }
  32. /* -------------------------------------------------- */
  33. /* Document */
  34. /* -------------------------------------------------- */
  35. export interface Page {
  36. id: string
  37. type: "page"
  38. childIndex: number
  39. name: string
  40. shapes: Record<string, Shape>
  41. }
  42. export enum ShapeType {
  43. Dot = "dot",
  44. Circle = "circle",
  45. Ellipse = "ellipse",
  46. Line = "line",
  47. Ray = "ray",
  48. Polyline = "polyline",
  49. Rectangle = "rectangle",
  50. }
  51. // Consider:
  52. // Glob = "glob",
  53. // Spline = "spline",
  54. // Cubic = "cubic",
  55. // Conic = "conic",
  56. export type ShapeStyles = Partial<React.SVGProps<SVGUseElement>>
  57. export interface BaseShape {
  58. id: string
  59. type: ShapeType
  60. parentId: string
  61. childIndex: number
  62. isGenerated: boolean
  63. name: string
  64. point: number[]
  65. rotation: number
  66. style: ShapeStyles
  67. }
  68. export interface DotShape extends BaseShape {
  69. type: ShapeType.Dot
  70. }
  71. export interface CircleShape extends BaseShape {
  72. type: ShapeType.Circle
  73. radius: number
  74. }
  75. export interface EllipseShape extends BaseShape {
  76. type: ShapeType.Ellipse
  77. radiusX: number
  78. radiusY: number
  79. }
  80. export interface LineShape extends BaseShape {
  81. type: ShapeType.Line
  82. direction: number[]
  83. }
  84. export interface RayShape extends BaseShape {
  85. type: ShapeType.Ray
  86. direction: number[]
  87. }
  88. export interface PolylineShape extends BaseShape {
  89. type: ShapeType.Polyline
  90. points: number[][]
  91. }
  92. export interface RectangleShape extends BaseShape {
  93. type: ShapeType.Rectangle
  94. size: number[]
  95. radius: number
  96. }
  97. export type MutableShape =
  98. | DotShape
  99. | CircleShape
  100. | EllipseShape
  101. | LineShape
  102. | RayShape
  103. | PolylineShape
  104. | RectangleShape
  105. export type Shape = Readonly<MutableShape>
  106. export interface Shapes {
  107. [ShapeType.Dot]: Readonly<DotShape>
  108. [ShapeType.Circle]: Readonly<CircleShape>
  109. [ShapeType.Ellipse]: Readonly<EllipseShape>
  110. [ShapeType.Line]: Readonly<LineShape>
  111. [ShapeType.Ray]: Readonly<RayShape>
  112. [ShapeType.Polyline]: Readonly<PolylineShape>
  113. [ShapeType.Rectangle]: Readonly<RectangleShape>
  114. }
  115. export type ShapeByType<T extends ShapeType> = Shapes[T]
  116. export interface CodeFile {
  117. id: string
  118. name: string
  119. code: string
  120. }
  121. /* -------------------------------------------------- */
  122. /* Editor UI */
  123. /* -------------------------------------------------- */
  124. export interface PointerInfo {
  125. target: string
  126. pointerId: number
  127. origin: number[]
  128. point: number[]
  129. shiftKey: boolean
  130. ctrlKey: boolean
  131. metaKey: boolean
  132. altKey: boolean
  133. }
  134. export enum Edge {
  135. Top = "top_edge",
  136. Right = "right_edge",
  137. Bottom = "bottom_edge",
  138. Left = "left_edge",
  139. }
  140. export enum Corner {
  141. TopLeft = "top_left_corner",
  142. TopRight = "top_right_corner",
  143. BottomRight = "bottom_right_corner",
  144. BottomLeft = "bottom_left_corner",
  145. }
  146. export interface Bounds {
  147. minX: number
  148. minY: number
  149. maxX: number
  150. maxY: number
  151. width: number
  152. height: number
  153. }
  154. export interface RotatedBounds extends Bounds {
  155. rotation: number
  156. }
  157. export interface ShapeBounds extends Bounds {
  158. id: string
  159. }
  160. export interface PointSnapshot extends Bounds {
  161. nx: number
  162. nmx: number
  163. ny: number
  164. nmy: number
  165. }
  166. export interface BoundsSnapshot extends PointSnapshot {
  167. nw: number
  168. nh: number
  169. }
  170. export type Difference<A, B> = A extends B ? never : A
  171. export type ShapeSpecificProps<T extends Shape> = Pick<
  172. T,
  173. Difference<keyof T, keyof BaseShape>
  174. >
  175. export type ShapeIndicatorProps<T extends Shape> = ShapeSpecificProps<T>
  176. export type ShapeUtil<K extends Shape> = {
  177. create(props: Partial<K>): K
  178. getBounds(shape: K): Bounds
  179. hitTest(shape: K, test: number[]): boolean
  180. hitTestBounds(shape: K, bounds: Bounds): boolean
  181. rotate(shape: K): K
  182. translate(shape: K, delta: number[]): K
  183. scale(shape: K, scale: number): K
  184. stretch(shape: K, scaleX: number, scaleY: number): K
  185. render(shape: K): JSX.Element
  186. }
  187. export enum MoveType {
  188. Backward,
  189. Forward,
  190. ToFront,
  191. ToBack,
  192. }
  193. export enum AlignType {
  194. Top,
  195. CenterVertical,
  196. Bottom,
  197. Left,
  198. CenterHorizontal,
  199. Right,
  200. }
  201. export enum StretchType {
  202. Horizontal,
  203. Vertical,
  204. }
  205. export enum DistributeType {
  206. Horizontal,
  207. Vertical,
  208. }
  209. /* -------------------------------------------------- */
  210. /* Code Editor */
  211. /* -------------------------------------------------- */
  212. export type IMonaco = typeof monaco
  213. export type IMonacoEditor = monaco.editor.IStandaloneCodeEditor
  214. export enum ControlType {
  215. Number = "number",
  216. Vector = "vector",
  217. Text = "text",
  218. Select = "select",
  219. }
  220. export interface BaseCodeControl {
  221. id: string
  222. type: ControlType
  223. label: string
  224. }
  225. export interface NumberCodeControl extends BaseCodeControl {
  226. type: ControlType.Number
  227. min?: number
  228. max?: number
  229. value: number
  230. step: number
  231. format?: (value: number) => number
  232. }
  233. export interface VectorCodeControl extends BaseCodeControl {
  234. type: ControlType.Vector
  235. value: number[]
  236. isNormalized: boolean
  237. format?: (value: number[]) => number[]
  238. }
  239. export interface TextCodeControl extends BaseCodeControl {
  240. type: ControlType.Text
  241. value: string
  242. format?: (value: string) => string
  243. }
  244. export interface SelectCodeControl<T extends string = "">
  245. extends BaseCodeControl {
  246. type: ControlType.Select
  247. value: T
  248. options: T[]
  249. format?: (string: T) => string
  250. }
  251. export type CodeControl =
  252. | NumberCodeControl
  253. | VectorCodeControl
  254. | TextCodeControl
  255. | SelectCodeControl