UNPKG

@antv/s2

Version:

effective spreadsheet render core lib

211 lines 10 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processSelectedTableByDataCell = exports.asyncProcessSelectedAllTable = exports.processSelectedTableByHeader = void 0; const tslib_1 = require("tslib"); const lodash_1 = require("lodash"); const common_1 = require("../../../common"); const method_1 = require("../method"); const base_data_cell_copy_1 = require("./base-data-cell-copy"); const common_2 = require("./common"); class TableDataCellCopy extends base_data_cell_copy_1.BaseDataCellCopy { constructor(params) { super(params); this.getValueFromMeta = (meta) => { var _a; const [, colNode] = (0, common_2.getHeaderNodeFromMeta)(meta, this.spreadsheet); const field = (0, method_1.getColNodeFieldFromNode)(this.spreadsheet.isPivotMode, colNode); const value = this.isSeriesNumberField(field) ? meta.rowIndex + 1 : (_a = this.displayData[meta.rowIndex]) === null || _a === void 0 ? void 0 : _a[field]; const formatter = this.getFormatter({ field, rowIndex: meta.rowIndex, colIndex: meta.colIndex, }); return formatter(value); }; this.displayData = this.getSelectedDisplayData(); this.columnNodes = this.getSelectedColNodes(); } getHeaderNodeMatrix(node) { // 明细表的表头配置即作为列头, 也作为数值, 所以列头不应该被格式化 return super.getHeaderNodeMatrix(node); } getSelectedColNodes() { const selectedCols = (0, method_1.getSelectedCols)(this.config.selectedCells); const colLeafNodes = this.spreadsheet.facet.getColLeafNodes(); if (selectedCols.length === 0) { return colLeafNodes; } return (0, lodash_1.map)(selectedCols, (meta) => colLeafNodes[meta.colIndex]); } getSelectedDisplayData() { const selectedRows = (0, method_1.getSelectedRows)(this.config.selectedCells); const originDisplayData = this.spreadsheet.dataSet.getDisplayDataSet(); if (selectedRows.length === 0) { return originDisplayData; } return (0, lodash_1.map)(selectedRows, (cell) => originDisplayData[cell.rowIndex]); } getDataMatrix() { const { seriesNumber } = this.spreadsheet.options; return this.displayData.map((row, i) => this.columnNodes.map((node, j) => { const field = node === null || node === void 0 ? void 0 : node.field; if (common_1.SERIES_NUMBER_FIELD === field && (seriesNumber === null || seriesNumber === void 0 ? void 0 : seriesNumber.enable)) { return (i + 1).toString(); } const formatter = this.getFormatter({ field, rowIndex: i, colIndex: j, }); const value = row === null || row === void 0 ? void 0 : row[field]; return formatter(value); })); } getDataMatrixRIC() { const { seriesNumber } = this.spreadsheet.options; const result = []; let rowIndex = 0; return new Promise((resolve, reject) => { try { const dataMatrixIdleCallback = (deadline) => { const rowLength = this.displayData.length; // requestIdleCallback 浏览器空闲时会多次执行, 只有一行数据时执行一次即可, 避免生成重复数据 this.initIdleCallbackCount(rowLength); while ((deadline.timeRemaining() > 0 || deadline.didTimeout || process.env['NODE_ENV'] === 'test') && rowIndex <= rowLength - 1 && this.idleCallbackCount > 0) { for (let j = rowIndex; j < rowLength && this.idleCallbackCount > 0; j++) { const rowData = this.displayData[j]; const row = []; for (let i = 0; i < this.columnNodes.length; i++) { const colNode = this.columnNodes[i]; const field = colNode.field; if (common_1.SERIES_NUMBER_FIELD === field && (seriesNumber === null || seriesNumber === void 0 ? void 0 : seriesNumber.enable)) { row.push((j + 1).toString()); // eslint-disable-next-line no-continue continue; } const formatter = this.getFormatter({ field, rowIndex, colIndex: i, }); const value = rowData[field]; const dataItem = formatter(value); row.push(dataItem); } // 生成一行数据后,rowIndex + 1,下次 requestIdleCallback 时从下一行开始 rowIndex++; result.push(row); this.idleCallbackCount--; } } if (rowIndex === rowLength) { resolve(result); } else { // 重置 idleCallbackCount,避免下次 requestIdleCallback 时 idleCallbackCount 为 0 this.initIdleCallbackCount(rowLength); requestIdleCallback(dataMatrixIdleCallback, common_2.ricOptions); } }; requestIdleCallback(dataMatrixIdleCallback, common_2.ricOptions); } catch (e) { reject(e); } }); } isSeriesNumberField(field) { const { seriesNumber } = this.spreadsheet.options; return common_1.SERIES_NUMBER_FIELD === field && (seriesNumber === null || seriesNumber === void 0 ? void 0 : seriesNumber.enable); } getColMatrix() { return (0, lodash_1.zip)(...this.columnNodes.map((node) => this.getHeaderNodeMatrix(node))); } getDataMatrixByDataCell(cellMetaMatrix) { const { copy } = this.spreadsheet.options.interaction; // 因为通过复制数据单元格的方式和通过行列头复制的方式不同,所以不能复用 getDataMatrix 方法 const dataMatrix = (0, lodash_1.map)(cellMetaMatrix, (cellsMeta) => (0, lodash_1.map)(cellsMeta, (meta) => this.getValueFromMeta(meta))); if (!(copy === null || copy === void 0 ? void 0 : copy.withHeader)) { return this.matrixTransformer(dataMatrix, this.config.separator); } const colMatrix = this.getColMatrix(); return this.matrixTransformer((0, common_2.assembleMatrix)({ colMatrix, dataMatrix }), this.config.separator); } /** * allSelected: false 时,明细表点击 行头/列头 进行复制逻辑 * allSelected: true 时,明细表点击 全选 进行复制逻辑 * @deprecated 后续将废弃,使用 asyncProcessSelectedTable 替代 */ processSelectedTable(allSelected = false) { const matrix = this.getDataMatrix(); if (!allSelected) { return this.matrixTransformer(matrix, this.config.separator); } const colMatrix = this.getColMatrix(); return this.matrixTransformer((0, common_2.assembleMatrix)({ colMatrix, dataMatrix: matrix }), this.config.separator); } asyncProcessSelectedTable() { return tslib_1.__awaiter(this, arguments, void 0, function* (allSelected = false) { const matrix = this.isEnableASync() ? yield this.getDataMatrixRIC() : yield Promise.resolve(this.getDataMatrix()); if (!allSelected) { return this.matrixTransformer(matrix, this.config.separator); } const colMatrix = this.getColMatrix(); return this.matrixTransformer((0, common_2.assembleMatrix)({ colMatrix, dataMatrix: matrix }), this.config.separator); }); } } /** * 明细表点击行头进行复制逻辑 * @param {SpreadSheet} spreadsheet * @param {CellMeta[]} selectedHeaders * @return {CopyableList} */ const processSelectedTableByHeader = (spreadsheet, selectedHeaders) => { const tableDataCellCopy = new TableDataCellCopy({ spreadsheet, config: { selectedCells: selectedHeaders, }, }); return tableDataCellCopy.processSelectedTable(); }; exports.processSelectedTableByHeader = processSelectedTableByHeader; // 导出全部数据 const asyncProcessSelectedAllTable = (params) => { const { sheetInstance, split, formatOptions, customTransformer, async } = params; const tableDataCellCopy = new TableDataCellCopy({ spreadsheet: sheetInstance, config: { selectedCells: [], separator: split, formatOptions, customTransformer, async: async !== null && async !== void 0 ? async : true, }, isExport: true, }); return tableDataCellCopy.asyncProcessSelectedTable(true); }; exports.asyncProcessSelectedAllTable = asyncProcessSelectedAllTable; // 通过选中数据单元格进行复制 const processSelectedTableByDataCell = ({ spreadsheet, selectedCells, headerSelectedCells, }) => { const tableDataCellCopy = new TableDataCellCopy({ spreadsheet, config: { selectedCells: headerSelectedCells, formatOptions: true, }, }); return tableDataCellCopy.getDataMatrixByDataCell(selectedCells); }; exports.processSelectedTableByDataCell = processSelectedTableByDataCell; //# sourceMappingURL=table-copy.js.map