Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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