@rokkit/actions 1.0.0-next.96 → 1.0.0-next.97
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/package.json +5 -5
- package/src/index.js +2 -0
- package/src/lib/event-manager.js +18 -1
- package/src/lib/index.js +4 -3
- package/src/lib/viewport.js +2 -1
- package/src/navigator.js +0 -2
- package/src/traversable.js +51 -62
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rokkit/actions",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.97",
|
|
4
4
|
"description": "Contains generic actions that can be used in various components.",
|
|
5
5
|
"author": "Jerry Thomas <me@jerrythomas.name>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"typescript": "^5.4.5",
|
|
23
23
|
"vite": "^5.2.12",
|
|
24
24
|
"vitest": "~1.6.0",
|
|
25
|
-
"shared-config": "1.0.0-next.
|
|
26
|
-
"validators": "1.0.0-next.
|
|
25
|
+
"shared-config": "1.0.0-next.97",
|
|
26
|
+
"validators": "1.0.0-next.97"
|
|
27
27
|
},
|
|
28
28
|
"files": [
|
|
29
29
|
"src/**/*.js",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"ramda": "^0.30.1",
|
|
43
|
-
"@rokkit/core": "1.0.0-next.
|
|
44
|
-
"@rokkit/stores": "1.0.0-next.
|
|
43
|
+
"@rokkit/core": "1.0.0-next.97",
|
|
44
|
+
"@rokkit/stores": "1.0.0-next.97"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"format": "prettier --write .",
|
package/src/index.js
CHANGED
package/src/lib/event-manager.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* EventManager class to manage event listeners on an element.
|
|
3
|
+
*
|
|
4
|
+
* @param {HTMLElement} element - The element to listen for events on.
|
|
5
|
+
* @param {Object} handlers - An object with event names as keys and event handlers as values.
|
|
6
|
+
* @returns {Object} - An object with methods to activate, reset, and update the event listeners.
|
|
3
7
|
*/
|
|
4
8
|
export function EventManager(element, handlers = {}) {
|
|
5
9
|
let listening = false
|
|
6
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Activate the event listeners.
|
|
13
|
+
*/
|
|
7
14
|
function activate() {
|
|
8
15
|
if (!listening) {
|
|
9
16
|
Object.entries(handlers).forEach(([event, handler]) =>
|
|
@@ -12,6 +19,10 @@ export function EventManager(element, handlers = {}) {
|
|
|
12
19
|
listening = true
|
|
13
20
|
}
|
|
14
21
|
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Reset the event listeners.
|
|
25
|
+
*/
|
|
15
26
|
function reset() {
|
|
16
27
|
if (listening) {
|
|
17
28
|
Object.entries(handlers).forEach(([event, handler]) =>
|
|
@@ -20,11 +31,17 @@ export function EventManager(element, handlers = {}) {
|
|
|
20
31
|
listening = false
|
|
21
32
|
}
|
|
22
33
|
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Update the event listeners.
|
|
37
|
+
*
|
|
38
|
+
* @param {Object} newHandlers - An object with event names as keys and event handlers as values.
|
|
39
|
+
* @param {boolean} enabled - Whether to enable or disable the event listeners.
|
|
40
|
+
*/
|
|
23
41
|
function update(newHandlers = handlers, enabled = true) {
|
|
24
42
|
if (listening !== enabled || handlers !== newHandlers) {
|
|
25
43
|
reset()
|
|
26
44
|
handlers = newHandlers
|
|
27
|
-
// console.log(listening, enabled, handlers)
|
|
28
45
|
if (enabled) activate()
|
|
29
46
|
}
|
|
30
47
|
}
|
package/src/lib/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { dimensionAttributes, defaultResizerOptions, defaultVirtualListOptions } from './constants'
|
|
2
|
+
// skipcq: JS-E1004 - Needed for exposing all functions
|
|
2
3
|
export * from './internal'
|
|
3
|
-
export
|
|
4
|
-
export
|
|
4
|
+
export { EventManager } from './event-manager'
|
|
5
|
+
export { virtualListViewport } from './viewport'
|
package/src/lib/viewport.js
CHANGED
|
@@ -8,7 +8,8 @@ import {
|
|
|
8
8
|
} from './internal'
|
|
9
9
|
|
|
10
10
|
export function virtualListViewport(options) {
|
|
11
|
-
|
|
11
|
+
const { gap = 0 } = options
|
|
12
|
+
let { minSize = 40, maxVisible = 0, visibleSize } = options
|
|
12
13
|
let current = { lower: 0, upper: 0 }
|
|
13
14
|
const bounds = writable({ lower: 0, upper: 0 })
|
|
14
15
|
const space = writable({
|
package/src/navigator.js
CHANGED
|
@@ -24,8 +24,6 @@ export function navigator(element, options) {
|
|
|
24
24
|
|
|
25
25
|
if (!enabled) return { destroy: noop }
|
|
26
26
|
|
|
27
|
-
// todo: Update should handle selection value change
|
|
28
|
-
// should we wait a tick before updating?
|
|
29
27
|
const update = (input) => {
|
|
30
28
|
const previousNode = currentNode
|
|
31
29
|
items = input.items
|
package/src/traversable.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { has } from 'ramda'
|
|
2
2
|
import { EventManager } from './lib'
|
|
3
|
+
|
|
3
4
|
const defaultConfig = {
|
|
4
5
|
allowDrag: false,
|
|
5
6
|
allowDrop: false,
|
|
@@ -21,40 +22,49 @@ const defaultConfig = {
|
|
|
21
22
|
* @param {boolean} config.options.vertical - The orientation of the list/tree
|
|
22
23
|
*/
|
|
23
24
|
export function traversable(root, config) {
|
|
24
|
-
let store = config.store
|
|
25
25
|
const manager = EventManager(root, {})
|
|
26
|
+
const events = config.store.events
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
function update(config) {
|
|
32
|
-
store = config.store
|
|
33
|
-
const options = { ...defaultConfig, ...config.options }
|
|
34
|
-
|
|
35
|
-
const listeners = {
|
|
36
|
-
keydown: getKeydownHandler(store, options, root),
|
|
37
|
-
click: getClickHandler(store, options)
|
|
38
|
-
}
|
|
39
|
-
if (options.allowDrag) listeners.dragstart = getDragStartHandler(store)
|
|
40
|
-
if (options.allowDrop) {
|
|
41
|
-
listeners.dragover = getDragOverHandler(store)
|
|
42
|
-
listeners.drop = getDropHandler(store)
|
|
28
|
+
const unsubscribe = events.subscribe((data) => {
|
|
29
|
+
if (data.length > 0) {
|
|
30
|
+
data.forEach(({ type, detail }) => root.dispatchEvent(new CustomEvent(type, { detail })))
|
|
31
|
+
events.set([])
|
|
43
32
|
}
|
|
44
|
-
|
|
45
|
-
}
|
|
33
|
+
})
|
|
46
34
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
35
|
+
updateEventHandlers(root, manager, config)
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
destroy: () => {
|
|
39
|
+
// console.log(typeof unsubscribe)
|
|
40
|
+
unsubscribe()
|
|
41
|
+
manager.reset()
|
|
42
|
+
},
|
|
43
|
+
update: (newConfig) => updateEventHandlers(root, manager, newConfig)
|
|
53
44
|
}
|
|
45
|
+
}
|
|
54
46
|
|
|
55
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Update the event handlers based on the configuration
|
|
49
|
+
*
|
|
50
|
+
* @param {HTMLElement} root - The DOM root node to add the action to
|
|
51
|
+
* @param {Object} manager - The event manager object
|
|
52
|
+
* @param {Object} config - The configuration object
|
|
53
|
+
*/
|
|
54
|
+
function updateEventHandlers(root, manager, config) {
|
|
55
|
+
const store = config.store
|
|
56
|
+
const options = { ...defaultConfig, ...config.options }
|
|
56
57
|
|
|
57
|
-
|
|
58
|
+
const listeners = {
|
|
59
|
+
keydown: getKeydownHandler(store, options, root),
|
|
60
|
+
click: getClickHandler(store, options)
|
|
61
|
+
}
|
|
62
|
+
if (options.allowDrag) listeners.dragstart = getDragEventHandler(store, 'dragStart')
|
|
63
|
+
if (options.allowDrop) {
|
|
64
|
+
listeners.dragover = getDragEventHandler(store, 'dragOver')
|
|
65
|
+
listeners.drop = getDragEventHandler(store, 'dropOver')
|
|
66
|
+
}
|
|
67
|
+
manager.update(listeners)
|
|
58
68
|
}
|
|
59
69
|
|
|
60
70
|
/**
|
|
@@ -145,44 +155,22 @@ function getClickHandler(store, options) {
|
|
|
145
155
|
|
|
146
156
|
return handleClick
|
|
147
157
|
}
|
|
148
|
-
/**
|
|
149
|
-
* Get a function to handle the dragstart event
|
|
150
|
-
*
|
|
151
|
-
* @param {Object} store - The store object with navigation methods
|
|
152
|
-
*/
|
|
153
|
-
function getDragStartHandler(store) {
|
|
154
|
-
function handleDragStart(event) {
|
|
155
|
-
const index = getTargetIndex(event)
|
|
156
|
-
if (index) store.dragStart(index)
|
|
157
|
-
}
|
|
158
|
-
return handleDragStart
|
|
159
|
-
}
|
|
160
158
|
|
|
161
159
|
/**
|
|
162
|
-
* Get
|
|
160
|
+
* Get the handler function for the drag events
|
|
163
161
|
*
|
|
164
|
-
* @param {Object} store
|
|
162
|
+
* @param {Object} store - The store object with navigation methods
|
|
163
|
+
* @param {string} eventName - The name of the event to dispatch
|
|
164
|
+
* @returns {Function} The event handler function
|
|
165
165
|
*/
|
|
166
|
-
function
|
|
167
|
-
function
|
|
166
|
+
function getDragEventHandler(store, eventName) {
|
|
167
|
+
function handle(event) {
|
|
168
168
|
const index = getTargetIndex(event)
|
|
169
|
-
if (index) store
|
|
169
|
+
if (index) store[eventName](index)
|
|
170
170
|
}
|
|
171
|
-
return
|
|
171
|
+
return handle
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
/**
|
|
175
|
-
* Get a function to handle the drop event
|
|
176
|
-
*
|
|
177
|
-
* @param {Object} store - The store object with navigation methods
|
|
178
|
-
*/
|
|
179
|
-
function getDropHandler(store) {
|
|
180
|
-
function handleDrop(event) {
|
|
181
|
-
const index = getTargetIndex(event)
|
|
182
|
-
if (index) store.dropOver(index)
|
|
183
|
-
}
|
|
184
|
-
return handleDrop
|
|
185
|
-
}
|
|
186
174
|
/**
|
|
187
175
|
* Handle multi-select based on the modifier keys pressed
|
|
188
176
|
*
|
|
@@ -199,6 +187,7 @@ function handleMultiSelect(store, index, modifier) {
|
|
|
199
187
|
store.select(index)
|
|
200
188
|
}
|
|
201
189
|
}
|
|
190
|
+
|
|
202
191
|
/**
|
|
203
192
|
* Get the keydown event handler
|
|
204
193
|
*
|
|
@@ -220,7 +209,7 @@ function getKeydownHandler(store, options, root) {
|
|
|
220
209
|
event.preventDefault()
|
|
221
210
|
action()
|
|
222
211
|
scrollIntoView(root, store)
|
|
223
|
-
dispatchEvents(root, store)
|
|
212
|
+
// dispatchEvents(root, store)
|
|
224
213
|
}
|
|
225
214
|
}
|
|
226
215
|
|
|
@@ -390,7 +379,7 @@ function scrollIntoView(root, store) {
|
|
|
390
379
|
* @param {HTMLElement} root - The root element to dispatch the events from
|
|
391
380
|
* @param {Object} store - The store object with navigation methods
|
|
392
381
|
*/
|
|
393
|
-
function dispatchEvents(root, store) {
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
}
|
|
382
|
+
// function dispatchEvents(root, store) {
|
|
383
|
+
// const events = store.getEvents()
|
|
384
|
+
// events.forEach((event, detail) => root.dispatchEvent(new CustomEvent(event, { detail })))
|
|
385
|
+
// }
|