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

register.tsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import React from 'react'
  2. import {
  3. vec,
  4. pointInBounds,
  5. getBoundsCenter,
  6. getBoundsFromPoints,
  7. getRotatedCorners,
  8. boundsCollidePolygon,
  9. boundsContainPolygon,
  10. } from 'utils'
  11. import { Shape, ShapeUtility } from 'types'
  12. function getDefaultShapeUtil<T extends Shape>(): ShapeUtility<T> {
  13. return {
  14. boundsCache: new WeakMap(),
  15. canTransform: true,
  16. canChangeAspectRatio: true,
  17. canStyleFill: true,
  18. canEdit: false,
  19. isShy: false,
  20. isParent: false,
  21. isForeignObject: false,
  22. defaultProps: {} as T,
  23. create(props) {
  24. return {
  25. ...this.defaultProps,
  26. ...props,
  27. style: {
  28. ...this.defaultProps.style,
  29. ...props.style,
  30. isFilled: this.canStyleFill
  31. ? props.style?.isFilled || this.defaultProps.style.isFilled
  32. : false,
  33. },
  34. } as T
  35. },
  36. render(shape) {
  37. return <circle id={shape.id} />
  38. },
  39. translateBy(shape, delta) {
  40. shape.point = vec.round(vec.add(shape.point, delta))
  41. return this
  42. },
  43. translateTo(shape, point) {
  44. shape.point = vec.round(point)
  45. return this
  46. },
  47. rotateTo(shape, rotation) {
  48. shape.rotation = rotation
  49. return this
  50. },
  51. rotateBy(shape, rotation) {
  52. shape.rotation += rotation
  53. return this
  54. },
  55. transform(shape, bounds) {
  56. shape.point = [bounds.minX, bounds.minY]
  57. return this
  58. },
  59. transformSingle(shape, bounds, info) {
  60. return this.transform(shape, bounds, info)
  61. },
  62. onChildrenChange() {
  63. return this
  64. },
  65. onBindingChange() {
  66. return this
  67. },
  68. onHandleChange() {
  69. return this
  70. },
  71. onDoublePointHandle() {
  72. return this
  73. },
  74. onDoubleFocus() {
  75. return this
  76. },
  77. onBoundsReset() {
  78. return this
  79. },
  80. onSessionComplete() {
  81. return this
  82. },
  83. getBounds(shape) {
  84. const [x, y] = shape.point
  85. return {
  86. minX: x,
  87. minY: y,
  88. maxX: x + 1,
  89. maxY: y + 1,
  90. width: 1,
  91. height: 1,
  92. }
  93. },
  94. getRotatedBounds(shape) {
  95. return getBoundsFromPoints(
  96. getRotatedCorners(this.getBounds(shape), shape.rotation)
  97. )
  98. },
  99. getCenter(shape) {
  100. return getBoundsCenter(this.getBounds(shape))
  101. },
  102. hitTest(shape, point) {
  103. return pointInBounds(point, this.getBounds(shape))
  104. },
  105. hitTestBounds(shape, brushBounds) {
  106. const rotatedCorners = getRotatedCorners(
  107. this.getBounds(shape),
  108. shape.rotation
  109. )
  110. return (
  111. boundsContainPolygon(brushBounds, rotatedCorners) ||
  112. boundsCollidePolygon(brushBounds, rotatedCorners)
  113. )
  114. },
  115. setProperty(shape, prop, value) {
  116. shape[prop] = value
  117. return this
  118. },
  119. applyStyles(shape, style) {
  120. Object.assign(shape.style, style)
  121. return this
  122. },
  123. shouldDelete() {
  124. return false
  125. },
  126. invalidate(shape) {
  127. this.boundsCache.delete(shape)
  128. return this
  129. },
  130. shouldRender() {
  131. return true
  132. },
  133. }
  134. }
  135. /**
  136. * A factory of shape utilities, with typing enforced.
  137. * @param shape
  138. * @returns
  139. */
  140. export function registerShapeUtils<K extends Shape>(
  141. shapeUtil: Partial<ShapeUtility<K>>
  142. ): ShapeUtility<K> {
  143. return Object.freeze({ ...getDefaultShapeUtil<K>(), ...shapeUtil })
  144. }