UNPKG

5.15 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.createTable = createTable;
9exports.updateCellContent = updateCellContent;
10exports.insertRow = insertRow;
11exports.deleteRow = deleteRow;
12exports.insertColumn = insertColumn;
13exports.deleteColumn = deleteColumn;
14
15var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
16
17var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
18
19var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));
20
21var _lodash = require("lodash");
22
23/**
24 * External dependencies
25 */
26
27/**
28 * Creates a table state.
29 *
30 * @param {number} options.rowCount Row count for the table to create.
31 * @param {number} options.columnCount Column count for the table to create.
32 *
33 * @return {Object} New table state.
34 */
35function createTable(_ref) {
36 var rowCount = _ref.rowCount,
37 columnCount = _ref.columnCount;
38 return {
39 body: (0, _lodash.times)(rowCount, function () {
40 return {
41 cells: (0, _lodash.times)(columnCount, function () {
42 return {
43 content: '',
44 tag: 'td'
45 };
46 })
47 };
48 })
49 };
50}
51/**
52 * Updates cell content in the table state.
53 *
54 * @param {Object} state Current table state.
55 * @param {string} options.section Section of the cell to update.
56 * @param {number} options.rowIndex Row index of the cell to update.
57 * @param {number} options.columnIndex Column index of the cell to update.
58 * @param {Array} options.content Content to set for the cell.
59 *
60 * @return {Object} New table state.
61 */
62
63
64function updateCellContent(state, _ref2) {
65 var section = _ref2.section,
66 rowIndex = _ref2.rowIndex,
67 columnIndex = _ref2.columnIndex,
68 content = _ref2.content;
69 return (0, _defineProperty2.default)({}, section, state[section].map(function (row, currentRowIndex) {
70 if (currentRowIndex !== rowIndex) {
71 return row;
72 }
73
74 return {
75 cells: row.cells.map(function (cell, currentColumnIndex) {
76 if (currentColumnIndex !== columnIndex) {
77 return cell;
78 }
79
80 return (0, _objectSpread2.default)({}, cell, {
81 content: content
82 });
83 })
84 };
85 }));
86}
87/**
88 * Inserts a row in the table state.
89 *
90 * @param {Object} state Current table state.
91 * @param {string} options.section Section in which to insert the row.
92 * @param {number} options.rowIndex Row index at which to insert the row.
93 *
94 * @return {Object} New table state.
95 */
96
97
98function insertRow(state, _ref4) {
99 var section = _ref4.section,
100 rowIndex = _ref4.rowIndex;
101 var cellCount = state[section][0].cells.length;
102 return (0, _defineProperty2.default)({}, section, [].concat((0, _toConsumableArray2.default)(state[section].slice(0, rowIndex)), [{
103 cells: (0, _lodash.times)(cellCount, function () {
104 return {
105 content: '',
106 tag: 'td'
107 };
108 })
109 }], (0, _toConsumableArray2.default)(state[section].slice(rowIndex))));
110}
111/**
112 * Deletes a row from the table state.
113 *
114 * @param {Object} state Current table state.
115 * @param {string} options.section Section in which to delete the row.
116 * @param {number} options.rowIndex Row index to delete.
117 *
118 * @return {Object} New table state.
119 */
120
121
122function deleteRow(state, _ref6) {
123 var section = _ref6.section,
124 rowIndex = _ref6.rowIndex;
125 return (0, _defineProperty2.default)({}, section, state[section].filter(function (row, index) {
126 return index !== rowIndex;
127 }));
128}
129/**
130 * Inserts a column in the table state.
131 *
132 * @param {Object} state Current table state.
133 * @param {string} options.section Section in which to insert the column.
134 * @param {number} options.columnIndex Column index at which to insert the column.
135 *
136 * @return {Object} New table state.
137 */
138
139
140function insertColumn(state, _ref8) {
141 var section = _ref8.section,
142 columnIndex = _ref8.columnIndex;
143 return (0, _defineProperty2.default)({}, section, state[section].map(function (row) {
144 return {
145 cells: [].concat((0, _toConsumableArray2.default)(row.cells.slice(0, columnIndex)), [{
146 content: '',
147 tag: 'td'
148 }], (0, _toConsumableArray2.default)(row.cells.slice(columnIndex)))
149 };
150 }));
151}
152/**
153 * Deletes a column from the table state.
154 *
155 * @param {Object} state Current table state.
156 * @param {string} options.section Section in which to delete the column.
157 * @param {number} options.columnIndex Column index to delete.
158 *
159 * @return {Object} New table state.
160 */
161
162
163function deleteColumn(state, _ref10) {
164 var section = _ref10.section,
165 columnIndex = _ref10.columnIndex;
166 return (0, _defineProperty2.default)({}, section, state[section].map(function (row) {
167 return {
168 cells: row.cells.filter(function (cell, index) {
169 return index !== columnIndex;
170 })
171 };
172 }).filter(function (row) {
173 return row.cells.length;
174 }));
175}
176//# sourceMappingURL=state.js.map
\No newline at end of file