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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import { uniqueId, isMobile } from 'utils/utils'
  2. import vec from 'utils/vec'
  3. import { TextShape, ShapeType, FontSize, SizeStyle } 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. // A div used for measurement
  14. const mdiv = document.createElement('pre')
  15. mdiv.id = '__textMeasure'
  16. Object.assign(mdiv.style, {
  17. whiteSpace: 'pre',
  18. width: 'auto',
  19. border: '1px solid red',
  20. padding: '4px',
  21. margin: '0px',
  22. opacity: '0',
  23. position: 'absolute',
  24. top: '-500px',
  25. left: '0px',
  26. zIndex: '9999',
  27. pointerEvents: 'none',
  28. })
  29. mdiv.tabIndex = -1
  30. document.body.appendChild(mdiv)
  31. function normalizeText(text: string) {
  32. return text.replace(/\t/g, ' ').replace(/\r?\n|\r/g, '\n')
  33. }
  34. const text = registerShapeUtils<TextShape>({
  35. isForeignObject: true,
  36. canChangeAspectRatio: false,
  37. canEdit: true,
  38. boundsCache: new WeakMap([]),
  39. create(props) {
  40. return {
  41. id: uniqueId(),
  42. seed: Math.random(),
  43. type: ShapeType.Text,
  44. isGenerated: false,
  45. name: 'Text',
  46. parentId: 'page1',
  47. childIndex: 0,
  48. point: [0, 0],
  49. rotation: 0,
  50. isAspectRatioLocked: false,
  51. isLocked: false,
  52. isHidden: false,
  53. style: defaultStyle,
  54. text: '',
  55. scale: 1,
  56. size: 'auto',
  57. fontSize: FontSize.Medium,
  58. ...props,
  59. }
  60. },
  61. render(shape, { isEditing, ref }) {
  62. const { id, text, style } = shape
  63. const styles = getShapeStyle(style)
  64. const font = getFontStyle(shape.scale, shape.style)
  65. const bounds = this.getBounds(shape)
  66. function handleChange(e: React.ChangeEvent<HTMLTextAreaElement>) {
  67. state.send('EDITED_SHAPE', {
  68. change: { text: normalizeText(e.currentTarget.value) },
  69. })
  70. }
  71. function handleKeyDown(e: React.KeyboardEvent) {
  72. e.stopPropagation()
  73. if (e.key === 'Tab') {
  74. e.preventDefault()
  75. }
  76. }
  77. function handleBlur() {
  78. state.send('BLURRED_EDITING_SHAPE')
  79. }
  80. function handleFocus(e: React.FocusEvent<HTMLTextAreaElement>) {
  81. e.currentTarget.select()
  82. state.send('FOCUSED_EDITING_SHAPE')
  83. }
  84. return (
  85. <foreignObject
  86. id={id}
  87. x={0}
  88. y={0}
  89. width={bounds.width}
  90. height={bounds.height}
  91. pointerEvents="none"
  92. >
  93. {isEditing ? (
  94. <StyledTextArea
  95. ref={ref}
  96. style={{
  97. font,
  98. color: styles.stroke,
  99. }}
  100. value={text}
  101. tabIndex={0}
  102. autoComplete="false"
  103. autoCapitalize="false"
  104. autoCorrect="false"
  105. autoSave="false"
  106. placeholder=""
  107. name="text"
  108. autoFocus={isMobile() ? true : false}
  109. onFocus={handleFocus}
  110. onBlur={handleBlur}
  111. onKeyDown={handleKeyDown}
  112. onChange={handleChange}
  113. />
  114. ) : (
  115. <StyledText
  116. style={{
  117. font,
  118. color: styles.stroke,
  119. }}
  120. >
  121. {text}
  122. </StyledText>
  123. )}
  124. </foreignObject>
  125. )
  126. },
  127. getBounds(shape) {
  128. if (!this.boundsCache.has(shape)) {
  129. mdiv.innerHTML = shape.text + '&zwj;'
  130. mdiv.style.font = getFontStyle(shape.scale, shape.style)
  131. const [minX, minY] = shape.point
  132. const [width, height] = [mdiv.offsetWidth, mdiv.offsetHeight]
  133. this.boundsCache.set(shape, {
  134. minX,
  135. maxX: minX + width,
  136. minY,
  137. maxY: minY + height,
  138. width,
  139. height,
  140. })
  141. }
  142. return this.boundsCache.get(shape)
  143. },
  144. hitTest(shape, test) {
  145. return true
  146. },
  147. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  148. if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
  149. shape.point = [bounds.minX, bounds.minY]
  150. shape.scale = initialShape.scale * Math.abs(scaleX)
  151. } else {
  152. shape.point = [bounds.minX, bounds.minY]
  153. shape.rotation =
  154. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  155. ? -initialShape.rotation
  156. : initialShape.rotation
  157. shape.scale = initialShape.scale * Math.abs(Math.min(scaleX, scaleY))
  158. }
  159. return this
  160. },
  161. transformSingle(shape, bounds, { initialShape, scaleX }) {
  162. shape.point = [bounds.minX, bounds.minY]
  163. shape.scale = initialShape.scale * Math.abs(scaleX)
  164. return this
  165. },
  166. onBoundsReset(shape) {
  167. const center = this.getCenter(shape)
  168. this.boundsCache.delete(shape)
  169. shape.scale = 1
  170. const newCenter = this.getCenter(shape)
  171. shape.point = vec.add(shape.point, vec.sub(center, newCenter))
  172. return this
  173. },
  174. applyStyles(shape, style) {
  175. const center = this.getCenter(shape)
  176. this.boundsCache.delete(shape)
  177. Object.assign(shape.style, style)
  178. const newCenter = this.getCenter(shape)
  179. shape.point = vec.add(shape.point, vec.sub(center, newCenter))
  180. return this
  181. },
  182. shouldDelete(shape) {
  183. return shape.text.length === 0
  184. },
  185. })
  186. export default text
  187. const StyledText = styled('div', {
  188. width: '100%',
  189. height: '100%',
  190. border: 'none',
  191. padding: '4px',
  192. whiteSpace: 'pre',
  193. minHeight: 1,
  194. minWidth: 1,
  195. outline: 0,
  196. backgroundColor: 'transparent',
  197. overflow: 'hidden',
  198. pointerEvents: 'none',
  199. userSelect: 'none',
  200. WebkitUserSelect: 'none',
  201. display: 'inline-block',
  202. })
  203. const StyledTextArea = styled('textarea', {
  204. zIndex: 1,
  205. width: '100%',
  206. height: '100%',
  207. border: 'none',
  208. padding: '4px',
  209. whiteSpace: 'pre',
  210. resize: 'none',
  211. minHeight: 1,
  212. minWidth: 1,
  213. outline: 0,
  214. backgroundColor: '$boundsBg',
  215. overflow: 'hidden',
  216. pointerEvents: 'all',
  217. backfaceVisibility: 'hidden',
  218. display: 'inline-block',
  219. userSelect: 'text',
  220. WebkitUserSelect: 'text',
  221. WebkitTouchCallout: 'none',
  222. })