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

types.ts 5.6KB

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