@nocode-factory/sanity-kit 2.66.0-rc.1 → 2.66.0-rc.3

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.
@@ -7,7 +7,7 @@ import {vercelStegaClean, vercelStegaDecode} from '@vercel/stega'
7
7
  import {coveringEntry, inheritedListValue, materializeInheritedList, parsePath, readAt, writeAt} from './inherited'
8
8
  import {ACCENT, CHIP, FONT, type PreviewEditingLabels} from './labels'
9
9
  import {READ_DEFAULTS_MESSAGE, READ_DEFAULTS_RESULT} from './protocol'
10
- import {inlineEdit, isEditMode, isSpanTextPath, parseNumberInput, stegaRuns, type InlineTarget} from './store'
10
+ import {formatNumberInput, inlineEdit, isEditMode, isSpanTextPath, parseNumberInput, stegaRuns, type InlineTarget} from './store'
11
11
 
12
12
  /**
13
13
  * Éditeur de texte SUR PLACE : au clic sur un texte modifiable, un champ de
@@ -255,6 +255,24 @@ export function ensureEditingStyle() {
255
255
  document.head.appendChild(style)
256
256
  }
257
257
 
258
+ /**
259
+ * Largeur exacte d'un texte dans la typographie du chiffre affiché : la saisie d'un
260
+ * chiffre s'élargit juste assez, sans recouvrir le suffixe voisin (« + », « % »).
261
+ */
262
+ let measureCanvas: HTMLCanvasElement | null = null
263
+ export function measureText(text: string, style: CSSProperties): number {
264
+ try {
265
+ measureCanvas ??= document.createElement('canvas')
266
+ const ctx = measureCanvas.getContext('2d')
267
+ if (!ctx) return 0
268
+ ctx.font = [style.fontStyle, style.fontWeight, style.fontSize, style.fontFamily].filter(Boolean).join(' ')
269
+ const spacing = parseFloat(String(style.letterSpacing ?? '')) || 0
270
+ return Math.ceil(ctx.measureText(text).width + spacing * text.length)
271
+ } catch {
272
+ return 0
273
+ }
274
+ }
275
+
258
276
  /** Padding de l'élément édité (un seul élément ; un bloc de paragraphes n'en a pas). */
