@conduction/nextcloud-vue 2.174.0 → 2.175.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/esm/components/CnActionDialog/CnActionDialog.vue.js +1 -1
- package/dist/esm/components/CnActionDialog/CnActionDialog.vue2.js +114 -11
- package/dist/esm/components/CnActionDialog/CnActionDialog.vue2.js.map +1 -1
- package/dist/esm/components/CnActionDialog/CnActionDialog.vue3.js +67 -17
- package/dist/esm/components/CnActionDialog/CnActionDialog.vue3.js.map +1 -1
- package/dist/esm/components/CnBlockListEditor/CnBlockListEditor.vue.js +9 -0
- package/dist/esm/components/CnBlockListEditor/CnBlockListEditor.vue.js.map +1 -0
- package/dist/esm/components/CnBlockListEditor/CnBlockListEditor.vue2.js +352 -0
- package/dist/esm/components/CnBlockListEditor/CnBlockListEditor.vue2.js.map +1 -0
- package/dist/esm/components/CnBlockListEditor/CnBlockListEditor.vue3.js +266 -0
- package/dist/esm/components/CnBlockListEditor/CnBlockListEditor.vue3.js.map +1 -0
- package/dist/esm/components/CnChecklistRunWidget/CnChecklistRunWidget.vue.js +1 -1
- package/dist/esm/components/CnChecklistRunWidget/CnChecklistRunWidget.vue2.js +257 -7
- package/dist/esm/components/CnChecklistRunWidget/CnChecklistRunWidget.vue2.js.map +1 -1
- package/dist/esm/components/CnChecklistRunWidget/CnChecklistRunWidget.vue3.js +113 -59
- package/dist/esm/components/CnChecklistRunWidget/CnChecklistRunWidget.vue3.js.map +1 -1
- package/dist/esm/components/CnChildRecordsField/CnChildRecordsField.vue.js +1 -1
- package/dist/esm/components/CnChildRecordsField/CnChildRecordsField.vue2.js +210 -5
- package/dist/esm/components/CnChildRecordsField/CnChildRecordsField.vue2.js.map +1 -1
- package/dist/esm/components/CnChildRecordsField/CnChildRecordsField.vue3.js +134 -46
- package/dist/esm/components/CnChildRecordsField/CnChildRecordsField.vue3.js.map +1 -1
- package/dist/esm/components/CnChoiceMatrix/CnChoiceMatrix.vue.js +9 -0
- package/dist/esm/components/CnChoiceMatrix/CnChoiceMatrix.vue.js.map +1 -0
- package/dist/esm/components/CnChoiceMatrix/CnChoiceMatrix.vue2.js +385 -0
- package/dist/esm/components/CnChoiceMatrix/CnChoiceMatrix.vue2.js.map +1 -0
- package/dist/esm/components/CnChoiceMatrix/CnChoiceMatrix.vue3.js +277 -0
- package/dist/esm/components/CnChoiceMatrix/CnChoiceMatrix.vue3.js.map +1 -0
- package/dist/esm/components/CnCountdownWidget/CnCountdownWidget.vue.js +1 -1
- package/dist/esm/components/CnCountdownWidget/CnCountdownWidget.vue2.js +175 -20
- package/dist/esm/components/CnCountdownWidget/CnCountdownWidget.vue2.js.map +1 -1
- package/dist/esm/components/CnCountdownWidget/CnCountdownWidget.vue3.js +30 -8
- package/dist/esm/components/CnCountdownWidget/CnCountdownWidget.vue3.js.map +1 -1
- package/dist/esm/components/CnCreateFormWidget/CnCreateFormWidget.vue.js +1 -1
- package/dist/esm/components/CnCreateFormWidget/CnCreateFormWidget.vue2.js +72 -6
- package/dist/esm/components/CnCreateFormWidget/CnCreateFormWidget.vue2.js.map +1 -1
- package/dist/esm/components/CnCreateFormWidget/CnCreateFormWidget.vue3.js +48 -14
- package/dist/esm/components/CnCreateFormWidget/CnCreateFormWidget.vue3.js.map +1 -1
- package/dist/esm/components/CnDataMatrix/CnDataMatrix.vue.js +1 -1
- package/dist/esm/components/CnDataMatrix/CnDataMatrix.vue2.js +162 -9
- package/dist/esm/components/CnDataMatrix/CnDataMatrix.vue2.js.map +1 -1
- package/dist/esm/components/CnDataMatrix/CnDataMatrix.vue3.js +63 -23
- package/dist/esm/components/CnDataMatrix/CnDataMatrix.vue3.js.map +1 -1
- package/dist/esm/components/CnDisplayScreen/CnDisplayScreen.vue.js +9 -0
- package/dist/esm/components/CnDisplayScreen/CnDisplayScreen.vue.js.map +1 -0
- package/dist/esm/components/CnDisplayScreen/CnDisplayScreen.vue2.js +175 -0
- package/dist/esm/components/CnDisplayScreen/CnDisplayScreen.vue2.js.map +1 -0
- package/dist/esm/components/CnDisplayScreen/CnDisplayScreen.vue3.js +114 -0
- package/dist/esm/components/CnDisplayScreen/CnDisplayScreen.vue3.js.map +1 -0
- package/dist/esm/components/CnDurationField/CnDurationField.vue.js +1 -1
- package/dist/esm/components/CnDurationField/CnDurationField.vue2.js +218 -4
- package/dist/esm/components/CnDurationField/CnDurationField.vue2.js.map +1 -1
- package/dist/esm/components/CnDurationField/CnDurationField.vue3.js +133 -44
- package/dist/esm/components/CnDurationField/CnDurationField.vue3.js.map +1 -1
- package/dist/esm/components/CnFormDialog/CnFormDialog.vue.js +1 -1
- package/dist/esm/components/CnFormDialog/CnFormDialog.vue2.js.map +1 -1
- package/dist/esm/components/CnFormDialog/CnFormDialog.vue3.js +5 -1
- package/dist/esm/components/CnFormDialog/CnFormDialog.vue3.js.map +1 -1
- package/dist/esm/components/CnGeoCaptureField/CnGeoCaptureField.vue.js +9 -0
- package/dist/esm/components/CnGeoCaptureField/CnGeoCaptureField.vue.js.map +1 -0
- package/dist/esm/components/CnGeoCaptureField/CnGeoCaptureField.vue2.js +113 -0
- package/dist/esm/components/CnGeoCaptureField/CnGeoCaptureField.vue2.js.map +1 -0
- package/dist/esm/components/CnGeoCaptureField/CnGeoCaptureField.vue3.js +43 -0
- package/dist/esm/components/CnGeoCaptureField/CnGeoCaptureField.vue3.js.map +1 -0
- package/dist/esm/components/CnMarkdownEditor/CnMarkdownEditor.vue.js +1 -1
- package/dist/esm/components/CnMarkdownEditor/CnMarkdownEditor.vue2.js +110 -1
- package/dist/esm/components/CnMarkdownEditor/CnMarkdownEditor.vue2.js.map +1 -1
- package/dist/esm/components/CnMarkdownEditor/CnMarkdownEditor.vue3.js +60 -18
- package/dist/esm/components/CnMarkdownEditor/CnMarkdownEditor.vue3.js.map +1 -1
- package/dist/esm/components/CnMenuTreeNode/CnMenuTreeNode.vue2.js +2 -2
- package/dist/esm/components/CnMenuTreeNode/CnMenuTreeNode.vue2.js.map +1 -1
- package/dist/esm/components/CnObjectKanban/CnObjectKanban.vue2.js +2 -2
- package/dist/esm/components/CnObjectKanban/CnObjectKanban.vue2.js.map +1 -1
- package/dist/esm/components/CnPageTreeNode/CnPageTreeNode.vue2.js +2 -2
- package/dist/esm/components/CnPageTreeNode/CnPageTreeNode.vue2.js.map +1 -1
- package/dist/esm/components/CnQrCode/CnQrCode.vue.js +9 -0
- package/dist/esm/components/CnQrCode/CnQrCode.vue.js.map +1 -0
- package/dist/esm/components/CnQrCode/CnQrCode.vue2.js +241 -0
- package/dist/esm/components/CnQrCode/CnQrCode.vue2.js.map +1 -0
- package/dist/esm/components/CnQrCode/CnQrCode.vue3.js +75 -0
- package/dist/esm/components/CnQrCode/CnQrCode.vue3.js.map +1 -0
- package/dist/esm/components/CnReadinessChecklist/CnReadinessChecklist.vue.js +1 -1
- package/dist/esm/components/CnReadinessChecklist/CnReadinessChecklist.vue2.js +34 -3
- package/dist/esm/components/CnReadinessChecklist/CnReadinessChecklist.vue2.js.map +1 -1
- package/dist/esm/components/CnReadinessChecklist/CnReadinessChecklist.vue3.js +81 -19
- package/dist/esm/components/CnReadinessChecklist/CnReadinessChecklist.vue3.js.map +1 -1
- package/dist/esm/components/CnRecordListWidget/CnRecordListWidget.vue.js +1 -1
- package/dist/esm/components/CnRecordListWidget/CnRecordListWidget.vue2.js +11 -0
- package/dist/esm/components/CnRecordListWidget/CnRecordListWidget.vue2.js.map +1 -1
- package/dist/esm/components/CnRecordListWidget/CnRecordListWidget.vue3.js +112 -88
- package/dist/esm/components/CnRecordListWidget/CnRecordListWidget.vue3.js.map +1 -1
- package/dist/esm/components/CnRevealField/CnRevealField.vue.js +9 -0
- package/dist/esm/components/CnRevealField/CnRevealField.vue.js.map +1 -0
- package/dist/esm/components/CnRevealField/CnRevealField.vue2.js +157 -0
- package/dist/esm/components/CnRevealField/CnRevealField.vue2.js.map +1 -0
- package/dist/esm/components/CnRevealField/CnRevealField.vue3.js +116 -0
- package/dist/esm/components/CnRevealField/CnRevealField.vue3.js.map +1 -0
- package/dist/esm/components/CnTextWidget/CnTextWidget.vue.js +1 -1
- package/dist/esm/components/CnTextWidget/CnTextWidget.vue2.js +43 -3
- package/dist/esm/components/CnTextWidget/CnTextWidget.vue2.js.map +1 -1
- package/dist/esm/components/CnTextWidget/CnTextWidget.vue3.js +19 -12
- package/dist/esm/components/CnTextWidget/CnTextWidget.vue3.js.map +1 -1
- package/dist/esm/components/CnTransferCard/CnTransferCard.vue.js +9 -0
- package/dist/esm/components/CnTransferCard/CnTransferCard.vue.js.map +1 -0
- package/dist/esm/components/CnTransferCard/CnTransferCard.vue2.js +87 -0
- package/dist/esm/components/CnTransferCard/CnTransferCard.vue2.js.map +1 -0
- package/dist/esm/components/CnTransferCard/CnTransferCard.vue3.js +126 -0
- package/dist/esm/components/CnTransferCard/CnTransferCard.vue3.js.map +1 -0
- package/dist/esm/components/CnTreeView/CnTreeNode.vue.js +1 -1
- package/dist/esm/components/CnTreeView/CnTreeNode.vue2.js +66 -1
- package/dist/esm/components/CnTreeView/CnTreeNode.vue2.js.map +1 -1
- package/dist/esm/components/CnTreeView/CnTreeNode.vue3.js +129 -29
- package/dist/esm/components/CnTreeView/CnTreeNode.vue3.js.map +1 -1
- package/dist/esm/components/CnTreeView/CnTreeView.vue.js +1 -1
- package/dist/esm/components/CnTreeView/CnTreeView.vue2.js +158 -6
- package/dist/esm/components/CnTreeView/CnTreeView.vue2.js.map +1 -1
- package/dist/esm/components/CnTreeView/CnTreeView.vue3.js +100 -39
- package/dist/esm/components/CnTreeView/CnTreeView.vue3.js.map +1 -1
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/l10n/en.json.js +63 -1
- package/dist/esm/l10n/en.json.js.map +1 -1
- package/dist/esm/l10n/nl.json.js +63 -1
- package/dist/esm/l10n/nl.json.js.map +1 -1
- package/dist/esm/library-style.js +1 -1
- package/dist/esm/nextcloud-vue.css +1134 -499
- package/dist/esm/node_modules/vuedraggable/dist/vuedraggable.umd.js +2 -2
- package/dist/esm/utils/captureLocation.js +32 -0
- package/dist/esm/utils/captureLocation.js.map +1 -0
- package/dist/esm/utils/childRecordTotals.js +157 -0
- package/dist/esm/utils/childRecordTotals.js.map +1 -0
- package/dist/esm/utils/isoDuration.js +51 -1
- package/dist/esm/utils/isoDuration.js.map +1 -1
- package/dist/esm/utils/qr.js +649 -0
- package/dist/esm/utils/qr.js.map +1 -0
- package/dist/esm/utils/schema.js +3 -0
- package/dist/esm/utils/schema.js.map +1 -1
- package/dist/esm/utils/templateVariables.js +54 -0
- package/dist/esm/utils/templateVariables.js.map +1 -0
- package/dist/nextcloud-vue.cjs.js +20307 -14544
- package/dist/nextcloud-vue.cjs.js.map +2 -2
- package/dist/nextcloud-vue.css +1134 -499
- package/l10n/en.json +63 -1
- package/l10n/nl.json +63 -1
- package/package.json +1 -1
- package/src/components/CnActionDialog/CnActionDialog.vue +156 -9
- package/src/components/CnBlockListEditor/CnBlockListEditor.vue +615 -0
- package/src/components/CnBlockListEditor/index.js +9 -0
- package/src/components/CnChecklistRunWidget/CnChecklistRunWidget.vue +311 -10
- package/src/components/CnChildRecordsField/CnChildRecordsField.vue +306 -15
- package/src/components/CnChoiceMatrix/CnChoiceMatrix.vue +710 -0
- package/src/components/CnChoiceMatrix/index.js +9 -0
- package/src/components/CnCountdownWidget/CnCountdownWidget.vue +223 -28
- package/src/components/CnCreateFormWidget/CnCreateFormWidget.vue +97 -6
- package/src/components/CnDataMatrix/CnDataMatrix.vue +199 -13
- package/src/components/CnDisplayScreen/CnDisplayScreen.vue +308 -0
- package/src/components/CnDisplayScreen/index.js +9 -0
- package/src/components/CnDurationField/CnDurationField.vue +330 -5
- package/src/components/CnFormDialog/CnFormDialog.vue +4 -0
- package/src/components/CnGeoCaptureField/CnGeoCaptureField.vue +148 -0
- package/src/components/CnGeoCaptureField/index.js +9 -0
- package/src/components/CnMarkdownEditor/CnMarkdownEditor.vue +139 -1
- package/src/components/CnQrCode/CnQrCode.vue +321 -0
- package/src/components/CnQrCode/index.js +9 -0
- package/src/components/CnReadinessChecklist/CnReadinessChecklist.vue +80 -3
- package/src/components/CnRecordListWidget/CnRecordListWidget.vue +34 -1
- package/src/components/CnRevealField/CnRevealField.vue +273 -0
- package/src/components/CnRevealField/index.js +9 -0
- package/src/components/CnTextWidget/CnTextWidget.vue +62 -3
- package/src/components/CnTransferCard/CnTransferCard.vue +205 -0
- package/src/components/CnTransferCard/index.js +9 -0
- package/src/components/CnTreeView/CnTreeNode.vue +145 -2
- package/src/components/CnTreeView/CnTreeView.vue +210 -6
- package/src/components/index.js +7 -0
- package/src/index.js +8 -0
- package/src/utils/captureLocation.js +29 -0
- package/src/utils/childRecordTotals.js +153 -0
- package/src/utils/isoDuration.js +50 -0
- package/src/utils/qr.js +681 -0
- package/src/utils/schema.js +3 -0
- package/src/utils/templateVariables.js +51 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SPDX-FileCopyrightText: 2026 Conduction B.V.
|
|
3
|
+
* SPDX-License-Identifier: EUPL-1.2
|
|
4
|
+
*
|
|
5
|
+
* Arithmetic for the rows of `CnChildRecordsField`: computed columns per row,
|
|
6
|
+
* totals over all rows, and the values a picked source record gives a new row.
|
|
7
|
+
* Formulas go through `evalFormula`, so no `eval` runs.
|
|
8
|
+
*
|
|
9
|
+
* @module utils/childRecordTotals
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { evalFormula } from './evalFormula.js'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The identifiers a formula names.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} formula The formula.
|
|
18
|
+
* @return {string[]} The identifiers.
|
|
19
|
+
*/
|
|
20
|
+
function identifiersOf(formula) {
|
|
21
|
+
return String(formula || '').match(/[A-Za-z_][A-Za-z0-9_]*/g) || []
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The variables for a formula: every identifier it names, as a number, 0 when
|
|
26
|
+
* the value is empty or no number, so a half-filled row still computes.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} formula The formula.
|
|
29
|
+
* @param {object} values The named values.
|
|
30
|
+
* @return {Object<string, number>} The variables.
|
|
31
|
+
*/
|
|
32
|
+
function varsFor(formula, values) {
|
|
33
|
+
const vars = {}
|
|
34
|
+
for (const id of identifiersOf(formula)) {
|
|
35
|
+
const n = Number(values?.[id])
|
|
36
|
+
vars[id] = Number.isFinite(n) ? n : 0
|
|
37
|
+
}
|
|
38
|
+
return vars
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Evaluate a formula over named values.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} formula The formula (`quantity * price`).
|
|
45
|
+
* @param {object} values The named values.
|
|
46
|
+
* @return {number|null} The result, or null on an invalid formula or a division by zero.
|
|
47
|
+
* @spec openspec/changes/child-records-totals/tasks.md#task-1
|
|
48
|
+
*/
|
|
49
|
+
export function evalOver(formula, values) {
|
|
50
|
+
if (typeof formula !== 'string' || formula.trim() === '') {
|
|
51
|
+
return null
|
|
52
|
+
}
|
|
53
|
+
return evalFormula(formula, varsFor(formula, values))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The computed values of one row, in order, so a later computed column can
|
|
58
|
+
* name an earlier one.
|
|
59
|
+
*
|
|
60
|
+
* @param {object} row The row.
|
|
61
|
+
* @param {Array<{key: string, formula: string}>} computed The computed columns.
|
|
62
|
+
* @return {Object<string, number|null>} Computed value per key.
|
|
63
|
+
* @spec openspec/changes/child-records-totals/tasks.md#task-1
|
|
64
|
+
*/
|
|
65
|
+
export function computeRow(row, computed) {
|
|
66
|
+
const out = {}
|
|
67
|
+
const scope = { ...(row || {}) }
|
|
68
|
+
for (const col of Array.isArray(computed) ? computed : []) {
|
|
69
|
+
if (!col || !col.key) {
|
|
70
|
+
continue
|
|
71
|
+
}
|
|
72
|
+
const value = evalOver(col.formula, scope)
|
|
73
|
+
out[col.key] = value
|
|
74
|
+
scope[col.key] = value
|
|
75
|
+
}
|
|
76
|
+
return out
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The row with every computed column marked `store` written into it.
|
|
81
|
+
*
|
|
82
|
+
* @param {object} row The row.
|
|
83
|
+
* @param {Array<{key: string, formula: string, store?: boolean}>} computed The computed columns.
|
|
84
|
+
* @return {object} The row, a new object when something was written.
|
|
85
|
+
* @spec openspec/changes/child-records-totals/tasks.md#task-1
|
|
86
|
+
*/
|
|
87
|
+
export function storeComputed(row, computed) {
|
|
88
|
+
const stored = (Array.isArray(computed) ? computed : []).filter((c) => c && c.key && c.store)
|
|
89
|
+
if (stored.length === 0) {
|
|
90
|
+
return row
|
|
91
|
+
}
|
|
92
|
+
const values = computeRow(row, computed)
|
|
93
|
+
const next = { ...row }
|
|
94
|
+
for (const col of stored) {
|
|
95
|
+
next[col.key] = values[col.key]
|
|
96
|
+
}
|
|
97
|
+
return next
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The totals over all rows. A total with `sum` adds that formula over every
|
|
102
|
+
* row (a column key is a formula too); a total with `formula` computes over
|
|
103
|
+
* the totals before it (`subtotal + tax`).
|
|
104
|
+
*
|
|
105
|
+
* @param {object[]} rows The rows.
|
|
106
|
+
* @param {Array<{key: string, sum?: string, formula?: string}>} totals The totals.
|
|
107
|
+
* @param {Array<{key: string, formula: string}>} [computed] The computed columns, which `sum` may name.
|
|
108
|
+
* @return {Object<string, number|null>} Total per key.
|
|
109
|
+
* @spec openspec/changes/child-records-totals/tasks.md#task-2
|
|
110
|
+
*/
|
|
111
|
+
export function computeTotals(rows, totals, computed = []) {
|
|
112
|
+
const list = Array.isArray(rows) ? rows : []
|
|
113
|
+
const scoped = list.map((row) => ({ ...(row || {}), ...computeRow(row, computed) }))
|
|
114
|
+
const out = {}
|
|
115
|
+
for (const total of Array.isArray(totals) ? totals : []) {
|
|
116
|
+
if (!total || !total.key) {
|
|
117
|
+
continue
|
|
118
|
+
}
|
|
119
|
+
if (typeof total.sum === 'string' && total.sum !== '') {
|
|
120
|
+
let sum = 0
|
|
121
|
+
let valid = true
|
|
122
|
+
for (const row of scoped) {
|
|
123
|
+
const value = evalOver(total.sum, row)
|
|
124
|
+
if (value === null) {
|
|
125
|
+
valid = false
|
|
126
|
+
break
|
|
127
|
+
}
|
|
128
|
+
sum += value
|
|
129
|
+
}
|
|
130
|
+
out[total.key] = valid ? sum : null
|
|
131
|
+
} else {
|
|
132
|
+
out[total.key] = evalOver(total.formula, out)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return out
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The values a picked source record gives a new row. `map` names, per row
|
|
140
|
+
* key, the source key to copy; `@id` copies the source's id (or uuid).
|
|
141
|
+
*
|
|
142
|
+
* @param {object} source The picked record.
|
|
143
|
+
* @param {Object<string, string>} map Row key to source key.
|
|
144
|
+
* @return {object} The new row.
|
|
145
|
+
* @spec openspec/changes/child-records-totals/tasks.md#task-3
|
|
146
|
+
*/
|
|
147
|
+
export function rowFromSource(source, map) {
|
|
148
|
+
const row = {}
|
|
149
|
+
for (const [rowKey, sourceKey] of Object.entries(map || {})) {
|
|
150
|
+
row[rowKey] = sourceKey === '@id' ? (source?.id ?? source?.uuid ?? null) : (source?.[sourceKey] ?? null)
|
|
151
|
+
}
|
|
152
|
+
return row
|
|
153
|
+
}
|
package/src/utils/isoDuration.js
CHANGED
|
@@ -95,3 +95,53 @@ export function durationSeconds(value) {
|
|
|
95
95
|
}
|
|
96
96
|
return Object.keys(p).reduce((sum, k) => sum + p[k] * SECONDS[k], 0)
|
|
97
97
|
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Write a number of seconds as the canonical hours, minutes and seconds.
|
|
101
|
+
*
|
|
102
|
+
* @param {number} seconds The length; negative or fractional parts are dropped.
|
|
103
|
+
* @return {string} `PT1H4M23S`, `PT6M`, `PT0S`.
|
|
104
|
+
*/
|
|
105
|
+
export function secondsToDuration(seconds) {
|
|
106
|
+
const total = Math.max(0, Math.trunc(Number(seconds) || 0))
|
|
107
|
+
const h = Math.floor(total / 3600)
|
|
108
|
+
const m = Math.floor((total % 3600) / 60)
|
|
109
|
+
const s = total % 60
|
|
110
|
+
if (total === 0) {
|
|
111
|
+
return 'PT0S'
|
|
112
|
+
}
|
|
113
|
+
return `PT${h ? `${h}H` : ''}${m ? `${m}M` : ''}${s ? `${s}S` : ''}`
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Show a number of seconds as a clock: `04:23`, or `1:04:23` from an hour.
|
|
118
|
+
*
|
|
119
|
+
* @param {number} seconds The length.
|
|
120
|
+
* @return {string}
|
|
121
|
+
*/
|
|
122
|
+
export function formatClock(seconds) {
|
|
123
|
+
const total = Math.max(0, Math.trunc(Number(seconds) || 0))
|
|
124
|
+
const h = Math.floor(total / 3600)
|
|
125
|
+
const m = String(Math.floor((total % 3600) / 60)).padStart(2, '0')
|
|
126
|
+
const s = String(total % 60).padStart(2, '0')
|
|
127
|
+
return h ? `${h}:${m}:${s}` : `${m}:${s}`
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Read what a user typed as a length: `6` is minutes, `04:23` is minutes and
|
|
132
|
+
* seconds, `1:04:23` adds hours.
|
|
133
|
+
*
|
|
134
|
+
* @param {string} text The typed text.
|
|
135
|
+
* @return {number|null} Seconds, or null when the text is no length.
|
|
136
|
+
*/
|
|
137
|
+
export function parseClock(text) {
|
|
138
|
+
const trimmed = String(text ?? '').trim()
|
|
139
|
+
if (/^\d+$/.test(trimmed)) {
|
|
140
|
+
return Number(trimmed) * 60
|
|
141
|
+
}
|
|
142
|
+
const m = trimmed.match(/^(?:(\d+):)?(\d{1,3}):([0-5]\d)$/)
|
|
143
|
+
if (!m) {
|
|
144
|
+
return null
|
|
145
|
+
}
|
|
146
|
+
return Number(m[1] || 0) * 3600 + Number(m[2]) * 60 + Number(m[3])
|
|
147
|
+
}
|