@webbio/strapi-plugin-page-builder 0.12.2-platform → 0.12.3-platform
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/README.md +166 -166
- package/admin/src/api/collection-type.ts +111 -111
- package/admin/src/api/entity-relation.ts +42 -42
- package/admin/src/api/has-page-relation.ts +37 -37
- package/admin/src/api/has-platform-relation.ts +40 -40
- package/admin/src/api/page-type-relation.ts +41 -41
- package/admin/src/api/platform-page-types.ts +45 -45
- package/admin/src/api/platform-relation.ts +42 -42
- package/admin/src/api/platform.ts +35 -35
- package/admin/src/api/search-filtered-entity.ts +114 -114
- package/admin/src/api/template.ts +39 -39
- package/admin/src/components/Combobox/index.tsx +83 -83
- package/admin/src/components/Combobox/react-select-custom-styles.tsx +121 -121
- package/admin/src/components/Combobox/styles.ts +46 -46
- package/admin/src/components/ConfirmModal/index.tsx +90 -90
- package/admin/src/components/EditView/CollectionTypeSearch/index.tsx +127 -127
- package/admin/src/components/EditView/CollectionTypeSettings/CreatePageButton/index.tsx +149 -149
- package/admin/src/components/EditView/CollectionTypeSettings/CreatePageButton/styles.ts +19 -19
- package/admin/src/components/EditView/CollectionTypeSettings/index.tsx +82 -82
- package/admin/src/components/EditView/Details/index.tsx +48 -48
- package/admin/src/components/EditView/Details/styles.ts +51 -51
- package/admin/src/components/EditView/PageSettings/index.tsx +124 -124
- package/admin/src/components/EditView/Platform/platform-select.tsx +30 -30
- package/admin/src/components/EditView/Template/TemplateConfirmModal/index.tsx +36 -36
- package/admin/src/components/EditView/Template/TemplateSelect/index.tsx +70 -70
- package/admin/src/components/EditView/Template/TemplateSelect/use-template-modules.ts +41 -41
- package/admin/src/components/EditView/index.tsx +35 -35
- package/admin/src/components/EditView/page-type-select.tsx +32 -32
- package/admin/src/components/EditView/wrapper.tsx +41 -41
- package/admin/src/components/GlobalPlatformSelect/index.tsx +40 -40
- package/admin/src/components/GlobalPlatformSelect/styles.ts +27 -27
- package/admin/src/components/Initializer/index.tsx +24 -24
- package/admin/src/components/PageFilters/PageTypeFilter/index.tsx +39 -39
- package/admin/src/components/PageFilters/PlatformFilter/index.tsx +32 -32
- package/admin/src/components/PageFilters/filters.tsx +189 -189
- package/admin/src/components/PageFilters/index.tsx +35 -35
- package/admin/src/components/PageTypeEditView/TemplatePlatformSelect/index.tsx +76 -76
- package/admin/src/components/PageTypeEditView/index.tsx +53 -53
- package/admin/src/components/PlatformFilteredSelectField/InputIcon/index.tsx +23 -23
- package/admin/src/components/PlatformFilteredSelectField/Multi/index.tsx +210 -210
- package/admin/src/components/PlatformFilteredSelectField/Single/index.tsx +197 -197
- package/admin/src/components/PlatformFilteredSelectField/hooks/useRelationLoad.tsx +128 -128
- package/admin/src/components/PlatformFilteredSelectField/index.tsx +85 -85
- package/admin/src/components/PlatformFilteredSelectField/styles.tsx +77 -77
- package/admin/src/components/PlatformFilteredSelectField/utils/get-translations.ts +3 -3
- package/admin/src/components/PlatformFilteredSelectField/utils/relation-helper.ts +147 -147
- package/admin/src/components/PluginIcon/index.tsx +94 -94
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/components/Relations/RelationInput.tsx +689 -689
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/components/Relations/RelationInputDataManager.tsx +6 -6
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/components/Relations/useRelation.ts +170 -170
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/components/Relations/utils/getRelationLink.ts +5 -5
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/components/Relations/utils/normalizeRelations.ts +52 -52
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/constants/attributes.ts +3 -3
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/hooks/useDragAndDrop.ts +253 -253
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/hooks/useKeyboardDragAndDrop.ts +96 -96
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/hooks/usePrev.ts +11 -11
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/dragAndDrop.ts +8 -8
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/paths.ts +29 -29
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/refs.ts +19 -19
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/translations.ts +3 -3
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/collection-types.ts +300 -300
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/components.ts +72 -72
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/content-types.ts +116 -116
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/index.ts +8 -8
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/init.ts +22 -22
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/relations.ts +80 -80
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/review-workflows.ts +88 -88
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/single-types.ts +112 -112
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/uid.ts +48 -48
- package/admin/src/components/StrapiCore/content-manager/shared/index.ts +1 -1
- package/admin/src/constants.ts +3 -3
- package/admin/src/index.tsx +101 -101
- package/admin/src/middlewares/index.tsx +37 -37
- package/admin/src/pages/app/index.tsx +14 -14
- package/admin/src/pluginId.ts +5 -5
- package/admin/src/translations/en.json +9 -9
- package/admin/src/translations/nl.json +9 -9
- package/admin/src/utils/findDomElement.ts +6 -6
- package/admin/src/utils/findElementParent.ts +20 -20
- package/admin/src/utils/getObjectFromFormName.ts +42 -42
- package/admin/src/utils/getRequestUrl.ts +11 -11
- package/admin/src/utils/getTrad.ts +5 -5
- package/admin/src/utils/hooks/useDebounce.ts +17 -17
- package/admin/src/utils/hooks/useDefaultPlatformFromLocalStorage.ts +61 -61
- package/admin/src/utils/hooks/useGetLocaleFromUrl.ts +9 -9
- package/admin/src/utils/hooks/useHideOverviewFilterTags.ts +34 -34
- package/admin/src/utils/hooks/usePlatformFormData.ts +64 -64
- package/admin/src/utils/hooks/usePrevious.ts +12 -12
- package/admin/src/utils/sanitizeModules.ts +93 -93
- package/custom.d.ts +6 -6
- package/dist/package.json +1 -1
- package/dist/server/bootstrap.js +1 -1
- package/dist/server/graphql/page-by-path.js +20 -17
- package/dist/server/graphql/pages-by-uid.js +5 -5
- package/dist/server/services/private-content/graphql/index.js +27 -27
- package/dist/server/services/private-content/graphql/types/index.js +74 -74
- package/dist/tsconfig.server.tsbuildinfo +1 -1
- package/package.json +78 -78
- package/server/bootstrap/collection-type-lifecycles.ts +47 -47
- package/server/bootstrap/permissions.ts +161 -161
- package/server/bootstrap.ts +261 -261
- package/server/config/index.ts +4 -4
- package/server/content-types/index.ts +7 -7
- package/server/content-types/user-category/schema.json +23 -23
- package/server/controllers/collection-types.ts +32 -32
- package/server/controllers/index.ts +19 -19
- package/server/controllers/page-type.ts +18 -18
- package/server/controllers/page.ts +9 -9
- package/server/controllers/platform.ts +21 -21
- package/server/controllers/private-content.ts +17 -17
- package/server/controllers/sitemap.ts +32 -32
- package/server/controllers/template.ts +16 -16
- package/server/controllers/user-category.ts +3 -3
- package/server/destroy.ts +5 -5
- package/server/graphql/index.ts +9 -9
- package/server/graphql/page-by-path.ts +135 -132
- package/server/graphql/page-type.ts +67 -67
- package/server/graphql/pages-by-uid.ts +89 -89
- package/server/index.ts +23 -23
- package/server/middlewares/index.ts +1 -1
- package/server/policies/index.ts +5 -5
- package/server/policies/isAuthorizedPage.ts +11 -11
- package/server/register.ts +22 -22
- package/server/routes/index.ts +115 -115
- package/server/routes/user-category.ts +3 -3
- package/server/schema/page-end.json +96 -96
- package/server/schema/page-start.json +87 -87
- package/server/schema/page-type-end.json +53 -53
- package/server/schema/page-type-start.json +38 -38
- package/server/schema/platform-start.json +21 -21
- package/server/schema/template-end.json +40 -40
- package/server/schema/template-start.json +35 -35
- package/server/services/builder.ts +232 -232
- package/server/services/collection-types.ts +95 -95
- package/server/services/email.ts +127 -127
- package/server/services/index.ts +23 -23
- package/server/services/page-type.ts +30 -30
- package/server/services/page.ts +24 -24
- package/server/services/platform.ts +30 -30
- package/server/services/private-content/components/admin-email.json +22 -22
- package/server/services/private-content/components/email.json +22 -22
- package/server/services/private-content/components/platform-email.json +30 -30
- package/server/services/private-content/constants/index.ts +13 -13
- package/server/services/private-content/graphql/index.ts +88 -88
- package/server/services/private-content/graphql/resolvers/findOnePage.ts +40 -40
- package/server/services/private-content/graphql/resolvers/findPage.ts +45 -45
- package/server/services/private-content/graphql/resolvers/forgot-password.ts +34 -34
- package/server/services/private-content/graphql/resolvers/login.ts +56 -56
- package/server/services/private-content/graphql/resolvers/register.ts +78 -78
- package/server/services/private-content/graphql/resolvers/reset-password.ts +44 -44
- package/server/services/private-content/graphql/types/index.ts +96 -96
- package/server/services/private-content/index.ts +95 -95
- package/server/services/private-content/mail-template/txtMail.email.template.text.ts +6 -6
- package/server/services/private-content/page.ts +20 -20
- package/server/services/private-content/platform.ts +19 -19
- package/server/services/private-content/schemas/index.ts +28 -28
- package/server/services/private-content/user.ts +197 -197
- package/server/services/sitemap.ts +83 -83
- package/server/services/template.ts +13 -13
- package/server/services/user-category.ts +3 -3
- package/server/utils/filter-underscore-arguments.ts +12 -12
- package/server/utils/reload-strapi-on-load.ts +13 -13
- package/server/utils/strapi.ts +50 -50
- package/shared/utils/constants.ts +8 -8
- package/shared/utils/sleep.ts +1 -1
- package/strapi-admin.js +3 -3
- package/strapi-server.js +3 -3
- package/tsconfig.json +20 -20
- package/tsconfig.server.json +25 -25
- package/dist/server/graphql/page-by-slug.js +0 -89
- package/dist/server/utils/graphql.js +0 -100
- package/dist/server/utils/paginationValidation.js +0 -31
package/admin/src/components/StrapiCore/admin/admin/src/content-manager/hooks/useDragAndDrop.ts
CHANGED
|
@@ -1,253 +1,253 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
useDrag,
|
|
5
|
-
useDrop,
|
|
6
|
-
type HandlerManager,
|
|
7
|
-
type ConnectDragSource,
|
|
8
|
-
type ConnectDropTarget,
|
|
9
|
-
type ConnectDragPreview,
|
|
10
|
-
type DragSourceMonitor
|
|
11
|
-
} from 'react-dnd';
|
|
12
|
-
|
|
13
|
-
import { useKeyboardDragAndDrop, type UseKeyboardDragAndDropCallbacks } from './useKeyboardDragAndDrop';
|
|
14
|
-
|
|
15
|
-
import type { Entity } from '@strapi/types';
|
|
16
|
-
|
|
17
|
-
const DIRECTIONS = {
|
|
18
|
-
UPWARD: 'upward',
|
|
19
|
-
DOWNWARD: 'downward'
|
|
20
|
-
} as const;
|
|
21
|
-
|
|
22
|
-
const DROP_SENSITIVITY = {
|
|
23
|
-
REGULAR: 'regular',
|
|
24
|
-
IMMEDIATE: 'immediate'
|
|
25
|
-
} as const;
|
|
26
|
-
|
|
27
|
-
interface UseDragAndDropOptions<
|
|
28
|
-
TIndex extends number | Array<number> = number,
|
|
29
|
-
TItem extends { index: TIndex } = { index: TIndex }
|
|
30
|
-
> extends UseKeyboardDragAndDropCallbacks<TIndex> {
|
|
31
|
-
type?: string;
|
|
32
|
-
index: TIndex;
|
|
33
|
-
item?: TItem;
|
|
34
|
-
onStart?: () => void;
|
|
35
|
-
onEnd?: () => void;
|
|
36
|
-
dropSensitivity?: (typeof DROP_SENSITIVITY)[keyof typeof DROP_SENSITIVITY];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
type Identifier = ReturnType<HandlerManager['getHandlerId']>;
|
|
40
|
-
|
|
41
|
-
type UseDragAndDropReturn = [
|
|
42
|
-
props: {
|
|
43
|
-
handlerId: Identifier;
|
|
44
|
-
isDragging: boolean;
|
|
45
|
-
handleKeyDown: (event: React.KeyboardEvent<HTMLButtonElement>) => void;
|
|
46
|
-
isOverDropTarget: boolean;
|
|
47
|
-
direction: (typeof DIRECTIONS)[keyof typeof DIRECTIONS] | null;
|
|
48
|
-
},
|
|
49
|
-
objectRef: React.RefObject<HTMLElement>,
|
|
50
|
-
dropRef: ConnectDropTarget,
|
|
51
|
-
dragRef: ConnectDragSource,
|
|
52
|
-
dragPreviewRef: ConnectDragPreview
|
|
53
|
-
];
|
|
54
|
-
|
|
55
|
-
type DropCollectedProps = {
|
|
56
|
-
handlerId: Identifier;
|
|
57
|
-
isOver: boolean;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* A utility hook abstracting the general drag and drop hooks from react-dnd.
|
|
62
|
-
* Centralising the same behaviours and by default offering keyboard support.
|
|
63
|
-
*/
|
|
64
|
-
const useDragAndDrop = <
|
|
65
|
-
TIndex extends number | Array<number>,
|
|
66
|
-
TItem extends { index: TIndex; id?: Entity.ID; [key: string]: unknown }
|
|
67
|
-
>(
|
|
68
|
-
active: boolean,
|
|
69
|
-
{
|
|
70
|
-
type = 'STRAPI_DND',
|
|
71
|
-
index,
|
|
72
|
-
item,
|
|
73
|
-
onStart,
|
|
74
|
-
onEnd,
|
|
75
|
-
onGrabItem,
|
|
76
|
-
onDropItem,
|
|
77
|
-
onCancel,
|
|
78
|
-
onMoveItem,
|
|
79
|
-
dropSensitivity = DROP_SENSITIVITY.REGULAR
|
|
80
|
-
}: UseDragAndDropOptions<TIndex, TItem>
|
|
81
|
-
): UseDragAndDropReturn => {
|
|
82
|
-
const objectRef = React.useRef<HTMLElement>(null);
|
|
83
|
-
|
|
84
|
-
const [{ handlerId, isOver }, dropRef] = useDrop<TItem, void, DropCollectedProps>({
|
|
85
|
-
accept: type,
|
|
86
|
-
collect(monitor) {
|
|
87
|
-
return {
|
|
88
|
-
handlerId: monitor.getHandlerId(),
|
|
89
|
-
isOver: monitor.isOver({ shallow: true })
|
|
90
|
-
};
|
|
91
|
-
},
|
|
92
|
-
drop(item) {
|
|
93
|
-
const draggedIndex = item.index;
|
|
94
|
-
const newIndex = index;
|
|
95
|
-
|
|
96
|
-
if (isOver && onDropItem) {
|
|
97
|
-
onDropItem(draggedIndex, newIndex);
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
hover(item, monitor) {
|
|
101
|
-
if (!objectRef.current || !onMoveItem) {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const dragIndex = item.index;
|
|
106
|
-
const newIndex = index;
|
|
107
|
-
|
|
108
|
-
const hoverBoundingRect = objectRef.current?.getBoundingClientRect();
|
|
109
|
-
const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
|
|
110
|
-
const clientOffset = monitor.getClientOffset();
|
|
111
|
-
if (!clientOffset) return;
|
|
112
|
-
|
|
113
|
-
const hoverClientY = clientOffset && clientOffset.y - hoverBoundingRect.top;
|
|
114
|
-
if (typeof dragIndex === 'number' && typeof newIndex === 'number') {
|
|
115
|
-
if (dragIndex === newIndex) {
|
|
116
|
-
// Don't replace items with themselves
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (dropSensitivity === DROP_SENSITIVITY.REGULAR) {
|
|
121
|
-
// Dragging downwards
|
|
122
|
-
if (dragIndex < newIndex && hoverClientY < hoverMiddleY) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Dragging upwards
|
|
127
|
-
if (dragIndex > newIndex && hoverClientY > hoverMiddleY) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Time to actually perform the action
|
|
133
|
-
onMoveItem(newIndex, dragIndex);
|
|
134
|
-
item.index = newIndex;
|
|
135
|
-
} else {
|
|
136
|
-
// Using numbers as indices doesn't work for nested list items with path like [1, 1, 0]
|
|
137
|
-
if (Array.isArray(dragIndex) && Array.isArray(newIndex))
|
|
138
|
-
if (dropSensitivity === DROP_SENSITIVITY.REGULAR) {
|
|
139
|
-
// Indices comparison to find item position in nested list
|
|
140
|
-
const minLength = Math.min(dragIndex.length, newIndex.length);
|
|
141
|
-
let areEqual = true;
|
|
142
|
-
let isLessThan = false;
|
|
143
|
-
let isGreaterThan = false;
|
|
144
|
-
|
|
145
|
-
for (let i = 0; i < minLength; i++) {
|
|
146
|
-
if (dragIndex[i] < newIndex[i]) {
|
|
147
|
-
isLessThan = true;
|
|
148
|
-
areEqual = false;
|
|
149
|
-
break;
|
|
150
|
-
} else if (dragIndex[i] > newIndex[i]) {
|
|
151
|
-
isGreaterThan = true;
|
|
152
|
-
areEqual = false;
|
|
153
|
-
break;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// Don't replace items with themselves
|
|
158
|
-
if (areEqual && dragIndex.length === newIndex.length) {
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
// Dragging downwards
|
|
162
|
-
if (isLessThan && !isGreaterThan && hoverClientY < hoverMiddleY) {
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// Dragging upwards
|
|
167
|
-
if (isGreaterThan && !isLessThan && hoverClientY > hoverMiddleY) {
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
onMoveItem(newIndex, dragIndex);
|
|
172
|
-
item.index = newIndex;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
const getDragDirection = (monitor: DragSourceMonitor<TItem, void>) => {
|
|
178
|
-
if (
|
|
179
|
-
monitor &&
|
|
180
|
-
monitor.isDragging() &&
|
|
181
|
-
!monitor.didDrop() &&
|
|
182
|
-
monitor.getInitialClientOffset() &&
|
|
183
|
-
monitor.getClientOffset()
|
|
184
|
-
) {
|
|
185
|
-
const deltaY = monitor.getInitialClientOffset()!.y - monitor.getClientOffset()!.y;
|
|
186
|
-
|
|
187
|
-
if (deltaY > 0) return DIRECTIONS.UPWARD;
|
|
188
|
-
|
|
189
|
-
if (deltaY < 0) return DIRECTIONS.DOWNWARD;
|
|
190
|
-
|
|
191
|
-
return null;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return null;
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
const [{ isDragging, direction }, dragRef, dragPreviewRef] = useDrag({
|
|
198
|
-
type,
|
|
199
|
-
item() {
|
|
200
|
-
if (onStart) {
|
|
201
|
-
onStart();
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* This will be attached and it helps define the preview sizes
|
|
206
|
-
* when a component is flexy e.g. Relations
|
|
207
|
-
*/
|
|
208
|
-
const { width } = objectRef.current?.getBoundingClientRect() ?? {};
|
|
209
|
-
|
|
210
|
-
return { index, width, ...item };
|
|
211
|
-
},
|
|
212
|
-
end() {
|
|
213
|
-
if (onEnd) {
|
|
214
|
-
onEnd();
|
|
215
|
-
}
|
|
216
|
-
},
|
|
217
|
-
canDrag: active,
|
|
218
|
-
/**
|
|
219
|
-
* This is useful when the item is in a virtualized list.
|
|
220
|
-
* However, if we don't have an ID then we want the libraries
|
|
221
|
-
* defaults to take care of this.
|
|
222
|
-
*/
|
|
223
|
-
isDragging: item?.id
|
|
224
|
-
? (monitor) => {
|
|
225
|
-
return item.id === monitor.getItem().id;
|
|
226
|
-
}
|
|
227
|
-
: undefined,
|
|
228
|
-
collect: (monitor) => ({
|
|
229
|
-
isDragging: monitor.isDragging(),
|
|
230
|
-
initialOffset: monitor.getInitialClientOffset(),
|
|
231
|
-
currentOffset: monitor.getClientOffset(),
|
|
232
|
-
direction: getDragDirection(monitor)
|
|
233
|
-
})
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
const handleKeyDown = useKeyboardDragAndDrop(active, index, {
|
|
237
|
-
onGrabItem,
|
|
238
|
-
onDropItem,
|
|
239
|
-
onCancel,
|
|
240
|
-
onMoveItem
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
return [
|
|
244
|
-
{ handlerId, isDragging, handleKeyDown, isOverDropTarget: isOver, direction },
|
|
245
|
-
objectRef,
|
|
246
|
-
dropRef,
|
|
247
|
-
dragRef,
|
|
248
|
-
dragPreviewRef
|
|
249
|
-
];
|
|
250
|
-
};
|
|
251
|
-
|
|
252
|
-
export type { UseDragAndDropReturn, UseDragAndDropOptions };
|
|
253
|
-
export { useDragAndDrop, DIRECTIONS, DROP_SENSITIVITY };
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
useDrag,
|
|
5
|
+
useDrop,
|
|
6
|
+
type HandlerManager,
|
|
7
|
+
type ConnectDragSource,
|
|
8
|
+
type ConnectDropTarget,
|
|
9
|
+
type ConnectDragPreview,
|
|
10
|
+
type DragSourceMonitor
|
|
11
|
+
} from 'react-dnd';
|
|
12
|
+
|
|
13
|
+
import { useKeyboardDragAndDrop, type UseKeyboardDragAndDropCallbacks } from './useKeyboardDragAndDrop';
|
|
14
|
+
|
|
15
|
+
import type { Entity } from '@strapi/types';
|
|
16
|
+
|
|
17
|
+
const DIRECTIONS = {
|
|
18
|
+
UPWARD: 'upward',
|
|
19
|
+
DOWNWARD: 'downward'
|
|
20
|
+
} as const;
|
|
21
|
+
|
|
22
|
+
const DROP_SENSITIVITY = {
|
|
23
|
+
REGULAR: 'regular',
|
|
24
|
+
IMMEDIATE: 'immediate'
|
|
25
|
+
} as const;
|
|
26
|
+
|
|
27
|
+
interface UseDragAndDropOptions<
|
|
28
|
+
TIndex extends number | Array<number> = number,
|
|
29
|
+
TItem extends { index: TIndex } = { index: TIndex }
|
|
30
|
+
> extends UseKeyboardDragAndDropCallbacks<TIndex> {
|
|
31
|
+
type?: string;
|
|
32
|
+
index: TIndex;
|
|
33
|
+
item?: TItem;
|
|
34
|
+
onStart?: () => void;
|
|
35
|
+
onEnd?: () => void;
|
|
36
|
+
dropSensitivity?: (typeof DROP_SENSITIVITY)[keyof typeof DROP_SENSITIVITY];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type Identifier = ReturnType<HandlerManager['getHandlerId']>;
|
|
40
|
+
|
|
41
|
+
type UseDragAndDropReturn = [
|
|
42
|
+
props: {
|
|
43
|
+
handlerId: Identifier;
|
|
44
|
+
isDragging: boolean;
|
|
45
|
+
handleKeyDown: (event: React.KeyboardEvent<HTMLButtonElement>) => void;
|
|
46
|
+
isOverDropTarget: boolean;
|
|
47
|
+
direction: (typeof DIRECTIONS)[keyof typeof DIRECTIONS] | null;
|
|
48
|
+
},
|
|
49
|
+
objectRef: React.RefObject<HTMLElement>,
|
|
50
|
+
dropRef: ConnectDropTarget,
|
|
51
|
+
dragRef: ConnectDragSource,
|
|
52
|
+
dragPreviewRef: ConnectDragPreview
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
type DropCollectedProps = {
|
|
56
|
+
handlerId: Identifier;
|
|
57
|
+
isOver: boolean;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A utility hook abstracting the general drag and drop hooks from react-dnd.
|
|
62
|
+
* Centralising the same behaviours and by default offering keyboard support.
|
|
63
|
+
*/
|
|
64
|
+
const useDragAndDrop = <
|
|
65
|
+
TIndex extends number | Array<number>,
|
|
66
|
+
TItem extends { index: TIndex; id?: Entity.ID; [key: string]: unknown }
|
|
67
|
+
>(
|
|
68
|
+
active: boolean,
|
|
69
|
+
{
|
|
70
|
+
type = 'STRAPI_DND',
|
|
71
|
+
index,
|
|
72
|
+
item,
|
|
73
|
+
onStart,
|
|
74
|
+
onEnd,
|
|
75
|
+
onGrabItem,
|
|
76
|
+
onDropItem,
|
|
77
|
+
onCancel,
|
|
78
|
+
onMoveItem,
|
|
79
|
+
dropSensitivity = DROP_SENSITIVITY.REGULAR
|
|
80
|
+
}: UseDragAndDropOptions<TIndex, TItem>
|
|
81
|
+
): UseDragAndDropReturn => {
|
|
82
|
+
const objectRef = React.useRef<HTMLElement>(null);
|
|
83
|
+
|
|
84
|
+
const [{ handlerId, isOver }, dropRef] = useDrop<TItem, void, DropCollectedProps>({
|
|
85
|
+
accept: type,
|
|
86
|
+
collect(monitor) {
|
|
87
|
+
return {
|
|
88
|
+
handlerId: monitor.getHandlerId(),
|
|
89
|
+
isOver: monitor.isOver({ shallow: true })
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
drop(item) {
|
|
93
|
+
const draggedIndex = item.index;
|
|
94
|
+
const newIndex = index;
|
|
95
|
+
|
|
96
|
+
if (isOver && onDropItem) {
|
|
97
|
+
onDropItem(draggedIndex, newIndex);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
hover(item, monitor) {
|
|
101
|
+
if (!objectRef.current || !onMoveItem) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const dragIndex = item.index;
|
|
106
|
+
const newIndex = index;
|
|
107
|
+
|
|
108
|
+
const hoverBoundingRect = objectRef.current?.getBoundingClientRect();
|
|
109
|
+
const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
|
|
110
|
+
const clientOffset = monitor.getClientOffset();
|
|
111
|
+
if (!clientOffset) return;
|
|
112
|
+
|
|
113
|
+
const hoverClientY = clientOffset && clientOffset.y - hoverBoundingRect.top;
|
|
114
|
+
if (typeof dragIndex === 'number' && typeof newIndex === 'number') {
|
|
115
|
+
if (dragIndex === newIndex) {
|
|
116
|
+
// Don't replace items with themselves
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (dropSensitivity === DROP_SENSITIVITY.REGULAR) {
|
|
121
|
+
// Dragging downwards
|
|
122
|
+
if (dragIndex < newIndex && hoverClientY < hoverMiddleY) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Dragging upwards
|
|
127
|
+
if (dragIndex > newIndex && hoverClientY > hoverMiddleY) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Time to actually perform the action
|
|
133
|
+
onMoveItem(newIndex, dragIndex);
|
|
134
|
+
item.index = newIndex;
|
|
135
|
+
} else {
|
|
136
|
+
// Using numbers as indices doesn't work for nested list items with path like [1, 1, 0]
|
|
137
|
+
if (Array.isArray(dragIndex) && Array.isArray(newIndex))
|
|
138
|
+
if (dropSensitivity === DROP_SENSITIVITY.REGULAR) {
|
|
139
|
+
// Indices comparison to find item position in nested list
|
|
140
|
+
const minLength = Math.min(dragIndex.length, newIndex.length);
|
|
141
|
+
let areEqual = true;
|
|
142
|
+
let isLessThan = false;
|
|
143
|
+
let isGreaterThan = false;
|
|
144
|
+
|
|
145
|
+
for (let i = 0; i < minLength; i++) {
|
|
146
|
+
if (dragIndex[i] < newIndex[i]) {
|
|
147
|
+
isLessThan = true;
|
|
148
|
+
areEqual = false;
|
|
149
|
+
break;
|
|
150
|
+
} else if (dragIndex[i] > newIndex[i]) {
|
|
151
|
+
isGreaterThan = true;
|
|
152
|
+
areEqual = false;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Don't replace items with themselves
|
|
158
|
+
if (areEqual && dragIndex.length === newIndex.length) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
// Dragging downwards
|
|
162
|
+
if (isLessThan && !isGreaterThan && hoverClientY < hoverMiddleY) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Dragging upwards
|
|
167
|
+
if (isGreaterThan && !isLessThan && hoverClientY > hoverMiddleY) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
onMoveItem(newIndex, dragIndex);
|
|
172
|
+
item.index = newIndex;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const getDragDirection = (monitor: DragSourceMonitor<TItem, void>) => {
|
|
178
|
+
if (
|
|
179
|
+
monitor &&
|
|
180
|
+
monitor.isDragging() &&
|
|
181
|
+
!monitor.didDrop() &&
|
|
182
|
+
monitor.getInitialClientOffset() &&
|
|
183
|
+
monitor.getClientOffset()
|
|
184
|
+
) {
|
|
185
|
+
const deltaY = monitor.getInitialClientOffset()!.y - monitor.getClientOffset()!.y;
|
|
186
|
+
|
|
187
|
+
if (deltaY > 0) return DIRECTIONS.UPWARD;
|
|
188
|
+
|
|
189
|
+
if (deltaY < 0) return DIRECTIONS.DOWNWARD;
|
|
190
|
+
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return null;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const [{ isDragging, direction }, dragRef, dragPreviewRef] = useDrag({
|
|
198
|
+
type,
|
|
199
|
+
item() {
|
|
200
|
+
if (onStart) {
|
|
201
|
+
onStart();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* This will be attached and it helps define the preview sizes
|
|
206
|
+
* when a component is flexy e.g. Relations
|
|
207
|
+
*/
|
|
208
|
+
const { width } = objectRef.current?.getBoundingClientRect() ?? {};
|
|
209
|
+
|
|
210
|
+
return { index, width, ...item };
|
|
211
|
+
},
|
|
212
|
+
end() {
|
|
213
|
+
if (onEnd) {
|
|
214
|
+
onEnd();
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
canDrag: active,
|
|
218
|
+
/**
|
|
219
|
+
* This is useful when the item is in a virtualized list.
|
|
220
|
+
* However, if we don't have an ID then we want the libraries
|
|
221
|
+
* defaults to take care of this.
|
|
222
|
+
*/
|
|
223
|
+
isDragging: item?.id
|
|
224
|
+
? (monitor) => {
|
|
225
|
+
return item.id === monitor.getItem().id;
|
|
226
|
+
}
|
|
227
|
+
: undefined,
|
|
228
|
+
collect: (monitor) => ({
|
|
229
|
+
isDragging: monitor.isDragging(),
|
|
230
|
+
initialOffset: monitor.getInitialClientOffset(),
|
|
231
|
+
currentOffset: monitor.getClientOffset(),
|
|
232
|
+
direction: getDragDirection(monitor)
|
|
233
|
+
})
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
const handleKeyDown = useKeyboardDragAndDrop(active, index, {
|
|
237
|
+
onGrabItem,
|
|
238
|
+
onDropItem,
|
|
239
|
+
onCancel,
|
|
240
|
+
onMoveItem
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
return [
|
|
244
|
+
{ handlerId, isDragging, handleKeyDown, isOverDropTarget: isOver, direction },
|
|
245
|
+
objectRef,
|
|
246
|
+
dropRef,
|
|
247
|
+
dragRef,
|
|
248
|
+
dragPreviewRef
|
|
249
|
+
];
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
export type { UseDragAndDropReturn, UseDragAndDropOptions };
|
|
253
|
+
export { useDragAndDrop, DIRECTIONS, DROP_SENSITIVITY };
|