@pairdraw/dom-css 0.19.2 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pairdraw/dom-css",
3
- "version": "0.19.2",
3
+ "version": "0.20.0",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",
@@ -11,7 +11,7 @@
11
11
  "types": "./dist/index.d.ts",
12
12
  "dependencies": {
13
13
  "@acemir/cssom": "^0.9.31",
14
- "@pairdraw/emit": "^0.19.2",
14
+ "@pairdraw/emit": "^0.20.0",
15
15
  "css-tree": "^3.2.1",
16
16
  "cssesc": "^3.0.0",
17
17
  "dedent": "^1.7.2",
@@ -24,7 +24,7 @@
24
24
  "twirlwind": "^0.4.1"
25
25
  },
26
26
  "peerDependencies": {
27
- "@pairdraw/scene-graph": "^0.19.2"
27
+ "@pairdraw/scene-graph": "^0.20.0"
28
28
  },
29
29
  "exports": {
30
30
  ".": {
package/src/export/css.ts CHANGED
@@ -107,9 +107,10 @@ export type BorderSide = 'top' | 'right' | 'bottom' | 'left'
107
107
  * border insets the box its absolutely positioned children are placed in, where a stroke does not.
108
108
  */
109
109
  export function borderWidths(node: SceneNode): Record<BorderSide, number> {
110
- if (!strokeColorToCSS(node.strokes.at(0))) return { top: 0, right: 0, bottom: 0, left: 0 }
110
+ const stroke = node.strokes.find((item) => item.visible)
111
+ if (!strokeColorToCSS(stroke)) return { top: 0, right: 0, bottom: 0, left: 0 }
111
112
  if (!node.independentStrokeWeights) {
112
- const weight = node.strokes[0]?.weight ?? 0
113
+ const weight = stroke?.weight ?? 0
113
114
  return { top: weight, right: weight, bottom: weight, left: weight }
114
115
  }
115
116
  return {
@@ -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. */
@@ -336,6 +365,15 @@ function styleFromSceneNode(
336
365
  addImageStyle(style, node)
337
366
  addStroke(style, node, css)
338
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
+ }
339
377
  if (node.opacity < 1) style.opacity = css('opacity', String(node.opacity))
340
378
  const blend = CSS_BLEND_MODES[node.blendMode]
341
379
  if (blend) style['mix-blend-mode'] = blend
@@ -481,6 +519,59 @@ function vectorDesignNode(
481
519
  }
482
520
  }
483
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
+
484
575
  function sceneNodeToDesignNode(
485
576
  context: ProjectionContext,
486
577
  node: SceneNode,
@@ -514,9 +605,7 @@ function sceneNodeToDesignNode(
514
605
  if (vector) return vector
515
606
  }
516
607
 
517
- const children = nodeChildren(graph, node)
518
- .map((child) => sceneNodeToDesignNode(context, child, modes, false))
519
- .filter((child): child is DesignNode => child !== null)
608
+ const children = projectChildren(context, node, modes, false)
520
609
 
521
610
  if (node.type === 'CANVAS') {
522
611
  return {
@@ -571,11 +660,7 @@ export function sceneGraphToDesignDocument(
571
660
  const root = graph.getNode(options.rootId ?? graph.rootId)
572
661
  const context = projectionContext(graph, options)
573
662
  const modes = context.tokens?.defaultModes() ?? new Map<string, string>()
574
- const children = root
575
- ? nodeChildren(graph, root)
576
- .map((child) => sceneNodeToDesignNode(context, child, modes, true))
577
- .filter((child): child is DesignNode => child !== null)
578
- : []
663
+ const children = root ? projectChildren(context, root, modes, true) : []
579
664
 
580
665
  return { type: 'document', sourceGraph: graph, tokens: context.tokens, children }
581
666
  }
@@ -1,25 +1,9 @@
1
- import { DEFAULT_FONT_FAMILY } from '@pairdraw/scene-graph/constants'
2
- import { parseCSSFontFamilies } from '@pairdraw/scene-graph/css'
3
-
4
- /** CSS generic families, by the concrete face class they stand for. */
5
- const GENERIC_FAMILIES: Record<string, 'sans-serif' | 'serif' | 'monospace' | undefined> = {
6
- 'sans-serif': 'sans-serif',
7
- 'ui-sans-serif': 'sans-serif',
8
- 'system-ui': 'sans-serif',
9
- '-apple-system': 'sans-serif',
10
- blinkmacsystemfont: 'sans-serif',
11
- serif: 'serif',
12
- 'ui-serif': 'serif',
13
- monospace: 'monospace',
14
- 'ui-monospace': 'monospace'
15
- }
16
-
17
- /** Known system faces of each class, best first. The app bundles Inter for sans-serif. */
18
- const DEFAULT_FAMILIES = {
19
- 'sans-serif': [DEFAULT_FONT_FAMILY],
20
- serif: ['Georgia', 'Times New Roman', 'Times'],
21
- monospace: ['Menlo', 'SF Mono', 'Consolas', 'Courier New']
22
- } as const satisfies Record<'sans-serif' | 'serif' | 'monospace', readonly string[]>
1
+ import {
2
+ GENERIC_FONT_FAMILIES_BY_CLASS,
3
+ genericFontFamilyClass,
4
+ parseCSSFontFamilies,
5
+ type GenericFontClass
6
+ } from '@pairdraw/scene-graph/css'
23
7
 
24
8
  export interface FontStackOptions {
25
9
  /**
@@ -29,11 +13,8 @@ export interface FontStackOptions {
29
13
  isAvailable?: (family: string) => boolean
30
14
  }
31
15
 
32
- function defaultFamily(
33
- kind: 'sans-serif' | 'serif' | 'monospace',
34
- options: FontStackOptions
35
- ): string {
36
- const candidates = DEFAULT_FAMILIES[kind]
16
+ function defaultFamily(kind: GenericFontClass, options: FontStackOptions): string {
17
+ const candidates = GENERIC_FONT_FAMILIES_BY_CLASS[kind]
37
18
  return (
38
19
  (options.isAvailable ? candidates.find((family) => options.isAvailable?.(family)) : null) ??
39
20
  candidates[0]
@@ -50,8 +31,8 @@ export function resolveFontFamilyStack(
50
31
  options: FontStackOptions = {}
51
32
  ): string | null {
52
33
  for (const family of parseCSSFontFamilies(value)) {
53
- const generic = GENERIC_FAMILIES[family.trim().toLowerCase()]
54
- if (generic) return defaultFamily(generic, options)
34
+ const kind = genericFontFamilyClass(family)
35
+ if (kind) return defaultFamily(kind, options)
55
36
  if (options.isAvailable && !options.isAvailable(family)) continue
56
37
  return family
57
38
  }
@@ -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,
@@ -51,9 +52,10 @@ const IMAGE_SOURCE_URL_KEY = 'image-source-url'
51
52
  * by half its blur length, which the canvas draws from the radius that gives that sigma.
52
53
  */
53
54
  function cssShadows(value: string | undefined) {
54
- return parseCSSShadows(value).map((effect) =>
55
- effect.type === 'SHADER' ? effect : { ...effect, radius: blurRadiusForSigma(effect.radius / 2) }
56
- )
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
+ })
57
59
  }
58
60
 
59
61
  export interface DesignDocumentToSceneGraphOptions {
@@ -365,11 +367,10 @@ function applyImageFill(
365
367
  const bytes = bytesFromDataURL(source)
366
368
  if (!bytes) {
367
369
  if (source) {
368
- node.pluginData.push({
369
- pluginId: DOM_CSS_PLUGIN_ID,
370
- key: IMAGE_SOURCE_URL_KEY,
371
- value: source
372
- })
370
+ node.pluginData = [
371
+ ...node.pluginData,
372
+ { pluginId: DOM_CSS_PLUGIN_ID, key: IMAGE_SOURCE_URL_KEY, value: source }
373
+ ]
373
374
  }
374
375
  return
375
376
  }