@contember/react-board-dnd-kit 1.3.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/development/BoardDndKitRenderer.cjs +199 -0
- package/dist/development/BoardDndKitRenderer.cjs.map +1 -0
- package/dist/development/BoardDndKitRenderer.js +199 -0
- package/dist/development/BoardDndKitRenderer.js.map +1 -0
- package/dist/development/index.cjs +22 -0
- package/dist/development/index.cjs.map +1 -0
- package/dist/development/index.js +16 -0
- package/dist/development/index.js.map +1 -0
- package/dist/development/internal/multipleContainersKeyboardCoordinates.cjs +92 -0
- package/dist/development/internal/multipleContainersKeyboardCoordinates.cjs.map +1 -0
- package/dist/development/internal/multipleContainersKeyboardCoordinates.js +92 -0
- package/dist/development/internal/multipleContainersKeyboardCoordinates.js.map +1 -0
- package/dist/production/BoardDndKitRenderer.cjs +199 -0
- package/dist/production/BoardDndKitRenderer.cjs.map +1 -0
- package/dist/production/BoardDndKitRenderer.js +199 -0
- package/dist/production/BoardDndKitRenderer.js.map +1 -0
- package/dist/production/index.cjs +22 -0
- package/dist/production/index.cjs.map +1 -0
- package/dist/production/index.js +16 -0
- package/dist/production/index.js.map +1 -0
- package/dist/production/internal/multipleContainersKeyboardCoordinates.cjs +92 -0
- package/dist/production/internal/multipleContainersKeyboardCoordinates.cjs.map +1 -0
- package/dist/production/internal/multipleContainersKeyboardCoordinates.js +92 -0
- package/dist/production/internal/multipleContainersKeyboardCoordinates.js.map +1 -0
- package/dist/types/BoardDndKitRenderer.d.ts +30 -0
- package/dist/types/BoardDndKitRenderer.d.ts.map +1 -0
- package/dist/types/index.d.ts +15 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/internal/multipleContainersKeyboardCoordinates.d.ts +12 -0
- package/dist/types/internal/multipleContainersKeyboardCoordinates.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/dist/types/types.d.ts +3 -0
- package/dist/types/types.d.ts.map +1 -0
- package/package.json +51 -0
- package/src/BoardDndKitRenderer.tsx +303 -0
- package/src/index.ts +23 -0
- package/src/internal/multipleContainersKeyboardCoordinates.ts +124 -0
- package/src/tsconfig.json +10 -0
- package/src/types.ts +3 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAE/C,MAAM,MAAM,gBAAgB,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@contember/react-board-dnd-kit",
|
|
3
|
+
"license": "Apache-2.0",
|
|
4
|
+
"version": "1.3.0-alpha.8",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/production/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/types/index.d.ts",
|
|
12
|
+
"development": "./dist/development/index.js",
|
|
13
|
+
"production": "./dist/production/index.js",
|
|
14
|
+
"default": "./dist/production/index.js"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist/",
|
|
20
|
+
"src/"
|
|
21
|
+
],
|
|
22
|
+
"typings": "./dist/types/index.d.ts",
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "yarn build:js:dev && yarn build:js:prod",
|
|
25
|
+
"build:js:dev": "vite build --mode development",
|
|
26
|
+
"build:js:prod": "vite build --mode production",
|
|
27
|
+
"ae:build": "api-extractor run --local",
|
|
28
|
+
"ae:test": "api-extractor run",
|
|
29
|
+
"test": "vitest"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/contember/interface.git",
|
|
34
|
+
"directory": "packages/react-board-dnd-kit"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@contember/react-binding": "1.3.0-alpha.8",
|
|
38
|
+
"@contember/react-board": "1.3.0-alpha.8",
|
|
39
|
+
"@dnd-kit/core": "^6.1.0",
|
|
40
|
+
"@dnd-kit/sortable": "^8.0.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"react": "^17 || ^18",
|
|
44
|
+
"react-dom": "^17 || ^18"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"react": "^18.2.0",
|
|
48
|
+
"react-dom": "^18.2.0"
|
|
49
|
+
},
|
|
50
|
+
"stableVersion": "0.0.0"
|
|
51
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import React, { FunctionComponent, ReactNode, useCallback, useMemo, useState } from 'react'
|
|
2
|
+
import { createPortal } from 'react-dom'
|
|
3
|
+
import {
|
|
4
|
+
DndContext,
|
|
5
|
+
DragEndEvent,
|
|
6
|
+
DragOverlay,
|
|
7
|
+
KeyboardSensor,
|
|
8
|
+
MeasuringStrategy,
|
|
9
|
+
MouseSensor,
|
|
10
|
+
TouchSensor,
|
|
11
|
+
UniqueIdentifier,
|
|
12
|
+
useSensor,
|
|
13
|
+
useSensors,
|
|
14
|
+
} from '@dnd-kit/core'
|
|
15
|
+
import { horizontalListSortingStrategy, SortableContext, useSortable, verticalListSortingStrategy } from '@dnd-kit/sortable'
|
|
16
|
+
import { coordinateGetter as multipleContainersCoordinateGetter } from './internal/multipleContainersKeyboardCoordinates'
|
|
17
|
+
|
|
18
|
+
import { AccessorTree, Entity, EntityAccessor, useAccessorTreeState } from '@contember/react-binding'
|
|
19
|
+
import { UseSortableProps } from './types'
|
|
20
|
+
import { BoardBindingProps, BoardColumn, BoardItem, BoardMethods } from '@contember/react-board'
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
type BoardIndexItem =
|
|
24
|
+
| {
|
|
25
|
+
type: 'column'
|
|
26
|
+
column: BoardColumn
|
|
27
|
+
}
|
|
28
|
+
| {
|
|
29
|
+
type: 'item'
|
|
30
|
+
column: BoardColumn
|
|
31
|
+
item: BoardItem
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type BoardIndex = Record<UniqueIdentifier, BoardIndexItem>
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
const useBoardIndex = (columns: BoardColumn[]): BoardIndex => {
|
|
38
|
+
return useMemo(() => Object.fromEntries(columns.flatMap((column): [UniqueIdentifier, BoardIndexItem][] => [
|
|
39
|
+
[column.id, { type: 'column', column }],
|
|
40
|
+
...column.items.map((item): [UniqueIdentifier, BoardIndexItem] => [item.id, { type: 'item', column, item }]),
|
|
41
|
+
])), [columns])
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type BoardDndKitRendererProps =
|
|
45
|
+
& BoardBindingProps<any>
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
export type BoardColumnRendererProps = {
|
|
49
|
+
children?: ReactNode
|
|
50
|
+
value?: BoardColumn
|
|
51
|
+
dragOverlay?: boolean
|
|
52
|
+
sortableProps?: UseSortableProps
|
|
53
|
+
boardMethods?: BoardMethods<any>
|
|
54
|
+
dropIndicator?: 'before' | 'after'
|
|
55
|
+
};
|
|
56
|
+
export type BoardItemRendererProps = {
|
|
57
|
+
value?: BoardItem,
|
|
58
|
+
dragOverlay?: boolean,
|
|
59
|
+
sortableProps?: UseSortableProps
|
|
60
|
+
boardMethods?: BoardMethods<any>
|
|
61
|
+
dropIndicator?: 'before' | 'after'
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const createBoardDndKitRenderer = <ColumnExtraProps extends {}, ItemExtraProps extends {}>({ Column, Item, Wrapper, usePortalProvider }: {
|
|
65
|
+
Wrapper: React.ComponentType<{ children: ReactNode }>
|
|
66
|
+
Column: React.ComponentType<BoardColumnRendererProps & ColumnExtraProps>
|
|
67
|
+
Item: React.ComponentType<BoardItemRendererProps & ItemExtraProps>
|
|
68
|
+
usePortalProvider?: () => Element | null
|
|
69
|
+
}): FunctionComponent<BoardDndKitRendererProps & ColumnExtraProps & ItemExtraProps> => {
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
const SortableItem = ({ dragOverlay, value, boardMethods, column, ...props }: {
|
|
73
|
+
value: BoardItem
|
|
74
|
+
column: BoardColumn
|
|
75
|
+
dragOverlay?: boolean
|
|
76
|
+
boardMethods: BoardMethods<any>
|
|
77
|
+
} & ItemExtraProps) => {
|
|
78
|
+
const sortableProps = useSortable({
|
|
79
|
+
id: value.id,
|
|
80
|
+
data: {
|
|
81
|
+
type: 'item',
|
|
82
|
+
item: value,
|
|
83
|
+
column,
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
const { isOver, active, data } = sortableProps
|
|
87
|
+
const showDropIndicator = isOver && active?.data.current?.type === 'item'
|
|
88
|
+
const activeSortable = active?.data.current?.sortable
|
|
89
|
+
const isAfter = data?.sortable.containerId === activeSortable?.containerId
|
|
90
|
+
&& (data?.sortable.index ?? 0) > activeSortable?.index
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<Entity accessor={value.value}>
|
|
95
|
+
<Item
|
|
96
|
+
value={value}
|
|
97
|
+
boardMethods={boardMethods}
|
|
98
|
+
dragOverlay={dragOverlay}
|
|
99
|
+
sortableProps={sortableProps}
|
|
100
|
+
dropIndicator={showDropIndicator ? (isAfter ? 'after' : 'before') : undefined}
|
|
101
|
+
{...props as unknown as ItemExtraProps}
|
|
102
|
+
/>
|
|
103
|
+
</Entity>
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
const SortableColumn = ({ children, value, dragOverlay, boardMethods, ...props }: {
|
|
109
|
+
children: ReactNode,
|
|
110
|
+
value: BoardColumn,
|
|
111
|
+
dragOverlay?: boolean
|
|
112
|
+
boardMethods: BoardMethods<any>
|
|
113
|
+
} & ColumnExtraProps) => {
|
|
114
|
+
const sortableProps = useSortable({
|
|
115
|
+
id: value.id,
|
|
116
|
+
data: {
|
|
117
|
+
type: 'column',
|
|
118
|
+
column: value,
|
|
119
|
+
},
|
|
120
|
+
})
|
|
121
|
+
const { active, isOver, over, data } = sortableProps
|
|
122
|
+
const activeSortable = active?.data.current?.sortable
|
|
123
|
+
|
|
124
|
+
const overColumnOrItsItem = isOver || (over?.data.current?.type === 'item' && over?.data.current?.column?.id === value?.id)
|
|
125
|
+
const showDropIndicator = value?.value !== null && overColumnOrItsItem && active?.data.current?.type === 'column'
|
|
126
|
+
const isAfter = data?.sortable.containerId === activeSortable?.containerId && (data?.sortable.index ?? 0) > activeSortable?.index
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<ColumnEntityWrapper value={value}>
|
|
130
|
+
<Column
|
|
131
|
+
value={value}
|
|
132
|
+
dragOverlay={dragOverlay}
|
|
133
|
+
sortableProps={sortableProps}
|
|
134
|
+
boardMethods={boardMethods}
|
|
135
|
+
dropIndicator={showDropIndicator ? (isAfter ? 'after' : 'before') : undefined}
|
|
136
|
+
{...props as unknown as ColumnExtraProps}
|
|
137
|
+
>
|
|
138
|
+
{children}
|
|
139
|
+
</Column>
|
|
140
|
+
</ColumnEntityWrapper>
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const ColumnEntityWrapper = ({ value, children }: { value: BoardColumn, children: ReactNode, }) => {
|
|
145
|
+
if (!(value.value instanceof EntityAccessor)) {
|
|
146
|
+
return <>{children}</>
|
|
147
|
+
}
|
|
148
|
+
return (
|
|
149
|
+
<Entity accessor={value.value}>
|
|
150
|
+
{children}
|
|
151
|
+
</Entity>
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
const ContainerDragOverlay = ({ column, boardMethods, ...props }: {
|
|
157
|
+
column: BoardColumn,
|
|
158
|
+
boardMethods: BoardMethods<any>
|
|
159
|
+
} & ColumnExtraProps & ItemExtraProps) => (
|
|
160
|
+
<ColumnEntityWrapper value={column}>
|
|
161
|
+
<Column
|
|
162
|
+
dragOverlay
|
|
163
|
+
value={column}
|
|
164
|
+
boardMethods={boardMethods}
|
|
165
|
+
{...props as unknown as ColumnExtraProps}
|
|
166
|
+
>
|
|
167
|
+
{column.items.map(item => (
|
|
168
|
+
<Entity accessor={item.value} key={item.id}>
|
|
169
|
+
<Item
|
|
170
|
+
dragOverlay
|
|
171
|
+
value={item}
|
|
172
|
+
boardMethods={boardMethods}
|
|
173
|
+
{...props as unknown as ItemExtraProps}
|
|
174
|
+
/>
|
|
175
|
+
</Entity>
|
|
176
|
+
))}
|
|
177
|
+
</Column>
|
|
178
|
+
</ColumnEntityWrapper>
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
return ({ columns, moveColumn, moveItem, removeColumn, removeItem, addItem, addColumn, ...extraProps }) => {
|
|
182
|
+
const [activeId, setActiveId] = useState<UniqueIdentifier | null>(null)
|
|
183
|
+
|
|
184
|
+
const boardMethods = useMemo(() => ({
|
|
185
|
+
moveColumn,
|
|
186
|
+
moveItem,
|
|
187
|
+
removeColumn,
|
|
188
|
+
removeItem,
|
|
189
|
+
addItem,
|
|
190
|
+
addColumn,
|
|
191
|
+
}), [moveColumn, moveItem, removeColumn, removeItem, addItem, addColumn])
|
|
192
|
+
|
|
193
|
+
const boardIndex = useBoardIndex(columns)
|
|
194
|
+
|
|
195
|
+
const activeItem = activeId ? boardIndex[activeId] : undefined
|
|
196
|
+
|
|
197
|
+
const sensors = useSensors(
|
|
198
|
+
useSensor(MouseSensor),
|
|
199
|
+
useSensor(TouchSensor),
|
|
200
|
+
useSensor(KeyboardSensor, {
|
|
201
|
+
coordinateGetter: multipleContainersCoordinateGetter,
|
|
202
|
+
}),
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
const onDragCancel = () => {
|
|
206
|
+
setActiveId(null)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
const portal = usePortalProvider ? usePortalProvider() : document.body
|
|
211
|
+
|
|
212
|
+
const onDragEnd = useCallback(({ active, over }: DragEndEvent) => {
|
|
213
|
+
setActiveId(null)
|
|
214
|
+
|
|
215
|
+
const overItem = over ? boardIndex[over.id] : null
|
|
216
|
+
const activeItem = boardIndex[active.id]
|
|
217
|
+
if (!activeItem) {
|
|
218
|
+
return
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (activeItem.type === 'column' && activeItem.column.value instanceof EntityAccessor) {
|
|
222
|
+
console.log(overItem)
|
|
223
|
+
moveColumn?.(activeItem.column.value, overItem?.column.index ?? columns.length)
|
|
224
|
+
return
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (activeItem.type === 'item' && overItem) {
|
|
228
|
+
moveItem?.(activeItem.item.value, overItem.column.value, overItem.type === 'item' ? overItem.item.index : overItem.column.items.length)
|
|
229
|
+
return
|
|
230
|
+
}
|
|
231
|
+
}, [boardIndex, columns.length, moveColumn, moveItem])
|
|
232
|
+
|
|
233
|
+
const accessorTreeState = useAccessorTreeState()
|
|
234
|
+
|
|
235
|
+
return (
|
|
236
|
+
<DndContext
|
|
237
|
+
sensors={sensors}
|
|
238
|
+
measuring={{
|
|
239
|
+
droppable: {
|
|
240
|
+
strategy: MeasuringStrategy.Always,
|
|
241
|
+
},
|
|
242
|
+
}}
|
|
243
|
+
onDragStart={({ active }) => {
|
|
244
|
+
setActiveId(active.id)
|
|
245
|
+
}}
|
|
246
|
+
onDragEnd={onDragEnd}
|
|
247
|
+
onDragCancel={onDragCancel}
|
|
248
|
+
>
|
|
249
|
+
<Wrapper>
|
|
250
|
+
<SortableContext
|
|
251
|
+
items={columns}
|
|
252
|
+
strategy={horizontalListSortingStrategy}
|
|
253
|
+
>
|
|
254
|
+
{columns.map(column => (
|
|
255
|
+
<SortableColumn
|
|
256
|
+
key={column.id}
|
|
257
|
+
value={column}
|
|
258
|
+
boardMethods={boardMethods}
|
|
259
|
+
|
|
260
|
+
{...extraProps as unknown as ColumnExtraProps}
|
|
261
|
+
>
|
|
262
|
+
<SortableContext items={column.items} strategy={verticalListSortingStrategy}>
|
|
263
|
+
{column.items.map(value => (
|
|
264
|
+
<SortableItem
|
|
265
|
+
key={value.id}
|
|
266
|
+
value={value}
|
|
267
|
+
column={column}
|
|
268
|
+
boardMethods={boardMethods}
|
|
269
|
+
{...extraProps as unknown as ItemExtraProps}
|
|
270
|
+
/>
|
|
271
|
+
))}
|
|
272
|
+
</SortableContext>
|
|
273
|
+
</SortableColumn>
|
|
274
|
+
))}
|
|
275
|
+
</SortableContext>
|
|
276
|
+
</Wrapper>
|
|
277
|
+
{portal ? createPortal(
|
|
278
|
+
<DragOverlay>
|
|
279
|
+
<AccessorTree state={accessorTreeState}>
|
|
280
|
+
{activeItem
|
|
281
|
+
? activeItem.type === 'column'
|
|
282
|
+
? <ContainerDragOverlay
|
|
283
|
+
boardMethods={boardMethods}
|
|
284
|
+
column={activeItem.column}
|
|
285
|
+
{...extraProps as unknown as ColumnExtraProps & ItemExtraProps}
|
|
286
|
+
/>
|
|
287
|
+
: <Entity accessor={activeItem.item.value}>
|
|
288
|
+
<Item
|
|
289
|
+
dragOverlay
|
|
290
|
+
value={activeItem.item}
|
|
291
|
+
boardMethods={boardMethods}
|
|
292
|
+
{...extraProps as unknown as ItemExtraProps}
|
|
293
|
+
/>
|
|
294
|
+
</Entity>
|
|
295
|
+
: null}
|
|
296
|
+
</AccessorTree>
|
|
297
|
+
</DragOverlay>
|
|
298
|
+
, portal) : null}
|
|
299
|
+
</DndContext>
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
}
|
|
303
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { BoardBaseProps, createBoard } from '@contember/react-board'
|
|
2
|
+
import { BoardColumnRendererProps, BoardItemRendererProps, createBoardDndKitRenderer } from './BoardDndKitRenderer'
|
|
3
|
+
import React, { FunctionComponent, ReactNode } from 'react'
|
|
4
|
+
|
|
5
|
+
export * from './BoardDndKitRenderer'
|
|
6
|
+
export * from './types'
|
|
7
|
+
|
|
8
|
+
export const createBoardDndKit = <ColumnExtraProps extends {}, ItemExtraProps extends {}>({ Column, Item, Wrapper, usePortalProvider }: {
|
|
9
|
+
Wrapper: React.ComponentType<{ children: ReactNode }>
|
|
10
|
+
Column: React.ComponentType<BoardColumnRendererProps & ColumnExtraProps>
|
|
11
|
+
Item: React.ComponentType<BoardItemRendererProps & ItemExtraProps>
|
|
12
|
+
usePortalProvider?: () => Element | null
|
|
13
|
+
}): FunctionComponent<BoardBaseProps<ColumnExtraProps & ItemExtraProps>> => {
|
|
14
|
+
const renderer = createBoardDndKitRenderer({ Column, Item, Wrapper, usePortalProvider })
|
|
15
|
+
|
|
16
|
+
return createBoard<ColumnExtraProps & ItemExtraProps>({
|
|
17
|
+
Renderer: renderer,
|
|
18
|
+
ItemStaticRender: Item as any,
|
|
19
|
+
ColumnStaticRender: Column as any,
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export * from '@contember/react-board'
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copied from https://github.com/clauderic/dnd-kit/blob/master/stories/2%20-%20Presets/Sortable/multipleContainersKeyboardCoordinates.ts
|
|
3
|
+
*
|
|
4
|
+
* MIT License
|
|
5
|
+
*
|
|
6
|
+
* @copyright (c) 2021, Claudéric Demers
|
|
7
|
+
*
|
|
8
|
+
* see https://github.com/clauderic/dnd-kit/blob/master/LICENSE
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
closestCorners,
|
|
13
|
+
getFirstCollision,
|
|
14
|
+
KeyboardCode,
|
|
15
|
+
DroppableContainer,
|
|
16
|
+
KeyboardCoordinateGetter,
|
|
17
|
+
} from '@dnd-kit/core'
|
|
18
|
+
|
|
19
|
+
const directions: string[] = [
|
|
20
|
+
KeyboardCode.Down,
|
|
21
|
+
KeyboardCode.Right,
|
|
22
|
+
KeyboardCode.Up,
|
|
23
|
+
KeyboardCode.Left,
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
export const coordinateGetter: KeyboardCoordinateGetter = (
|
|
27
|
+
event,
|
|
28
|
+
{ context: { active, droppableRects, droppableContainers, collisionRect } },
|
|
29
|
+
) => {
|
|
30
|
+
if (directions.includes(event.code)) {
|
|
31
|
+
event.preventDefault()
|
|
32
|
+
|
|
33
|
+
if (!active || !collisionRect) {
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const filteredContainers: DroppableContainer[] = []
|
|
38
|
+
|
|
39
|
+
droppableContainers.getEnabled().forEach(entry => {
|
|
40
|
+
if (!entry || entry?.disabled) {
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const rect = droppableRects.get(entry.id)
|
|
45
|
+
|
|
46
|
+
if (!rect) {
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const data = entry.data.current
|
|
51
|
+
|
|
52
|
+
if (data) {
|
|
53
|
+
const { type, children } = data
|
|
54
|
+
|
|
55
|
+
if (type === 'container' && children?.length > 0) {
|
|
56
|
+
if (active.data.current?.type !== 'container') {
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
switch (event.code) {
|
|
63
|
+
case KeyboardCode.Down:
|
|
64
|
+
if (collisionRect.top < rect.top) {
|
|
65
|
+
filteredContainers.push(entry)
|
|
66
|
+
}
|
|
67
|
+
break
|
|
68
|
+
case KeyboardCode.Up:
|
|
69
|
+
if (collisionRect.top > rect.top) {
|
|
70
|
+
filteredContainers.push(entry)
|
|
71
|
+
}
|
|
72
|
+
break
|
|
73
|
+
case KeyboardCode.Left:
|
|
74
|
+
if (collisionRect.left >= rect.left + rect.width) {
|
|
75
|
+
filteredContainers.push(entry)
|
|
76
|
+
}
|
|
77
|
+
break
|
|
78
|
+
case KeyboardCode.Right:
|
|
79
|
+
if (collisionRect.left + collisionRect.width <= rect.left) {
|
|
80
|
+
filteredContainers.push(entry)
|
|
81
|
+
}
|
|
82
|
+
break
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
const collisions = closestCorners({
|
|
87
|
+
active,
|
|
88
|
+
collisionRect: collisionRect,
|
|
89
|
+
droppableRects,
|
|
90
|
+
droppableContainers: filteredContainers,
|
|
91
|
+
pointerCoordinates: null,
|
|
92
|
+
})
|
|
93
|
+
const closestId = getFirstCollision(collisions, 'id')
|
|
94
|
+
|
|
95
|
+
if (closestId != null) {
|
|
96
|
+
const newDroppable = droppableContainers.get(closestId)
|
|
97
|
+
const newNode = newDroppable?.node.current
|
|
98
|
+
const newRect = newDroppable?.rect.current
|
|
99
|
+
|
|
100
|
+
if (newNode && newRect) {
|
|
101
|
+
if (newDroppable.id === 'placeholder') {
|
|
102
|
+
return {
|
|
103
|
+
x: newRect.left + (newRect.width - collisionRect.width) / 2,
|
|
104
|
+
y: newRect.top + (newRect.height - collisionRect.height) / 2,
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (newDroppable.data.current?.type === 'container') {
|
|
109
|
+
return {
|
|
110
|
+
x: newRect.left + 20,
|
|
111
|
+
y: newRect.top + 74,
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
x: newRect.left,
|
|
117
|
+
y: newRect.top,
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return undefined
|
|
124
|
+
}
|
package/src/types.ts
ADDED