@wordpress/block-library
Version:
Block library for the WordPress editor.
154 lines (145 loc) • 4.55 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
/**
* External dependencies
*/
import { times } from 'lodash';
/**
* Creates a table state.
*
* @param {number} options.rowCount Row count for the table to create.
* @param {number} options.columnCount Column count for the table to create.
*
* @return {Object} New table state.
*/
export function createTable(_ref) {
var rowCount = _ref.rowCount,
columnCount = _ref.columnCount;
return {
body: times(rowCount, function () {
return {
cells: times(columnCount, function () {
return {
content: '',
tag: 'td'
};
})
};
})
};
}
/**
* Updates cell content in the table state.
*
* @param {Object} state Current table state.
* @param {string} options.section Section of the cell to update.
* @param {number} options.rowIndex Row index of the cell to update.
* @param {number} options.columnIndex Column index of the cell to update.
* @param {Array} options.content Content to set for the cell.
*
* @return {Object} New table state.
*/
export function updateCellContent(state, _ref2) {
var section = _ref2.section,
rowIndex = _ref2.rowIndex,
columnIndex = _ref2.columnIndex,
content = _ref2.content;
return _defineProperty({}, section, state[section].map(function (row, currentRowIndex) {
if (currentRowIndex !== rowIndex) {
return row;
}
return {
cells: row.cells.map(function (cell, currentColumnIndex) {
if (currentColumnIndex !== columnIndex) {
return cell;
}
return _objectSpread({}, cell, {
content: content
});
})
};
}));
}
/**
* Inserts a row in the table state.
*
* @param {Object} state Current table state.
* @param {string} options.section Section in which to insert the row.
* @param {number} options.rowIndex Row index at which to insert the row.
*
* @return {Object} New table state.
*/
export function insertRow(state, _ref4) {
var section = _ref4.section,
rowIndex = _ref4.rowIndex;
var cellCount = state[section][0].cells.length;
return _defineProperty({}, section, [].concat(_toConsumableArray(state[section].slice(0, rowIndex)), [{
cells: times(cellCount, function () {
return {
content: '',
tag: 'td'
};
})
}], _toConsumableArray(state[section].slice(rowIndex))));
}
/**
* Deletes a row from the table state.
*
* @param {Object} state Current table state.
* @param {string} options.section Section in which to delete the row.
* @param {number} options.rowIndex Row index to delete.
*
* @return {Object} New table state.
*/
export function deleteRow(state, _ref6) {
var section = _ref6.section,
rowIndex = _ref6.rowIndex;
return _defineProperty({}, section, state[section].filter(function (row, index) {
return index !== rowIndex;
}));
}
/**
* Inserts a column in the table state.
*
* @param {Object} state Current table state.
* @param {string} options.section Section in which to insert the column.
* @param {number} options.columnIndex Column index at which to insert the column.
*
* @return {Object} New table state.
*/
export function insertColumn(state, _ref8) {
var section = _ref8.section,
columnIndex = _ref8.columnIndex;
return _defineProperty({}, section, state[section].map(function (row) {
return {
cells: [].concat(_toConsumableArray(row.cells.slice(0, columnIndex)), [{
content: '',
tag: 'td'
}], _toConsumableArray(row.cells.slice(columnIndex)))
};
}));
}
/**
* Deletes a column from the table state.
*
* @param {Object} state Current table state.
* @param {string} options.section Section in which to delete the column.
* @param {number} options.columnIndex Column index to delete.
*
* @return {Object} New table state.
*/
export function deleteColumn(state, _ref10) {
var section = _ref10.section,
columnIndex = _ref10.columnIndex;
return _defineProperty({}, section, state[section].map(function (row) {
return {
cells: row.cells.filter(function (cell, index) {
return index !== columnIndex;
})
};
}).filter(function (row) {
return row.cells.length;
}));
}
//# sourceMappingURL=state.js.map