@nocode-factory/sanity-kit 2.61.1-rc.0 → 2.61.1-rc.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.
@@ -29,8 +29,11 @@ export function InlineTextEditor({labels}: {labels: PreviewEditingLabels}) {
29
29
  // Texte en plusieurs paragraphes (un seul champ, rendu en <p> successifs) : Sanity
30
30
  // n'encadre que le dernier (ses marqueurs sont à la fin du texte). Un clic sur
31
31
  // n'importe quel paragraphe ouvre le texte entier, sur tout le bloc.
32
- const hovered = (direct && paragraphGroup(direct)) ?? direct ?? paragraphGroupAt(event.target)
33
- if (!hovered || !(event.target instanceof Node) || !hovered.element.contains(event.target)) return
32
+ const clicked = event.target instanceof Node ? event.target : null
33
+ const own = direct && clicked && direct.element.contains(clicked) ? direct : null
34
+ const hovered = (own && paragraphGroup(own)) ?? own ?? paragraphGroupAt(event.target)
35
+ const area = hovered?.parts ?? (hovered ? [hovered.element] : [])
36
+ if (!hovered || !clicked || !area.some((el) => el.contains(clicked))) return
34
37
  if (inlineEdit.getEditing()?.element === hovered.element) return
35
38
  event.preventDefault()
36
39
  inlineEdit.open(hovered)
@@ -73,28 +76,36 @@ function singleStegaNode(el: Element): SanityNode | null {
73
76
  }
74
77
 
75
78
  /**
76
- * Bloc de paragraphes d'un même champ : le parent direct qui contient au moins deux
77
- * blocs (p, li...) et UN SEUL texte marqué (celui du champ, à la fin du dernier bloc).
79
+ * Bloc de paragraphes d'un même champ : les paragraphes CONSÉCUTIFS de même balise autour
80
+ * de celui-ci (un bouton ou un titre voisin ne compte pas), avec UN SEUL texte marqué,
81
+ * dans le dernier (les marqueurs sont à la fin du texte du champ).
78
82
  */
79
83
  function groupOf(block: Element): InlineTarget | null {
80
84
  const parent = block.parentElement
81
85
  if (!parent || parent === document.body) return null
82
- const blocks = Array.from(parent.children).filter((c) => c.matches(BLOCK))
83
- if (blocks.length < 2 || !blocks.includes(block)) return null
84
- // Mêmes balises (un texte en paragraphes), et le marqueur dans le DERNIER bloc.
85
- if (blocks.some((b) => b.tagName !== block.tagName)) return null
86
- const last = blocks[blocks.length - 1]!
87
- if (stegaRuns(last.textContent ?? '') !== 1) return null
88
- const node = singleStegaNode(parent)
86
+ const siblings = Array.from(parent.children)
87
+ const at = siblings.indexOf(block)
88
+ let first = at
89
+ let last = at
90
+ while (first > 0 && siblings[first - 1]!.tagName === block.tagName) first--
91
+ while (last < siblings.length - 1 && siblings[last + 1]!.tagName === block.tagName) last++
92
+ const run = siblings.slice(first, last + 1) as HTMLElement[]
93
+ if (run.length < 2) return null
94
+ const withStega = run.filter((b) => stegaRuns(b.textContent ?? '') > 0)
95
+ const end = run[run.length - 1]!
96
+ if (withStega.length !== 1 || withStega[0] !== end || stegaRuns(end.textContent ?? '') !== 1) return null
97
+ // Les paragraphes du champ : depuis le dernier paragraphe marqué qui précède (exclu).
98
+ const node = singleStegaNode(end)
99
+ if (!node) return null
89
100
  const expect = vercelStegaClean(block.textContent ?? '').replace(/\s+/g, ' ').trim()
90
- return node && parent instanceof HTMLElement ? {element: parent, node, expect} : null
101
+ return {element: end, node, expect, parts: run}
91
102
  }
92
103
 
93
104
  /** Cible déjà trouvée (dernier paragraphe) : élargie au bloc entier quand c'en est un. */
94
105
  function paragraphGroup(target: InlineTarget): InlineTarget | null {
95
106
  const block = target.element.matches(BLOCK) ? target.element : target.element.closest(BLOCK)
96
107
  const group = block ? groupOf(block) : null
97
- return group && group.node.path === target.node.path ? group : null
108
+ return group && group.node.path === target.node.path ? {...group, expect: undefined} : null
98
109
  }
99
110
 
100
111
  /** Clic sur un paragraphe SANS marqueur (pas le dernier) : le bloc dont il fait partie. */
@@ -261,7 +272,9 @@ function EditorSession({target, labels}: {target: InlineTarget; labels: PreviewE
261
272
  const [value, setValue] = useState('')
262
273
  const [inherited, setInherited] = useState(false)
263
274
  const [box, setBox] = useState<Box | null>(null)
264
- const [typo] = useState(() => textStyle(element))
275
+ // Bloc de paragraphes : tous ses paragraphes ; sinon l'élément seul.
276
+ const parts = target.parts?.length ? target.parts : [element]
277
+ const [typo] = useState(() => textStyle(parts[0]!))
265
278
  const initial = useRef('')
266
279
  // Texte de départ d'un texte hérité (valeur par défaut) : inchangé = rien à enregistrer.
267
280
  const inheritedStart = useRef('')
@@ -274,19 +287,27 @@ function EditorSession({target, labels}: {target: InlineTarget; labels: PreviewE
274
287
 
275
288
  // Le texte d'origine est masqué (mise en page conservée) le temps de l'édition.
276
289
  useEffect(() => {
277
- const previous = element.style.visibility
278
- element.style.visibility = 'hidden'
290
+ const previous = parts.map((el) => el.style.visibility)
291
+ for (const el of parts) el.style.visibility = 'hidden'
279
292
  return () => {
280
- element.style.visibility = previous
293
+ parts.forEach((el, i) => {
294
+ el.style.visibility = previous[i] ?? ''
295
+ })
281
296
  }
297
+ // eslint-disable-next-line react-hooks/exhaustive-deps
282
298
  }, [element])
283
299
 
284
300
  // Suit l'élément (défilement, défilement doux, redimensionnement).
285
301
  useEffect(() => {
286
302
  let frame = 0
287
303
  const tick = () => {
288
- const r = element.getBoundingClientRect()
289
- const next = {top: r.top, left: r.left, width: r.width, height: r.height}
304
+ // Cadre : l'union des paragraphes du bloc (ou l'élément seul).
305
+ const rects = parts.map((el) => el.getBoundingClientRect())
306
+ const top = Math.min(...rects.map((r) => r.top))
307
+ const left = Math.min(...rects.map((r) => r.left))
308
+ const right = Math.max(...rects.map((r) => r.right))
309
+ const bottom = Math.max(...rects.map((r) => r.bottom))
310
+ const next = {top, left, width: right - left, height: bottom - top}
290
311
  setBox((prev) => (sameBox(prev, next) ? prev : next))
291
312
  frame = requestAnimationFrame(tick)
292
313
  }
@@ -322,7 +343,7 @@ function EditorSession({target, labels}: {target: InlineTarget; labels: PreviewE
322
343
  const resolved =
323
344
  typeof stored === 'string' && stored !== ''
324
345
  ? stored
325
- : (fromSource ?? inheritedValueFor(node.path) ?? (list ? inheritedListValue(list, node.path) : null) ?? vercelStegaClean(element.textContent ?? '').trim())
346
+ : (fromSource ?? inheritedValueFor(node.path) ?? (list ? inheritedListValue(list, node.path) : null) ?? vercelStegaClean(parts.map((el) => el.textContent ?? '').join('\n\n')).trim())
326
347
  // Bloc de paragraphes : le paragraphe cliqué doit faire partie de ce texte (sinon
327
348
  // c'était un texte voisin, hors champ : on n'ouvre rien).
328
349
  if (target.expect && !resolved.replace(/\s+/g, ' ').includes(target.expect)) {
@@ -392,6 +413,12 @@ function EditorSession({target, labels}: {target: InlineTarget; labels: PreviewE
392
413
  // Valeur héritée de toute forme : la valeur brute des valeurs par défaut est recopiée
393
414
  // dans la page avec ce texte modifié, en un seul enregistrement.
394
415
  const copy = inherited && rawInherited.current ? writeAt(rawInherited.current.raw, parsePath(rawInherited.current.rest) ?? [], next) : null
416
+ // Garde-fou : une recopie identique aux valeurs par défaut n'écrit rien (la page doit
417
+ // continuer d'hériter : une valeur par défaut modifiée plus tard s'y verra).
418
+ if (copy !== null && JSON.stringify(copy) === JSON.stringify(rawInherited.current?.raw)) {
419
+ inlineEdit.close()
420
+ return
421
+ }
395
422
  if (copy !== null && rawInherited.current) {
396
423
  doc.patch([at(rawInherited.current.path, set(copy))])
397
424
  setStatus('saving')
@@ -405,6 +432,10 @@ function EditorSession({target, labels}: {target: InlineTarget; labels: PreviewE
405
432
  inlineEdit.close()
406
433
  return
407
434
  }
435
+ if (list && items && JSON.stringify(items) === JSON.stringify(list.items)) {
436
+ inlineEdit.close()
437
+ return
438
+ }
408
439
  if (list && items) doc.patch([at(list.path, set(items))])
409
440
  // Morceau de texte riche : jamais retiré (un morceau sans texte casse le bloc), vidé.
410
441
  else if (isSpanTextPath(node.path)) doc.patch([at(node.path, set(next))])
@@ -419,7 +450,7 @@ function EditorSession({target, labels}: {target: InlineTarget; labels: PreviewE
419
450
  const expected = value.replace(/\s+/g, ' ').trim()
420
451
  const started = performance.now()
421
452
  const timer = window.setInterval(() => {
422
- const shown = vercelStegaClean(element.textContent ?? '').replace(/\s+/g, ' ').trim()
453
+ const shown = vercelStegaClean(parts.map((el) => el.textContent ?? '').join(' ')).replace(/\s+/g, ' ').trim()
423
454
  if (shown.includes(expected) || expected === '' || performance.now() - started > 6000) inlineEdit.close()
424
455
  }, 150)
425
456
  return () => window.clearInterval(timer)
@@ -14,6 +14,8 @@ export type InlineTarget = {
14
14
  node: SanityNode
15
15
  /** Bloc de paragraphes : texte du paragraphe cliqué, qui doit figurer dans la valeur. */
16
16
  expect?: string
17
+ /** Bloc de paragraphes : tous les paragraphes du champ (masqués, cadre = leur union). */
18
+ parts?: HTMLElement[]
17
19
  }
18
20
 
19
21
  let hovered: InlineTarget | null = null