Data grid / foldkit-data-grid

The clipboard

Copy and paste speak the tab-separated text spreadsheets do. Clipboard is pure: it writes and reads that text and lays a paste onto the grid; the view wires it to the browser's copy, cut and paste events. A copy also carries the same cells as an HTML table, for a target that reads text/html.

ts
import { Clipboard } from 'foldkit-data-grid'

Clipboard.toTsv([['SKU', 'Price'], ['A-1', '9']]) // 'SKU\tPrice\nA-1\t9'
Clipboard.parseTsv('a\t"b\tc"\r\n') // [['a', 'b\tc']]
Clipboard.toHtml([['a<b', 'x\ny']]) // '<table><tbody><tr><td>a&lt;b</td><td>x<br>y</td></tr></tbody></table>'
  • Clipboard.toHtml(rows) is the same matrix as a <table>. Every cell is escaped (&, <, >, ") and a line break becomes <br>, so nothing a cell holds is markup. The view derives it from the TSV it wrote, so the two cannot disagree. Paste still reads text/plain only: HTML pasted from elsewhere is not parsed.
  • Clipboard.copy(projection, box, textOf) is a box's cells as TSV, rows in the projection's order and columns in display order.
  • Clipboard.pasteAt(projection, anchor, matrix, { editable, from }) is where pasted text lands: laid from the anchor, dropping cells past the edges, on columns that do not edit, or on rows not loaded. Each cell keeps from(address), the text it showed, as an edit would begin from.
  • Fill carries a range on, as a spreadsheet's fill does, and is pure. Fill.plan(projection, { source, to }) is the source's box and the box beyond it toward to, along one axis (toward: down, up, right or left; rows win a diagonal). Fill.cells(projection, plan, { editable, from }) is the cells it writes, each lane of the source carried on by Fill.series(texts, count, backward): two or more numbers a constant step apart continue their step, keeping the finest decimals; two or more YYYY-MM-DD days continue by months when they share a day of the month a constant number of months apart (a 31st clamps to a shorter month and comes back: Jan 31, Feb 28, Mar 31), else by days when a constant number of days apart, and when both fit months win; anything else repeats in order. The inference reads text, not the column, so a text column that happens to hold ISO days steps where it once repeated. A column's own series is edit: { fill }, below. Like a paste, it drops columns that do not edit and rows not loaded, and keeps each cell's from.
  • Pasted({ cells }) hands those cells to the grid, which drops each one its text leaves unchanged (as an edit judges it), checks the rest against their columns' schema, and reports one Out.Pasted({ accepted, refused }): one change for the application to apply, and say what it refused. A paste or cut that changes no cell reports nothing. A paste while a cell is edited is the field's.
  • Copied({ rows, columns }) is what the view's copy reports once it has put the text on the clipboard: the size of the box copied, so a status line can say "Copied 12 cells". It carries no text and changes nothing. A cut reports the cells it clears as a Pasted and no Copied; a copy while a cell is edited or a filter form is open is the field's and reports nothing.
  • UndoRequested and RedoRequested (Ctrl or Meta with Z, with Shift or Ctrl+Y to redo) report Out.UndoRequested and Out.RedoRequested. They carry nothing: the grid keeps no history, since what an edit meant and how to take it back are the application's, read from its Model when the request arrives. While a cell is edited they are the field's.
  • A fill comes from Ctrl or Meta with D (down) or R (right) over a range, FillRequested, or from dragging the range's fill handle (FillStarted, FillDragged, FillEnded, the drag held in the Model's filling), and reports Out.Filled({ source, to }): the range, and the cell it was carried to. The grid holds no rows, so the application works out what it writes when it arrives, Grid.fill(rows, model.grid, request), with the rows it draws: { accepted, refused }, judged as a paste's cells are (Fill.plan and Fill.cells, above). A fill from a lone cell, or by key from a range one row tall, carries on the cell above (or before) it.
  • A column's own series, edit: { fill: lane => Option<ReadonlyArray<string>> }, replaces Fill.series for a fill down or up in that column. It is called once per lane with { source, count, toward }: the source's drafts top to bottom, how many cells to write, and the direction. Return the texts, the first next to the source, or Option.none() for the default. Of a longer answer the first count are taken; for each cell past a shorter one the fill reports a refusal in refused (empty text, the error "The column’s series gave no text here") rather than padding or repeating. A fill right or left crosses columns and keeps Fill.series. The texts are judged by the column's schema like any fill.
ts
import { Option } from 'effect'
import { Columns } from 'foldkit-data-grid'

const weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']

interface Shift {
  readonly day: string
}

Columns.define<Shift>()({
  day: {
    header: 'Day',
    value: shift => shift.day,
    edit: {
      fill: ({ source, count, toward }) => {
        const last = weekdays.indexOf(toward === 'down' ? source[source.length - 1]! : source[0]!)
        return last === -1
          ? Option.none()
          : Option.some(
              Array.from({ length: count }, (_, index) => {
                const step = toward === 'down' ? index + 1 : -(index + 1)
                return weekdays[(((last + step) % 5) + 5) % 5]!
              }),
            )
      },
    },
  },
})