@10x-media/fields 4.0.0-alpha.1 → 4.0.0-alpha.2

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @10x-media/fields
2
2
 
3
+ ## 4.0.0-alpha.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Measurement: an empty major part next to a typed minor part of a compound unit now reads as zero, so inches typed into an empty `ft in` field (or pounds into an empty `st lb` field) commit on blur. `5` in the inches input saves as `0 ft 5 in` instead of being dropped while the form turned dirty. Clearing the feet of a stored value while its inches are still shown likewise keeps those inches. Clearing both parts still clears the value.
8
+
3
9
  ## 4.0.0-alpha.1
4
10
 
5
11
  ### Major Changes
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  Reusable fields for Payload that look and behave native: a full color picker in a constant-height row, a searchable icon picker over pluggable icon libraries, encrypted fields with key rotation and opt-in exact-match querying, and a unit-aware measurement field. Built from `@payloadcms/ui` primitives on Payload design tokens, indistinguishable from Payload's own fields.
11
11
 
12
- [![npm](https://img.shields.io/npm/v/@10x-media/fields?style=flat-square)](https://www.npmjs.com/package/@10x-media/fields)
12
+ [![npm](https://img.shields.io/npm/v/@10x-media/fields/alpha?style=flat-square)](https://www.npmjs.com/package/@10x-media/fields)
13
13
 
14
14
  Part of the [@10x-media Payload plugins](https://github.com/10x-media/payload-plugins) collection. In alpha on this branch: published under the `alpha` dist-tag until Payload 4 is stable.
15
15
 
@@ -8,6 +8,15 @@ const parseDraft = (raw) => {
8
8
  const parsed = Number(raw);
9
9
  return Number.isFinite(parsed) ? parsed : null;
10
10
  };
11
+ /**
12
+ * The major part of a compound draft. An empty major beside a minor that holds a number
13
+ * reads as zero (`'' ft 5 in` is 5 in), so typing only the minor part commits; with both
14
+ * parts empty, or an unparseable major, there is nothing to commit.
15
+ */
16
+ const parseMajorDraft = (drafts) => {
17
+ if (drafts.primary.trim() === "" && parseDraft(drafts.minor) !== null) return 0;
18
+ return parseDraft(drafts.primary);
19
+ };
11
20
  const ALL_DIRTY = {
12
21
  primary: true,
13
22
  minor: true
@@ -22,7 +31,7 @@ const commitDrafts = (drafts, opts) => {
22
31
  const exact = engine.decompose(storedValue, storageUnit, displayUnit, storageDigits);
23
32
  const exactTotal = exact.major * compound.ratio + exact.minor;
24
33
  if (dirty.primary) {
25
- let typedMajor = parseDraft(drafts.primary);
34
+ let typedMajor = parseMajorDraft(drafts);
26
35
  if (typedMajor === null) return null;
27
36
  if (entry === "quantize") typedMajor = roundTo(typedMajor, engine.precisionFor(compound.major, precision));
28
37
  const paintedMajor = parseDraft(paintedDrafts.primary) ?? exact.major;
@@ -42,9 +51,9 @@ const commitDrafts = (drafts, opts) => {
42
51
  const stored = hasStored ? engine.decompose(storedValue, storageUnit, displayUnit, storageDigits) : null;
43
52
  let major;
44
53
  if (dirty.primary) {
45
- major = parseDraft(drafts.primary);
54
+ major = parseMajorDraft(drafts);
46
55
  if (major !== null && entry === "quantize") major = roundTo(major, engine.precisionFor(compound.major, precision));
47
- } else major = stored ? stored.major : parseDraft(drafts.primary);
56
+ } else major = stored ? stored.major : parseMajorDraft(drafts);
48
57
  if (major === null) return null;
49
58
  let minor;
50
59
  if (dirty.minor) {
@@ -1 +1 @@
1
- {"version":3,"file":"editModel.js","names":[],"sources":["../../../../src/fields/measurement/client/editModel.ts"],"sourcesContent":["import { roundTo } from '../engine/convert'\nimport type { MeasurementEngine } from '../engine/registry'\nimport { COMPOUNDS, type MeasurementUnitId, STORAGE_FRACTION_DIGITS } from '../engine/units'\n\n/** Re-exported so MeasurementField/MeasurementCell keep one import site for the edit model. */\nexport { resolveDisplayUnit } from '../engine/locale'\n\nexport type MeasurementDrafts = { primary: string; minor: string }\n\n/** Which inputs the viewer has actually typed into since the last resync. */\nexport type DirtyDrafts = { primary: boolean; minor: boolean }\n\ntype UnitContext = {\n\t/** Bound over the field's custom units, so custom ids convert and format like built-ins. */\n\tengine: MeasurementEngine\n\tstorageUnit: MeasurementUnitId\n\tdisplayUnit: MeasurementUnitId\n\tprecision?: Partial<Record<MeasurementUnitId, number>>\n\t/**\n\t * Draft-repaint policy: 'faithful' (default) escalates fraction digits until the\n\t * draft round-trips to the stored value; 'display' renders straight at display\n\t * digits, so a stored value can print shorter than it is stored.\n\t */\n\tdraft?: 'display' | 'faithful'\n\t/** Fraction digits kept in the storage unit. Defaults to STORAGE_FRACTION_DIGITS. */\n\tstorageDigits?: number\n}\n\ntype CommitOptions = UnitContext & {\n\t/**\n\t * Entry-unit typing behavior: 'quantize' rounds a dirty part at its own display\n\t * digits, in the unit it was typed in, before converting; 'free' (default) keeps\n\t * the typed value exact.\n\t */\n\tentry?: 'quantize' | 'free'\n\t/** An untouched part never commits its displayed string; only a dirty one does. */\n\tdirty?: DirtyDrafts\n\t/** Current stored value, used to derive an untouched part's exact contribution. */\n\tstoredValue?: number | null\n\t/**\n\t * The compound drafts as painted on screen immediately before this edit (not the\n\t * post-keystroke `drafts`). A display-digit repaint can carry (182.5 cm storage\n\t * splits exactly to 5 ft 11.850394 in but paints as 6 ft 0 in), so when exactly\n\t * one part is dirty, the edited part's contribution is computed as a delta off\n\t * this baseline rather than a full replace, which would otherwise bake the\n\t * painted carry into storage.\n\t */\n\tpaintedDrafts?: MeasurementDrafts\n}\n\nconst numberToDraft = (value: number): string => String(value)\n\nconst parseDraft = (raw: string): number | null => {\n\tif (raw.trim() === '') return null\n\tconst parsed = Number(raw)\n\treturn Number.isFinite(parsed) ? parsed : null\n}\n\n// draftsFor/commitDrafts default to 'faithful'/'free'/ALL_DIRTY when a caller omits\n// them, i.e. the shape of 'exact' mode, not the engine's own 'readable' default: the\n// component always threads its resolved precision explicitly, so these bare defaults\n// exist only for a caller invoking editModel directly (tests, or an embedder outside\n// the component), and preserve the single behavior this module had before precision\n// modes existed.\nconst ALL_DIRTY: DirtyDrafts = { primary: true, minor: true }\n\nexport const commitDrafts = (drafts: MeasurementDrafts, opts: CommitOptions): number | null => {\n\tconst {\n\t\tdisplayUnit,\n\t\tdirty = ALL_DIRTY,\n\t\tengine,\n\t\tentry = 'free',\n\t\tpaintedDrafts,\n\t\tprecision,\n\t\tstorageDigits = STORAGE_FRACTION_DIGITS,\n\t\tstorageUnit,\n\t\tstoredValue = null,\n\t} = opts\n\n\tif (engine.isCompoundUnit(displayUnit)) {\n\t\tconst compound = COMPOUNDS[displayUnit]\n\t\tconst hasStored = typeof storedValue === 'number'\n\t\tconst dirtyCount = (dirty.primary ? 1 : 0) + (dirty.minor ? 1 : 0)\n\n\t\t// Delta path: exactly one part was touched and a painted baseline exists, so\n\t\t// the untouched part's contribution comes entirely from the exact stored\n\t\t// total (never its carried, display-precision draft), and the touched part\n\t\t// contributes only the change the viewer actually typed, in minor units.\n\t\tif (hasStored && dirtyCount === 1 && paintedDrafts) {\n\t\t\tconst exact = engine.decompose(storedValue as number, storageUnit, displayUnit, storageDigits)\n\t\t\tconst exactTotal = exact.major * compound.ratio + exact.minor\n\n\t\t\tif (dirty.primary) {\n\t\t\t\tlet typedMajor = parseDraft(drafts.primary)\n\t\t\t\tif (typedMajor === null) return null\n\t\t\t\tif (entry === 'quantize') {\n\t\t\t\t\ttypedMajor = roundTo(typedMajor, engine.precisionFor(compound.major, precision))\n\t\t\t\t}\n\t\t\t\tconst paintedMajor = parseDraft(paintedDrafts.primary) ?? exact.major\n\t\t\t\tconst newTotal = exactTotal + (typedMajor - paintedMajor) * compound.ratio\n\t\t\t\treturn roundTo(\n\t\t\t\t\tengine.compose({ major: 0, minor: newTotal }, displayUnit, storageUnit),\n\t\t\t\t\tstorageDigits\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tconst rawMinor = parseDraft(drafts.minor) ?? 0\n\t\t\tconst typedMinor =\n\t\t\t\tentry === 'quantize'\n\t\t\t\t\t? roundTo(rawMinor, engine.precisionFor(displayUnit, precision))\n\t\t\t\t\t: rawMinor\n\t\t\tconst paintedMinor = parseDraft(paintedDrafts.minor) ?? 0\n\t\t\tconst newTotal = exactTotal + (typedMinor - paintedMinor)\n\t\t\treturn roundTo(\n\t\t\t\tengine.compose({ major: 0, minor: newTotal }, displayUnit, storageUnit),\n\t\t\t\tstorageDigits\n\t\t\t)\n\t\t}\n\n\t\t// Both parts dirty (an explicit full replace) or no baseline to diff\n\t\t// against: compose the two parts directly, as before.\n\t\tconst stored = hasStored\n\t\t\t? engine.decompose(storedValue as number, storageUnit, displayUnit, storageDigits)\n\t\t\t: null\n\n\t\tlet major: number | null\n\t\tif (dirty.primary) {\n\t\t\tmajor = parseDraft(drafts.primary)\n\t\t\tif (major !== null && entry === 'quantize') {\n\t\t\t\tmajor = roundTo(major, engine.precisionFor(compound.major, precision))\n\t\t\t}\n\t\t} else {\n\t\t\tmajor = stored ? stored.major : parseDraft(drafts.primary)\n\t\t}\n\t\tif (major === null) return null\n\n\t\tlet minor: number\n\t\tif (dirty.minor) {\n\t\t\tconst rawMinor = parseDraft(drafts.minor) ?? 0\n\t\t\tconst maxMinor = compound.ratio - 0.001\n\t\t\t// Clamp the raw typed magnitude (bounds a literal 24) before quantizing:\n\t\t\t// a quantized value landing exactly on the ratio (11.96 -> 12) must be\n\t\t\t// free to carry into the major via compose, matching decompose's own\n\t\t\t// carry contract, so nothing clamps it back down afterward.\n\t\t\tconst clampedRaw = Math.sign(rawMinor) * Math.min(Math.abs(rawMinor), maxMinor)\n\t\t\tminor =\n\t\t\t\tentry === 'quantize'\n\t\t\t\t\t? roundTo(clampedRaw, engine.precisionFor(displayUnit, precision))\n\t\t\t\t\t: clampedRaw\n\t\t} else {\n\t\t\t// No stored value to derive an exact minor from (a brand new, never-saved\n\t\t\t// field): fall back to the painted baseline, matching major's own fallback,\n\t\t\t// never a bare 0 (a cleared-then-retyped major would otherwise commit with\n\t\t\t// its minor silently zeroed).\n\t\t\tminor = stored ? stored.minor : (parseDraft(paintedDrafts?.minor ?? '') ?? 0)\n\t\t}\n\n\t\treturn roundTo(engine.compose({ major, minor }, displayUnit, storageUnit), storageDigits)\n\t}\n\n\tif (!dirty.primary) return storedValue\n\tconst parsed = parseDraft(drafts.primary)\n\tif (parsed === null) return null\n\tconst value =\n\t\tentry === 'quantize' ? roundTo(parsed, engine.precisionFor(displayUnit, precision)) : parsed\n\treturn roundTo(engine.convert(value, displayUnit, storageUnit), storageDigits)\n}\n\n/**\n * Drafts for repainting from a stored value. 'faithful' (default) is the shortest\n * draft that commits back to the stored value: display precision first, then one\n * more fraction digit at a time up to storage precision, since a draft that\n * silently truncates the stored value would write the truncation back on the next\n * save. 'display' skips escalation and renders straight at display digits\n * (compound parts each at their own digits), trading faithfulness for a value that\n * always matches what the unit picker promises.\n */\nexport const draftsFor = (value: number | null, opts: UnitContext): MeasurementDrafts => {\n\tif (typeof value !== 'number' || Number.isNaN(value)) return { minor: '', primary: '' }\n\tconst {\n\t\tdisplayUnit,\n\t\tdraft = 'faithful',\n\t\tengine,\n\t\tprecision,\n\t\tstorageDigits = STORAGE_FRACTION_DIGITS,\n\t\tstorageUnit,\n\t} = opts\n\tconst draftAt = (digits: number): MeasurementDrafts => {\n\t\tif (engine.isCompoundUnit(displayUnit)) {\n\t\t\tconst parts = engine.decompose(value, storageUnit, displayUnit, digits)\n\t\t\treturn { minor: numberToDraft(parts.minor), primary: numberToDraft(parts.major) }\n\t\t}\n\t\tconst converted = roundTo(engine.convert(value, storageUnit, displayUnit), digits)\n\t\treturn { minor: '', primary: numberToDraft(converted) }\n\t}\n\n\tconst displayDigits = engine.precisionFor(displayUnit, precision)\n\tif (draft === 'display') return draftAt(displayDigits)\n\n\t// Digits past storage precision never survive a save, so that is where escalation stops.\n\tconst maxDigits = Math.max(displayDigits, storageDigits)\n\tconst target = roundTo(value, storageDigits)\n\t// A bare probe: never forward `opts` itself, so an entry/dirty/storedValue field\n\t// a future CommitOptions caller happens to carry can never poison this check.\n\tconst probeOpts: UnitContext = { displayUnit, engine, precision, storageDigits, storageUnit }\n\tfor (let digits = displayDigits; digits < maxDigits; digits++) {\n\t\tconst candidate = dra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AAG,OAAO;EAAE,OAAO;EAAI,SAAS;CAAG;CACtF,MAAM,EACL,aACA,QAAQ,YACR,QACA,WACA,gBAAA,GACA,gBACG;CACJ,MAAM,WAAW,WAAsC;EACtD,IAAI,OAAO,eAAe,WAAW,GAAG;GACvC,MAAM,QAAQ,OAAO,UAAU,OAAO,aAAa,aAAa,MAAM;GACtE,OAAO;IAAE,OAAO,cAAc,MAAM,KAAK;IAAG,SAAS,cAAc,MAAM,KAAK;GAAE;EACjF;EAEA,OAAO;GAAE,OAAO;GAAI,SAAS,cADX,QAAQ,OAAO,QAAQ,OAAO,aAAa,WAAW,GAAG,MACxB,CAAC;EAAE;CACvD;CAEA,MAAM,gBAAgB,OAAO,aAAa,aAAa,SAAS;CAChE,IAAI,UAAU,WAAW,OAAO,QAAQ,aAAa;CAGrD,MAAM,YAAY,KAAK,IAAI,eAAe,aAAa;CACvD,MAAM,SAAS,QAAQ,OAAO,aAAa;CAG3C,MAAM,YAAyB;EAAE;EAAa;EAAQ;EAAW;EAAe;CAAY;CAC5F,KAAK,IAAI,SAAS,eAAe,SAAS,WAAW,UAAU;EAC9D,MAAM,YAAY,QAAQ,MAAM;EAChC,IAAI,aAAa,WAAW,SAAS,MAAM,QAAQ,OAAO;CAC3D;CACA,OAAO,QAAQ,SAAS;AACzB"}
1
+ {"version":3,"file":"editModel.js","names":[],"sources":["../../../../src/fields/measurement/client/editModel.ts"],"sourcesContent":["import { roundTo } from '../engine/convert'\nimport type { MeasurementEngine } from '../engine/registry'\nimport { COMPOUNDS, type MeasurementUnitId, STORAGE_FRACTION_DIGITS } from '../engine/units'\n\n/** Re-exported so MeasurementField/MeasurementCell keep one import site for the edit model. */\nexport { resolveDisplayUnit } from '../engine/locale'\n\nexport type MeasurementDrafts = { primary: string; minor: string }\n\n/** Which inputs the viewer has actually typed into since the last resync. */\nexport type DirtyDrafts = { primary: boolean; minor: boolean }\n\ntype UnitContext = {\n\t/** Bound over the field's custom units, so custom ids convert and format like built-ins. */\n\tengine: MeasurementEngine\n\tstorageUnit: MeasurementUnitId\n\tdisplayUnit: MeasurementUnitId\n\tprecision?: Partial<Record<MeasurementUnitId, number>>\n\t/**\n\t * Draft-repaint policy: 'faithful' (default) escalates fraction digits until the\n\t * draft round-trips to the stored value; 'display' renders straight at display\n\t * digits, so a stored value can print shorter than it is stored.\n\t */\n\tdraft?: 'display' | 'faithful'\n\t/** Fraction digits kept in the storage unit. Defaults to STORAGE_FRACTION_DIGITS. */\n\tstorageDigits?: number\n}\n\ntype CommitOptions = UnitContext & {\n\t/**\n\t * Entry-unit typing behavior: 'quantize' rounds a dirty part at its own display\n\t * digits, in the unit it was typed in, before converting; 'free' (default) keeps\n\t * the typed value exact.\n\t */\n\tentry?: 'quantize' | 'free'\n\t/** An untouched part never commits its displayed string; only a dirty one does. */\n\tdirty?: DirtyDrafts\n\t/** Current stored value, used to derive an untouched part's exact contribution. */\n\tstoredValue?: number | null\n\t/**\n\t * The compound drafts as painted on screen immediately before this edit (not the\n\t * post-keystroke `drafts`). A display-digit repaint can carry (182.5 cm storage\n\t * splits exactly to 5 ft 11.850394 in but paints as 6 ft 0 in), so when exactly\n\t * one part is dirty, the edited part's contribution is computed as a delta off\n\t * this baseline rather than a full replace, which would otherwise bake the\n\t * painted carry into storage.\n\t */\n\tpaintedDrafts?: MeasurementDrafts\n}\n\nconst numberToDraft = (value: number): string => String(value)\n\nconst parseDraft = (raw: string): number | null => {\n\tif (raw.trim() === '') return null\n\tconst parsed = Number(raw)\n\treturn Number.isFinite(parsed) ? parsed : null\n}\n\n/**\n * The major part of a compound draft. An empty major beside a minor that holds a number\n * reads as zero (`'' ft 5 in` is 5 in), so typing only the minor part commits; with both\n * parts empty, or an unparseable major, there is nothing to commit.\n */\nconst parseMajorDraft = (drafts: MeasurementDrafts): number | null => {\n\tif (drafts.primary.trim() === '' && parseDraft(drafts.minor) !== null) return 0\n\treturn parseDraft(drafts.primary)\n}\n\n// draftsFor/commitDrafts default to 'faithful'/'free'/ALL_DIRTY when a caller omits\n// them, i.e. the shape of 'exact' mode, not the engine's own 'readable' default: the\n// component always threads its resolved precision explicitly, so these bare defaults\n// exist only for a caller invoking editModel directly (tests, or an embedder outside\n// the component), and preserve the single behavior this module had before precision\n// modes existed.\nconst ALL_DIRTY: DirtyDrafts = { primary: true, minor: true }\n\nexport const commitDrafts = (drafts: MeasurementDrafts, opts: CommitOptions): number | null => {\n\tconst {\n\t\tdisplayUnit,\n\t\tdirty = ALL_DIRTY,\n\t\tengine,\n\t\tentry = 'free',\n\t\tpaintedDrafts,\n\t\tprecision,\n\t\tstorageDigits = STORAGE_FRACTION_DIGITS,\n\t\tstorageUnit,\n\t\tstoredValue = null,\n\t} = opts\n\n\tif (engine.isCompoundUnit(displayUnit)) {\n\t\tconst compound = COMPOUNDS[displayUnit]\n\t\tconst hasStored = typeof storedValue === 'number'\n\t\tconst dirtyCount = (dirty.primary ? 1 : 0) + (dirty.minor ? 1 : 0)\n\n\t\t// Delta path: exactly one part was touched and a painted baseline exists, so\n\t\t// the untouched part's contribution comes entirely from the exact stored\n\t\t// total (never its carried, display-precision draft), and the touched part\n\t\t// contributes only the change the viewer actually typed, in minor units.\n\t\tif (hasStored && dirtyCount === 1 && paintedDrafts) {\n\t\t\tconst exact = engine.decompose(storedValue as number, storageUnit, displayUnit, storageDigits)\n\t\t\tconst exactTotal = exact.major * compound.ratio + exact.minor\n\n\t\t\tif (dirty.primary) {\n\t\t\t\tlet typedMajor = parseMajorDraft(drafts)\n\t\t\t\tif (typedMajor === null) return null\n\t\t\t\tif (entry === 'quantize') {\n\t\t\t\t\ttypedMajor = roundTo(typedMajor, engine.precisionFor(compound.major, precision))\n\t\t\t\t}\n\t\t\t\tconst paintedMajor = parseDraft(paintedDrafts.primary) ?? exact.major\n\t\t\t\tconst newTotal = exactTotal + (typedMajor - paintedMajor) * compound.ratio\n\t\t\t\treturn roundTo(\n\t\t\t\t\tengine.compose({ major: 0, minor: newTotal }, displayUnit, storageUnit),\n\t\t\t\t\tstorageDigits\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tconst rawMinor = parseDraft(drafts.minor) ?? 0\n\t\t\tconst typedMinor =\n\t\t\t\tentry === 'quantize'\n\t\t\t\t\t? roundTo(rawMinor, engine.precisionFor(displayUnit, precision))\n\t\t\t\t\t: rawMinor\n\t\t\tconst paintedMinor = parseDraft(paintedDrafts.minor) ?? 0\n\t\t\tconst newTotal = exactTotal + (typedMinor - paintedMinor)\n\t\t\treturn roundTo(\n\t\t\t\tengine.compose({ major: 0, minor: newTotal }, displayUnit, storageUnit),\n\t\t\t\tstorageDigits\n\t\t\t)\n\t\t}\n\n\t\t// Both parts dirty (an explicit full replace) or no baseline to diff\n\t\t// against: compose the two parts directly, as before.\n\t\tconst stored = hasStored\n\t\t\t? engine.decompose(storedValue as number, storageUnit, displayUnit, storageDigits)\n\t\t\t: null\n\n\t\tlet major: number | null\n\t\tif (dirty.primary) {\n\t\t\tmajor = parseMajorDraft(drafts)\n\t\t\tif (major !== null && entry === 'quantize') {\n\t\t\t\tmajor = roundTo(major, engine.precisionFor(compound.major, precision))\n\t\t\t}\n\t\t} else {\n\t\t\tmajor = stored ? stored.major : parseMajorDraft(drafts)\n\t\t}\n\t\tif (major === null) return null\n\n\t\tlet minor: number\n\t\tif (dirty.minor) {\n\t\t\tconst rawMinor = parseDraft(drafts.minor) ?? 0\n\t\t\tconst maxMinor = compound.ratio - 0.001\n\t\t\t// Clamp the raw typed magnitude (bounds a literal 24) before quantizing:\n\t\t\t// a quantized value landing exactly on the ratio (11.96 -> 12) must be\n\t\t\t// free to carry into the major via compose, matching decompose's own\n\t\t\t// carry contract, so nothing clamps it back down afterward.\n\t\t\tconst clampedRaw = Math.sign(rawMinor) * Math.min(Math.abs(rawMinor), maxMinor)\n\t\t\tminor =\n\t\t\t\tentry === 'quantize'\n\t\t\t\t\t? roundTo(clampedRaw, engine.precisionFor(displayUnit, precision))\n\t\t\t\t\t: clampedRaw\n\t\t} else {\n\t\t\t// No stored value to derive an exact minor from (a brand new, never-saved\n\t\t\t// field): fall back to the painted baseline, matching major's own fallback,\n\t\t\t// never a bare 0 (a cleared-then-retyped major would otherwise commit with\n\t\t\t// its minor silently zeroed).\n\t\t\tminor = stored ? stored.minor : (parseDraft(paintedDrafts?.minor ?? '') ?? 0)\n\t\t}\n\n\t\treturn roundTo(engine.compose({ major, minor }, displayUnit, storageUnit), storageDigits)\n\t}\n\n\tif (!dirty.primary) return storedValue\n\tconst parsed = parseDraft(drafts.primary)\n\tif (parsed === null) return null\n\tconst value =\n\t\tentry === 'quantize' ? roundTo(parsed, engine.precisionFor(displayUnit, precision)) : parsed\n\treturn roundTo(engine.convert(value, displayUnit, storageUnit), storageDigits)\n}\n\n/**\n * Drafts for repainting from a stored value. 'faithful' (default) is the shortest\n * draft that commits back to the stored value: display precision first, then one\n * more fraction digit at a time up to storage precision, since a draft that\n * silently truncates the stored value would write the truncation back on the next\n * save. 'display' skips escalation and renders straight at display digits\n * (compound parts each at their own digits), trading faithfulness for a value that\n * always matches what the unit picker promises.\n */\nexport const draftsFor = (value: number | null, opts: UnitContext): MeasurementDrafts => {\n\tif (typeof value !== 'number' || Number.isNaN(value)) return { minor: '', primary: '' }\n\tconst {\n\t\tdisplayUnit,\n\t\tdraft = 'faithful',\n\t\tengine,\n\t\tprecision,\n\t\tstorageDigits = STORAGE_FRACTION_DIGITS,\n\t\tstorageUnit,\n\t} = opts\n\tconst draftAt = (digits: number): MeasurementDrafts => {\n\t\tif (engine.isCompoundUnit(displayUnit)) {\n\t\t\tconst parts = engine.decompose(value, storageUnit, displayUnit, digits)\n\t\t\treturn { minor: numberToDraft(parts.minor), primary: numberToDraft(parts.major) }\n\t\t}\n\t\tconst converted = roundTo(engine.convert(value, storageUnit, displayUnit), digits)\n\t\treturn { minor: '', primary: numberToDraft(converted) }\n\t}\n\n\tconst displayDigits = engine.precisionFor(displayUnit, precision)\n\tif (draft === 'display') return draftAt(displayDigits)\n\n\t// Digits past storage precision never survive a save, so that is where escalation stops.\n\tconst maxDigits = Math.max(displayDigits, storageDigits)\n\tconst target = roundTo(value, storageDigits)\n\t// A bare probe: never forward `opts` itself, so an entry/dirty/storedValue field\n\t// a future CommitOptions caller happens to carry can never poison this check.\n\tconst probeOpts: UnitContext = { displayUnit, engine, precision, storageDigits, storageUnit }\n\tfor (let digits = displayDigits; digits < maxDigits; digits++) {\n\t\tconst candidate = draftAt(digits)\n\t\tif (commitDrafts(candidate, probeOpts) === target) return candidate\n\t}\n\treturn draftAt(maxDigits)\n}\n"],"mappings":";;;;AAkDA,MAAM,iBAAiB,UAA0B,OAAO,KAAK;AAE7D,MAAM,cAAc,QAA+B;CAClD,IAAI,IAAI,KAAK,MAAM,IAAI,OAAO;CAC9B,MAAM,SAAS,OAAO,GAAG;CACzB,OAAO,OAAO,SAAS,MAAM,IAAI,SAAS;AAC3C;;;;;;AAOA,MAAM,mBAAmB,WAA6C;CACrE,IAAI,OAAO,QAAQ,KAAK,MAAM,MAAM,WAAW,OAAO,KAAK,MAAM,MAAM,OAAO;CAC9E,OAAO,WAAW,OAAO,OAAO;AACjC;AAQA,MAAM,YAAyB;CAAE,SAAS;CAAM,OAAO;AAAK;AAE5D,MAAa,gBAAgB,QAA2B,SAAuC;CAC9F,MAAM,EACL,aACA,QAAQ,WACR,QACA,QAAQ,QACR,eACA,WACA,gBAAA,GACA,aACA,cAAc,SACX;CAEJ,IAAI,OAAO,eAAe,WAAW,GAAG;EACvC,MAAM,WAAW,UAAU;EAC3B,MAAM,YAAY,OAAO,gBAAgB;EACzC,MAAM,cAAc,MAAM,UAAU,IAAI,MAAM,MAAM,QAAQ,IAAI;EAMhE,IAAI,aAAa,eAAe,KAAK,eAAe;GACnD,MAAM,QAAQ,OAAO,UAAU,aAAuB,aAAa,aAAa,aAAa;GAC7F,MAAM,aAAa,MAAM,QAAQ,SAAS,QAAQ,MAAM;GAExD,IAAI,MAAM,SAAS;IAClB,IAAI,aAAa,gBAAgB,MAAM;IACvC,IAAI,eAAe,MAAM,O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
2
  "name": "@10x-media/fields",
3
- "version": "4.0.0-alpha.1",
3
+ "version": "4.0.0-alpha.2",
4
4
  "description": "Reusable admin fields for Payload: color picker, icon picker, encrypted fields, and more",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -182,9 +182,9 @@
182
182
  "typescript": "5.9.3",
183
183
  "vitest": "4.1.7",
184
184
  "@10x-media/payload-test-harness": "0.0.0",
185
+ "@10x-media/tsconfig": "0.0.0",
185
186
  "@10x-media/tsdown-config": "0.0.0",
186
- "@10x-media/vitest-config": "0.0.0",
187
- "@10x-media/tsconfig": "0.0.0"
187
+ "@10x-media/vitest-config": "0.0.0"
188
188
  },
189
189
  "publishConfig": {
190
190
  "access": "public"