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

Plugin: add-rows-on-paste #599

Merged
merged 3 commits into from
Oct 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/plugins/add-rows-on-past.plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { PluginProviders } from '../types/plugin.types';
import { BasePlugin } from './base.plugin';

/**
* Automatically adds new rows when pasted data is larger than current rows
* @event newRows - is triggered when new rows are added. Data of new rows can be filled with default values. If the event is prevented, no rows will be added. Event data: { newRows: RowData[] }
*/
export class AutoAddRowsPlugin extends BasePlugin {
constructor(revogrid: HTMLRevoGridElement, providers: PluginProviders) {
super(revogrid, providers);
this.addEventListener('beforepasteapply', evt =>
this.handleBeforePasteApply(evt),
);
}

handleBeforePasteApply(
event: CustomEvent<{
raw: string;
parsed: string[][];
event: ClipboardEvent;
}>,
) {
const start = this.providers.selection.focused;
const isEditing = this.providers.selection.edit != null;

if (!start || isEditing) {
return;
}

const rowLength =
this.providers.data.stores.rgRow.store.get('items').length;

const endRow = start.y + event.detail.parsed.length;

if (rowLength < endRow) {
const count = endRow - rowLength;
const newRows = Array.from({ length: count }, (_, i) => ({
index: rowLength + i,
data: {},
}));

const event = this.emit('newRows', { newRows: newRows });

if (event.defaultPrevented) {
return;
}

const items = [
...this.providers.data.stores.rgRow.store.get('source'),
...event.detail.newRows.map(j => j.data),
];

this.providers.data.setData(items);
}
}
}
1 change: 1 addition & 0 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './groupingRow/grouping.row.plugin';
export * from './moveColumn/column.drag.plugin';
export * from './sorting/sorting.plugin';
export * from './sorting/sorting.sign';
export * from './add-rows-on-past.plugin';