@pairdraw/dom-css 0.19.0 → 0.19.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/dist/browser.d.ts +16 -1
- package/dist/browser.js +2 -2
- package/dist/chunks/behaviours/controls.js +34 -2
- package/dist/chunks/behaviours/controls.js.map +1 -1
- package/dist/chunks/behaviours/states/layers.js +25 -0
- package/dist/chunks/behaviours/states/layers.js.map +1 -1
- package/dist/chunks/behaviours/states/tailwind.js +17 -2
- package/dist/chunks/behaviours/states/tailwind.js.map +1 -1
- package/dist/chunks/browser.js +228 -49
- package/dist/chunks/browser.js.map +1 -1
- package/dist/chunks/export/components/groups.js +3 -2
- package/dist/chunks/export/components/groups.js.map +1 -1
- package/dist/chunks/export/components/layers.js +50 -8
- package/dist/chunks/export/components/layers.js.map +1 -1
- package/dist/export.d.ts +9 -0
- package/dist/scene-graph2.d.ts +6 -0
- package/package.json +3 -3
- package/src/behaviours/controls.ts +49 -2
- package/src/behaviours/states/tailwind.ts +4 -1
- package/src/browser.ts +1 -0
- package/src/export/components/model.ts +60 -7
- package/src/export/components/react/index.ts +4 -0
- package/src/export/components/repeats.ts +7 -2
- package/src/export/components/vue.ts +38 -9
- package/src/export/font-size-class.ts +13 -0
- package/src/export/html.ts +5 -1
- package/src/export/projection.ts +26 -0
- package/src/import/flow.ts +15 -0
- package/src/import/fonts.ts +59 -0
- package/src/import/scene-graph.ts +190 -47
|
@@ -28,8 +28,10 @@ import {
|
|
|
28
28
|
elementDisplay,
|
|
29
29
|
flowOf,
|
|
30
30
|
inheritedTextStyle,
|
|
31
|
+
type FlowKind,
|
|
31
32
|
type ParentFlow
|
|
32
33
|
} from './flow'
|
|
34
|
+
import { resolveFontFamilyStack } from './fonts'
|
|
33
35
|
import {
|
|
34
36
|
blockRichText,
|
|
35
37
|
characterStyleFromCSS,
|
|
@@ -56,6 +58,39 @@ function cssShadows(value: string | undefined) {
|
|
|
56
58
|
|
|
57
59
|
export interface DesignDocumentToSceneGraphOptions {
|
|
58
60
|
pageName?: string
|
|
61
|
+
/**
|
|
62
|
+
* The family a CSS `font-family` stack should draw, given the stack's families; null keeps it.
|
|
63
|
+
* Hosts with a font service pass one that skips faces the document cannot draw; without it the
|
|
64
|
+
* first concrete family wins and a generic keyword maps to a concrete face of its class.
|
|
65
|
+
*/
|
|
66
|
+
resolveFontFamily?: (value: string) => string | null
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The document with every element's font stack resolved to the family its text layer draws, so
|
|
71
|
+
* the per-character CSS parse sees a concrete name instead of a generic keyword.
|
|
72
|
+
*/
|
|
73
|
+
function withResolvedFontFamilies(
|
|
74
|
+
node: DesignNode,
|
|
75
|
+
resolve: (value: string) => string | null
|
|
76
|
+
): DesignNode {
|
|
77
|
+
if (node.type !== 'element') return node
|
|
78
|
+
const resolved = (
|
|
79
|
+
style: DesignStyleDeclaration | undefined
|
|
80
|
+
): DesignStyleDeclaration | undefined => {
|
|
81
|
+
const stack = style?.['font-family']
|
|
82
|
+
const family = stack === undefined ? null : resolve(stack)
|
|
83
|
+
return family && family !== stack ? { ...style, 'font-family': family } : style
|
|
84
|
+
}
|
|
85
|
+
const computedStyle = resolved(node.computedStyle)
|
|
86
|
+
const inlineStyle = resolved(node.inlineStyle)
|
|
87
|
+
const next: DesignElement = {
|
|
88
|
+
...node,
|
|
89
|
+
children: node.children.map((child) => withResolvedFontFamilies(child, resolve))
|
|
90
|
+
}
|
|
91
|
+
if (computedStyle) next.computedStyle = computedStyle
|
|
92
|
+
if (inlineStyle) next.inlineStyle = inlineStyle
|
|
93
|
+
return next
|
|
59
94
|
}
|
|
60
95
|
|
|
61
96
|
function textContent(node: DesignNode): string {
|
|
@@ -381,6 +416,8 @@ function applyElementStyle(
|
|
|
381
416
|
}
|
|
382
417
|
node.strokes = strokes
|
|
383
418
|
setBorderWeights(node, style, strokes[0])
|
|
419
|
+
// A CSS border takes room: content sits inside it, as auto layout with strokes included does.
|
|
420
|
+
node.strokesIncludedInLayout = true
|
|
384
421
|
}
|
|
385
422
|
|
|
386
423
|
const effects = cssShadows(pickStyle(style, 'box-shadow'))
|
|
@@ -498,53 +535,117 @@ function linkText(node: SceneNode, element: DesignElement): void {
|
|
|
498
535
|
}
|
|
499
536
|
}
|
|
500
537
|
|
|
538
|
+
const BOX_PROPERTIES = [
|
|
539
|
+
'aspect-ratio',
|
|
540
|
+
'background-color',
|
|
541
|
+
'border-radius',
|
|
542
|
+
'box-shadow',
|
|
543
|
+
'display',
|
|
544
|
+
'height',
|
|
545
|
+
'width',
|
|
546
|
+
'min-width',
|
|
547
|
+
'max-width',
|
|
548
|
+
'min-height',
|
|
549
|
+
'max-height',
|
|
550
|
+
'object-fit',
|
|
551
|
+
'overflow',
|
|
552
|
+
'position',
|
|
553
|
+
'top',
|
|
554
|
+
'right',
|
|
555
|
+
'bottom',
|
|
556
|
+
'left',
|
|
557
|
+
'flex-wrap',
|
|
558
|
+
'align-self'
|
|
559
|
+
]
|
|
560
|
+
|
|
561
|
+
const PADDING_PROPERTIES = [
|
|
562
|
+
'padding',
|
|
563
|
+
'padding-top',
|
|
564
|
+
'padding-right',
|
|
565
|
+
'padding-bottom',
|
|
566
|
+
'padding-left',
|
|
567
|
+
'padding-block',
|
|
568
|
+
'padding-inline'
|
|
569
|
+
]
|
|
570
|
+
|
|
571
|
+
const BORDER_SIDES = ['top', 'right', 'bottom', 'left'] as const
|
|
572
|
+
|
|
573
|
+
/** Whether every length of a value is zero, as `0`, `0px`, and `0 0` are. */
|
|
574
|
+
function isZeroLength(value: string): boolean {
|
|
575
|
+
return value
|
|
576
|
+
.trim()
|
|
577
|
+
.split(/\s+/)
|
|
578
|
+
.every((part) => parseCSSNumber(part) === 0)
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/** Whether any side's border draws: it has a style other than none and a width other than zero. */
|
|
582
|
+
function drawsBorder(style: DesignStyleDeclaration): boolean {
|
|
583
|
+
return BORDER_SIDES.some((side) => {
|
|
584
|
+
const borderStyle = pickStyle(style, `border-${side}-style`) ?? pickStyle(style, 'border-style')
|
|
585
|
+
if (borderStyle === undefined || borderStyle === 'none' || borderStyle === 'hidden')
|
|
586
|
+
return false
|
|
587
|
+
// A styled border without a width is CSS's medium one.
|
|
588
|
+
const width = pickStyle(style, `border-${side}-width`) ?? pickStyle(style, 'border-width')
|
|
589
|
+
return width === undefined || !isZeroLength(width)
|
|
590
|
+
})
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Whether the element is a box rather than a run of the text around it. Padding and borders of
|
|
595
|
+
* no size, which resets such as Tailwind's give every element, leave it a run.
|
|
596
|
+
*/
|
|
501
597
|
function hasBoxStyle(style: DesignStyleDeclaration): boolean {
|
|
502
|
-
return
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
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)
|
|
598
|
+
if (BOX_PROPERTIES.some((property) => pickStyle(style, property) !== undefined)) return true
|
|
599
|
+
if (
|
|
600
|
+
PADDING_PROPERTIES.some((property) => {
|
|
601
|
+
const value = pickStyle(style, property)
|
|
602
|
+
return value !== undefined && !isZeroLength(value)
|
|
603
|
+
})
|
|
604
|
+
) {
|
|
605
|
+
return true
|
|
606
|
+
}
|
|
607
|
+
return drawsBorder(style)
|
|
542
608
|
}
|
|
543
609
|
|
|
544
610
|
function hasContent(element: DesignElement): boolean {
|
|
545
611
|
return element.children.some((child) => child.type !== 'text' || child.text.trim().length > 0)
|
|
546
612
|
}
|
|
547
613
|
|
|
614
|
+
/** Whether a child sits in a line of its parent's content or leaves it, rather than a block. */
|
|
615
|
+
function isInLine(node: DesignNode): boolean {
|
|
616
|
+
if (node.type === 'text' || node.tagName.toLowerCase() === 'br') return true
|
|
617
|
+
const style = mergedStyle(node)
|
|
618
|
+
const display = elementDisplay(node, style)
|
|
619
|
+
const position = pickStyle(style, 'position')
|
|
620
|
+
return (
|
|
621
|
+
display.startsWith('inline') ||
|
|
622
|
+
display === 'none' ||
|
|
623
|
+
position === 'absolute' ||
|
|
624
|
+
position === 'fixed'
|
|
625
|
+
)
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Whether a block holds inline content with boxes in it, such as a paragraph with a styled
|
|
630
|
+
* `<code>`, which CSS lays out in lines rather than stacking each run and box.
|
|
631
|
+
*/
|
|
632
|
+
function holdsInlineBoxes(element: DesignElement): boolean {
|
|
633
|
+
return (
|
|
634
|
+
element.children.some((child) => child.type === 'element') && element.children.every(isInLine)
|
|
635
|
+
)
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/** The flow a box lays its children out in, and whether they are one text instead. */
|
|
639
|
+
function containerFlow(element: DesignElement, display: string) {
|
|
640
|
+
// A box without content keeps its own size, as an image does, unless it declares a layout.
|
|
641
|
+
const own = hasContent(element) || flowOf(display) === 'flex' ? flowOf(display) : null
|
|
642
|
+
const richText = own !== 'flex' && holdsRichText(element)
|
|
643
|
+
// Inline content that cannot be one text runs in lines that wrap at the block's width.
|
|
644
|
+
const lines = own === 'block' && !richText && holdsInlineBoxes(element)
|
|
645
|
+
const flow: FlowKind | null = lines ? 'inline' : own
|
|
646
|
+
return { flow, richText, lines }
|
|
647
|
+
}
|
|
648
|
+
|
|
548
649
|
function createElementNode(
|
|
549
650
|
graph: SceneGraph,
|
|
550
651
|
parentId: string,
|
|
@@ -553,7 +654,8 @@ function createElementNode(
|
|
|
553
654
|
): SceneNode | null {
|
|
554
655
|
const style = mergedStyle(element)
|
|
555
656
|
const display = elementDisplay(element, style)
|
|
556
|
-
|
|
657
|
+
// A line break between boxes draws nothing; rich text reads it as a new line.
|
|
658
|
+
if (display === 'none' || element.tagName.toLowerCase() === 'br') return null
|
|
557
659
|
if (
|
|
558
660
|
isTextLikeElement(element) &&
|
|
559
661
|
!hasBoxStyle(style) &&
|
|
@@ -573,28 +675,38 @@ function createElementNode(
|
|
|
573
675
|
clipsContent: false
|
|
574
676
|
})
|
|
575
677
|
applyElementStyle(graph, node, element, style)
|
|
576
|
-
|
|
577
|
-
const flow = hasContent(element) || flowOf(display) === 'flex' ? flowOf(display) : null
|
|
678
|
+
const { flow, richText, lines } = containerFlow(element, display)
|
|
578
679
|
if (flow) {
|
|
579
680
|
applyContainerLayout(node, element, style, flow)
|
|
580
681
|
if (flow === 'flex') applyFlexGap(node, style)
|
|
682
|
+
if (lines) node.layoutWrap = 'WRAP'
|
|
581
683
|
}
|
|
582
684
|
applyFlowSizing(graph, node, style, display, parentFlow)
|
|
583
685
|
|
|
584
686
|
// Blocks of text in a box's own flow are one text; a flex box's children are items.
|
|
585
|
-
if (
|
|
687
|
+
if (richText) {
|
|
586
688
|
const textStyle = inheritedTextStyle(element, style)
|
|
587
689
|
const textDisplay = flow === 'inline' ? 'inline' : 'block'
|
|
588
690
|
createRichTextNode(graph, node.id, element, textStyle, textDisplay, flow ?? 'none')
|
|
589
691
|
return node
|
|
590
692
|
}
|
|
591
|
-
|
|
693
|
+
const children = flow === 'flex' ? element.children.toSorted(byFlexOrder) : element.children
|
|
694
|
+
for (const child of children) {
|
|
592
695
|
createDesignNode(graph, node.id, child, flow ?? 'none', inheritedTextStyle(element, style))
|
|
593
696
|
}
|
|
594
697
|
|
|
595
698
|
return node
|
|
596
699
|
}
|
|
597
700
|
|
|
701
|
+
function flexOrder(node: DesignNode): number {
|
|
702
|
+
return node.type === 'element' ? (parseCSSNumber(pickStyle(mergedStyle(node), 'order')) ?? 0) : 0
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/** Flex items in the order CSS places them: by `order`, then as written. */
|
|
706
|
+
function byFlexOrder(a: DesignNode, b: DesignNode): number {
|
|
707
|
+
return flexOrder(a) - flexOrder(b)
|
|
708
|
+
}
|
|
709
|
+
|
|
598
710
|
function createDesignNode(
|
|
599
711
|
graph: SceneGraph,
|
|
600
712
|
parentId: string,
|
|
@@ -609,7 +721,33 @@ function createDesignNode(
|
|
|
609
721
|
return createTextNode(graph, parentId, node.text, inheritedStyle, display, parentFlow)
|
|
610
722
|
}
|
|
611
723
|
|
|
612
|
-
return createElementNode(graph, parentId, node, parentFlow)
|
|
724
|
+
return createElementNode(graph, parentId, withFieldText(node), parentFlow)
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/** Input types whose value is text a person reads in the field. */
|
|
728
|
+
const TEXT_INPUT_TYPES = new Set([
|
|
729
|
+
'',
|
|
730
|
+
'text',
|
|
731
|
+
'email',
|
|
732
|
+
'search',
|
|
733
|
+
'url',
|
|
734
|
+
'tel',
|
|
735
|
+
'number',
|
|
736
|
+
'password'
|
|
737
|
+
])
|
|
738
|
+
|
|
739
|
+
/**
|
|
740
|
+
* A text field with the text a browser draws in it, its value or else its placeholder, as a
|
|
741
|
+
* child: an `<input>` holds no content of its own. A password shows as dots.
|
|
742
|
+
*/
|
|
743
|
+
function withFieldText(element: DesignElement): DesignElement {
|
|
744
|
+
if (element.tagName.toLowerCase() !== 'input' || element.children.length > 0) return element
|
|
745
|
+
const type = Object.hasOwn(element.attrs, 'type') ? element.attrs.type.trim().toLowerCase() : ''
|
|
746
|
+
if (!TEXT_INPUT_TYPES.has(type)) return element
|
|
747
|
+
const value = element.attrs.value || element.attrs.placeholder
|
|
748
|
+
if (!value) return element
|
|
749
|
+
const text = type === 'password' && element.attrs.value ? '•'.repeat(value.length) : value
|
|
750
|
+
return { ...element, children: [{ type: 'text', text }] }
|
|
613
751
|
}
|
|
614
752
|
|
|
615
753
|
function fitPageToChildren(page: SceneNode, graph: SceneGraph): void {
|
|
@@ -623,12 +761,17 @@ export function designDocumentToSceneGraph(
|
|
|
623
761
|
document: DesignDocument,
|
|
624
762
|
options: DesignDocumentToSceneGraphOptions = {}
|
|
625
763
|
): SceneGraph {
|
|
764
|
+
const resolve = options.resolveFontFamily ?? resolveFontFamilyStack
|
|
765
|
+
const resolved: DesignDocument = {
|
|
766
|
+
...document,
|
|
767
|
+
children: document.children.map((child) => withResolvedFontFamilies(child, resolve))
|
|
768
|
+
}
|
|
626
769
|
const graph = new SceneGraph()
|
|
627
770
|
const page = graph.getPages().find((node) => node.type === 'CANVAS') ?? graph.addPage('DesignDOM')
|
|
628
771
|
|
|
629
772
|
page.name = options.pageName ?? 'DesignDOM'
|
|
630
773
|
|
|
631
|
-
for (const child of
|
|
774
|
+
for (const child of resolved.children) {
|
|
632
775
|
createDesignNode(graph, page.id, child, 'none')
|
|
633
776
|
}
|
|
634
777
|
|