@cratis/scene.react 2.0.0 → 3.1.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/README.md +29 -2
- package/core/corePackage.ts +2 -0
- package/dist/cjs/core/corePackage.js +3 -1
- package/dist/cjs/core/corePackage.js.map +1 -1
- package/dist/cjs/renderer/createReactRenderer.js +21 -6
- package/dist/cjs/renderer/createReactRenderer.js.map +1 -1
- package/dist/cjs/renderer/panelLayout.js +108 -0
- package/dist/cjs/renderer/panelLayout.js.map +1 -0
- package/dist/esm/core/corePackage.d.ts.map +1 -1
- package/dist/esm/core/corePackage.js +3 -1
- package/dist/esm/core/corePackage.js.map +1 -1
- package/dist/esm/for_panelLayout/when_arranging_a_panel.d.ts +2 -0
- package/dist/esm/for_panelLayout/when_arranging_a_panel.d.ts.map +1 -0
- package/dist/esm/for_panelLayout/when_arranging_a_panel.js +91 -0
- package/dist/esm/for_panelLayout/when_arranging_a_panel.js.map +1 -0
- package/dist/esm/renderer/createReactRenderer.d.ts.map +1 -1
- package/dist/esm/renderer/createReactRenderer.js +21 -6
- package/dist/esm/renderer/createReactRenderer.js.map +1 -1
- package/dist/esm/renderer/panelLayout.d.ts +5 -0
- package/dist/esm/renderer/panelLayout.d.ts.map +1 -0
- package/dist/esm/renderer/panelLayout.js +105 -0
- package/dist/esm/renderer/panelLayout.js.map +1 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/for_panelLayout/when_arranging_a_panel.ts +112 -0
- package/package.json +15 -5
- package/renderer/createReactRenderer.tsx +21 -5
- package/renderer/panelLayout.ts +109 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { isGrid, isDockPanel, isWrapPanel, isStackPanel } from '@cratis/scene.engine';
|
|
2
|
+
import { Dock, Orientation, GridUnitType } from '@cratis/scene.model';
|
|
3
|
+
|
|
4
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
5
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
6
|
+
/**
|
|
7
|
+
* Turns a panel into the CSS that arranges it, and a child into the CSS that places it inside one.
|
|
8
|
+
*
|
|
9
|
+
* The panels say what an arrangement *is*; this says what it looks like on the web. A second renderer -
|
|
10
|
+
* native, desktop - answers the same question with its own platform's layout primitives, which is why
|
|
11
|
+
* this lives in `Scene.React` rather than in the model or the engine.
|
|
12
|
+
*
|
|
13
|
+
* Where a panel needs to say something about an individual child - which grid cell, which edge - it does
|
|
14
|
+
* so through that child's own `properties` bag, because the model has no attached properties of its own.
|
|
15
|
+
* The keys are the ones the model's documentation names: `Grid.Row`, `Grid.Column`, `Grid.RowSpan`,
|
|
16
|
+
* `Grid.ColumnSpan` and `Dock`.
|
|
17
|
+
*/ const track = (length)=>{
|
|
18
|
+
switch(length.unitType){
|
|
19
|
+
case GridUnitType.Auto:
|
|
20
|
+
return 'auto';
|
|
21
|
+
case GridUnitType.Absolute:
|
|
22
|
+
return `${length.value}px`;
|
|
23
|
+
case GridUnitType.Star:
|
|
24
|
+
return `${length.value}fr`;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const bound = (minimum, maximum, size)=>minimum === 0 && !Number.isFinite(maximum) ? size : `minmax(${minimum}px, ${Number.isFinite(maximum) ? `${maximum}px` : size})`;
|
|
28
|
+
const rowTrack = (row)=>bound(row.minimumHeight, row.maximumHeight, track(row.height));
|
|
29
|
+
const columnTrack = (column)=>bound(column.minimumWidth, column.maximumWidth, track(column.width));
|
|
30
|
+
const number = (value)=>typeof value === 'number' ? value : undefined;
|
|
31
|
+
/** The CSS that arranges a panel's own children. */ function panelStyle(panel) {
|
|
32
|
+
if (isGrid(panel)) {
|
|
33
|
+
return {
|
|
34
|
+
display: 'grid',
|
|
35
|
+
gridTemplateRows: panel.rows.map(rowTrack).join(' ') || undefined,
|
|
36
|
+
gridTemplateColumns: panel.columns.map(columnTrack).join(' ') || undefined
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (isDockPanel(panel)) {
|
|
40
|
+
// Docking has no single CSS primitive. A column of rows with a middle that grows is the shape a
|
|
41
|
+
// dock panel actually produces, and `lastChildFill` decides whether the final child is that
|
|
42
|
+
// middle or just another docked edge.
|
|
43
|
+
return {
|
|
44
|
+
display: 'flex',
|
|
45
|
+
flexDirection: 'column'
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (isStackPanel(panel)) {
|
|
49
|
+
return {
|
|
50
|
+
display: 'flex',
|
|
51
|
+
flexDirection: panel.orientation === Orientation.Horizontal ? 'row' : 'column',
|
|
52
|
+
gap: panel.spacing ? `${panel.spacing}px` : undefined
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (isWrapPanel(panel)) {
|
|
56
|
+
return {
|
|
57
|
+
display: 'flex',
|
|
58
|
+
flexWrap: 'wrap',
|
|
59
|
+
flexDirection: panel.orientation === Orientation.Horizontal ? 'row' : 'column'
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
/** The CSS that places one child within its panel, or `undefined` when the panel places children implicitly. */ function childStyle(panel, child, index) {
|
|
65
|
+
const properties = child.properties ?? {};
|
|
66
|
+
if (isGrid(panel)) {
|
|
67
|
+
const row = number(properties['Grid.Row']);
|
|
68
|
+
const column = number(properties['Grid.Column']);
|
|
69
|
+
const rowSpan = number(properties['Grid.RowSpan']) ?? 1;
|
|
70
|
+
const columnSpan = number(properties['Grid.ColumnSpan']) ?? 1;
|
|
71
|
+
if (row === undefined && column === undefined) return undefined;
|
|
72
|
+
return {
|
|
73
|
+
gridRow: row === undefined ? undefined : `${row + 1} / span ${rowSpan}`,
|
|
74
|
+
gridColumn: column === undefined ? undefined : `${column + 1} / span ${columnSpan}`
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (isDockPanel(panel)) {
|
|
78
|
+
const dock = properties['Dock'];
|
|
79
|
+
const isFill = panel.lastChildFill && index === (panel.children?.length ?? 0) - 1;
|
|
80
|
+
if (isFill) return {
|
|
81
|
+
flex: '1 1 auto',
|
|
82
|
+
minHeight: 0
|
|
83
|
+
};
|
|
84
|
+
// Left and right dock along the cross axis of the column the panel lays out, so they become a row
|
|
85
|
+
// of their own rather than a sibling in the column.
|
|
86
|
+
return dock === Dock.Left || dock === Dock.Right ? {
|
|
87
|
+
alignSelf: dock === Dock.Left ? 'flex-start' : 'flex-end',
|
|
88
|
+
flex: '0 0 auto'
|
|
89
|
+
} : {
|
|
90
|
+
flex: '0 0 auto'
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
if (isWrapPanel(panel)) {
|
|
94
|
+
if (panel.itemWidth === undefined && panel.itemHeight === undefined) return undefined;
|
|
95
|
+
return {
|
|
96
|
+
width: panel.itemWidth,
|
|
97
|
+
height: panel.itemHeight,
|
|
98
|
+
flex: '0 0 auto'
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { childStyle, panelStyle };
|
|
105
|
+
//# sourceMappingURL=panelLayout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"panelLayout.js","sources":["../../../renderer/panelLayout.ts"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport type { CSSProperties } from 'react';\nimport { isDockPanel, isGrid, isStackPanel, isWrapPanel } from '@cratis/scene.engine';\nimport { Dock, GridUnitType, Orientation } from '@cratis/scene.model';\nimport type { ColumnDefinition, GridLength, Panel, RowDefinition, SceneElement } from '@cratis/scene.model';\n\n/**\n * Turns a panel into the CSS that arranges it, and a child into the CSS that places it inside one.\n *\n * The panels say what an arrangement *is*; this says what it looks like on the web. A second renderer -\n * native, desktop - answers the same question with its own platform's layout primitives, which is why\n * this lives in `Scene.React` rather than in the model or the engine.\n *\n * Where a panel needs to say something about an individual child - which grid cell, which edge - it does\n * so through that child's own `properties` bag, because the model has no attached properties of its own.\n * The keys are the ones the model's documentation names: `Grid.Row`, `Grid.Column`, `Grid.RowSpan`,\n * `Grid.ColumnSpan` and `Dock`.\n */\n\nconst track = (length: GridLength): string => {\n switch (length.unitType) {\n case GridUnitType.Auto: return 'auto';\n case GridUnitType.Absolute: return `${length.value}px`;\n case GridUnitType.Star: return `${length.value}fr`;\n }\n};\n\nconst bound = (minimum: number, maximum: number, size: string): string =>\n minimum === 0 && !Number.isFinite(maximum)\n ? size\n : `minmax(${minimum}px, ${Number.isFinite(maximum) ? `${maximum}px` : size})`;\n\nconst rowTrack = (row: RowDefinition): string => bound(row.minimumHeight, row.maximumHeight, track(row.height));\nconst columnTrack = (column: ColumnDefinition): string => bound(column.minimumWidth, column.maximumWidth, track(column.width));\n\nconst number = (value: unknown): number | undefined => (typeof value === 'number' ? value : undefined);\n\n/** The CSS that arranges a panel's own children. */\nexport function panelStyle(panel: Panel): CSSProperties | undefined {\n if (isGrid(panel)) {\n return {\n display: 'grid',\n gridTemplateRows: panel.rows.map(rowTrack).join(' ') || undefined,\n gridTemplateColumns: panel.columns.map(columnTrack).join(' ') || undefined,\n };\n }\n\n if (isDockPanel(panel)) {\n // Docking has no single CSS primitive. A column of rows with a middle that grows is the shape a\n // dock panel actually produces, and `lastChildFill` decides whether the final child is that\n // middle or just another docked edge.\n return { display: 'flex', flexDirection: 'column' };\n }\n\n if (isStackPanel(panel)) {\n return {\n display: 'flex',\n flexDirection: panel.orientation === Orientation.Horizontal ? 'row' : 'column',\n gap: panel.spacing ? `${panel.spacing}px` : undefined,\n };\n }\n\n if (isWrapPanel(panel)) {\n return {\n display: 'flex',\n flexWrap: 'wrap',\n flexDirection: panel.orientation === Orientation.Horizontal ? 'row' : 'column',\n };\n }\n\n return undefined;\n}\n\n/** The CSS that places one child within its panel, or `undefined` when the panel places children implicitly. */\nexport function childStyle(panel: Panel, child: SceneElement, index: number): CSSProperties | undefined {\n const properties = child.properties ?? {};\n\n if (isGrid(panel)) {\n const row = number(properties['Grid.Row']);\n const column = number(properties['Grid.Column']);\n const rowSpan = number(properties['Grid.RowSpan']) ?? 1;\n const columnSpan = number(properties['Grid.ColumnSpan']) ?? 1;\n if (row === undefined && column === undefined) return undefined;\n return {\n gridRow: row === undefined ? undefined : `${row + 1} / span ${rowSpan}`,\n gridColumn: column === undefined ? undefined : `${column + 1} / span ${columnSpan}`,\n };\n }\n\n if (isDockPanel(panel)) {\n const dock = properties['Dock'] as Dock | undefined;\n const isFill = panel.lastChildFill && index === (panel.children?.length ?? 0) - 1;\n if (isFill) return { flex: '1 1 auto', minHeight: 0 };\n // Left and right dock along the cross axis of the column the panel lays out, so they become a row\n // of their own rather than a sibling in the column.\n return dock === Dock.Left || dock === Dock.Right\n ? { alignSelf: dock === Dock.Left ? 'flex-start' : 'flex-end', flex: '0 0 auto' }\n : { flex: '0 0 auto' };\n }\n\n if (isWrapPanel(panel)) {\n if (panel.itemWidth === undefined && panel.itemHeight === undefined) return undefined;\n return { width: panel.itemWidth, height: panel.itemHeight, flex: '0 0 auto' };\n }\n\n return undefined;\n}\n"],"names":["track","length","unitType","GridUnitType","Auto","Absolute","value","Star","bound","minimum","maximum","size","Number","isFinite","rowTrack","row","minimumHeight","maximumHeight","height","columnTrack","column","minimumWidth","maximumWidth","width","number","undefined","panelStyle","panel","isGrid","display","gridTemplateRows","rows","map","join","gridTemplateColumns","columns","isDockPanel","flexDirection","isStackPanel","orientation","Orientation","Horizontal","gap","spacing","isWrapPanel","flexWrap","childStyle","child","index","properties","rowSpan","columnSpan","gridRow","gridColumn","dock","isFill","lastChildFill","children","flex","minHeight","Dock","Left","Right","alignSelf","itemWidth","itemHeight"],"mappings":";;;AAAA;AACA;AAOA;;;;;;;;;;;IAaA,MAAMA,QAAQ,CAACC,MAAAA,GAAAA;AACX,IAAA,OAAQA,OAAOC,QAAQ;AACnB,QAAA,KAAKC,aAAaC,IAAI;YAAE,OAAO,MAAA;AAC/B,QAAA,KAAKD,aAAaE,QAAQ;AAAE,YAAA,OAAO,CAAA,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