@rokkit/actions 1.0.0-next.96 → 1.0.0-next.98

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rokkit/actions",
3
- "version": "1.0.0-next.96",
3
+ "version": "1.0.0-next.98",
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",
@@ -12,18 +12,18 @@
12
12
  "access": "public"
13
13
  },
14
14
  "devDependencies": {
15
- "@sveltejs/vite-plugin-svelte": "^3.1.1",
16
- "@testing-library/svelte": "^5.1.0",
17
- "@types/ramda": "^0.30.0",
15
+ "@sveltejs/vite-plugin-svelte": "^3.1.2",
16
+ "@testing-library/svelte": "^5.2.1",
17
+ "@types/ramda": "^0.30.2",
18
18
  "@vitest/coverage-v8": "^1.6.0",
19
19
  "@vitest/ui": "~1.6.0",
20
- "jsdom": "^24.1.0",
21
- "svelte": "^4.2.17",
22
- "typescript": "^5.4.5",
23
- "vite": "^5.2.12",
20
+ "jsdom": "^24.1.3",
21
+ "svelte": "^4.2.19",
22
+ "typescript": "^5.6.2",
23
+ "vite": "^5.4.6",
24
24
  "vitest": "~1.6.0",
25
- "shared-config": "1.0.0-next.96",
26
- "validators": "1.0.0-next.96"
25
+ "shared-config": "1.0.0-next.98",
26
+ "validators": "1.0.0-next.98"
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.96",
44
- "@rokkit/stores": "1.0.0-next.96"
43
+ "@rokkit/core": "1.0.0-next.98",
44
+ "@rokkit/stores": "1.0.0-next.98"
45
45
  },
46
46
  "scripts": {
47
47
  "format": "prettier --write .",
package/src/index.js CHANGED
@@ -1,4 +1,6 @@
1
+ // skipcq: JS-E1004 - Needed for exposing all types
1
2
  export * from './types'
3
+ // skipcq: JS-E1004 - Needed for exposing collection of functions
2
4
  export * from './lib'
3
5
  export { fillable } from './fillable'
4
6
  export { pannable } from './pannable'
@@ -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 * from './constants'
1
+ export { dimensionAttributes, defaultResizerOptions, defaultVirtualListOptions } from './constants'
2
+ // skipcq: JS-E1004 - Needed for exposing all functions
2
3
  export * from './internal'
3
- export * from './event-manager'
4
- export * from './viewport'
4
+ export { EventManager } from './event-manager'
5
+ export { virtualListViewport } from './viewport'
@@ -8,7 +8,8 @@ import {
8
8
  } from './internal'
9
9
 
10
10
  export function virtualListViewport(options) {
11
- let { minSize = 40, maxVisible = 0, visibleSize, gap = 0 } = options
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
@@ -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
- * Update the event handlers based on the configuration
29
- * @param {Object} config - The configuration object
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
- manager.update(listeners)
45
- }
33
+ })
46
34
 
47
- /**
48
- * Cleanup action on destroy
49
- */
50
- function destroy() {
51
- manager.reset()
52
- // store.onNavigate(null)
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
- update(config)
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
- return { destroy, update }
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 a function to handle the dragover event
160
+ * Get the handler function for the drag events
163
161
  *
164
- * @param {Object} store - The store object with navigation methods
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 getDragOverHandler(store) {
167
- function handleDragOver(event) {
166
+ function getDragEventHandler(store, eventName) {
167
+ function handle(event) {
168
168
  const index = getTargetIndex(event)
169
- if (index) store.dragOver(index)
169
+ if (index) store[eventName](index)
170
170
  }
171
- return handleDragOver
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
- const events = store.getEvents()
395
- events.forEach((event, detail) => root.dispatchEvent(new CustomEvent(event, { detail })))
396
- }
382
+ // function dispatchEvents(root, store) {
383
+ // const events = store.getEvents()
384
+ // events.forEach((event, detail) => root.dispatchEvent(new CustomEvent(event, { detail })))
385
+ // }