Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

text.tsx 5.7KB

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