@olenbetong/appframe-ds 1.0.3 → 1.2.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 +13 -0
- package/package.json +8 -7
- package/scripts/copyAssets.mjs +27 -55
- package/src/AfLookup/Lookup.css +129 -125
- package/src/AfLookup/Lookup.tsx +184 -162
- package/src/AfLookup/LookupCombobox.css +78 -78
- package/src/AfLookup/LookupCombobox.tsx +237 -237
- package/src/AfLookup/LookupEdit.tsx +130 -130
- package/src/AfLookup/LookupGrid.tsx +124 -124
- package/src/AfLookup/index.ts +5 -5
- package/src/autocomplete/Autocomplete.css +256 -256
- package/src/autocomplete/Autocomplete.tsx +320 -320
- package/src/autocomplete/AutocompleteFrame.tsx +168 -168
- package/src/autocomplete/Combobox.tsx +286 -286
- package/src/autocomplete/index.ts +4 -4
- package/src/autocomplete/types.ts +204 -204
- package/src/autocomplete/utils.ts +183 -183
- package/src/binding/BoundNumericTextField.tsx +102 -102
- package/src/binding/BoundTextField.tsx +49 -49
- package/src/binding/CancelButton.tsx +30 -30
- package/src/binding/CancelIconButton.tsx +30 -30
- package/src/binding/DataEditToolbar.tsx +92 -92
- package/src/binding/DeleteButton.tsx +35 -35
- package/src/binding/DeleteIconButton.tsx +35 -35
- package/src/binding/RefreshButton.tsx +31 -31
- package/src/binding/RefreshIconButton.tsx +33 -33
- package/src/binding/RefreshRowIconButton.tsx +33 -33
- package/src/binding/SaveButton.tsx +37 -37
- package/src/binding/SaveIconButton.tsx +38 -38
- package/src/container/Container.tsx +49 -49
- package/src/container/index.ts +1 -1
- package/src/cutoff/CutOffPicker.css +12 -0
- package/src/cutoff/CutOffPicker.tsx +165 -0
- package/src/cutoff/context.tsx +194 -0
- package/src/cutoff/dsCutOffDates.ts +21 -0
- package/src/cutoff/index.ts +4 -0
- package/src/cutoff/utils.test.ts +59 -0
- package/src/cutoff/utils.ts +276 -0
- package/src/filter/ChipListInput.tsx +79 -79
- package/src/filter/FieldFilterPanel.css +150 -150
- package/src/filter/FieldFilterPanel.tsx +309 -309
- package/src/filter/FilterBuilder.css +152 -152
- package/src/filter/FilterBuilder.test.tsx +153 -153
- package/src/filter/FilterBuilder.tsx +435 -435
- package/src/filter/FilterEditor.css +231 -231
- package/src/filter/FilterEditor.test.tsx +259 -259
- package/src/filter/FilterEditor.tsx +75 -74
- package/src/filter/FilterGroupEditor.tsx +160 -160
- package/src/filter/FilterNameDialog.css +22 -22
- package/src/filter/FilterNameDialog.tsx +81 -81
- package/src/filter/FilterRow.tsx +117 -117
- package/src/filter/FilterShareDialog.css +28 -28
- package/src/filter/FilterShareDialog.tsx +201 -201
- package/src/filter/FilterStringField.tsx +78 -77
- package/src/filter/FilterValueEditor.tsx +220 -220
- package/src/filter/SavedFilterTree.css +107 -107
- package/src/filter/SavedFilterTree.test.tsx +94 -94
- package/src/filter/SavedFilterTree.tsx +218 -222
- package/src/filter/fieldFilter.test.ts +158 -158
- package/src/filter/fieldFilter.ts +139 -139
- package/src/filter/index.ts +27 -27
- package/src/filter/types.ts +38 -38
- package/src/filter/useDistinctValues.test.ts +102 -102
- package/src/filter/useDistinctValues.ts +230 -230
- package/src/global.d.ts +11 -11
- package/src/grid/AfGridColumnsPanel.tsx +118 -118
- package/src/grid/AfGridContext.tsx +51 -51
- package/src/grid/AfGridError.tsx +46 -46
- package/src/grid/AfHeaderFilterCell.tsx +147 -151
- package/src/grid/AfHeaderFilterPanel.tsx +111 -111
- package/src/grid/GridEditLookup.css +12 -0
- package/src/grid/GridEditLookup.tsx +121 -0
- package/src/grid/Toolbar.tsx +233 -233
- package/src/grid/editing.ts +100 -100
- package/src/grid/filter.ts +78 -78
- package/src/grid/formatters.ts +48 -48
- package/src/grid/index.css +245 -245
- package/src/grid/index.tsx +489 -484
- package/src/grid/license.ts +48 -48
- package/src/grid/localization.ts +369 -369
- package/src/grid/slots/index.tsx +307 -307
- package/src/grid/slots/slots.css +64 -64
- package/src/grid/theme.tsx +138 -138
- package/src/grid/useAfColumns.tsx +371 -371
- package/src/grid/useAfCurrentIndex.ts +63 -63
- package/src/grid/useAfData.ts +20 -20
- package/src/grid/useAfFilter.ts +160 -160
- package/src/grid/useAfFilterFields.ts +55 -55
- package/src/grid/useAfGridApi.ts +71 -71
- package/src/grid/useAfKeyBindings.ts +36 -36
- package/src/grid/useAfNewItemRow.ts +258 -258
- package/src/grid/useAfPagination.ts +53 -53
- package/src/grid/useAfPersistedState.ts +230 -230
- package/src/grid/useAfRowEditModel.ts +145 -145
- package/src/grid/useAfRowGrouping.ts +47 -47
- package/src/grid/useAfServerAggregation.ts +145 -145
- package/src/grid/useAfSortModel.ts +84 -84
- package/src/i18n.ts +13 -0
- package/src/index.ts +14 -12
- package/src/input/InputAdornments.css +75 -75
- package/src/input/InputAdornments.tsx +102 -102
- package/src/input/index.ts +1 -1
- package/src/layout/AppMenu.tsx +74 -75
- package/src/layout/AppMenuDrawer.css +70 -70
- package/src/layout/AppMenuDrawer.tsx +122 -122
- package/src/layout/ErrorBoundary.tsx +162 -162
- package/src/layout/ResponsiveDialog.css +86 -0
- package/src/layout/ResponsiveDialog.tsx +55 -0
- package/src/layout/index.tsx +69 -68
- package/src/link/Link.tsx +23 -0
- package/src/link/index.ts +1 -0
- package/src/mdi/Mdi.css +138 -138
- package/src/mdi/Mdi.test.tsx +91 -87
- package/src/mdi/Mdi.tsx +141 -141
- package/src/mdi/MdiConfirmCloseDialog.tsx +47 -47
- package/src/mdi/MdiContext.tsx +59 -59
- package/src/mdi/MdiDocumentContext.tsx +60 -60
- package/src/mdi/MdiPanel.tsx +61 -61
- package/src/mdi/MdiRestore.test.tsx +86 -82
- package/src/mdi/MdiTab.tsx +30 -30
- package/src/mdi/controller.test.ts +382 -382
- package/src/mdi/controller.ts +656 -656
- package/src/mdi/dockviewHost.ts +132 -132
- package/src/mdi/fakeHost.ts +200 -200
- package/src/mdi/host.ts +44 -44
- package/src/mdi/ids.test.ts +20 -20
- package/src/mdi/ids.ts +18 -18
- package/src/mdi/index.ts +31 -31
- package/src/mdi/layoutRestore.test.ts +156 -156
- package/src/mdi/layoutRestore.ts +311 -311
- package/src/mdi/layoutSpec.test.ts +143 -143
- package/src/mdi/layoutSpec.ts +214 -214
- package/src/mdi/layoutStore.test.ts +93 -93
- package/src/mdi/layoutStore.ts +99 -99
- package/src/mdi/memorySettingsStore.ts +21 -21
- package/src/mdi/registry.ts +34 -34
- package/src/mdi/testTypes.ts +37 -37
- package/src/mdi/theme.ts +22 -22
- package/src/mdi/types.ts +214 -214
- package/src/page/Page.css +53 -53
- package/src/page/PageBlock.css +64 -64
- package/src/page/PageBlock.tsx +92 -92
- package/src/page/PageHeader.css +88 -88
- package/src/page/PageHeader.tsx +115 -116
- package/src/page/PageTitle.css +5 -0
- package/src/page/PageTitle.tsx +21 -0
- package/src/page/index.ts +4 -3
- package/src/paper/Paper.tsx +52 -52
- package/src/progress/LinearProgress.css +93 -93
- package/src/progress/LinearProgress.tsx +104 -104
- package/src/progress/index.ts +1 -1
- package/src/report-downloader/ReportDownloader.css +67 -67
- package/src/report-downloader/components/StatusItem.tsx +87 -86
- package/src/report-downloader/components/StatusList.tsx +28 -28
- package/src/report-downloader/index.ts +41 -41
- package/src/report-downloader/status.tsx +92 -92
- package/src/sortable/DropIndicator.css +51 -51
- package/src/sortable/DropIndicator.tsx +27 -27
- package/src/sortable/SortableItemList.css +21 -21
- package/src/sortable/SortableItemList.tsx +264 -264
- package/src/sortable/index.ts +8 -8
- package/src/sortable/reorderSortOrder.ts +126 -126
- package/src/sortable/sortOrder.ts +101 -101
- package/src/sortable/types.ts +9 -9
- package/src/sortable/useSortableContainer.ts +53 -53
- package/src/sortable/useSortableItem.ts +95 -95
- package/src/sortable/useSortableList.ts +117 -117
- package/src/test/happy-dom-document-class.ts +24 -24
- package/src/test/setup.ts +12 -12
- package/src/theme/useDsColorScheme.ts +53 -53
- package/tsconfig.build.json +9 -9
- package/tsconfig.json +8 -8
- package/vitest.config.ts +10 -10
|
@@ -1,126 +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
|
-
}
|
|
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
|
+
}
|
|
@@ -1,101 +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
|
-
}
|
|
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
|
+
}
|
package/src/sortable/types.ts
CHANGED
|
@@ -1,9 +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 };
|
|
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 };
|
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
import { monitorForElements } from "@atlaskit/pragmatic-drag-and-drop/adapter/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
|
-
}
|
|
1
|
+
import { monitorForElements } from "@atlaskit/pragmatic-drag-and-drop/adapter/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
|
+
}
|