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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import { uniqueId, isMobile } from 'utils'
  2. import vec from 'utils/vec'
  3. import { TextShape, ShapeType } 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. ...props,
  60. }
  61. },
  62. shouldRender(shape, prev) {
  63. return (
  64. shape.text !== prev.text ||
  65. shape.scale !== prev.scale ||
  66. shape.style !== prev.style
  67. )
  68. },
  69. render(shape, { isEditing, ref }) {
  70. const { id, text, style } = shape
  71. const styles = getShapeStyle(style)
  72. const font = getFontStyle(shape.scale, shape.style)
  73. const bounds = this.getBounds(shape)
  74. function handleChange(e: React.ChangeEvent<HTMLTextAreaElement>) {
  75. state.send('EDITED_SHAPE', {
  76. change: { text: normalizeText(e.currentTarget.value) },
  77. })
  78. }
  79. function handleKeyDown(e: React.KeyboardEvent) {
  80. e.stopPropagation()
  81. if (e.key === 'Tab') {
  82. e.preventDefault()
  83. }
  84. }
  85. function handleBlur() {
  86. state.send('BLURRED_EDITING_SHAPE')
  87. }
  88. function handleFocus(e: React.FocusEvent<HTMLTextAreaElement>) {
  89. e.currentTarget.select()
  90. state.send('FOCUSED_EDITING_SHAPE')
  91. }
  92. const fontSize = getFontSize(shape.style.size) * shape.scale
  93. const gap = fontSize * 0.4
  94. if (!isEditing) {
  95. return (
  96. <g id={id} pointerEvents="none">
  97. {text.split('\n').map((str, i) => (
  98. <text
  99. key={i}
  100. x={4}
  101. y={4 + gap / 2 + i * (fontSize + gap)}
  102. fontFamily="Verveine Regular"
  103. fontStyle="normal"
  104. fontWeight="regular"
  105. fontSize={fontSize}
  106. width={bounds.width}
  107. height={bounds.height}
  108. fill={styles.stroke}
  109. dominantBaseline="hanging"
  110. >
  111. {str}
  112. </text>
  113. ))}
  114. </g>
  115. )
  116. }
  117. return (
  118. <foreignObject
  119. id={id}
  120. x={0}
  121. y={0}
  122. width={bounds.width}
  123. height={bounds.height}
  124. pointerEvents="none"
  125. >
  126. <StyledTextArea
  127. ref={ref}
  128. style={{
  129. font,
  130. color: styles.stroke,
  131. }}
  132. value={text}
  133. tabIndex={0}
  134. autoComplete="false"
  135. autoCapitalize="false"
  136. autoCorrect="false"
  137. autoSave="false"
  138. placeholder=""
  139. name="text"
  140. autoFocus={isMobile() ? true : false}
  141. onFocus={handleFocus}
  142. onBlur={handleBlur}
  143. onKeyDown={handleKeyDown}
  144. onChange={handleChange}
  145. />
  146. </foreignObject>
  147. )
  148. },
  149. getBounds(shape) {
  150. if (!this.boundsCache.has(shape)) {
  151. mdiv.innerHTML = shape.text + '&zwj;'
  152. mdiv.style.font = getFontStyle(shape.scale, shape.style)
  153. const [minX, minY] = shape.point
  154. const [width, height] = [mdiv.offsetWidth, mdiv.offsetHeight]
  155. this.boundsCache.set(shape, {
  156. minX,
  157. maxX: minX + width,
  158. minY,
  159. maxY: minY + height,
  160. width,
  161. height,
  162. })
  163. }
  164. return this.boundsCache.get(shape)
  165. },
  166. hitTest() {
  167. return true
  168. },
  169. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  170. if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
  171. shape.point = [bounds.minX, bounds.minY]
  172. shape.scale = initialShape.scale * Math.abs(scaleX)
  173. } else {
  174. shape.point = [bounds.minX, bounds.minY]
  175. shape.rotation =
  176. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  177. ? -initialShape.rotation
  178. : initialShape.rotation
  179. shape.scale = initialShape.scale * Math.abs(Math.min(scaleX, scaleY))
  180. }
  181. return this
  182. },
  183. transformSingle(shape, bounds, { initialShape, scaleX }) {
  184. shape.point = [bounds.minX, bounds.minY]
  185. shape.scale = initialShape.scale * Math.abs(scaleX)
  186. return this
  187. },
  188. onBoundsReset(shape) {
  189. const center = this.getCenter(shape)
  190. this.boundsCache.delete(shape)
  191. shape.scale = 1
  192. const newCenter = this.getCenter(shape)
  193. shape.point = vec.add(shape.point, vec.sub(center, newCenter))
  194. return this
  195. },
  196. applyStyles(shape, style) {
  197. const center = this.getCenter(shape)
  198. this.boundsCache.delete(shape)
  199. Object.assign(shape.style, style)
  200. const newCenter = this.getCenter(shape)
  201. shape.point = vec.add(shape.point, vec.sub(center, newCenter))
  202. return this
  203. },
  204. shouldDelete(shape) {
  205. return shape.text.length === 0
  206. },
  207. })
  208. export default text
  209. const StyledTextArea = styled('textarea', {
  210. zIndex: 1,
  211. width: '100%',
  212. height: '100%',
  213. border: 'none',
  214. padding: '4px',
  215. whiteSpace: 'pre',
  216. resize: 'none',
  217. minHeight: 1,
  218. minWidth: 1,
  219. outline: 0,
  220. backgroundColor: '$boundsBg',
  221. overflow: 'hidden',
  222. pointerEvents: 'all',
  223. backfaceVisibility: 'hidden',
  224. display: 'inline-block',
  225. userSelect: 'text',
  226. WebkitUserSelect: 'text',
  227. WebkitTouchCallout: 'none',
  228. })