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'
  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. type: ShapeType.Text,
  47. isGenerated: false,
  48. name: 'Text',
  49. parentId: 'page1',
  50. childIndex: 0,
  51. point: [0, 0],
  52. rotation: 0,
  53. isAspectRatioLocked: false,
  54. isLocked: false,
  55. isHidden: false,
  56. style: defaultStyle,
  57. text: '',
  58. scale: 1,
  59. size: 'auto',
  60. fontSize: FontSize.Medium,
  61. ...props,
  62. }
  63. },
  64. render(shape, { isEditing, ref }) {
  65. const { id, text, style } = shape
  66. const styles = getShapeStyle(style)
  67. const font = getFontStyle(shape.scale, shape.style)
  68. const bounds = this.getBounds(shape)
  69. function handleChange(e: React.ChangeEvent<HTMLTextAreaElement>) {
  70. state.send('EDITED_SHAPE', {
  71. change: { text: normalizeText(e.currentTarget.value) },
  72. })
  73. }
  74. function handleKeyDown(e: React.KeyboardEvent) {
  75. e.stopPropagation()
  76. if (e.key === 'Tab') {
  77. e.preventDefault()
  78. }
  79. }
  80. function handleBlur() {
  81. state.send('BLURRED_EDITING_SHAPE')
  82. }
  83. function handleFocus(e: React.FocusEvent<HTMLTextAreaElement>) {
  84. e.currentTarget.select()
  85. state.send('FOCUSED_EDITING_SHAPE')
  86. }
  87. const fontSize = getFontSize(shape.style.size) * shape.scale
  88. const gap = fontSize * 0.4
  89. if (!isEditing) {
  90. return (
  91. <g id={id} pointerEvents="none">
  92. {text.split('\n').map((str, i) => (
  93. <text
  94. key={i}
  95. x={4}
  96. y={4 + gap / 2 + i * (fontSize + gap)}
  97. fontFamily="Verveine Regular"
  98. fontStyle="normal"
  99. fontWeight="regular"
  100. fontSize={fontSize}
  101. width={bounds.width}
  102. height={bounds.height}
  103. dominantBaseline="hanging"
  104. >
  105. {str}
  106. </text>
  107. ))}
  108. </g>
  109. )
  110. }
  111. return (
  112. <foreignObject
  113. id={id}
  114. x={0}
  115. y={0}
  116. width={bounds.width}
  117. height={bounds.height}
  118. pointerEvents="none"
  119. >
  120. <StyledTextArea
  121. ref={ref}
  122. style={{
  123. font,
  124. color: styles.stroke,
  125. }}
  126. value={text}
  127. tabIndex={0}
  128. autoComplete="false"
  129. autoCapitalize="false"
  130. autoCorrect="false"
  131. autoSave="false"
  132. placeholder=""
  133. name="text"
  134. autoFocus={isMobile() ? true : false}
  135. onFocus={handleFocus}
  136. onBlur={handleBlur}
  137. onKeyDown={handleKeyDown}
  138. onChange={handleChange}
  139. />
  140. </foreignObject>
  141. )
  142. },
  143. getBounds(shape) {
  144. if (!this.boundsCache.has(shape)) {
  145. mdiv.innerHTML = shape.text + '&zwj;'
  146. mdiv.style.font = getFontStyle(shape.scale, shape.style)
  147. const [minX, minY] = shape.point
  148. const [width, height] = [mdiv.offsetWidth, mdiv.offsetHeight]
  149. this.boundsCache.set(shape, {
  150. minX,
  151. maxX: minX + width,
  152. minY,
  153. maxY: minY + height,
  154. width,
  155. height,
  156. })
  157. }
  158. return this.boundsCache.get(shape)
  159. },
  160. hitTest() {
  161. return true
  162. },
  163. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  164. if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
  165. shape.point = [bounds.minX, bounds.minY]
  166. shape.scale = initialShape.scale * Math.abs(scaleX)
  167. } else {
  168. shape.point = [bounds.minX, bounds.minY]
  169. shape.rotation =
  170. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  171. ? -initialShape.rotation
  172. : initialShape.rotation
  173. shape.scale = initialShape.scale * Math.abs(Math.min(scaleX, scaleY))
  174. }
  175. return this
  176. },
  177. transformSingle(shape, bounds, { initialShape, scaleX }) {
  178. shape.point = [bounds.minX, bounds.minY]
  179. shape.scale = initialShape.scale * Math.abs(scaleX)
  180. return this
  181. },
  182. onBoundsReset(shape) {
  183. const center = this.getCenter(shape)
  184. this.boundsCache.delete(shape)
  185. shape.scale = 1
  186. const newCenter = this.getCenter(shape)
  187. shape.point = vec.add(shape.point, vec.sub(center, newCenter))
  188. return this
  189. },
  190. applyStyles(shape, style) {
  191. const center = this.getCenter(shape)
  192. this.boundsCache.delete(shape)
  193. Object.assign(shape.style, style)
  194. const newCenter = this.getCenter(shape)
  195. shape.point = vec.add(shape.point, vec.sub(center, newCenter))
  196. return this
  197. },
  198. shouldDelete(shape) {
  199. return shape.text.length === 0
  200. },
  201. })
  202. export default text
  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. })