@webbio/strapi-plugin-page-builder 0.8.5-platform → 0.9.1-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/admin/src/components/PlatformFilteredSelectField/Multi/index.tsx +19 -7
- package/admin/src/components/PlatformFilteredSelectField/hooks/useRelationLoad.tsx +4 -2
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/components/Relations/RelationInput.tsx +689 -0
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/components/Relations/RelationInputDataManager.tsx +6 -0
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/components/Relations/useRelation.ts +170 -0
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/components/Relations/utils/getRelationLink.ts +5 -0
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/components/Relations/utils/normalizeRelations.ts +52 -0
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/constants/attributes.ts +3 -0
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/hooks/useDragAndDrop.ts +253 -0
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/hooks/useKeyboardDragAndDrop.ts +96 -0
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/hooks/usePrev.ts +11 -0
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/dragAndDrop.ts +8 -0
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/paths.ts +29 -0
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/refs.ts +19 -0
- package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/translations.ts +3 -0
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/collection-types.ts +300 -0
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/components.ts +72 -0
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/content-types.ts +116 -0
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/index.ts +8 -0
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/init.ts +22 -0
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/relations.ts +80 -0
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/review-workflows.ts +88 -0
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/single-types.ts +112 -0
- package/admin/src/components/StrapiCore/content-manager/shared/contracts/uid.ts +48 -0
- package/admin/src/components/StrapiCore/content-manager/shared/index.ts +1 -0
- package/dist/package.json +1 -1
- package/dist/tsconfig.server.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// @ts-nocheck build compiler thinks differently than runtime compiler
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
import { useCallbackRef, useFetchClient } from '@strapi/helper-plugin';
|
|
5
|
+
import { useInfiniteQuery } from 'react-query';
|
|
6
|
+
|
|
7
|
+
import { NormalizeRelationArgs, NormalizedRelation, normalizeRelations } from './utils/normalizeRelations';
|
|
8
|
+
|
|
9
|
+
import type { Contracts } from '../../../../../../content-manager/shared';
|
|
10
|
+
|
|
11
|
+
interface UseRelationArgs {
|
|
12
|
+
relation: {
|
|
13
|
+
enabled: boolean;
|
|
14
|
+
endpoint: string;
|
|
15
|
+
normalizeArguments: NormalizeRelationArgs;
|
|
16
|
+
onLoad: (data: NormalizedRelation[]) => void;
|
|
17
|
+
pageParams?: Record<string, any>;
|
|
18
|
+
pageGoal?: number;
|
|
19
|
+
};
|
|
20
|
+
search: {
|
|
21
|
+
endpoint: string;
|
|
22
|
+
pageParams?: Record<string, any>;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const useRelation = (cacheKey: any[] = [], { relation, search }: UseRelationArgs) => {
|
|
27
|
+
const [searchParams, setSearchParams] = useState({});
|
|
28
|
+
const [currentPage, setCurrentPage] = useState(0);
|
|
29
|
+
const { get } = useFetchClient();
|
|
30
|
+
|
|
31
|
+
const { onLoad: onLoadRelations, normalizeArguments } = relation;
|
|
32
|
+
|
|
33
|
+
const relationsRes = useInfiniteQuery(
|
|
34
|
+
['relation', ...cacheKey],
|
|
35
|
+
async ({ pageParam = 1 }) => {
|
|
36
|
+
try {
|
|
37
|
+
const { data } = await get<Contracts.Relations.FindExisting.Response>(relation?.endpoint, {
|
|
38
|
+
params: {
|
|
39
|
+
...(relation.pageParams ?? {}),
|
|
40
|
+
page: pageParam
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
setCurrentPage(pageParam);
|
|
45
|
+
|
|
46
|
+
return data;
|
|
47
|
+
} catch (err) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
cacheTime: 0,
|
|
53
|
+
enabled: relation.enabled,
|
|
54
|
+
getNextPageParam(lastPage) {
|
|
55
|
+
const isXToOneRelation = lastPage && !('pagination' in lastPage);
|
|
56
|
+
|
|
57
|
+
if (
|
|
58
|
+
!lastPage || // the API may send an empty 204 response
|
|
59
|
+
isXToOneRelation || // xToOne relations do not have a pagination
|
|
60
|
+
lastPage?.pagination.page >= lastPage?.pagination.pageCount
|
|
61
|
+
) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// eslint-disable-next-line consistent-return
|
|
66
|
+
return lastPage.pagination.page + 1;
|
|
67
|
+
},
|
|
68
|
+
select: (data) => ({
|
|
69
|
+
...data,
|
|
70
|
+
pages: data.pages.map((page) => {
|
|
71
|
+
if (!page) {
|
|
72
|
+
return page;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let normalizedResults: Contracts.Relations.RelationResult[] = [];
|
|
76
|
+
|
|
77
|
+
// xToOne relations return an object, which we normalize so that relations
|
|
78
|
+
// always have the same shape
|
|
79
|
+
if ('data' in page && page.data) {
|
|
80
|
+
normalizedResults = [page.data];
|
|
81
|
+
} else if ('results' in page && page.results) {
|
|
82
|
+
normalizedResults = [...page.results].reverse();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
pagination: 'pagination' in page ? page.pagination : undefined,
|
|
87
|
+
results: normalizedResults
|
|
88
|
+
};
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const { pageGoal } = relation;
|
|
95
|
+
|
|
96
|
+
const { status, data, fetchNextPage, hasNextPage } = relationsRes;
|
|
97
|
+
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
/**
|
|
100
|
+
* This ensures the infiniteQuery hook fetching has caught-up with the modifiedData
|
|
101
|
+
* state i.e. in circumstances where you add 10 relations, the browserState knows this,
|
|
102
|
+
* but the hook would think it could fetch more, when in reality, it can't.
|
|
103
|
+
*/
|
|
104
|
+
if (pageGoal && pageGoal > currentPage && hasNextPage && status === 'success') {
|
|
105
|
+
fetchNextPage({
|
|
106
|
+
pageParam: currentPage + 1
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}, [pageGoal, currentPage, fetchNextPage, hasNextPage, status]);
|
|
110
|
+
|
|
111
|
+
const onLoadRelationsCallback = useCallbackRef(onLoadRelations);
|
|
112
|
+
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
if (status === 'success' && data && data.pages?.at(-1)?.results && onLoadRelationsCallback) {
|
|
115
|
+
// everytime we fetch, we normalize prior to adding to redux
|
|
116
|
+
const normalizedResults = normalizeRelations(data.pages.at(-1)?.results ?? [], normalizeArguments);
|
|
117
|
+
|
|
118
|
+
// this is loadRelation from EditViewDataManagerProvider
|
|
119
|
+
onLoadRelationsCallback(normalizedResults);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
123
|
+
}, [status, onLoadRelationsCallback, data]);
|
|
124
|
+
|
|
125
|
+
const searchRes = useInfiniteQuery(
|
|
126
|
+
['relation', ...cacheKey, 'search', JSON.stringify(searchParams)],
|
|
127
|
+
async ({ pageParam = 1 }) => {
|
|
128
|
+
try {
|
|
129
|
+
const { data } = await get<Contracts.Relations.FindAvailable.Response>(search.endpoint, {
|
|
130
|
+
params: {
|
|
131
|
+
...(search.pageParams ?? {}),
|
|
132
|
+
...searchParams,
|
|
133
|
+
page: pageParam
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
return data;
|
|
138
|
+
} catch (err) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
enabled: Object.keys(searchParams).length > 0,
|
|
144
|
+
getNextPageParam(lastPage) {
|
|
145
|
+
if (
|
|
146
|
+
!lastPage?.pagination ||
|
|
147
|
+
(lastPage.pagination && lastPage.pagination.page >= lastPage.pagination.pageCount)
|
|
148
|
+
) {
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// eslint-disable-next-line consistent-return
|
|
153
|
+
return lastPage.pagination.page + 1;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
const searchFor = (term: string, options: object = {}) => {
|
|
159
|
+
setSearchParams({
|
|
160
|
+
...options,
|
|
161
|
+
_q: term,
|
|
162
|
+
_filter: '$containsi'
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
return { relations: relationsRes, search: searchRes, searchFor };
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export { useRelation };
|
|
170
|
+
export type { UseRelationArgs };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// @ts-nocheck build compiler thinks differently than runtime compiler
|
|
2
|
+
import type { Contracts } from '../../../../../../../content-manager/shared';
|
|
3
|
+
|
|
4
|
+
import { PUBLICATION_STATES } from '../RelationInputDataManager';
|
|
5
|
+
|
|
6
|
+
import { getRelationLink } from './getRelationLink';
|
|
7
|
+
|
|
8
|
+
export interface NormalizeRelationArgs {
|
|
9
|
+
shouldAddLink: boolean;
|
|
10
|
+
mainFieldName: string;
|
|
11
|
+
targetModel: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type NormalizedRelation = Contracts.Relations.RelationResult & {
|
|
15
|
+
href?: string;
|
|
16
|
+
mainField: string;
|
|
17
|
+
publicationState?: false | 'published' | 'draft';
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const normalizeRelation = (
|
|
21
|
+
relation: Contracts.Relations.RelationResult,
|
|
22
|
+
{ shouldAddLink, mainFieldName, targetModel }: NormalizeRelationArgs
|
|
23
|
+
) => {
|
|
24
|
+
const nextRelation: NormalizedRelation = {
|
|
25
|
+
...relation,
|
|
26
|
+
mainField: relation[mainFieldName]
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
if (shouldAddLink) {
|
|
30
|
+
nextRelation.href = getRelationLink(targetModel, nextRelation.id);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
nextRelation.publicationState = false;
|
|
34
|
+
|
|
35
|
+
if (nextRelation?.publishedAt !== undefined) {
|
|
36
|
+
nextRelation.publicationState = nextRelation.publishedAt ? PUBLICATION_STATES.PUBLISHED : PUBLICATION_STATES.DRAFT;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return nextRelation;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/*
|
|
43
|
+
* Applies some transformations to existing and new relations in order to display them correctly
|
|
44
|
+
* relations: raw relations data coming from useRelations
|
|
45
|
+
* shouldAddLink: comes from generateRelationQueryInfos, if true we display a link to the relation (TO FIX: explanation)
|
|
46
|
+
* mainFieldName: name of the main field inside the relation (e.g. text field), if no displayable main field exists (e.g. date field) we use the id of the entry
|
|
47
|
+
* targetModel: the model on which the relation is based on, used to create an URL link
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
export const normalizeRelations = (relations: Contracts.Relations.RelationResult[], args: NormalizeRelationArgs) => {
|
|
51
|
+
return [...relations].map((relation) => normalizeRelation(relation, args));
|
|
52
|
+
};
|
package/admin/src/components/StrapiCore/admin/admin/src/content-manager/hooks/useDragAndDrop.ts
ADDED
|
@@ -0,0 +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 };
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
export type UseKeyboardDragAndDropCallbacks<TIndex extends number | Array<number> = number> = {
|
|
4
|
+
onCancel?: (index: TIndex) => void;
|
|
5
|
+
onDropItem?: (currentIndex: TIndex, newIndex?: TIndex) => void;
|
|
6
|
+
onGrabItem?: (index: TIndex) => void;
|
|
7
|
+
onMoveItem?: (newIndex: TIndex, currentIndex: TIndex) => void;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Utility hook designed to implement keyboard accessibile drag and drop by
|
|
12
|
+
* returning an onKeyDown handler to be passed to the drag icon button.
|
|
13
|
+
*
|
|
14
|
+
* @internal - You should use `useDragAndDrop` instead.
|
|
15
|
+
*/
|
|
16
|
+
export const useKeyboardDragAndDrop = <TIndex extends number | Array<number> = number>(
|
|
17
|
+
active: boolean,
|
|
18
|
+
index: TIndex,
|
|
19
|
+
{ onCancel, onDropItem, onGrabItem, onMoveItem }: UseKeyboardDragAndDropCallbacks<TIndex>
|
|
20
|
+
) => {
|
|
21
|
+
const [isSelected, setIsSelected] = React.useState(false);
|
|
22
|
+
|
|
23
|
+
const handleMove = (movement: 'UP' | 'DOWN') => {
|
|
24
|
+
if (!isSelected) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (typeof index === 'number' && onMoveItem) {
|
|
28
|
+
if (movement === 'UP') {
|
|
29
|
+
onMoveItem((index - 1) as TIndex, index);
|
|
30
|
+
} else if (movement === 'DOWN') {
|
|
31
|
+
onMoveItem((index + 1) as TIndex, index);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const handleDragClick = () => {
|
|
37
|
+
if (isSelected) {
|
|
38
|
+
if (onDropItem) {
|
|
39
|
+
onDropItem(index);
|
|
40
|
+
}
|
|
41
|
+
setIsSelected(false);
|
|
42
|
+
} else {
|
|
43
|
+
if (onGrabItem) {
|
|
44
|
+
onGrabItem(index);
|
|
45
|
+
}
|
|
46
|
+
setIsSelected(true);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const handleCancel = () => {
|
|
51
|
+
if (isSelected) {
|
|
52
|
+
setIsSelected(false);
|
|
53
|
+
|
|
54
|
+
if (onCancel) {
|
|
55
|
+
onCancel(index);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const handleKeyDown: React.KeyboardEventHandler<HTMLButtonElement> = (e) => {
|
|
61
|
+
if (!active) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (e.key === 'Tab' && !isSelected) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
e.preventDefault();
|
|
70
|
+
|
|
71
|
+
switch (e.key) {
|
|
72
|
+
case ' ':
|
|
73
|
+
case 'Enter':
|
|
74
|
+
handleDragClick();
|
|
75
|
+
break;
|
|
76
|
+
|
|
77
|
+
case 'Escape':
|
|
78
|
+
handleCancel();
|
|
79
|
+
break;
|
|
80
|
+
|
|
81
|
+
case 'ArrowDown':
|
|
82
|
+
case 'ArrowRight':
|
|
83
|
+
handleMove('DOWN');
|
|
84
|
+
break;
|
|
85
|
+
|
|
86
|
+
case 'ArrowUp':
|
|
87
|
+
case 'ArrowLeft':
|
|
88
|
+
handleMove('UP');
|
|
89
|
+
break;
|
|
90
|
+
|
|
91
|
+
default:
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return handleKeyDown;
|
|
96
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import get from 'lodash/get';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This is typically used in circumstances where there are re-orderable pieces e.g. Dynamic Zones
|
|
5
|
+
* or Repeatable fields. It finds the _original_ location of the initial data using `__temp_key__` values
|
|
6
|
+
* which are added to the fields in the `INIT_FORM` reducer to give array data a stable (when you add
|
|
7
|
+
* a new item they wont have a server ID).
|
|
8
|
+
*/
|
|
9
|
+
export const getInitialDataPathUsingTempKeys =
|
|
10
|
+
(initialData: Record<string, any>, modifiedData: Record<string, any>) => (currentPath: string) => {
|
|
11
|
+
const splitPath = currentPath.split('.');
|
|
12
|
+
|
|
13
|
+
return splitPath.reduce<string[]>((acc, currentValue, index) => {
|
|
14
|
+
const initialDataParent = get(initialData, acc);
|
|
15
|
+
const modifiedDataTempKey = get(modifiedData, [...splitPath.slice(0, index), currentValue, '__temp_key__']);
|
|
16
|
+
|
|
17
|
+
if (Array.isArray(initialDataParent) && typeof modifiedDataTempKey === 'number') {
|
|
18
|
+
const initialDataIndex = initialDataParent.findIndex((entry) => entry.__temp_key__ === modifiedDataTempKey);
|
|
19
|
+
|
|
20
|
+
acc.push(initialDataIndex.toString());
|
|
21
|
+
|
|
22
|
+
return acc;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
acc.push(currentValue);
|
|
26
|
+
|
|
27
|
+
return acc;
|
|
28
|
+
}, []);
|
|
29
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { MutableRefObject, Ref } from 'react';
|
|
2
|
+
|
|
3
|
+
type PossibleRef<T> = Ref<T> | undefined;
|
|
4
|
+
|
|
5
|
+
const setRef = <T>(ref: PossibleRef<T>, value: T) => {
|
|
6
|
+
if (typeof ref === 'function') {
|
|
7
|
+
ref(value);
|
|
8
|
+
} else if (ref !== null && ref !== undefined) {
|
|
9
|
+
(ref as MutableRefObject<T>).current = value;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A utility to compose multiple refs together
|
|
15
|
+
* Accepts callback refs and RefObject(s)
|
|
16
|
+
*/
|
|
17
|
+
export const composeRefs = <T>(...refs: PossibleRef<T>[]) => {
|
|
18
|
+
return (node: T) => refs.forEach((ref) => setRef(ref, node));
|
|
19
|
+
};
|