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.

index.ts 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import {
  2. ColorStyle,
  3. DashStyle,
  4. Mutable,
  5. Shape,
  6. ShapeUtility,
  7. SizeStyle,
  8. } from 'types'
  9. import { createShape, getShapeUtils } from 'state/shape-utils'
  10. import { setToArray } from 'utils'
  11. export const codeShapes = new Set<CodeShape<Shape>>([])
  12. function getOrderedShapes() {
  13. return setToArray(codeShapes).sort(
  14. (a, b) => a.shape.childIndex - b.shape.childIndex
  15. )
  16. }
  17. /**
  18. * A base class for code shapes. Note that creating a shape adds it to the
  19. * shape map, while deleting it removes it from the collected shapes set
  20. */
  21. /* ----------------- Start Copy Here ---------------- */
  22. export default class CodeShape<T extends Shape> {
  23. private _shape: Mutable<T>
  24. private utils: ShapeUtility<T>
  25. constructor(props: T) {
  26. this._shape = createShape(props.type, props) as Mutable<T>
  27. this.utils = getShapeUtils<T>(this._shape)
  28. codeShapes.add(this)
  29. }
  30. export(): Mutable<T> {
  31. return { ...this._shape }
  32. }
  33. /**
  34. * Destroy the shape.
  35. */
  36. destroy(): void {
  37. codeShapes.delete(this)
  38. }
  39. /**
  40. * Move the shape to a point.
  41. * @param delta
  42. */
  43. moveTo(point: number[]): CodeShape<T> {
  44. return this.translateTo(point)
  45. }
  46. /**
  47. * Move the shape to a point.
  48. * @param delta
  49. */
  50. translateTo(point: number[]): CodeShape<T> {
  51. this.utils.translateTo(this._shape, point)
  52. return this
  53. }
  54. /**
  55. * Move the shape by a delta.
  56. * @param delta
  57. */
  58. translateBy(delta: number[]): CodeShape<T> {
  59. this.utils.translateTo(this._shape, delta)
  60. return this
  61. }
  62. /**
  63. * Rotate the shape.
  64. */
  65. rotateTo(rotation: number): CodeShape<T> {
  66. this.utils.rotateTo(this._shape, rotation, this.shape.rotation - rotation)
  67. return this
  68. }
  69. /**
  70. * Rotate the shape by a delta.
  71. */
  72. rotateBy(rotation: number): CodeShape<T> {
  73. this.utils.rotateBy(this._shape, rotation)
  74. return this
  75. }
  76. /**
  77. * Get the shape's bounding box.
  78. */
  79. getBounds(): CodeShape<T> {
  80. this.utils.getBounds(this.shape)
  81. return this
  82. }
  83. /**
  84. * Test whether a point is inside of the shape.
  85. */
  86. hitTest(point: number[]): CodeShape<T> {
  87. this.utils.hitTest(this.shape, point)
  88. return this
  89. }
  90. /**
  91. * Move the shape to the back of the painting order.
  92. */
  93. moveToBack(): CodeShape<T> {
  94. const sorted = getOrderedShapes()
  95. if (sorted.length <= 1) return
  96. const first = sorted[0].childIndex
  97. sorted.forEach((shape) => shape.childIndex++)
  98. this.childIndex = first
  99. codeShapes.clear()
  100. sorted.forEach((shape) => codeShapes.add(shape))
  101. return this
  102. }
  103. /**
  104. * Move the shape to the top of the painting order.
  105. */
  106. moveToFront(): CodeShape<T> {
  107. const sorted = getOrderedShapes()
  108. if (sorted.length <= 1) return
  109. const ahead = sorted.slice(sorted.indexOf(this))
  110. const last = ahead[ahead.length - 1].childIndex
  111. ahead.forEach((shape) => shape.childIndex--)
  112. this.childIndex = last
  113. codeShapes.clear()
  114. sorted.forEach((shape) => codeShapes.add(shape))
  115. return this
  116. }
  117. /**
  118. * Move the shape backward in the painting order.
  119. */
  120. moveBackward(): CodeShape<T> {
  121. const sorted = getOrderedShapes()
  122. if (sorted.length <= 1) return
  123. const next = sorted[sorted.indexOf(this) - 1]
  124. if (!next) return
  125. const index = next.childIndex
  126. next.childIndex = this.childIndex
  127. this.childIndex = index
  128. codeShapes.clear()
  129. sorted.forEach((shape) => codeShapes.add(shape))
  130. return this
  131. }
  132. /**
  133. * Move the shape forward in the painting order.
  134. */
  135. moveForward(): CodeShape<T> {
  136. const sorted = getOrderedShapes()
  137. if (sorted.length <= 1) return
  138. const next = sorted[sorted.indexOf(this) + 1]
  139. if (!next) return
  140. const index = next.childIndex
  141. next.childIndex = this.childIndex
  142. this.childIndex = index
  143. codeShapes.clear()
  144. sorted.forEach((shape) => codeShapes.add(shape))
  145. return this
  146. }
  147. get id(): string {
  148. return this._shape.id
  149. }
  150. /**
  151. * The shape's underlying shape.
  152. */
  153. get shape(): T {
  154. return this._shape
  155. }
  156. /**
  157. * The shape's current point.
  158. */
  159. get point(): number[] {
  160. return [...this.shape.point]
  161. }
  162. set point(point: number[]) {
  163. getShapeUtils(this.shape).translateTo(this._shape, point)
  164. }
  165. /**
  166. * The shape's rotation.
  167. */
  168. get rotation(): number {
  169. return this.shape.rotation
  170. }
  171. set rotation(rotation: number) {
  172. getShapeUtils(this.shape).rotateTo(
  173. this._shape,
  174. rotation,
  175. rotation - this.shape.rotation
  176. )
  177. }
  178. /**
  179. * The shape's color style.
  180. */
  181. get color(): ColorStyle {
  182. return this.shape.style.color
  183. }
  184. set color(color: ColorStyle) {
  185. getShapeUtils(this.shape).applyStyles(this._shape, { color })
  186. }
  187. /**
  188. * The shape's dash style.
  189. */
  190. get dash(): DashStyle {
  191. return this.shape.style.dash
  192. }
  193. set dash(dash: DashStyle) {
  194. getShapeUtils(this.shape).applyStyles(this._shape, { dash })
  195. }
  196. /**
  197. * The shape's stroke width.
  198. */
  199. get strokeWidth(): SizeStyle {
  200. return this.shape.style.size
  201. }
  202. set strokeWidth(size: SizeStyle) {
  203. getShapeUtils(this.shape).applyStyles(this._shape, { size })
  204. }
  205. /**
  206. * The shape's index in the painting order.
  207. */
  208. get childIndex(): number {
  209. return this.shape.childIndex
  210. }
  211. set childIndex(childIndex: number) {
  212. getShapeUtils(this.shape).setProperty(this._shape, 'childIndex', childIndex)
  213. }
  214. }