@haiyue/ui 0.3.1 → 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 +5 -0
- package/README.md +68 -1
- 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 +21 -29
- 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/internal/drag-drop.d.ts +29 -0
- package/dist/internal/drag-drop.js +171 -0
- package/dist/internal/native-drop.d.ts +17 -0
- package/dist/internal/native-drop.js +62 -0
- package/dist/internal/pointer-drag.d.ts +15 -0
- package/dist/internal/pointer-drag.js +100 -0
- package/dist/resizable.d.ts +1 -1
- package/dist/resizable.js +22 -26
- package/dist/sortable.d.ts +62 -0
- package/dist/sortable.js +363 -0
- package/dist/upload.d.ts +1 -5
- package/dist/upload.js +13 -24
- package/package.json +33 -6
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/upload.d.ts
CHANGED
|
@@ -50,7 +50,7 @@ export declare class HYUpload extends HTMLElement {
|
|
|
50
50
|
private _listeners;
|
|
51
51
|
private _formDisabled;
|
|
52
52
|
private _selection;
|
|
53
|
-
private
|
|
53
|
+
private readonly _drop;
|
|
54
54
|
static get observedAttributes(): string[];
|
|
55
55
|
constructor();
|
|
56
56
|
connectedCallback(): void;
|
|
@@ -100,9 +100,5 @@ export declare class HYUpload extends HTMLElement {
|
|
|
100
100
|
private _reject;
|
|
101
101
|
private _sync;
|
|
102
102
|
private _onListClick;
|
|
103
|
-
private _onDragEnter;
|
|
104
|
-
private _onDragLeave;
|
|
105
|
-
private _onDragOver;
|
|
106
|
-
private _onDrop;
|
|
107
103
|
}
|
|
108
104
|
export declare function defineUploadComponents(): void;
|
package/dist/upload.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { HYNativeDrop, hasFiles } from './internal/native-drop.js';
|
|
1
2
|
let uploadFileId = 0;
|
|
2
3
|
export class HYUpload extends HTMLElement {
|
|
3
4
|
static formAssociated = true;
|
|
@@ -17,7 +18,13 @@ export class HYUpload extends HTMLElement {
|
|
|
17
18
|
_listeners = null;
|
|
18
19
|
_formDisabled = false;
|
|
19
20
|
_selection = 0;
|
|
20
|
-
|
|
21
|
+
_drop = new HYNativeDrop(this._zone, {
|
|
22
|
+
enabled: () => this.drag,
|
|
23
|
+
accepts: transfer => !this.disabled && !this._formDisabled && hasFiles(transfer),
|
|
24
|
+
active: value => this._zone.classList.toggle('dragover', value),
|
|
25
|
+
drop: event => { if (event.dataTransfer)
|
|
26
|
+
void this.selectFiles(event.dataTransfer.files); },
|
|
27
|
+
});
|
|
21
28
|
static get observedAttributes() { return ['name', 'accept', 'multiple', 'disabled', 'drag', 'required', 'max-count', 'max-size', 'auto-upload', 'label', 'aria-label', 'show-file-list']; }
|
|
22
29
|
constructor() {
|
|
23
30
|
super();
|
|
@@ -60,17 +67,16 @@ export class HYUpload extends HTMLElement {
|
|
|
60
67
|
// Native picker input precedes asynchronous acceptance; notify only after committing files.
|
|
61
68
|
this._input.addEventListener('input', event => event.stopPropagation(), { signal });
|
|
62
69
|
this._input.addEventListener('change', event => { event.stopPropagation(); const files = [...(this._input.files ?? [])]; this._input.value = ''; void this.selectFiles(files); }, { signal });
|
|
63
|
-
this.
|
|
64
|
-
this._zone.addEventListener('dragleave', this._onDragLeave, { signal });
|
|
65
|
-
this._zone.addEventListener('dragover', this._onDragOver, { signal });
|
|
66
|
-
this._zone.addEventListener('drop', this._onDrop, { signal });
|
|
70
|
+
this._drop.connect(signal);
|
|
67
71
|
this._list.addEventListener('click', this._onListClick, { signal });
|
|
68
72
|
this._sync();
|
|
69
73
|
}
|
|
70
|
-
disconnectedCallback() { this._listeners?.abort(); this._listeners = null; this._selection++; this.abort(); this.
|
|
74
|
+
disconnectedCallback() { this._listeners?.abort(); this._listeners = null; this._selection++; this.abort(); this._drop.reset(); }
|
|
71
75
|
attributeChangedCallback(name, previous, next) {
|
|
72
76
|
if (previous === next)
|
|
73
77
|
return;
|
|
78
|
+
if (name === 'disabled' || name === 'drag')
|
|
79
|
+
this._drop.reset();
|
|
74
80
|
if (name === 'disabled' && this.disabled) {
|
|
75
81
|
this._selection++;
|
|
76
82
|
this.abort();
|
|
@@ -118,6 +124,7 @@ export class HYUpload extends HTMLElement {
|
|
|
118
124
|
formDisabledCallback(disabled) { this._formDisabled = disabled; if (disabled) {
|
|
119
125
|
this._selection++;
|
|
120
126
|
this.abort();
|
|
127
|
+
this._drop.reset();
|
|
121
128
|
} this._sync(); }
|
|
122
129
|
formResetCallback() { this.fileList = this._initialFiles ?? []; this._message.textContent = ''; }
|
|
123
130
|
focus(options) { this._button.focus(options); }
|
|
@@ -329,24 +336,6 @@ export class HYUpload extends HTMLElement {
|
|
|
329
336
|
else if (button?.dataset.retry)
|
|
330
337
|
void this.upload(button.dataset.retry);
|
|
331
338
|
};
|
|
332
|
-
_onDragEnter = (event) => { if (!this.drag)
|
|
333
|
-
return; event.preventDefault(); if (!this.disabled && !this._formDisabled) {
|
|
334
|
-
this._dragDepth++;
|
|
335
|
-
this._zone.classList.add('dragover');
|
|
336
|
-
} };
|
|
337
|
-
_onDragLeave = (event) => { if (!this.drag)
|
|
338
|
-
return; event.preventDefault(); if (--this._dragDepth <= 0) {
|
|
339
|
-
this._dragDepth = 0;
|
|
340
|
-
this._zone.classList.remove('dragover');
|
|
341
|
-
} };
|
|
342
|
-
_onDragOver = (event) => { if (this.drag) {
|
|
343
|
-
event.preventDefault();
|
|
344
|
-
if (event.dataTransfer)
|
|
345
|
-
event.dataTransfer.dropEffect = this.disabled || this._formDisabled ? 'none' : 'copy';
|
|
346
|
-
} };
|
|
347
|
-
_onDrop = (event) => { if (!this.drag)
|
|
348
|
-
return; event.preventDefault(); this._dragDepth = 0; this._zone.classList.remove('dragover'); if (event.dataTransfer)
|
|
349
|
-
void this.selectFiles(event.dataTransfer.files); };
|
|
350
339
|
}
|
|
351
340
|
function nonnegative(value) { const number = Number(value); return Number.isFinite(number) ? Math.max(0, number) : 0; }
|
|
352
341
|
function matchesAccept(file, accept) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@haiyue/ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Dependency-free Web Components for HaiyueStudio tools and products.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -101,6 +101,18 @@
|
|
|
101
101
|
"types": "./dist/draggable.d.ts",
|
|
102
102
|
"import": "./dist/draggable.js"
|
|
103
103
|
},
|
|
104
|
+
"./drag-source": {
|
|
105
|
+
"types": "./dist/drag-source.d.ts",
|
|
106
|
+
"import": "./dist/drag-source.js"
|
|
107
|
+
},
|
|
108
|
+
"./dropzone": {
|
|
109
|
+
"types": "./dist/dropzone.d.ts",
|
|
110
|
+
"import": "./dist/dropzone.js"
|
|
111
|
+
},
|
|
112
|
+
"./sortable": {
|
|
113
|
+
"types": "./dist/sortable.d.ts",
|
|
114
|
+
"import": "./dist/sortable.js"
|
|
115
|
+
},
|
|
104
116
|
"./dropdown": {
|
|
105
117
|
"types": "./dist/dropdown.d.ts",
|
|
106
118
|
"import": "./dist/dropdown.js"
|
|
@@ -273,11 +285,26 @@
|
|
|
273
285
|
"types": "./dist/tree.d.ts",
|
|
274
286
|
"import": "./dist/tree.js"
|
|
275
287
|
},
|
|
276
|
-
"./empty": {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
"./
|
|
288
|
+
"./empty": {
|
|
289
|
+
"types": "./dist/empty.d.ts",
|
|
290
|
+
"import": "./dist/empty.js"
|
|
291
|
+
},
|
|
292
|
+
"./result": {
|
|
293
|
+
"types": "./dist/result.d.ts",
|
|
294
|
+
"import": "./dist/result.js"
|
|
295
|
+
},
|
|
296
|
+
"./avatar": {
|
|
297
|
+
"types": "./dist/avatar.d.ts",
|
|
298
|
+
"import": "./dist/avatar.js"
|
|
299
|
+
},
|
|
300
|
+
"./card": {
|
|
301
|
+
"types": "./dist/card.d.ts",
|
|
302
|
+
"import": "./dist/card.js"
|
|
303
|
+
},
|
|
304
|
+
"./descriptions": {
|
|
305
|
+
"types": "./dist/descriptions.d.ts",
|
|
306
|
+
"import": "./dist/descriptions.js"
|
|
307
|
+
},
|
|
281
308
|
"./themes/light.css": "./themes/haiyue-light.css",
|
|
282
309
|
"./themes/dark.css": "./themes/haiyue-dark.css",
|
|
283
310
|
"./package.json": "./package.json",
|