-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
304 lines (287 loc) · 7.71 KB
/
index.ts
File metadata and controls
304 lines (287 loc) · 7.71 KB
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
export interface Dimension {
width: number
height: number
}
export interface Offset {
top: number
left: number
}
type Axis<T extends string> = {
placements: Record<
T,
(
parentStart: number,
parentLength: number,
childLength: number,
gap: number
) => number
>
avoidOverlap: boolean
}
const primaryAxis: Axis<'start' | 'end'> = {
placements: {
start: (parentStart, parentLength, childLength, gap) =>
parentStart - gap - childLength,
end: (parentStart, parentLength, childLength, gap) =>
parentStart + parentLength + gap,
},
avoidOverlap: true,
}
const secondaryAxis: Axis<'start' | 'end' | 'center'> = {
placements: {
start: (parentStart, parentLength, childLength, gap) => parentStart,
end: (parentStart, parentLength, childLength, gap) =>
parentStart - childLength + parentLength,
center: (parentStart, parentLength, childLength, gap) =>
parentStart - childLength / 2 + parentLength / 2,
},
avoidOverlap: false,
}
const Direction: Record<
'horizontal' | 'vertical',
{
start: (rect: Offset) => number
length: (rect: Dimension) => number
}
> = {
horizontal: {
start: (offset) => offset.left,
length: (dimension) => dimension.width,
},
vertical: {
start: (offset) => offset.top,
length: (dimension) => dimension.height,
},
}
/**
* Adjust the raw (suggested) position to keep the entire popup onscreen.
*/
function adjustPosition(
suggestedPosition: number,
childLength: number,
viewportLength: number
): number {
return Math.max(0, Math.min(viewportLength - childLength, suggestedPosition))
}
/**
* Find the length of overlapping section between two ranges.
*/
function overlappingLength(
parentStart: number,
parentLength: number,
childStart: number,
childLength: number
): number {
return Math.max(
0,
Math.min(childStart + childLength, parentStart + parentLength) -
Math.max(childStart, parentStart)
)
}
function createAxis<T extends string>(
axisType: Axis<T>,
preferredPlacement: T
) {
return (
parentStart: number,
parentLength: number,
childLength: number,
gap: number,
viewportLength: number
): number => {
const results: Array<{
position: number
adjustment: number
deviation: number
overlap: number
}> = []
const preferredPosition = axisType.placements[preferredPlacement](
parentStart,
parentLength,
childLength,
gap
)
// Try all the possible placements
for (const placement in axisType.placements) {
const suggestedPosition = axisType.placements[placement](
parentStart,
parentLength,
childLength,
gap
)
const adjustedPosition = adjustPosition(
suggestedPosition,
childLength,
viewportLength
)
const adjustment = Math.abs(suggestedPosition - adjustedPosition)
const deviation = Math.abs(preferredPosition - adjustedPosition)
const overlap = overlappingLength(
parentStart,
parentLength,
adjustedPosition,
childLength
)
results.push({
position: adjustedPosition,
adjustment: adjustment,
deviation: deviation,
overlap: overlap,
})
}
return results.sort(
(a, b) =>
// Prefer a placement that doesn’t require any adjustment...
(a.adjustment > 0 ? 1 : 0) - (b.adjustment > 0 ? 1 : 0) ||
// ...that has the least/most overlapping area...
(a.overlap - b.overlap) * (axisType.avoidOverlap ? 1 : -1) ||
// ...that is closest to the preferred position...
a.deviation - b.deviation ||
// ...with minimal amount of adjustment needed to stay fully onscreen
a.adjustment - b.adjustment
)[0].position
}
}
const fallbackPrimaryAxis = createAxis(primaryAxis, 'end')
const fallbackSecondaryAxis = createAxis(secondaryAxis, 'start')
function createStrategy(
xAxis: ReturnType<typeof createAxis>,
yAxis: ReturnType<typeof createAxis>
) {
return (
parent: Offset & Dimension,
child: Dimension,
viewport: Dimension,
options: { gap: number }
) => {
function calculate(
direction: (typeof Direction)[keyof typeof Direction],
calculatePosition: ReturnType<typeof createAxis>
) {
return calculatePosition(
direction.start(parent),
direction.length(parent),
direction.length(child),
options.gap,
direction.length(viewport)
)
}
const suggestedPosition = {
left: calculate(Direction.horizontal, xAxis),
top: calculate(Direction.vertical, yAxis),
}
const choices = [
suggestedPosition,
{
left: calculate(Direction.horizontal, fallbackSecondaryAxis),
top: calculate(Direction.vertical, fallbackPrimaryAxis),
},
{
left: calculate(Direction.horizontal, fallbackPrimaryAxis),
top: calculate(Direction.vertical, fallbackSecondaryAxis),
},
].map((position) => {
const deviation =
(position.left - suggestedPosition.left) ** 2 +
(position.top - suggestedPosition.top) ** 2
const overlappedArea =
overlappingLength(
parent.left,
parent.width,
position.left,
child.width
) *
overlappingLength(parent.top, parent.height, position.top, child.height)
return {
position,
deviation,
overlappedArea,
}
})
return choices.sort(
(a, b) => a.overlappedArea - b.overlappedArea || a.deviation - b.deviation
)[0].position
}
}
const strategies = {
top: createStrategy(
createAxis(secondaryAxis, 'center'),
createAxis(primaryAxis, 'start')
),
bottom: createStrategy(
createAxis(secondaryAxis, 'center'),
createAxis(primaryAxis, 'end')
),
left: createStrategy(
createAxis(primaryAxis, 'start'),
createAxis(secondaryAxis, 'center')
),
right: createStrategy(
createAxis(primaryAxis, 'end'),
createAxis(secondaryAxis, 'center')
),
'top left': createStrategy(
createAxis(secondaryAxis, 'start'),
createAxis(primaryAxis, 'start')
),
'top center': createStrategy(
createAxis(secondaryAxis, 'center'),
createAxis(primaryAxis, 'start')
),
'top right': createStrategy(
createAxis(secondaryAxis, 'end'),
createAxis(primaryAxis, 'start')
),
'bottom left': createStrategy(
createAxis(secondaryAxis, 'start'),
createAxis(primaryAxis, 'end')
),
'bottom center': createStrategy(
createAxis(secondaryAxis, 'center'),
createAxis(primaryAxis, 'end')
),
'bottom right': createStrategy(
createAxis(secondaryAxis, 'end'),
createAxis(primaryAxis, 'end')
),
'left top': createStrategy(
createAxis(primaryAxis, 'start'),
createAxis(secondaryAxis, 'start')
),
'left center': createStrategy(
createAxis(primaryAxis, 'start'),
createAxis(secondaryAxis, 'center')
),
'left bottom': createStrategy(
createAxis(primaryAxis, 'start'),
createAxis(secondaryAxis, 'end')
),
'right top': createStrategy(
createAxis(primaryAxis, 'end'),
createAxis(secondaryAxis, 'start')
),
'right center': createStrategy(
createAxis(primaryAxis, 'end'),
createAxis(secondaryAxis, 'center')
),
'right bottom': createStrategy(
createAxis(primaryAxis, 'end'),
createAxis(secondaryAxis, 'end')
),
}
export type Strategy = keyof typeof strategies
export function calculateChildPosition(
strategyName: Strategy,
parentRect: Offset & Dimension,
childDimension: Dimension,
viewportDimension: Dimension,
options: { gap: number } = { gap: 0 }
) {
return strategies[strategyName](
parentRect,
childDimension,
viewportDimension,
options
)
}
export default calculateChildPosition