Skip to content

Commit

Permalink
feat: bypass formatter export limitation if current cell is not in ed…
Browse files Browse the repository at this point in the history
…it mode
  • Loading branch information
zewa666 committed Feb 5, 2024
1 parent 957a36d commit 111645c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { EditCommand, Formatter, GridOption } from '../../interfaces/index';
import { Formatters } from '../../formatters';
import { SharedService } from '../../services/shared.service';
import { SlickCellExcelCopyManager } from '../slickCellExcelCopyManager';
import { SlickCellSelectionModel } from '../slickCellSelectionModel';
import { SlickCellExternalCopyManager } from '../slickCellExternalCopyManager';
import { SlickEvent, SlickEventData, SlickGrid, SlickRange } from '../../core/index';
import { Editors } from '../../editors';

jest.mock('flatpickr', () => { });

Expand All @@ -17,6 +17,8 @@ const gridStub = {
getData: jest.fn(),
getOptions: jest.fn(),
getSelectionModel: jest.fn(),
getActiveCell: jest.fn(),
getCellEditor: jest.fn(),
getEditorLock: () => getEditorLockMock,
focus: jest.fn(),
registerPlugin: jest.fn(),
Expand Down Expand Up @@ -356,14 +358,36 @@ describe('CellExcelCopyManager', () => {
expect(output).toBe('John');
});

it('should return null when calling "dataItemColumnValueExtractor" callback without editable', () => {
it('should return null when calling "dataItemColumnValueExtractor" callback with editable and editor, which is active on the current cell', () => {
jest.spyOn(gridStub, 'getOptions').mockReturnValue(gridOptionsMock);
plugin.init(gridStub);
(gridStub.getCellEditor as jest.Mock).mockReturnValue({});
(gridStub.getActiveCell as jest.Mock).mockReturnValue({ row: 6, cell: 6 });

const output = plugin.addonOptions!.dataItemColumnValueExtractor!({ firstName: '<b>John</b>', lastName: 'Doe' }, { id: 'firstName', field: 'firstName', exportWithFormatter: true, editor: { model: Editors.text }, formatter: myBoldFormatter}, 6, 6);

expect(output).toBeNull();
});

it('should forward provided row and cell to formatter when calling "dataItemColumnValueExtractor"', () => {
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock);
plugin.init(gridStub);

const rowCellFormatter: Formatter = (row, cell) => `${row}:${cell}`;
const output = plugin.addonOptions!.dataItemColumnValueExtractor!({ firstName: '<b>John</b>', lastName: 'Doe' }, { id: 'firstName', field: 'firstName', exportWithFormatter: true, formatter: rowCellFormatter }, 6, 6);

expect(output).toBe('6:6');
});

it('should format output even if not editable and an editor is configured but a formatter is defined', () => {
gridOptionsMock.editable = false;
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock);
plugin.init(gridStub);

const output = plugin.addonOptions!.dataItemColumnValueExtractor!({ firstName: '<b>John</b>', lastName: 'Doe' }, { id: 'firstName', field: 'firstName' });
const rowCellFormatter: Formatter = (row, cell) => `${row}:${cell}`;
const output = plugin.addonOptions!.dataItemColumnValueExtractor!({ firstName: '<b>John</b>', lastName: 'Doe' }, { id: 'firstName', field: 'firstName', exportWithFormatter: true, editor: { model: Editors.text }, formatter: rowCellFormatter }, 6, 6);

expect(output).toBeNull();
expect(output).toBe('6:6');
});
});
});
5 changes: 4 additions & 1 deletion packages/common/src/extensions/slickCellExcelCopyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ export class SlickCellExcelCopyManager {
dataItemColumnValueExtractor: (item: any, columnDef: Column, row: number = 0, cell: number = 0) => {
// when grid or cell is not editable, we will possibly evaluate the Formatter if it was passed
// to decide if we evaluate the Formatter, we will use the same flag from Export which is "exportWithFormatter"
if (!this.gridOptions.editable || !columnDef.editor) {
const activeCell = this._grid.getActiveCell();
const isActiveEditorCurrentCell = this._grid.getCellEditor() && activeCell?.row === row && activeCell?.cell === cell;

if (!this.gridOptions.editable || !columnDef.editor || !isActiveEditorCurrentCell) {
const isEvaluatingFormatter = (columnDef.exportWithFormatter !== undefined) ? columnDef.exportWithFormatter : (this.gridOptions.textExportOptions?.exportWithFormatter);
if (columnDef.formatter && isEvaluatingFormatter) {
const formattedOutput = columnDef.formatter(row, cell, item[columnDef.field], columnDef, item, this._grid);
Expand Down

0 comments on commit 111645c

Please sign in to comment.