259
277
  export function paddingOf(parts: HTMLElement[]): string {
260
278
  if (parts.length !== 1) return '0'
@@ -314,6 +332,8 @@ function EditorSession({target, labels}: {target: InlineTarget; labels: PreviewE
314
332
  // Chiffre (champ nombre) : saisie numérique, validée avant d'enregistrer.
315
333
  const isNumber = target.kind === 'number'
316
334
  const [invalid, setInvalid] = useState(false)
335
+ // Langue de la page (séparateurs de la saisie : « 98,5 », « 1 200 »).
336
+ const [locale] = useState(() => element.closest('[lang]')?.getAttribute('lang') || document.documentElement.lang || undefined)
317
337
  const rootRef = useRef<HTMLDivElement>(null)
318
338
  const fieldRef = useRef<HTMLTextAreaElement>(null)
319
339
 
@@ -373,7 +393,7 @@ function EditorSession({target, labels}: {target: InlineTarget; labels: PreviewE
373
393
  if (isNumber) {
374
394
  // Chiffre : la valeur ENREGISTRÉE (98.5), jamais le chiffre affiché (animé,
375
395
  // formaté « 98,5 »). Vide dans la page : celle des valeurs par défaut.
376
- let start = typeof stored === 'number' ? String(stored) : ''
396
+ let start = typeof stored === 'number' ? formatNumberInput(stored, locale) : ''
377
397
  if (typeof stored !== 'number') {
378
398
  const covering = inheritedSourceFor(node.path)
379
399
  if (covering) {
@@ -383,12 +403,12 @@ function EditorSession({target, labels}: {target: InlineTarget; labels: PreviewE
383
403
  if (raw !== undefined) {
384
404
  rawInherited.current = {path: covering.entry.path, raw, rest: covering.rest}
385
405
  const v = readAt(raw, parsePath(covering.rest) ?? [])
386
- if (typeof v === 'number') start = String(v)
406
+ if (typeof v === 'number') start = formatNumberInput(v, locale)
387
407
  }
388
408
  }
389
409
  }
390
410
  if (cancelled) return
391
- initial.current = typeof stored === 'number' ? String(stored) : ''
411
+ initial.current = typeof stored === 'number' ? formatNumberInput(stored, locale) : ''
392
412
  inheritedStart.current = start
393
413
  multiline.current = false
394
414
  setBreaks(false)
@@ -449,7 +469,10 @@ function EditorSession({target, labels}: {target: InlineTarget; labels: PreviewE
449
469
  if (!field) return
450
470
  const focusField = () => {
451
471
  field.focus({preventScroll: true})
452
- field.setSelectionRange(field.value.length, field.value.length)
472
+ // Chiffre : tout est présélectionné, on tape directement le nouveau (sinon « 100 »
473
+ // puis « 120 » donnait « 100120 », vu sur JAF). Texte : curseur à la fin.
474
+ if (isNumber) field.select()
475
+ else field.setSelectionRange(field.value.length, field.value.length)
453
476
  }
454
477
  focusField()
455
478
  // Le Studio ouvre le champ dans son panneau juste après le clic et y place le
@@ -476,13 +499,13 @@ function EditorSession({target, labels}: {target: InlineTarget; labels: PreviewE
476
499
 
477
500
  const save = useCallback(() => {
478
501
  if (isNumber) {
479
- const parsed = parseNumberInput(value)
502
+ const parsed = parseNumberInput(value, locale)
480
503
  if (Number.isNaN(parsed)) {
481
504
  setInvalid(true)
482
505
  return
483
506
  }
484
- const same = parsed === (initial.current === '' ? null : Number(initial.current))
485
- if (same || (inherited && parsed !== null && String(parsed) === inheritedStart.current)) {
507
+ const same = parsed === (initial.current === '' ? null : parseNumberInput(initial.current, locale))
508
+ if (same || (inherited && parsed !== null && parsed === parseNumberInput(inheritedStart.current, locale))) {
486
509
  inlineEdit.close()
487
510
  return
488
511
  }
@@ -603,12 +626,16 @@ function EditorSession({target, labels}: {target: InlineTarget; labels: PreviewE
603
626
  if (invalid) setInvalid(false)
604
627
  }}
605
628
  inputMode={isNumber ? 'decimal' : undefined}
629
+ wrap={isNumber ? 'off' : undefined}
606
630
  onKeyDown={onKeyDown}
607
631
  style={{
608
632
  ...typo,
609
633
  color: readableColor(typo.color as string | undefined),
610
634
  display: 'block',
611
- width: '100%',
635
+ // Chiffre : toujours sur une ligne, la saisie s'élargit si la valeur dépasse le
636
+ // chiffre affiché (« 98.5 » passait sur deux lignes, vu sur JAF).
637
+ width: isNumber ? Math.max(box.width, measureText(value, typo) + 4) : '100%',
638
+ whiteSpace: isNumber ? 'nowrap' : undefined,
612
639
  minHeight: box.height,
613
640
  margin: 0,
614
641
  padding,
@@ -112,8 +112,24 @@ export const isSpanTextPath = (path: string) => /\.children\[[^\]]+\]\.text$/.te
112
112
  * Saisie d'un chiffre : « 98,5 », « 98.5 », « 1 200 » (espaces, espaces insécables) ;
113
113
  * vide = retour à la valeur par défaut (null) ; autre chose = NaN (rien n'est enregistré).
114
114
  */
115
- export function parseNumberInput(text: string): number | null {
116
- const clean = text.replace(/[\s\u00a0\u202f]/g, '').replace(',', '.')
115
+ export function parseNumberInput(text: string, locale?: string): number | null {
116
+ // Séparateurs de la langue de la page : décimale « , » (fr) ou « . » (en), milliers
117
+ // « 1 200 » (fr) ou « 1,200 » (en). Sans langue : virgule et point acceptés en décimale.
118
+ const parts = locale ? new Intl.NumberFormat(locale).formatToParts(12345.6) : []
119
+ const group = parts.find((p) => p.type === 'group')?.value
120
+ const decimal = parts.find((p) => p.type === 'decimal')?.value
121
+ let clean = text.replace(/[\s\u00a0\u202f]/g, '')
122
+ if (group && !/\s/.test(group) && group !== decimal) clean = clean.split(group).join('')
123
+ clean = decimal && decimal !== '.' ? clean.split(decimal).join('.') : clean.replace(',', '.')
117
124
  if (clean === '') return null
118
125
  return /^-?\d+(\.\d+)?$/.test(clean) ? Number(clean) : Number.NaN
119
126
  }
127
+
128
+ /** Chiffre affiché dans la saisie au format de la langue de la page (« 98,5 », « 1 200 »). */
129
+ export function formatNumberInput(value: number, locale?: string): string {
130
+ try {
131
+ return new Intl.NumberFormat(locale, {maximumFractionDigits: 20}).format(value)
132
+ } catch {
133
+ return String(value)
134
+ }
135
+ }