@proyecto-viviana/solid-stately 0.2.3 → 0.2.7
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/LICENSE +21 -0
- package/dist/autocomplete/createAutocompleteState.d.ts +2 -1
- package/dist/checkbox/createCheckboxGroupState.d.ts +10 -1
- package/dist/collections/types.d.ts +11 -0
- package/dist/color/getColorChannels.d.ts +20 -0
- package/dist/data/createAsyncList.d.ts +111 -0
- package/dist/data/createListData.d.ts +65 -0
- package/dist/data/createTreeData.d.ts +61 -0
- package/dist/data/index.d.ts +3 -0
- package/dist/datepicker/index.d.ts +10 -0
- package/dist/grid/types.d.ts +5 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +3737 -2697
- package/dist/index.js.map +1 -7
- package/dist/menu/index.d.ts +8 -0
- package/dist/radio/createRadioGroupState.d.ts +10 -1
- package/dist/select/createSelectState.d.ts +17 -0
- package/dist/selection/index.d.ts +11 -0
- package/dist/toast/createToastState.d.ts +7 -1
- package/dist/toggle/createToggleGroupState.d.ts +45 -0
- package/dist/toggle/index.d.ts +1 -0
- package/dist/tree/TreeCollection.d.ts +3 -2
- package/package.json +6 -5
- package/src/autocomplete/createAutocompleteState.ts +10 -11
- package/src/calendar/createDateFieldState.ts +24 -1
- package/src/checkbox/createCheckboxGroupState.ts +42 -6
- package/src/collections/ListCollection.ts +152 -146
- package/src/collections/createListState.ts +266 -264
- package/src/collections/createMenuState.ts +106 -106
- package/src/collections/createSelectionState.ts +336 -336
- package/src/collections/index.ts +46 -46
- package/src/collections/types.ts +181 -169
- package/src/color/Color.ts +951 -951
- package/src/color/createColorAreaState.ts +293 -293
- package/src/color/createColorFieldState.ts +292 -292
- package/src/color/createColorSliderState.ts +241 -241
- package/src/color/createColorWheelState.ts +211 -211
- package/src/color/getColorChannels.ts +34 -0
- package/src/color/index.ts +47 -47
- package/src/color/types.ts +127 -127
- package/src/combobox/createComboBoxState.ts +703 -703
- package/src/combobox/index.ts +13 -13
- package/src/data/createAsyncList.ts +377 -0
- package/src/data/createListData.ts +298 -0
- package/src/data/createTreeData.ts +433 -0
- package/src/data/index.ts +25 -0
- package/src/datepicker/index.ts +36 -0
- package/src/disclosure/createDisclosureState.ts +4 -4
- package/src/dnd/createDragState.ts +153 -153
- package/src/dnd/createDraggableCollectionState.ts +165 -165
- package/src/dnd/createDropState.ts +212 -212
- package/src/dnd/createDroppableCollectionState.ts +357 -357
- package/src/dnd/index.ts +76 -76
- package/src/dnd/types.ts +317 -317
- package/src/form/createFormValidationState.ts +389 -389
- package/src/form/index.ts +15 -15
- package/src/grid/types.ts +5 -0
- package/src/index.ts +49 -0
- package/src/menu/index.ts +19 -0
- package/src/numberfield/createNumberFieldState.ts +427 -383
- package/src/numberfield/index.ts +5 -5
- package/src/overlays/createOverlayTriggerState.ts +67 -67
- package/src/overlays/index.ts +5 -5
- package/src/radio/createRadioGroupState.ts +44 -6
- package/src/searchfield/createSearchFieldState.ts +62 -62
- package/src/searchfield/index.ts +5 -5
- package/src/select/createSelectState.ts +290 -181
- package/src/select/index.ts +5 -5
- package/src/selection/index.ts +28 -0
- package/src/slider/createSliderState.ts +211 -211
- package/src/slider/index.ts +6 -6
- package/src/tabs/createTabListState.ts +37 -11
- package/src/toast/createToastState.d.ts +6 -1
- package/src/toast/createToastState.ts +8 -1
- package/src/toggle/createToggleGroupState.ts +127 -0
- package/src/toggle/index.ts +6 -0
- package/src/tooltip/createTooltipTriggerState.ts +183 -183
- package/src/tooltip/index.ts +6 -6
- package/src/tree/TreeCollection.ts +208 -175
- package/src/tree/createTreeState.ts +392 -392
- package/src/tree/index.ts +13 -13
- package/src/tree/types.ts +174 -174
|
@@ -1,153 +1,153 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Drag state management for solid-stately.
|
|
3
|
-
*
|
|
4
|
-
* Provides reactive state for drag operations.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { createSignal, createMemo, type Accessor } from 'solid-js';
|
|
8
|
-
import type {
|
|
9
|
-
DragItem,
|
|
10
|
-
DragStartEvent,
|
|
11
|
-
DragMoveEvent,
|
|
12
|
-
DragEndEvent,
|
|
13
|
-
DropOperation,
|
|
14
|
-
DragPreviewRenderer,
|
|
15
|
-
} from './types';
|
|
16
|
-
|
|
17
|
-
export interface DragStateOptions {
|
|
18
|
-
/** A function that returns the items being dragged. */
|
|
19
|
-
getItems: () => DragItem[];
|
|
20
|
-
/** Function that returns the allowed drop operations. */
|
|
21
|
-
getAllowedDropOperations?: () => DropOperation[];
|
|
22
|
-
/** Handler that is called when a drag operation is started. */
|
|
23
|
-
onDragStart?: (e: DragStartEvent) => void;
|
|
24
|
-
/** Handler that is called when the drag is moved. */
|
|
25
|
-
onDragMove?: (e: DragMoveEvent) => void;
|
|
26
|
-
/** Handler that is called when the drag operation ends. */
|
|
27
|
-
onDragEnd?: (e: DragEndEvent) => void;
|
|
28
|
-
/** Whether the drag operation is disabled. */
|
|
29
|
-
isDisabled?: boolean;
|
|
30
|
-
/** Whether there is a separate drag button affordance. */
|
|
31
|
-
hasDragButton?: boolean;
|
|
32
|
-
/** Preview renderer function ref. */
|
|
33
|
-
preview?: { current: DragPreviewRenderer | null };
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface DragState {
|
|
37
|
-
/** Whether an element is currently being dragged. */
|
|
38
|
-
readonly isDragging: boolean;
|
|
39
|
-
/** Whether drag is disabled. */
|
|
40
|
-
readonly isDisabled: boolean;
|
|
41
|
-
/** Start a drag operation. */
|
|
42
|
-
startDrag(x: number, y: number): void;
|
|
43
|
-
/** Update drag position. */
|
|
44
|
-
moveDrag(x: number, y: number): void;
|
|
45
|
-
/** End a drag operation. */
|
|
46
|
-
endDrag(x: number, y: number, dropOperation: DropOperation): void;
|
|
47
|
-
/** Cancel a drag operation. */
|
|
48
|
-
cancelDrag(): void;
|
|
49
|
-
/** Get the items being dragged. */
|
|
50
|
-
getItems(): DragItem[];
|
|
51
|
-
/** Get allowed drop operations. */
|
|
52
|
-
getAllowedDropOperations(): DropOperation[];
|
|
53
|
-
/** Whether there is a drag button. */
|
|
54
|
-
readonly hasDragButton: boolean;
|
|
55
|
-
/** Preview renderer. */
|
|
56
|
-
readonly preview: { current: DragPreviewRenderer | null } | undefined;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Creates drag state for managing drag operations.
|
|
61
|
-
*
|
|
62
|
-
* @param props - Accessor returning drag state options
|
|
63
|
-
* @returns Drag state object
|
|
64
|
-
*/
|
|
65
|
-
export function createDragState(
|
|
66
|
-
props: Accessor<DragStateOptions>
|
|
67
|
-
): DragState {
|
|
68
|
-
const getProps = createMemo(() => props());
|
|
69
|
-
|
|
70
|
-
const [isDragging, setIsDragging] = createSignal(false);
|
|
71
|
-
|
|
72
|
-
const startDrag = (x: number, y: number) => {
|
|
73
|
-
const p = getProps();
|
|
74
|
-
if (p.isDisabled) return;
|
|
75
|
-
|
|
76
|
-
setIsDragging(true);
|
|
77
|
-
|
|
78
|
-
if (typeof p.onDragStart === 'function') {
|
|
79
|
-
p.onDragStart({
|
|
80
|
-
type: 'dragstart',
|
|
81
|
-
x,
|
|
82
|
-
y,
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const moveDrag = (x: number, y: number) => {
|
|
88
|
-
const p = getProps();
|
|
89
|
-
if (!isDragging() || p.isDisabled) return;
|
|
90
|
-
|
|
91
|
-
if (typeof p.onDragMove === 'function') {
|
|
92
|
-
p.onDragMove({
|
|
93
|
-
type: 'dragmove',
|
|
94
|
-
x,
|
|
95
|
-
y,
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const endDrag = (x: number, y: number, dropOperation: DropOperation) => {
|
|
101
|
-
const p = getProps();
|
|
102
|
-
if (!isDragging()) return;
|
|
103
|
-
|
|
104
|
-
setIsDragging(false);
|
|
105
|
-
|
|
106
|
-
if (typeof p.onDragEnd === 'function') {
|
|
107
|
-
p.onDragEnd({
|
|
108
|
-
type: 'dragend',
|
|
109
|
-
x,
|
|
110
|
-
y,
|
|
111
|
-
dropOperation,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const cancelDrag = () => {
|
|
117
|
-
endDrag(0, 0, 'cancel');
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const getItems = () => {
|
|
121
|
-
const p = getProps();
|
|
122
|
-
return p.getItems();
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
const getAllowedDropOperations = (): DropOperation[] => {
|
|
126
|
-
const p = getProps();
|
|
127
|
-
if (typeof p.getAllowedDropOperations === 'function') {
|
|
128
|
-
return p.getAllowedDropOperations();
|
|
129
|
-
}
|
|
130
|
-
return ['move', 'copy', 'link'];
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
return {
|
|
134
|
-
get isDragging() {
|
|
135
|
-
return isDragging();
|
|
136
|
-
},
|
|
137
|
-
get isDisabled() {
|
|
138
|
-
return getProps().isDisabled ?? false;
|
|
139
|
-
},
|
|
140
|
-
get hasDragButton() {
|
|
141
|
-
return getProps().hasDragButton ?? false;
|
|
142
|
-
},
|
|
143
|
-
get preview() {
|
|
144
|
-
return getProps().preview;
|
|
145
|
-
},
|
|
146
|
-
startDrag,
|
|
147
|
-
moveDrag,
|
|
148
|
-
endDrag,
|
|
149
|
-
cancelDrag,
|
|
150
|
-
getItems,
|
|
151
|
-
getAllowedDropOperations,
|
|
152
|
-
};
|
|
153
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Drag state management for solid-stately.
|
|
3
|
+
*
|
|
4
|
+
* Provides reactive state for drag operations.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createSignal, createMemo, type Accessor } from 'solid-js';
|
|
8
|
+
import type {
|
|
9
|
+
DragItem,
|
|
10
|
+
DragStartEvent,
|
|
11
|
+
DragMoveEvent,
|
|
12
|
+
DragEndEvent,
|
|
13
|
+
DropOperation,
|
|
14
|
+
DragPreviewRenderer,
|
|
15
|
+
} from './types';
|
|
16
|
+
|
|
17
|
+
export interface DragStateOptions {
|
|
18
|
+
/** A function that returns the items being dragged. */
|
|
19
|
+
getItems: () => DragItem[];
|
|
20
|
+
/** Function that returns the allowed drop operations. */
|
|
21
|
+
getAllowedDropOperations?: () => DropOperation[];
|
|
22
|
+
/** Handler that is called when a drag operation is started. */
|
|
23
|
+
onDragStart?: (e: DragStartEvent) => void;
|
|
24
|
+
/** Handler that is called when the drag is moved. */
|
|
25
|
+
onDragMove?: (e: DragMoveEvent) => void;
|
|
26
|
+
/** Handler that is called when the drag operation ends. */
|
|
27
|
+
onDragEnd?: (e: DragEndEvent) => void;
|
|
28
|
+
/** Whether the drag operation is disabled. */
|
|
29
|
+
isDisabled?: boolean;
|
|
30
|
+
/** Whether there is a separate drag button affordance. */
|
|
31
|
+
hasDragButton?: boolean;
|
|
32
|
+
/** Preview renderer function ref. */
|
|
33
|
+
preview?: { current: DragPreviewRenderer | null };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface DragState {
|
|
37
|
+
/** Whether an element is currently being dragged. */
|
|
38
|
+
readonly isDragging: boolean;
|
|
39
|
+
/** Whether drag is disabled. */
|
|
40
|
+
readonly isDisabled: boolean;
|
|
41
|
+
/** Start a drag operation. */
|
|
42
|
+
startDrag(x: number, y: number): void;
|
|
43
|
+
/** Update drag position. */
|
|
44
|
+
moveDrag(x: number, y: number): void;
|
|
45
|
+
/** End a drag operation. */
|
|
46
|
+
endDrag(x: number, y: number, dropOperation: DropOperation): void;
|
|
47
|
+
/** Cancel a drag operation. */
|
|
48
|
+
cancelDrag(): void;
|
|
49
|
+
/** Get the items being dragged. */
|
|
50
|
+
getItems(): DragItem[];
|
|
51
|
+
/** Get allowed drop operations. */
|
|
52
|
+
getAllowedDropOperations(): DropOperation[];
|
|
53
|
+
/** Whether there is a drag button. */
|
|
54
|
+
readonly hasDragButton: boolean;
|
|
55
|
+
/** Preview renderer. */
|
|
56
|
+
readonly preview: { current: DragPreviewRenderer | null } | undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Creates drag state for managing drag operations.
|
|
61
|
+
*
|
|
62
|
+
* @param props - Accessor returning drag state options
|
|
63
|
+
* @returns Drag state object
|
|
64
|
+
*/
|
|
65
|
+
export function createDragState(
|
|
66
|
+
props: Accessor<DragStateOptions>
|
|
67
|
+
): DragState {
|
|
68
|
+
const getProps = createMemo(() => props());
|
|
69
|
+
|
|
70
|
+
const [isDragging, setIsDragging] = createSignal(false);
|
|
71
|
+
|
|
72
|
+
const startDrag = (x: number, y: number) => {
|
|
73
|
+
const p = getProps();
|
|
74
|
+
if (p.isDisabled) return;
|
|
75
|
+
|
|
76
|
+
setIsDragging(true);
|
|
77
|
+
|
|
78
|
+
if (typeof p.onDragStart === 'function') {
|
|
79
|
+
p.onDragStart({
|
|
80
|
+
type: 'dragstart',
|
|
81
|
+
x,
|
|
82
|
+
y,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const moveDrag = (x: number, y: number) => {
|
|
88
|
+
const p = getProps();
|
|
89
|
+
if (!isDragging() || p.isDisabled) return;
|
|
90
|
+
|
|
91
|
+
if (typeof p.onDragMove === 'function') {
|
|
92
|
+
p.onDragMove({
|
|
93
|
+
type: 'dragmove',
|
|
94
|
+
x,
|
|
95
|
+
y,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const endDrag = (x: number, y: number, dropOperation: DropOperation) => {
|
|
101
|
+
const p = getProps();
|
|
102
|
+
if (!isDragging()) return;
|
|
103
|
+
|
|
104
|
+
setIsDragging(false);
|
|
105
|
+
|
|
106
|
+
if (typeof p.onDragEnd === 'function') {
|
|
107
|
+
p.onDragEnd({
|
|
108
|
+
type: 'dragend',
|
|
109
|
+
x,
|
|
110
|
+
y,
|
|
111
|
+
dropOperation,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const cancelDrag = () => {
|
|
117
|
+
endDrag(0, 0, 'cancel');
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const getItems = () => {
|
|
121
|
+
const p = getProps();
|
|
122
|
+
return p.getItems();
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const getAllowedDropOperations = (): DropOperation[] => {
|
|
126
|
+
const p = getProps();
|
|
127
|
+
if (typeof p.getAllowedDropOperations === 'function') {
|
|
128
|
+
return p.getAllowedDropOperations();
|
|
129
|
+
}
|
|
130
|
+
return ['move', 'copy', 'link'];
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
get isDragging() {
|
|
135
|
+
return isDragging();
|
|
136
|
+
},
|
|
137
|
+
get isDisabled() {
|
|
138
|
+
return getProps().isDisabled ?? false;
|
|
139
|
+
},
|
|
140
|
+
get hasDragButton() {
|
|
141
|
+
return getProps().hasDragButton ?? false;
|
|
142
|
+
},
|
|
143
|
+
get preview() {
|
|
144
|
+
return getProps().preview;
|
|
145
|
+
},
|
|
146
|
+
startDrag,
|
|
147
|
+
moveDrag,
|
|
148
|
+
endDrag,
|
|
149
|
+
cancelDrag,
|
|
150
|
+
getItems,
|
|
151
|
+
getAllowedDropOperations,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
@@ -1,165 +1,165 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Draggable collection state management for solid-stately.
|
|
3
|
-
*
|
|
4
|
-
* Provides reactive state for dragging items from a collection.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { createSignal, createMemo, type Accessor } from 'solid-js';
|
|
8
|
-
import type {
|
|
9
|
-
DragItem,
|
|
10
|
-
DraggableCollectionStartEvent,
|
|
11
|
-
DraggableCollectionMoveEvent,
|
|
12
|
-
DraggableCollectionEndEvent,
|
|
13
|
-
DropOperation,
|
|
14
|
-
DragPreviewRenderer,
|
|
15
|
-
} from './types';
|
|
16
|
-
|
|
17
|
-
export interface DraggableCollectionStateOptions<T = object> {
|
|
18
|
-
/** A function that returns the items being dragged. */
|
|
19
|
-
getItems: (keys: Set<string | number>) => DragItem[];
|
|
20
|
-
/** Function that returns the allowed drop operations. */
|
|
21
|
-
getAllowedDropOperations?: () => DropOperation[];
|
|
22
|
-
/** Handler that is called when a drag operation is started. */
|
|
23
|
-
onDragStart?: (e: DraggableCollectionStartEvent) => void;
|
|
24
|
-
/** Handler that is called when the drag is moved. */
|
|
25
|
-
onDragMove?: (e: DraggableCollectionMoveEvent) => void;
|
|
26
|
-
/** Handler that is called when the drag operation ends. */
|
|
27
|
-
onDragEnd?: (e: DraggableCollectionEndEvent) => void;
|
|
28
|
-
/** Whether the drag operation is disabled. */
|
|
29
|
-
isDisabled?: boolean;
|
|
30
|
-
/** Preview renderer function ref. */
|
|
31
|
-
preview?: { current: DragPreviewRenderer | null };
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface DraggableCollectionState {
|
|
35
|
-
/** Whether items are currently being dragged. */
|
|
36
|
-
readonly isDragging: boolean;
|
|
37
|
-
/** The keys of the items being dragged. */
|
|
38
|
-
readonly draggingKeys: Set<string | number>;
|
|
39
|
-
/** Whether dragging is disabled. */
|
|
40
|
-
readonly isDisabled: boolean;
|
|
41
|
-
/** Start a drag operation with the given keys. */
|
|
42
|
-
startDrag(keys: Set<string | number>, x: number, y: number): void;
|
|
43
|
-
/** Update drag position. */
|
|
44
|
-
moveDrag(x: number, y: number): void;
|
|
45
|
-
/** End a drag operation. */
|
|
46
|
-
endDrag(x: number, y: number, dropOperation: DropOperation, isInternal: boolean): void;
|
|
47
|
-
/** Cancel a drag operation. */
|
|
48
|
-
cancelDrag(): void;
|
|
49
|
-
/** Get the items being dragged for the given keys. */
|
|
50
|
-
getItems(keys: Set<string | number>): DragItem[];
|
|
51
|
-
/** Get allowed drop operations. */
|
|
52
|
-
getAllowedDropOperations(): DropOperation[];
|
|
53
|
-
/** Preview renderer. */
|
|
54
|
-
readonly preview: { current: DragPreviewRenderer | null } | undefined;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Creates state for dragging items from a collection.
|
|
59
|
-
*
|
|
60
|
-
* @param props - Accessor returning draggable collection options
|
|
61
|
-
* @returns Draggable collection state object
|
|
62
|
-
*/
|
|
63
|
-
export function createDraggableCollectionState<T = object>(
|
|
64
|
-
props: Accessor<DraggableCollectionStateOptions<T>>
|
|
65
|
-
): DraggableCollectionState {
|
|
66
|
-
const getProps = createMemo(() => props());
|
|
67
|
-
|
|
68
|
-
const [isDragging, setIsDragging] = createSignal(false);
|
|
69
|
-
const [draggingKeys, setDraggingKeys] = createSignal<Set<string | number>>(
|
|
70
|
-
new Set()
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
const startDrag = (keys: Set<string | number>, x: number, y: number) => {
|
|
74
|
-
const p = getProps();
|
|
75
|
-
if (p.isDisabled) return;
|
|
76
|
-
|
|
77
|
-
setIsDragging(true);
|
|
78
|
-
setDraggingKeys(keys);
|
|
79
|
-
|
|
80
|
-
if (typeof p.onDragStart === 'function') {
|
|
81
|
-
p.onDragStart({
|
|
82
|
-
type: 'dragstart',
|
|
83
|
-
x,
|
|
84
|
-
y,
|
|
85
|
-
keys,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
const moveDrag = (x: number, y: number) => {
|
|
91
|
-
const p = getProps();
|
|
92
|
-
if (!isDragging() || p.isDisabled) return;
|
|
93
|
-
|
|
94
|
-
if (typeof p.onDragMove === 'function') {
|
|
95
|
-
p.onDragMove({
|
|
96
|
-
type: 'dragmove',
|
|
97
|
-
x,
|
|
98
|
-
y,
|
|
99
|
-
keys: draggingKeys(),
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
const endDrag = (
|
|
105
|
-
x: number,
|
|
106
|
-
y: number,
|
|
107
|
-
dropOperation: DropOperation,
|
|
108
|
-
isInternal: boolean
|
|
109
|
-
) => {
|
|
110
|
-
const p = getProps();
|
|
111
|
-
const keys = draggingKeys();
|
|
112
|
-
|
|
113
|
-
setIsDragging(false);
|
|
114
|
-
setDraggingKeys(new Set<string | number>());
|
|
115
|
-
|
|
116
|
-
if (typeof p.onDragEnd === 'function') {
|
|
117
|
-
p.onDragEnd({
|
|
118
|
-
type: 'dragend',
|
|
119
|
-
x,
|
|
120
|
-
y,
|
|
121
|
-
dropOperation,
|
|
122
|
-
keys,
|
|
123
|
-
isInternal,
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
const cancelDrag = () => {
|
|
129
|
-
endDrag(0, 0, 'cancel', false);
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
const getItems = (keys: Set<string | number>) => {
|
|
133
|
-
const p = getProps();
|
|
134
|
-
return p.getItems(keys);
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
const getAllowedDropOperations = (): DropOperation[] => {
|
|
138
|
-
const p = getProps();
|
|
139
|
-
if (typeof p.getAllowedDropOperations === 'function') {
|
|
140
|
-
return p.getAllowedDropOperations();
|
|
141
|
-
}
|
|
142
|
-
return ['move', 'copy', 'link'];
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
return {
|
|
146
|
-
get isDragging() {
|
|
147
|
-
return isDragging();
|
|
148
|
-
},
|
|
149
|
-
get draggingKeys() {
|
|
150
|
-
return draggingKeys();
|
|
151
|
-
},
|
|
152
|
-
get isDisabled() {
|
|
153
|
-
return getProps().isDisabled ?? false;
|
|
154
|
-
},
|
|
155
|
-
get preview() {
|
|
156
|
-
return getProps().preview;
|
|
157
|
-
},
|
|
158
|
-
startDrag,
|
|
159
|
-
moveDrag,
|
|
160
|
-
endDrag,
|
|
161
|
-
cancelDrag,
|
|
162
|
-
getItems,
|
|
163
|
-
getAllowedDropOperations,
|
|
164
|
-
};
|
|
165
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Draggable collection state management for solid-stately.
|
|
3
|
+
*
|
|
4
|
+
* Provides reactive state for dragging items from a collection.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createSignal, createMemo, type Accessor } from 'solid-js';
|
|
8
|
+
import type {
|
|
9
|
+
DragItem,
|
|
10
|
+
DraggableCollectionStartEvent,
|
|
11
|
+
DraggableCollectionMoveEvent,
|
|
12
|
+
DraggableCollectionEndEvent,
|
|
13
|
+
DropOperation,
|
|
14
|
+
DragPreviewRenderer,
|
|
15
|
+
} from './types';
|
|
16
|
+
|
|
17
|
+
export interface DraggableCollectionStateOptions<T = object> {
|
|
18
|
+
/** A function that returns the items being dragged. */
|
|
19
|
+
getItems: (keys: Set<string | number>) => DragItem[];
|
|
20
|
+
/** Function that returns the allowed drop operations. */
|
|
21
|
+
getAllowedDropOperations?: () => DropOperation[];
|
|
22
|
+
/** Handler that is called when a drag operation is started. */
|
|
23
|
+
onDragStart?: (e: DraggableCollectionStartEvent) => void;
|
|
24
|
+
/** Handler that is called when the drag is moved. */
|
|
25
|
+
onDragMove?: (e: DraggableCollectionMoveEvent) => void;
|
|
26
|
+
/** Handler that is called when the drag operation ends. */
|
|
27
|
+
onDragEnd?: (e: DraggableCollectionEndEvent) => void;
|
|
28
|
+
/** Whether the drag operation is disabled. */
|
|
29
|
+
isDisabled?: boolean;
|
|
30
|
+
/** Preview renderer function ref. */
|
|
31
|
+
preview?: { current: DragPreviewRenderer | null };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface DraggableCollectionState {
|
|
35
|
+
/** Whether items are currently being dragged. */
|
|
36
|
+
readonly isDragging: boolean;
|
|
37
|
+
/** The keys of the items being dragged. */
|
|
38
|
+
readonly draggingKeys: Set<string | number>;
|
|
39
|
+
/** Whether dragging is disabled. */
|
|
40
|
+
readonly isDisabled: boolean;
|
|
41
|
+
/** Start a drag operation with the given keys. */
|
|
42
|
+
startDrag(keys: Set<string | number>, x: number, y: number): void;
|
|
43
|
+
/** Update drag position. */
|
|
44
|
+
moveDrag(x: number, y: number): void;
|
|
45
|
+
/** End a drag operation. */
|
|
46
|
+
endDrag(x: number, y: number, dropOperation: DropOperation, isInternal: boolean): void;
|
|
47
|
+
/** Cancel a drag operation. */
|
|
48
|
+
cancelDrag(): void;
|
|
49
|
+
/** Get the items being dragged for the given keys. */
|
|
50
|
+
getItems(keys: Set<string | number>): DragItem[];
|
|
51
|
+
/** Get allowed drop operations. */
|
|
52
|
+
getAllowedDropOperations(): DropOperation[];
|
|
53
|
+
/** Preview renderer. */
|
|
54
|
+
readonly preview: { current: DragPreviewRenderer | null } | undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Creates state for dragging items from a collection.
|
|
59
|
+
*
|
|
60
|
+
* @param props - Accessor returning draggable collection options
|
|
61
|
+
* @returns Draggable collection state object
|
|
62
|
+
*/
|
|
63
|
+
export function createDraggableCollectionState<T = object>(
|
|
64
|
+
props: Accessor<DraggableCollectionStateOptions<T>>
|
|
65
|
+
): DraggableCollectionState {
|
|
66
|
+
const getProps = createMemo(() => props());
|
|
67
|
+
|
|
68
|
+
const [isDragging, setIsDragging] = createSignal(false);
|
|
69
|
+
const [draggingKeys, setDraggingKeys] = createSignal<Set<string | number>>(
|
|
70
|
+
new Set()
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const startDrag = (keys: Set<string | number>, x: number, y: number) => {
|
|
74
|
+
const p = getProps();
|
|
75
|
+
if (p.isDisabled) return;
|
|
76
|
+
|
|
77
|
+
setIsDragging(true);
|
|
78
|
+
setDraggingKeys(keys);
|
|
79
|
+
|
|
80
|
+
if (typeof p.onDragStart === 'function') {
|
|
81
|
+
p.onDragStart({
|
|
82
|
+
type: 'dragstart',
|
|
83
|
+
x,
|
|
84
|
+
y,
|
|
85
|
+
keys,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const moveDrag = (x: number, y: number) => {
|
|
91
|
+
const p = getProps();
|
|
92
|
+
if (!isDragging() || p.isDisabled) return;
|
|
93
|
+
|
|
94
|
+
if (typeof p.onDragMove === 'function') {
|
|
95
|
+
p.onDragMove({
|
|
96
|
+
type: 'dragmove',
|
|
97
|
+
x,
|
|
98
|
+
y,
|
|
99
|
+
keys: draggingKeys(),
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const endDrag = (
|
|
105
|
+
x: number,
|
|
106
|
+
y: number,
|
|
107
|
+
dropOperation: DropOperation,
|
|
108
|
+
isInternal: boolean
|
|
109
|
+
) => {
|
|
110
|
+
const p = getProps();
|
|
111
|
+
const keys = draggingKeys();
|
|
112
|
+
|
|
113
|
+
setIsDragging(false);
|
|
114
|
+
setDraggingKeys(new Set<string | number>());
|
|
115
|
+
|
|
116
|
+
if (typeof p.onDragEnd === 'function') {
|
|
117
|
+
p.onDragEnd({
|
|
118
|
+
type: 'dragend',
|
|
119
|
+
x,
|
|
120
|
+
y,
|
|
121
|
+
dropOperation,
|
|
122
|
+
keys,
|
|
123
|
+
isInternal,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const cancelDrag = () => {
|
|
129
|
+
endDrag(0, 0, 'cancel', false);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const getItems = (keys: Set<string | number>) => {
|
|
133
|
+
const p = getProps();
|
|
134
|
+
return p.getItems(keys);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const getAllowedDropOperations = (): DropOperation[] => {
|
|
138
|
+
const p = getProps();
|
|
139
|
+
if (typeof p.getAllowedDropOperations === 'function') {
|
|
140
|
+
return p.getAllowedDropOperations();
|
|
141
|
+
}
|
|
142
|
+
return ['move', 'copy', 'link'];
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
get isDragging() {
|
|
147
|
+
return isDragging();
|
|
148
|
+
},
|
|
149
|
+
get draggingKeys() {
|
|
150
|
+
return draggingKeys();
|
|
151
|
+
},
|
|
152
|
+
get isDisabled() {
|
|
153
|
+
return getProps().isDisabled ?? false;
|
|
154
|
+
},
|
|
155
|
+
get preview() {
|
|
156
|
+
return getProps().preview;
|
|
157
|
+
},
|
|
158
|
+
startDrag,
|
|
159
|
+
moveDrag,
|
|
160
|
+
endDrag,
|
|
161
|
+
cancelDrag,
|
|
162
|
+
getItems,
|
|
163
|
+
getAllowedDropOperations,
|
|
164
|
+
};
|
|
165
|
+
}
|