@10x-media/fields 0.1.0-beta.6 → 0.1.0-beta.8
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/CHANGELOG.md +41 -0
- package/LICENSE +21 -21
- package/README.md +3 -1
- package/dist/exports/client.d.ts +7 -1
- package/dist/exports/client.js +7 -1
- package/dist/exports/measurement-utils.d.ts +9 -0
- package/dist/exports/measurement-utils.js +8 -0
- package/dist/exports/measurement.d.ts +11 -0
- package/dist/exports/measurement.js +10 -0
- package/dist/exports/phone-utils.d.ts +4 -0
- package/dist/exports/phone-utils.js +4 -0
- package/dist/exports/phone.d.ts +6 -0
- package/dist/exports/phone.js +3 -0
- package/dist/exports/rsc.d.ts +5 -1
- package/dist/exports/rsc.js +5 -1
- package/dist/exports/types.d.ts +3 -2
- package/dist/fields/icon/client/icon-field.css +4 -2
- package/dist/fields/icon/rsc/IconFieldServer.js +1 -1
- package/dist/fields/icon/rsc/IconFieldServer.js.map +1 -1
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js +1 -1
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js.map +1 -1
- package/dist/fields/icon/shared/iconLabel.js +1 -1
- package/dist/fields/icon/shared/iconLabel.js.map +1 -1
- package/dist/fields/measurement/client/MeasurementCell.d.ts +18 -0
- package/dist/fields/measurement/client/MeasurementCell.js +47 -0
- package/dist/fields/measurement/client/MeasurementCell.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementField.d.ts +11 -0
- package/dist/fields/measurement/client/MeasurementField.js +386 -0
- package/dist/fields/measurement/client/MeasurementField.js.map +1 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.d.ts +24 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.js +61 -0
- package/dist/fields/measurement/client/MeasurementUnitsProvider.js.map +1 -0
- package/dist/fields/measurement/client/editModel.js +115 -0
- package/dist/fields/measurement/client/editModel.js.map +1 -0
- package/dist/fields/measurement/client/measurementCell.css +4 -0
- package/dist/fields/measurement/client/measurementField.css +215 -0
- package/dist/fields/measurement/engine/convert.d.ts +8 -0
- package/dist/fields/measurement/engine/convert.js +21 -0
- package/dist/fields/measurement/engine/convert.js.map +1 -0
- package/dist/fields/measurement/engine/format.d.ts +37 -0
- package/dist/fields/measurement/engine/format.js +156 -0
- package/dist/fields/measurement/engine/format.js.map +1 -0
- package/dist/fields/measurement/engine/locale.d.ts +25 -0
- package/dist/fields/measurement/engine/locale.js +79 -0
- package/dist/fields/measurement/engine/locale.js.map +1 -0
- package/dist/fields/measurement/engine/precision.d.ts +34 -0
- package/dist/fields/measurement/engine/precision.js +63 -0
- package/dist/fields/measurement/engine/precision.js.map +1 -0
- package/dist/fields/measurement/engine/registry.d.ts +55 -0
- package/dist/fields/measurement/engine/registry.js +117 -0
- package/dist/fields/measurement/engine/registry.js.map +1 -0
- package/dist/fields/measurement/engine/units.d.ts +178 -0
- package/dist/fields/measurement/engine/units.js +198 -0
- package/dist/fields/measurement/engine/units.js.map +1 -0
- package/dist/fields/measurement/measurementField.d.ts +17 -0
- package/dist/fields/measurement/measurementField.js +139 -0
- package/dist/fields/measurement/measurementField.js.map +1 -0
- package/dist/fields/measurement/options.d.ts +76 -0
- package/dist/fields/measurement/options.js +9 -0
- package/dist/fields/measurement/options.js.map +1 -0
- package/dist/fields/measurement/presets.d.ts +117 -0
- package/dist/fields/measurement/presets.js +136 -0
- package/dist/fields/measurement/presets.js.map +1 -0
- package/dist/fields/measurement/server/MeasurementCellServer.d.ts +21 -0
- package/dist/fields/measurement/server/MeasurementCellServer.js +53 -0
- package/dist/fields/measurement/server/MeasurementCellServer.js.map +1 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.d.ts +16 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.js +45 -0
- package/dist/fields/measurement/server/MeasurementFieldServer.js.map +1 -0
- package/dist/fields/measurement/server/getUserUnits.js +39 -0
- package/dist/fields/measurement/server/getUserUnits.js.map +1 -0
- package/dist/fields/measurement/server/resolvePrecisionSafe.js +24 -0
- package/dist/fields/measurement/server/resolvePrecisionSafe.js.map +1 -0
- package/dist/fields/measurement/server/resolveRegistryDefault.js +26 -0
- package/dist/fields/measurement/server/resolveRegistryDefault.js.map +1 -0
- package/dist/fields/phoneNumber/client/CountryFlag.d.ts +16 -0
- package/dist/fields/phoneNumber/client/CountryFlag.js +40 -0
- package/dist/fields/phoneNumber/client/CountryFlag.js.map +1 -0
- package/dist/fields/phoneNumber/client/CountryPicker.d.ts +21 -0
- package/dist/fields/phoneNumber/client/CountryPicker.js +255 -0
- package/dist/fields/phoneNumber/client/CountryPicker.js.map +1 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.d.ts +26 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.js +318 -0
- package/dist/fields/phoneNumber/client/PhoneNumberField.js.map +1 -0
- package/dist/fields/phoneNumber/client/editModel.js +86 -0
- package/dist/fields/phoneNumber/client/editModel.js.map +1 -0
- package/dist/fields/phoneNumber/client/phoneNumberField.css +305 -0
- package/dist/fields/phoneNumber/engine/countries.d.ts +36 -0
- package/dist/fields/phoneNumber/engine/countries.js +68 -0
- package/dist/fields/phoneNumber/engine/countries.js.map +1 -0
- package/dist/fields/phoneNumber/engine/draft.d.ts +13 -0
- package/dist/fields/phoneNumber/engine/draft.js +72 -0
- package/dist/fields/phoneNumber/engine/draft.js.map +1 -0
- package/dist/fields/phoneNumber/engine/metadata.d.ts +11 -0
- package/dist/fields/phoneNumber/engine/metadata.js +32 -0
- package/dist/fields/phoneNumber/engine/metadata.js.map +1 -0
- package/dist/fields/phoneNumber/engine/phone.d.ts +37 -0
- package/dist/fields/phoneNumber/engine/phone.js +59 -0
- package/dist/fields/phoneNumber/engine/phone.js.map +1 -0
- package/dist/fields/phoneNumber/options.d.ts +72 -0
- package/dist/fields/phoneNumber/options.js +37 -0
- package/dist/fields/phoneNumber/options.js.map +1 -0
- package/dist/fields/phoneNumber/phoneNumberField.d.ts +14 -0
- package/dist/fields/phoneNumber/phoneNumberField.js +167 -0
- package/dist/fields/phoneNumber/phoneNumberField.js.map +1 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.d.ts +19 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.js +73 -0
- package/dist/fields/phoneNumber/server/PhoneNumberCellServer.js.map +1 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.d.ts +16 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.js +57 -0
- package/dist/fields/phoneNumber/server/PhoneNumberFieldServer.js.map +1 -0
- package/dist/fields/phoneNumber/server/flagsEndpoint.js +51 -0
- package/dist/fields/phoneNumber/server/flagsEndpoint.js.map +1 -0
- package/dist/fields/phoneNumber/server/readPhoneOptions.js +18 -0
- package/dist/fields/phoneNumber/server/readPhoneOptions.js.map +1 -0
- package/dist/fields/phoneNumber/server/resolvePhoneOptionsSafe.js +33 -0
- package/dist/fields/phoneNumber/server/resolvePhoneOptionsSafe.js.map +1 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -1
- package/dist/translations/ar.js +12 -1
- package/dist/translations/ar.js.map +1 -1
- package/dist/translations/de.js +12 -1
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +12 -1
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +12 -1
- package/dist/translations/es.js.map +1 -1
- package/dist/translations/fr.js +12 -1
- package/dist/translations/fr.js.map +1 -1
- package/dist/translations/id.js +12 -1
- package/dist/translations/id.js.map +1 -1
- package/dist/translations/keys.d.ts +11 -0
- package/dist/translations/keys.js +12 -1
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +12 -1
- package/dist/translations/ko.js.map +1 -1
- package/dist/translations/pt.js +12 -1
- package/dist/translations/pt.js.map +1 -1
- package/dist/translations/ru.js +12 -1
- package/dist/translations/ru.js.map +1 -1
- package/dist/translations/uk.js +12 -1
- package/dist/translations/uk.js.map +1 -1
- package/dist/translations/zh.js +12 -1
- package/dist/translations/zh.js.map +1 -1
- package/dist/types.d.ts +41 -1
- package/dist/utils/resolveStaticLabel.js +13 -0
- package/dist/utils/resolveStaticLabel.js.map +1 -0
- package/package.json +27 -5
- package/dist/fields/icon/shared/resolveStaticLabel.js +0 -16
- package/dist/fields/icon/shared/resolveStaticLabel.js.map +0 -1
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
//#region src/fields/measurement/engine/units.ts
|
|
2
|
+
const LB = .45359237;
|
|
3
|
+
const IN = .0254;
|
|
4
|
+
const UNITS = {
|
|
5
|
+
g: {
|
|
6
|
+
dimension: "mass",
|
|
7
|
+
factor: .001,
|
|
8
|
+
intlUnit: "gram",
|
|
9
|
+
shortLabel: "g"
|
|
10
|
+
},
|
|
11
|
+
kg: {
|
|
12
|
+
dimension: "mass",
|
|
13
|
+
factor: 1,
|
|
14
|
+
intlUnit: "kilogram",
|
|
15
|
+
shortLabel: "kg"
|
|
16
|
+
},
|
|
17
|
+
oz: {
|
|
18
|
+
dimension: "mass",
|
|
19
|
+
factor: LB / 16,
|
|
20
|
+
intlUnit: "ounce",
|
|
21
|
+
shortLabel: "oz"
|
|
22
|
+
},
|
|
23
|
+
lb: {
|
|
24
|
+
dimension: "mass",
|
|
25
|
+
factor: LB,
|
|
26
|
+
intlUnit: "pound",
|
|
27
|
+
shortLabel: "lb"
|
|
28
|
+
},
|
|
29
|
+
st: {
|
|
30
|
+
dimension: "mass",
|
|
31
|
+
factor: 14 * LB,
|
|
32
|
+
intlUnit: "stone",
|
|
33
|
+
shortLabel: "st"
|
|
34
|
+
},
|
|
35
|
+
mm: {
|
|
36
|
+
dimension: "length",
|
|
37
|
+
factor: .001,
|
|
38
|
+
intlUnit: "millimeter",
|
|
39
|
+
shortLabel: "mm"
|
|
40
|
+
},
|
|
41
|
+
cm: {
|
|
42
|
+
dimension: "length",
|
|
43
|
+
factor: .01,
|
|
44
|
+
intlUnit: "centimeter",
|
|
45
|
+
shortLabel: "cm"
|
|
46
|
+
},
|
|
47
|
+
m: {
|
|
48
|
+
dimension: "length",
|
|
49
|
+
factor: 1,
|
|
50
|
+
intlUnit: "meter",
|
|
51
|
+
shortLabel: "m"
|
|
52
|
+
},
|
|
53
|
+
km: {
|
|
54
|
+
dimension: "length",
|
|
55
|
+
factor: 1e3,
|
|
56
|
+
intlUnit: "kilometer",
|
|
57
|
+
shortLabel: "km"
|
|
58
|
+
},
|
|
59
|
+
in: {
|
|
60
|
+
dimension: "length",
|
|
61
|
+
factor: IN,
|
|
62
|
+
intlUnit: "inch",
|
|
63
|
+
shortLabel: "in"
|
|
64
|
+
},
|
|
65
|
+
ft: {
|
|
66
|
+
dimension: "length",
|
|
67
|
+
factor: 12 * IN,
|
|
68
|
+
intlUnit: "foot",
|
|
69
|
+
shortLabel: "ft"
|
|
70
|
+
},
|
|
71
|
+
yd: {
|
|
72
|
+
dimension: "length",
|
|
73
|
+
factor: 36 * IN,
|
|
74
|
+
intlUnit: "yard",
|
|
75
|
+
shortLabel: "yd"
|
|
76
|
+
},
|
|
77
|
+
mi: {
|
|
78
|
+
dimension: "length",
|
|
79
|
+
factor: 1609.344,
|
|
80
|
+
intlUnit: "mile",
|
|
81
|
+
shortLabel: "mi"
|
|
82
|
+
},
|
|
83
|
+
ml: {
|
|
84
|
+
dimension: "volume",
|
|
85
|
+
factor: .001,
|
|
86
|
+
intlUnit: "milliliter",
|
|
87
|
+
shortLabel: "ml"
|
|
88
|
+
},
|
|
89
|
+
l: {
|
|
90
|
+
dimension: "volume",
|
|
91
|
+
factor: 1,
|
|
92
|
+
intlUnit: "liter",
|
|
93
|
+
shortLabel: "l"
|
|
94
|
+
},
|
|
95
|
+
"fl-oz": {
|
|
96
|
+
dimension: "volume",
|
|
97
|
+
factor: .0295735295625,
|
|
98
|
+
intlUnit: "fluid-ounce",
|
|
99
|
+
shortLabel: "fl oz"
|
|
100
|
+
},
|
|
101
|
+
gal: {
|
|
102
|
+
dimension: "volume",
|
|
103
|
+
factor: 3.785411784,
|
|
104
|
+
intlUnit: "gallon",
|
|
105
|
+
shortLabel: "gal"
|
|
106
|
+
},
|
|
107
|
+
c: {
|
|
108
|
+
dimension: "temperature",
|
|
109
|
+
factor: 1,
|
|
110
|
+
intlUnit: "celsius",
|
|
111
|
+
shortLabel: "°C"
|
|
112
|
+
},
|
|
113
|
+
f: {
|
|
114
|
+
dimension: "temperature",
|
|
115
|
+
factor: 5 / 9,
|
|
116
|
+
offset: -160 / 9,
|
|
117
|
+
intlUnit: "fahrenheit",
|
|
118
|
+
shortLabel: "°F"
|
|
119
|
+
},
|
|
120
|
+
"km/h": {
|
|
121
|
+
dimension: "speed",
|
|
122
|
+
factor: 1e3 / 3600,
|
|
123
|
+
intlUnit: "kilometer-per-hour",
|
|
124
|
+
shortLabel: "km/h"
|
|
125
|
+
},
|
|
126
|
+
mph: {
|
|
127
|
+
dimension: "speed",
|
|
128
|
+
factor: .44704,
|
|
129
|
+
intlUnit: "mile-per-hour",
|
|
130
|
+
shortLabel: "mph"
|
|
131
|
+
},
|
|
132
|
+
"m/s": {
|
|
133
|
+
dimension: "speed",
|
|
134
|
+
factor: 1,
|
|
135
|
+
intlUnit: "meter-per-second",
|
|
136
|
+
shortLabel: "m/s"
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
const COMPOUNDS = {
|
|
140
|
+
"ft-in": {
|
|
141
|
+
major: "ft",
|
|
142
|
+
minor: "in",
|
|
143
|
+
ratio: 12
|
|
144
|
+
},
|
|
145
|
+
"st-lb": {
|
|
146
|
+
major: "st",
|
|
147
|
+
minor: "lb",
|
|
148
|
+
ratio: 14
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
const isCompoundUnit = (unit) => Object.hasOwn(COMPOUNDS, unit);
|
|
152
|
+
const isScalarUnit = (unit) => Object.hasOwn(UNITS, unit);
|
|
153
|
+
const dimensionOf = (unit) => isCompoundUnit(unit) ? UNITS[COMPOUNDS[unit].major].dimension : UNITS[unit].dimension;
|
|
154
|
+
/** Built-in scalars and compounds belonging to a dimension, compounds keyed by their major. */
|
|
155
|
+
const unitsOfDimension = (dimension) => {
|
|
156
|
+
const scalars = Object.keys(UNITS).filter((id) => UNITS[id].dimension === dimension);
|
|
157
|
+
const compounds = Object.keys(COMPOUNDS).filter((id) => UNITS[COMPOUNDS[id].major].dimension === dimension);
|
|
158
|
+
return [...scalars, ...compounds];
|
|
159
|
+
};
|
|
160
|
+
/** Values round-trip through display without visible drift at these precisions. */
|
|
161
|
+
const DISPLAY_PRECISION = {
|
|
162
|
+
g: 0,
|
|
163
|
+
kg: 1,
|
|
164
|
+
oz: 0,
|
|
165
|
+
lb: 0,
|
|
166
|
+
st: 0,
|
|
167
|
+
mm: 0,
|
|
168
|
+
cm: 0,
|
|
169
|
+
m: 2,
|
|
170
|
+
km: 1,
|
|
171
|
+
in: 1,
|
|
172
|
+
ft: 1,
|
|
173
|
+
yd: 1,
|
|
174
|
+
mi: 1,
|
|
175
|
+
ml: 0,
|
|
176
|
+
l: 2,
|
|
177
|
+
"fl-oz": 1,
|
|
178
|
+
gal: 2,
|
|
179
|
+
c: 1,
|
|
180
|
+
f: 0,
|
|
181
|
+
"km/h": 0,
|
|
182
|
+
mph: 0,
|
|
183
|
+
"m/s": 1
|
|
184
|
+
};
|
|
185
|
+
const COMPOUND_MINOR_PRECISION = {
|
|
186
|
+
"ft-in": 0,
|
|
187
|
+
"st-lb": 0
|
|
188
|
+
};
|
|
189
|
+
const STORAGE_FRACTION_DIGITS = 6;
|
|
190
|
+
const precisionFor = (unit, overrides) => {
|
|
191
|
+
const override = overrides?.[unit];
|
|
192
|
+
if (typeof override === "number") return override;
|
|
193
|
+
return isCompoundUnit(unit) ? COMPOUND_MINOR_PRECISION[unit] : DISPLAY_PRECISION[unit];
|
|
194
|
+
};
|
|
195
|
+
//#endregion
|
|
196
|
+
export { COMPOUNDS, COMPOUND_MINOR_PRECISION, DISPLAY_PRECISION, STORAGE_FRACTION_DIGITS, UNITS, dimensionOf, isCompoundUnit, isScalarUnit, precisionFor, unitsOfDimension };
|
|
197
|
+
|
|
198
|
+
//# sourceMappingURL=units.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"units.js","names":[],"sources":["../../../../src/fields/measurement/engine/units.ts"],"sourcesContent":["export type CoreDimension = 'length' | 'mass' | 'speed' | 'temperature' | 'volume'\nexport type Dimension = CoreDimension | (string & {})\n\nexport type ScalarUnitId =\n\t| 'c'\n\t| 'cm'\n\t| 'f'\n\t| 'fl-oz'\n\t| 'ft'\n\t| 'g'\n\t| 'gal'\n\t| 'in'\n\t| 'kg'\n\t| 'km'\n\t| 'km/h'\n\t| 'l'\n\t| 'lb'\n\t| 'm'\n\t| 'm/s'\n\t| 'mi'\n\t| 'ml'\n\t| 'mm'\n\t| 'mph'\n\t| 'oz'\n\t| 'st'\n\t| 'yd'\n\nexport type CompoundUnitId = 'ft-in' | 'st-lb'\nexport type UnitId = CompoundUnitId | ScalarUnitId\n\n/**\n * Any unit id an engine may know: the built-ins, plus whatever a field declares in\n * `custom`. Keeps built-in ids autocompleting while custom ids stay assignable.\n */\nexport type MeasurementUnitId = UnitId | (string & {})\n\n/**\n * canonical = value * factor + offset, per dimension canonical (kg, m, l,\n * celsius, m/s). Factors are the exact statute definitions where one exists.\n */\nexport type UnitDef = {\n\tdimension: Dimension\n\tfactor: number\n\toffset?: number\n\t/** ECMA-402 sanctioned unit identifier for Intl.NumberFormat. */\n\tintlUnit: string\n\t/** Static short symbol for the in-row unit affordance. */\n\tshortLabel: string\n}\n\nexport type CompoundDef = { major: ScalarUnitId; minor: ScalarUnitId; ratio: number }\n\nconst LB = 0.45359237\nconst IN = 0.0254\n\nexport const UNITS = {\n\tg: { dimension: 'mass', factor: 0.001, intlUnit: 'gram', shortLabel: 'g' },\n\tkg: { dimension: 'mass', factor: 1, intlUnit: 'kilogram', shortLabel: 'kg' },\n\toz: { dimension: 'mass', factor: LB / 16, intlUnit: 'ounce', shortLabel: 'oz' },\n\tlb: { dimension: 'mass', factor: LB, intlUnit: 'pound', shortLabel: 'lb' },\n\tst: { dimension: 'mass', factor: 14 * LB, intlUnit: 'stone', shortLabel: 'st' },\n\tmm: { dimension: 'length', factor: 0.001, intlUnit: 'millimeter', shortLabel: 'mm' },\n\tcm: { dimension: 'length', factor: 0.01, intlUnit: 'centimeter', shortLabel: 'cm' },\n\tm: { dimension: 'length', factor: 1, intlUnit: 'meter', shortLabel: 'm' },\n\tkm: { dimension: 'length', factor: 1000, intlUnit: 'kilometer', shortLabel: 'km' },\n\tin: { dimension: 'length', factor: IN, intlUnit: 'inch', shortLabel: 'in' },\n\tft: { dimension: 'length', factor: 12 * IN, intlUnit: 'foot', shortLabel: 'ft' },\n\tyd: { dimension: 'length', factor: 36 * IN, intlUnit: 'yard', shortLabel: 'yd' },\n\tmi: { dimension: 'length', factor: 1609.344, intlUnit: 'mile', shortLabel: 'mi' },\n\tml: { dimension: 'volume', factor: 0.001, intlUnit: 'milliliter', shortLabel: 'ml' },\n\tl: { dimension: 'volume', factor: 1, intlUnit: 'liter', shortLabel: 'l' },\n\t'fl-oz': {\n\t\tdimension: 'volume',\n\t\tfactor: 0.0295735295625,\n\t\tintlUnit: 'fluid-ounce',\n\t\tshortLabel: 'fl oz',\n\t},\n\tgal: { dimension: 'volume', factor: 3.785411784, intlUnit: 'gallon', shortLabel: 'gal' },\n\tc: { dimension: 'temperature', factor: 1, intlUnit: 'celsius', shortLabel: '°C' },\n\tf: {\n\t\tdimension: 'temperature',\n\t\tfactor: 5 / 9,\n\t\toffset: -160 / 9,\n\t\tintlUnit: 'fahrenheit',\n\t\tshortLabel: '°F',\n\t},\n\t'km/h': {\n\t\tdimension: 'speed',\n\t\tfactor: 1000 / 3600,\n\t\tintlUnit: 'kilometer-per-hour',\n\t\tshortLabel: 'km/h',\n\t},\n\tmph: { dimension: 'speed', factor: 0.44704, intlUnit: 'mile-per-hour', shortLabel: 'mph' },\n\t'm/s': { dimension: 'speed', factor: 1, intlUnit: 'meter-per-second', shortLabel: 'm/s' },\n} as const satisfies Record<ScalarUnitId, UnitDef>\n\nexport const COMPOUNDS = {\n\t'ft-in': { major: 'ft', minor: 'in', ratio: 12 },\n\t'st-lb': { major: 'st', minor: 'lb', ratio: 14 },\n} as const satisfies Record<CompoundUnitId, CompoundDef>\n\nexport const isCompoundUnit = (unit: UnitId): unit is CompoundUnitId =>\n\tObject.hasOwn(COMPOUNDS, unit)\nexport const isScalarUnit = (unit: string): unit is ScalarUnitId => Object.hasOwn(UNITS, unit)\n\nexport const dimensionOf = (unit: UnitId): Dimension =>\n\tisCompoundUnit(unit) ? UNITS[COMPOUNDS[unit].major].dimension : UNITS[unit].dimension\n\n/** Dimension of a built-in scalar unit, literal when S is one and the full union otherwise. */\nexport type DimOf<S extends ScalarUnitId> = (typeof UNITS)[S]['dimension']\n\n/** Scalar unit ids whose dimension is (a member of) D. */\nexport type ScalarOfDimension<D extends CoreDimension> = {\n\t[K in ScalarUnitId]: (typeof UNITS)[K]['dimension'] extends D ? K : never\n}[ScalarUnitId]\n\n/** Compound unit ids whose major scalar's dimension is (a member of) D. */\ntype CompoundOfDimension<D extends CoreDimension> = {\n\t[K in CompoundUnitId]: (typeof UNITS)[(typeof COMPOUNDS)[K]['major']]['dimension'] extends D\n\t\t? K\n\t\t: never\n}[CompoundUnitId]\n\n/** Every unit (scalar or compound) selectable for D: scalars of D plus compounds whose major is D. */\nexport type UnitOfDimension<D extends CoreDimension> = ScalarOfDimension<D> | CompoundOfDimension<D>\n\n/** Built-in scalars and compounds belonging to a dimension, compounds keyed by their major. */\nexport const unitsOfDimension = (dimension: string): UnitId[] => {\n\tconst scalars = (Object.keys(UNITS) as ScalarUnitId[]).filter(\n\t\t(id) => UNITS[id].dimension === dimension\n\t)\n\tconst compounds = (Object.keys(COMPOUNDS) as CompoundUnitId[]).filter(\n\t\t(id) => UNITS[COMPOUNDS[id].major].dimension === dimension\n\t)\n\treturn [...scalars, ...compounds]\n}\n\n/** Values round-trip through display without visible drift at these precisions. */\nexport const DISPLAY_PRECISION: Record<ScalarUnitId, number> = {\n\tg: 0,\n\tkg: 1,\n\toz: 0,\n\tlb: 0,\n\tst: 0,\n\tmm: 0,\n\tcm: 0,\n\tm: 2,\n\tkm: 1,\n\tin: 1,\n\tft: 1,\n\tyd: 1,\n\tmi: 1,\n\tml: 0,\n\tl: 2,\n\t'fl-oz': 1,\n\tgal: 2,\n\tc: 1,\n\tf: 0,\n\t'km/h': 0,\n\tmph: 0,\n\t'm/s': 1,\n}\n\nexport const COMPOUND_MINOR_PRECISION: Record<CompoundUnitId, number> = { 'ft-in': 0, 'st-lb': 0 }\n\nexport const STORAGE_FRACTION_DIGITS = 6\n\nexport const precisionFor = (unit: UnitId, overrides?: Partial<Record<UnitId, number>>): number => {\n\tconst override = overrides?.[unit]\n\tif (typeof override === 'number') return override\n\treturn isCompoundUnit(unit) ? COMPOUND_MINOR_PRECISION[unit] : DISPLAY_PRECISION[unit]\n}\n"],"mappings":";AAoDA,MAAM,KAAK;AACX,MAAM,KAAK;AAEX,MAAa,QAAQ;CACpB,GAAG;EAAE,WAAW;EAAQ,QAAQ;EAAO,UAAU;EAAQ,YAAY;CAAI;CACzE,IAAI;EAAE,WAAW;EAAQ,QAAQ;EAAG,UAAU;EAAY,YAAY;CAAK;CAC3E,IAAI;EAAE,WAAW;EAAQ,QAAQ,KAAK;EAAI,UAAU;EAAS,YAAY;CAAK;CAC9E,IAAI;EAAE,WAAW;EAAQ,QAAQ;EAAI,UAAU;EAAS,YAAY;CAAK;CACzE,IAAI;EAAE,WAAW;EAAQ,QAAQ,KAAK;EAAI,UAAU;EAAS,YAAY;CAAK;CAC9E,IAAI;EAAE,WAAW;EAAU,QAAQ;EAAO,UAAU;EAAc,YAAY;CAAK;CACnF,IAAI;EAAE,WAAW;EAAU,QAAQ;EAAM,UAAU;EAAc,YAAY;CAAK;CAClF,GAAG;EAAE,WAAW;EAAU,QAAQ;EAAG,UAAU;EAAS,YAAY;CAAI;CACxE,IAAI;EAAE,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Ae,IAAI,IAAI,MAAM,UAAU,MAAM,OAAO,YAAY,MAAM,MAAM;;AAqB7E,MAAa,oBAAoB,cAAgC;CAChE,MAAM,UAAW,OAAO,KAAK,KAAK,EAAqB,QACrD,OAAO,MAAM,IAAI,cAAc,SACjC;CACA,MAAM,YAAa,OAAO,KAAK,SAAS,EAAuB,QAC7D,OAAO,MAAM,UAAU,IAAI,OAAO,cAAc,SAClD;CACA,OAAO,CAAC,GAAG,SAAS,GAAG,SAAS;AACjC;;AAGA,MAAa,oBAAkD;CAC9D,GAAG;CACH,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,GAAG;CACH,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,GAAG;CACH,SAAS;CACT,KAAK;CACL,GAAG;CACH,GAAG;CACH,QAAQ;CACR,KAAK;CACL,OAAO;AACR;AAEA,MAAa,2BAA2D;CAAE,SAAS;CAAG,SAAS;AAAE;AAEjG,MAAa,0BAA0B;AAEvC,MAAa,gBAAgB,MAAc,cAAwD;CAClG,MAAM,WAAW,YAAY;CAC7B,IAAI,OAAO,aAAa,UAAU,OAAO;CACzC,OAAO,eAAe,IAAI,IAAI,yBAAyB,QAAQ,kBAAkB;AAClF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ScalarUnitId } from "./engine/units.js";
|
|
2
|
+
import { MeasurementCustomFieldOptions, MeasurementFieldOptions } from "./options.js";
|
|
3
|
+
import { NumberField } from "payload";
|
|
4
|
+
|
|
5
|
+
//#region src/fields/measurement/measurementField.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Number field storing a canonical value in `storageUnit` while each admin user
|
|
8
|
+
* edits and reads in their preferred unit. Spread a preset for a curated bundle,
|
|
9
|
+
* or declare `storageUnit`/`units`/`preferenceKey` directly. Validation is
|
|
10
|
+
* Payload's native number validation plus a NaN guard (min/max in storage units);
|
|
11
|
+
* the client converts at the edges.
|
|
12
|
+
*/
|
|
13
|
+
declare function measurementField<S extends ScalarUnitId>(options: MeasurementFieldOptions<S>): NumberField;
|
|
14
|
+
declare function measurementField(options: MeasurementCustomFieldOptions): NumberField;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { measurementField };
|
|
17
|
+
//# sourceMappingURL=measurementField.d.ts.map
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { roundTo } from "./engine/convert.js";
|
|
2
|
+
import { createEngine } from "./engine/registry.js";
|
|
3
|
+
import { MEASUREMENT_CUSTOM_KEY } from "./options.js";
|
|
4
|
+
import { resolvePrecisionSafe } from "./server/resolvePrecisionSafe.js";
|
|
5
|
+
import { number } from "payload/shared";
|
|
6
|
+
//#region src/fields/measurement/measurementField.ts
|
|
7
|
+
/**
|
|
8
|
+
* Storage digits may come from the plugin registry, so this resolves per request
|
|
9
|
+
* rather than once at field-build time. `fieldPrecision` is the field's own
|
|
10
|
+
* (unresolved) layer, stamped once when the field was built.
|
|
11
|
+
*/
|
|
12
|
+
const buildRoundStorageHook = (fieldPrecision) => {
|
|
13
|
+
return ({ req, value }) => {
|
|
14
|
+
if (typeof value !== "number" || Number.isNaN(value)) return value;
|
|
15
|
+
const { storage } = resolvePrecisionSafe({
|
|
16
|
+
fieldPrecision,
|
|
17
|
+
payload: req.payload
|
|
18
|
+
});
|
|
19
|
+
return roundTo(value, storage);
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Payload coerces string input through parseFloat before validation, and its
|
|
24
|
+
* native number validation lets the resulting NaN through (typeof NaN is
|
|
25
|
+
* 'number'). Mongoose happens to reject NaN at the driver; Postgres stores
|
|
26
|
+
* NaN::numeric, which then sorts above every real value. Guard here, then
|
|
27
|
+
* defer to the native validator (Payload spreads the field into options, so
|
|
28
|
+
* min/max/required behave exactly as the default install).
|
|
29
|
+
*/
|
|
30
|
+
const validateMeasurement = (value, options) => {
|
|
31
|
+
if (typeof value === "number" && Number.isNaN(value)) return options.req.t("validation:enterNumber");
|
|
32
|
+
return number(value, options);
|
|
33
|
+
};
|
|
34
|
+
const buildEngine = (options, name) => {
|
|
35
|
+
try {
|
|
36
|
+
return createEngine(options.custom);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw new Error(`measurementField(${name}): ${error.message}`);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
/** One path segment, so it round-trips through the preferences document untouched. */
|
|
42
|
+
const isValidPreferenceKey = (key) => key !== "" && !/[\s/]/.test(key);
|
|
43
|
+
const assertUnits = (args) => {
|
|
44
|
+
const { dimension, engine, name, units } = args;
|
|
45
|
+
if (units.length === 0) throw new Error(`measurementField(${name}): units is empty, so no unit can be displayed`);
|
|
46
|
+
for (const unit of units) {
|
|
47
|
+
if (!engine.isScalarUnit(unit) && !engine.isCompoundUnit(unit)) throw new Error(`measurementField(${name}): unit "${unit}" is not a known unit`);
|
|
48
|
+
const unitDimension = engine.dimensionOf(unit);
|
|
49
|
+
if (unitDimension !== dimension) throw new Error(`measurementField(${name}): unit "${unit}" is ${unitDimension}, but this field stores ${dimension} (dimension mismatch)`);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const isPrecisionMode = (mode) => mode === "readable" || mode === "exact";
|
|
53
|
+
/**
|
|
54
|
+
* Validates the field's own (unresolved) precision layer at config time, so a bad
|
|
55
|
+
* value fails fast rather than surfacing later from `resolvePrecision`. Storage
|
|
56
|
+
* range is re-checked there too, once merged with the plugin registry's layer;
|
|
57
|
+
* `display` digits stay factory-only, since only the factory has unit context.
|
|
58
|
+
*/
|
|
59
|
+
const assertPrecision = (args) => {
|
|
60
|
+
const { dimension, engine, name, precision } = args;
|
|
61
|
+
if (typeof precision === "string") {
|
|
62
|
+
if (!isPrecisionMode(precision)) throw new Error(`measurementField(${name}): precision "${precision}" must be "readable" or "exact"`);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const { display, draft, entry, mode, storage } = precision;
|
|
66
|
+
if (mode !== void 0 && !isPrecisionMode(mode)) throw new Error(`measurementField(${name}): precision.mode "${mode}" must be "readable" or "exact"`);
|
|
67
|
+
if (entry !== void 0 && entry !== "quantize" && entry !== "free") throw new Error(`measurementField(${name}): precision.entry "${entry}" must be "quantize" or "free"`);
|
|
68
|
+
if (draft !== void 0 && draft !== "display" && draft !== "faithful") throw new Error(`measurementField(${name}): precision.draft "${draft}" must be "display" or "faithful"`);
|
|
69
|
+
if (storage !== void 0 && (typeof storage !== "number" || !Number.isInteger(storage) || storage < 0 || storage > 12)) throw new Error(`measurementField(${name}): precision.storage must be an integer between 0 and 12, got ${storage}`);
|
|
70
|
+
if (display === void 0) return;
|
|
71
|
+
for (const [unit, digits] of Object.entries(display)) {
|
|
72
|
+
if (!engine.isScalarUnit(unit) && !engine.isCompoundUnit(unit)) throw new Error(`measurementField(${name}): precision.display key "${unit}" is not a known unit`);
|
|
73
|
+
if (engine.dimensionOf(unit) !== dimension) throw new Error(`measurementField(${name}): precision.display key "${unit}" is not a unit of ${dimension}`);
|
|
74
|
+
if (typeof digits !== "number" || !Number.isInteger(digits) || digits < 0 || digits > 100) throw new Error(`measurementField(${name}): precision.display for "${unit}" must be an integer between 0 and 100, got ${digits}`);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
function measurementField(options) {
|
|
78
|
+
const { custom, fallbackUnit, index, label, localeDefaults, localized, max, min, overrides, required, storageUnit } = options;
|
|
79
|
+
const earlyName = options.name ?? options.preferenceKey ?? storageUnit;
|
|
80
|
+
const engine = buildEngine(options, earlyName);
|
|
81
|
+
if (!engine.isScalarUnit(storageUnit)) throw new Error(engine.isCompoundUnit(storageUnit) ? `measurementField(${earlyName}): storageUnit "${storageUnit}" is compound; compound units are display-only` : `measurementField(${earlyName}): storageUnit "${storageUnit}" is not a known scalar unit`);
|
|
82
|
+
const dimension = engine.dimensionOf(storageUnit);
|
|
83
|
+
const preferenceKey = options.preferenceKey ?? dimension;
|
|
84
|
+
const name = options.name ?? preferenceKey;
|
|
85
|
+
const units = options.units ?? engine.unitsOfDimension(dimension);
|
|
86
|
+
const precision = options.precision;
|
|
87
|
+
if (!isValidPreferenceKey(preferenceKey)) throw new Error(`measurementField(${name}): preferenceKey "${preferenceKey}" must be a non-empty string with no whitespace or slashes`);
|
|
88
|
+
assertUnits({
|
|
89
|
+
dimension,
|
|
90
|
+
engine,
|
|
91
|
+
name,
|
|
92
|
+
units
|
|
93
|
+
});
|
|
94
|
+
if (fallbackUnit !== void 0 && !units.includes(fallbackUnit)) throw new Error(`measurementField(${name}): fallbackUnit "${fallbackUnit}" is not in this field's units (${units.join(", ")})`);
|
|
95
|
+
if (precision !== void 0) assertPrecision({
|
|
96
|
+
dimension,
|
|
97
|
+
engine,
|
|
98
|
+
name,
|
|
99
|
+
precision
|
|
100
|
+
});
|
|
101
|
+
const measurementOptions = {
|
|
102
|
+
dimension,
|
|
103
|
+
preferenceKey,
|
|
104
|
+
storageUnit,
|
|
105
|
+
units,
|
|
106
|
+
...localeDefaults !== void 0 ? { localeDefaults } : {},
|
|
107
|
+
...fallbackUnit !== void 0 ? { fallbackUnit } : {},
|
|
108
|
+
...precision !== void 0 ? { precision } : {},
|
|
109
|
+
...custom !== void 0 ? { custom } : {}
|
|
110
|
+
};
|
|
111
|
+
const base = {
|
|
112
|
+
name,
|
|
113
|
+
type: "number",
|
|
114
|
+
...label !== void 0 ? { label } : {},
|
|
115
|
+
...required !== void 0 ? { required } : {},
|
|
116
|
+
...localized !== void 0 ? { localized } : {},
|
|
117
|
+
...index !== void 0 ? { index } : {},
|
|
118
|
+
...min !== void 0 ? { min } : {},
|
|
119
|
+
...max !== void 0 ? { max } : {},
|
|
120
|
+
admin: { components: {
|
|
121
|
+
Cell: {
|
|
122
|
+
clientProps: { measurementOptions },
|
|
123
|
+
path: "@10x-media/fields/rsc#MeasurementCellServer"
|
|
124
|
+
},
|
|
125
|
+
Field: {
|
|
126
|
+
clientProps: { measurementOptions },
|
|
127
|
+
path: "@10x-media/fields/rsc#MeasurementFieldServer"
|
|
128
|
+
}
|
|
129
|
+
} },
|
|
130
|
+
custom: { [MEASUREMENT_CUSTOM_KEY]: measurementOptions },
|
|
131
|
+
hooks: { beforeValidate: [buildRoundStorageHook(precision)] },
|
|
132
|
+
validate: validateMeasurement
|
|
133
|
+
};
|
|
134
|
+
return typeof overrides === "function" ? overrides({ field: base }) : base;
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
137
|
+
export { measurementField };
|
|
138
|
+
|
|
139
|
+
//# sourceMappingURL=measurementField.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"measurementField.js","names":[],"sources":["../../../src/fields/measurement/measurementField.ts"],"sourcesContent":["import type { FieldHook, NumberField, NumberFieldSingleValidation } from 'payload'\nimport { number } from 'payload/shared'\nimport { roundTo } from './engine/convert'\nimport type { MeasurementPrecision, PrecisionMode } from './engine/precision'\nimport { createEngine, type MeasurementEngine } from './engine/registry'\nimport type { ScalarUnitId } from './engine/units'\nimport {\n\ttype AnyMeasurementFieldOptions,\n\tMEASUREMENT_CUSTOM_KEY,\n\ttype MeasurementClientOptions,\n\ttype MeasurementCustomFieldOptions,\n\ttype MeasurementFieldOptions,\n} from './options'\nimport { resolvePrecisionSafe } from './server/resolvePrecisionSafe'\n\n/**\n * Storage digits may come from the plugin registry, so this resolves per request\n * rather than once at field-build time. `fieldPrecision` is the field's own\n * (unresolved) layer, stamped once when the field was built.\n */\nconst buildRoundStorageHook = (\n\tfieldPrecision: MeasurementPrecision | PrecisionMode | undefined\n): FieldHook => {\n\treturn ({ req, value }) => {\n\t\tif (typeof value !== 'number' || Number.isNaN(value)) return value\n\t\tconst { storage } = resolvePrecisionSafe({ fieldPrecision, payload: req.payload })\n\t\treturn roundTo(value, storage)\n\t}\n}\n\n/**\n * Payload coerces string input through parseFloat before validation, and its\n * native number validation lets the resulting NaN through (typeof NaN is\n * 'number'). Mongoose happens to reject NaN at the driver; Postgres stores\n * NaN::numeric, which then sorts above every real value. Guard here, then\n * defer to the native validator (Payload spreads the field into options, so\n * min/max/required behave exactly as the default install).\n */\nconst validateMeasurement: NumberFieldSingleValidation = (value, options) => {\n\tif (typeof value === 'number' && Number.isNaN(value)) {\n\t\treturn options.req.t('validation:enterNumber')\n\t}\n\treturn number(value, options)\n}\n\nconst buildEngine = (options: AnyMeasurementFieldOptions, name: string): MeasurementEngine => {\n\ttry {\n\t\treturn createEngine(options.custom)\n\t} catch (error) {\n\t\tthrow new Error(`measurementField(${name}): ${(error as Error).message}`)\n\t}\n}\n\n/** One path segment, so it round-trips through the preferences document untouched. */\nconst isValidPreferenceKey = (key: string): boolean => key !== '' && !/[\\s/]/.test(key)\n\nconst assertUnits = (args: {\n\tengine: MeasurementEngine\n\tdimension: string\n\tname: string\n\tunits: readonly string[]\n}): void => {\n\tconst { dimension, engine, name, units } = args\n\tif (units.length === 0) {\n\t\tthrow new Error(`measurementField(${name}): units is empty, so no unit can be displayed`)\n\t}\n\tfor (const unit of units) {\n\t\tif (!engine.isScalarUnit(unit) && !engine.isCompoundUnit(unit)) {\n\t\t\tthrow new Error(`measurementField(${name}): unit \"${unit}\" is not a known unit`)\n\t\t}\n\t\tconst unitDimension = engine.dimensionOf(unit)\n\t\tif (unitDimension !== dimension) {\n\t\t\tthrow new Error(\n\t\t\t\t`measurementField(${name}): unit \"${unit}\" is ${unitDimension}, but this field stores ${dimension} (dimension mismatch)`\n\t\t\t)\n\t\t}\n\t}\n}\n\nconst isPrecisionMode = (mode: unknown): mode is PrecisionMode =>\n\tmode === 'readable' || mode === 'exact'\n\n/**\n * Validates the field's own (unresolved) precision layer at config time, so a bad\n * value fails fast rather than surfacing later from `resolvePrecision`. Storage\n * range is re-checked there too, once merged with the plugin registry's layer;\n * `display` digits stay factory-only, since only the factory has unit context.\n */\nconst assertPrecision = (args: {\n\tengine: MeasurementEngine\n\tdimension: string\n\tname: string\n\tprecision: MeasurementPrecision | PrecisionMode\n}): void => {\n\tconst { dimension, engine, name, precision } = args\n\tif (typeof precision === 'string') {\n\t\tif (!isPrecisionMode(precision)) {\n\t\t\tthrow new Error(\n\t\t\t\t`measurementField(${name}): precision \"${precision}\" must be \"readable\" or \"exact\"`\n\t\t\t)\n\t\t}\n\t\treturn\n\t}\n\tconst { display, draft, entry, mode, storage } = precision\n\tif (mode !== undefined && !isPrecisionMode(mode)) {\n\t\tthrow new Error(\n\t\t\t`measurementField(${name}): precision.mode \"${mode}\" must be \"readable\" or \"exact\"`\n\t\t)\n\t}\n\tif (entry !== undefined && entry !== 'quantize' && entry !== 'free') {\n\t\tthrow new Error(\n\t\t\t`measurementField(${name}): precision.entry \"${entry}\" must be \"quantize\" or \"free\"`\n\t\t)\n\t}\n\tif (draft !== undefined && draft !== 'display' && draft !== 'faithful') {\n\t\tthrow new Error(\n\t\t\t`measurementField(${name}): precision.draft \"${draft}\" must be \"display\" or \"faithful\"`\n\t\t)\n\t}\n\tif (\n\t\tstorage !== undefined &&\n\t\t(typeof storage !== 'number' || !Number.isInteger(storage) || storage < 0 || storage > 12)\n\t) {\n\t\tthrow new Error(\n\t\t\t`measurementField(${name}): precision.storage must be an integer between 0 and 12, got ${storage}`\n\t\t)\n\t}\n\tif (display === undefined) return\n\tfor (const [unit, digits] of Object.entries(display)) {\n\t\tif (!engine.isScalarUnit(unit) && !engine.isCompoundUnit(unit)) {\n\t\t\tthrow new Error(\n\t\t\t\t`measurementField(${name}): precision.display key \"${unit}\" is not a known unit`\n\t\t\t)\n\t\t}\n\t\tif (engine.dimensionOf(unit) !== dimension) {\n\t\t\tthrow new Error(\n\t\t\t\t`measurementField(${name}): precision.display key \"${unit}\" is not a unit of ${dimension}`\n\t\t\t)\n\t\t}\n\t\t// Intl.NumberFormat rejects out-of-range maximumFractionDigits at render\n\t\t// time; fail at config time instead.\n\t\tif (typeof digits !== 'number' || !Number.isInteger(digits) || digits < 0 || digits > 100) {\n\t\t\tthrow new Error(\n\t\t\t\t`measurementField(${name}): precision.display for \"${unit}\" must be an integer between 0 and 100, got ${digits}`\n\t\t\t)\n\t\t}\n\t}\n}\n\n/**\n * Number field storing a canonical value in `storageUnit` while each admin user\n * edits and reads in their preferred unit. Spread a preset for a curated bundle,\n * or declare `storageUnit`/`units`/`preferenceKey` directly. Validation is\n * Payload's native number validation plus a NaN guard (min/max in storage units);\n * the client converts at the edges.\n */\nexport function measurementField<S extends ScalarUnitId>(\n\toptions: MeasurementFieldOptions<S>\n): NumberField\nexport function measurementField(options: MeasurementCustomFieldOptions): NumberField\nexport function measurementField(options: AnyMeasurementFieldOptions): NumberField {\n\tconst {\n\t\tcustom,\n\t\tfallbackUnit,\n\t\tindex,\n\t\tlabel,\n\t\tlocaleDefaults,\n\t\tlocalized,\n\t\tmax,\n\t\tmin,\n\t\toverrides,\n\t\trequired,\n\t\tstorageUnit,\n\t} = options\n\t// The real field name defaults off the dimension, which needs a valid storage\n\t// unit; these two checks run before that, so they label themselves by hand.\n\tconst earlyName = options.name ?? options.preferenceKey ?? storageUnit\n\tconst engine = buildEngine(options, earlyName)\n\n\tif (!engine.isScalarUnit(storageUnit)) {\n\t\tthrow new Error(\n\t\t\tengine.isCompoundUnit(storageUnit)\n\t\t\t\t? `measurementField(${earlyName}): storageUnit \"${storageUnit}\" is compound; compound units are display-only`\n\t\t\t\t: `measurementField(${earlyName}): storageUnit \"${storageUnit}\" is not a known scalar unit`\n\t\t)\n\t}\n\n\tconst dimension = engine.dimensionOf(storageUnit)\n\tconst preferenceKey = options.preferenceKey ?? dimension\n\tconst name = options.name ?? preferenceKey\n\tconst units = options.units ?? engine.unitsOfDimension(dimension)\n\tconst precision = options.precision\n\n\tif (!isValidPreferenceKey(preferenceKey)) {\n\t\tthrow new Error(\n\t\t\t`measurementField(${name}): preferenceKey \"${preferenceKey}\" must be a non-empty string with no whitespace or slashes`\n\t\t)\n\t}\n\tassertUnits({ dimension, engine, name, units })\n\tif (fallbackUnit !== undefined && !units.includes(fallbackUnit)) {\n\t\tthrow new Error(\n\t\t\t`measurementField(${name}): fallbackUnit \"${fallbackUnit}\" is not in this field's units (${units.join(', ')})`\n\t\t)\n\t}\n\tif (precision !== undefined) assertPrecision({ dimension, engine, name, precision })\n\n\tconst measurementOptions: MeasurementClientOptions = {\n\t\tdimension,\n\t\tpreferenceKey,\n\t\tstorageUnit,\n\t\tunits,\n\t\t...(localeDefaults !== undefined ? { localeDefaults } : {}),\n\t\t...(fallbackUnit !== undefined ? { fallbackUnit } : {}),\n\t\t...(precision !== undefined ? { precision } : {}),\n\t\t...(custom !== undefined ? { custom } : {}),\n\t}\n\n\tconst base: NumberField = {\n\t\tname,\n\t\ttype: 'number',\n\t\t...(label !== undefined ? { label } : {}),\n\t\t...(required !== undefined ? { required } : {}),\n\t\t...(localized !== undefined ? { localized } : {}),\n\t\t...(index !== undefined ? { index } : {}),\n\t\t...(min !== undefined ? { min } : {}),\n\t\t...(max !== undefined ? { max } : {}),\n\t\tadmin: {\n\t\t\tcomponents: {\n\t\t\t\tCell: {\n\t\t\t\t\tclientProps: { measurementOptions },\n\t\t\t\t\tpath: '@10x-media/fields/rsc#MeasurementCellServer',\n\t\t\t\t},\n\t\t\t\tField: {\n\t\t\t\t\tclientProps: { measurementOptions },\n\t\t\t\t\tpath: '@10x-media/fields/rsc#MeasurementFieldServer',\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tcustom: { [MEASUREMENT_CUSTOM_KEY]: measurementOptions },\n\t\thooks: { beforeValidate: [buildRoundStorageHook(precision)] },\n\t\tvalidate: validateMeasurement,\n\t}\n\n\treturn typeof overrides === 'function' ? overrides({ field: base }) : base\n}\n"],"mappings":";;;;;;;;;;;AAoBA,MAAM,yBACL,mBACe;CACf,QAAQ,EAAE,KAAK,YAAY;EAC1B,IAAI,OAAO,UAAU,YAAY,OAAO,MAAM,KAAK,GAAG,OAAO;EAC7D,MAAM,EAAE,YAAY,qBAAqB;GAAE;GAAgB,SAAS,IAAI;EAAQ,CAAC;EACjF,OAAO,QAAQ,OAAO,OAAO;CAC9B;AACD;;;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MAAM;GACP;GACA,OAAO;IACN,aAAa,EAAE,mBAAmB;IAClC,MAAM;GACP;EACD,EACD;EACA,QAAQ,GAAG,yBAAyB,mBAAmB;EACvD,OAAO,EAAE,gBAAgB,CAAC,sBAAsB,SAAS,CAAC,EAAE;EAC5D,UAAU;CACX;CAEA,OAAO,OAAO,cAAc,aAAa,UAAU,EAAE,OAAO,KAAK,CAAC,IAAI;AACvE"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { MeasurementPrecision, PrecisionMode, ResolvedPrecision } from "./engine/precision.js";
|
|
2
|
+
import { DimOf, MeasurementUnitId, ScalarUnitId, UnitOfDimension } from "./engine/units.js";
|
|
3
|
+
import { MeasurementSystem } from "./engine/locale.js";
|
|
4
|
+
import { MeasurementCustomConfig } from "./engine/registry.js";
|
|
5
|
+
import { NumberField } from "payload";
|
|
6
|
+
|
|
7
|
+
//#region src/fields/measurement/options.d.ts
|
|
8
|
+
/** payload-preferences key holding the flat per-bucket unit map. */
|
|
9
|
+
declare const MEASUREMENT_PREFERENCE_KEY = "10x-fields-measurement";
|
|
10
|
+
/** Saved display units, keyed by each field's `preferenceKey`. */
|
|
11
|
+
type MeasurementUnitsPreference = Partial<Record<string, MeasurementUnitId>>;
|
|
12
|
+
/** `MeasurementPrecision` with `display` narrowed to a field's own unit union. */
|
|
13
|
+
type MeasurementPrecisionFor<U extends string> = Omit<MeasurementPrecision, 'display'> & {
|
|
14
|
+
display?: Partial<Record<U, number>>;
|
|
15
|
+
};
|
|
16
|
+
type CommonFieldOptions<U extends string> = {
|
|
17
|
+
name?: string;
|
|
18
|
+
label?: NumberField['label'];
|
|
19
|
+
required?: boolean;
|
|
20
|
+
localized?: boolean;
|
|
21
|
+
index?: boolean; /** Bounds in the storage unit, enforced by Payload's native number validation. */
|
|
22
|
+
min?: number;
|
|
23
|
+
max?: number; /** Units offered in the unit picker. Defaults to every unit of the storage unit's dimension. */
|
|
24
|
+
units?: readonly U[]; /** Preference bucket shared with every field using the same key. Defaults to the dimension. */
|
|
25
|
+
preferenceKey?: string; /** Display fallback for users with no saved preference, ahead of the plugin and locale defaults. */
|
|
26
|
+
fallbackUnit?: U; /** Locale-system defaults for this field, ahead of the per-dimension table. */
|
|
27
|
+
localeDefaults?: Partial<Record<MeasurementSystem, U>>;
|
|
28
|
+
/**
|
|
29
|
+
* `'readable'` (default) quantizes entry and repaints drafts from the display
|
|
30
|
+
* unit; `'exact'` keeps entry free and repaints from the stored value. Pass a
|
|
31
|
+
* `MeasurementPrecisionFor` object to override individual knobs, including
|
|
32
|
+
* per-unit display fraction digits via `display`.
|
|
33
|
+
*/
|
|
34
|
+
precision?: MeasurementPrecisionFor<U> | PrecisionMode;
|
|
35
|
+
overrides?: (args: {
|
|
36
|
+
field: NumberField;
|
|
37
|
+
}) => NumberField;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Built-in units only: `units`, `fallbackUnit`, `localeDefaults` and `precision`
|
|
41
|
+
* keys narrow to the storage unit's dimension.
|
|
42
|
+
*/
|
|
43
|
+
type MeasurementFieldOptions<S extends ScalarUnitId = ScalarUnitId> = CommonFieldOptions<UnitOfDimension<DimOf<S>>> & {
|
|
44
|
+
/** Unit the number is stored in. Match existing data to adopt with zero migration. */storageUnit: S;
|
|
45
|
+
custom?: undefined;
|
|
46
|
+
};
|
|
47
|
+
/** Custom units in play: ids are plain strings, so runtime validation carries the narrowing. */
|
|
48
|
+
type MeasurementCustomFieldOptions = CommonFieldOptions<string> & {
|
|
49
|
+
storageUnit: string;
|
|
50
|
+
custom: MeasurementCustomConfig;
|
|
51
|
+
};
|
|
52
|
+
type AnyMeasurementFieldOptions = MeasurementCustomFieldOptions | MeasurementFieldOptions;
|
|
53
|
+
/**
|
|
54
|
+
* Serializable options shipped to both the Field and Cell components. Unit ids stay
|
|
55
|
+
* widened here: the components rebuild the field's engine from `custom` and resolve
|
|
56
|
+
* every id through it, so a custom id is as valid as a built-in one.
|
|
57
|
+
*/
|
|
58
|
+
type MeasurementClientOptions = {
|
|
59
|
+
storageUnit: MeasurementUnitId;
|
|
60
|
+
units: readonly MeasurementUnitId[];
|
|
61
|
+
preferenceKey: string;
|
|
62
|
+
dimension: string;
|
|
63
|
+
localeDefaults?: Partial<Record<MeasurementSystem, MeasurementUnitId>>;
|
|
64
|
+
fallbackUnit?: MeasurementUnitId; /** The field-declared layer, unresolved: still needs merging with the plugin-registry default. */
|
|
65
|
+
precision?: MeasurementPrecision | PrecisionMode;
|
|
66
|
+
custom?: MeasurementCustomConfig;
|
|
67
|
+
};
|
|
68
|
+
/** What MeasurementFieldServer hands the client after per-request resolution. */
|
|
69
|
+
type MeasurementResolvedClientOptions = Omit<MeasurementClientOptions, 'precision'> & {
|
|
70
|
+
/** The viewer's saved unit for this bucket, read server-side for a flash-free first paint. */initialUnit?: MeasurementUnitId; /** Plugin-registry default for this bucket. */
|
|
71
|
+
registryDefault?: MeasurementUnitId; /** Merged against the plugin-registry default, so the client only ever sees a final answer. */
|
|
72
|
+
precision?: ResolvedPrecision;
|
|
73
|
+
};
|
|
74
|
+
//#endregion
|
|
75
|
+
export { AnyMeasurementFieldOptions, MEASUREMENT_PREFERENCE_KEY, MeasurementClientOptions, MeasurementCustomFieldOptions, MeasurementFieldOptions, MeasurementPrecisionFor, MeasurementResolvedClientOptions, MeasurementUnitsPreference };
|
|
76
|
+
//# sourceMappingURL=options.d.ts.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/fields/measurement/options.ts
|
|
2
|
+
/** payload-preferences key holding the flat per-bucket unit map. */
|
|
3
|
+
const MEASUREMENT_PREFERENCE_KEY = "10x-fields-measurement";
|
|
4
|
+
/** field.custom key the factory stamps its resolved config under, for tooling and secondary apps. */
|
|
5
|
+
const MEASUREMENT_CUSTOM_KEY = "@10x-media/fields";
|
|
6
|
+
//#endregion
|
|
7
|
+
export { MEASUREMENT_CUSTOM_KEY, MEASUREMENT_PREFERENCE_KEY };
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.js","names":[],"sources":["../../../src/fields/measurement/options.ts"],"sourcesContent":["import type { NumberField } from 'payload'\nimport type { MeasurementSystem } from './engine/locale'\nimport type { MeasurementPrecision, PrecisionMode, ResolvedPrecision } from './engine/precision'\nimport type { MeasurementCustomConfig } from './engine/registry'\nimport type { DimOf, MeasurementUnitId, ScalarUnitId, UnitOfDimension } from './engine/units'\n\n/** payload-preferences key holding the flat per-bucket unit map. */\nexport const MEASUREMENT_PREFERENCE_KEY = '10x-fields-measurement'\n\n/** field.custom key the factory stamps its resolved config under, for tooling and secondary apps. */\nexport const MEASUREMENT_CUSTOM_KEY = '@10x-media/fields'\n\n/** Saved display units, keyed by each field's `preferenceKey`. */\nexport type MeasurementUnitsPreference = Partial<Record<string, MeasurementUnitId>>\n\n/** `MeasurementPrecision` with `display` narrowed to a field's own unit union. */\nexport type MeasurementPrecisionFor<U extends string> = Omit<MeasurementPrecision, 'display'> & {\n\tdisplay?: Partial<Record<U, number>>\n}\n\ntype CommonFieldOptions<U extends string> = {\n\tname?: string\n\tlabel?: NumberField['label']\n\trequired?: boolean\n\tlocalized?: boolean\n\tindex?: boolean\n\t/** Bounds in the storage unit, enforced by Payload's native number validation. */\n\tmin?: number\n\tmax?: number\n\t/** Units offered in the unit picker. Defaults to every unit of the storage unit's dimension. */\n\tunits?: readonly U[]\n\t/** Preference bucket shared with every field using the same key. Defaults to the dimension. */\n\tpreferenceKey?: string\n\t/** Display fallback for users with no saved preference, ahead of the plugin and locale defaults. */\n\tfallbackUnit?: U\n\t/** Locale-system defaults for this field, ahead of the per-dimension table. */\n\tlocaleDefaults?: Partial<Record<MeasurementSystem, U>>\n\t/**\n\t * `'readable'` (default) quantizes entry and repaints drafts from the display\n\t * unit; `'exact'` keeps entry free and repaints from the stored value. Pass a\n\t * `MeasurementPrecisionFor` object to override individual knobs, including\n\t * per-unit display fraction digits via `display`.\n\t */\n\tprecision?: MeasurementPrecisionFor<U> | PrecisionMode\n\toverrides?: (args: { field: NumberField }) => NumberField\n}\n\n/**\n * Built-in units only: `units`, `fallbackUnit`, `localeDefaults` and `precision`\n * keys narrow to the storage unit's dimension.\n */\nexport type MeasurementFieldOptions<S extends ScalarUnitId = ScalarUnitId> = CommonFieldOptions<\n\tUnitOfDimension<DimOf<S>>\n> & {\n\t/** Unit the number is stored in. Match existing data to adopt with zero migration. */\n\tstorageUnit: S\n\tcustom?: undefined\n}\n\n/** Custom units in play: ids are plain strings, so runtime validation carries the narrowing. */\nexport type MeasurementCustomFieldOptions = CommonFieldOptions<string> & {\n\tstorageUnit: string\n\tcustom: MeasurementCustomConfig\n}\n\nexport type AnyMeasurementFieldOptions = MeasurementCustomFieldOptions | MeasurementFieldOptions\n\n/**\n * Serializable options shipped to both the Field and Cell components. Unit ids stay\n * widened here: the components rebuild the field's engine from `custom` and resolve\n * every id through it, so a custom id is as valid as a built-in one.\n */\nexport type MeasurementClientOptions = {\n\tstorageUnit: MeasurementUnitId\n\tunits: readonly MeasurementUnitId[]\n\tpreferenceKey: string\n\tdimension: string\n\tlocaleDefaults?: Partial<Record<MeasurementSystem, MeasurementUnitId>>\n\tfallbackUnit?: MeasurementUnitId\n\t/** The field-declared layer, unresolved: still needs merging with the plugin-registry default. */\n\tprecision?: MeasurementPrecision | PrecisionMode\n\tcustom?: MeasurementCustomConfig\n}\n\n/** What MeasurementFieldServer hands the client after per-request resolution. */\nexport type MeasurementResolvedClientOptions = Omit<MeasurementClientOptions, 'precision'> & {\n\t/** The viewer's saved unit for this bucket, read server-side for a flash-free first paint. */\n\tinitialUnit?: MeasurementUnitId\n\t/** Plugin-registry default for this bucket. */\n\tregistryDefault?: MeasurementUnitId\n\t/** Merged against the plugin-registry default, so the client only ever sees a final answer. */\n\tprecision?: ResolvedPrecision\n}\n"],"mappings":";;AAOA,MAAa,6BAA6B;;AAG1C,MAAa,yBAAyB"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { MeasurementPrecision } from "./engine/precision.js";
|
|
2
|
+
import { ScalarUnitId, UnitId } from "./engine/units.js";
|
|
3
|
+
import { MeasurementSystem } from "./engine/locale.js";
|
|
4
|
+
|
|
5
|
+
//#region src/fields/measurement/presets.d.ts
|
|
6
|
+
/** Spreadable bundle of measurementField options: storage, offered units, preference bucket, name. */
|
|
7
|
+
type MeasurementPreset = {
|
|
8
|
+
storageUnit: ScalarUnitId;
|
|
9
|
+
units: readonly UnitId[]; /** Preference bucket every field spreading this preset shares. */
|
|
10
|
+
preferenceKey: string;
|
|
11
|
+
name: string;
|
|
12
|
+
localeDefaults: Partial<Record<MeasurementSystem, UnitId>>;
|
|
13
|
+
precision?: MeasurementPrecision;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Curated starting points, spread into measurementField() and overridden per field.
|
|
17
|
+
* The preference keys are data: renaming one orphans every unit a user already saved.
|
|
18
|
+
*/
|
|
19
|
+
declare const presets: {
|
|
20
|
+
readonly bodyWeight: {
|
|
21
|
+
readonly storageUnit: "kg";
|
|
22
|
+
readonly units: readonly ["kg", "lb", "st-lb"];
|
|
23
|
+
readonly preferenceKey: "bodyWeight";
|
|
24
|
+
readonly name: "weight";
|
|
25
|
+
readonly localeDefaults: {
|
|
26
|
+
readonly metric: "kg";
|
|
27
|
+
readonly us: "lb";
|
|
28
|
+
readonly uk: "st-lb";
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
readonly personHeight: {
|
|
32
|
+
readonly storageUnit: "cm";
|
|
33
|
+
readonly units: readonly ["cm", "m", "in", "ft-in"];
|
|
34
|
+
readonly preferenceKey: "personHeight";
|
|
35
|
+
readonly name: "height";
|
|
36
|
+
readonly localeDefaults: {
|
|
37
|
+
readonly metric: "cm";
|
|
38
|
+
readonly us: "ft-in";
|
|
39
|
+
readonly uk: "ft-in";
|
|
40
|
+
};
|
|
41
|
+
readonly precision: {
|
|
42
|
+
readonly display: {
|
|
43
|
+
readonly cm: 1;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
readonly distance: {
|
|
48
|
+
readonly storageUnit: "km";
|
|
49
|
+
readonly units: readonly ["m", "km", "mi"];
|
|
50
|
+
readonly preferenceKey: "distance";
|
|
51
|
+
readonly name: "distance";
|
|
52
|
+
readonly localeDefaults: {
|
|
53
|
+
readonly metric: "km";
|
|
54
|
+
readonly us: "mi";
|
|
55
|
+
readonly uk: "mi";
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
readonly mass: {
|
|
59
|
+
readonly storageUnit: "kg";
|
|
60
|
+
readonly units: readonly ["g", "kg", "oz", "lb"];
|
|
61
|
+
readonly preferenceKey: "mass";
|
|
62
|
+
readonly name: "mass";
|
|
63
|
+
readonly localeDefaults: {
|
|
64
|
+
readonly metric: "kg";
|
|
65
|
+
readonly us: "lb";
|
|
66
|
+
readonly uk: "lb";
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
readonly length: {
|
|
70
|
+
readonly storageUnit: "cm";
|
|
71
|
+
readonly units: readonly ["mm", "cm", "m", "km", "in", "ft", "yd", "mi"];
|
|
72
|
+
readonly preferenceKey: "length";
|
|
73
|
+
readonly name: "length";
|
|
74
|
+
readonly localeDefaults: {
|
|
75
|
+
readonly metric: "cm";
|
|
76
|
+
readonly us: "in";
|
|
77
|
+
readonly uk: "in";
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
readonly volume: {
|
|
81
|
+
readonly storageUnit: "l";
|
|
82
|
+
readonly units: readonly ["ml", "l", "fl-oz", "gal"];
|
|
83
|
+
readonly preferenceKey: "volume";
|
|
84
|
+
readonly name: "volume";
|
|
85
|
+
readonly localeDefaults: {
|
|
86
|
+
readonly metric: "l";
|
|
87
|
+
readonly us: "fl-oz";
|
|
88
|
+
readonly uk: "l";
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
readonly temperature: {
|
|
92
|
+
readonly storageUnit: "c";
|
|
93
|
+
readonly units: readonly ["c", "f"];
|
|
94
|
+
readonly preferenceKey: "temperature";
|
|
95
|
+
readonly name: "temperature";
|
|
96
|
+
readonly localeDefaults: {
|
|
97
|
+
readonly metric: "c";
|
|
98
|
+
readonly us: "f";
|
|
99
|
+
readonly uk: "c";
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
readonly speed: {
|
|
103
|
+
readonly storageUnit: "km/h";
|
|
104
|
+
readonly units: readonly ["km/h", "mph", "m/s"];
|
|
105
|
+
readonly preferenceKey: "speed";
|
|
106
|
+
readonly name: "speed";
|
|
107
|
+
readonly localeDefaults: {
|
|
108
|
+
readonly metric: "km/h";
|
|
109
|
+
readonly us: "mph";
|
|
110
|
+
readonly uk: "mph";
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
type MeasurementUsage = keyof typeof presets;
|
|
115
|
+
//#endregion
|
|
116
|
+
export { MeasurementPreset, MeasurementUsage, presets };
|
|
117
|
+
//# sourceMappingURL=presets.d.ts.map
|