@koeroesi86/canvas-grid 26.11.7 → 26.11.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,7 +9,7 @@ import HeaderRenderer from './header';
9
9
  import CellValueRenderer from './cell-value';
10
10
  import type { TextStyle } from './text';
11
11
  import type { LayerOptions } from './utils/layer';
12
- import type { CellContext, CellRenderer, CellState, Key, RendererRef, RowId, RowInput, SortDirection, SortKey } from './types';
12
+ import type { BuiltInCellEditor, CellContext, CellEditable, CellEditContext, CellEditEndDetail, CellEditor, CellEditorContext, CellEditorInstance, CellEditStartDetail, CellFocusDetail, CellPosition, CellRange, CellRenderer, CellState, CellValueChange, CellValueChangedDetail, ContextMenuDetail, ContextMenuItem, CustomCellEditor, EditOutOfView, GridSelection, Key, RendererRef, RowId, RowInput, SelectionChangeDetail, SelectionCopyDetail, SelectionMode, SelectOption, SortDirection, SortKey } from './types';
13
13
  import type { RowIdGetter } from './utils/data-store';
14
14
  /** What the grid remembers about a cell between frames. */
15
15
  interface CellEntry {
@@ -24,6 +24,12 @@ export interface ColumnHeader {
24
24
  id: Key;
25
25
  displayName?: string;
26
26
  width: number;
27
+ /** Narrowest the column can get when resized, in px. Defaults to 20. */
28
+ minWidth?: number;
29
+ /** Widest the column can get when resized, in px. Defaults to unlimited. */
30
+ maxWidth?: number;
31
+ /** Whether the user can drag this column's right border. Defaults to `true`; `setColumnWidth` ignores it. */
32
+ resizable?: boolean;
27
33
  /** Renderers drawing a cell of this column, in order. Defaults to the grid's `cellRenderers`. */
28
34
  renderers?: RendererRef[];
29
35
  valueFormatter: (value: unknown) => string;
@@ -33,6 +39,28 @@ export interface ColumnHeader {
33
39
  headerFont?: string;
34
40
  /** Renderer drawing the header of this column. */
35
41
  headerRenderer: RendererRef;
42
+ /** Whether the user can edit this column's cells: `true`, or a function asked per cell. Defaults to `false`. `startEditing` follows it too. */
43
+ editable?: CellEditable;
44
+ /** Editor over the cell: `'text'` (default), `'number'`, `'checkbox'`, `{ type: 'select', options }`, or a function building a custom one. */
45
+ cellEditor?: CellEditor;
46
+ /** Turns the text of the text and number editors into the value to store; throw to mark the text invalid. Defaults to the text, or a number for `'number'`. */
47
+ valueParser?: (text: string, change: CellValueChange) => unknown;
48
+ /** Says whether a parsed value may be stored: `true`, or `false` / a message for an invalid-state editor that cannot be committed. */
49
+ validator?: (value: unknown, change: CellValueChange) => boolean | string;
50
+ /** Applies a committed edit instead of the grid storing it. Return `false` to reject the edit and keep the editor open. */
51
+ valueSetter?: (change: CellValueChange) => boolean | void;
52
+ }
53
+ /** What `getColumnState` returns and `setColumnState` takes: enough to restore the column widths later. */
54
+ export interface ColumnState {
55
+ id: Key;
56
+ width: number;
57
+ }
58
+ /** What triggered a resize: the user dragging or double-clicking a border, `autoSizeColumns`, or code calling `setColumnWidth` / `setColumnState`. */
59
+ export type ColumnResizeSource = 'drag' | 'auto' | 'api';
60
+ export interface ColumnResizeDetail {
61
+ columnId: Key;
62
+ width: number;
63
+ source: ColumnResizeSource;
36
64
  }
37
65
  export type ColumnDefinition = Key | (Partial<ColumnHeader> & {
38
66
  id: Key;
@@ -48,19 +76,41 @@ interface AgApi {
48
76
  setColumnDefs: (columns: ColumnDefinition[]) => void;
49
77
  getRowNodeId: RowIdGetter;
50
78
  }
51
- export type { CellContext, CellRenderer, CellState, LayerOptions, RendererRef, SortDirection, SortKey };
79
+ export type { BuiltInCellEditor, CellContext, CellEditable, CellEditContext, CellEditEndDetail, CellEditor, CellEditorContext, CellEditorInstance, CellEditStartDetail, CellFocusDetail, CellPosition, CellRange, CellRenderer, CellState, CellValueChange, CellValueChangedDetail, ContextMenuDetail, ContextMenuItem, CustomCellEditor, EditOutOfView, GridSelection, LayerOptions, RendererRef, SelectionChangeDetail, SelectionCopyDetail, SelectionMode, SelectOption, SortDirection, SortKey, };
80
+ export interface RowResizeDetail {
81
+ rowId: RowId;
82
+ height: number;
83
+ source: RowResizeSource;
84
+ }
85
+ /** What triggered a row resize: the user dragging a row-number border, or code calling `setRowHeightById`. */
86
+ export type RowResizeSource = 'drag' | 'api';
52
87
  declare global {
53
88
  interface HTMLElementTagNameMap {
54
89
  'canvas-grid': CanvasGridComponent;
55
90
  }
91
+ interface HTMLElementEventMap {
92
+ columnresizing: CustomEvent<ColumnResizeDetail>;
93
+ columnresized: CustomEvent<ColumnResizeDetail>;
94
+ rowresizing: CustomEvent<RowResizeDetail>;
95
+ rowresized: CustomEvent<RowResizeDetail>;
96
+ selectionchange: CustomEvent<SelectionChangeDetail>;
97
+ cellfocus: CustomEvent<CellFocusDetail>;
98
+ selectioncopy: CustomEvent<SelectionCopyDetail>;
99
+ gridcontextmenu: CustomEvent<ContextMenuDetail>;
100
+ celleditstart: CustomEvent<CellEditStartDetail>;
101
+ celleditend: CustomEvent<CellEditEndDetail>;
102
+ cellvaluechanged: CustomEvent<CellValueChangedDetail>;
103
+ }
56
104
  }
57
105
  declare class CanvasGridComponent extends HTMLElement {
58
106
  /** Maximum redraw rate. */
59
107
  fps: number;
60
- _font: string;
61
- _headerFont: string;
62
- headerHeight: number;
63
- cellHeight: number;
108
+ /** Theme read from the CSS custom properties, `refreshTheme` keeps it current. Explicit options win over it. */
109
+ private theme;
110
+ _font: string | undefined;
111
+ _headerFont: string | undefined;
112
+ _headerHeight: number | undefined;
113
+ _cellHeight: number | undefined;
64
114
  cellPadding: {
65
115
  top: number;
66
116
  right: number;
@@ -71,9 +121,26 @@ declare class CanvasGridComponent extends HTMLElement {
71
121
  showHeaders: boolean;
72
122
  rowNumbersWidth: number;
73
123
  ellipsis: string;
74
- _backgroundColor: string;
75
- _headerBackgroundColor: string;
76
- textColor: string;
124
+ private _backgroundColor;
125
+ private _headerBackgroundColor;
126
+ private _textColor;
127
+ /** Tint of selected cells; set it, then call `forceDraw`. */
128
+ selectionColor: string | undefined;
129
+ /** Colour of the outline of a selection and of the border around the active cell. */
130
+ selectionBorderColor: string | undefined;
131
+ /** Cell background; set it to override `--cg-background`, `undefined` hands it back to the theme. */
132
+ get backgroundColor(): string;
133
+ set backgroundColor(color: string | undefined);
134
+ /** Header background; overrides `--cg-header-background`. An empty string hides the corner above the row numbers. */
135
+ get headerBackgroundColor(): string;
136
+ set headerBackgroundColor(color: string | undefined);
137
+ /** Text colour of cells and headers; overrides `--cg-text-color` and `--cg-header-text-color`. */
138
+ get textColor(): string;
139
+ set textColor(color: string | undefined);
140
+ get headerHeight(): number;
141
+ set headerHeight(height: number);
142
+ get cellHeight(): number;
143
+ set cellHeight(height: number);
77
144
  dataStore: DataStore;
78
145
  _dataCollection: import("./utils/row-store").default;
79
146
  /** Per-cell memory of the rows drawn recently; bounded, since only the rows in view need it. */
@@ -163,6 +230,24 @@ declare class CanvasGridComponent extends HTMLElement {
163
230
  */
164
231
  refreshFonts(): void;
165
232
  private readonly onFontsLoaded;
233
+ private themeQuery;
234
+ private themeObserver;
235
+ private colorCheck;
236
+ /** The `theme` attribute when it names a theme, else the colour scheme the page prefers. */
237
+ private get themeName();
238
+ /**
239
+ * Re-reads the `--cg-*` custom properties and redraws when a value changed. Runs by itself when the colour scheme,
240
+ * the `theme` attribute or the `class`, `style` or `data-theme` attribute of the grid or an ancestor change;
241
+ * call it after changing the tokens any other way, such as a stylesheet rule that is toggled.
242
+ */
243
+ refreshTheme(): void;
244
+ /** Hands the theme, with the explicit options on top, to the renderers and redraws. */
245
+ private applyTheme;
246
+ /** Row heights follow `--cg-row-height` until `cellHeight`, `setRowHeight` or a drag sets them. */
247
+ private syncThemeHeights;
248
+ private readonly onThemeChange;
249
+ private watchTheme;
250
+ private unwatchTheme;
166
251
  cancelRender(): void;
167
252
  /**
168
253
  * Schedules a draw for the next animation frame. Nothing is drawn while the grid is idle:
@@ -184,10 +269,27 @@ declare class CanvasGridComponent extends HTMLElement {
184
269
  draw(forced?: boolean): void;
185
270
  /** The columns overlapping the viewport horizontally, with their x position and values; found once per draw, not per row. */
186
271
  private getVisibleColumns;
272
+ /** Columns in display order, and where each starts; rebuilt when columns or widths change. */
273
+ private columnList;
274
+ private columnOffsets;
275
+ private updateColumnLayout;
276
+ /** The selection must be drawn again on the next frame. */
277
+ private selectionDirty;
278
+ /** The headers and row numbers must be drawn again, because the selection they show changed. */
279
+ private headersDirty;
280
+ /** How much of a column is selected, for its header. */
281
+ private columnSelection;
282
+ /** How much of a row is selected, for its row number. */
283
+ private rowSelection;
284
+ /** Whether the selection layer holds anything, so a grid that never selects never clears it. */
285
+ private selectionDrawn;
286
+ /** Tints each selected range and outlines the active cell: one rectangle per range, however many cells it covers. */
287
+ private renderSelection;
187
288
  renderRowNumber(row: {
188
289
  index: number;
189
290
  height?: number;
190
291
  }, y: number): void;
292
+ private clearCustomLayers;
191
293
  forceDraw(): void;
192
294
  /** Viewport size in CSS pixels as of the last resize; cached because reading `clientWidth` per cell is slow. */
193
295
  viewportWidth: number;
@@ -215,6 +317,197 @@ declare class CanvasGridComponent extends HTMLElement {
215
317
  renderBrick(): void;
216
318
  renderHeaders(): void;
217
319
  getColumnHeader(key: Key): ColumnHeader | undefined;
320
+ /** Redraws everything on the next frame, without waiting for the frame cap; for changes that move cells, like resizing. */
321
+ private requestRelayout;
322
+ private relayoutPending;
323
+ private emitResized;
324
+ /** Sets the widths within each column's limits and refreshes the layout once; returns the columns that actually changed. */
325
+ private applyColumnWidths;
326
+ /** Sets a column's width in px, within its `minWidth` / `maxWidth`, and fires `columnresized` when it changed. Works for columns that are not `resizable`. */
327
+ setColumnWidth(id: Key, width: number): void;
328
+ /** The widths of all columns, in display order; pass it to `setColumnState` to restore them. */
329
+ getColumnState(): ColumnState[];
330
+ /** Restores widths from `getColumnState`; unknown columns are skipped. Fires `columnresized` for each column that changed. */
331
+ setColumnState(state: readonly ColumnState[]): void;
332
+ private measureContext;
333
+ /**
334
+ * Fits columns (all of them by default) to their content: the header and the rows in view, at most
335
+ * `AUTO_SIZE_SAMPLE_ROWS` of them, so it stays fast with any number of rows. Fires `columnresized` for each that changed.
336
+ */
337
+ autoSizeColumns(ids?: readonly Key[]): void;
338
+ /** Smallest row height the user can drag to, or a row-height function can return. */
339
+ private static readonly MIN_ROW_HEIGHT;
340
+ /** Heights set for single rows by dragging or `setRowHeightById`, by row id; they win over `setRowHeight`'s function. */
341
+ private readonly rowHeightOverrides;
342
+ private rowHeightOf;
343
+ private heightOfRow;
344
+ /** Only asked for rows being added, and only when it can differ from `cellHeight`. */
345
+ private readonly newRowHeight;
346
+ private refreshRowHeights;
347
+ private emitRowResized;
348
+ /**
349
+ * Sets the height of every row: a number in px (also the height of rows added later), or a function of the row id
350
+ * returning one, called for existing rows now and for rows as they are added once their values are stored.
351
+ * Replaces the heights of rows resized one by one. Fires no events.
352
+ */
353
+ setRowHeight(height: number | ((id: RowId) => number)): void;
354
+ /** Sets one row's height in px (at least 8) and fires `rowresized`; the height sticks to the row through sorting and updates. */
355
+ setRowHeightById(id: RowId, height: number): void;
356
+ private resizeDrag;
357
+ /** The column border or row border under the pointer, when it can be dragged. */
358
+ private findResizeTarget;
359
+ private findColumnBorder;
360
+ private findRowBorder;
361
+ private moveResizeGuide;
362
+ private readonly resizeGuide;
363
+ private readonly onResizePointerDown;
364
+ private readonly onResizePointerMove;
365
+ private dragColumn;
366
+ private dragRow;
367
+ /** Whether enough time has passed since the last live event; counts this one as sent when it has. */
368
+ private dueForLiveEvent;
369
+ private readonly onResizePointerEnd;
370
+ private readonly onResizeDoubleClick;
371
+ /** Widens the last column to the right edge of the viewport when the columns leave it empty; returns whether there was room. */
372
+ private fillRemainingSpace;
373
+ /** Keeps a touch that starts on a border from scrolling the grid, so the pointer events can drag it instead. */
374
+ private readonly onResizeTouchStart;
375
+ private readonly selectionModel;
376
+ private mode;
377
+ /**
378
+ * What the user can select with the pointer: `'none'` (default), one `'cell'`, `'range'`s of cells with whole rows and columns
379
+ * (like a spreadsheet), or whole `'row'`s or `'column'`s. `getSelection` and `setSelection` work in every mode; switching to `'none'` clears it.
380
+ */
381
+ set selectionMode(mode: SelectionMode);
382
+ /** Whether the grid put `tabindex` on itself, so it is only taken away again when it was not the page's. */
383
+ private ownsTabIndex;
384
+ /** A grid with selection takes keyboard focus; one without it stays out of the tab order, as before. */
385
+ private updateTabIndex;
386
+ get selectionMode(): SelectionMode;
387
+ /** The selected ranges and the active cell, by displayed position; the ranges are copies. */
388
+ getSelection(): GridSelection;
389
+ /**
390
+ * Selects the given ranges, with `active` in focus (the first cell of the last range by default). A range that leaves out `rows` or `columns`
391
+ * covers all of them. Positions beyond the data are cut off when it changes. Fires `selectionchange`, and `cellfocus` when the active cell moved.
392
+ */
393
+ setSelection(ranges: readonly CellRange[], active?: CellPosition): void;
394
+ clearSelection(): void;
395
+ private emitSelectionChange;
396
+ private emitCellFocus;
397
+ /** Applies a change to the selection, then redraws it and fires the events if something differs. */
398
+ private changeSelection;
399
+ /** Cuts the selection to the data after rows or columns changed, and redraws it: ranges that reach the end follow the data. */
400
+ private refitSelection;
401
+ private selectDrag;
402
+ private touchTap;
403
+ /** Where in the grid, in px from its top left, a point of the viewport is. */
404
+ private offsetsOf;
405
+ /** Index of the column at `contentX` px from the left of the first column, or undefined past the last one. */
406
+ private columnIndexAt;
407
+ /** Index of the row at `contentY` px from the top of the first row, or undefined past the last one. */
408
+ private rowIndexAt;
409
+ /** What is under the pointer that a press can select: a cell, a column header, a row number or the corner. Scrollbars and empty space are nothing. */
410
+ private hitAt;
411
+ /** The cell nearest to the pointer, wherever it is: dragging past an edge selects up to that edge. */
412
+ private nearestCell;
413
+ /** The row and column a header selection that was just made starts from: those at the top left of the viewport. */
414
+ private firstVisible;
415
+ /**
416
+ * Applies a press on a cell: plain selects it, shift extends from the active cell, ctrl or cmd adds a range.
417
+ * Returns the cell a drag from here starts at.
418
+ */
419
+ private pressCell;
420
+ /**
421
+ * Applies a press on a column header or row number: it selects the whole column or row, shift extends from the active cell's,
422
+ * ctrl or cmd adds one. Returns the cell a drag from here starts at.
423
+ */
424
+ private pressLine;
425
+ /** Selects everything, keeping the active cell. */
426
+ private selectEverything;
427
+ /** Which modes a press in each part of the grid selects in; a column header does nothing when only rows can be selected, and so on. */
428
+ private static readonly ZONE_MODES;
429
+ /** Applies a press wherever it landed. Returns the drag it starts, or undefined when there is nothing to drag. */
430
+ private press;
431
+ private dragSelectTo;
432
+ private readonly onSelectPointerDown;
433
+ private readonly onSelectPointerMove;
434
+ /** Keeps scrolling, and extending the selection with it, for as long as the pointer is held outside the cells. */
435
+ private readonly scrollWhileDragging;
436
+ private readonly onSelectPointerEnd;
437
+ private endSelectDrag;
438
+ /** Scrolls the least that brings a cell fully into view. */
439
+ private revealCell;
440
+ private get gridSize();
441
+ /** Selects one cell, or its whole row or column in those modes, and brings it into view. */
442
+ private moveTo;
443
+ /** Stretches the newest range to `cell`; it keeps being whole columns or whole rows when it was, and the active cell stays. */
444
+ private extendTo;
445
+ /** Moves the active cell inside a selection of several cells; with one cell selected Enter moves the selection, and Tab is left to the page. */
446
+ private stepActive;
447
+ /** Selects the whole column (ctrl+space) or row (shift+space) of the active cell. */
448
+ private selectLineOfActive;
449
+ /** Collapses the selection to its active cell. */
450
+ private collapseSelection;
451
+ /** Moves or extends the selection for a navigation key; the first press just selects the cell at the top left of the view. */
452
+ private navigate;
453
+ /** Returns whether the key was a selection key, which is then kept from also scrolling the page. */
454
+ private handleSelectionKey;
455
+ private readonly onKeyDown;
456
+ /** The newest range as tab-separated text of the formatted values, as shown; undefined when nothing is selected. */
457
+ private copiedSelection;
458
+ /** The selection as it is about to be copied, once `selectioncopy` let it through; undefined when there is nothing to copy or a listener cancelled. */
459
+ private requestCopy;
460
+ /** Puts the selection on the clipboard when the user copies. `selectioncopy` can change the text or cancel the copy. */
461
+ private readonly onCopy;
462
+ /**
463
+ * Copies the newest selected range to the clipboard, as the copy shortcut does, `selectioncopy` included.
464
+ * Resolves to whether anything was copied. Browsers allow it from a user action only, such as a click.
465
+ */
466
+ copySelection(): Promise<boolean>;
467
+ /** The entries of the context menu. Replace them to change the menu; an empty list keeps the browser's own menu. */
468
+ contextMenuItems: ContextMenuItem[];
469
+ private contextMenu;
470
+ private menuDetail;
471
+ /** Whether a right click on this hit already has what it selects selected; those that do keep their selection. */
472
+ private isSelected;
473
+ /** Where the menu opens for a key press: the corner of the active cell, or of the grid's cells. */
474
+ private menuAnchor;
475
+ /** Opens the context menu on a right click, a long press or the menu key, once `gridcontextmenu` let it. */
476
+ private readonly onContextMenu;
477
+ /** What happens to an edit when its cell scrolls out of view: `'cancel'` (default) drops it, `'commit'` writes it, `'keep'` hides the editor until the cell is back. */
478
+ editOutOfView: EditOutOfView;
479
+ private edit;
480
+ private cellOf;
481
+ private canEdit;
482
+ /**
483
+ * Puts an editor over a cell, committing the edit in progress first. Returns whether one opened: the cell has to exist and be `editable`,
484
+ * and a cancelled `celleditstart` or an edit that cannot be committed stop it. The cell is scrolled into view.
485
+ */
486
+ startEditing(rowId: RowId, columnId: Key): boolean;
487
+ /** Ends the edit in progress: commits it, or drops it with `cancel`. Returns false when there was none, or the value is invalid or rejected, which keeps the editor open. */
488
+ stopEditing(cancel?: boolean): boolean;
489
+ private openEditor;
490
+ private finishEditIfOpen;
491
+ /** The editor's value parsed and validated against the value the cell holds now. */
492
+ private checkEdit;
493
+ private showValidity;
494
+ /** Writes a committed value, through the column's `valueSetter` when it has one. Returns false when that rejected it. */
495
+ private applyEdit;
496
+ private finishEdit;
497
+ private closeEditor;
498
+ /** Keeps the editor on its cell as the grid scrolls, resizes or re-sorts; applies `editOutOfView` once the cell is gone from the view. */
499
+ private positionEditor;
500
+ /** Enter and Tab commit and move the selection on, Escape drops the edit; the keys stay inside the editor, which the grid would otherwise act on. */
501
+ private readonly onEditorKeyDown;
502
+ /** Leaving the editor commits it, or drops it when its value is invalid. Switching windows leaves it be. */
503
+ private readonly onEditorFocusOut;
504
+ /** The cell at a displayed row and column, when both exist. */
505
+ private cellAt;
506
+ private canEditAt;
507
+ private editAt;
508
+ private readonly onEditDoubleClick;
509
+ /** Starts editing the active cell for F2, Enter (with a single cell selected) or a typed character, which begins the text. */
510
+ private handleEditKey;
218
511
  get totalHeight(): number;
219
512
  refreshScrollbar(): void;
220
513
  queueUpdate(row: RowInput): void;