@haiyue/ui 0.3.0 → 0.4.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 +17 -0
- package/README.md +84 -2
- package/dist/adapters/web/controls.js +48 -8
- package/dist/adapters/web/renderer.js +8 -3
- package/dist/anchor.d.ts +2 -0
- package/dist/anchor.js +23 -4
- package/dist/border-beam.js +2 -1
- package/dist/cascader.js +7 -1
- package/dist/context-menu.js +0 -1
- package/dist/core/actions.js +7 -8
- package/dist/core/data.d.ts +1 -1
- package/dist/core/data.js +44 -10
- package/dist/core/platform-view.js +8 -2
- package/dist/core/selection.js +4 -4
- package/dist/core/semantics.d.ts +26 -0
- package/dist/core/semantics.js +8 -0
- package/dist/core/text.js +3 -3
- package/dist/core/validation.d.ts +3 -1
- package/dist/core/validation.js +23 -5
- package/dist/date-picker.js +6 -0
- package/dist/dialog.d.ts +4 -0
- package/dist/dialog.js +37 -16
- package/dist/drag-source.d.ts +27 -0
- package/dist/drag-source.js +114 -0
- package/dist/draggable.d.ts +1 -2
- package/dist/draggable.js +25 -31
- package/dist/drawer.js +4 -12
- package/dist/dropdown.js +0 -1
- package/dist/dropzone.d.ts +29 -0
- package/dist/dropzone.js +89 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +9 -0
- package/dist/input.js +3 -3
- package/dist/internal/drag-drop.d.ts +29 -0
- package/dist/internal/drag-drop.js +171 -0
- package/dist/internal/focus.d.ts +2 -0
- package/dist/internal/focus.js +31 -0
- package/dist/internal/keyboard.d.ts +2 -0
- package/dist/internal/keyboard.js +12 -0
- package/dist/internal/native-drop.d.ts +17 -0
- package/dist/internal/native-drop.js +62 -0
- package/dist/internal/numbers.d.ts +2 -0
- package/dist/internal/numbers.js +8 -0
- package/dist/internal/pointer-drag.d.ts +15 -0
- package/dist/internal/pointer-drag.js +100 -0
- package/dist/modal.js +1 -30
- package/dist/notification.js +2 -1
- package/dist/portable/page.js +25 -14
- package/dist/radio.js +1 -1
- package/dist/resizable.d.ts +1 -1
- package/dist/resizable.js +26 -27
- package/dist/sortable.d.ts +62 -0
- package/dist/sortable.js +363 -0
- package/dist/table.d.ts +3 -1
- package/dist/table.js +65 -42
- package/dist/time-picker.js +6 -0
- package/dist/toast.js +3 -2
- package/dist/tooltip.d.ts +6 -0
- package/dist/tooltip.js +103 -2
- package/dist/tour.js +15 -6
- package/dist/tree-model.d.ts +2 -0
- package/dist/tree-model.js +32 -11
- package/dist/tree.d.ts +0 -2
- package/dist/tree.js +8 -24
- package/dist/upload.d.ts +2 -5
- package/dist/upload.js +19 -25
- package/dist/virtual-keyboard.d.ts +1 -0
- package/dist/virtual-keyboard.js +67 -36
- package/dist/virtual-list.d.ts +1 -0
- package/dist/virtual-list.js +21 -4
- package/dist/virtual-mouse.js +2 -1
- package/dist/watermark.js +2 -1
- package/package.json +33 -6
package/dist/portable/page.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { HYUIError, hyValidateCapabilities, hyValidateNode, hyValidateProps, snapshotValidatedProps } from '../core/validation.js';
|
|
2
|
-
import { equalData
|
|
1
|
+
import { HYUIError, hyValidateCapabilities, hyValidateNode, hyValidateProps, snapshotValidatedProps, snapshotProtocolData } from '../core/validation.js';
|
|
2
|
+
import { equalData } from '../core/data.js';
|
|
3
|
+
import { componentInteraction } from '../core/semantics.js';
|
|
3
4
|
import { diff, flatten } from './tree.js';
|
|
4
|
-
const interactive = new Set(['Button', 'Toggle', 'Select', 'Tabs', 'TextInput', 'TextArea', 'Slider', 'PlatformView']);
|
|
5
5
|
/** A synchronous, bounded page reconciler. It owns bindings/rendering, never the application store. */
|
|
6
6
|
export class HYPage {
|
|
7
7
|
options;
|
|
@@ -21,13 +21,15 @@ export class HYPage {
|
|
|
21
21
|
#subscriptions = new Set();
|
|
22
22
|
#focused;
|
|
23
23
|
#boundStores = new Set();
|
|
24
|
+
#frames = [];
|
|
24
25
|
#openers = new Map();
|
|
25
26
|
#cancelTransaction = 0;
|
|
26
27
|
constructor(renderer, options = {}) {
|
|
27
28
|
this.options = options;
|
|
28
|
-
|
|
29
|
+
const capabilities = snapshotProtocolData(renderer.capabilities, { property: 'capabilities' });
|
|
30
|
+
hyValidateCapabilities(capabilities);
|
|
29
31
|
this.#renderer = renderer;
|
|
30
|
-
this.#capabilities =
|
|
32
|
+
this.#capabilities = capabilities;
|
|
31
33
|
}
|
|
32
34
|
get status() { return this.#status; }
|
|
33
35
|
get revision() { return this.#revision; }
|
|
@@ -73,9 +75,12 @@ export class HYPage {
|
|
|
73
75
|
throw new HYUIError('invalid-props', 'Binding target missing or kind changed', { key });
|
|
74
76
|
const context = { key, ...(entry.node.source ? { source: entry.node.source } : {}) };
|
|
75
77
|
const accepted = snapshotValidatedProps(kind, props, context, this.#capabilities);
|
|
76
|
-
if (equalData(entry.node.props,
|
|
78
|
+
if (equalData(entry.node.props, accepted))
|
|
77
79
|
return;
|
|
78
|
-
const
|
|
80
|
+
const frame = this.#frames.at(-1);
|
|
81
|
+
const next = frame?.draft === this.#desired ? frame.draft : new Map(this.#desired);
|
|
82
|
+
if (frame)
|
|
83
|
+
frame.draft = next;
|
|
79
84
|
next.set(key, { ...entry, node: Object.freeze({ ...entry.node, props: accepted }) });
|
|
80
85
|
this.#desired = next;
|
|
81
86
|
this.#updates?.add(key);
|
|
@@ -85,6 +90,7 @@ export class HYPage {
|
|
|
85
90
|
this.#assertMutable();
|
|
86
91
|
const before = this.#desired, updates = this.#updates && new Set(this.#updates);
|
|
87
92
|
this.#depth++;
|
|
93
|
+
this.#frames.push({});
|
|
88
94
|
try {
|
|
89
95
|
const result = run();
|
|
90
96
|
if (result && typeof result.then === 'function')
|
|
@@ -97,7 +103,11 @@ export class HYPage {
|
|
|
97
103
|
}
|
|
98
104
|
finally {
|
|
99
105
|
this.#depth--;
|
|
106
|
+
this.#frames.pop();
|
|
100
107
|
}
|
|
108
|
+
const parent = this.#frames.at(-1);
|
|
109
|
+
if (parent && this.#desired !== before)
|
|
110
|
+
parent.draft = this.#desired;
|
|
101
111
|
this.#flush();
|
|
102
112
|
}
|
|
103
113
|
/** One store subscription: a store transaction produces one final page batch across all bindings. */
|
|
@@ -108,7 +118,7 @@ export class HYPage {
|
|
|
108
118
|
const definitions = [...bindings];
|
|
109
119
|
if (new Set(definitions.map(b => b.key)).size !== definitions.length)
|
|
110
120
|
throw new HYUIError('duplicate-key', 'Duplicate binding target');
|
|
111
|
-
const select = (state) => definitions.map(b => b.select(state));
|
|
121
|
+
const select = (state) => definitions.map(b => snapshotValidatedProps(b.kind, b.select(state), { key: b.key }, this.#capabilities));
|
|
112
122
|
const equal = (a, b) => a.every((p, i) => (definitions[i].equal ?? equalData)(p, b[i]));
|
|
113
123
|
let applied = [];
|
|
114
124
|
const apply = (props) => {
|
|
@@ -119,7 +129,7 @@ export class HYPage {
|
|
|
119
129
|
// A grouped subscription does not make every binding dirty. Reuse a
|
|
120
130
|
// previously validated result only while its target is still unchanged.
|
|
121
131
|
// External render/setProps and failed transactions invalidate this path.
|
|
122
|
-
if (node === applied[i] && node && equalData(node.props, props[i]))
|
|
132
|
+
if (node === applied[i] && node && (binding.equal ?? equalData)(node.props, props[i]))
|
|
123
133
|
return;
|
|
124
134
|
this.setProps(binding.key, binding.kind, props[i]);
|
|
125
135
|
}));
|
|
@@ -217,7 +227,7 @@ export class HYPage {
|
|
|
217
227
|
}
|
|
218
228
|
#inside(key, parent) { return key === parent || key.startsWith(`${parent}/`); }
|
|
219
229
|
#dialogTarget(key) {
|
|
220
|
-
return [...this.#rendered].find(([id, entry]) => this.#inside(id, key) &&
|
|
230
|
+
return [...this.#rendered].find(([id, entry]) => this.#inside(id, key) && componentInteraction[entry.node.kind] && this.#available(id))?.[0] ?? key;
|
|
221
231
|
}
|
|
222
232
|
#restoreFocus(previous) {
|
|
223
233
|
const oldDialogs = this.#dialogs(previous), dialogs = this.#dialogs(this.#rendered);
|
|
@@ -309,17 +319,18 @@ export class HYPage {
|
|
|
309
319
|
throw new HYUIError('lifecycle', 'Only a faulted page may rebuild');
|
|
310
320
|
if (renderer === this.#renderer)
|
|
311
321
|
throw new HYUIError('lifecycle', 'Rebuild needs a fresh renderer');
|
|
312
|
-
|
|
322
|
+
const capabilities = snapshotProtocolData(renderer.capabilities, { property: 'capabilities' });
|
|
323
|
+
hyValidateCapabilities(capabilities);
|
|
313
324
|
for (const [key, entry] of this.#desired) {
|
|
314
|
-
hyValidateProps(entry.node.kind, entry.node.props, { key },
|
|
325
|
+
hyValidateProps(entry.node.kind, entry.node.props, { key }, capabilities);
|
|
315
326
|
for (const feature of entry.node.requires ?? [])
|
|
316
|
-
if (!
|
|
327
|
+
if (!capabilities.features.includes(feature))
|
|
317
328
|
throw new HYUIError('capability', `Missing capability ${feature}`, { key });
|
|
318
329
|
}
|
|
319
330
|
++this.#epoch;
|
|
320
331
|
this.#renderer.dispose();
|
|
321
332
|
this.#renderer = renderer;
|
|
322
|
-
this.#capabilities =
|
|
333
|
+
this.#capabilities = capabilities;
|
|
323
334
|
this.#rendered = new Map();
|
|
324
335
|
this.#updates = undefined;
|
|
325
336
|
this.#order.clear();
|
package/dist/radio.js
CHANGED
|
@@ -199,7 +199,7 @@ export class HYRadio extends HTMLElement {
|
|
|
199
199
|
? root.querySelectorAll(`hy-radio[name="${cssEscape(this.name)}"]`)
|
|
200
200
|
: [];
|
|
201
201
|
for (const peer of peers) {
|
|
202
|
-
if (peer === this || peer.
|
|
202
|
+
if (peer === this || peer.form !== this.form)
|
|
203
203
|
continue;
|
|
204
204
|
peer.removeAttribute('checked');
|
|
205
205
|
}
|
package/dist/resizable.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ export declare class HYResizable extends HTMLElement {
|
|
|
32
32
|
private _origin;
|
|
33
33
|
private _pointerX;
|
|
34
34
|
private _pointerY;
|
|
35
|
-
private
|
|
35
|
+
private readonly _pointer;
|
|
36
36
|
static get observedAttributes(): string[];
|
|
37
37
|
constructor();
|
|
38
38
|
connectedCallback(): void;
|
package/dist/resizable.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isKeyboardControlEvent } from './internal/keyboard.js';
|
|
2
|
+
import { HYPointerDrag } from './internal/pointer-drag.js';
|
|
1
3
|
const HANDLES = ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw'];
|
|
2
4
|
const DEFAULT_CANCEL = 'input, textarea, select, button, a, [contenteditable="true"], [data-no-move]';
|
|
3
5
|
function finiteNumber(value, fallback) {
|
|
@@ -34,7 +36,7 @@ export class HYResizable extends HTMLElement {
|
|
|
34
36
|
_origin = { x: 0, y: 0, width: 200, height: 120 };
|
|
35
37
|
_pointerX = 0;
|
|
36
38
|
_pointerY = 0;
|
|
37
|
-
|
|
39
|
+
_pointer = new HYPointerDrag();
|
|
38
40
|
static get observedAttributes() {
|
|
39
41
|
return [
|
|
40
42
|
'x', 'y', 'width', 'height', 'min-width', 'min-height', 'max-width', 'max-height',
|
|
@@ -209,26 +211,25 @@ export class HYResizable extends HTMLElement {
|
|
|
209
211
|
const handle = this._resolveHandle(event);
|
|
210
212
|
if (handle === 'move' && (!this.movable || this._isCancelledTarget(event)))
|
|
211
213
|
return;
|
|
212
|
-
|
|
213
|
-
|
|
214
|
+
this._pointer.start(event, this._frame, {
|
|
215
|
+
start: () => {
|
|
216
|
+
const start = new CustomEvent('resize-start', {
|
|
217
|
+
detail: { ...this.rect, handle, source: 'pointer' }, bubbles: true, composed: true, cancelable: true,
|
|
218
|
+
});
|
|
219
|
+
if (!this.dispatchEvent(start) || this.disabled || !this.isConnected)
|
|
220
|
+
return false;
|
|
221
|
+
this._frame.focus({ preventScroll: true });
|
|
222
|
+
this._pointerId = event.pointerId;
|
|
223
|
+
this._activeHandle = handle;
|
|
224
|
+
this._origin = this.rect;
|
|
225
|
+
this._pointerX = event.clientX;
|
|
226
|
+
this._pointerY = event.clientY;
|
|
227
|
+
this.toggleAttribute('resizing', true);
|
|
228
|
+
return true;
|
|
229
|
+
},
|
|
230
|
+
move: move => this._handlePointerMove(move),
|
|
231
|
+
end: (up, cancelled) => this._finishPointer(up.pointerId, cancelled),
|
|
214
232
|
});
|
|
215
|
-
if (!this.dispatchEvent(start))
|
|
216
|
-
return;
|
|
217
|
-
event.preventDefault();
|
|
218
|
-
this._frame.focus({ preventScroll: true });
|
|
219
|
-
this._frame.setPointerCapture(event.pointerId);
|
|
220
|
-
this._pointerId = event.pointerId;
|
|
221
|
-
this._activeHandle = handle;
|
|
222
|
-
this._origin = this.rect;
|
|
223
|
-
this._pointerX = event.clientX;
|
|
224
|
-
this._pointerY = event.clientY;
|
|
225
|
-
this.toggleAttribute('resizing', true);
|
|
226
|
-
const abort = new AbortController();
|
|
227
|
-
this._dragAbort = abort;
|
|
228
|
-
this._frame.addEventListener('pointermove', move => this._handlePointerMove(move), { signal: abort.signal });
|
|
229
|
-
this._frame.addEventListener('pointerup', up => this._finishPointer(up.pointerId, false), { signal: abort.signal });
|
|
230
|
-
this._frame.addEventListener('pointercancel', cancel => this._finishPointer(cancel.pointerId, true), { signal: abort.signal });
|
|
231
|
-
this._frame.addEventListener('lostpointercapture', () => this._cancelDrag(true), { signal: abort.signal });
|
|
232
233
|
}
|
|
233
234
|
_handlePointerMove(event) {
|
|
234
235
|
if (event.pointerId !== this._pointerId)
|
|
@@ -242,24 +243,22 @@ export class HYResizable extends HTMLElement {
|
|
|
242
243
|
if (pointerId !== this._pointerId)
|
|
243
244
|
return;
|
|
244
245
|
const handle = this._activeHandle;
|
|
245
|
-
this._dragAbort?.abort();
|
|
246
|
-
this._dragAbort = null;
|
|
247
246
|
this._pointerId = null;
|
|
248
247
|
this.toggleAttribute('resizing', false);
|
|
249
|
-
if (this._frame.hasPointerCapture(pointerId))
|
|
250
|
-
this._frame.releasePointerCapture(pointerId);
|
|
251
248
|
this.dispatchEvent(new CustomEvent('resize-end', {
|
|
252
249
|
detail: { ...this.rect, handle, source: 'pointer', cancelled }, bubbles: true, composed: true,
|
|
253
250
|
}));
|
|
254
251
|
}
|
|
255
252
|
_cancelDrag(cancelled) {
|
|
256
|
-
if (
|
|
257
|
-
this.
|
|
253
|
+
if (cancelled)
|
|
254
|
+
this._pointer.cancel();
|
|
258
255
|
}
|
|
259
256
|
_handleKeydown(event) {
|
|
260
|
-
if (this.disabled || !this.keyboard)
|
|
257
|
+
if (this.disabled || !this.keyboard || event.defaultPrevented || isKeyboardControlEvent(event, this._frame))
|
|
261
258
|
return;
|
|
262
259
|
const handle = event.composedPath().find((node) => node instanceof HTMLElement && Boolean(node.dataset.resizeHandle))?.dataset.resizeHandle ?? 'move';
|
|
260
|
+
if (handle === 'move' && !this.movable)
|
|
261
|
+
return;
|
|
263
262
|
const step = this.grid * (event.shiftKey ? 10 : 1);
|
|
264
263
|
let deltaX = 0;
|
|
265
264
|
let deltaY = 0;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export type HYSortableDirection = 'vertical' | 'horizontal' | 'grid';
|
|
2
|
+
export interface HYSortableChangeDetail {
|
|
3
|
+
readonly item: HTMLElement;
|
|
4
|
+
readonly from: HYSortable;
|
|
5
|
+
readonly to: HYSortable;
|
|
6
|
+
readonly oldIndex: number;
|
|
7
|
+
readonly newIndex: number;
|
|
8
|
+
readonly source: 'pointer' | 'keyboard' | 'api';
|
|
9
|
+
readonly fromOrder: readonly string[];
|
|
10
|
+
readonly toOrder: readonly string[];
|
|
11
|
+
}
|
|
12
|
+
export interface HYSortableEndDetail extends HYSortableChangeDetail {
|
|
13
|
+
readonly accepted: boolean;
|
|
14
|
+
readonly cancelled: boolean;
|
|
15
|
+
}
|
|
16
|
+
/** Reorders the original slotted elements with pointer, touch, keyboard or API input. */
|
|
17
|
+
export declare class HYSortable extends HTMLElement {
|
|
18
|
+
private readonly _surface;
|
|
19
|
+
private readonly _slot;
|
|
20
|
+
private readonly _indicator;
|
|
21
|
+
private readonly _status;
|
|
22
|
+
private readonly _instructions;
|
|
23
|
+
private readonly _pointer;
|
|
24
|
+
private readonly _owned;
|
|
25
|
+
private readonly _observer;
|
|
26
|
+
private _unregister;
|
|
27
|
+
private _candidate;
|
|
28
|
+
private _drag;
|
|
29
|
+
private _lastChange;
|
|
30
|
+
static get observedAttributes(): string[];
|
|
31
|
+
constructor();
|
|
32
|
+
connectedCallback(): void;
|
|
33
|
+
disconnectedCallback(): void;
|
|
34
|
+
attributeChangedCallback(): void;
|
|
35
|
+
get items(): HTMLElement[];
|
|
36
|
+
get order(): readonly string[];
|
|
37
|
+
get direction(): HYSortableDirection;
|
|
38
|
+
set direction(value: HYSortableDirection);
|
|
39
|
+
get group(): string;
|
|
40
|
+
set group(value: string);
|
|
41
|
+
get disabled(): boolean;
|
|
42
|
+
set disabled(value: boolean);
|
|
43
|
+
get keyboard(): boolean;
|
|
44
|
+
set keyboard(value: boolean);
|
|
45
|
+
get handle(): string;
|
|
46
|
+
set handle(value: string);
|
|
47
|
+
get columns(): number;
|
|
48
|
+
set columns(value: number);
|
|
49
|
+
move(fromIndex: number, toIndex: number): boolean;
|
|
50
|
+
refresh(): void;
|
|
51
|
+
private _own;
|
|
52
|
+
private _restore;
|
|
53
|
+
private _eventItem;
|
|
54
|
+
private _onPointerDown;
|
|
55
|
+
private _onKeydown;
|
|
56
|
+
private _begin;
|
|
57
|
+
private _accepts;
|
|
58
|
+
private _indexAt;
|
|
59
|
+
private _showIndicator;
|
|
60
|
+
private _commit;
|
|
61
|
+
}
|
|
62
|
+
export declare function defineSortableComponents(): void;
|
package/dist/sortable.js
ADDED
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import { HYPointerDrag, dragTargetAllowed } from './internal/pointer-drag.js';
|
|
2
|
+
import { activeDrag, beginDrag, cancelDragSource, composedContains, finishDrag, moveDrag, registerDropTarget } from './internal/drag-drop.js';
|
|
3
|
+
import { finiteNumber } from './internal/numbers.js';
|
|
4
|
+
import { focusedElement } from './internal/focus.js';
|
|
5
|
+
const keys = new WeakMap();
|
|
6
|
+
let keySequence = 0;
|
|
7
|
+
function itemKey(item) {
|
|
8
|
+
if (item.dataset.id || item.id)
|
|
9
|
+
return item.dataset.id || item.id;
|
|
10
|
+
let key = keys.get(item);
|
|
11
|
+
if (!key) {
|
|
12
|
+
key = `hy-sortable-item-${++keySequence}`;
|
|
13
|
+
keys.set(item, key);
|
|
14
|
+
}
|
|
15
|
+
return key;
|
|
16
|
+
}
|
|
17
|
+
/** Reorders the original slotted elements with pointer, touch, keyboard or API input. */
|
|
18
|
+
export class HYSortable extends HTMLElement {
|
|
19
|
+
_surface = document.createElement('div');
|
|
20
|
+
_slot = document.createElement('slot');
|
|
21
|
+
_indicator = document.createElement('div');
|
|
22
|
+
_status = document.createElement('div');
|
|
23
|
+
_instructions = document.createElement('div');
|
|
24
|
+
_pointer = new HYPointerDrag();
|
|
25
|
+
_owned = new Map();
|
|
26
|
+
_observer = new MutationObserver(() => this.refresh());
|
|
27
|
+
_unregister = null;
|
|
28
|
+
_candidate = null;
|
|
29
|
+
_drag = null;
|
|
30
|
+
_lastChange = null;
|
|
31
|
+
static get observedAttributes() { return ['direction', 'group', 'disabled', 'handle', 'keyboard', 'columns', 'aria-label']; }
|
|
32
|
+
constructor() {
|
|
33
|
+
super();
|
|
34
|
+
const root = this.attachShadow({ mode: 'open' });
|
|
35
|
+
const style = document.createElement('style');
|
|
36
|
+
style.textContent = `
|
|
37
|
+
:host { display: block; min-width: 0; color: var(--hy-text-color, #eef0ff); font: var(--hy-font-size-sm, 12px)/var(--hy-line-height-relaxed, 1.5) var(--hy-font-family, system-ui, sans-serif); }
|
|
38
|
+
:host([hidden]), [hidden] { display: none !important; }
|
|
39
|
+
.items { position: relative; display: flex; flex-direction: column; gap: var(--hy-sortable-gap, var(--hy-space-4, 8px)); min-height: var(--hy-sortable-min-height, 48px); }
|
|
40
|
+
slot { display: contents; }
|
|
41
|
+
:host([direction="horizontal"]) .items { flex-direction: row; }
|
|
42
|
+
:host([direction="grid"]) .items { display: grid; grid-template-columns: repeat(var(--_hy-sortable-columns, 3), minmax(0, 1fr)); }
|
|
43
|
+
::slotted(*) { min-width: 0; touch-action: var(--hy-sortable-touch-action, none); }
|
|
44
|
+
.items:focus-visible, ::slotted(:focus-visible) { outline: 2px solid var(--hy-focus-border-color, #8177f2); outline-offset: 2px; }
|
|
45
|
+
::slotted([data-hy-sort-active]) { opacity: var(--hy-sortable-drag-opacity, .45); }
|
|
46
|
+
.indicator { position: absolute; z-index: 2; height: 3px; background: var(--hy-drop-indicator-color, var(--hy-accent-color, #8177f2)); border-radius: var(--hy-radius-xs, 2px); pointer-events: none; }
|
|
47
|
+
.indicator.cross { width: 3px; height: auto; }
|
|
48
|
+
.sr { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
|
|
49
|
+
`;
|
|
50
|
+
this._surface.className = 'items';
|
|
51
|
+
this._surface.part.add('items');
|
|
52
|
+
this._surface.setAttribute('role', 'list');
|
|
53
|
+
this._indicator.className = 'indicator';
|
|
54
|
+
this._indicator.part.add('indicator');
|
|
55
|
+
this._indicator.hidden = true;
|
|
56
|
+
this._indicator.setAttribute('aria-hidden', 'true');
|
|
57
|
+
this._instructions.className = 'sr';
|
|
58
|
+
this._instructions.textContent = 'Press Space to pick up. Use arrow keys, Home or End to choose a position, Tab to another list, Enter to drop, and Escape to cancel.';
|
|
59
|
+
this._status.className = 'sr';
|
|
60
|
+
this._status.part.add('status');
|
|
61
|
+
this._status.setAttribute('role', 'status');
|
|
62
|
+
this._status.setAttribute('aria-live', 'polite');
|
|
63
|
+
this._surface.append(this._slot, this._indicator);
|
|
64
|
+
root.append(style, this._surface, this._instructions, this._status);
|
|
65
|
+
this._slot.addEventListener('slotchange', () => this.refresh());
|
|
66
|
+
this.addEventListener('pointerdown', this._onPointerDown);
|
|
67
|
+
this.addEventListener('keydown', this._onKeydown);
|
|
68
|
+
this.addEventListener('dragstart', event => event.preventDefault());
|
|
69
|
+
}
|
|
70
|
+
connectedCallback() {
|
|
71
|
+
this._unregister?.();
|
|
72
|
+
this._unregister = registerDropTarget({
|
|
73
|
+
element: this,
|
|
74
|
+
accepts: payload => this._accepts(payload),
|
|
75
|
+
over: (payload, point) => {
|
|
76
|
+
const { item, from } = payload.data;
|
|
77
|
+
if (point)
|
|
78
|
+
this._candidate = this._indexAt(item, point.x, point.y);
|
|
79
|
+
else
|
|
80
|
+
this._candidate ??= from === this ? this.items.indexOf(item) : this.items.length;
|
|
81
|
+
this._showIndicator(item);
|
|
82
|
+
},
|
|
83
|
+
leave: () => { this._candidate = null; this._indicator.hidden = true; },
|
|
84
|
+
drop: payload => {
|
|
85
|
+
const { item, from } = payload.data;
|
|
86
|
+
return this._commit(item, from, this._candidate ?? this.items.length, payload.input);
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
this._observer.observe(this, { childList: true });
|
|
90
|
+
this.refresh();
|
|
91
|
+
}
|
|
92
|
+
disconnectedCallback() {
|
|
93
|
+
this._pointer.cancel();
|
|
94
|
+
cancelDragSource(this);
|
|
95
|
+
this._unregister?.();
|
|
96
|
+
this._unregister = null;
|
|
97
|
+
this._observer.disconnect();
|
|
98
|
+
for (const item of [...this._owned.keys()])
|
|
99
|
+
this._restore(item);
|
|
100
|
+
this._indicator.hidden = true;
|
|
101
|
+
this._candidate = null;
|
|
102
|
+
}
|
|
103
|
+
attributeChangedCallback() {
|
|
104
|
+
this._pointer.cancel();
|
|
105
|
+
cancelDragSource(this);
|
|
106
|
+
this._candidate = null;
|
|
107
|
+
this._indicator.hidden = true;
|
|
108
|
+
this.refresh();
|
|
109
|
+
}
|
|
110
|
+
get items() { return [...this.children].filter((item) => item instanceof HTMLElement && !item.slot && !item.hidden); }
|
|
111
|
+
get order() { return this.items.map(itemKey); }
|
|
112
|
+
get direction() { const value = this.getAttribute('direction'); return value === 'horizontal' || value === 'grid' ? value : 'vertical'; }
|
|
113
|
+
set direction(value) { this.setAttribute('direction', value); }
|
|
114
|
+
get group() { return this.getAttribute('group') ?? ''; }
|
|
115
|
+
set group(value) { this.setAttribute('group', value); }
|
|
116
|
+
get disabled() { return this.hasAttribute('disabled'); }
|
|
117
|
+
set disabled(value) { this.toggleAttribute('disabled', value); }
|
|
118
|
+
get keyboard() { return this.getAttribute('keyboard') !== 'false'; }
|
|
119
|
+
set keyboard(value) { this.setAttribute('keyboard', String(value)); }
|
|
120
|
+
get handle() { return this.getAttribute('handle') ?? ''; }
|
|
121
|
+
set handle(value) { this.setAttribute('handle', value); }
|
|
122
|
+
get columns() { return Math.max(1, Math.min(12, Math.floor(finiteNumber(this.getAttribute('columns'), 3)))); }
|
|
123
|
+
set columns(value) { this.setAttribute('columns', String(value)); }
|
|
124
|
+
move(fromIndex, toIndex) {
|
|
125
|
+
this._pointer.cancel();
|
|
126
|
+
cancelDragSource(this);
|
|
127
|
+
const item = this.items[fromIndex];
|
|
128
|
+
return Boolean(item && Number.isInteger(fromIndex) && Number.isInteger(toIndex) && this._commit(item, this, toIndex, 'api'));
|
|
129
|
+
}
|
|
130
|
+
refresh() {
|
|
131
|
+
const items = this.items;
|
|
132
|
+
if (this._drag && !items.includes(this._drag.item)) {
|
|
133
|
+
this._pointer.cancel();
|
|
134
|
+
cancelDragSource(this);
|
|
135
|
+
}
|
|
136
|
+
for (const item of [...this._owned.keys()])
|
|
137
|
+
if (!items.includes(item))
|
|
138
|
+
this._restore(item);
|
|
139
|
+
this._surface.style.setProperty('--_hy-sortable-columns', String(this.columns));
|
|
140
|
+
this._surface.setAttribute('aria-label', this.getAttribute('aria-label') || 'Sortable list');
|
|
141
|
+
this._surface.tabIndex = this.disabled || !this.keyboard ? -1 : 0;
|
|
142
|
+
if (!this.isConnected)
|
|
143
|
+
return;
|
|
144
|
+
for (const item of items) {
|
|
145
|
+
const saved = this._owned.get(item)?.get('tabindex');
|
|
146
|
+
const original = saved ? saved.original : item.getAttribute('tabindex');
|
|
147
|
+
this._own(item, 'tabindex', this.disabled || !this.keyboard || item.hasAttribute('data-sort-disabled') ? '-1' : original ?? '0');
|
|
148
|
+
if (!item.hasAttribute('role'))
|
|
149
|
+
this._own(item, 'role', 'listitem');
|
|
150
|
+
// Slotted items cannot resolve ID references into this shadow root.
|
|
151
|
+
if (!item.hasAttribute('aria-description'))
|
|
152
|
+
this._own(item, 'aria-description', this._instructions.textContent);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
_own(item, name, value) {
|
|
156
|
+
let attributes = this._owned.get(item);
|
|
157
|
+
if (!attributes) {
|
|
158
|
+
attributes = new Map();
|
|
159
|
+
this._owned.set(item, attributes);
|
|
160
|
+
}
|
|
161
|
+
const saved = attributes.get(name);
|
|
162
|
+
attributes.set(name, { original: saved ? saved.original : item.getAttribute(name), value });
|
|
163
|
+
if (item.getAttribute(name) !== value)
|
|
164
|
+
item.setAttribute(name, value);
|
|
165
|
+
}
|
|
166
|
+
_restore(item, name) {
|
|
167
|
+
const attributes = this._owned.get(item);
|
|
168
|
+
if (!attributes)
|
|
169
|
+
return;
|
|
170
|
+
for (const [key, saved] of [...attributes]) {
|
|
171
|
+
if (name && name !== key)
|
|
172
|
+
continue;
|
|
173
|
+
if (item.getAttribute(key) === saved.value) {
|
|
174
|
+
if (saved.original === null)
|
|
175
|
+
item.removeAttribute(key);
|
|
176
|
+
else
|
|
177
|
+
item.setAttribute(key, saved.original);
|
|
178
|
+
}
|
|
179
|
+
attributes.delete(key);
|
|
180
|
+
}
|
|
181
|
+
if (!attributes.size)
|
|
182
|
+
this._owned.delete(item);
|
|
183
|
+
}
|
|
184
|
+
_eventItem(event) { return this.items.find(item => event.composedPath().includes(item)); }
|
|
185
|
+
_onPointerDown = (event) => {
|
|
186
|
+
const item = this._eventItem(event);
|
|
187
|
+
if (!item || this.disabled || item.hasAttribute('data-sort-disabled') || !dragTargetAllowed(event, this, this.handle))
|
|
188
|
+
return;
|
|
189
|
+
if (this._pointer.start(event, this._surface, {
|
|
190
|
+
start: () => this._begin(item, 'pointer'),
|
|
191
|
+
move: move => moveDrag(this.ownerDocument, move.clientX, move.clientY),
|
|
192
|
+
end: (up, cancelled) => {
|
|
193
|
+
if (!cancelled)
|
|
194
|
+
moveDrag(this.ownerDocument, up.clientX, up.clientY);
|
|
195
|
+
finishDrag(this.ownerDocument, cancelled);
|
|
196
|
+
},
|
|
197
|
+
}, 6))
|
|
198
|
+
event.stopPropagation();
|
|
199
|
+
};
|
|
200
|
+
_onKeydown = (event) => {
|
|
201
|
+
if (this.disabled || !this.keyboard || event.defaultPrevented)
|
|
202
|
+
return;
|
|
203
|
+
const payload = activeDrag(this.ownerDocument);
|
|
204
|
+
const item = this._eventItem(event);
|
|
205
|
+
if (!dragTargetAllowed(event, this) && !(this.handle && dragTargetAllowed(event, this, this.handle)))
|
|
206
|
+
return;
|
|
207
|
+
if (!payload) {
|
|
208
|
+
if (item && (event.key === ' ' || event.key === 'Enter') && !item.hasAttribute('data-sort-disabled')) {
|
|
209
|
+
event.preventDefault();
|
|
210
|
+
this._begin(item, 'keyboard');
|
|
211
|
+
}
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (!this._accepts(payload) || payload.input !== 'keyboard')
|
|
215
|
+
return;
|
|
216
|
+
if (event.key === ' ' || event.key === 'Enter') {
|
|
217
|
+
event.preventDefault();
|
|
218
|
+
finishDrag(this.ownerDocument);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const dragged = payload.data.item;
|
|
222
|
+
const length = this.items.filter(value => value !== dragged).length;
|
|
223
|
+
let next = this._candidate ?? (payload.source === this ? this.items.indexOf(dragged) : length);
|
|
224
|
+
const rtl = getComputedStyle(this).direction === 'rtl';
|
|
225
|
+
if (event.key === 'Home')
|
|
226
|
+
next = 0;
|
|
227
|
+
else if (event.key === 'End')
|
|
228
|
+
next = length;
|
|
229
|
+
else if (this.direction !== 'horizontal' && event.key === 'ArrowDown')
|
|
230
|
+
next += this.direction === 'grid' ? this.columns : 1;
|
|
231
|
+
else if (this.direction !== 'horizontal' && event.key === 'ArrowUp')
|
|
232
|
+
next -= this.direction === 'grid' ? this.columns : 1;
|
|
233
|
+
else if (this.direction !== 'vertical' && event.key === 'ArrowRight')
|
|
234
|
+
next += rtl ? -1 : 1;
|
|
235
|
+
else if (this.direction !== 'vertical' && event.key === 'ArrowLeft')
|
|
236
|
+
next += rtl ? 1 : -1;
|
|
237
|
+
else
|
|
238
|
+
return;
|
|
239
|
+
event.preventDefault();
|
|
240
|
+
this._candidate = Math.max(0, Math.min(length, next));
|
|
241
|
+
this._showIndicator(dragged);
|
|
242
|
+
this._status.textContent = `Drop at position ${this._candidate + 1}.`;
|
|
243
|
+
};
|
|
244
|
+
_begin(item, input) {
|
|
245
|
+
if (this.disabled || !this.isConnected || item.parentElement !== this || activeDrag(this.ownerDocument))
|
|
246
|
+
return false;
|
|
247
|
+
const oldIndex = this.items.indexOf(item);
|
|
248
|
+
const detail = { item, from: this, to: this, oldIndex, newIndex: oldIndex, source: input, fromOrder: this.order, toOrder: this.order };
|
|
249
|
+
if (!this.dispatchEvent(new CustomEvent('sort-start', { detail, bubbles: true, composed: true, cancelable: true })) || this.disabled || item.parentElement !== this)
|
|
250
|
+
return false;
|
|
251
|
+
this._drag = { item, oldIndex, input };
|
|
252
|
+
this._lastChange = null;
|
|
253
|
+
const started = beginDrag({ type: 'hy-sortable', data: { item, from: this }, source: this, effect: 'move', input }, end => {
|
|
254
|
+
this._drag = null;
|
|
255
|
+
this._pointer.cancel();
|
|
256
|
+
this._restore(item, 'data-hy-sort-active');
|
|
257
|
+
this._status.textContent = end.accepted ? 'Item moved.' : end.cancelled ? 'Sorting cancelled.' : 'Order unchanged.';
|
|
258
|
+
const finalDetail = this._lastChange ?? detail;
|
|
259
|
+
this._lastChange = null;
|
|
260
|
+
this.dispatchEvent(new CustomEvent('sort-end', { detail: { ...finalDetail, accepted: end.accepted, cancelled: end.cancelled }, bubbles: true, composed: true }));
|
|
261
|
+
if (item.isConnected)
|
|
262
|
+
item.focus({ preventScroll: true });
|
|
263
|
+
});
|
|
264
|
+
if (!started) {
|
|
265
|
+
this._drag = null;
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
this._own(item, 'data-hy-sort-active', '');
|
|
269
|
+
item.focus({ preventScroll: true });
|
|
270
|
+
this._status.textContent = `Picked up ${itemKey(item)} at position ${oldIndex + 1}.`;
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
_accepts(payload) {
|
|
274
|
+
if (this.disabled || payload.type !== 'hy-sortable' || !(payload.source instanceof HYSortable))
|
|
275
|
+
return false;
|
|
276
|
+
const { item, from } = payload.data;
|
|
277
|
+
return !from.disabled && item.parentElement === from && !composedContains(item, this) && (from === this || Boolean(this.group && this.group === from.group));
|
|
278
|
+
}
|
|
279
|
+
_indexAt(item, x, y) {
|
|
280
|
+
const items = this.items.filter(value => value !== item);
|
|
281
|
+
const rtl = getComputedStyle(this).direction === 'rtl';
|
|
282
|
+
const index = items.findIndex(value => {
|
|
283
|
+
const rect = value.getBoundingClientRect();
|
|
284
|
+
if (this.direction === 'vertical')
|
|
285
|
+
return y < rect.top + rect.height / 2;
|
|
286
|
+
const before = rtl ? x > rect.left + rect.width / 2 : x < rect.left + rect.width / 2;
|
|
287
|
+
return this.direction === 'horizontal' ? before : y < rect.top || (y <= rect.bottom && before);
|
|
288
|
+
});
|
|
289
|
+
return index < 0 ? items.length : index;
|
|
290
|
+
}
|
|
291
|
+
_showIndicator(item) {
|
|
292
|
+
const items = this.items.filter(value => value !== item);
|
|
293
|
+
const index = this._candidate ?? 0;
|
|
294
|
+
const target = items[index] ?? items.at(-1);
|
|
295
|
+
const bounds = this._surface.getBoundingClientRect();
|
|
296
|
+
const rect = target?.getBoundingClientRect();
|
|
297
|
+
const cross = this.direction !== 'vertical';
|
|
298
|
+
this._indicator.classList.toggle('cross', cross);
|
|
299
|
+
this._indicator.hidden = false;
|
|
300
|
+
this._indicator.style.inset = 'auto';
|
|
301
|
+
if (cross) {
|
|
302
|
+
const rtl = getComputedStyle(this).direction === 'rtl';
|
|
303
|
+
const edge = rect ? (index < items.length ? (rtl ? rect.right : rect.left) : (rtl ? rect.left : rect.right)) : bounds.left;
|
|
304
|
+
this._indicator.style.left = `${edge - bounds.left}px`;
|
|
305
|
+
this._indicator.style.top = `${rect ? rect.top - bounds.top : 0}px`;
|
|
306
|
+
this._indicator.style.height = `${rect?.height ?? 40}px`;
|
|
307
|
+
this._indicator.style.width = '3px';
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
this._indicator.style.left = '0';
|
|
311
|
+
this._indicator.style.width = '100%';
|
|
312
|
+
this._indicator.style.height = '3px';
|
|
313
|
+
this._indicator.style.top = `${rect ? (index < items.length ? rect.top : rect.bottom) - bounds.top : 0}px`;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
_commit(item, from, requested, source) {
|
|
317
|
+
if (this.disabled || from.disabled || !this.isConnected || !from.isConnected || item.parentElement !== from || composedContains(item, this) || !Number.isInteger(requested))
|
|
318
|
+
return false;
|
|
319
|
+
const originItems = from.items;
|
|
320
|
+
const oldIndex = originItems.indexOf(item);
|
|
321
|
+
if (oldIndex < 0)
|
|
322
|
+
return false;
|
|
323
|
+
const remaining = this.items.filter(value => value !== item);
|
|
324
|
+
const newIndex = Math.max(0, Math.min(remaining.length, requested));
|
|
325
|
+
if (from === this && oldIndex === newIndex)
|
|
326
|
+
return false;
|
|
327
|
+
const fromItems = from.items.filter(value => value !== item);
|
|
328
|
+
const toItems = [...remaining];
|
|
329
|
+
toItems.splice(newIndex, 0, item);
|
|
330
|
+
const detail = { item, from, to: this, oldIndex, newIndex, source, fromOrder: (from === this ? toItems : fromItems).map(itemKey), toOrder: toItems.map(itemKey) };
|
|
331
|
+
if (!this.dispatchEvent(new CustomEvent('sort-before-change', { detail, bubbles: true, composed: true, cancelable: true })) ||
|
|
332
|
+
this.disabled || from.disabled || !this.isConnected || !from.isConnected || item.parentElement !== from || composedContains(item, this))
|
|
333
|
+
return false;
|
|
334
|
+
// Applications may render new content from the cancelable event. Never use stale anchors or orders.
|
|
335
|
+
const currentFrom = from.items;
|
|
336
|
+
const currentTo = this.items.filter(value => value !== item);
|
|
337
|
+
if (currentFrom.length !== originItems.length || currentFrom.some((value, index) => value !== originItems[index]) ||
|
|
338
|
+
currentTo.length !== remaining.length || currentTo.some((value, index) => value !== remaining[index]))
|
|
339
|
+
return false;
|
|
340
|
+
from._restore(item);
|
|
341
|
+
const anchor = remaining[newIndex] ?? null;
|
|
342
|
+
const focused = focusedElement(this.ownerDocument);
|
|
343
|
+
const parent = this;
|
|
344
|
+
if (parent.moveBefore)
|
|
345
|
+
parent.moveBefore(item, anchor);
|
|
346
|
+
else
|
|
347
|
+
this.insertBefore(item, anchor);
|
|
348
|
+
from.refresh();
|
|
349
|
+
if (from !== this)
|
|
350
|
+
this.refresh();
|
|
351
|
+
if (focused && composedContains(item, focused) && focused.isConnected)
|
|
352
|
+
focused.focus({ preventScroll: true });
|
|
353
|
+
if (from._drag)
|
|
354
|
+
from._lastChange = detail;
|
|
355
|
+
this.dispatchEvent(new CustomEvent('sort-change', { detail, bubbles: true, composed: true }));
|
|
356
|
+
return true;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
export function defineSortableComponents() {
|
|
360
|
+
if (!customElements.get('hy-sortable'))
|
|
361
|
+
customElements.define('hy-sortable', HYSortable);
|
|
362
|
+
}
|
|
363
|
+
//# sourceMappingURL=sortable.js.map
|
package/dist/table.d.ts
CHANGED
|
@@ -59,7 +59,7 @@ export declare class HYTable<T extends object = Record<string, unknown>> extends
|
|
|
59
59
|
static get observedAttributes(): string[];
|
|
60
60
|
constructor();
|
|
61
61
|
connectedCallback(): void;
|
|
62
|
-
attributeChangedCallback(): void;
|
|
62
|
+
attributeChangedCallback(name?: string): void;
|
|
63
63
|
get columns(): readonly HYTableColumn<T>[];
|
|
64
64
|
set columns(value: readonly HYTableColumn<T>[]);
|
|
65
65
|
get dataSource(): readonly T[];
|
|
@@ -87,7 +87,9 @@ export declare class HYTable<T extends object = Record<string, unknown>> extends
|
|
|
87
87
|
setFilter(key: string, value: string): void;
|
|
88
88
|
clearFilters(): void;
|
|
89
89
|
private _syncAttributes;
|
|
90
|
+
private _update;
|
|
90
91
|
private _render;
|
|
92
|
+
private _syncHeaderState;
|
|
91
93
|
private _renderHeader;
|
|
92
94
|
private _createFilterMenu;
|
|
93
95
|
private _updateFilterFromControl;
|