Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. /**
  148. * The shape's underlying shape.
  149. */
  150. get shape(): T {
  151. return this._shape
  152. }
  153. /**
  154. * The shape's current point.
  155. */
  156. get point(): number[] {
  157. return [...this.shape.point]
  158. }
  159. set point(point: number[]) {
  160. getShapeUtils(this.shape).translateTo(this._shape, point)
  161. }
  162. /**
  163. * The shape's rotation.
  164. */
  165. get rotation(): number {
  166. return this.shape.rotation
  167. }
  168. set rotation(rotation: number) {
  169. getShapeUtils(this.shape).rotateTo(
  170. this._shape,
  171. rotation,
  172. rotation - this.shape.rotation
  173. )
  174. }
  175. /**
  176. * The shape's color style.
  177. */
  178. get color(): ColorStyle {
  179. return this.shape.style.color
  180. }
  181. set color(color: ColorStyle) {
  182. getShapeUtils(this.shape).applyStyles(this._shape, { color })
  183. }
  184. /**
  185. * The shape's dash style.
  186. */
  187. get dash(): DashStyle {
  188. return this.shape.style.dash
  189. }
  190. set dash(dash: DashStyle) {
  191. getShapeUtils(this.shape).applyStyles(this._shape, { dash })
  192. }
  193. /**
  194. * The shape's stroke width.
  195. */
  196. get strokeWidth(): SizeStyle {
  197. return this.shape.style.size
  198. }
  199. set strokeWidth(size: SizeStyle) {
  200. getShapeUtils(this.shape).applyStyles(this._shape, { size })
  201. }
  202. /**
  203. * The shape's index in the painting order.
  204. */
  205. get childIndex(): number {
  206. return this.shape.childIndex
  207. }
  208. set childIndex(childIndex: number) {
  209. getShapeUtils(this.shape).setProperty(this._shape, 'childIndex', childIndex)
  210. }
  211. }