@elementor/editor-canvas 4.2.0-912 → 4.2.0-914
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/index.js +197 -87
- package/dist/index.mjs +178 -68
- package/package.json +18 -18
- package/src/components/grid-outline/__tests__/grid-outline-overlay.test.tsx +18 -1
- package/src/components/grid-outline/__tests__/grid-outline.test.tsx +106 -83
- package/src/components/grid-outline/{grid-outline-cell.tsx → cell.tsx} +2 -3
- package/src/components/grid-outline/grid-outline.tsx +44 -13
- package/src/components/grid-outline/line.tsx +26 -0
- package/src/hooks/__tests__/use-grid-children.test.ts +158 -0
- package/src/hooks/__tests__/use-grid-tracks.test.ts +58 -4
- package/src/hooks/use-grid-children.ts +48 -0
- package/src/hooks/use-grid-tracks.ts +3 -1
- package/src/utils/__tests__/grid-outline-utils.test.ts +72 -1
- package/src/utils/grid-outline-utils.ts +48 -0
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { type GridTracks } from '../../hooks/use-grid-tracks';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
computeCellRects,
|
|
4
|
+
computeGridLines,
|
|
5
|
+
parseTrackList,
|
|
6
|
+
snapToHalfPixel,
|
|
7
|
+
toGridTracks,
|
|
8
|
+
toPx,
|
|
9
|
+
} from '../grid-outline-utils';
|
|
3
10
|
|
|
4
11
|
function makeTracks( partial: Partial< GridTracks > = {} ): GridTracks {
|
|
5
12
|
return {
|
|
@@ -87,6 +94,70 @@ describe( 'computeCellRects', () => {
|
|
|
87
94
|
} );
|
|
88
95
|
} );
|
|
89
96
|
|
|
97
|
+
describe( 'computeGridLines', () => {
|
|
98
|
+
it( 'returns no lines when there are no tracks', () => {
|
|
99
|
+
expect( computeGridLines( makeTracks(), 100, 100 ) ).toEqual( { vertical: [], horizontal: [] } );
|
|
100
|
+
} );
|
|
101
|
+
|
|
102
|
+
it( 'collapses shared boundaries into a single line when there is no gap', () => {
|
|
103
|
+
const tracks = makeTracks( { columns: [ 100, 100, 100 ], rows: [ 80, 80 ] } );
|
|
104
|
+
|
|
105
|
+
const lines = computeGridLines( tracks, 300, 160 );
|
|
106
|
+
|
|
107
|
+
expect( lines.vertical.map( ( l ) => l.x1 ) ).toEqual( [ 0, 100, 200, 300 ] );
|
|
108
|
+
expect( lines.horizontal.map( ( l ) => l.y1 ) ).toEqual( [ 0, 80, 160 ] );
|
|
109
|
+
} );
|
|
110
|
+
|
|
111
|
+
it( 'splits the inner boundary into two parallel lines when a gap is set', () => {
|
|
112
|
+
const tracks = makeTracks( {
|
|
113
|
+
columns: [ 100, 100, 100 ],
|
|
114
|
+
rows: [ 80, 80 ],
|
|
115
|
+
columnGap: 10,
|
|
116
|
+
rowGap: 8,
|
|
117
|
+
} );
|
|
118
|
+
|
|
119
|
+
const lines = computeGridLines( tracks, 320, 176 );
|
|
120
|
+
|
|
121
|
+
expect( lines.vertical.map( ( l ) => l.x1 ) ).toEqual( [ 0, 100, 110, 210, 220, 320 ] );
|
|
122
|
+
expect( lines.horizontal.map( ( l ) => l.y1 ) ).toEqual( [ 0, 80, 88, 168 ] );
|
|
123
|
+
} );
|
|
124
|
+
|
|
125
|
+
it( 'extends vertical lines from the top to the bottom of the row span', () => {
|
|
126
|
+
const tracks = makeTracks( { columns: [ 100, 100 ], rows: [ 50, 60 ], rowGap: 5 } );
|
|
127
|
+
|
|
128
|
+
const lines = computeGridLines( tracks, 200, 115 );
|
|
129
|
+
|
|
130
|
+
for ( const line of lines.vertical ) {
|
|
131
|
+
expect( line.y1 ).toBe( 0 );
|
|
132
|
+
expect( line.y2 ).toBe( 115 );
|
|
133
|
+
}
|
|
134
|
+
} );
|
|
135
|
+
|
|
136
|
+
it( 'extends horizontal lines from the left to the right of the column span', () => {
|
|
137
|
+
const tracks = makeTracks( { columns: [ 100, 100 ], rows: [ 50, 50 ], columnGap: 10 } );
|
|
138
|
+
|
|
139
|
+
const lines = computeGridLines( tracks, 210, 100 );
|
|
140
|
+
|
|
141
|
+
for ( const line of lines.horizontal ) {
|
|
142
|
+
expect( line.x1 ).toBe( 0 );
|
|
143
|
+
expect( line.x2 ).toBe( 210 );
|
|
144
|
+
}
|
|
145
|
+
} );
|
|
146
|
+
|
|
147
|
+
it( 'offsets the lines by the grid padding', () => {
|
|
148
|
+
const tracks = makeTracks( {
|
|
149
|
+
columns: [ 100 ],
|
|
150
|
+
rows: [ 80 ],
|
|
151
|
+
padding: { top: 5, right: 4, bottom: 3, left: 20 },
|
|
152
|
+
} );
|
|
153
|
+
|
|
154
|
+
const lines = computeGridLines( tracks, 200, 100 );
|
|
155
|
+
|
|
156
|
+
expect( lines.vertical.map( ( l ) => l.x1 ) ).toEqual( [ 20, 120 ] );
|
|
157
|
+
expect( lines.horizontal.map( ( l ) => l.y1 ) ).toEqual( [ 5, 85 ] );
|
|
158
|
+
} );
|
|
159
|
+
} );
|
|
160
|
+
|
|
90
161
|
describe( 'snapToHalfPixel', () => {
|
|
91
162
|
it.each( [
|
|
92
163
|
[ 0, 0.5 ],
|
|
@@ -7,6 +7,18 @@ export type CellRect = {
|
|
|
7
7
|
height: number;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
+
export type GridLine = {
|
|
11
|
+
x1: number;
|
|
12
|
+
y1: number;
|
|
13
|
+
x2: number;
|
|
14
|
+
y2: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type GridLines = {
|
|
18
|
+
vertical: GridLine[];
|
|
19
|
+
horizontal: GridLine[];
|
|
20
|
+
};
|
|
21
|
+
|
|
10
22
|
type TrackSegment = {
|
|
11
23
|
start: number;
|
|
12
24
|
size: number;
|
|
@@ -57,6 +69,42 @@ export function computeCellRects( tracks: GridTracks, width: number, height: num
|
|
|
57
69
|
return cells;
|
|
58
70
|
}
|
|
59
71
|
|
|
72
|
+
export function computeGridLines( tracks: GridTracks, width: number, height: number ): GridLines {
|
|
73
|
+
const { columns, rows, columnGap, rowGap, padding } = tracks;
|
|
74
|
+
|
|
75
|
+
const hasColumns = columns.length > 0;
|
|
76
|
+
const hasRows = rows.length > 0;
|
|
77
|
+
|
|
78
|
+
if ( ! hasColumns && ! hasRows ) {
|
|
79
|
+
return { vertical: [], horizontal: [] };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const columnSegments = hasColumns
|
|
83
|
+
? computeTrackSegments( columns, columnGap, padding.left )
|
|
84
|
+
: [ { start: padding.left, size: width - padding.left - padding.right } ];
|
|
85
|
+
|
|
86
|
+
const rowSegments = hasRows
|
|
87
|
+
? computeTrackSegments( rows, rowGap, padding.top )
|
|
88
|
+
: [ { start: padding.top, size: height - padding.top - padding.bottom } ];
|
|
89
|
+
|
|
90
|
+
const xs = uniqueSorted( columnSegments.flatMap( ( s ) => [ s.start, s.start + s.size ] ) );
|
|
91
|
+
const ys = uniqueSorted( rowSegments.flatMap( ( s ) => [ s.start, s.start + s.size ] ) );
|
|
92
|
+
|
|
93
|
+
const yTop = ys[ 0 ];
|
|
94
|
+
const yBottom = ys[ ys.length - 1 ];
|
|
95
|
+
const xLeft = xs[ 0 ];
|
|
96
|
+
const xRight = xs[ xs.length - 1 ];
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
vertical: xs.map( ( x ) => ( { x1: x, y1: yTop, x2: x, y2: yBottom } ) ),
|
|
100
|
+
horizontal: ys.map( ( y ) => ( { x1: xLeft, y1: y, x2: xRight, y2: y } ) ),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function uniqueSorted( values: number[] ): number[] {
|
|
105
|
+
return Array.from( new Set( values ) ).sort( ( a, b ) => a - b );
|
|
106
|
+
}
|
|
107
|
+
|
|
60
108
|
function computeTrackSegments( sizes: number[], gap: number, offset: number ): TrackSegment[] {
|
|
61
109
|
const segments: TrackSegment[] = [];
|
|
62
110
|
let cursor = offset;
|