Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { uniqueId } from 'utils/utils'
  2. import vec from 'utils/vec'
  3. import { TextShape, ShapeType, FontSize } from 'types'
  4. import { registerShapeUtils } from './index'
  5. import { defaultStyle, getFontStyle, getShapeStyle } from 'lib/shape-styles'
  6. import styled from 'styles'
  7. import state from 'state'
  8. import { useEffect, useRef } from 'react'
  9. // A div used for measurement
  10. if (document.getElementById('__textMeasure')) {
  11. document.getElementById('__textMeasure').remove()
  12. }
  13. const mdiv = document.createElement('pre')
  14. mdiv.id = '__textMeasure'
  15. mdiv.style.whiteSpace = 'pre'
  16. mdiv.style.width = 'auto'
  17. mdiv.style.border = '1px solid red'
  18. mdiv.style.padding = '4px'
  19. mdiv.style.margin = '0px'
  20. mdiv.style.opacity = '0'
  21. mdiv.style.position = 'absolute'
  22. mdiv.style.top = '-500px'
  23. mdiv.style.left = '0px'
  24. mdiv.style.zIndex = '9999'
  25. document.body.appendChild(mdiv)
  26. const text = registerShapeUtils<TextShape>({
  27. isForeignObject: true,
  28. canChangeAspectRatio: false,
  29. canEdit: true,
  30. boundsCache: new WeakMap([]),
  31. create(props) {
  32. return {
  33. id: uniqueId(),
  34. seed: Math.random(),
  35. type: ShapeType.Text,
  36. isGenerated: false,
  37. name: 'Text',
  38. parentId: 'page1',
  39. childIndex: 0,
  40. point: [0, 0],
  41. rotation: 0,
  42. isAspectRatioLocked: false,
  43. isLocked: false,
  44. isHidden: false,
  45. style: defaultStyle,
  46. text: '',
  47. size: 'auto',
  48. fontSize: FontSize.Medium,
  49. ...props,
  50. }
  51. },
  52. render(shape, { isEditing }) {
  53. const { id, text, style } = shape
  54. const styles = getShapeStyle(style)
  55. const font = getFontStyle(shape.fontSize, shape.style)
  56. const bounds = this.getBounds(shape)
  57. const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
  58. state.send('EDITED_SHAPE', { change: { text: e.currentTarget.value } })
  59. }
  60. return (
  61. <foreignObject
  62. id={id}
  63. x={0}
  64. y={0}
  65. width={bounds.width}
  66. height={bounds.height}
  67. pointerEvents="none"
  68. >
  69. <StyledText
  70. key={id}
  71. style={{
  72. font,
  73. color: styles.fill,
  74. }}
  75. value={text}
  76. onChange={handleChange}
  77. isEditing={isEditing}
  78. onFocus={(e) => e.currentTarget.select()}
  79. />
  80. </foreignObject>
  81. )
  82. },
  83. getBounds(shape) {
  84. const [minX, minY] = shape.point
  85. let width: number
  86. let height: number
  87. if (shape.size === 'auto') {
  88. // Calculate a size by rendering text into a div
  89. mdiv.innerHTML = shape.text + '&nbsp;'
  90. mdiv.style.font = getFontStyle(shape.fontSize, shape.style)
  91. ;[width, height] = [mdiv.offsetWidth, mdiv.offsetHeight]
  92. } else {
  93. // Use the shape's explicit size for width and height.
  94. ;[width, height] = shape.size
  95. }
  96. return {
  97. minX,
  98. maxX: minX + width,
  99. minY,
  100. maxY: minY + height,
  101. width,
  102. height,
  103. }
  104. },
  105. hitTest(shape, test) {
  106. return true
  107. },
  108. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  109. if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
  110. shape.size = [bounds.width, bounds.height]
  111. shape.point = [bounds.minX, bounds.minY]
  112. } else {
  113. if (initialShape.size === 'auto') return
  114. shape.size = vec.mul(
  115. initialShape.size,
  116. Math.min(Math.abs(scaleX), Math.abs(scaleY))
  117. )
  118. shape.point = [
  119. bounds.minX +
  120. (bounds.width - shape.size[0]) *
  121. (scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
  122. bounds.minY +
  123. (bounds.height - shape.size[1]) *
  124. (scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
  125. ]
  126. shape.rotation =
  127. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  128. ? -initialShape.rotation
  129. : initialShape.rotation
  130. }
  131. return this
  132. },
  133. transformSingle(shape, bounds) {
  134. shape.size = [bounds.width, bounds.height]
  135. shape.point = [bounds.minX, bounds.minY]
  136. return this
  137. },
  138. onBoundsReset(shape) {
  139. shape.size = 'auto'
  140. return this
  141. },
  142. getShouldDelete(shape) {
  143. return shape.text.length === 0
  144. },
  145. })
  146. export default text
  147. const StyledText = styled('textarea', {
  148. width: '100%',
  149. height: '100%',
  150. border: 'none',
  151. padding: '4px',
  152. whiteSpace: 'pre',
  153. resize: 'none',
  154. minHeight: 1,
  155. minWidth: 1,
  156. outline: 'none',
  157. backgroundColor: 'transparent',
  158. overflow: 'hidden',
  159. variants: {
  160. isEditing: {
  161. true: {
  162. backgroundColor: '$boundsBg',
  163. pointerEvents: 'all',
  164. },
  165. },
  166. },
  167. })