@elementor/editor-canvas 4.2.0-913 → 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.
@@ -1,5 +1,12 @@
1
1
  import { type GridTracks } from '../../hooks/use-grid-tracks';
2
- import { computeCellRects, parseTrackList, snapToHalfPixel, toGridTracks, toPx } from '../grid-outline-utils';
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;