Skip to content

Commit

Permalink
fix: SlickCellExternalCopyManager should work w/hidden cols fixes #1634
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Aug 24, 2024
1 parent 559d7de commit b156bfa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
6 changes: 3 additions & 3 deletions packages/common/src/core/slickGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4150,7 +4150,7 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e

const d = this.getDataItem(row);

// TODO: shorten this loop (index? heuristics? binary search?)
// TODO: shorten this loop (index? heuristics? binary search?)
for (let i = 0, ii = this.columns.length; i < ii; i++) {
if (!this.columns[i] || this.columns[i].hidden) { continue; }

Expand Down Expand Up @@ -5104,7 +5104,7 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e

let w = 0;
for (let i = 0; i < this.columns.length && w <= x; i++) {
if (!this.columns[i] || this.columns[i].hidden) {
if (!this.columns[i]) {
continue;
}
w += this.columns[i].width as number;
Expand Down Expand Up @@ -5232,7 +5232,7 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
const y2 = y1 + this._options.rowHeight! - 1;
let x1 = 0;
for (let i = 0; i < cell; i++) {
if (!this.columns[i] || this.columns[i].hidden) {
if (!this.columns[i]) {
continue;
}

Expand Down
28 changes: 18 additions & 10 deletions packages/common/src/extensions/slickCellExternalCopyManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createDomElement, getHtmlStringOutput, stripTags } from '@slickgrid-universal/utils';

import { DataWrapperService } from '../services/dataWrapperService';
import type { Column, Editor, EditorConstructor, ElementPosition, ExcelCopyBufferOption, ExternalCopyClipCommand, OnEventArgs } from '../interfaces/index';
import type { Column, CssStyleHash, Editor, EditorConstructor, ElementPosition, ExcelCopyBufferOption, ExternalCopyClipCommand, OnEventArgs } from '../interfaces/index';
import { type SlickDataView, SlickEvent, SlickEventData, SlickEventHandler, type SlickGrid, SlickRange, Utils as SlickUtils } from '../core/index';

// using external SlickGrid JS libraries
Expand Down Expand Up @@ -311,31 +311,40 @@ export class SlickCellExternalCopyManager {
maxDestX: this._grid.getColumns().length,
h: 0,
w: 0,

execute: () => {
clipCommand.h = 0;
for (let y = 0; y < clipCommand.destH; y++) {
clipCommand.oldValues[y] = [];
clipCommand.w = 0;
clipCommand.h++;
let xOffset = 0; // the x offset for hidden col

for (let x = 0; x < clipCommand.destW; x++) {
clipCommand.w++;
const desty = activeRow + y;
const destx = activeCell + x;
const column = columns[destx];

// paste on hidden column will be skipped, but we need to paste 1 cell further on X axis
// we'll increase our X and increase the offset`
if (column.hidden) {
clipCommand.destW++;
xOffset++;
continue;
}
clipCommand.w++;

if (desty < clipCommand.maxDestY && destx < clipCommand.maxDestX) {
// const nd = this._grid.getCellNode(desty, destx);
const dt = this._dataWrapper.getDataItem(desty);

if (this._grid.triggerEvent(this.onBeforePasteCell, { row: desty, cell: destx, dt, column: columns[destx], target: 'grid' }).getReturnValue() === false) {
if (this._grid.triggerEvent(this.onBeforePasteCell, { row: desty, cell: destx, dt, column, target: 'grid' }).getReturnValue() === false) {
continue;
}

clipCommand.oldValues[y][x] = dt[columns[destx]['field']];
clipCommand.oldValues[y][x - xOffset] = dt[column['field']];
if (oneCellToMultiple) {
this.setDataItemValueForColumn(dt, columns[destx], clippedRange[0][0]);
this.setDataItemValueForColumn(dt, column, clippedRange[0][0]);
} else {
this.setDataItemValueForColumn(dt, columns[destx], clippedRange[y] ? clippedRange[y][x] : '');
this.setDataItemValueForColumn(dt, column, clippedRange[y] ? clippedRange[y][x - xOffset] : '');
}
this._grid.updateCell(desty, destx);
this._grid.onCellChange.notify({
Expand All @@ -359,7 +368,6 @@ export class SlickCellExternalCopyManager {
this._grid.getSelectionModel()?.setSelectedRanges([bRange]);
this.onPasteCells.notify({ ranges: [bRange] });
},

undo: () => {
for (let y = 0; y < clipCommand.destH; y++) {
for (let x = 0; x < clipCommand.destW; x++) {
Expand Down Expand Up @@ -522,7 +530,7 @@ export class SlickCellExternalCopyManager {
this.clearCopySelection();

const columns = this._grid.getColumns();
const hash: any = {};
const hash: CssStyleHash = {};
for (const range of ranges) {
for (let j = range.fromRow; j <= range.toRow; j++) {
hash[j] = {};
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/extensions/slickCellSelectionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class SlickCellSelectionModel implements SelectionModel {

this._selector = (options === undefined || options.cellRangeSelector === undefined)
? new SlickCellRangeSelector({ selectionCss: { border: '2px solid black' } as CSSStyleDeclaration })
: this._selector = options.cellRangeSelector;
: options.cellRangeSelector;

this._addonOptions = options;
}
Expand Down

0 comments on commit b156bfa

Please sign in to comment.