選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

types.ts 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. pressure: number
  220. shiftKey: boolean
  221. ctrlKey: boolean
  222. metaKey: boolean
  223. altKey: boolean
  224. }
  225. export enum Edge {
  226. Top = 'top_edge',
  227. Right = 'right_edge',
  228. Bottom = 'bottom_edge',
  229. Left = 'left_edge',
  230. }
  231. export enum Corner {
  232. TopLeft = 'top_left_corner',
  233. TopRight = 'top_right_corner',
  234. BottomRight = 'bottom_right_corner',
  235. BottomLeft = 'bottom_left_corner',
  236. }
  237. export interface Bounds {
  238. minX: number
  239. minY: number
  240. maxX: number
  241. maxY: number
  242. width: number
  243. height: number
  244. rotation?: number
  245. }
  246. export interface RotatedBounds extends Bounds {
  247. rotation: number
  248. }
  249. export interface ShapeBounds extends Bounds {
  250. id: string
  251. }
  252. export interface PointSnapshot extends Bounds {
  253. nx: number
  254. nmx: number
  255. ny: number
  256. nmy: number
  257. }
  258. export interface BoundsSnapshot extends PointSnapshot {
  259. nw: number
  260. nh: number
  261. }
  262. export type Difference<A, B> = A extends B ? never : A
  263. export type ShapeSpecificProps<T extends Shape> = Pick<
  264. T,
  265. Difference<keyof T, keyof BaseShape>
  266. >
  267. export type ShapeIndicatorProps<T extends Shape> = ShapeSpecificProps<T>
  268. export type ShapeUtil<K extends Shape> = {
  269. create(props: Partial<K>): K
  270. getBounds(shape: K): Bounds
  271. hitTest(shape: K, test: number[]): boolean
  272. hitTestBounds(shape: K, bounds: Bounds): boolean
  273. rotate(shape: K): K
  274. translate(shape: K, delta: number[]): K
  275. scale(shape: K, scale: number): K
  276. stretch(shape: K, scaleX: number, scaleY: number): K
  277. render(shape: K): JSX.Element
  278. }
  279. export enum MoveType {
  280. Backward,
  281. Forward,
  282. ToFront,
  283. ToBack,
  284. }
  285. export enum AlignType {
  286. Top,
  287. CenterVertical,
  288. Bottom,
  289. Left,
  290. CenterHorizontal,
  291. Right,
  292. }
  293. export enum StretchType {
  294. Horizontal,
  295. Vertical,
  296. }
  297. export enum DistributeType {
  298. Horizontal,
  299. Vertical,
  300. }
  301. /* -------------------------------------------------- */
  302. /* Code Editor */
  303. /* -------------------------------------------------- */
  304. export type IMonaco = typeof monaco
  305. export type IMonacoEditor = monaco.editor.IStandaloneCodeEditor
  306. export enum ControlType {
  307. Number = 'number',
  308. Vector = 'vector',
  309. Text = 'text',
  310. Select = 'select',
  311. }
  312. export interface BaseCodeControl {
  313. id: string
  314. type: ControlType
  315. label: string
  316. }
  317. export interface NumberCodeControl extends BaseCodeControl {
  318. type: ControlType.Number
  319. min?: number
  320. max?: number
  321. value: number
  322. step: number
  323. format?: (value: number) => number
  324. }
  325. export interface VectorCodeControl extends BaseCodeControl {
  326. type: ControlType.Vector
  327. value: number[]
  328. isNormalized: boolean
  329. format?: (value: number[]) => number[]
  330. }
  331. export interface TextCodeControl extends BaseCodeControl {
  332. type: ControlType.Text
  333. value: string
  334. format?: (value: string) => string
  335. }
  336. export interface SelectCodeControl<T extends string = ''>
  337. extends BaseCodeControl {
  338. type: ControlType.Select
  339. value: T
  340. options: T[]
  341. format?: (string: T) => string
  342. }
  343. export type CodeControl =
  344. | NumberCodeControl
  345. | VectorCodeControl
  346. | TextCodeControl
  347. | SelectCodeControl
  348. export type PropsOfType<T extends object, K> = {
  349. [K in keyof T]: T[K] extends boolean ? K : never
  350. }[keyof T]
  351. export type Mutable<T extends Shape> = { -readonly [K in keyof T]: T[K] }