@marianmeres/stuic 1.63.0 → 1.65.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/dist/actions/drag-drop.d.ts +27 -0
- package/dist/actions/drag-drop.js +130 -0
- package/dist/actions/tooltip/_set-position.js +1 -0
- package/dist/components/ColorScheme/color-scheme.js +3 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/utils/device-pointer.d.ts +3 -3
- package/dist/utils/device-pointer.js +4 -3
- package/dist/utils/window-size.js +4 -3
- package/package.json +1 -1
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/// <reference types="svelte" />
|
|
2
|
+
import type { Writable } from 'svelte/store';
|
|
3
|
+
export type EffectAllowed = 'copy' | 'none' | 'copyLink' | 'copyMove' | 'link' | 'linkMove' | 'move' | 'all' | 'uninitialized';
|
|
4
|
+
export type DropEffect = 'copy' | 'none' | 'link' | 'move';
|
|
5
|
+
export interface DraggableOptions {
|
|
6
|
+
id?: string;
|
|
7
|
+
enabled?: boolean;
|
|
8
|
+
payload?: any;
|
|
9
|
+
effectAllowed?: EffectAllowed;
|
|
10
|
+
isDragged?: Writable<Record<string, boolean>>;
|
|
11
|
+
}
|
|
12
|
+
export declare const draggable: (node: HTMLElement, options: DraggableOptions) => {
|
|
13
|
+
update(newOptions?: DraggableOptions): void;
|
|
14
|
+
destroy(): void;
|
|
15
|
+
};
|
|
16
|
+
export interface DroppableOptions {
|
|
17
|
+
id?: string;
|
|
18
|
+
enabled?: boolean;
|
|
19
|
+
onDrop: (data: any, e: DragEvent) => void;
|
|
20
|
+
onDragover?: (data: any) => void;
|
|
21
|
+
dropEffect?: DropEffect;
|
|
22
|
+
isDraggedOver?: Writable<Record<string, boolean>>;
|
|
23
|
+
}
|
|
24
|
+
export declare const droppable: (node: HTMLElement, options: DroppableOptions) => {
|
|
25
|
+
update(newOptions?: DroppableOptions): void;
|
|
26
|
+
destroy(): void;
|
|
27
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { createClog } from '@marianmeres/clog';
|
|
2
|
+
const clog = createClog('drag-drop');
|
|
3
|
+
const _isFn = (v) => typeof v === 'function';
|
|
4
|
+
export const draggable = (node, options) => {
|
|
5
|
+
const DEFAULT_OPTIONS = {
|
|
6
|
+
id: 'draggable-' + Math.random().toString(36).slice(2),
|
|
7
|
+
enabled: true,
|
|
8
|
+
effectAllowed: 'all',
|
|
9
|
+
};
|
|
10
|
+
options = { ...DEFAULT_OPTIONS, ...options };
|
|
11
|
+
// initalizer
|
|
12
|
+
const _init = (_opts) => {
|
|
13
|
+
if (_opts.enabled) {
|
|
14
|
+
node.setAttribute('draggable', 'true');
|
|
15
|
+
node.setAttribute('aria-grabbed', 'false');
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
node.removeAttribute('draggable');
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
// init now
|
|
22
|
+
_init(options);
|
|
23
|
+
const _payload = () => (_isFn(options?.payload) ? options.payload() : options.payload);
|
|
24
|
+
//
|
|
25
|
+
const onDragstart = (e) => {
|
|
26
|
+
const pld = _payload();
|
|
27
|
+
if (pld !== undefined) {
|
|
28
|
+
// add "stuic" as a custom data format
|
|
29
|
+
e.dataTransfer?.setData('stuic', JSON.stringify({ id: options.id, payload: pld }));
|
|
30
|
+
e.dataTransfer.effectAllowed = options.effectAllowed;
|
|
31
|
+
}
|
|
32
|
+
options?.isDragged?.update((old) => ({ ...old, [options.id]: true }));
|
|
33
|
+
node.setAttribute('aria-grabbed', 'true');
|
|
34
|
+
};
|
|
35
|
+
const onDrag = (e) => {
|
|
36
|
+
// const el: HTMLElement = e.currentTarget as HTMLElement;
|
|
37
|
+
// clog(el);
|
|
38
|
+
// ?
|
|
39
|
+
};
|
|
40
|
+
const onDragend = (e) => {
|
|
41
|
+
// e.preventDefault();
|
|
42
|
+
options?.isDragged?.update((old) => ({ ...old, [options.id]: false }));
|
|
43
|
+
node.setAttribute('aria-grabbed', 'false');
|
|
44
|
+
};
|
|
45
|
+
node.addEventListener('dragstart', onDragstart);
|
|
46
|
+
node.addEventListener('drag', onDrag);
|
|
47
|
+
node.addEventListener('dragend', onDragend);
|
|
48
|
+
return {
|
|
49
|
+
update(newOptions) {
|
|
50
|
+
options = { ...options, ...(newOptions || {}) };
|
|
51
|
+
// reinit maybe
|
|
52
|
+
_init(options);
|
|
53
|
+
},
|
|
54
|
+
destroy() {
|
|
55
|
+
node.removeEventListener('dragstart', onDragstart);
|
|
56
|
+
node.removeEventListener('drag', onDrag);
|
|
57
|
+
node.removeEventListener('dragend', onDragend);
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
export const droppable = (node, options) => {
|
|
62
|
+
const DEFAULT_OPTIONS = {
|
|
63
|
+
id: 'droppable-' + Math.random().toString(36).slice(2),
|
|
64
|
+
enabled: true,
|
|
65
|
+
dropEffect: 'move',
|
|
66
|
+
};
|
|
67
|
+
options = { ...DEFAULT_OPTIONS, ...options };
|
|
68
|
+
//
|
|
69
|
+
const onDragenter = (e) => {
|
|
70
|
+
// e.preventDefault();
|
|
71
|
+
// console.log('dragover', e.dataTransfer);
|
|
72
|
+
e.dataTransfer.dropEffect = options.dropEffect;
|
|
73
|
+
options?.isDraggedOver?.update((old) => ({ ...old, [options.id]: true }));
|
|
74
|
+
};
|
|
75
|
+
const onDragover = (e) => {
|
|
76
|
+
e.preventDefault(); // this prevents animation
|
|
77
|
+
};
|
|
78
|
+
const onDragleave = (e) => {
|
|
79
|
+
// e.preventDefault();
|
|
80
|
+
options?.isDraggedOver?.update((old) => ({ ...old, [options.id]: false }));
|
|
81
|
+
};
|
|
82
|
+
const onDrop = (e) => {
|
|
83
|
+
e.preventDefault();
|
|
84
|
+
const target = e.target;
|
|
85
|
+
e.dataTransfer.dropEffect = options.dropEffect;
|
|
86
|
+
options?.isDraggedOver?.update((old) => ({ ...old, [options.id]: false }));
|
|
87
|
+
if (_isFn(options.onDrop)) {
|
|
88
|
+
let stuicData = e.dataTransfer?.getData('stuic');
|
|
89
|
+
// prettier-ignore
|
|
90
|
+
try {
|
|
91
|
+
stuicData = JSON.parse(stuicData);
|
|
92
|
+
}
|
|
93
|
+
catch (e) { }
|
|
94
|
+
return options.onDrop(stuicData, e);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const _removeListeners = () => {
|
|
98
|
+
node.removeEventListener('dragenter', onDragenter);
|
|
99
|
+
node.removeEventListener('dragover', onDragover);
|
|
100
|
+
node.removeEventListener('dragleave', onDragleave);
|
|
101
|
+
node.removeEventListener('drop', onDrop);
|
|
102
|
+
};
|
|
103
|
+
//
|
|
104
|
+
let _listens = false;
|
|
105
|
+
const _init = (_opts) => {
|
|
106
|
+
if (_opts.enabled && !_listens) {
|
|
107
|
+
node.addEventListener('dragenter', onDragenter);
|
|
108
|
+
node.addEventListener('dragover', onDragover);
|
|
109
|
+
node.addEventListener('dragleave', onDragleave);
|
|
110
|
+
node.addEventListener('drop', onDrop);
|
|
111
|
+
_listens = true;
|
|
112
|
+
}
|
|
113
|
+
else if (!_opts.enabled && _listens) {
|
|
114
|
+
_removeListeners();
|
|
115
|
+
_listens = false;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
// init now
|
|
119
|
+
_init(options);
|
|
120
|
+
return {
|
|
121
|
+
update(newOptions) {
|
|
122
|
+
options = { ...options, ...(newOptions || {}) };
|
|
123
|
+
// reinit maybe
|
|
124
|
+
_init(options);
|
|
125
|
+
},
|
|
126
|
+
destroy() {
|
|
127
|
+
_removeListeners();
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
};
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
+
const _window = typeof window !== 'undefined' ? window : null;
|
|
1
2
|
export class ColorScheme {
|
|
2
3
|
static KEY = 'color-scheme';
|
|
3
4
|
static DARK = 'dark';
|
|
4
5
|
static LIGHT = 'light';
|
|
5
|
-
static getSystemValue = () =>
|
|
6
|
+
static getSystemValue = () => _window?.matchMedia(`(prefers-color-scheme: ${ColorScheme.DARK})`).matches
|
|
6
7
|
? ColorScheme.DARK
|
|
7
8
|
: ColorScheme.LIGHT;
|
|
8
9
|
static getLocalValue = (fallback = null) => localStorage?.getItem(ColorScheme.KEY) || fallback;
|
|
9
10
|
static getValue = () => ColorScheme.getLocalValue(ColorScheme.getSystemValue());
|
|
10
11
|
static toggle = () => {
|
|
11
12
|
// returns bool, indicating whether token is in the list after the call or not.
|
|
12
|
-
const isDark =
|
|
13
|
+
const isDark = _window?.document.documentElement.classList.toggle(ColorScheme.DARK);
|
|
13
14
|
localStorage?.setItem(ColorScheme.KEY, isDark ? ColorScheme.DARK : ColorScheme.LIGHT);
|
|
14
15
|
};
|
|
15
16
|
static reset = () => {
|
package/dist/index.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ export { default as Switch, SwitchConfig } from './components/Switch/Switch.svel
|
|
|
21
21
|
export { default as Thc, type THC, isTHCNotEmpty } from './components/Thc/Thc.svelte';
|
|
22
22
|
export { default as X } from './components/X/X.svelte';
|
|
23
23
|
export { autogrow } from './actions/autogrow.js';
|
|
24
|
+
export { draggable, droppable, type DraggableOptions, type DroppableOptions, } from './actions/drag-drop.js';
|
|
24
25
|
export { focusTrap } from './actions/focus-trap.js';
|
|
25
26
|
export { onOutside } from './actions/on-outside.js';
|
|
26
27
|
export { preSubmitValidityCheck } from './actions/pre-submit-validity-check.js';
|
package/dist/index.js
CHANGED
|
@@ -36,6 +36,7 @@ export { default as Thc, isTHCNotEmpty } from './components/Thc/Thc.svelte';
|
|
|
36
36
|
export { default as X } from './components/X/X.svelte';
|
|
37
37
|
// actions
|
|
38
38
|
export { autogrow } from './actions/autogrow.js';
|
|
39
|
+
export { draggable, droppable, } from './actions/drag-drop.js';
|
|
39
40
|
export { focusTrap } from './actions/focus-trap.js';
|
|
40
41
|
export { onOutside } from './actions/on-outside.js';
|
|
41
42
|
export { preSubmitValidityCheck } from './actions/pre-submit-validity-check.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare class DevicePointer {
|
|
2
|
-
static readonly isNone: boolean;
|
|
3
|
-
static readonly isCoarse: boolean;
|
|
4
|
-
static readonly isFine: boolean;
|
|
2
|
+
static readonly isNone: boolean | undefined;
|
|
3
|
+
static readonly isCoarse: boolean | undefined;
|
|
4
|
+
static readonly isFine: boolean | undefined;
|
|
5
5
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
const _window = typeof window !== 'undefined' ? window : null;
|
|
1
2
|
export class DevicePointer {
|
|
2
3
|
// none - The primary input mechanism does not include a pointing device.
|
|
3
|
-
static isNone =
|
|
4
|
+
static isNone = _window?.matchMedia('(any-pointer:none)').matches;
|
|
4
5
|
// coarse - The primary input mechanism includes a pointing device of limited accuracy,
|
|
5
6
|
// such as a finger on a touchscreen.
|
|
6
|
-
static isCoarse =
|
|
7
|
+
static isCoarse = _window?.matchMedia('(any-pointer:coarse)').matches;
|
|
7
8
|
// fine - The primary input mechanism includes an accurate pointing device, such as a mouse.
|
|
8
|
-
static isFine =
|
|
9
|
+
static isFine = _window?.matchMedia('(any-pointer:fine)').matches;
|
|
9
10
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { derived, writable } from 'svelte/store';
|
|
2
|
+
const _window = typeof window !== 'undefined' ? window : null;
|
|
2
3
|
const getWindowSize = () => {
|
|
3
|
-
const { width, height, scale } =
|
|
4
|
+
const { width, height, scale } = _window?.visualViewport || {
|
|
4
5
|
width: 0,
|
|
5
6
|
height: 0,
|
|
6
7
|
scale: 1,
|
|
@@ -15,8 +16,8 @@ export const windowSize = {
|
|
|
15
16
|
// init now!
|
|
16
17
|
windowSize.touch();
|
|
17
18
|
// intentionally not debounced
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
_window?.visualViewport?.addEventListener('resize', windowSize.touch);
|
|
20
|
+
_window?.visualViewport?.addEventListener('scroll', windowSize.touch);
|
|
20
21
|
export const breakpoint = derived([windowSize], ([{ width: w }]) => {
|
|
21
22
|
const list = [
|
|
22
23
|
['sm', 640],
|