-
Notifications
You must be signed in to change notification settings - Fork 2k
/
menu-hooks.ts
217 lines (201 loc) · 5.08 KB
/
menu-hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import {
Editor,
TLArrowShape,
TLDrawShape,
TLGroupShape,
TLLineShape,
TLTextShape,
useEditor,
useValue,
} from '@tldraw/editor'
import { getArrowBindings } from '../../shapes/arrow/shared'
function shapesWithUnboundArrows(editor: Editor) {
const selectedShapeIds = editor.getSelectedShapeIds()
const selectedShapes = selectedShapeIds.map((id) => {
return editor.getShape(id)
})
return selectedShapes.filter((shape) => {
if (!shape) return false
if (editor.isShapeOfType<TLArrowShape>(shape, 'arrow')) {
const bindings = getArrowBindings(editor, shape)
if (bindings.start || bindings.end) return false
}
return true
})
}
/** @internal */
export const useThreeStackableItems = () => {
const editor = useEditor()
return useValue('threeStackableItems', () => shapesWithUnboundArrows(editor).length > 2, [editor])
}
/** @internal */
export const useIsInSelectState = () => {
const editor = useEditor()
return useValue('isInSelectState', () => editor.isIn('select'), [editor])
}
/** @internal */
export const useAllowGroup = () => {
const editor = useEditor()
return useValue(
'allow group',
() => {
// We can't group arrows that are bound to shapes that aren't selected
// if more than one shape has an arrow bound to it, allow group
const selectedShapes = editor.getSelectedShapes()
if (selectedShapes.length < 2) return false
for (const shape of selectedShapes) {
if (editor.isShapeOfType<TLArrowShape>(shape, 'arrow')) {
const bindings = getArrowBindings(editor, shape)
if (bindings.start) {
// if the other shape is not among the selected shapes...
if (!selectedShapes.some((s) => s.id === bindings.start!.toId)) {
return false
}
}
if (bindings.end) {
// if the other shape is not among the selected shapes...
if (!selectedShapes.some((s) => s.id === bindings.end!.toId)) {
return false
}
}
}
}
return true
},
[editor]
)
}
/** @internal */
export const useAllowUngroup = () => {
const editor = useEditor()
return useValue(
'allowUngroup',
() => editor.getSelectedShapeIds().some((id) => editor.getShape(id)?.type === 'group'),
[editor]
)
}
export const showMenuPaste =
typeof window !== 'undefined' &&
'navigator' in window &&
Boolean(navigator.clipboard) &&
Boolean(navigator.clipboard.read)
/**
* Returns true if the number of LOCKED OR UNLOCKED selected shapes is at least min or at most max.
*/
export function useAnySelectedShapesCount(min?: number, max?: number) {
const editor = useEditor()
return useValue(
'selectedShapes',
() => {
const len = editor.getSelectedShapes().length
if (min === undefined) {
if (max === undefined) {
// just length
return len
} else {
// max but no min
return len <= max
}
} else {
if (max === undefined) {
// min but no max
return len >= min
} else {
// max and min
return len >= min && len <= max
}
}
},
[editor, min, max]
)
}
/**
* Returns true if the number of UNLOCKED selected shapes is at least min or at most max.
*/
export function useUnlockedSelectedShapesCount(min?: number, max?: number) {
const editor = useEditor()
return useValue(
'selectedShapes',
() => {
const len = editor
.getSelectedShapes()
.filter((s) => !editor.isShapeOrAncestorLocked(s)).length
if (min === undefined) {
if (max === undefined) {
// just length
return len
} else {
// max but no min
return len <= max
}
} else {
if (max === undefined) {
// min but no max
return len >= min
} else {
// max and min
return len >= min && len <= max
}
}
},
[editor]
)
}
export function useShowAutoSizeToggle() {
const editor = useEditor()
return useValue(
'showAutoSizeToggle',
() => {
const selectedShapes = editor.getSelectedShapes()
return (
selectedShapes.length === 1 &&
editor.isShapeOfType<TLTextShape>(selectedShapes[0], 'text') &&
selectedShapes[0].props.autoSize === false
)
},
[editor]
)
}
export function useHasLinkShapeSelected() {
const editor = useEditor()
return useValue(
'hasLinkShapeSelected',
() => {
const onlySelectedShape = editor.getOnlySelectedShape()
return !!(
onlySelectedShape &&
onlySelectedShape.type !== 'embed' &&
'url' in onlySelectedShape.props &&
!onlySelectedShape.isLocked
)
},
[editor]
)
}
export function useOnlyFlippableShape() {
const editor = useEditor()
return useValue(
'onlyFlippableShape',
() => {
const shape = editor.getOnlySelectedShape()
return (
shape &&
(editor.isShapeOfType<TLGroupShape>(shape, 'group') ||
editor.isShapeOfType<TLArrowShape>(shape, 'arrow') ||
editor.isShapeOfType<TLLineShape>(shape, 'line') ||
editor.isShapeOfType<TLDrawShape>(shape, 'draw'))
)
},
[editor]
)
}
/** @public */
export function useCanRedo() {
const editor = useEditor()
return useValue('useCanRedo', () => editor.getCanRedo(), [editor])
}
/** @public */
export function useCanUndo() {
const editor = useEditor()
return useValue('useCanUndo', () => editor.getCanUndo(), [editor])
}