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 5.8KB

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