@webbio/strapi-plugin-page-builder 0.8.4-platform → 0.9.0-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.
Files changed (27) hide show
  1. package/admin/src/components/Combobox/react-select-custom-styles.tsx +1 -0
  2. package/admin/src/components/PlatformFilteredSelectField/Multi/index.tsx +21 -10
  3. package/admin/src/components/StrapiCore/admin/admin/src/content-manager/components/Relations/RelationInput.tsx +690 -0
  4. package/admin/src/components/StrapiCore/admin/admin/src/content-manager/components/Relations/RelationInputDataManager.tsx +6 -0
  5. package/admin/src/components/StrapiCore/admin/admin/src/content-manager/constants/attributes.ts +3 -0
  6. package/admin/src/components/StrapiCore/admin/admin/src/content-manager/hooks/useDragAndDrop.ts +253 -0
  7. package/admin/src/components/StrapiCore/admin/admin/src/content-manager/hooks/useKeyboardDragAndDrop.ts +96 -0
  8. package/admin/src/components/StrapiCore/admin/admin/src/content-manager/hooks/usePrev.ts +11 -0
  9. package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/dragAndDrop.ts +8 -0
  10. package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/normalizeRelations.ts +7 -0
  11. package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/paths.ts +36 -0
  12. package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/refs.ts +19 -0
  13. package/admin/src/components/StrapiCore/admin/admin/src/content-manager/utils/translations.ts +3 -0
  14. package/admin/src/components/StrapiCore/content-manager/shared/contracts/collection-types.ts +300 -0
  15. package/admin/src/components/StrapiCore/content-manager/shared/contracts/components.ts +72 -0
  16. package/admin/src/components/StrapiCore/content-manager/shared/contracts/content-types.ts +116 -0
  17. package/admin/src/components/StrapiCore/content-manager/shared/contracts/index.ts +8 -0
  18. package/admin/src/components/StrapiCore/content-manager/shared/contracts/init.ts +22 -0
  19. package/admin/src/components/StrapiCore/content-manager/shared/contracts/relations.ts +80 -0
  20. package/admin/src/components/StrapiCore/content-manager/shared/contracts/review-workflows.ts +88 -0
  21. package/admin/src/components/StrapiCore/content-manager/shared/contracts/single-types.ts +112 -0
  22. package/admin/src/components/StrapiCore/content-manager/shared/contracts/uid.ts +48 -0
  23. package/admin/src/components/StrapiCore/content-manager/shared/index.ts +1 -0
  24. package/custom.d.ts +1 -0
  25. package/dist/package.json +1 -1
  26. package/dist/tsconfig.server.tsbuildinfo +1 -1
  27. package/package.json +1 -1
