@olenbetong/appframe-ds 0.6.0 → 0.7.0
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/CHANGELOG.md +10 -0
- package/package.json +8 -6
- package/scripts/copyAssets.mjs +6 -1
- package/src/AfLookup/Lookup.tsx +4 -4
- package/src/AfLookup/{Combobox.tsx → LookupCombobox.tsx} +8 -8
- package/src/AfLookup/LookupEdit.tsx +2 -2
- package/src/AfLookup/index.ts +1 -1
- package/src/autocomplete/Autocomplete.css +222 -0
- package/src/autocomplete/Autocomplete.tsx +316 -0
- package/src/autocomplete/AutocompleteFrame.tsx +149 -0
- package/src/autocomplete/Combobox.tsx +260 -0
- package/src/autocomplete/index.ts +4 -0
- package/src/autocomplete/types.ts +200 -0
- package/src/autocomplete/utils.ts +183 -0
- package/src/index.ts +2 -0
- package/src/sortable/DropIndicator.css +51 -0
- package/src/sortable/DropIndicator.tsx +27 -0
- package/src/sortable/SortableItemList.css +21 -0
- package/src/sortable/SortableItemList.tsx +264 -0
- package/src/sortable/index.ts +8 -0
- package/src/sortable/reorderSortOrder.ts +126 -0
- package/src/sortable/sortOrder.ts +101 -0
- package/src/sortable/types.ts +9 -0
- package/src/sortable/useSortableContainer.ts +53 -0
- package/src/sortable/useSortableItem.ts +95 -0
- package/src/sortable/useSortableList.ts +117 -0
- /package/src/AfLookup/{Combobox.css → LookupCombobox.css} +0 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import "./DropIndicator.css";
|
|
2
|
+
|
|
3
|
+
import type { Edge } from "@atlaskit/pragmatic-drag-and-drop-hitbox/types";
|
|
4
|
+
import clsx from "clsx";
|
|
5
|
+
import type { CSSProperties } from "react";
|
|
6
|
+
|
|
7
|
+
export type DropIndicatorProps = {
|
|
8
|
+
/** Which edge of the item the dragged item will be dropped on */
|
|
9
|
+
edge: Edge;
|
|
10
|
+
/** Distance from the edge, e.g. to account for a gap between items */
|
|
11
|
+
gap?: string;
|
|
12
|
+
className?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Line rendered on the edge of a list item to show where a dragged item will
|
|
17
|
+
* be placed. Must be rendered inside a positioned (`position: relative`)
|
|
18
|
+
* element, normally the list item itself.
|
|
19
|
+
*/
|
|
20
|
+
export function DropIndicator({ edge, gap, className }: DropIndicatorProps) {
|
|
21
|
+
return (
|
|
22
|
+
<div
|
|
23
|
+
className={clsx("ObDropIndicator-root", `ObDropIndicator-${edge}`, className)}
|
|
24
|
+
style={{ "--ob-drop-indicator-gap": gap ?? "0rem" } as CSSProperties}
|
|
25
|
+
/>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
.ObSortableItemList-item {
|
|
2
|
+
position: relative;
|
|
3
|
+
|
|
4
|
+
&.ObSortableItemList-dragging {
|
|
5
|
+
outline: 3px dashed;
|
|
6
|
+
outline-offset: -3px;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
.ObSortableItemList-handle {
|
|
11
|
+
cursor: grab;
|
|
12
|
+
flex-shrink: 0;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.ObSortableItemList-handle:active {
|
|
16
|
+
cursor: grabbing;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.ObSortableItemList-empty {
|
|
20
|
+
color: var(--ds-color-neutral-text-subtle, #666);
|
|
21
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import "./SortableItemList.css";
|
|
2
|
+
|
|
3
|
+
import { Button, Dropdown } from "@digdir/designsystemet-react";
|
|
4
|
+
import { DragVerticalIcon } from "@navikt/aksel-icons";
|
|
5
|
+
import { getLocalizedString } from "@olenbetong/appframe-core";
|
|
6
|
+
import clsx from "clsx";
|
|
7
|
+
import type React from "react";
|
|
8
|
+
import { createContext, useContext, useId, useRef } from "react";
|
|
9
|
+
|
|
10
|
+
import { ItemList } from "../list/ItemList.js";
|
|
11
|
+
import { Paper } from "../paper/Paper.js";
|
|
12
|
+
import { DropIndicator } from "./DropIndicator.js";
|
|
13
|
+
import type { SortableRecord } from "./types.js";
|
|
14
|
+
import { type SortableItemState, useSortableItem } from "./useSortableItem.js";
|
|
15
|
+
import { type SortableList, type SortableListOptions, useSortableList } from "./useSortableList.js";
|
|
16
|
+
|
|
17
|
+
export interface SortableItemContextValue<T extends SortableRecord = SortableRecord> {
|
|
18
|
+
record: T;
|
|
19
|
+
/** Index of the record in the sorted list */
|
|
20
|
+
index: number;
|
|
21
|
+
/** Number of records in the list */
|
|
22
|
+
count: number;
|
|
23
|
+
/** Records in the list, sorted by `SortOrder` */
|
|
24
|
+
records: T[];
|
|
25
|
+
/** Current drag state of the item */
|
|
26
|
+
state: SortableItemState;
|
|
27
|
+
/** Element that starts the drag. Attach with `<SortableItemList.DragHandle />` */
|
|
28
|
+
handleRef: React.RefObject<HTMLButtonElement | null>;
|
|
29
|
+
enabled: boolean;
|
|
30
|
+
moveItem: SortableList<T>["moveItem"];
|
|
31
|
+
/** Moves the item one step up, to the top, one step down or to the bottom */
|
|
32
|
+
moveTo: (position: "top" | "up" | "down" | "bottom") => Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const SortableItemContext = createContext<SortableItemContextValue | null>(null);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Gives access to the state of the surrounding sortable item, e.g. to
|
|
39
|
+
* suppress click handlers while the item is being dragged.
|
|
40
|
+
*/
|
|
41
|
+
export function useSortableItemContext<T extends SortableRecord = SortableRecord>(): SortableItemContextValue<T> {
|
|
42
|
+
let context = useContext(SortableItemContext);
|
|
43
|
+
if (!context) {
|
|
44
|
+
throw new Error("useSortableItemContext must be used inside a SortableItemList item");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return context as SortableItemContextValue<T>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type SortableDragHandleProps = Omit<React.ComponentProps<typeof Button>, "ref" | "icon"> & {
|
|
51
|
+
/** Accessible label for the handle */
|
|
52
|
+
label?: string;
|
|
53
|
+
/**
|
|
54
|
+
* Adds a menu with move up/down/top/bottom actions, so the list can be
|
|
55
|
+
* reordered without a pointer.
|
|
56
|
+
* @default true
|
|
57
|
+
*/
|
|
58
|
+
menu?: boolean;
|
|
59
|
+
children?: React.ReactNode;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Drag handle for a `SortableItemList` item. Dragging is only started from
|
|
64
|
+
* this element, so the rest of the item stays clickable. By default it also
|
|
65
|
+
* opens a menu with keyboard accessible move actions.
|
|
66
|
+
*/
|
|
67
|
+
export function SortableDragHandle({
|
|
68
|
+
label,
|
|
69
|
+
menu = true,
|
|
70
|
+
children,
|
|
71
|
+
className,
|
|
72
|
+
disabled,
|
|
73
|
+
...props
|
|
74
|
+
}: SortableDragHandleProps) {
|
|
75
|
+
let { handleRef, index, count, enabled, moveTo } = useSortableItemContext();
|
|
76
|
+
let dropdownId = useId();
|
|
77
|
+
let isDisabled = disabled || !enabled;
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<>
|
|
81
|
+
<Button
|
|
82
|
+
ref={handleRef}
|
|
83
|
+
icon
|
|
84
|
+
variant="tertiary"
|
|
85
|
+
data-color="neutral"
|
|
86
|
+
className={clsx("ObSortableItemList-handle", className)}
|
|
87
|
+
aria-label={label ?? getLocalizedString("Change order")}
|
|
88
|
+
disabled={isDisabled}
|
|
89
|
+
popoverTarget={menu && !isDisabled ? dropdownId : undefined}
|
|
90
|
+
{...props}
|
|
91
|
+
>
|
|
92
|
+
{children ?? <DragVerticalIcon aria-hidden />}
|
|
93
|
+
</Button>
|
|
94
|
+
{menu && !isDisabled && (
|
|
95
|
+
<Dropdown id={dropdownId}>
|
|
96
|
+
<Dropdown.List>
|
|
97
|
+
<Dropdown.Item>
|
|
98
|
+
<Dropdown.Button disabled={index === 0} onClick={() => moveTo("top")}>
|
|
99
|
+
{getLocalizedString("Move to top")}
|
|
100
|
+
</Dropdown.Button>
|
|
101
|
+
</Dropdown.Item>
|
|
102
|
+
<Dropdown.Item>
|
|
103
|
+
<Dropdown.Button disabled={index === 0} onClick={() => moveTo("up")}>
|
|
104
|
+
{getLocalizedString("Move up")}
|
|
105
|
+
</Dropdown.Button>
|
|
106
|
+
</Dropdown.Item>
|
|
107
|
+
<Dropdown.Item>
|
|
108
|
+
<Dropdown.Button disabled={index === count - 1} onClick={() => moveTo("down")}>
|
|
109
|
+
{getLocalizedString("Move down")}
|
|
110
|
+
</Dropdown.Button>
|
|
111
|
+
</Dropdown.Item>
|
|
112
|
+
<Dropdown.Item>
|
|
113
|
+
<Dropdown.Button disabled={index === count - 1} onClick={() => moveTo("bottom")}>
|
|
114
|
+
{getLocalizedString("Move to bottom")}
|
|
115
|
+
</Dropdown.Button>
|
|
116
|
+
</Dropdown.Item>
|
|
117
|
+
</Dropdown.List>
|
|
118
|
+
</Dropdown>
|
|
119
|
+
)}
|
|
120
|
+
</>
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function SortableItem<T extends SortableRecord>({
|
|
125
|
+
record,
|
|
126
|
+
index,
|
|
127
|
+
list,
|
|
128
|
+
className,
|
|
129
|
+
indicatorGap,
|
|
130
|
+
children,
|
|
131
|
+
}: {
|
|
132
|
+
record: T;
|
|
133
|
+
index: number;
|
|
134
|
+
list: SortableList<T>;
|
|
135
|
+
className?: string;
|
|
136
|
+
indicatorGap?: string;
|
|
137
|
+
children: (context: SortableItemContextValue<T>) => React.ReactNode;
|
|
138
|
+
}) {
|
|
139
|
+
let itemRef = useRef<HTMLElement>(null);
|
|
140
|
+
let handleRef = useRef<HTMLButtonElement>(null);
|
|
141
|
+
let { records, moveItem, enabled, type } = list;
|
|
142
|
+
let state = useSortableItem({ type, record, itemRef, handleRef, enabled });
|
|
143
|
+
|
|
144
|
+
async function moveTo(position: "top" | "up" | "down" | "bottom") {
|
|
145
|
+
let target =
|
|
146
|
+
position === "top"
|
|
147
|
+
? records[0]
|
|
148
|
+
: position === "up"
|
|
149
|
+
? records[index - 1]
|
|
150
|
+
: position === "down"
|
|
151
|
+
? records[index + 1]
|
|
152
|
+
: records[records.length - 1];
|
|
153
|
+
if (!target || target.PrimKey === record.PrimKey) return;
|
|
154
|
+
|
|
155
|
+
await moveItem(record.PrimKey, target.PrimKey, position === "top" || position === "up" ? null : "bottom");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let context: SortableItemContextValue<T> = {
|
|
159
|
+
record,
|
|
160
|
+
index,
|
|
161
|
+
count: records.length,
|
|
162
|
+
records,
|
|
163
|
+
state,
|
|
164
|
+
handleRef,
|
|
165
|
+
enabled,
|
|
166
|
+
moveItem,
|
|
167
|
+
moveTo,
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
return (
|
|
171
|
+
<SortableItemContext.Provider value={context as SortableItemContextValue}>
|
|
172
|
+
<ItemList.Item
|
|
173
|
+
ref={itemRef}
|
|
174
|
+
className={clsx(
|
|
175
|
+
"ObSortableItemList-item",
|
|
176
|
+
state.type === "is-dragging" && "ObSortableItemList-dragging",
|
|
177
|
+
className,
|
|
178
|
+
)}
|
|
179
|
+
>
|
|
180
|
+
{children(context)}
|
|
181
|
+
{state.type === "is-dragging-over" && state.edge && <DropIndicator edge={state.edge} gap={indicatorGap} />}
|
|
182
|
+
</ItemList.Item>
|
|
183
|
+
</SortableItemContext.Provider>
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export type SortableItemListProps<T extends SortableRecord> = SortableListOptions<T> & {
|
|
188
|
+
/** Renders the content of a single item. Include a `<SortableItemList.DragHandle />` to allow dragging */
|
|
189
|
+
children: (record: T, context: SortableItemContextValue<T>) => React.ReactNode;
|
|
190
|
+
/** Rendered instead of the items when the list is empty */
|
|
191
|
+
empty?: React.ReactNode;
|
|
192
|
+
/** Wraps the list in a `Paper` surface @default true */
|
|
193
|
+
paper?: boolean;
|
|
194
|
+
/** Renders a divider between items @default true */
|
|
195
|
+
dividers?: boolean;
|
|
196
|
+
className?: string;
|
|
197
|
+
/** Class name added to every item */
|
|
198
|
+
itemClassName?: string;
|
|
199
|
+
/** Distance between the drop indicator and the item edge, when items have a gap */
|
|
200
|
+
indicatorGap?: string;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
function SortableItemListComponent<T extends SortableRecord>({
|
|
204
|
+
children,
|
|
205
|
+
empty,
|
|
206
|
+
paper = true,
|
|
207
|
+
dividers = true,
|
|
208
|
+
className,
|
|
209
|
+
itemClassName,
|
|
210
|
+
indicatorGap,
|
|
211
|
+
...options
|
|
212
|
+
}: SortableItemListProps<T>) {
|
|
213
|
+
let list = useSortableList<T>(options);
|
|
214
|
+
|
|
215
|
+
let items = list.records.map((record, index) => (
|
|
216
|
+
<SortableItem
|
|
217
|
+
key={record.PrimKey}
|
|
218
|
+
record={record}
|
|
219
|
+
index={index}
|
|
220
|
+
list={list}
|
|
221
|
+
className={itemClassName}
|
|
222
|
+
indicatorGap={indicatorGap}
|
|
223
|
+
>
|
|
224
|
+
{(context) => children(record, context)}
|
|
225
|
+
</SortableItem>
|
|
226
|
+
));
|
|
227
|
+
|
|
228
|
+
let content = list.records.length === 0 && empty ? <li className="ObSortableItemList-empty">{empty}</li> : items;
|
|
229
|
+
|
|
230
|
+
let listElement = (
|
|
231
|
+
<ItemList dividers={dividers} className={paper ? undefined : className}>
|
|
232
|
+
{content}
|
|
233
|
+
</ItemList>
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
return paper ? <Paper className={className}>{listElement}</Paper> : listElement;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* `ItemList` bound to a data object, where the items can be reordered by
|
|
241
|
+
* dragging them or through the menu on the drag handle. The list is sorted by
|
|
242
|
+
* the `SortOrder` column, and dropping an item writes a new sort order between
|
|
243
|
+
* its new neighbours through the data object.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* ```jsx
|
|
247
|
+
* <SortableItemList
|
|
248
|
+
* dataObject={dsRelatedProducts}
|
|
249
|
+
* filter={(record) => record.ProductRef === productRef}
|
|
250
|
+
* empty={<Paragraph>No related products added yet.</Paragraph>}
|
|
251
|
+
* >
|
|
252
|
+
* {(record) => (
|
|
253
|
+
* <>
|
|
254
|
+
* <SortableItemList.DragHandle label="Change related product order" />
|
|
255
|
+
* <ItemList.ItemText primary={record.Title} secondary={record.Slug} />
|
|
256
|
+
* </>
|
|
257
|
+
* )}
|
|
258
|
+
* </SortableItemList>
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
export const SortableItemList = Object.assign(SortableItemListComponent, {
|
|
262
|
+
DragHandle: SortableDragHandle,
|
|
263
|
+
ItemText: ItemList.ItemText,
|
|
264
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./DropIndicator.js";
|
|
2
|
+
export * from "./SortableItemList.js";
|
|
3
|
+
export * from "./reorderSortOrder.js";
|
|
4
|
+
export * from "./sortOrder.js";
|
|
5
|
+
export * from "./types.js";
|
|
6
|
+
export * from "./useSortableContainer.js";
|
|
7
|
+
export * from "./useSortableItem.js";
|
|
8
|
+
export * from "./useSortableList.js";
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { Edge } from "@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge";
|
|
2
|
+
import type { DataObject } from "@olenbetong/appframe-data";
|
|
3
|
+
|
|
4
|
+
import { DEFAULT_SORT_ORDER_SPACING, ensureNonZeroSortOrder } from "./sortOrder.js";
|
|
5
|
+
import type { SortableRecord } from "./types.js";
|
|
6
|
+
|
|
7
|
+
function hasDuplicateSortOrders<T extends SortableRecord>(records: T[]) {
|
|
8
|
+
if (records.length < 2) return false;
|
|
9
|
+
let values = new Set(records.map((record) => record.SortOrder ?? 0));
|
|
10
|
+
return values.size !== records.length;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ReorderSortOrderOptions<T extends SortableRecord> {
|
|
14
|
+
/** Records in their current (sorted) order */
|
|
15
|
+
records: T[];
|
|
16
|
+
/** `PrimKey` of the record being moved */
|
|
17
|
+
sourcePrimKey: string;
|
|
18
|
+
/** `PrimKey` of the record it is dropped on */
|
|
19
|
+
targetPrimKey: string;
|
|
20
|
+
/** Which edge of the target it was dropped on. `"bottom"` places it after the target */
|
|
21
|
+
edge: Edge | null;
|
|
22
|
+
/** Spacing used when appending to the end of the list */
|
|
23
|
+
spacing?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Called when the list contains duplicate sort orders, which means there is
|
|
26
|
+
* no free decimal slot to move an item into. Typically executes a stored
|
|
27
|
+
* procedure that renumbers the list with even spacing.
|
|
28
|
+
*/
|
|
29
|
+
onNormalize?: () => Promise<void>;
|
|
30
|
+
/** Reloads the records after normalizing. Required for `onNormalize` to have an effect */
|
|
31
|
+
refreshRecords?: () => Promise<T[]>;
|
|
32
|
+
/** Data object holding the list records. Updated locally for an optimistic render */
|
|
33
|
+
listDataObject: DataObject<T>;
|
|
34
|
+
/** Data object used to persist the new sort order. Defaults to `listDataObject` */
|
|
35
|
+
updateDataObject: DataObject<any>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Moves a record to a new position by giving it a sort order between its new
|
|
40
|
+
* neighbours, normalizing the list first if there is no room left.
|
|
41
|
+
*/
|
|
42
|
+
export async function reorderSortOrder<T extends SortableRecord>({
|
|
43
|
+
records,
|
|
44
|
+
sourcePrimKey,
|
|
45
|
+
targetPrimKey,
|
|
46
|
+
edge,
|
|
47
|
+
spacing = DEFAULT_SORT_ORDER_SPACING,
|
|
48
|
+
onNormalize,
|
|
49
|
+
refreshRecords,
|
|
50
|
+
listDataObject,
|
|
51
|
+
updateDataObject,
|
|
52
|
+
}: ReorderSortOrderOptions<T>) {
|
|
53
|
+
let currentRecords = records;
|
|
54
|
+
if (onNormalize && hasDuplicateSortOrders(currentRecords)) {
|
|
55
|
+
await onNormalize();
|
|
56
|
+
if (refreshRecords) {
|
|
57
|
+
currentRecords = await refreshRecords();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let from = currentRecords.findIndex((record) => record.PrimKey === sourcePrimKey);
|
|
62
|
+
let to = currentRecords.findIndex((record) => record.PrimKey === targetPrimKey);
|
|
63
|
+
if (from < 0 || to < 0) return;
|
|
64
|
+
|
|
65
|
+
let targetIndex = to;
|
|
66
|
+
if (edge === "bottom") {
|
|
67
|
+
targetIndex += 1;
|
|
68
|
+
}
|
|
69
|
+
if (targetIndex === from) return;
|
|
70
|
+
|
|
71
|
+
let existingSortOrders = currentRecords
|
|
72
|
+
.filter((_, idx) => idx !== from)
|
|
73
|
+
.map((record) => (record.SortOrder ?? 0) as number);
|
|
74
|
+
|
|
75
|
+
let newSort = currentRecords[from].SortOrder ?? spacing;
|
|
76
|
+
let beforeSort: number | null = null;
|
|
77
|
+
let afterSort: number | null = null;
|
|
78
|
+
|
|
79
|
+
if (targetIndex === 0) {
|
|
80
|
+
afterSort = currentRecords[0]?.SortOrder ?? null;
|
|
81
|
+
newSort = afterSort == null ? spacing : afterSort / 2;
|
|
82
|
+
} else if (targetIndex >= currentRecords.length) {
|
|
83
|
+
beforeSort = currentRecords[currentRecords.length - 1]?.SortOrder ?? null;
|
|
84
|
+
newSort = beforeSort == null ? spacing : beforeSort + spacing;
|
|
85
|
+
} else {
|
|
86
|
+
beforeSort = currentRecords[targetIndex - 1].SortOrder ?? null;
|
|
87
|
+
afterSort = currentRecords[targetIndex].SortOrder ?? null;
|
|
88
|
+
let beforeValue = beforeSort ?? spacing;
|
|
89
|
+
let afterValue = afterSort ?? spacing;
|
|
90
|
+
newSort = (beforeValue + afterValue) / 2;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
newSort = ensureNonZeroSortOrder(newSort, {
|
|
94
|
+
before: beforeSort,
|
|
95
|
+
after: afterSort,
|
|
96
|
+
existing: existingSortOrders,
|
|
97
|
+
step: spacing,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
let primKey = currentRecords[from].PrimKey;
|
|
101
|
+
|
|
102
|
+
let applyLocalSortOrder = <TRecord extends SortableRecord>(
|
|
103
|
+
dataObject: DataObject<TRecord>,
|
|
104
|
+
updated: Partial<TRecord>,
|
|
105
|
+
) => {
|
|
106
|
+
if (!updated.PrimKey) return;
|
|
107
|
+
let index = dataObject.findIndex((record) => record.PrimKey === updated.PrimKey);
|
|
108
|
+
if (index < 0) return;
|
|
109
|
+
|
|
110
|
+
dataObject.updateRow(index, updated);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
let changes = { PrimKey: primKey, SortOrder: newSort } as Partial<T>;
|
|
114
|
+
applyLocalSortOrder(listDataObject, changes);
|
|
115
|
+
if (updateDataObject !== listDataObject) {
|
|
116
|
+
applyLocalSortOrder(updateDataObject, changes);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let updatedRow = await updateDataObject.dataHandler.update(changes);
|
|
120
|
+
if (updatedRow && typeof updatedRow === "object") {
|
|
121
|
+
applyLocalSortOrder(listDataObject, updatedRow);
|
|
122
|
+
if (updateDataObject !== listDataObject) {
|
|
123
|
+
applyLocalSortOrder(updateDataObject, updatedRow);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
export const DEFAULT_SORT_ORDER_SPACING = 128;
|
|
2
|
+
|
|
3
|
+
export interface EnsureSortOrderOptions {
|
|
4
|
+
before?: number | null;
|
|
5
|
+
after?: number | null;
|
|
6
|
+
existing?: number[];
|
|
7
|
+
step?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function sanitizeExisting(values: number[] | undefined): number[] {
|
|
11
|
+
if (!values?.length) {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return values.map((value) => (typeof value === "number" && Number.isFinite(value) ? value : 0)).sort((a, b) => a - b);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Zero is treated as "no sort order" by the database, so a computed sort order
|
|
20
|
+
* of exactly zero must be nudged to a nearby free value.
|
|
21
|
+
*
|
|
22
|
+
* @param candidate The computed sort order
|
|
23
|
+
* @param options Neighbouring and existing sort orders used to find a free slot
|
|
24
|
+
* @returns A non-zero sort order that keeps the intended position
|
|
25
|
+
*/
|
|
26
|
+
export function ensureNonZeroSortOrder(candidate: number, options: EnsureSortOrderOptions = {}): number {
|
|
27
|
+
if (candidate !== 0) {
|
|
28
|
+
return candidate;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let { before = null, after = null, existing, step = DEFAULT_SORT_ORDER_SPACING } = options;
|
|
32
|
+
let sanitizedExisting = sanitizeExisting(existing);
|
|
33
|
+
|
|
34
|
+
if (typeof before === "number" && before < 0) {
|
|
35
|
+
let adjusted = before / 2;
|
|
36
|
+
let attempts = 0;
|
|
37
|
+
while ((adjusted === 0 || sanitizedExisting.includes(adjusted)) && attempts < 10) {
|
|
38
|
+
adjusted /= 2;
|
|
39
|
+
attempts += 1;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (adjusted !== 0 && !sanitizedExisting.includes(adjusted)) {
|
|
43
|
+
return adjusted;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (before == null && typeof after === "number" && after > 0) {
|
|
48
|
+
let adjusted = -Math.abs(after) / 2;
|
|
49
|
+
let attempts = 0;
|
|
50
|
+
while ((adjusted === 0 || sanitizedExisting.includes(adjusted)) && attempts < 10) {
|
|
51
|
+
adjusted /= 2;
|
|
52
|
+
attempts += 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (adjusted !== 0 && !sanitizedExisting.includes(adjusted)) {
|
|
56
|
+
return adjusted;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let negativeValues = sanitizedExisting.filter((value) => value < 0);
|
|
61
|
+
if (negativeValues.length > 0) {
|
|
62
|
+
let adjusted = negativeValues[negativeValues.length - 1] / 2;
|
|
63
|
+
let attempts = 0;
|
|
64
|
+
while ((adjusted === 0 || sanitizedExisting.includes(adjusted)) && attempts < 10) {
|
|
65
|
+
adjusted /= 2;
|
|
66
|
+
attempts += 1;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (adjusted !== 0 && !sanitizedExisting.includes(adjusted)) {
|
|
70
|
+
return adjusted;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let minValue = sanitizedExisting.length ? sanitizedExisting[0] : 0;
|
|
75
|
+
let adjusted = minValue <= 0 ? minValue - step : -step;
|
|
76
|
+
while (adjusted === 0 || sanitizedExisting.includes(adjusted)) {
|
|
77
|
+
adjusted -= step;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return adjusted;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Computes the sort order to use for a new item appended to the end of a list.
|
|
85
|
+
* The next sort order is the highest existing sort order plus the default
|
|
86
|
+
* spacing, or the default spacing if the list is empty. The result is
|
|
87
|
+
* guaranteed to be non-zero, since zero is used as "no sort order" in the
|
|
88
|
+
* database.
|
|
89
|
+
*
|
|
90
|
+
* @param existingSortOrders Sort orders of the items already in the list
|
|
91
|
+
* @returns The sort order to assign to the new item
|
|
92
|
+
*/
|
|
93
|
+
export function getNextSortOrder(existingSortOrders: (number | null | undefined)[]): number {
|
|
94
|
+
let sortOrders = existingSortOrders
|
|
95
|
+
.map((value) => (typeof value === "number" && Number.isFinite(value) ? value : 0))
|
|
96
|
+
.sort((a, b) => a - b);
|
|
97
|
+
let beforeSort = sortOrders.length > 0 ? sortOrders[sortOrders.length - 1] : null;
|
|
98
|
+
let newSort = beforeSort == null ? DEFAULT_SORT_ORDER_SPACING : beforeSort + DEFAULT_SORT_ORDER_SPACING;
|
|
99
|
+
|
|
100
|
+
return ensureNonZeroSortOrder(newSort, { after: beforeSort, existing: sortOrders });
|
|
101
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimum shape a record must have to take part in a sortable list.
|
|
3
|
+
*
|
|
4
|
+
* `PrimKey` is the `uniqueidentifier` primary key every Appframe table has,
|
|
5
|
+
* and `SortOrder` is a `decimal(18, 6)` column. Because the sort order is a
|
|
6
|
+
* decimal, items can be moved by placing them halfway between their new
|
|
7
|
+
* neighbours instead of renumbering the whole list.
|
|
8
|
+
*/
|
|
9
|
+
export type SortableRecord = { PrimKey: string; SortOrder?: number | null };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { monitorForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
|
|
2
|
+
import { type Edge, extractClosestEdge } from "@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge";
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
|
+
|
|
5
|
+
import type { SortableRecord } from "./types.js";
|
|
6
|
+
|
|
7
|
+
export type SortableDragData<T extends SortableRecord> = { type: string; record: T };
|
|
8
|
+
|
|
9
|
+
export function isSortableDragData<T extends SortableRecord>(value: any, type: string): value is SortableDragData<T> {
|
|
10
|
+
return value?.type === type;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface UseSortableContainerOptions<T extends SortableRecord> {
|
|
14
|
+
/** Identifies the list. Only items sharing the same type can be dropped on each other */
|
|
15
|
+
type: string;
|
|
16
|
+
enabled?: boolean;
|
|
17
|
+
onDrop: (args: { source: T; target: T; edge: Edge | null }) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Monitors drops for a sortable list and reports which record was dropped
|
|
22
|
+
* where. Use together with {@link useSortableItem}.
|
|
23
|
+
*/
|
|
24
|
+
export function useSortableContainer<T extends SortableRecord>({
|
|
25
|
+
type,
|
|
26
|
+
enabled = true,
|
|
27
|
+
onDrop,
|
|
28
|
+
}: UseSortableContainerOptions<T>) {
|
|
29
|
+
let onDropRef = useRef(onDrop);
|
|
30
|
+
onDropRef.current = onDrop;
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (!enabled) return;
|
|
34
|
+
|
|
35
|
+
return monitorForElements({
|
|
36
|
+
canMonitor({ source }) {
|
|
37
|
+
return isSortableDragData<T>(source.data, type);
|
|
38
|
+
},
|
|
39
|
+
onDrop({ location, source }) {
|
|
40
|
+
let target = location.current.dropTargets[0];
|
|
41
|
+
if (!target) return;
|
|
42
|
+
|
|
43
|
+
let sourceData = source.data;
|
|
44
|
+
let targetData = target.data;
|
|
45
|
+
|
|
46
|
+
if (isSortableDragData<T>(sourceData, type) && isSortableDragData<T>(targetData, type)) {
|
|
47
|
+
let edge = extractClosestEdge(targetData);
|
|
48
|
+
onDropRef.current({ source: sourceData.record, target: targetData.record, edge });
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
}, [enabled, type]);
|
|
53
|
+
}
|