Skip to content

Commit ff2842c

Browse files
committed
PB-108 MFTF stabilisation - take maxima of non empty column count from all column lines
1 parent 92516cc commit ff2842c

File tree

4 files changed

+89
-22
lines changed

4 files changed

+89
-22
lines changed

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/column-group/grid-size.js

Lines changed: 17 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/column-group/preview.js

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/column-group/grid-size.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ export function resizeGrid(
5353

5454
validateNewGridSize(columnGroup, newGridSize);
5555

56-
// if we have more columns than the new grid size allows, remove empty columns until we are at the correct size
57-
if (newGridSize < columnGroup.getChildren()().length) {
58-
removeEmptyColumnsToFit(columnGroup, newGridSize);
59-
}
56+
columnGroup.getChildren()().forEach(
57+
(columnLine: ContentTypeCollectionInterface<ColumnLinePreview>, index: number) => {
58+
// if we have more columns than the new grid size allows, remove empty columns until we are at the correct size
59+
console.log(columnLine.getChildren()().length);
60+
if (newGridSize < columnLine.getChildren()().length) {
61+
removeEmptyColumnsToFit(columnLine, newGridSize);
62+
}
63+
});
6064

6165
// update column widths
6266
redistributeColumnWidths(columnGroup, newGridSize, gridSizeHistory);
@@ -78,7 +82,7 @@ function validateNewGridSize(columnGroup: ContentTypeCollectionInterface<ColumnG
7882

7983
// Validate that the operation will be successful
8084

81-
85+
let doThrowException = false;
8286
columnGroup.getChildren()().forEach(
8387
(columnLine: ContentTypeCollectionInterface<ColumnLinePreview>, index: number) => {
8488
let numEmptyColumns = 0;
@@ -92,12 +96,16 @@ function validateNewGridSize(columnGroup: ContentTypeCollectionInterface<ColumnG
9296
}
9397
});
9498
if (newGridSize < numCols - numEmptyColumns) {
95-
throw new GridSizeError(
96-
$t("Grid size cannot be smaller than the current total amount of columns, minus any empty columns."),
97-
);
99+
doThrowException = true;
98100
}
99101
}
100102
});
103+
104+
if (doThrowException) {
105+
throw new Error(
106+
$t("Grid size cannot be smaller than the current total amount of columns, minus any empty columns."),
107+
);
108+
}
101109
}
102110

103111
/**
@@ -106,14 +114,14 @@ function validateNewGridSize(columnGroup: ContentTypeCollectionInterface<ColumnG
106114
* @param {ContentTypeCollectionInterface<Preview>} columnGroup
107115
* @param {number} newGridSize
108116
*/
109-
function removeEmptyColumnsToFit(columnGroup: ContentTypeCollectionInterface<ColumnGroupPreview>, newGridSize: number) {
110-
const columns = columnGroup.getChildren()() as Array<ContentTypeCollectionInterface<ColumnPreview>>;
117+
function removeEmptyColumnsToFit(columnLine: ContentTypeCollectionInterface<ColumnLinePreview>, newGridSize: number) {
118+
const columns = columnLine.getChildren()() as Array<ContentTypeCollectionInterface<ColumnPreview>>;
111119
let numColumns = columns.length;
112120
let i;
113121
for (i = numColumns - 1; i >= 0; i--) {
114122
const column: ContentTypeCollectionInterface<ColumnPreview> = columns[i];
115123
if (newGridSize < numColumns && column.getChildren()().length === 0) {
116-
columnGroup.removeChild(column);
124+
columnLine.removeChild(column);
117125
numColumns--;
118126
}
119127
}

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/column-group/preview.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
import {createColumn, createColumnLine} from "./factory";
4242
import {getDefaultGridSize, getMaxGridSize, GridSizeError, resizeGrid} from "./grid-size";
4343
import {getDragColumn, removeDragColumn, setDragColumn} from "./registry";
44+
import ColumnLinePreview from "Magento_PageBuilder/js/content-type/column-line/preview";
4445

4546
/**
4647
* @api
@@ -155,7 +156,7 @@ export default class Preview extends PreviewCollection {
155156

156157
const appearance = this.contentType.dataStore.get("appearance") ? this.contentType.dataStore.get("appearance") : "default";
157158
this.contentType.dataStore.set("appearance", appearance);
158-
this.contentType.dataStore.set("non_empty_column_count", numCols - numEmptyColumns);
159+
this.contentType.dataStore.set("non_empty_column_count", this.getNonEmptyColumnCount());
159160
this.contentType.dataStore.set("max_grid_size", getMaxGridSize());
160161
this.contentType.dataStore.set("initial_grid_size", this.contentType.dataStore.get("grid_size"));
161162
super.openEdit();
@@ -1131,6 +1132,33 @@ export default class Preview extends PreviewCollection {
11311132
this.gridSizeHistory.set(newGridSize, columnWidths);
11321133
}
11331134
}
1135+
1136+
/**
1137+
* Figure out the maximum number of non-empty columns in various column lines
1138+
* @private
1139+
*/
1140+
private getNonEmptyColumnCount(): number {
1141+
1142+
let nonEmptyColumnCount = 0;
1143+
this.contentType.getChildren()().forEach(
1144+
(columnLine: ContentTypeCollectionInterface<ColumnLinePreview>, index: number) => {
1145+
let numEmptyColumns = 0;
1146+
const numCols = columnLine.getChildren()().length;
1147+
columnLine.getChildren()().forEach(
1148+
(column: ContentTypeCollectionInterface<ColumnPreview>) => {
1149+
if (column.getChildren()().length === 0) {
1150+
numEmptyColumns++;
1151+
}
1152+
});
1153+
1154+
if (numCols - numEmptyColumns > nonEmptyColumnCount) {
1155+
nonEmptyColumnCount = numCols - numEmptyColumns;
1156+
}
1157+
1158+
});
1159+
1160+
return nonEmptyColumnCount;
1161+
}
11341162
}
11351163

11361164
export interface GroupPositionCache {

0 commit comments

Comments
 (0)