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

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