|
@@ -2,16 +2,7 @@
|
2
|
2
|
/* refresh-reset */
|
3
|
3
|
|
4
|
4
|
import * as React from 'react'
|
5
|
|
-import {
|
6
|
|
- TLShape,
|
7
|
|
- Utils,
|
8
|
|
- TLBounds,
|
9
|
|
- TLShapeUtil,
|
10
|
|
- HTMLContainer,
|
11
|
|
- TLComponent,
|
12
|
|
- SVGContainer,
|
13
|
|
- TLIndicator,
|
14
|
|
-} from '@tldraw/core'
|
|
5
|
+import { TLShape, Utils, TLBounds, TLShapeUtil, HTMLContainer, SVGContainer } from '@tldraw/core'
|
15
|
6
|
|
16
|
7
|
// Define a custom shape
|
17
|
8
|
|
|
@@ -37,86 +28,84 @@ export const boxShape: BoxShape = {
|
37
|
28
|
export class BoxUtil extends TLShapeUtil<BoxShape, HTMLDivElement> {
|
38
|
29
|
age = 100
|
39
|
30
|
|
40
|
|
- Component: TLComponent<BoxShape, HTMLDivElement> = (
|
41
|
|
- { shape, events, onShapeChange, isEditing, meta },
|
42
|
|
- ref
|
43
|
|
- ) => {
|
44
|
|
- console.log('hi')
|
45
|
|
- const color = meta.isDarkMode ? 'white' : 'black'
|
|
31
|
+ Component = TLShapeUtil.Component<BoxShape, HTMLDivElement>(
|
|
32
|
+ ({ shape, events, onShapeChange, isEditing, meta }, ref) => {
|
|
33
|
+ const color = meta.isDarkMode ? 'white' : 'black'
|
46
|
34
|
|
47
|
|
- const rInput = React.useRef<HTMLDivElement>(null)
|
|
35
|
+ const rInput = React.useRef<HTMLDivElement>(null)
|
48
|
36
|
|
49
|
|
- function updateShapeSize() {
|
50
|
|
- const elm = rInput.current!
|
|
37
|
+ function updateShapeSize() {
|
|
38
|
+ const elm = rInput.current!
|
51
|
39
|
|
52
|
|
- onShapeChange?.({
|
53
|
|
- ...shape,
|
54
|
|
- text: elm.innerText,
|
55
|
|
- size: [elm.offsetWidth + 44, elm.offsetHeight + 44],
|
56
|
|
- })
|
57
|
|
- }
|
58
|
|
-
|
59
|
|
- React.useLayoutEffect(() => {
|
60
|
|
- const elm = rInput.current!
|
61
|
|
-
|
62
|
|
- const observer = new MutationObserver(updateShapeSize)
|
63
|
|
-
|
64
|
|
- observer.observe(elm, {
|
65
|
|
- attributes: true,
|
66
|
|
- characterData: true,
|
67
|
|
- subtree: true,
|
68
|
|
- })
|
69
|
|
-
|
70
|
|
- elm.innerText = shape.text
|
71
|
|
- updateShapeSize()
|
72
|
|
-
|
73
|
|
- return () => {
|
74
|
|
- observer.disconnect()
|
|
40
|
+ onShapeChange?.({
|
|
41
|
+ ...shape,
|
|
42
|
+ text: elm.innerText,
|
|
43
|
+ size: [elm.offsetWidth + 44, elm.offsetHeight + 44],
|
|
44
|
+ })
|
75
|
45
|
}
|
76
|
|
- }, [])
|
77
|
46
|
|
78
|
|
- React.useEffect(() => {
|
79
|
|
- if (isEditing) {
|
80
|
|
- rInput.current!.focus()
|
81
|
|
- }
|
82
|
|
- }, [isEditing])
|
83
|
|
-
|
84
|
|
- return (
|
85
|
|
- <HTMLContainer ref={ref}>
|
86
|
|
- <div
|
87
|
|
- {...events}
|
88
|
|
- style={{
|
89
|
|
- pointerEvents: 'all',
|
90
|
|
- width: shape.size[0],
|
91
|
|
- height: shape.size[1],
|
92
|
|
- display: 'flex',
|
93
|
|
- fontSize: 20,
|
94
|
|
- fontFamily: 'sans-serif',
|
95
|
|
- alignItems: 'center',
|
96
|
|
- justifyContent: 'center',
|
97
|
|
- border: `2px solid ${color}`,
|
98
|
|
- color,
|
99
|
|
- }}
|
100
|
|
- >
|
101
|
|
- <div onPointerDown={(e) => isEditing && e.stopPropagation()}>
|
102
|
|
- <div
|
103
|
|
- ref={rInput}
|
104
|
|
- style={{
|
105
|
|
- whiteSpace: 'nowrap',
|
106
|
|
- overflow: 'hidden',
|
107
|
|
- textAlign: 'center',
|
108
|
|
- outline: 'none',
|
109
|
|
- userSelect: isEditing ? 'all' : 'none',
|
110
|
|
- }}
|
111
|
|
- contentEditable={isEditing}
|
112
|
|
- />
|
|
47
|
+ React.useLayoutEffect(() => {
|
|
48
|
+ const elm = rInput.current!
|
|
49
|
+
|
|
50
|
+ const observer = new MutationObserver(updateShapeSize)
|
|
51
|
+
|
|
52
|
+ observer.observe(elm, {
|
|
53
|
+ attributes: true,
|
|
54
|
+ characterData: true,
|
|
55
|
+ subtree: true,
|
|
56
|
+ })
|
|
57
|
+
|
|
58
|
+ elm.innerText = shape.text
|
|
59
|
+ updateShapeSize()
|
|
60
|
+
|
|
61
|
+ return () => {
|
|
62
|
+ observer.disconnect()
|
|
63
|
+ }
|
|
64
|
+ }, [])
|
|
65
|
+
|
|
66
|
+ React.useEffect(() => {
|
|
67
|
+ if (isEditing) {
|
|
68
|
+ rInput.current!.focus()
|
|
69
|
+ }
|
|
70
|
+ }, [isEditing])
|
|
71
|
+
|
|
72
|
+ return (
|
|
73
|
+ <HTMLContainer ref={ref}>
|
|
74
|
+ <div
|
|
75
|
+ {...events}
|
|
76
|
+ style={{
|
|
77
|
+ pointerEvents: 'all',
|
|
78
|
+ width: shape.size[0],
|
|
79
|
+ height: shape.size[1],
|
|
80
|
+ display: 'flex',
|
|
81
|
+ fontSize: 20,
|
|
82
|
+ fontFamily: 'sans-serif',
|
|
83
|
+ alignItems: 'center',
|
|
84
|
+ justifyContent: 'center',
|
|
85
|
+ border: `2px solid ${color}`,
|
|
86
|
+ color,
|
|
87
|
+ }}
|
|
88
|
+ >
|
|
89
|
+ <div onPointerDown={(e) => isEditing && e.stopPropagation()}>
|
|
90
|
+ <div
|
|
91
|
+ ref={rInput}
|
|
92
|
+ style={{
|
|
93
|
+ whiteSpace: 'nowrap',
|
|
94
|
+ overflow: 'hidden',
|
|
95
|
+ textAlign: 'center',
|
|
96
|
+ outline: 'none',
|
|
97
|
+ userSelect: isEditing ? 'all' : 'none',
|
|
98
|
+ }}
|
|
99
|
+ contentEditable={isEditing}
|
|
100
|
+ />
|
|
101
|
+ </div>
|
113
|
102
|
</div>
|
114
|
|
- </div>
|
115
|
|
- </HTMLContainer>
|
116
|
|
- )
|
117
|
|
- }
|
|
103
|
+ </HTMLContainer>
|
|
104
|
+ )
|
|
105
|
+ }
|
|
106
|
+ )
|
118
|
107
|
|
119
|
|
- Indicator: TLIndicator<BoxShape> = ({ shape }) => {
|
|
108
|
+ Indicator = TLShapeUtil.Indicator<BoxShape>(({ shape }) => {
|
120
|
109
|
return (
|
121
|
110
|
<SVGContainer>
|
122
|
111
|
<rect
|
|
@@ -129,7 +118,7 @@ export class BoxUtil extends TLShapeUtil<BoxShape, HTMLDivElement> {
|
129
|
118
|
/>
|
130
|
119
|
</SVGContainer>
|
131
|
120
|
)
|
132
|
|
- }
|
|
121
|
+ })
|
133
|
122
|
|
134
|
123
|
getBounds = (shape: BoxShape) => {
|
135
|
124
|
const bounds = Utils.getFromCache(this.boundsCache, shape, () => {
|