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.

text.tsx 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.lineHeight = '1'
  20. mdiv.style.margin = '0px'
  21. mdiv.style.opacity = '0'
  22. mdiv.style.position = 'absolute'
  23. mdiv.style.top = '-500px'
  24. mdiv.style.left = '0px'
  25. mdiv.style.zIndex = '9999'
  26. mdiv.setAttribute('readonly', 'true')
  27. document.body.appendChild(mdiv)
  28. const text = registerShapeUtils<TextShape>({
  29. isForeignObject: true,
  30. canChangeAspectRatio: false,
  31. canEdit: true,
  32. boundsCache: new WeakMap([]),
  33. create(props) {
  34. return {
  35. id: uniqueId(),
  36. seed: Math.random(),
  37. type: ShapeType.Text,
  38. isGenerated: false,
  39. name: 'Text',
  40. parentId: 'page1',
  41. childIndex: 0,
  42. point: [0, 0],
  43. rotation: 0,
  44. isAspectRatioLocked: false,
  45. isLocked: false,
  46. isHidden: false,
  47. style: defaultStyle,
  48. text: '',
  49. scale: 1,
  50. size: 'auto',
  51. fontSize: FontSize.Medium,
  52. ...props,
  53. }
  54. },
  55. render(shape, { isEditing, ref }) {
  56. const { id, text, style } = shape
  57. const styles = getShapeStyle(style)
  58. const font = getFontStyle(shape.fontSize, shape.scale, shape.style)
  59. const bounds = this.getBounds(shape)
  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. {isEditing ? (
  70. <StyledTextArea
  71. ref={ref}
  72. style={{
  73. font,
  74. color: styles.stroke,
  75. }}
  76. value={text}
  77. autoComplete="false"
  78. autoCapitalize="false"
  79. autoCorrect="false"
  80. onFocus={(e) => {
  81. e.currentTarget.select()
  82. state.send('FOCUSED_EDITING_SHAPE')
  83. }}
  84. onBlur={() => {
  85. state.send('BLURRED_EDITING_SHAPE')
  86. }}
  87. onChange={(e) => {
  88. state.send('EDITED_SHAPE', {
  89. change: { text: e.currentTarget.value },
  90. })
  91. }}
  92. />
  93. ) : (
  94. <StyledText
  95. style={{
  96. font,
  97. color: styles.stroke,
  98. }}
  99. >
  100. {text}
  101. </StyledText>
  102. )}
  103. </foreignObject>
  104. )
  105. },
  106. getBounds(shape) {
  107. if (!this.boundsCache.has(shape)) {
  108. mdiv.innerHTML = shape.text || ' ' // + '&nbsp;'
  109. mdiv.style.font = getFontStyle(shape.fontSize, shape.scale, shape.style)
  110. const [minX, minY] = shape.point
  111. const [width, height] = [mdiv.offsetWidth, mdiv.offsetHeight]
  112. this.boundsCache.set(shape, {
  113. minX,
  114. maxX: minX + width,
  115. minY,
  116. maxY: minY + height,
  117. width,
  118. height,
  119. })
  120. }
  121. return this.boundsCache.get(shape)
  122. },
  123. hitTest(shape, test) {
  124. return true
  125. },
  126. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  127. if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
  128. shape.point = [bounds.minX, bounds.minY]
  129. shape.scale = initialShape.scale * Math.abs(scaleX)
  130. } else {
  131. shape.point = [bounds.minX, bounds.minY]
  132. shape.rotation =
  133. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  134. ? -initialShape.rotation
  135. : initialShape.rotation
  136. shape.scale = initialShape.scale * Math.abs(Math.min(scaleX, scaleY))
  137. }
  138. return this
  139. },
  140. transformSingle(shape, bounds, { initialShape, scaleX }) {
  141. shape.point = [bounds.minX, bounds.minY]
  142. shape.scale = initialShape.scale * Math.abs(scaleX)
  143. return this
  144. },
  145. onBoundsReset(shape) {
  146. shape.size = 'auto'
  147. return this
  148. },
  149. shouldDelete(shape) {
  150. return shape.text.length === 0
  151. },
  152. })
  153. export default text
  154. const StyledText = styled('div', {
  155. width: '100%',
  156. height: '100%',
  157. border: 'none',
  158. padding: '4px',
  159. whiteSpace: 'pre',
  160. resize: 'none',
  161. minHeight: 1,
  162. minWidth: 1,
  163. outline: 'none',
  164. backgroundColor: 'transparent',
  165. overflow: 'hidden',
  166. pointerEvents: 'none',
  167. userSelect: 'none',
  168. })
  169. const StyledTextArea = styled('textarea', {
  170. width: '100%',
  171. height: '100%',
  172. border: 'none',
  173. padding: '4px',
  174. whiteSpace: 'pre',
  175. resize: 'none',
  176. minHeight: 1,
  177. minWidth: 1,
  178. outline: 'none',
  179. overflow: 'hidden',
  180. backgroundColor: '$boundsBg',
  181. pointerEvents: 'all',
  182. })