# Clipboard Plugin

> Copy and paste grid data with Excel-compatible clipboard support.

The Clipboard plugin brings familiar copy/paste functionality to your grid with full keyboard shortcut support (Ctrl+C, Ctrl+V). It handles single cells, multi-cell selections, and interoperates with Excel and other spreadsheet applications via tab-delimited output.

:::tip
Works best with [SelectionPlugin](https://toolboxjs.com/grid/plugins/selection.md) for copying/pasting selected cells. Without it, copies the entire grid and pastes at row 0, column 0.
:::

## Installation

```ts
import '@toolbox-web/grid/features/clipboard';
```

## Basic Usage

Just enable the feature and you're ready to go—keyboard shortcuts work automatically. Configure options like `includeHeaders` for copying column headers or `delimiter` for custom CSV formats.

#### TypeScript

```ts
import { queryGrid } from '@toolbox-web/grid';

const grid = queryGrid('tbw-grid');
grid.gridConfig = {
  columns: [
    { field: 'name', header: 'Name' },
    { field: 'email', header: 'Email' },
    { field: 'department', header: 'Department' }
  ],
  features: {
    clipboard: {
      includeHeaders: true,
      quoteStrings: true, // Wrap text in quotes for CSV compatibility
    },
  },
};
```

#### React

```tsx
import '@toolbox-web/grid-react/features/selection';
import '@toolbox-web/grid-react/features/clipboard';
import { DataGrid } from '@toolbox-web/grid-react';
import type { GridConfig } from '@toolbox-web/grid-react';

const gridConfig: GridConfig = {
  columns: [
    { field: 'name', header: 'Name' },
    { field: 'email', header: 'Email' },
    { field: 'department', header: 'Department' }
  ],
  features: {
    selection: 'range',
    clipboard: { includeHeaders: true, quoteStrings: true },
  },
};

function MyGrid({ data }) {
  return <DataGrid rows={data} gridConfig={gridConfig} style={{ height: '400px' }} />;
}
```

:::note[Alternative: feature prop]
You can instead pass `columns={[ … ]}`, `selection="range"`, and `clipboard={{ … }}` as individual props on `<DataGrid>`. That form is idiomatic React, but `gridConfig` keeps the configuration identical across every framework.
:::

#### Vue

```html
<script setup>
import '@toolbox-web/grid-vue/features/selection';
import '@toolbox-web/grid-vue/features/clipboard';
import { TbwGrid } from '@toolbox-web/grid-vue';
import type { GridConfig } from '@toolbox-web/grid-vue';

const data = [
  { name: 'Alice', email: 'alice@example.com', department: 'Engineering' },
  { name: 'Bob', email: 'bob@example.com', department: 'Marketing' },
];

const gridConfig: GridConfig = {
  columns: [
    { field: 'name', header: 'Name' },
    { field: 'email', header: 'Email' },
    { field: 'department', header: 'Department' },
  ],
  features: {
    selection: 'range',
    clipboard: { includeHeaders: true, quoteStrings: true },
  },
};
</script>

<template>
  <TbwGrid :rows="data" :grid-config="gridConfig" style="height: 400px" />
</template>
```

:::note[Alternative: prop + light-dom columns]
You can instead set `selection="range"`, bind `:clipboard="{ … }"`, and declare columns with `<TbwGridColumn>` children. That form is idiomatic Vue, but `gridConfig` keeps the configuration identical across every framework.
:::

#### Angular

```typescript
import '@toolbox-web/grid-angular/features/selection';
import '@toolbox-web/grid-angular/features/clipboard';
import { Component } from '@angular/core';
import { Grid } from '@toolbox-web/grid-angular';
import type { GridConfig } from '@toolbox-web/grid-angular';

@Component({
  selector: 'app-my-grid',
  imports: [Grid],
  template: `
    <tbw-grid
      [rows]="rows"
      [gridConfig]="gridConfig"
      style="height: 400px; display: block;">
    </tbw-grid>
  `,
})
export class MyGridComponent {
  rows = [...];

  gridConfig: GridConfig = {
    columns: [
      { field: 'name', header: 'Name' },
      { field: 'email', header: 'Email' },
      { field: 'department', header: 'Department' }
    ],
    features: {
      selection: 'range',
      clipboard: { includeHeaders: true, quoteStrings: true },
    },
  };
}
```

:::note[Alternative: per-feature directive]
You can instead import `GridSelectionDirective` and `GridClipboardDirective` and bind `[selection]` and `[clipboard]` directly on `<tbw-grid>`. That form gives a compile-time check that the features are imported, but `gridConfig.features` above is the recommended, framework-consistent approach.
:::

## Demos

### Default Clipboard

```ts
// ClipboardDefaultDemo.astro
import '@toolbox-web/grid';
import { queryGrid } from '@toolbox-web/grid';
import '@toolbox-web/grid/features/clipboard';
import '@toolbox-web/grid/features/editing';
import '@toolbox-web/grid/features/selection';

const container = document.getElementById('clipboard-default-demo');
if (container) {
  const sampleData = [
    { id: 1, name: 'Alice', email: 'alice@example.com', department: 'Engineering' },
    { id: 2, name: 'Bob', email: 'bob@example.com', department: 'Marketing' },
    { id: 3, name: 'Carol', email: 'carol@example.com', department: 'Engineering' },
    { id: 4, name: 'Dan', email: 'dan@example.com', department: 'Sales' },
  ];
  const columns = [
    { field: 'id', header: 'ID', type: 'number' },
    { field: 'name', header: 'Name', editable: true },
    { field: 'email', header: 'Email', editable: true },
    { field: 'department', header: 'Department', editable: true },
  ];

  const grid = queryGrid('tbw-grid', container)!;

  function rebuild(includeHeaders = false, quoteStrings = false) {
    grid.gridConfig = {
      columns,
      features: {
        selection: 'range',
        clipboard: { includeHeaders, quoteStrings },
        editing: true,
      },
    };
    grid.rows = sampleData;
  }

  rebuild();

  container.addEventListener('control-change', ((e: CustomEvent) => {
    const v = e.detail.allValues;
    rebuild(v.includeHeaders as boolean, v.quoteStrings as boolean);
  }) as EventListener);
}
```

Select cells and use Ctrl+C to copy, Ctrl+V to paste.

### Copy & Paste (Auto-Paste)

```ts
// ClipboardCopyPasteDemo.astro
import '@toolbox-web/grid';
import { queryGrid } from '@toolbox-web/grid';

import '@toolbox-web/grid/features/clipboard';
import '@toolbox-web/grid/features/editing';
import '@toolbox-web/grid/features/selection';

const container = document.getElementById('clipboard-copy-paste-demo');
if (container) {
  const grid = queryGrid('tbw-grid', container);

  grid.gridConfig = {
    columns: [
      { field: 'col1', header: 'Column 1', editable: true },
      { field: 'col2', header: 'Column 2', editable: true },
      { field: 'col3', header: 'Column 3', editable: true },
    ],
    features: {
      selection: 'range',
      clipboard: true,
      editing: true,
    },
  };

  grid.rows = [
    { col1: 'A1', col2: 'B1', col3: 'C1' },
    { col1: 'A2', col2: 'B2', col3: 'C2' },
    { col1: 'A3', col2: 'B3', col3: 'C3' },
    { col1: 'A4', col2: 'B4', col3: 'C4' },
  ];
}
```

By default, the ClipboardPlugin handles paste operations automatically—no event handling needed.
Just add the plugin and paste works out of the box.

### Custom Paste Handler

```ts
// ClipboardCustomPasteHandlerDemo.astro
import '@toolbox-web/grid';
import { queryGrid } from '@toolbox-web/grid';

import '@toolbox-web/grid/features/clipboard';
import '@toolbox-web/grid/features/selection';

const container = document.getElementById('clipboard-custom-paste-handler-demo');
if (container) {
  // Sample data for clipboard demos
  const sampleData = [
    { id: 1, name: 'Alice', email: 'alice@example.com', department: 'Engineering' },
    { id: 2, name: 'Bob', email: 'bob@example.com', department: 'Marketing' },
    { id: 3, name: 'Carol', email: 'carol@example.com', department: 'Engineering' },
    { id: 4, name: 'Dan', email: 'dan@example.com', department: 'Sales' },
  ];
  const columns = [
    { field: 'id', header: 'ID', type: 'number' },
    { field: 'name', header: 'Name' },
    { field: 'email', header: 'Email' },
    { field: 'department', header: 'Department' },
  ];

  // Sample data to copy
      const sampleDataBox = document.createElement('div');
      sampleDataBox.style.cssText = `
        padding: 12px;
        background: var(--demo-sample-bg);
        border-bottom: 1px solid var(--demo-sample-border);
        font-family: monospace;
        font-size: 13px;
        white-space: pre;
        user-select: all;
        cursor: text;
      `;
      sampleDataBox.textContent = `hello\tworld
  foo\tbar`;
      sampleDataBox.title = 'Paste this into the grid - values will be uppercased!';

      // Instructions banner
      const banner = document.createElement('div');
      banner.style.cssText = `
        padding: 12px;
        background: var(--demo-info-bg);
        border-bottom: 1px solid var(--demo-info-border);
        font-size: 14px;
        color: var(--demo-info-fg);
      `;
      banner.innerHTML = `
        <strong>Custom Handler:</strong> Pasted values are automatically UPPERCASED.
        <br>This demo also prevents adding new rows when pasting beyond the grid.
      `;

      // Grid
      const grid = queryGrid('tbw-grid', container);
      grid.style.cssText = 'flex: 1;';

      // Custom paste handler that uppercases values and prevents adding rows
      const customPasteHandler = (detail, gridEl) => {
        if (!detail.target) return;

        const { rows: pastedRows, target, fields } = detail;
        const columns = gridEl.effectiveConfig.columns ?? [];
        const allFields = columns.map((c) => c.field);
        const currentRows = [...(gridEl.rows as Record<string, string>[])];

        pastedRows.forEach((rowData, rowOffset) => {
          const targetRowIndex = target.row + rowOffset;

          // This handler allows adding rows (unlike the code sample which doesn't)
          while (targetRowIndex >= currentRows.length) {
            const emptyRow = {};
            allFields.forEach((f) => (emptyRow[f] = ''));
            currentRows.push(emptyRow);
          }

          currentRows[targetRowIndex] = { ...currentRows[targetRowIndex] };
          rowData.forEach((value, colOffset) => {
            const field = fields[colOffset];
            if (field) {
              // Transform: uppercase all values
              currentRows[targetRowIndex][field] = value.toUpperCase();
            }
          });
        });

        gridEl.rows = currentRows;
      };

      grid.gridConfig = {
        columns: [
          { field: 'col1', header: 'Column 1' },
          { field: 'col2', header: 'Column 2' },
          { field: 'col3', header: 'Column 3' },
        ],
        features: {
          selection: 'range',
          clipboard: { pasteHandler: customPasteHandler },
        },
      };

      grid.rows = [
        { col1: 'A1', col2: 'B1', col3: 'C1' },
        { col1: 'A2', col2: 'B2', col3: 'C2' },
        { col1: 'A3', col2: 'B3', col3: 'C3' },
      ];
}
```

For advanced use cases, provide a custom `pasteHandler` to validate, transform, or integrate with state management.

### Single Cell Mode

```ts
// ClipboardSingleCellModeDemo.astro
import '@toolbox-web/grid';
import { queryGrid } from '@toolbox-web/grid';

import '@toolbox-web/grid/features/clipboard';

const container = document.getElementById('clipboard-single-cell-demo');
if (container) {
  const grid = queryGrid('tbw-grid', container);

  grid.gridConfig = {
    columns: [
      { field: 'id', header: 'ID', type: 'number', width: 60 },
      { field: 'name', header: 'Name' },
      { field: 'department', header: 'Department' },
      { field: 'email', header: 'Email' },
      { field: 'salary', header: 'Salary', type: 'number', align: 'right' },
    ],
    features: { clipboard: true },
  };

  grid.rows = [
    { id: 1, name: 'Alice Johnson', department: 'Engineering', email: 'alice@company.com', salary: 85000 },
    { id: 2, name: 'Bob Smith', department: 'Marketing', email: 'bob@company.com', salary: 72000 },
    { id: 3, name: 'Carol Williams', department: 'Engineering', email: 'carol@company.com', salary: 92000 },
    { id: 4, name: 'David Brown', department: 'Sales', email: 'david@company.com', salary: 68000 },
    { id: 5, name: 'Emma Davis', department: 'HR', email: 'emma@company.com', salary: 65000 },
    { id: 6, name: 'Frank Miller', department: 'Engineering', email: 'frank@company.com', salary: 88000 },
  ];
}
```

Without a SelectionPlugin, the clipboard operates on a single focused cell — press <kbd>Ctrl+C</kbd> to copy just that cell's value.

## Configuration Options

See [`ClipboardConfig`](./interfaces/clipboardconfig/) for the full list of options and defaults.

Two options are worth calling out:

- **`escapeFormulas`** — on by default. String values starting with `=`, `+`, `-`, `@`, tab or CR are
  prefixed with `'` so a spreadsheet treats them as text instead of evaluating them (CWE-1236).
  Applies to the `text/plain` representation only; numbers, dates and objects are never escaped
  (a formatted `-5` stays numeric), and a `processCell` callback owns its output verbatim.
- **`quoteStrings`** — wraps every string value in double quotes. Off by default, in which case a
  string is quoted only when it contains the delimiter, a newline or a quote.
- **`fillSelection`** — tiles the clipboard source across a larger multi-cell selection
  (see [Fill Selection](#fill-selection)).

### Per-Column Paste Guard/Transform (`column.onPaste`)

Use `column.onPaste` for cell-level paste rules without replacing the whole paste pipeline:

- `false` rejects pastes into that column,
- `(ctx) => false` rejects a specific cell,
- `(ctx) => ({ value })` transforms the incoming value,
- omitted / `true` accepts as-is.

```ts
columns: [
  { field: 'id', onPaste: false },
  {
    field: 'price',
    onPaste: ({ value, sourceField }) => {
      // Reject cross-column pastes into this field.
      if (sourceField && sourceField !== 'price') return false;
      const n = Number(String(value).replace(/[^0-9.]/g, ''));
      return Number.isFinite(n) ? { value: n } : false;
    },
  },
];
```

Cells rejected by `onPaste` emit one `paste-rejected` event per paste with a list of rejected cells.

:::note
If you provide a custom `pasteHandler`, you should still call `resolveColumnPaste()` and `emitPasteRejected()` from `@toolbox-web/grid/plugins/clipboard` so `onPaste` and `paste-rejected` continue to work.
:::

## Copy Behavior

### Multi-Range Selections

With `selection: 'range'`, holding <kbd>Ctrl</kbd>/<kbd>⌘</kbd> while dragging adds disjoint ranges to the selection. Copying such a selection produces the **union bounding box** of all ranges, exactly as Excel does:

- cells inside at least one range carry their displayed value,
- cells inside the bounding box but outside every range are emitted as blanks.

Keeping the block rectangular means the paste lines up correctly in an external target such as Excel or a text editor.

Explicit bounds bypass this masking — `copy({ columns })` and `copyRows(indices)` always emit every cell of the requested columns/rows.

## Paste Behavior

The clipboard plugin respects both selection bounds and the `editable` column property when pasting:

### Editable Columns

**Paste only works on columns marked `editable: true`.** Non-editable columns are skipped during paste, preserving column alignment:

```ts
columns: [
  { field: 'id', header: 'ID' },                    // NOT editable - paste skipped
  { field: 'name', header: 'Name', editable: true }, // Paste allowed
  { field: 'email', header: 'Email', editable: true }, // Paste allowed
]
```

If you paste `"1\t2\t3"` into this grid, only the `name` and `email` columns receive values (`2` and `3`). The `id` column remains unchanged.

:::note
Paste editability is resolved by the **EditingPlugin** (via its `getCellEditableResolver` query), which also honors row-conditional `editable: (row) => boolean`. Load the EditingPlugin for paste to write into cells — marking a column `editable: true` gates paste without you having to trigger interactive editing, but with **no EditingPlugin loaded every cell is treated as non-editable and paste is a no-op.**
:::

### Dirty Tracking, Undo & Validation

When your grid has a `getRowId` and the **EditingPlugin** loaded, paste routes each cell change through the same commit pipeline as an interactive edit. That means a paste automatically:

- **marks rows dirty** (surfaces in `isDirty()` / `getDirtyRows()` when `dirtyTracking: true`),
- **is undoable** with the **UndoRedoPlugin** (<kbd>Ctrl+Z</kbd> reverts the whole paste),
- **emits `cell-commit`** per cell — a handler that calls `preventDefault()` **vetoes** that value, so you can coerce or reject pasted input,
- **triggers cascades** and other editing side effects.

```ts
grid.addEventListener('cell-commit', (e) => {
  // Reject a pasted value that fails validation.
  if (e.detail.field === 'age' && Number.isNaN(Number(e.detail.value))) {
    e.preventDefault();
  }
});
```

:::note
Rows **without** a resolvable `getRowId` (and freshly-created empty rows when a paste grows the grid) fall back to a direct write that bypasses the pipeline. Configure `getRowId` to make paste fully dirty-tracked and undoable.
:::

### Copied Headers

When `includeHeaders: true`, copied text carries a header row so it round-trips cleanly to spreadsheets and text editors. Pasting **back into cells** drops that header row automatically — only values land in cells, never the header label. The header is stripped when the pasted first row consists entirely of this grid's column labels, so it is removed even for a **cross-column** paste (copy one column, paste into another), while external pastes without a matching header row are left intact.

### Structured Values (Same-Grid & Cross-Window Paste)

Copy is **WYSIWYG for external targets** — `processCell` (or the column `format`) turns each cell into text, so pasting into a spreadsheet or editor looks right. At the same time the grid writes a **structured payload**: it keeps the raw cell values in memory _and_ embeds them in a `text/html` representation on the clipboard.

When you paste **back into a grid** — the same grid, **another grid**, or **another browser window/tab** — those raw values are restored, so an **object-valued cell** (e.g. `{ id, name }`, arrays) round-trips **losslessly** instead of degrading to its display text. The same-grid path uses the in-memory snapshot (preserving exact value types such as `Date`); cross-grid / cross-window uses the `text/html` payload. Each pasted cell is cloned, so tiling one source across many cells (fill-selection) never shares a reference, and array→cells pastes (equal or unequal counts, expansion, and clipping) behave exactly like a text paste.

For an **external** paste into an object column (e.g. from Excel), only text is available; the grid can't reconstruct an arbitrary object shape from a string, so your app decides how to handle it (e.g. reject via validation, or map the text to a lookup).

### Selection Bounds

| Selection Type  | Paste Behavior                                                                 |
| --------------- | ------------------------------------------------------------------------------ |
| Single cell     | Paste expands freely from that cell, adding rows if needed                     |
| Range selection | Paste is clipped to fit within the selected range                              |
| Row selection   | Paste is clipped to the selected rows (all columns within those rows)          |
| No active range | Paste starts at the selection anchor/focused cell when available; otherwise row 0, column 0 |

**Example:** If you have a 2×2 range selected and paste 3×3 data, only the 2×2 portion that fits will be applied.

With a combined selection mode such as `mode: ['row', 'column']` or `['range', 'column']`, copy and paste follow the axis that currently holds the selection, so the row and range rules above apply unchanged.

### Fill Selection

With `fillSelection: true`, pasting a source that is **smaller** than a bounded multi-cell selection tiles (repeats) the source to fill the whole selection. This is handy for applying one value across many cells without copying it repeatedly.

```ts
new ClipboardPlugin({ fillSelection: true });
```

| Copied source | Selection | Result                                  |
| ------------- | --------- | --------------------------------------- |
| 1 cell        | 4 cells   | value repeated in all 4 cells           |
| `v1, v2`      | 4 cells   | `v1, v2, v1, v2`                        |
| 2×2 block     | 4×4 range | the block tiled four times              |

Fill only applies to a bounded (multi-cell) selection and never grows the grid. Non-editable columns are still skipped. When `fillSelection` is `false` (the default), paste writes only the source extent.

## Keyboard Shortcuts

| Shortcut           | Action                    |
| ------------------ | ------------------------- |
| `Ctrl+C` / `Cmd+C` | Copy selected cells       |
| `Ctrl+V` / `Cmd+V` | Paste into selected cells |

## Programmatic API

The ClipboardPlugin exposes methods for programmatic copy operations with fine-grained control over which columns and rows to include. This is ideal for workflows where users select rows in the grid, then choose columns via a dialog before copying.

### Basic Copy

```ts
const plugin = grid.getPluginByName('clipboard');

// Copy current selection to clipboard
await plugin.copy();

// Copy with headers included
await plugin.copy({ includeHeaders: true });
```

### Copy with Column/Row Control (`CopyOptions`)

The `copy()` method accepts `CopyOptions` to precisely control what gets copied—independently of the current selection:

```ts
const plugin = grid.getPluginByName('clipboard');

// Copy specific columns from specific rows
await plugin.copy({
  rowIndices: [0, 3, 7],         // Non-contiguous rows supported
  columns: ['name', 'email'],    // Only these columns
  includeHeaders: true,
});

// Copy specific rows with all visible columns
await plugin.copyRows([0, 5]);

// Copy specific rows with column filter
await plugin.copyRows([0, 5], { columns: ['name', 'department'] });
```

### Preview Without Copying

Use `getSelectionAsText()` to get the formatted text without writing to the clipboard—useful for preview dialogs:

```ts
const text = plugin.getSelectionAsText({
  columns: ['name', 'email', 'department'],
  includeHeaders: true,
});
// "Name\tEmail\tDepartment\nAlice\talice@example.com\tEngineering\n..."
```

### `CopyOptions` Reference

See [`CopyOptions`](./interfaces/copyoptions/) for every per-call option. `columns` and
`rowIndices` scope what is copied (row indices need not be contiguous); the remaining options
mirror [`ClipboardConfig`](./interfaces/clipboardconfig/) and fall back to its values when omitted.

### Paste

```ts
// Paste from clipboard
await plugin.paste();

// Get last copied info
const lastCopied = plugin.getLastCopied();
// { text: '...', timestamp: 1234567890 }
```

## Excel Compatibility

The default tab delimiter ensures copied data pastes correctly into Excel:

```ts
features: {
  clipboard: {
    delimiter: '\t', // Tab for Excel
    includeHeaders: true, // Include column names
  },
},
```

## Events

| Event   | Detail                                     | Description                   |
| ------- | ------------------------------------------ | ----------------------------- |
| `copy`  | `{ text, rowCount, columnCount }`          | Fired after cells are copied  |
| `paste` | `{ rows, text, target, fields, rawRows?, sourceFields?, fillSelection? }` | Fired before data is applied (default handler or custom `pasteHandler` can consume it) |
| `paste-rejected` | `{ rejected: PasteRejectedCell[] }` | Fired when one or more cells are rejected by column `onPaste` |

## Styling

The clipboard plugin doesn't add visible UI elements. Selection styling is handled by the SelectionPlugin.

## See Also

- [Selection](https://toolboxjs.com/grid/plugins/selection.md) — Cell and row selection
- [Editing](https://toolboxjs.com/grid/plugins/editing.md) — Inline cell editing
- [Export](https://toolboxjs.com/grid/plugins/export.md) — Export to CSV/JSON
