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

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