@meonode/canvas 1.0.0-beta.1
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/CONTRIBUTING.md +75 -0
- package/LICENSE +21 -0
- package/Readme.md +382 -0
- package/dist/cjs/canvas/canvas.helper.d.ts +57 -0
- package/dist/cjs/canvas/canvas.helper.d.ts.map +1 -0
- package/dist/cjs/canvas/canvas.helper.js +239 -0
- package/dist/cjs/canvas/canvas.helper.js.map +1 -0
- package/dist/cjs/canvas/canvas.type.d.ts +657 -0
- package/dist/cjs/canvas/canvas.type.d.ts.map +1 -0
- package/dist/cjs/canvas/grid.canvas.util.d.ts +39 -0
- package/dist/cjs/canvas/grid.canvas.util.d.ts.map +1 -0
- package/dist/cjs/canvas/grid.canvas.util.js +263 -0
- package/dist/cjs/canvas/grid.canvas.util.js.map +1 -0
- package/dist/cjs/canvas/image.canvas.util.d.ts +34 -0
- package/dist/cjs/canvas/image.canvas.util.d.ts.map +1 -0
- package/dist/cjs/canvas/image.canvas.util.js +310 -0
- package/dist/cjs/canvas/image.canvas.util.js.map +1 -0
- package/dist/cjs/canvas/layout.canvas.util.d.ts +123 -0
- package/dist/cjs/canvas/layout.canvas.util.d.ts.map +1 -0
- package/dist/cjs/canvas/layout.canvas.util.js +785 -0
- package/dist/cjs/canvas/layout.canvas.util.js.map +1 -0
- package/dist/cjs/canvas/root.canvas.util.d.ts +42 -0
- package/dist/cjs/canvas/root.canvas.util.d.ts.map +1 -0
- package/dist/cjs/canvas/root.canvas.util.js +140 -0
- package/dist/cjs/canvas/root.canvas.util.js.map +1 -0
- package/dist/cjs/canvas/text.canvas.util.d.ts +148 -0
- package/dist/cjs/canvas/text.canvas.util.d.ts.map +1 -0
- package/dist/cjs/canvas/text.canvas.util.js +1112 -0
- package/dist/cjs/canvas/text.canvas.util.js.map +1 -0
- package/dist/cjs/constant/common.const.d.ts +37 -0
- package/dist/cjs/constant/common.const.d.ts.map +1 -0
- package/dist/cjs/constant/common.const.js +51 -0
- package/dist/cjs/constant/common.const.js.map +1 -0
- package/dist/cjs/index.d.ts +7 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +31 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/canvas/canvas.helper.d.ts +57 -0
- package/dist/esm/canvas/canvas.helper.d.ts.map +1 -0
- package/dist/esm/canvas/canvas.helper.js +214 -0
- package/dist/esm/canvas/canvas.type.d.ts +657 -0
- package/dist/esm/canvas/canvas.type.d.ts.map +1 -0
- package/dist/esm/canvas/grid.canvas.util.d.ts +39 -0
- package/dist/esm/canvas/grid.canvas.util.d.ts.map +1 -0
- package/dist/esm/canvas/grid.canvas.util.js +259 -0
- package/dist/esm/canvas/image.canvas.util.d.ts +34 -0
- package/dist/esm/canvas/image.canvas.util.d.ts.map +1 -0
- package/dist/esm/canvas/image.canvas.util.js +306 -0
- package/dist/esm/canvas/layout.canvas.util.d.ts +123 -0
- package/dist/esm/canvas/layout.canvas.util.d.ts.map +1 -0
- package/dist/esm/canvas/layout.canvas.util.js +777 -0
- package/dist/esm/canvas/root.canvas.util.d.ts +42 -0
- package/dist/esm/canvas/root.canvas.util.d.ts.map +1 -0
- package/dist/esm/canvas/root.canvas.util.js +116 -0
- package/dist/esm/canvas/text.canvas.util.d.ts +148 -0
- package/dist/esm/canvas/text.canvas.util.d.ts.map +1 -0
- package/dist/esm/canvas/text.canvas.util.js +1108 -0
- package/dist/esm/constant/common.const.d.ts +37 -0
- package/dist/esm/constant/common.const.d.ts.map +1 -0
- package/dist/esm/constant/common.const.js +23 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/meonode-canvas-1.0.0-beta.1.tgz +0 -0
- package/package.json +79 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { GridProps } from '../canvas/canvas.type.js';
|
|
2
|
+
import { BoxNode, RowNode } from '../canvas/layout.canvas.util.js';
|
|
3
|
+
/**
|
|
4
|
+
* Grid layout node that arranges children in a configurable number of columns or rows.
|
|
5
|
+
* Uses Yoga's flexbox capabilities with wrapping and gap properties to simulate a grid.
|
|
6
|
+
* @extends RowNode
|
|
7
|
+
*/
|
|
8
|
+
export declare class GridNode extends RowNode {
|
|
9
|
+
private readonly columns;
|
|
10
|
+
private readonly columnGapValue;
|
|
11
|
+
private readonly rowGapValue;
|
|
12
|
+
private readonly isVertical;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new grid layout node
|
|
15
|
+
* @param props - Grid configuration properties
|
|
16
|
+
*/
|
|
17
|
+
constructor(props: GridProps);
|
|
18
|
+
/**
|
|
19
|
+
* Appends a child node to this grid.
|
|
20
|
+
* Overridden primarily for documentation/clarity, functionality is inherited.
|
|
21
|
+
* @param child - Child node to append
|
|
22
|
+
* @param index - Index at which to insert the child
|
|
23
|
+
*/
|
|
24
|
+
protected appendChild(child: BoxNode, index: number): void;
|
|
25
|
+
/**
|
|
26
|
+
* Update layout calculations after the initial layout is computed.
|
|
27
|
+
* This method calculates the appropriate flex-basis for children based on the
|
|
28
|
+
* number of columns and gaps, respecting the container's padding,
|
|
29
|
+
* and applies the gaps using Yoga's built-in properties.
|
|
30
|
+
*/
|
|
31
|
+
protected updateLayoutBasedOnComputedSize(): void;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Factory function to create a new GridNode instance.
|
|
35
|
+
* @param props - Grid configuration properties.
|
|
36
|
+
* @returns A new GridNode instance.
|
|
37
|
+
*/
|
|
38
|
+
export declare const Grid: (props: GridProps) => GridNode;
|
|
39
|
+
//# sourceMappingURL=grid.canvas.util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grid.canvas.util.d.ts","sourceRoot":"","sources":["../../../src/canvas/grid.canvas.util.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAA;AAGjE;;;;GAIG;AACH,qBAAa,QAAS,SAAQ,OAAO;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAQ;IAChC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAuB;IACtD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAuB;IACnD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IAEpC;;;OAGG;gBACS,KAAK,EAAE,SAAS;IAsD5B;;;;;OAKG;cACgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM;IAI5D;;;;;OAKG;cACgB,+BAA+B;CAsMnD;AAED;;;;GAIG;AACH,eAAO,MAAM,IAAI,GAAI,OAAO,SAAS,aAAwB,CAAA"}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { RowNode } from './layout.canvas.util.js';
|
|
2
|
+
import { Style } from '../constant/common.const.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Grid layout node that arranges children in a configurable number of columns or rows.
|
|
6
|
+
* Uses Yoga's flexbox capabilities with wrapping and gap properties to simulate a grid.
|
|
7
|
+
* @extends RowNode
|
|
8
|
+
*/
|
|
9
|
+
class GridNode extends RowNode {
|
|
10
|
+
columns;
|
|
11
|
+
columnGapValue;
|
|
12
|
+
rowGapValue;
|
|
13
|
+
isVertical; // True if the main axis is vertical (flexDirection: column or column-reverse)
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new grid layout node
|
|
16
|
+
* @param props - Grid configuration properties
|
|
17
|
+
*/
|
|
18
|
+
constructor(props) {
|
|
19
|
+
const columns = Math.max(1, props.columns || 1);
|
|
20
|
+
const direction = props.direction || 'row'; // Default to horizontal row
|
|
21
|
+
const isVertical = direction === 'column' || direction === 'column-reverse';
|
|
22
|
+
// Map direction string to Yoga FlexDirection
|
|
23
|
+
let flexDirection;
|
|
24
|
+
switch (direction) {
|
|
25
|
+
case 'row':
|
|
26
|
+
flexDirection = Style.FlexDirection.Row;
|
|
27
|
+
break;
|
|
28
|
+
case 'column':
|
|
29
|
+
flexDirection = Style.FlexDirection.Column;
|
|
30
|
+
break;
|
|
31
|
+
case 'row-reverse':
|
|
32
|
+
flexDirection = Style.FlexDirection.RowReverse;
|
|
33
|
+
break;
|
|
34
|
+
case 'column-reverse':
|
|
35
|
+
flexDirection = Style.FlexDirection.ColumnReverse;
|
|
36
|
+
break;
|
|
37
|
+
default:
|
|
38
|
+
console.warn(`[GridNode] Invalid direction "${direction}". Defaulting to "row".`);
|
|
39
|
+
flexDirection = Style.FlexDirection.Row;
|
|
40
|
+
}
|
|
41
|
+
// Determine the column and row gap values from props
|
|
42
|
+
let columnGap = 0;
|
|
43
|
+
let rowGap = 0;
|
|
44
|
+
if (typeof props.gap === 'number' || (typeof props.gap === 'string' && props.gap.trim() !== '')) {
|
|
45
|
+
// Single value applies to both row and column gaps
|
|
46
|
+
columnGap = props.gap;
|
|
47
|
+
rowGap = props.gap;
|
|
48
|
+
}
|
|
49
|
+
else if (props.gap && typeof props.gap === 'object') {
|
|
50
|
+
// Object format: prioritize a specific direction (Column/Row), then All
|
|
51
|
+
columnGap = props.gap.Column ?? props.gap.All ?? 0;
|
|
52
|
+
rowGap = props.gap.Row ?? props.gap.All ?? 0;
|
|
53
|
+
}
|
|
54
|
+
super({
|
|
55
|
+
name: 'Grid',
|
|
56
|
+
flexWrap: Style.Wrap.Wrap, // Essential for grid behavior
|
|
57
|
+
flexDirection,
|
|
58
|
+
...props,
|
|
59
|
+
// Explicitly remove the 'direction' prop passed to super, as it's handled by flexDirection
|
|
60
|
+
direction: undefined,
|
|
61
|
+
});
|
|
62
|
+
this.columns = columns;
|
|
63
|
+
this.columnGapValue = columnGap;
|
|
64
|
+
this.rowGapValue = rowGap;
|
|
65
|
+
this.isVertical = isVertical;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Appends a child node to this grid.
|
|
69
|
+
* Overridden primarily for documentation/clarity, functionality is inherited.
|
|
70
|
+
* @param child - Child node to append
|
|
71
|
+
* @param index - Index at which to insert the child
|
|
72
|
+
*/
|
|
73
|
+
appendChild(child, index) {
|
|
74
|
+
super.appendChild(child, index);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Update layout calculations after the initial layout is computed.
|
|
78
|
+
* This method calculates the appropriate flex-basis for children based on the
|
|
79
|
+
* number of columns and gaps, respecting the container's padding,
|
|
80
|
+
* and applies the gaps using Yoga's built-in properties.
|
|
81
|
+
*/
|
|
82
|
+
updateLayoutBasedOnComputedSize() {
|
|
83
|
+
// Step 1: Early return if the grid is empty or invalid
|
|
84
|
+
if (this.columns <= 0 || this.children.length === 0) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
// Step 2: Get container dimensions and padding after the initial layout
|
|
88
|
+
const width = this.node.getComputedWidth();
|
|
89
|
+
const height = this.node.getComputedHeight();
|
|
90
|
+
const paddingLeft = this.node.getComputedPadding(Style.Edge.Left);
|
|
91
|
+
const paddingRight = this.node.getComputedPadding(Style.Edge.Right);
|
|
92
|
+
const paddingTop = this.node.getComputedPadding(Style.Edge.Top);
|
|
93
|
+
const paddingBottom = this.node.getComputedPadding(Style.Edge.Bottom);
|
|
94
|
+
// Calculate content box dimensions
|
|
95
|
+
const contentWidth = Math.max(0, width - paddingLeft - paddingRight);
|
|
96
|
+
const contentHeight = Math.max(0, height - paddingTop - paddingBottom);
|
|
97
|
+
// Step 3: Validate dimensions needed for calculations
|
|
98
|
+
if (!this.isVertical && contentWidth <= 0 && width > 0) {
|
|
99
|
+
console.warn(`[GridNode ${this.props.key} - Finalize] Grid content width (${contentWidth}) is zero or negative after accounting for padding (${paddingLeft}+${paddingRight}) on total width ${width}. Cannot calculate basis.`);
|
|
100
|
+
if (this.columns > 1)
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (this.isVertical && contentHeight <= 0 && height > 0) {
|
|
104
|
+
console.warn(`[GridNode ${this.props.key} - Finalize] Grid content height (${contentHeight}) is zero or negative after accounting for padding (${paddingTop}+${paddingBottom}) on total height ${height}. Cannot calculate basis.`);
|
|
105
|
+
if (this.columns > 1)
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
// Step 4: Calculate Gap Values in Pixels
|
|
109
|
+
let columnGapPixels = 0;
|
|
110
|
+
if (typeof this.columnGapValue === 'number') {
|
|
111
|
+
columnGapPixels = this.columnGapValue;
|
|
112
|
+
}
|
|
113
|
+
else if (typeof this.columnGapValue === 'string' && this.columnGapValue.trim().endsWith('%')) {
|
|
114
|
+
try {
|
|
115
|
+
const percent = parseFloat(this.columnGapValue);
|
|
116
|
+
if (!isNaN(percent) && contentWidth > 0) {
|
|
117
|
+
columnGapPixels = (percent / 100) * contentWidth;
|
|
118
|
+
}
|
|
119
|
+
else if (isNaN(percent)) {
|
|
120
|
+
console.warn(`[GridNode ${this.props.key}] Invalid percentage column gap format: "${this.columnGapValue}". Using 0px.`);
|
|
121
|
+
}
|
|
122
|
+
else if (contentWidth <= 0) {
|
|
123
|
+
console.warn(`[GridNode ${this.props.key}] Cannot calculate percentage column gap (${this.columnGapValue}) because content width is zero. Using 0px.`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
console.warn(`[GridNode ${this.props.key}] Error parsing percentage column gap: "${this.columnGapValue}". Using 0px.`, e);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else if (typeof this.columnGapValue === 'string' && this.columnGapValue.trim() !== '') {
|
|
131
|
+
console.warn(`[GridNode ${this.props.key}] Unsupported string column gap format: "${this.columnGapValue}". Using 0px. Only numbers and percentages ('%') are supported.`);
|
|
132
|
+
}
|
|
133
|
+
let rowGapPixels = 0;
|
|
134
|
+
if (typeof this.rowGapValue === 'number') {
|
|
135
|
+
rowGapPixels = this.rowGapValue;
|
|
136
|
+
}
|
|
137
|
+
else if (typeof this.rowGapValue === 'string' && this.rowGapValue.trim().endsWith('%')) {
|
|
138
|
+
try {
|
|
139
|
+
const percent = parseFloat(this.rowGapValue);
|
|
140
|
+
if (!isNaN(percent) && contentHeight > 0) {
|
|
141
|
+
rowGapPixels = (percent / 100) * contentHeight;
|
|
142
|
+
}
|
|
143
|
+
else if (isNaN(percent)) {
|
|
144
|
+
console.warn(`[GridNode ${this.props.key}] Invalid percentage row gap format: "${this.rowGapValue}". Using 0px.`);
|
|
145
|
+
}
|
|
146
|
+
else if (contentHeight <= 0) {
|
|
147
|
+
console.warn(`[GridNode ${this.props.key}] Cannot calculate percentage row gap (${this.rowGapValue}) because content height is zero. Using 0px.`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
catch (e) {
|
|
151
|
+
console.warn(`[GridNode ${this.props.key}] Error parsing percentage row gap: "${this.rowGapValue}". Using 0px.`, e);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else if (typeof this.rowGapValue === 'string' && this.rowGapValue.trim() !== '') {
|
|
155
|
+
console.warn(`[GridNode ${this.props.key}] Unsupported string row gap format: "${this.rowGapValue}". Using 0px. Only numbers and percentages ('%') are supported.`);
|
|
156
|
+
}
|
|
157
|
+
// Ensure gaps are not negative
|
|
158
|
+
columnGapPixels = Math.max(0, columnGapPixels);
|
|
159
|
+
rowGapPixels = Math.max(0, rowGapPixels);
|
|
160
|
+
// Step 5: Calculate flex-basis percentage for children
|
|
161
|
+
const mainAxisGapPixels = this.isVertical ? rowGapPixels : columnGapPixels;
|
|
162
|
+
const mainAxisContentSize = this.isVertical ? contentHeight : contentWidth;
|
|
163
|
+
let childWidth = 0;
|
|
164
|
+
if (mainAxisContentSize > 0 && this.columns > 0) {
|
|
165
|
+
// Total space taken up by gaps on the main axis
|
|
166
|
+
const totalGapSpaceOnMainAxis = this.columns > 1 ? mainAxisGapPixels * (this.columns - 1) : 0;
|
|
167
|
+
// Calculate the space available *only* for the items themselves
|
|
168
|
+
const availableSpaceOnMainAxis = Math.max(0, mainAxisContentSize - totalGapSpaceOnMainAxis);
|
|
169
|
+
// Calculate the exact pixel of the total content size that each item should occupy
|
|
170
|
+
const exactItemWidth = availableSpaceOnMainAxis / this.columns;
|
|
171
|
+
// Ensure it's not negative (shouldn't happen, but safety)
|
|
172
|
+
childWidth = Math.max(0, exactItemWidth - 0.5); // Slightly reduce to avoid rounding issues
|
|
173
|
+
}
|
|
174
|
+
else if (this.columns === 1) {
|
|
175
|
+
// If only one column, it takes up the full basis (gaps don't apply)
|
|
176
|
+
childWidth = mainAxisContentSize;
|
|
177
|
+
}
|
|
178
|
+
// Clamp basis percentage between 0 and 100 (mostly redundant after floor/max(0) but safe)
|
|
179
|
+
childWidth = Math.max(0, Math.min(mainAxisContentSize, childWidth));
|
|
180
|
+
// Step 6: Apply layout properties to children
|
|
181
|
+
let childrenNeedRecalculation = false;
|
|
182
|
+
for (const child of this.children) {
|
|
183
|
+
let childChanged = false;
|
|
184
|
+
const currentLayoutWidth = child.node.getWidth();
|
|
185
|
+
const currentWidthValue = currentLayoutWidth.value;
|
|
186
|
+
const currentWidthUnit = currentLayoutWidth.unit;
|
|
187
|
+
let widthNeedsUpdate = false;
|
|
188
|
+
if (currentWidthUnit === Style.Unit.Point) {
|
|
189
|
+
// If current width is in points, check if the value is significantly different
|
|
190
|
+
if (Math.abs(currentWidthValue - childWidth) > 0.01) {
|
|
191
|
+
widthNeedsUpdate = true;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
// If current width is not in points (e.g., Auto, Percent, Undefined), it needs to be set to points
|
|
196
|
+
widthNeedsUpdate = true;
|
|
197
|
+
}
|
|
198
|
+
if (widthNeedsUpdate) {
|
|
199
|
+
child.node.setWidth(childWidth);
|
|
200
|
+
childChanged = true;
|
|
201
|
+
}
|
|
202
|
+
// Ensure grow/shrink are set correctly for grid items
|
|
203
|
+
if (child.node.getFlexGrow() !== 0) {
|
|
204
|
+
child.node.setFlexGrow(0);
|
|
205
|
+
childChanged = true;
|
|
206
|
+
}
|
|
207
|
+
if (child.node.getFlexShrink() !== 1) {
|
|
208
|
+
child.node.setFlexShrink(1); // Allow shrinking
|
|
209
|
+
childChanged = true;
|
|
210
|
+
}
|
|
211
|
+
// Remove margins that might interfere with gap property
|
|
212
|
+
if (child.node.getMargin(Style.Edge.Bottom).unit !== Style.Unit.Undefined) {
|
|
213
|
+
child.node.setMargin(Style.Edge.Bottom, undefined);
|
|
214
|
+
childChanged = true;
|
|
215
|
+
}
|
|
216
|
+
if (child.node.getMargin(Style.Edge.Right).unit !== Style.Unit.Undefined) {
|
|
217
|
+
child.node.setMargin(Style.Edge.Right, undefined);
|
|
218
|
+
childChanged = true;
|
|
219
|
+
}
|
|
220
|
+
if (child.node.getMargin(Style.Edge.Top).unit !== Style.Unit.Undefined) {
|
|
221
|
+
child.node.setMargin(Style.Edge.Top, undefined);
|
|
222
|
+
childChanged = true;
|
|
223
|
+
}
|
|
224
|
+
if (child.node.getMargin(Style.Edge.Left).unit !== Style.Unit.Undefined) {
|
|
225
|
+
child.node.setMargin(Style.Edge.Left, undefined);
|
|
226
|
+
childChanged = true;
|
|
227
|
+
}
|
|
228
|
+
if (childChanged && !child.node.isDirty()) {
|
|
229
|
+
child.node.markDirty();
|
|
230
|
+
childrenNeedRecalculation = true;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
// Step 7: Apply gaps using Yoga's built-in gap properties
|
|
234
|
+
const currentColumnGap = this.node.getGap(Style.Gutter.Column).value;
|
|
235
|
+
const currentRowGap = this.node.getGap(Style.Gutter.Row).value;
|
|
236
|
+
let gapsChanged = false;
|
|
237
|
+
// Use a small tolerance for comparing gap pixels
|
|
238
|
+
if (Math.abs(currentColumnGap - columnGapPixels) > 0.001) {
|
|
239
|
+
this.node.setGap(Style.Gutter.Column, columnGapPixels);
|
|
240
|
+
gapsChanged = true;
|
|
241
|
+
}
|
|
242
|
+
if (Math.abs(currentRowGap - rowGapPixels) > 0.001) {
|
|
243
|
+
this.node.setGap(Style.Gutter.Row, rowGapPixels);
|
|
244
|
+
gapsChanged = true;
|
|
245
|
+
}
|
|
246
|
+
// Step 8: Mark the grid node itself as dirty if gaps changed or children changed
|
|
247
|
+
if ((gapsChanged || childrenNeedRecalculation) && !this.node.isDirty()) {
|
|
248
|
+
this.node.markDirty();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Factory function to create a new GridNode instance.
|
|
254
|
+
* @param props - Grid configuration properties.
|
|
255
|
+
* @returns A new GridNode instance.
|
|
256
|
+
*/
|
|
257
|
+
const Grid = (props) => new GridNode(props);
|
|
258
|
+
|
|
259
|
+
export { Grid, GridNode };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { BaseProps, ImageProps } from '../canvas/canvas.type.js';
|
|
2
|
+
import { type CanvasRenderingContext2D } from 'skia-canvas';
|
|
3
|
+
import { BoxNode } from '../canvas/layout.canvas.util.js';
|
|
4
|
+
/**
|
|
5
|
+
* Renders images with configurable sizing, positioning, and effects.
|
|
6
|
+
* Supports object-fit modes, positioning, border radius, and saturation filters.
|
|
7
|
+
*/
|
|
8
|
+
export declare class ImageNode extends BoxNode {
|
|
9
|
+
props: ImageProps & BaseProps;
|
|
10
|
+
private loadedImage;
|
|
11
|
+
private naturalWidth;
|
|
12
|
+
private naturalHeight;
|
|
13
|
+
private readonly loadingPromise;
|
|
14
|
+
constructor(props: ImageProps);
|
|
15
|
+
/**
|
|
16
|
+
* Loads and processes an image from various sources (URL, file path, or Buffer).
|
|
17
|
+
* Handles SVG color modifications and sets natural dimensions with an aspect ratio.
|
|
18
|
+
*
|
|
19
|
+
* @returns Promise that resolves when image loading completes
|
|
20
|
+
* @throws Error if image loading fails
|
|
21
|
+
*/
|
|
22
|
+
private _loadImage;
|
|
23
|
+
getLoadingPromise(): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Renders the image with correct sizing, clipping, and positioning.
|
|
26
|
+
* Handles object-fit, object-position, and visual effects like saturation.
|
|
27
|
+
*/
|
|
28
|
+
protected _renderContent(ctx: CanvasRenderingContext2D, x: number, y: number, width: number, height: number): void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Factory function to create ImageNode instances
|
|
32
|
+
*/
|
|
33
|
+
export declare const Image: (props: ImageProps) => ImageNode;
|
|
34
|
+
//# sourceMappingURL=image.canvas.util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image.canvas.util.d.ts","sourceRoot":"","sources":["../../../src/canvas/image.canvas.util.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACpE,OAAO,EAAE,KAAK,wBAAwB,EAAmC,MAAM,aAAa,CAAA;AAC5F,OAAO,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAA;AAsBxD;;;GAGG;AACH,qBAAa,SAAU,SAAQ,OAAO;IAC5B,KAAK,EAAE,UAAU,GAAG,SAAS,CAAA;IACrC,OAAO,CAAC,WAAW,CAA2B;IAC9C,OAAO,CAAC,YAAY,CAAI;IACxB,OAAO,CAAC,aAAa,CAAI;IACzB,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA6B;gBAEhD,KAAK,EAAE,UAAU;IAc7B;;;;;;OAMG;IACH,OAAO,CAAC,UAAU;IAoHX,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAIzC;;;OAGG;cACgB,cAAc,CAC/B,GAAG,EAAE,wBAAwB,EAC7B,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM;CAuJjB;AAED;;GAEG;AACH,eAAO,MAAM,KAAK,GAAI,OAAO,UAAU,cAAyB,CAAA"}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { loadImage } from 'skia-canvas';
|
|
2
|
+
import { BoxNode } from './layout.canvas.util.js';
|
|
3
|
+
import { parseBorderRadius, drawRoundedRectPath } from './canvas.helper.js';
|
|
4
|
+
import { promises } from 'fs';
|
|
5
|
+
import { Style } from '../constant/common.const.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Calculates pixel offset for image positioning based on percentage or pixel values.
|
|
9
|
+
* This handles centering, edge alignment, and percentage-based positioning.
|
|
10
|
+
*/
|
|
11
|
+
function calculateOffsetFromValue(positionValue, availableSpace) {
|
|
12
|
+
const value = positionValue ?? '50%';
|
|
13
|
+
if (typeof value === 'number') {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
if (typeof value === 'string' && value.endsWith('%')) {
|
|
17
|
+
const percentage = parseFloat(value) / 100;
|
|
18
|
+
return availableSpace * percentage;
|
|
19
|
+
}
|
|
20
|
+
console.warn(`[ImageNode] Invalid objectPosition value format: ${value}. Defaulting to 50%.`);
|
|
21
|
+
return availableSpace * 0.5;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Renders images with configurable sizing, positioning, and effects.
|
|
25
|
+
* Supports object-fit modes, positioning, border radius, and saturation filters.
|
|
26
|
+
*/
|
|
27
|
+
class ImageNode extends BoxNode {
|
|
28
|
+
loadedImage = null;
|
|
29
|
+
naturalWidth = 0;
|
|
30
|
+
naturalHeight = 0;
|
|
31
|
+
loadingPromise = null;
|
|
32
|
+
constructor(props) {
|
|
33
|
+
super({ name: 'Image', ...props, children: undefined });
|
|
34
|
+
this.props = {
|
|
35
|
+
objectFit: 'fill',
|
|
36
|
+
overflow: Style.Overflow.Hidden,
|
|
37
|
+
saturate: 1,
|
|
38
|
+
objectPosition: { Left: '50%', Top: '50%' },
|
|
39
|
+
...props,
|
|
40
|
+
};
|
|
41
|
+
this.loadingPromise = this._loadImage();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Loads and processes an image from various sources (URL, file path, or Buffer).
|
|
45
|
+
* Handles SVG color modifications and sets natural dimensions with an aspect ratio.
|
|
46
|
+
*
|
|
47
|
+
* @returns Promise that resolves when image loading completes
|
|
48
|
+
* @throws Error if image loading fails
|
|
49
|
+
*/
|
|
50
|
+
_loadImage() {
|
|
51
|
+
if (this.loadingPromise)
|
|
52
|
+
return this.loadingPromise;
|
|
53
|
+
if (this.loadedImage)
|
|
54
|
+
return Promise.resolve();
|
|
55
|
+
if (!this.props.src) {
|
|
56
|
+
const aspectRatioFromProps = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : undefined;
|
|
57
|
+
this.node.setAspectRatio(aspectRatioFromProps);
|
|
58
|
+
this.naturalWidth = 0;
|
|
59
|
+
this.naturalHeight = 0;
|
|
60
|
+
return Promise.resolve();
|
|
61
|
+
}
|
|
62
|
+
return new Promise(async (resolve) => {
|
|
63
|
+
const { fileTypeFromBuffer, fileTypeFromFile } = await import('file-type');
|
|
64
|
+
let finalSource = this.props.src;
|
|
65
|
+
let isSvg = false;
|
|
66
|
+
let contentBuffer = null;
|
|
67
|
+
let detectedMime;
|
|
68
|
+
try {
|
|
69
|
+
if (typeof this.props.src === 'string') {
|
|
70
|
+
if (this.props.src.startsWith('http')) {
|
|
71
|
+
const response = await fetch(this.props.src);
|
|
72
|
+
if (!response.ok) {
|
|
73
|
+
throw new Error(`HTTP error ${response.status} fetching image: ${this.props.src}`);
|
|
74
|
+
}
|
|
75
|
+
const imageArrayBuffer = await response.arrayBuffer();
|
|
76
|
+
contentBuffer = Buffer.from(imageArrayBuffer);
|
|
77
|
+
finalSource = contentBuffer;
|
|
78
|
+
const fileTypeResult = await fileTypeFromBuffer(contentBuffer);
|
|
79
|
+
detectedMime = fileTypeResult?.mime;
|
|
80
|
+
isSvg = detectedMime === 'image/svg+xml';
|
|
81
|
+
if ((!detectedMime || detectedMime === 'application/xml') &&
|
|
82
|
+
contentBuffer.toString('utf-8').includes('<svg')) {
|
|
83
|
+
isSvg = true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
finalSource = this.props.src;
|
|
88
|
+
const filePath = this.props.src;
|
|
89
|
+
try {
|
|
90
|
+
const fileTypeResult = await fileTypeFromFile(filePath);
|
|
91
|
+
detectedMime = fileTypeResult?.mime;
|
|
92
|
+
isSvg = detectedMime === 'image/svg+xml';
|
|
93
|
+
if ((!detectedMime || detectedMime === 'application/xml') && filePath.toLowerCase().endsWith('.svg')) {
|
|
94
|
+
isSvg = true;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
isSvg = filePath.toLowerCase().endsWith('.svg');
|
|
99
|
+
}
|
|
100
|
+
if (isSvg && this.props.color) {
|
|
101
|
+
try {
|
|
102
|
+
contentBuffer = await promises.readFile(filePath);
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
isSvg = false;
|
|
106
|
+
contentBuffer = null;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
contentBuffer = this.props.src;
|
|
113
|
+
finalSource = contentBuffer;
|
|
114
|
+
const fileTypeResult = await fileTypeFromBuffer(contentBuffer);
|
|
115
|
+
detectedMime = fileTypeResult?.mime;
|
|
116
|
+
isSvg = detectedMime === 'image/svg+xml';
|
|
117
|
+
}
|
|
118
|
+
if (isSvg && this.props.color && contentBuffer) {
|
|
119
|
+
const svgString = contentBuffer.toString('utf-8');
|
|
120
|
+
const modifiedSvgString = svgString.replace(/fill="[^"]*"/g, `fill="${this.props.color}"`);
|
|
121
|
+
if (modifiedSvgString !== svgString) {
|
|
122
|
+
finalSource = Buffer.from(modifiedSvgString);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
finalSource = contentBuffer;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const img = await loadImage(finalSource);
|
|
129
|
+
this.loadedImage = img;
|
|
130
|
+
this.naturalWidth = img.width;
|
|
131
|
+
this.naturalHeight = img.height;
|
|
132
|
+
const calculatedAspectRatio = this.naturalWidth > 0 && this.naturalHeight > 0 ? this.naturalWidth / this.naturalHeight : undefined;
|
|
133
|
+
const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0
|
|
134
|
+
? this.props.aspectRatio
|
|
135
|
+
: calculatedAspectRatio;
|
|
136
|
+
this.node.setAspectRatio(finalAspectRatio);
|
|
137
|
+
this.props.onLoad?.();
|
|
138
|
+
resolve();
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
this.naturalWidth = 0;
|
|
142
|
+
this.naturalHeight = 0;
|
|
143
|
+
const finalAspectRatioOnError = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : undefined;
|
|
144
|
+
this.node.setAspectRatio(finalAspectRatioOnError);
|
|
145
|
+
this.props.onError?.(error);
|
|
146
|
+
resolve();
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
getLoadingPromise() {
|
|
151
|
+
return this.loadingPromise ?? Promise.resolve();
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Renders the image with correct sizing, clipping, and positioning.
|
|
155
|
+
* Handles object-fit, object-position, and visual effects like saturation.
|
|
156
|
+
*/
|
|
157
|
+
_renderContent(ctx, x, y, width, height) {
|
|
158
|
+
super._renderContent(ctx, x, y, width, height);
|
|
159
|
+
if (!this.loadedImage || width <= 0 || height <= 0)
|
|
160
|
+
return;
|
|
161
|
+
const img = this.loadedImage;
|
|
162
|
+
const imgW = this.naturalWidth;
|
|
163
|
+
const imgH = this.naturalHeight;
|
|
164
|
+
if (imgW <= 0 || imgH <= 0)
|
|
165
|
+
return;
|
|
166
|
+
// Calculate content box accounting for padding and borders
|
|
167
|
+
const paddingLeft = this.node.getComputedPadding(Style.Edge.Left);
|
|
168
|
+
const paddingTop = this.node.getComputedPadding(Style.Edge.Top);
|
|
169
|
+
const paddingRight = this.node.getComputedPadding(Style.Edge.Right);
|
|
170
|
+
const paddingBottom = this.node.getComputedPadding(Style.Edge.Bottom);
|
|
171
|
+
const borderLeft = this.node.getComputedBorder(Style.Edge.Left);
|
|
172
|
+
const borderTop = this.node.getComputedBorder(Style.Edge.Top);
|
|
173
|
+
const borderRight = this.node.getComputedBorder(Style.Edge.Right);
|
|
174
|
+
const borderBottom = this.node.getComputedBorder(Style.Edge.Bottom);
|
|
175
|
+
const contentX = x + borderLeft + paddingLeft;
|
|
176
|
+
const contentY = y + borderTop + paddingTop;
|
|
177
|
+
const contentWidth = Math.max(0, width - borderLeft - paddingLeft - borderRight - paddingRight);
|
|
178
|
+
const contentHeight = Math.max(0, height - borderTop - paddingTop - borderBottom - paddingBottom);
|
|
179
|
+
if (contentWidth <= 0 || contentHeight <= 0)
|
|
180
|
+
return;
|
|
181
|
+
// Apply clipping for border radius
|
|
182
|
+
ctx.save();
|
|
183
|
+
const outerRadii = parseBorderRadius(this.props.borderRadius);
|
|
184
|
+
const innerBorderRadii = {
|
|
185
|
+
TopLeft: Math.max(0, outerRadii.TopLeft - borderTop),
|
|
186
|
+
TopRight: Math.max(0, outerRadii.TopRight - borderTop),
|
|
187
|
+
BottomRight: Math.max(0, outerRadii.BottomRight - borderBottom),
|
|
188
|
+
BottomLeft: Math.max(0, outerRadii.BottomLeft - borderBottom),
|
|
189
|
+
};
|
|
190
|
+
const contentRadii = {
|
|
191
|
+
TopLeft: Math.max(0, innerBorderRadii.TopLeft - Math.max(paddingLeft, paddingTop)),
|
|
192
|
+
TopRight: Math.max(0, innerBorderRadii.TopRight - Math.max(paddingRight, paddingTop)),
|
|
193
|
+
BottomRight: Math.max(0, innerBorderRadii.BottomRight - Math.max(paddingRight, paddingBottom)),
|
|
194
|
+
BottomLeft: Math.max(0, innerBorderRadii.BottomLeft - Math.max(paddingLeft, paddingBottom)),
|
|
195
|
+
};
|
|
196
|
+
drawRoundedRectPath(ctx, contentX, contentY, contentWidth, contentHeight, contentRadii);
|
|
197
|
+
ctx.clip();
|
|
198
|
+
// Calculate image dimensions based on object-fit
|
|
199
|
+
const nodeRatio = contentWidth / contentHeight;
|
|
200
|
+
const imgRatio = imgW / imgH;
|
|
201
|
+
const objectFit = this.props.objectFit;
|
|
202
|
+
let dw = contentWidth;
|
|
203
|
+
let dh = contentHeight;
|
|
204
|
+
if (objectFit === 'contain') {
|
|
205
|
+
if (imgRatio > nodeRatio) {
|
|
206
|
+
dw = contentWidth;
|
|
207
|
+
dh = contentWidth / imgRatio;
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
dh = contentHeight;
|
|
211
|
+
dw = contentHeight * imgRatio;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
else if (objectFit === 'cover') {
|
|
215
|
+
if (imgRatio > nodeRatio) {
|
|
216
|
+
dh = contentHeight;
|
|
217
|
+
dw = contentHeight * imgRatio;
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
dw = contentWidth;
|
|
221
|
+
dh = contentWidth / imgRatio;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else if (objectFit === 'none') {
|
|
225
|
+
dw = imgW;
|
|
226
|
+
dh = imgH;
|
|
227
|
+
}
|
|
228
|
+
else if (objectFit === 'scale-down') {
|
|
229
|
+
if (imgW <= contentWidth && imgH <= contentHeight) {
|
|
230
|
+
dw = imgW;
|
|
231
|
+
dh = imgH;
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
if (imgRatio > nodeRatio) {
|
|
235
|
+
dw = contentWidth;
|
|
236
|
+
dh = contentWidth / imgRatio;
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
dh = contentHeight;
|
|
240
|
+
dw = contentHeight * imgRatio;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
// Calculate image position based on object-position
|
|
245
|
+
const sx = 0;
|
|
246
|
+
const sy = 0;
|
|
247
|
+
const sw = imgW;
|
|
248
|
+
const sh = imgH;
|
|
249
|
+
const availableWidth = contentWidth - dw;
|
|
250
|
+
const availableHeight = contentHeight - dh;
|
|
251
|
+
const posProps = this.props.objectPosition || {};
|
|
252
|
+
const horizontalValue = posProps.Left !== undefined ? posProps.Left : posProps.Right !== undefined ? posProps.Right : '50%';
|
|
253
|
+
const verticalValue = posProps.Top !== undefined ? posProps.Top : posProps.Bottom !== undefined ? posProps.Bottom : '50%';
|
|
254
|
+
let offsetX = calculateOffsetFromValue(horizontalValue, availableWidth);
|
|
255
|
+
let offsetY = calculateOffsetFromValue(verticalValue, availableHeight);
|
|
256
|
+
if (posProps.Left === undefined && posProps.Right !== undefined) {
|
|
257
|
+
offsetX = availableWidth - offsetX;
|
|
258
|
+
}
|
|
259
|
+
if (posProps.Top === undefined && posProps.Bottom !== undefined) {
|
|
260
|
+
offsetY = availableHeight - offsetY;
|
|
261
|
+
}
|
|
262
|
+
const dx = contentX + offsetX;
|
|
263
|
+
const dy = contentY + offsetY;
|
|
264
|
+
// Draw image with filters
|
|
265
|
+
ctx.save();
|
|
266
|
+
try {
|
|
267
|
+
if (this.props.dropShadow) {
|
|
268
|
+
const shadow = this.props.dropShadow;
|
|
269
|
+
const shadowBlur = Math.max(shadow.offsetX ?? 0, shadow.offsetY ?? 0);
|
|
270
|
+
ctx.shadowOffsetX = shadow.offsetX ?? 0;
|
|
271
|
+
ctx.shadowOffsetY = shadow.offsetY ?? 0;
|
|
272
|
+
ctx.shadowBlur = Math.max(0, shadow.blur ?? shadowBlur);
|
|
273
|
+
ctx.shadowColor = shadow.color ?? 'black';
|
|
274
|
+
}
|
|
275
|
+
const saturateValue = this.props.saturate ?? 1;
|
|
276
|
+
let filterString = '';
|
|
277
|
+
if (saturateValue !== 1) {
|
|
278
|
+
filterString += `saturate(${saturateValue * 100}%) `;
|
|
279
|
+
}
|
|
280
|
+
if (filterString) {
|
|
281
|
+
const currentFilter = ctx.filter && ctx.filter !== 'none' ? ctx.filter + ' ' : '';
|
|
282
|
+
ctx.filter = currentFilter + filterString.trim();
|
|
283
|
+
}
|
|
284
|
+
const finalDX = Math.floor(dx);
|
|
285
|
+
const finalDY = Math.floor(dy);
|
|
286
|
+
const finalDW = Math.ceil(dw + (dx - finalDX));
|
|
287
|
+
const finalDH = Math.ceil(dh + (dy - finalDY));
|
|
288
|
+
if (finalDW > 0 && finalDH > 0) {
|
|
289
|
+
ctx.drawImage(img, sx, sy, sw, sh, finalDX, finalDY, finalDW, finalDH);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
catch (drawError) {
|
|
293
|
+
console.error('[ImageNode] Error drawing image:', drawError);
|
|
294
|
+
}
|
|
295
|
+
finally {
|
|
296
|
+
ctx.restore();
|
|
297
|
+
}
|
|
298
|
+
ctx.restore();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Factory function to create ImageNode instances
|
|
303
|
+
*/
|
|
304
|
+
const Image = (props) => new ImageNode(props);
|
|
305
|
+
|
|
306
|
+
export { Image, ImageNode };
|