-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathgrid-draw-to-insert-strategy.tsx
309 lines (275 loc) · 10.5 KB
/
grid-draw-to-insert-strategy.tsx
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import type { ElementPath } from 'utopia-shared/src/types'
import { MetadataUtils } from '../../../../core/model/element-metadata-utils'
import { stripNulls } from '../../../../core/shared/array-utils'
import * as EP from '../../../../core/shared/element-path'
import type { CanvasPoint, CanvasRectangle, Size } from '../../../../core/shared/math-utils'
import {
canvasPoint,
canvasRectangle,
pointDifference,
roundRectangleToNearestWhole,
size,
} from '../../../../core/shared/math-utils'
import * as PP from '../../../../core/shared/property-path'
import { assertNever } from '../../../../core/shared/utils'
import { EditorModes, type InsertionSubject } from '../../../editor/editor-modes'
import { childInsertionPath } from '../../../editor/store/insertion-path'
import { deleteProperties } from '../../commands/delete-properties-command'
import type { InsertElementInsertionSubject } from '../../commands/insert-element-insertion-subject'
import { insertElementInsertionSubject } from '../../commands/insert-element-insertion-subject'
import { showGridControls } from '../../commands/show-grid-controls-command'
import { updateHighlightedViews } from '../../commands/update-highlighted-views-command'
import { wildcardPatch } from '../../commands/wildcard-patch-command'
import { controlsForGridPlaceholders } from '../../controls/grid-controls-for-strategies'
import { canvasPointToWindowPoint } from '../../dom-lookup'
import {
getWrapperWithGeneratedUid,
getWrappingCommands,
type CanvasStrategyFactory,
} from '../canvas-strategies'
import {
emptyStrategyApplicationResult,
getInsertionSubjectsFromInteractionTarget,
strategyApplicationResult,
type CustomStrategyState,
type InteractionCanvasState,
} from '../canvas-strategy-types'
import type {
DragInteractionData,
HoverInteractionData,
InteractionSession,
} from '../interaction-state'
import {
getStyleAttributesForFrameInAbsolutePosition,
updateInsertionSubjectWithAttributes,
} from './draw-to-insert-metastrategy'
import { setGridPropsCommands } from './grid-helpers'
import { newReparentSubjects } from './reparent-helpers/reparent-strategy-helpers'
import { getReparentTargetUnified } from './reparent-helpers/reparent-strategy-parent-lookup'
import { getGridCellUnderMouseFromMetadata } from './grid-cell-bounds'
import { nukeAllAbsolutePositioningPropsCommands } from '../../../inspector/inspector-common'
import { gridContainerIdentifier } from '../../../editor/store/editor-state'
export const gridDrawToInsertText: CanvasStrategyFactory = (
canvasState: InteractionCanvasState,
interactionSession: InteractionSession | null,
customStrategyState: CustomStrategyState,
) => {
const insertionSubject = getInsertionSubjectsFromInteractionTarget(
canvasState.interactionTarget,
).at(0)
if (insertionSubject == null) {
return null
}
if (insertionSubject.textEdit) {
return gridDrawToInsertStrategyInner({
name: 'Draw to insert (Text)',
id: 'draw-text-into-grid',
insertionSubject: insertionSubject,
})(canvasState, interactionSession, customStrategyState)
}
return null
}
export const gridDrawToInsertStrategy: CanvasStrategyFactory = (
canvasState: InteractionCanvasState,
interactionSession: InteractionSession | null,
customStrategyState: CustomStrategyState,
) => {
const insertionSubject = getInsertionSubjectsFromInteractionTarget(
canvasState.interactionTarget,
).at(0)
if (insertionSubject == null) {
return null
}
if (insertionSubject.textEdit) {
return null
}
return gridDrawToInsertStrategyInner({
name: 'Draw to Insert (Grid)',
id: 'draw-into-grid',
insertionSubject: insertionSubject,
})(canvasState, interactionSession, customStrategyState)
}
const gridDrawToInsertStrategyInner =
({
name,
id,
insertionSubject,
}: {
name: string
id: string
insertionSubject: InsertionSubject
}): CanvasStrategyFactory =>
(
canvasState: InteractionCanvasState,
interactionSession: InteractionSession | null,
customStrategyState: CustomStrategyState,
) => {
if (interactionSession == null || interactionSession.interactionData.type === 'KEYBOARD') {
return null
}
const { interactionData } = interactionSession
const pointOnCanvas =
interactionData.type === 'DRAG' ? interactionData.originalDragStart : interactionData.point
const targetParent = getReparentTargetUnified(
newReparentSubjects(insertionSubject.defaultSize),
pointOnCanvas,
true, // cmd is necessary to allow reparenting,
canvasState,
canvasState.startingMetadata,
canvasState.startingElementPathTree,
canvasState.startingAllElementProps,
'allow-smaller-parent',
['supportsChildren'],
canvasState.propertyControlsInfo,
)?.newParent.intendedParentPath
const parent = MetadataUtils.findElementByElementPath(
canvasState.startingMetadata,
targetParent,
)
if (targetParent == null || parent == null || !MetadataUtils.isGridLayoutedContainer(parent)) {
return null
}
return {
id: id,
name: name,
descriptiveLabel: name,
icon: {
category: 'tools',
type: 'pointer',
},
controlsToRender: [controlsForGridPlaceholders(gridContainerIdentifier(targetParent))],
fitness: 5,
apply: (strategyLifecycle) => {
const canvasPointToUse =
interactionData.type === 'DRAG' ? interactionData.dragStart : interactionData.point
const newTargetCell = getGridCellUnderMouseFromMetadata(parent, canvasPointToUse)
if (strategyLifecycle === 'mid-interaction' && interactionData.type === 'HOVER') {
return strategyApplicationResult(
[
wildcardPatch('mid-interaction', {
selectedViews: { $set: [] },
}),
showGridControls(
'mid-interaction',
gridContainerIdentifier(targetParent),
newTargetCell?.gridCellCoordinates ?? null,
null,
),
updateHighlightedViews('mid-interaction', [targetParent]),
],
[],
)
}
if (newTargetCell == null) {
return emptyStrategyApplicationResult
}
const { gridCellCoordinates, cellCanvasRectangle } = newTargetCell
const defaultSize =
interactionData.type === 'DRAG' &&
interactionData.drag == null &&
strategyLifecycle === 'end-interaction'
? insertionSubject.defaultSize
: size(0, 0)
const insertionCommand = getInsertionCommand(
targetParent,
insertionSubject,
getFrameForInsertion(interactionData, defaultSize, cellCanvasRectangle),
)
const gridTemplate = parent.specialSizeMeasurements.containerGridProperties
const insertedElementPath = EP.appendToPath(targetParent, insertionSubject.uid)
const maybeWrapperWithUid = getWrapperWithGeneratedUid(customStrategyState, canvasState, [
insertionSubject,
])
const wrappingCommands =
maybeWrapperWithUid == null
? []
: getWrappingCommands(insertedElementPath, maybeWrapperWithUid)
return strategyApplicationResult(
[
insertionCommand,
...nukeAllAbsolutePositioningPropsCommands(insertedElementPath), // do not use absolute positioning in grid cells
...setGridPropsCommands(insertedElementPath, gridTemplate, {
gridRowStart: { numericalPosition: gridCellCoordinates.row },
gridColumnStart: { numericalPosition: gridCellCoordinates.column },
gridRowEnd: { numericalPosition: gridCellCoordinates.row + 1 },
gridColumnEnd: { numericalPosition: gridCellCoordinates.column + 1 },
// TODO! this is currently going to assign the element to the cell the interaction started in,
// however it would be good to instead assign the element to _all_ cells overlapping with the final
// inserted frame.
}),
...wrappingCommands,
...stripNulls([
insertionSubject.textEdit
? wildcardPatch('on-complete', {
mode: {
$set: EditorModes.textEditMode(
insertedElementPath,
canvasPointToWindowPoint(
pointOnCanvas,
canvasState.scale,
canvasState.canvasOffset,
),
'existing',
'no-text-selection',
),
},
})
: null,
]),
],
[targetParent],
{
strategyGeneratedUidsCache: {
[insertionSubject.uid]: maybeWrapperWithUid?.uid,
},
},
)
},
}
}
function getFrameForInsertion(
interactionData: DragInteractionData | HoverInteractionData,
defaultSize: Size,
cellOrigin: CanvasPoint,
): CanvasRectangle {
if (interactionData.type === 'DRAG') {
const dragStart = interactionData.dragStart
const mouseAt = {
x: interactionData.dragStart.x + (interactionData.drag?.x ?? 0),
y: interactionData.dragStart.y + (interactionData.drag?.y ?? 0),
}
const width = Math.abs(interactionData.drag?.x ?? defaultSize.width)
const height = Math.abs(interactionData.drag?.y ?? defaultSize.height)
const origin = canvasPoint({
x: Math.min(dragStart.x, mouseAt.x),
y: Math.min(dragStart.y, mouseAt.y),
})
const { x, y } = pointDifference(cellOrigin, origin)
return roundRectangleToNearestWhole(canvasRectangle({ x, y, width, height }))
}
if (interactionData.type === 'HOVER') {
const pointOnCanvas = interactionData.point
return roundRectangleToNearestWhole(
canvasRectangle({
x: pointOnCanvas.x,
y: pointOnCanvas.y,
width: defaultSize.width,
height: defaultSize.height,
}),
)
}
assertNever(interactionData)
}
function getInsertionCommand(
parentPath: ElementPath,
subject: InsertionSubject,
frame: CanvasRectangle,
): InsertElementInsertionSubject {
const updatedAttributesWithPosition = getStyleAttributesForFrameInAbsolutePosition(subject, frame)
const updatedInsertionSubject = updateInsertionSubjectWithAttributes(
subject,
updatedAttributesWithPosition,
)
const insertionPath = childInsertionPath(parentPath)
return insertElementInsertionSubject('always', updatedInsertionSubject, insertionPath)
}