@@ -0,0 +1,253 @@
1
+ // @ts-nocheck
2
+ import * as React from 'react';
3
+
4
+ import {
5
+ useDrag,
6
+ useDrop,
7
+ type HandlerManager,
8
+ type ConnectDragSource,
9
+ type ConnectDropTarget,
10
+ type ConnectDragPreview,
11
+ type DragSourceMonitor
12
+ } from 'react-dnd';
13
+
14
+ import { useKeyboardDragAndDrop, type UseKeyboardDragAndDropCallbacks } from './useKeyboardDragAndDrop';
15
+
16
+ import type { Entity } from '@strapi/types';
17
+
18
+ const DIRECTIONS = {
19
+ UPWARD: 'upward',
20
+ DOWNWARD: 'downward'
21
+ } as const;
22
+
23
+ const DROP_SENSITIVITY = {
24
+ REGULAR: 'regular',
25
+ IMMEDIATE: 'immediate'
26
+ } as const;
27
+
28
+ interface UseDragAndDropOptions<
29
+ TIndex extends number | Array<number> = number,
30
+ TItem extends { index: TIndex } = { index: TIndex }
31
+ > extends UseKeyboardDragAndDropCallbacks<TIndex> {
32
+ type?: string;
33
+ index: TIndex;
34
+ item?: TItem;
35
+ onStart?: () => void;
36
+ onEnd?: () => void;
37
+ dropSensitivity?: (typeof DROP_SENSITIVITY)[keyof typeof DROP_SENSITIVITY];
38
+ }
39
+
40
+ type Identifier = ReturnType<HandlerManager['getHandlerId']>;
41
+
42
+ type UseDragAndDropReturn = [
43
+ props: {
44
+ handlerId: Identifier;
45
+ isDragging: boolean;
46
+ handleKeyDown: (event: React.KeyboardEvent<HTMLButtonElement>) => void;
47
+ isOverDropTarget: boolean;
48
+ direction: (typeof DIRECTIONS)[keyof typeof DIRECTIONS] | null;
49
+ },
50
+ objectRef: React.RefObject<HTMLElement>,
51
+ dropRef: ConnectDropTarget,
52
+ dragRef: ConnectDragSource,
53
+ dragPreviewRef: ConnectDragPreview
54
+ ];
55
+
56
+ type DropCollectedProps = {
57
+ handlerId: Identifier;
58
+ isOver: boolean;
59
+ };
60
+
61
+ /**
62
+ * A utility hook abstracting the general drag and drop hooks from react-dnd.
63
+ * Centralising the same behaviours and by default offering keyboard support.
64
+ */
65
+ const useDragAndDrop = <
66
+ TIndex extends number | Array<number>,
67
+ TItem extends { index: TIndex; id?: Entity.ID; [key: string]: unknown }
68
+ >(
69
+ active: boolean,
70
+ {
71
+ type = 'STRAPI_DND',
72
+ index,
73
+ item,
74
+ onStart,
75
+ onEnd,
76
+ onGrabItem,
77
+ onDropItem,
78
+ onCancel,
79
+ onMoveItem,
80
+ dropSensitivity = DROP_SENSITIVITY.REGULAR
81
+ }: UseDragAndDropOptions<TIndex, TItem>
82
+ ): UseDragAndDropReturn => {
83
+ const objectRef = React.useRef<HTMLElement>(null);
84
+
85
+ const [{ handlerId, isOver }, dropRef] = useDrop<TItem, void, DropCollectedProps>({
86
+ accept: type,
87
+ collect(monitor) {
88
+ return {
89
+ handlerId: monitor.getHandlerId(),
90
+ isOver: monitor.isOver({ shallow: true })
91
+ };
92
+ },
93
+ drop(item) {
94
+ const draggedIndex = item.index;
95
+ const newIndex = index;
96
+
97
+ if (isOver && onDropItem) {
98
+ onDropItem(draggedIndex, newIndex);
99
+ }
100
+ },
101
+ hover(item, monitor) {
102
+ if (!objectRef.current || !onMoveItem) {
103
+ return;
104
+ }
105
+
106
+ const dragIndex = item.index;
107
+ const newIndex = index;
108
+
109
+ const hoverBoundingRect = objectRef.current?.getBoundingClientRect();
110
+ const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
111
+ const clientOffset = monitor.getClientOffset();
112
+ if (!clientOffset) return;
113
+
114
+ const hoverClientY = clientOffset && clientOffset.y - hoverBoundingRect.top;
115
+ if (typeof dragIndex === 'number' && typeof newIndex === 'number') {
116
+ if (dragIndex === newIndex) {
117
+ // Don't replace items with themselves
118
+ return;
119
+ }
120
+
121
+ if (dropSensitivity === DROP_SENSITIVITY.REGULAR) {
122
+ // Dragging downwards
123
+ if (dragIndex < newIndex && hoverClientY < hoverMiddleY) {
124
+ return;
125
+ }
126
+
127
+ // Dragging upwards
128
+ if (dragIndex > newIndex && hoverClientY > hoverMiddleY) {
129
+ return;
130
+ }
131
+ }
132
+
133
+ // Time to actually perform the action
134
+ onMoveItem(newIndex, dragIndex);
135
+ item.index = newIndex;
136
+ } else {
137
+ // Using numbers as indices doesn't work for nested list items with path like [1, 1, 0]
138
+ if (Array.isArray(dragIndex) && Array.isArray(newIndex))
139
+ if (dropSensitivity === DROP_SENSITIVITY.REGULAR) {
140
+ // Indices comparison to find item position in nested list
141
+ const minLength = Math.min(dragIndex.length, newIndex.length);
142
+ let areEqual = true;
143
+ let isLessThan = false;
144
+ let isGreaterThan = false;
145
+
146
+ for (let i = 0; i < minLength; i++) {
147
+ if (dragIndex[i] < newIndex[i]) {
148
+ isLessThan = true;
149
+ areEqual = false;
150
+ break;
151
+ } else if (dragIndex[i] > newIndex[i]) {
152
+ isGreaterThan = true;
153
+ areEqual = false;
154
+ break;
155
+ }
156
+ }
157
+
158
+ // Don't replace items with themselves
159
+ if (areEqual && dragIndex.length === newIndex.length) {
160
+ return;
161
+ }
162
+ // Dragging downwards
163
+ if (isLessThan && !isGreaterThan && hoverClientY < hoverMiddleY) {
164
+ return;
165
+ }
166
+
167
+ // Dragging upwards
168
+ if (isGreaterThan && !isLessThan && hoverClientY > hoverMiddleY) {
169
+ return;
170
+ }
171
+ }
172
+ onMoveItem(newIndex, dragIndex);
173
+ item.index = newIndex;
174
+ }
175
+ }
176
+ });
177
+
178
+ const getDragDirection = (monitor: DragSourceMonitor<TItem, void>) => {
179
+ if (
180
+ monitor &&
181
+ monitor.isDragging() &&
182
+ !monitor.didDrop() &&
183
+ monitor.getInitialClientOffset() &&
184
+ monitor.getClientOffset()
185
+ ) {
186
+ const deltaY = monitor.getInitialClientOffset()!.y - monitor.getClientOffset()!.y;
187
+
188
+ if (deltaY > 0) return DIRECTIONS.UPWARD;
189
+
190
+ if (deltaY < 0) return DIRECTIONS.DOWNWARD;
191
+
192
+ return null;
193
+ }
194
+
195
+ return null;
196
+ };
197
+
198
+ const [{ isDragging, direction }, dragRef, dragPreviewRef] = useDrag({
199
+ type,
200
+ item() {
201
+ if (onStart) {
202
+ onStart();
203
+ }
204
+
205
+ /**
206
+ * This will be attached and it helps define the preview sizes
207
+ * when a component is flexy e.g. Relations
208
+ */
209
+ const { width } = objectRef.current?.getBoundingClientRect() ?? {};
210
+
211
+ return { index, width, ...item };
212
+ },
213
+ end() {
214
+ if (onEnd) {
215
+ onEnd();
216
+ }
217
+ },
218
+ canDrag: active,
219
+ /**
220
+ * This is useful when the item is in a virtualized list.
221
+ * However, if we don't have an ID then we want the libraries
222
+ * defaults to take care of this.
223
+ */
224
+ isDragging: item?.id
225
+ ? (monitor) => {
226
+ return item.id === monitor.getItem().id;
227
+ }
228
+ : undefined,
229
+ collect: (monitor) => ({
230
+ isDragging: monitor.isDragging(),
231
+ initialOffset: monitor.getInitialClientOffset(),
232
+ currentOffset: monitor.getClientOffset(),
233
+ direction: getDragDirection(monitor)
234
+ })
235
+ });
236
+
237
+ const handleKeyDown = useKeyboardDragAndDrop(active, index, {
238
+ onGrabItem,
239
+ onDropItem,
240
+ onCancel,
241
+ onMoveItem
242
+ });
243
+
244
+ return [
245
+ { handlerId, isDragging, handleKeyDown, isOverDropTarget: isOver, direction },
246
+ objectRef,
247
+ dropRef,
248
+ dragRef,
249
+ dragPreviewRef
250
+ ];
251
+ };
252
+
253
+ export { useDragAndDrop, UseDragAndDropReturn, UseDragAndDropOptions, 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,11 @@
1
+ import { useEffect, useRef } from 'react';
2
+
3
+ export const usePrev = <T>(value: T): T | undefined => {
4
+ const ref = useRef<T>();
5
+
6
+ useEffect(() => {
7
+ ref.current = value;
8
+ }, [value]);
9
+
10
+ return ref.current;
11
+ };
@@ -0,0 +1,8 @@
1
+ export const ItemTypes = {
2
+ COMPONENT: 'component',
3
+ EDIT_FIELD: 'editField',
4
+ FIELD: 'field',
5
+ DYNAMIC_ZONE: 'dynamicZone',
6
+ RELATION: 'relation',
7
+ BLOCKS: 'blocks',
8
+ } as const;
@@ -0,0 +1,7 @@
1
+ import type { Contracts } from '../../../../../content-manager/shared';
2
+
3
+ export type NormalizedRelation = Contracts.Relations.RelationResult & {
4
+ href?: string;
5
+ mainField: string;
6
+ publicationState?: false | 'published' | 'draft';
7
+ };
@@ -0,0 +1,36 @@
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>) =>
11
+ (currentPath: string) => {
12
+ const splitPath = currentPath.split('.');
13
+
14
+ return splitPath.reduce<string[]>((acc, currentValue, index) => {
15
+ const initialDataParent = get(initialData, acc);
16
+ const modifiedDataTempKey = get(modifiedData, [
17
+ ...splitPath.slice(0, index),
18
+ currentValue,
19
+ '__temp_key__',
20
+ ]);
21
+
22
+ if (Array.isArray(initialDataParent) && typeof modifiedDataTempKey === 'number') {
23
+ const initialDataIndex = initialDataParent.findIndex(
24
+ (entry) => entry.__temp_key__ === modifiedDataTempKey
25
+ );
26
+
27
+ acc.push(initialDataIndex.toString());
28
+
29
+ return acc;
30
+ }
31
+
32
+ acc.push(currentValue);
33
+
34
+ return acc;
35
+ }, []);
36
+ };
@@ -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
+ };
@@ -0,0 +1,3 @@
1
+ const getTranslation = (id: string) => `content-manager.${id}`;
2
+
3
+ export { getTranslation };