@pairdraw/dom-css 0.19.1 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { isEqual } from 'es-toolkit/predicate'
2
2
  import { fromUint8Array } from 'js-base64'
3
3
 
4
- import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
4
+ import type { SceneGraph, SceneNode, Stroke } from '@pairdraw/scene-graph'
5
5
  import { BLACK } from '@pairdraw/scene-graph/constants'
6
6
  import { readIcon } from '@pairdraw/scene-graph/icons'
7
7
  import { imageMimeType } from '@pairdraw/scene-graph/images'
@@ -167,7 +167,7 @@ function addCornerRadii(style: DesignStyleDeclaration, node: SceneNode, css: Fie
167
167
  }
168
168
 
169
169
  function addStroke(style: DesignStyleDeclaration, node: SceneNode, css: FieldCSS): void {
170
- const stroke = node.strokes.at(0)
170
+ const stroke = node.strokes.find((item) => item.visible)
171
171
  const literalColor = strokeColorToCSS(stroke)
172
172
  if (!literalColor || !stroke) return
173
173
  const color = css('strokes/0/color', literalColor)
@@ -186,6 +186,35 @@ function addStroke(style: DesignStyleDeclaration, node: SceneNode, css: FieldCSS
186
186
  style['border-left-width'] = css('borderLeftWeight', px(node.borderLeftWeight))
187
187
  }
188
188
 
189
+ /**
190
+ * CSS gives an element one border, so the visible strokes after the first become rings in
191
+ * `box-shadow`, in layer order. An outside ring grows from the border's outer edge, an inside one
192
+ * from its inner edge, and a centred stroke gets a ring on each side of the border. Rings cannot
193
+ * dash or take per-side weights, and outer and inner rings paint in separate CSS layers, so a
194
+ * stroke order that alternates them is approximated.
195
+ */
196
+ function extraStrokeShadows(strokes: Stroke[], first: Stroke): string[] {
197
+ const outer: string[] = []
198
+ const inner: string[] = []
199
+ let outside = 0
200
+ let inside = first.weight
201
+ for (const stroke of strokes) {
202
+ if (!stroke.visible) continue
203
+ const color = strokeColorToCSS(stroke)
204
+ if (!color) continue
205
+ if (stroke.align !== 'OUTSIDE') {
206
+ inside += stroke.align === 'CENTER' ? stroke.weight / 2 : stroke.weight
207
+ inner.push(`inset 0 0 0 ${px(inside)} ${color}`)
208
+ }
209
+ if (stroke.align !== 'INSIDE') {
210
+ outside += stroke.align === 'CENTER' ? stroke.weight / 2 : stroke.weight
211
+ outer.push(`0 0 0 ${px(outside)} ${color}`)
212
+ }
213
+ }
214
+ // The first ring listed paints on top, so each group stays nearest first.
215
+ return [...outer, ...inner]
216
+ }
217
+
189
218
  const PADDING_FIELDS = ['paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft'] as const
190
219
 
191
220
  /** Sides compare by their CSS, so a token and a literal of the same size stay separate. */
@@ -296,6 +325,30 @@ function addBoxSizing(style: DesignStyleDeclaration): void {
296
325
  if (sized && boxed) style['box-sizing'] = 'border-box'
297
326
  }
298
327
 
328
+ /**
329
+ * The CSS keyword for a blend mode, where one paints the same mode. Linear burn would need
330
+ * `plus-darker`, which Chrome, Edge, and Firefox do not implement, so it gains no keyword and
331
+ * exports flat. A normal layer or an unscoped pass-through needs none either.
332
+ */
333
+ const CSS_BLEND_MODES: Partial<Record<SceneNode['blendMode'], string>> = {
334
+ DARKEN: 'darken',
335
+ MULTIPLY: 'multiply',
336
+ COLOR_BURN: 'color-burn',
337
+ LIGHTEN: 'lighten',
338
+ SCREEN: 'screen',
339
+ LINEAR_DODGE: 'plus-lighter',
340
+ COLOR_DODGE: 'color-dodge',
341
+ OVERLAY: 'overlay',
342
+ SOFT_LIGHT: 'soft-light',
343
+ HARD_LIGHT: 'hard-light',
344
+ DIFFERENCE: 'difference',
345
+ EXCLUSION: 'exclusion',
346
+ HUE: 'hue',
347
+ SATURATION: 'saturation',
348
+ COLOR: 'color',
349
+ LUMINOSITY: 'luminosity'
350
+ }
351
+
299
352
  function styleFromSceneNode(
300
353
  node: SceneNode,
301
354
  parent: SceneNode | undefined,
@@ -312,7 +365,18 @@ function styleFromSceneNode(
312
365
  addImageStyle(style, node)
313
366
  addStroke(style, node, css)
314
367
  Object.assign(style, effectsToCSS(node.effects))
368
+ const visibleStrokes = node.strokes.filter((stroke) => stroke.visible)
369
+ if (visibleStrokes.length > 1) {
370
+ const rings = extraStrokeShadows(visibleStrokes.slice(1), visibleStrokes[0])
371
+ if (rings.length > 0) {
372
+ style['box-shadow'] = [rings.join(', '), style['box-shadow']]
373
+ .filter((shadow): shadow is string => Boolean(shadow))
374
+ .join(', ')
375
+ }
376
+ }
315
377
  if (node.opacity < 1) style.opacity = css('opacity', String(node.opacity))
378
+ const blend = CSS_BLEND_MODES[node.blendMode]
379
+ if (blend) style['mix-blend-mode'] = blend
316
380
  addCornerRadii(style, node, css)
317
381
  if (node.clipsContent) style.overflow = 'hidden'
318
382
  const alignSelf = alignToCSS(node.layoutAlignSelf)
@@ -455,6 +519,59 @@ function vectorDesignNode(
455
519
  }
456
520
  }
457
521
 
522
+ /** The outline a vector mask clips siblings to, in the sibling's own box; null where CSS cannot. */
523
+ function vectorMaskClip(mask: SceneNode, sibling: SceneNode): string | null {
524
+ // The renderer clips to the rotated outline; a polygon built from the unrotated box would
525
+ // cut a different region, so a rotated mask stays unclipped as the export documents.
526
+ if (mask.maskType !== 'VECTOR' || mask.childIds.length > 0 || mask.rotation !== 0) return null
527
+ const left = mask.x - sibling.x
528
+ const top = mask.y - sibling.y
529
+ if (mask.type === 'RECTANGLE') {
530
+ const right = left + mask.width
531
+ const bottom = top + mask.height
532
+ return `polygon(${px(left)} ${px(top)}, ${px(right)} ${px(top)}, ${px(right)} ${px(bottom)}, ${px(left)} ${px(bottom)})`
533
+ }
534
+ if (mask.type === 'ELLIPSE') {
535
+ const rx = mask.width / 2
536
+ const ry = mask.height / 2
537
+ return `ellipse(${px(rx)} ${px(ry)} at ${px(left + rx)} ${px(top + ry)})`
538
+ }
539
+ return null
540
+ }
541
+
542
+ /**
543
+ * Projects a parent's children as the canvas draws them: a mask layer is never a layer of its
544
+ * own, and one vector mask clips the siblings after it. Alpha and luminance masks have no
545
+ * faithful CSS form over several elements, and two masks combine, so those stay unclipped.
546
+ */
547
+ function projectChildren(
548
+ context: ProjectionContext,
549
+ parent: SceneNode,
550
+ modes: CSSModes,
551
+ root: boolean
552
+ ): DesignNode[] {
553
+ const projected: DesignNode[] = []
554
+ let masks: SceneNode[] = []
555
+ let afterMask = false
556
+ for (const child of nodeChildren(context.graph, parent)) {
557
+ if (child.visible && child.isMask) {
558
+ masks = afterMask ? [...masks, child] : [child]
559
+ afterMask = true
560
+ continue
561
+ }
562
+ afterMask = false
563
+ const node = sceneNodeToDesignNode(context, child, modes, root)
564
+ if (!node) continue
565
+ const mask = masks.length === 1 ? masks[0] : null
566
+ if (mask && node.type === 'element') {
567
+ const clip = vectorMaskClip(mask, child)
568
+ if (clip) node.inlineStyle = { ...node.inlineStyle, 'clip-path': clip }
569
+ }
570
+ projected.push(node)
571
+ }
572
+ return projected
573
+ }
574
+
458
575
  function sceneNodeToDesignNode(
459
576
  context: ProjectionContext,
460
577
  node: SceneNode,
@@ -488,9 +605,7 @@ function sceneNodeToDesignNode(
488
605
  if (vector) return vector
489
606
  }
490
607
 
491
- const children = nodeChildren(graph, node)
492
- .map((child) => sceneNodeToDesignNode(context, child, modes, false))
493
- .filter((child): child is DesignNode => child !== null)
608
+ const children = projectChildren(context, node, modes, false)
494
609
 
495
610
  if (node.type === 'CANVAS') {
496
611
  return {
@@ -545,11 +660,7 @@ export function sceneGraphToDesignDocument(
545
660
  const root = graph.getNode(options.rootId ?? graph.rootId)
546
661
  const context = projectionContext(graph, options)
547
662
  const modes = context.tokens?.defaultModes() ?? new Map<string, string>()
548
- const children = root
549
- ? nodeChildren(graph, root)
550
- .map((child) => sceneNodeToDesignNode(context, child, modes, true))
551
- .filter((child): child is DesignNode => child !== null)
552
- : []
663
+ const children = root ? projectChildren(context, root, modes, true) : []
553
664
 
554
665
  return { type: 'document', sourceGraph: graph, tokens: context.tokens, children }
555
666
  }
@@ -203,6 +203,17 @@ function hasDefiniteWidth(graph: SceneGraph, node: SceneNode): boolean {
203
203
  return layoutSizing(graph, node, 'HORIZONTAL') !== 'HUG'
204
204
  }
205
205
 
206
+ /**
207
+ * Whether CSS keeps the text on its lines however narrow its box, and the text starts at the
208
+ * box's start edge, so its own width places it as the box does. A box narrower than the text
209
+ * clips it rather than breaking it, which wrapping text measured in other fonts would do.
210
+ */
211
+ function staysOnItsLines(style: DesignStyleDeclaration): boolean {
212
+ const whiteSpace = pickStyle(style, 'white-space')?.trim()
213
+ const align = pickStyle(style, 'text-align')?.trim() ?? 'start'
214
+ return (whiteSpace === 'nowrap' || whiteSpace === 'pre') && ['start', 'left'].includes(align)
215
+ }
216
+
206
217
  /**
207
218
  * Text wraps where its width is fixed or fills a parent with a width of its own, and hugs its
208
219
  * content otherwise. It is measured when the document is laid out.
@@ -243,6 +254,10 @@ export function applyFlowSizing(
243
254
  autoLayoutSizingFields(node.layoutMode, own(size.fixedWidth), own(size.fixedHeight))
244
255
  )
245
256
  }
257
+ if (node.type === 'TEXT' && staysOnItsLines(style)) {
258
+ applyTextSizing(graph, node, { fixedWidth: false, fixedHeight: size.fixedHeight })
259
+ return
260
+ }
246
261
  applyParentFill(graph, node, style, display, parentFlow, size)
247
262
  if (node.type === 'TEXT') applyTextSizing(graph, node, size)
248
263
  }
@@ -0,0 +1,40 @@
1
+ import {
2
+ GENERIC_FONT_FAMILIES_BY_CLASS,
3
+ genericFontFamilyClass,
4
+ parseCSSFontFamilies,
5
+ type GenericFontClass
6
+ } from '@pairdraw/scene-graph/css'
7
+
8
+ export interface FontStackOptions {
9
+ /**
10
+ * Whether the document can draw a family, such as the font service answering. Without it every
11
+ * concrete family is taken as available, as a headless import has no faces to ask.
12
+ */
13
+ isAvailable?: (family: string) => boolean
14
+ }
15
+
16
+ function defaultFamily(kind: GenericFontClass, options: FontStackOptions): string {
17
+ const candidates = GENERIC_FONT_FAMILIES_BY_CLASS[kind]
18
+ return (
19
+ (options.isAvailable ? candidates.find((family) => options.isAvailable?.(family)) : null) ??
20
+ candidates[0]
21
+ )
22
+ }
23
+
24
+ /**
25
+ * The family a CSS `font-family` stack asks a document to draw: the first concrete family the
26
+ * font service can provide; a generic keyword maps to a concrete face of its class. Null when the
27
+ * list has no family.
28
+ */
29
+ export function resolveFontFamilyStack(
30
+ value: string | undefined,
31
+ options: FontStackOptions = {}
32
+ ): string | null {
33
+ for (const family of parseCSSFontFamilies(value)) {
34
+ const kind = genericFontFamilyClass(family)
35
+ if (kind) return defaultFamily(kind, options)
36
+ if (options.isAvailable && !options.isAvailable(family)) continue
37
+ return family
38
+ }
39
+ return null
40
+ }
@@ -3,6 +3,7 @@ import { isValid, toUint8Array } from 'js-base64'
3
3
 
4
4
  import {
5
5
  SceneGraph,
6
+ type Effect,
6
7
  type Fill,
7
8
  type ImageScaleMode,
8
9
  type SceneNode,
@@ -28,8 +29,10 @@ import {
28
29
  elementDisplay,
29
30
  flowOf,
30
31
  inheritedTextStyle,
32
+ type FlowKind,
31
33
  type ParentFlow
32
34
  } from './flow'
35
+ import { resolveFontFamilyStack } from './fonts'
33
36
  import {
34
37
  blockRichText,
35
38
  characterStyleFromCSS,
@@ -49,13 +52,47 @@ const IMAGE_SOURCE_URL_KEY = 'image-source-url'
49
52
  * by half its blur length, which the canvas draws from the radius that gives that sigma.
50
53
  */
51
54
  function cssShadows(value: string | undefined) {
52
- return parseCSSShadows(value).map((effect) =>
53
- effect.type === 'SHADER' ? effect : { ...effect, radius: blurRadiusForSigma(effect.radius / 2) }
54
- )
55
+ return parseCSSShadows(value).map((effect): Effect => {
56
+ if (effect.type !== 'DROP_SHADOW' && effect.type !== 'INNER_SHADOW') return effect
57
+ return { ...effect, radius: blurRadiusForSigma(effect.radius / 2) }
58
+ })
55
59
  }
56
60
 
57
61
  export interface DesignDocumentToSceneGraphOptions {
58
62
  pageName?: string
63
+ /**
64
+ * The family a CSS `font-family` stack should draw, given the stack's families; null keeps it.
65
+ * Hosts with a font service pass one that skips faces the document cannot draw; without it the
66
+ * first concrete family wins and a generic keyword maps to a concrete face of its class.
67
+ */
68
+ resolveFontFamily?: (value: string) => string | null
69
+ }
70
+
71
+ /**
72
+ * The document with every element's font stack resolved to the family its text layer draws, so
73
+ * the per-character CSS parse sees a concrete name instead of a generic keyword.
74
+ */
75
+ function withResolvedFontFamilies(
76
+ node: DesignNode,
77
+ resolve: (value: string) => string | null
78
+ ): DesignNode {
79
+ if (node.type !== 'element') return node
80
+ const resolved = (
81
+ style: DesignStyleDeclaration | undefined
82
+ ): DesignStyleDeclaration | undefined => {
83
+ const stack = style?.['font-family']
84
+ const family = stack === undefined ? null : resolve(stack)
85
+ return family && family !== stack ? { ...style, 'font-family': family } : style
86
+ }
87
+ const computedStyle = resolved(node.computedStyle)
88
+ const inlineStyle = resolved(node.inlineStyle)
89
+ const next: DesignElement = {
90
+ ...node,
91
+ children: node.children.map((child) => withResolvedFontFamilies(child, resolve))
92
+ }
93
+ if (computedStyle) next.computedStyle = computedStyle
94
+ if (inlineStyle) next.inlineStyle = inlineStyle
95
+ return next
59
96
  }
60
97
 
61
98
  function textContent(node: DesignNode): string {
@@ -330,11 +367,10 @@ function applyImageFill(
330
367
  const bytes = bytesFromDataURL(source)
331
368
  if (!bytes) {
332
369
  if (source) {
333
- node.pluginData.push({
334
- pluginId: DOM_CSS_PLUGIN_ID,
335
- key: IMAGE_SOURCE_URL_KEY,
336
- value: source
337
- })
370
+ node.pluginData = [
371
+ ...node.pluginData,
372
+ { pluginId: DOM_CSS_PLUGIN_ID, key: IMAGE_SOURCE_URL_KEY, value: source }
373
+ ]
338
374
  }
339
375
  return
340
376
  }
@@ -381,6 +417,8 @@ function applyElementStyle(
381
417
  }
382
418
  node.strokes = strokes
383
419
  setBorderWeights(node, style, strokes[0])
420
+ // A CSS border takes room: content sits inside it, as auto layout with strokes included does.
421
+ node.strokesIncludedInLayout = true
384
422
  }
385
423
 
386
424
  const effects = cssShadows(pickStyle(style, 'box-shadow'))
@@ -498,53 +536,117 @@ function linkText(node: SceneNode, element: DesignElement): void {
498
536
  }
499
537
  }
500
538
 
539
+ const BOX_PROPERTIES = [
540
+ 'aspect-ratio',
541
+ 'background-color',
542
+ 'border-radius',
543
+ 'box-shadow',
544
+ 'display',
545
+ 'height',
546
+ 'width',
547
+ 'min-width',
548
+ 'max-width',
549
+ 'min-height',
550
+ 'max-height',
551
+ 'object-fit',
552
+ 'overflow',
553
+ 'position',
554
+ 'top',
555
+ 'right',
556
+ 'bottom',
557
+ 'left',
558
+ 'flex-wrap',
559
+ 'align-self'
560
+ ]
561
+
562
+ const PADDING_PROPERTIES = [
563
+ 'padding',
564
+ 'padding-top',
565
+ 'padding-right',
566
+ 'padding-bottom',
567
+ 'padding-left',
568
+ 'padding-block',
569
+ 'padding-inline'
570
+ ]
571
+
572
+ const BORDER_SIDES = ['top', 'right', 'bottom', 'left'] as const
573
+
574
+ /** Whether every length of a value is zero, as `0`, `0px`, and `0 0` are. */
575
+ function isZeroLength(value: string): boolean {
576
+ return value
577
+ .trim()
578
+ .split(/\s+/)
579
+ .every((part) => parseCSSNumber(part) === 0)
580
+ }
581
+
582
+ /** Whether any side's border draws: it has a style other than none and a width other than zero. */
583
+ function drawsBorder(style: DesignStyleDeclaration): boolean {
584
+ return BORDER_SIDES.some((side) => {
585
+ const borderStyle = pickStyle(style, `border-${side}-style`) ?? pickStyle(style, 'border-style')
586
+ if (borderStyle === undefined || borderStyle === 'none' || borderStyle === 'hidden')
587
+ return false
588
+ // A styled border without a width is CSS's medium one.
589
+ const width = pickStyle(style, `border-${side}-width`) ?? pickStyle(style, 'border-width')
590
+ return width === undefined || !isZeroLength(width)
591
+ })
592
+ }
593
+
594
+ /**
595
+ * Whether the element is a box rather than a run of the text around it. Padding and borders of
596
+ * no size, which resets such as Tailwind's give every element, leave it a run.
597
+ */
501
598
  function hasBoxStyle(style: DesignStyleDeclaration): boolean {
502
- return [
503
- 'aspect-ratio',
504
- 'background-color',
505
- 'border-color',
506
- 'border-style',
507
- 'border-width',
508
- 'border-top-style',
509
- 'border-top-width',
510
- 'border-right-style',
511
- 'border-right-width',
512
- 'border-bottom-style',
513
- 'border-bottom-width',
514
- 'border-left-style',
515
- 'border-left-width',
516
- 'border-radius',
517
- 'box-shadow',
518
- 'display',
519
- 'height',
520
- 'padding',
521
- 'padding-top',
522
- 'padding-right',
523
- 'padding-bottom',
524
- 'padding-left',
525
- 'padding-block',
526
- 'padding-inline',
527
- 'width',
528
- 'min-width',
529
- 'max-width',
530
- 'min-height',
531
- 'max-height',
532
- 'object-fit',
533
- 'overflow',
534
- 'position',
535
- 'top',
536
- 'right',
537
- 'bottom',
538
- 'left',
539
- 'flex-wrap',
540
- 'align-self'
541
- ].some((property) => pickStyle(style, property) !== undefined)
599
+ if (BOX_PROPERTIES.some((property) => pickStyle(style, property) !== undefined)) return true
600
+ if (
601
+ PADDING_PROPERTIES.some((property) => {
602
+ const value = pickStyle(style, property)
603
+ return value !== undefined && !isZeroLength(value)
604
+ })
605
+ ) {
606
+ return true
607
+ }
608
+ return drawsBorder(style)
542
609
  }
543
610
 
544
611
  function hasContent(element: DesignElement): boolean {
545
612
  return element.children.some((child) => child.type !== 'text' || child.text.trim().length > 0)
546
613
  }
547
614
 
615
+ /** Whether a child sits in a line of its parent's content or leaves it, rather than a block. */
616
+ function isInLine(node: DesignNode): boolean {
617
+ if (node.type === 'text' || node.tagName.toLowerCase() === 'br') return true
618
+ const style = mergedStyle(node)
619
+ const display = elementDisplay(node, style)
620
+ const position = pickStyle(style, 'position')
621
+ return (
622
+ display.startsWith('inline') ||
623
+ display === 'none' ||
624
+ position === 'absolute' ||
625
+ position === 'fixed'
626
+ )
627
+ }
628
+
629
+ /**
630
+ * Whether a block holds inline content with boxes in it, such as a paragraph with a styled
631
+ * `<code>`, which CSS lays out in lines rather than stacking each run and box.
632
+ */
633
+ function holdsInlineBoxes(element: DesignElement): boolean {
634
+ return (
635
+ element.children.some((child) => child.type === 'element') && element.children.every(isInLine)
636
+ )
637
+ }
638
+
639
+ /** The flow a box lays its children out in, and whether they are one text instead. */
640
+ function containerFlow(element: DesignElement, display: string) {
641
+ // A box without content keeps its own size, as an image does, unless it declares a layout.
642
+ const own = hasContent(element) || flowOf(display) === 'flex' ? flowOf(display) : null
643
+ const richText = own !== 'flex' && holdsRichText(element)
644
+ // Inline content that cannot be one text runs in lines that wrap at the block's width.
645
+ const lines = own === 'block' && !richText && holdsInlineBoxes(element)
646
+ const flow: FlowKind | null = lines ? 'inline' : own
647
+ return { flow, richText, lines }
648
+ }
649
+
548
650
  function createElementNode(
549
651
  graph: SceneGraph,
550
652
  parentId: string,
@@ -553,7 +655,8 @@ function createElementNode(
553
655
  ): SceneNode | null {
554
656
  const style = mergedStyle(element)
555
657
  const display = elementDisplay(element, style)
556
- if (display === 'none') return null
658
+ // A line break between boxes draws nothing; rich text reads it as a new line.
659
+ if (display === 'none' || element.tagName.toLowerCase() === 'br') return null
557
660
  if (
558
661
  isTextLikeElement(element) &&
559
662
  !hasBoxStyle(style) &&
@@ -573,28 +676,38 @@ function createElementNode(
573
676
  clipsContent: false
574
677
  })
575
678
  applyElementStyle(graph, node, element, style)
576
- // A box without content keeps its own size, as an image does, unless it declares a layout.
577
- const flow = hasContent(element) || flowOf(display) === 'flex' ? flowOf(display) : null
679
+ const { flow, richText, lines } = containerFlow(element, display)
578
680
  if (flow) {
579
681
  applyContainerLayout(node, element, style, flow)
580
682
  if (flow === 'flex') applyFlexGap(node, style)
683
+ if (lines) node.layoutWrap = 'WRAP'
581
684
  }
582
685
  applyFlowSizing(graph, node, style, display, parentFlow)
583
686
 
584
687
  // Blocks of text in a box's own flow are one text; a flex box's children are items.
585
- if (flow !== 'flex' && holdsRichText(element)) {
688
+ if (richText) {
586
689
  const textStyle = inheritedTextStyle(element, style)
587
690
  const textDisplay = flow === 'inline' ? 'inline' : 'block'
588
691
  createRichTextNode(graph, node.id, element, textStyle, textDisplay, flow ?? 'none')
589
692
  return node
590
693
  }
591
- for (const child of element.children) {
694
+ const children = flow === 'flex' ? element.children.toSorted(byFlexOrder) : element.children
695
+ for (const child of children) {
592
696
  createDesignNode(graph, node.id, child, flow ?? 'none', inheritedTextStyle(element, style))
593
697
  }
594
698
 
595
699
  return node
596
700
  }
597
701
 
702
+ function flexOrder(node: DesignNode): number {
703
+ return node.type === 'element' ? (parseCSSNumber(pickStyle(mergedStyle(node), 'order')) ?? 0) : 0
704
+ }
705
+
706
+ /** Flex items in the order CSS places them: by `order`, then as written. */
707
+ function byFlexOrder(a: DesignNode, b: DesignNode): number {
708
+ return flexOrder(a) - flexOrder(b)
709
+ }
710
+
598
711
  function createDesignNode(
599
712
  graph: SceneGraph,
600
713
  parentId: string,
@@ -609,7 +722,33 @@ function createDesignNode(
609
722
  return createTextNode(graph, parentId, node.text, inheritedStyle, display, parentFlow)
610
723
  }
611
724
 
612
- return createElementNode(graph, parentId, node, parentFlow)
725
+ return createElementNode(graph, parentId, withFieldText(node), parentFlow)
726
+ }
727
+
728
+ /** Input types whose value is text a person reads in the field. */
729
+ const TEXT_INPUT_TYPES = new Set([
730
+ '',
731
+ 'text',
732
+ 'email',
733
+ 'search',
734
+ 'url',
735
+ 'tel',
736
+ 'number',
737
+ 'password'
738
+ ])
739
+
740
+ /**
741
+ * A text field with the text a browser draws in it, its value or else its placeholder, as a
742
+ * child: an `<input>` holds no content of its own. A password shows as dots.
743
+ */
744
+ function withFieldText(element: DesignElement): DesignElement {
745
+ if (element.tagName.toLowerCase() !== 'input' || element.children.length > 0) return element
746
+ const type = Object.hasOwn(element.attrs, 'type') ? element.attrs.type.trim().toLowerCase() : ''
747
+ if (!TEXT_INPUT_TYPES.has(type)) return element
748
+ const value = element.attrs.value || element.attrs.placeholder
749
+ if (!value) return element
750
+ const text = type === 'password' && element.attrs.value ? '•'.repeat(value.length) : value
751
+ return { ...element, children: [{ type: 'text', text }] }
613
752
  }
614
753
 
615
754
  function fitPageToChildren(page: SceneNode, graph: SceneGraph): void {
@@ -623,12 +762,17 @@ export function designDocumentToSceneGraph(
623
762
  document: DesignDocument,
624
763
  options: DesignDocumentToSceneGraphOptions = {}
625
764
  ): SceneGraph {
765
+ const resolve = options.resolveFontFamily ?? resolveFontFamilyStack
766
+ const resolved: DesignDocument = {
767
+ ...document,
768
+ children: document.children.map((child) => withResolvedFontFamilies(child, resolve))
769
+ }
626
770
  const graph = new SceneGraph()
627
771
  const page = graph.getPages().find((node) => node.type === 'CANVAS') ?? graph.addPage('DesignDOM')
628
772
 
629
773
  page.name = options.pageName ?? 'DesignDOM'
630
774
 
631
- for (const child of document.children) {
775
+ for (const child of resolved.children) {
632
776
  createDesignNode(graph, page.id, child, 'none')
633
777
  }
634
778