tldraw/lib/shape-utils/text.tsx

259 lines
6 KiB
TypeScript
Raw Normal View History

import { uniqueId } from 'utils/utils'
import { TextShape, ShapeType, FontSize } from 'types'
import { registerShapeUtils } from './index'
2021-06-17 21:50:04 +00:00
import {
defaultStyle,
getFontSize,
getFontStyle,
getShapeStyle,
} from 'lib/shape-styles'
import styled from 'styles'
import state from 'state'
2021-06-17 21:50:04 +00:00
import React from 'react'
if (document.getElementById('__textMeasure')) {
document.getElementById('__textMeasure').remove()
}
2021-06-17 21:50:04 +00:00
// A div used for measurement
const mdiv = document.createElement('pre')
mdiv.id = '__textMeasure'
mdiv.style.whiteSpace = 'pre'
mdiv.style.width = 'auto'
mdiv.style.border = '1px solid red'
mdiv.style.padding = '4px'
mdiv.style.margin = '0px'
mdiv.style.opacity = '0'
mdiv.style.position = 'absolute'
mdiv.style.top = '-500px'
mdiv.style.left = '0px'
mdiv.style.zIndex = '9999'
2021-06-17 21:50:04 +00:00
mdiv.tabIndex = -1
2021-06-17 10:43:55 +00:00
mdiv.setAttribute('readonly', 'true')
document.body.appendChild(mdiv)
2021-06-17 21:50:04 +00:00
function normalizeText(text: string) {
return text.replace(/\t/g, ' ').replace(/\r?\n|\r/g, '\n')
}
const text = registerShapeUtils<TextShape>({
isForeignObject: true,
canChangeAspectRatio: false,
canEdit: true,
boundsCache: new WeakMap([]),
create(props) {
return {
id: uniqueId(),
seed: Math.random(),
type: ShapeType.Text,
isGenerated: false,
name: 'Text',
parentId: 'page1',
childIndex: 0,
point: [0, 0],
rotation: 0,
isAspectRatioLocked: false,
isLocked: false,
isHidden: false,
style: defaultStyle,
text: '',
2021-06-17 10:43:55 +00:00
scale: 1,
size: 'auto',
fontSize: FontSize.Medium,
...props,
}
},
2021-06-17 10:43:55 +00:00
render(shape, { isEditing, ref }) {
const { id, text, style } = shape
const styles = getShapeStyle(style)
2021-06-17 10:43:55 +00:00
const font = getFontStyle(shape.fontSize, shape.scale, shape.style)
const bounds = this.getBounds(shape)
2021-06-17 21:50:04 +00:00
function handleChange(e: React.ChangeEvent<HTMLTextAreaElement>) {
state.send('EDITED_SHAPE', {
change: { text: normalizeText(e.currentTarget.value) },
})
}
function handleKeyDown(e: React.KeyboardEvent) {
e.stopPropagation()
if (e.key === 'Tab') {
e.preventDefault()
}
}
function handleBlur() {
state.send('BLURRED_EDITING_SHAPE')
}
function handleFocus(e: React.FocusEvent<HTMLTextAreaElement>) {
e.currentTarget.select()
state.send('FOCUSED_EDITING_SHAPE')
}
const lineHeight = getFontSize(shape.fontSize) * shape.scale
const gap = lineHeight * 0.4
if (!isEditing) {
return (
<g id={id} pointerEvents="none">
{text.split('\n').map((str, i) => (
<text
key={i}
x={4}
y={4 + gap / 2 + i * (lineHeight + gap)}
style={{ font }}
width={bounds.width}
height={bounds.height}
dominant-baseline="hanging"
>
{str}
</text>
))}
</g>
)
}
return (
<foreignObject
id={id}
x={0}
y={0}
width={bounds.width}
height={bounds.height}
pointerEvents="none"
>
2021-06-17 10:43:55 +00:00
{isEditing ? (
<StyledTextArea
ref={ref}
style={{
font,
color: styles.stroke,
}}
2021-06-17 21:50:04 +00:00
tabIndex={0}
2021-06-17 10:43:55 +00:00
value={text}
autoComplete="false"
autoCapitalize="false"
autoCorrect="false"
2021-06-17 21:50:04 +00:00
spellCheck="false"
onFocus={handleFocus}
onBlur={handleBlur}
onKeyDown={handleKeyDown}
onChange={handleChange}
dir="auto"
2021-06-17 10:43:55 +00:00
/>
) : (
<StyledText
style={{
font,
color: styles.stroke,
}}
>
{text}
</StyledText>
)}
</foreignObject>
)
},
getBounds(shape) {
2021-06-17 10:43:55 +00:00
if (!this.boundsCache.has(shape)) {
2021-06-17 21:50:04 +00:00
mdiv.innerHTML = shape.text + '&zwj;'
2021-06-17 10:43:55 +00:00
mdiv.style.font = getFontStyle(shape.fontSize, shape.scale, shape.style)
const [minX, minY] = shape.point
const [width, height] = [mdiv.offsetWidth, mdiv.offsetHeight]
this.boundsCache.set(shape, {
minX,
maxX: minX + width,
minY,
maxY: minY + height,
width,
height,
})
}
2021-06-17 10:43:55 +00:00
return this.boundsCache.get(shape)
},
hitTest(shape, test) {
return true
},
transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
shape.point = [bounds.minX, bounds.minY]
2021-06-17 10:43:55 +00:00
shape.scale = initialShape.scale * Math.abs(scaleX)
} else {
2021-06-17 10:43:55 +00:00
shape.point = [bounds.minX, bounds.minY]
shape.rotation =
(scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
? -initialShape.rotation
: initialShape.rotation
2021-06-17 10:43:55 +00:00
shape.scale = initialShape.scale * Math.abs(Math.min(scaleX, scaleY))
}
return this
},
2021-06-17 10:43:55 +00:00
transformSingle(shape, bounds, { initialShape, scaleX }) {
shape.point = [bounds.minX, bounds.minY]
2021-06-17 10:43:55 +00:00
shape.scale = initialShape.scale * Math.abs(scaleX)
return this
},
onBoundsReset(shape) {
shape.size = 'auto'
return this
},
2021-06-17 10:43:55 +00:00
shouldDelete(shape) {
return shape.text.length === 0
},
})
export default text
2021-06-17 10:43:55 +00:00
const StyledText = styled('div', {
width: '100%',
height: '100%',
border: 'none',
padding: '4px',
whiteSpace: 'pre',
minHeight: 1,
minWidth: 1,
2021-06-17 21:50:04 +00:00
outline: 0,
backgroundColor: 'transparent',
overflow: 'hidden',
2021-06-17 10:43:55 +00:00
pointerEvents: 'none',
userSelect: 'none',
2021-06-17 21:50:04 +00:00
backfaceVisibility: 'hidden',
display: 'inline-block',
position: 'relative',
zIndex: 0,
2021-06-17 10:43:55 +00:00
})
2021-06-17 10:43:55 +00:00
const StyledTextArea = styled('textarea', {
width: '100%',
height: '100%',
border: 'none',
padding: '4px',
whiteSpace: 'pre',
resize: 'none',
minHeight: 1,
minWidth: 1,
2021-06-17 21:50:04 +00:00
outline: 0,
2021-06-17 10:43:55 +00:00
backgroundColor: '$boundsBg',
2021-06-17 21:50:04 +00:00
overflow: 'hidden',
2021-06-17 10:43:55 +00:00
pointerEvents: 'all',
2021-06-17 21:50:04 +00:00
backfaceVisibility: 'hidden',
display: 'inline-block',
})