@ditojs/ui 3.2.0 → 3.3.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.
@@ -1,24 +1,33 @@
1
1
  import { asArray } from '@ditojs/utils'
2
2
 
3
- export function addEvents(targets, events) {
3
+ // Adds the `events` handlers to all `targets`, and returns an object with a
4
+ // `remove()` method that removes them again. When a `signal` is passed, the
5
+ // handlers are also removed when it aborts, and not added at all when it
6
+ // already has.
7
+ export function addEvents(targets, events, { signal } = {}) {
4
8
  targets =
5
9
  targets instanceof NodeList
6
10
  ? Array.from(targets)
7
11
  : asArray(targets)
8
12
 
9
- for (const [type, handler] of Object.entries(events)) {
10
- for (const target of targets) {
11
- target.addEventListener(type, handler, false)
13
+ const controller = new AbortController()
14
+ if (!signal?.aborted) {
15
+ // Remove the handlers along with `signal`, through a listener that is
16
+ // itself removed when the handlers are removed first:
17
+ signal?.addEventListener('abort', () => controller.abort(), {
18
+ once: true,
19
+ signal: controller.signal
20
+ })
21
+ for (const [type, handler] of Object.entries(events)) {
22
+ for (const target of targets) {
23
+ target.addEventListener(type, handler, { signal: controller.signal })
24
+ }
12
25
  }
13
26
  }
14
27
 
15
28
  return {
16
29
  remove() {
17
- for (const [type, handler] of Object.entries(events)) {
18
- for (const target of targets) {
19
- target.removeEventListener(type, handler, false)
20
- }
21
- }
30
+ controller.abort()
22
31
  }
23
32
  }
24
33
  }
@@ -1,3 +1,4 @@
1
+ export * from './button.js'
1
2
  export * from './date.js'
2
3
  export * from './event.js'
3
4
  export * from './selection.js'
@@ -0,0 +1,107 @@
1
+ // Helpers for the time parts of the date and time pickers.
2
+
3
+ const timePartNames = ['hour', 'minute', 'second', 'millisecond']
4
+
5
+ export function isTimePartName(name) {
6
+ return timePartNames.includes(name)
7
+ }
8
+
9
+ // Returns the values from 0 to `count - 1` that `isDisabled()` allows.
10
+ export function getEnabledValues(count, isDisabled) {
11
+ const values = []
12
+ for (let value = 0; value < count; value++) {
13
+ if (!isDisabled(value)) {
14
+ values.push(value)
15
+ }
16
+ }
17
+ return values
18
+ }
19
+
20
+ // Returns the enabled value that follows `value` in the direction of `step`,
21
+ // wrapping around at the ends. `value` itself doesn't need to be enabled.
22
+ export function stepEnabledValue(values, value, step) {
23
+ if (values.length === 0) return null
24
+ if (step > 0) {
25
+ return values.find(enabled => enabled > value) ?? values[0]
26
+ } else {
27
+ return values.findLast(enabled => enabled < value) ?? values.at(-1)
28
+ }
29
+ }
30
+
31
+ // Returns `value` if it is enabled, or else the enabled value that follows it.
32
+ export function getEnabledValue(values, value) {
33
+ return values.includes(value) ? value : stepEnabledValue(values, value, 1)
34
+ }
35
+
36
+ export function isTimeDisabled(
37
+ date,
38
+ { disabledHour, disabledMinute, disabledSecond }
39
+ ) {
40
+ return (
41
+ disabledHour(date.getHours()) ||
42
+ disabledMinute(date.getMinutes()) ||
43
+ disabledSecond(date.getSeconds())
44
+ )
45
+ }
46
+
47
+ function isDigit(char) {
48
+ return /^\d$/.test(char)
49
+ }
50
+
51
+ // Edits the time in `input` as the key of the keydown `event` is typed, so
52
+ // that digits overwrite the existing ones instead of being inserted, and
53
+ // removed digits are replaced with zeros, keeping the time's format intact.
54
+ // `formattedLength` is the length of the text of the current value.
55
+ export function editTimeText(event, input, formattedLength) {
56
+ // Leave keyboard shortcuts alone, e.g. Cmd+A, Ctrl+V or Cmd+Z, but not the
57
+ // characters typed with Option on macOS or with AltGr (Ctrl+Alt) elsewhere.
58
+ if (event.metaKey || (event.ctrlKey && !event.altKey)) return
59
+ const start = input.selectionStart
60
+ if (start !== input.selectionEnd) return
61
+ const { value } = input
62
+ let position = start
63
+ if (event.key.length === 1) {
64
+ if (isDigit(event.key) && position < value.length) {
65
+ if (
66
+ value[position] === ':' &&
67
+ isDigit(value[position - 1]) &&
68
+ isDigit(value[position - 2])
69
+ ) {
70
+ position++
71
+ }
72
+ // Remove next digit so the event overrides it instead of inserting new
73
+ // chars.
74
+ if (
75
+ isDigit(value[position]) && (
76
+ isDigit(value[position + 1]) ||
77
+ isDigit(value[position - 1])
78
+ )
79
+ ) {
80
+ input.value = value.slice(0, position) + value.slice(position + 1)
81
+ input.setSelectionRange(position, position)
82
+ }
83
+ } else if (value.length === formattedLength) {
84
+ event.preventDefault()
85
+ } else if (value.length > formattedLength) {
86
+ input.value = value.slice(0, formattedLength)
87
+ input.setSelectionRange(start, start)
88
+ event.preventDefault()
89
+ }
90
+ } else if (event.key === 'Backspace') {
91
+ if (position === 1 || [' ', ':'].includes(value[position - 2])) {
92
+ if (position === value.length || value[position] === ':') {
93
+ position--
94
+ input.value = (
95
+ value.slice(0, position) +
96
+ ((value[position - 1] ?? '') + '00') +
97
+ value.slice(position + 1)
98
+ )
99
+ input.setSelectionRange(position, position)
100
+ } else if (isDigit(value[position]) && !isDigit(value[position + 1])) {
101
+ // When removing the first of two digits, replace with 0
102
+ input.value = value.slice(0, position) + '0' + value.slice(position)
103
+ input.setSelectionRange(position, position)
104
+ }
105
+ }
106
+ }
107
+ }