Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable viewport scrolling on paste #10630

Merged
merged 7 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changelogs/10630.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"issuesOrigin": "private",
"title": "Disabled table's viewport scrolling on data paste.",
"type": "changed",
"issueOrPR": 10630,
"breaking": false,
"framework": "none"
}
21 changes: 21 additions & 0 deletions handsontable/src/plugins/copyPaste/__tests__/paste.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,27 @@ describe('CopyPaste', () => {
expect(getSelectedRangeLast().to.col).toBe(9);
});

it('should paste data without scrolling the viewport', async() => {
handsontable({
data: createSpreadsheetData(50, 50),
width: 200,
height: 200,
});

selectCell(6, 2);
triggerPaste([
'test\ttest\ttest\ttest\ttest\ttest',
'test\ttest\ttest\ttest\ttest\ttest',
'test\ttest\ttest\ttest\ttest\ttest',
'test\ttest\ttest\ttest\ttest\ttest',
'test\ttest\ttest\ttest\ttest\ttest',
'test\ttest\ttest\ttest\ttest\ttest',
].join('\n'));

expect(topOverlay().getScrollPosition()).toBe(0);
expect(inlineStartOverlay().getScrollPosition()).toBe(0);
});

it('should sanitize pasted HTML', async() => {
handsontable();

Expand Down
25 changes: 25 additions & 0 deletions handsontable/src/plugins/copyPaste/copyPaste.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ export class CopyPaste extends BasePlugin {
columnsLimit: () => this.columnsLimit,
countColumnHeaders: () => this.hot.view.getColumnHeadersCount(),
});
/**
* Flag that indicates if the viewport scroll should be prevented after pasting the data.
*
* @type {boolean}
*/
#preventViewportScrollOnPaste = false;
/**
* Ranges of the cells coordinates, which should be used to copy/cut/paste actions.
*
Expand Down Expand Up @@ -220,6 +226,7 @@ export class CopyPaste extends BasePlugin {
}

this.addHook('afterContextMenuDefaultOptions', options => this.#onAfterContextMenuDefaultOptions(options));
this.addHook('afterSelection', (...args) => this.#onAfterSelection(...args));
this.addHook('afterSelectionEnd', () => this.#onAfterSelectionEnd());

this.eventManager.addEventListener(this.hot.rootDocument, 'copy', (...args) => this.onCopy(...args));
Expand Down Expand Up @@ -561,6 +568,7 @@ export class CopyPaste extends BasePlugin {
newRows.push(newRow);
}

this.#preventViewportScrollOnPaste = true;
this.hot.populateFromArray(startRow, startColumn, newRows, undefined, undefined, 'CopyPaste.paste', this.pasteMode);

return [startRow, startColumn, lastVisualRow, lastVisualColumn];
Expand Down Expand Up @@ -768,6 +776,23 @@ export class CopyPaste extends BasePlugin {
options.items.push(cutItem(this));
}

/**
* Disables the viewport scroll after pasting the data.
*
* @param {number} fromRow Selection start row visual index.
* @param {number} fromColumn Selection start column visual index.
* @param {number} toRow Selection end row visual index.
* @param {number} toColumn Selection end column visual index.
* @param {object} preventScrolling Object with `value` property. If `true`, the viewport scroll will be prevented.
*/
#onAfterSelection(fromRow, fromColumn, toRow, toColumn, preventScrolling) {
if (this.#preventViewportScrollOnPaste) {
preventScrolling.value = true;
}

this.#preventViewportScrollOnPaste = false;
}

/**
* Force focus on focusableElement after end of the selection.
*/
Expand Down