Remove references to core, fix core example

This commit is contained in:
Steve Ruiz 2021-10-29 15:00:55 +01:00
parent a8377139ab
commit b6f2e2940f
3 changed files with 72 additions and 85 deletions

View file

@ -2,16 +2,7 @@
/* refresh-reset */ /* refresh-reset */
import * as React from 'react' import * as React from 'react'
import { import { TLShape, Utils, TLBounds, TLShapeUtil, HTMLContainer, SVGContainer } from '@tldraw/core'
TLShape,
Utils,
TLBounds,
TLShapeUtil,
HTMLContainer,
TLComponent,
SVGContainer,
TLIndicator,
} from '@tldraw/core'
// Define a custom shape // Define a custom shape
@ -37,86 +28,84 @@ export const boxShape: BoxShape = {
export class BoxUtil extends TLShapeUtil<BoxShape, HTMLDivElement> { export class BoxUtil extends TLShapeUtil<BoxShape, HTMLDivElement> {
age = 100 age = 100
Component: TLComponent<BoxShape, HTMLDivElement> = ( Component = TLShapeUtil.Component<BoxShape, HTMLDivElement>(
{ shape, events, onShapeChange, isEditing, meta }, ({ shape, events, onShapeChange, isEditing, meta }, ref) => {
ref const color = meta.isDarkMode ? 'white' : 'black'
) => {
console.log('hi')
const color = meta.isDarkMode ? 'white' : 'black'
const rInput = React.useRef<HTMLDivElement>(null) const rInput = React.useRef<HTMLDivElement>(null)
function updateShapeSize() { function updateShapeSize() {
const elm = rInput.current! const elm = rInput.current!
onShapeChange?.({ onShapeChange?.({
...shape, ...shape,
text: elm.innerText, text: elm.innerText,
size: [elm.offsetWidth + 44, elm.offsetHeight + 44], size: [elm.offsetWidth + 44, elm.offsetHeight + 44],
}) })
}
React.useLayoutEffect(() => {
const elm = rInput.current!
const observer = new MutationObserver(updateShapeSize)
observer.observe(elm, {
attributes: true,
characterData: true,
subtree: true,
})
elm.innerText = shape.text
updateShapeSize()
return () => {
observer.disconnect()
} }
}, [])
React.useEffect(() => { React.useLayoutEffect(() => {
if (isEditing) { const elm = rInput.current!
rInput.current!.focus()
}
}, [isEditing])
return ( const observer = new MutationObserver(updateShapeSize)
<HTMLContainer ref={ref}>
<div observer.observe(elm, {
{...events} attributes: true,
style={{ characterData: true,
pointerEvents: 'all', subtree: true,
width: shape.size[0], })
height: shape.size[1],
display: 'flex', elm.innerText = shape.text
fontSize: 20, updateShapeSize()
fontFamily: 'sans-serif',
alignItems: 'center', return () => {
justifyContent: 'center', observer.disconnect()
border: `2px solid ${color}`, }
color, }, [])
}}
> React.useEffect(() => {
<div onPointerDown={(e) => isEditing && e.stopPropagation()}> if (isEditing) {
<div rInput.current!.focus()
ref={rInput} }
style={{ }, [isEditing])
whiteSpace: 'nowrap',
overflow: 'hidden', return (
textAlign: 'center', <HTMLContainer ref={ref}>
outline: 'none', <div
userSelect: isEditing ? 'all' : 'none', {...events}
}} style={{
contentEditable={isEditing} pointerEvents: 'all',
/> width: shape.size[0],
height: shape.size[1],
display: 'flex',
fontSize: 20,
fontFamily: 'sans-serif',
alignItems: 'center',
justifyContent: 'center',
border: `2px solid ${color}`,
color,
}}
>
<div onPointerDown={(e) => isEditing && e.stopPropagation()}>
<div
ref={rInput}
style={{
whiteSpace: 'nowrap',
overflow: 'hidden',
textAlign: 'center',
outline: 'none',
userSelect: isEditing ? 'all' : 'none',
}}
contentEditable={isEditing}
/>
</div>
</div> </div>
</div> </HTMLContainer>
</HTMLContainer> )
) }
} )
Indicator: TLIndicator<BoxShape> = ({ shape }) => { Indicator = TLShapeUtil.Indicator<BoxShape>(({ shape }) => {
return ( return (
<SVGContainer> <SVGContainer>
<rect <rect
@ -129,7 +118,7 @@ export class BoxUtil extends TLShapeUtil<BoxShape, HTMLDivElement> {
/> />
</SVGContainer> </SVGContainer>
) )
} })
getBounds = (shape: BoxShape) => { getBounds = (shape: BoxShape) => {
const bounds = Utils.getFromCache(this.boundsCache, shape, () => { const bounds = Utils.getFromCache(this.boundsCache, shape, () => {

View file

@ -9,7 +9,6 @@
"emitDeclarationOnly": false, "emitDeclarationOnly": false,
"paths": { "paths": {
"+*": ["./*"], "+*": ["./*"],
"@tldraw/core": ["../core"],
"@tldraw/tldraw": ["../tldraw"] "@tldraw/tldraw": ["../tldraw"]
} }
}, },

View file

@ -8,7 +8,6 @@
"baseUrl": "src", "baseUrl": "src",
"paths": { "paths": {
"~*": ["./*"], "~*": ["./*"],
"@tldraw/core": ["../core"],
"@tldraw/vec": ["../vec"], "@tldraw/vec": ["../vec"],
"@tldraw/intersect": ["../intersect"] "@tldraw/intersect": ["../intersect"]
} }