/
data-grid-types.ts
668 lines (571 loc) 路 19.8 KB
/
data-grid-types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
import type { Theme } from "../../common/styles.js";
import { assertNever, proveType } from "../../common/support.js";
import has from "lodash/has.js";
import type React from "react";
import type { CSSProperties } from "react";
import type { SpriteManager } from "./data-grid-sprites.js";
import type { OverlayImageEditorProps } from "../data-grid-overlay-editor/private/image-overlay-editor.js";
import type { ImageWindowLoader } from "./image-window-loader-interface.js";
import type { BaseGridMouseEventArgs } from "./event-args.js";
// Thoughts:
// rows/columns are called out as selected, but when selected they must also be added
// to the range. Handling delete events may have different desired outcomes depending on
// how the range came to be selected. The rows/columns properties retain this essential
// information.
/** @category Selection */
export interface GridSelection {
readonly current?: {
readonly cell: Item;
readonly range: Readonly<Rectangle>;
readonly rangeStack: readonly Readonly<Rectangle>[]; // lowest to highest, does not include range
};
readonly columns: CompactSelection;
readonly rows: CompactSelection;
}
/** @category Types */
export type ImageEditorType = React.ComponentType<OverlayImageEditorProps>;
/** @category Types */
export const BooleanEmpty = null;
/** @category Types */
export const BooleanIndeterminate = undefined;
/** @category Types */
export type BooleanEmpty = null;
/** @category Types */
export type BooleanIndeterminate = undefined;
/** @category Types */
export type DrawHeaderCallback = (
args: {
ctx: CanvasRenderingContext2D;
column: GridColumn;
columnIndex: number;
theme: Theme;
rect: Rectangle;
hoverAmount: number;
isSelected: boolean;
isHovered: boolean;
hasSelectedCell: boolean;
spriteManager: SpriteManager;
menuBounds: Rectangle;
},
drawContent: () => void
) => void;
/** @category Types */
export type DrawCellCallback = (
args: {
ctx: CanvasRenderingContext2D;
cell: GridCell;
theme: Theme;
rect: Rectangle;
col: number;
row: number;
hoverAmount: number;
hoverX: number | undefined;
hoverY: number | undefined;
highlighted: boolean;
imageLoader: ImageWindowLoader;
},
drawContent: () => void
) => void;
/** @category Cells */
export enum GridCellKind {
Uri = "uri",
Text = "text",
Image = "image",
RowID = "row-id",
Number = "number",
Bubble = "bubble",
Boolean = "boolean",
Loading = "loading",
Markdown = "markdown",
Drilldown = "drilldown",
Protected = "protected",
Custom = "custom",
}
/** @category Columns */
export enum GridColumnIcon {
HeaderRowID = "headerRowID",
HeaderCode = "headerCode",
HeaderNumber = "headerNumber",
HeaderString = "headerString",
HeaderBoolean = "headerBoolean",
HeaderAudioUri = "headerAudioUri",
HeaderVideoUri = "headerVideoUri",
HeaderEmoji = "headerEmoji",
HeaderImage = "headerImage",
HeaderUri = "headerUri",
HeaderPhone = "headerPhone",
HeaderMarkdown = "headerMarkdown",
HeaderDate = "headerDate",
HeaderTime = "headerTime",
HeaderEmail = "headerEmail",
HeaderReference = "headerReference",
HeaderIfThenElse = "headerIfThenElse",
HeaderSingleValue = "headerSingleValue",
HeaderLookup = "headerLookup",
HeaderTextTemplate = "headerTextTemplate",
HeaderMath = "headerMath",
HeaderRollup = "headerRollup",
HeaderJoinStrings = "headerJoinStrings",
HeaderSplitString = "headerSplitString",
HeaderGeoDistance = "headerGeoDistance",
HeaderArray = "headerArray",
RowOwnerOverlay = "rowOwnerOverlay",
ProtectedColumnOverlay = "protectedColumnOverlay",
}
/** @category Columns */
export enum GridColumnMenuIcon {
Triangle = "triangle",
Dots = "dots",
}
/** @category Types */
export type CellArray = readonly (readonly GridCell[])[];
/**
* This type is used to specify the coordinates of
* a cell or header within the dataset: positive row
* numbers identify cells.
*
* - `-1`: Header
* - `-2`: Group header
* - `0 and higher`: Row index
*
* @category Types
*/
export type Item = readonly [col: number, row: number];
export interface BaseGridColumn {
readonly title: string;
readonly group?: string;
readonly icon?: GridColumnIcon | string;
readonly overlayIcon?: GridColumnIcon | string;
readonly menuIcon?: GridColumnMenuIcon | string;
readonly indicatorIcon?: GridColumnIcon | string;
readonly hasMenu?: boolean;
readonly grow?: number;
readonly style?: "normal" | "highlight";
readonly themeOverride?: Partial<Theme>;
readonly trailingRowOptions?: {
readonly hint?: string;
readonly addIcon?: string;
readonly targetColumn?: number | GridColumn;
readonly themeOverride?: Partial<Theme>;
readonly disabled?: boolean;
};
}
/** @category Columns */
export function isSizedGridColumn(c: GridColumn): c is SizedGridColumn {
return "width" in c && typeof c.width === "number";
}
/** @category Columns */
export interface SizedGridColumn extends BaseGridColumn {
readonly width: number;
readonly id?: string;
}
/** @category Columns */
export interface AutoGridColumn extends BaseGridColumn {
readonly id: string;
}
/** @category Types */
export async function resolveCellsThunk(thunk: GetCellsThunk | CellArray): Promise<CellArray> {
if (typeof thunk === "object") return thunk;
return await thunk();
}
/** @category Types */
export type GetCellsThunk = () => Promise<CellArray>;
/** @category Columns */
export type GridColumn = SizedGridColumn | AutoGridColumn;
export type InnerColumnExtension = {
growOffset?: number;
rowMarker?: "square" | "circle";
rowMarkerChecked?: BooleanIndeterminate | boolean;
};
/** @category Columns */
export type InnerGridColumn = SizedGridColumn & InnerColumnExtension;
// export type SizedGridColumn = Omit<GridColumn, "width"> & { readonly width: number };
/** @category Cells */
export type ReadWriteGridCell = TextCell | NumberCell | MarkdownCell | UriCell | CustomCell | BooleanCell;
/** @category Cells */
export type EditableGridCell = TextCell | ImageCell | BooleanCell | MarkdownCell | UriCell | NumberCell | CustomCell;
/** @category Cells */
export type EditableGridCellKind = EditableGridCell["kind"];
// All EditableGridCells are inherently ValidatedGridCells, and this is more specific and thus more useful.
/** @category Cells */
export function isEditableGridCell(cell: GridCell): cell is ValidatedGridCell {
if (
cell.kind === GridCellKind.Loading ||
cell.kind === GridCellKind.Bubble ||
cell.kind === GridCellKind.RowID ||
cell.kind === GridCellKind.Protected ||
cell.kind === GridCellKind.Drilldown
) {
return false;
}
proveType<EditableGridCell>(cell);
return true;
}
/** @category Cells */
export function isTextEditableGridCell(cell: GridCell): cell is ReadWriteGridCell {
if (
cell.kind === GridCellKind.Loading ||
cell.kind === GridCellKind.Bubble ||
cell.kind === GridCellKind.RowID ||
cell.kind === GridCellKind.Protected ||
cell.kind === GridCellKind.Drilldown ||
cell.kind === GridCellKind.Boolean ||
cell.kind === GridCellKind.Image ||
cell.kind === GridCellKind.Custom
) {
return false;
}
proveType<ReadWriteGridCell>(cell);
return true;
}
/** @category Cells */
export function isInnerOnlyCell(cell: InnerGridCell): cell is InnerOnlyGridCell {
return cell.kind === InnerGridCellKind.Marker || cell.kind === InnerGridCellKind.NewRow;
}
/** @category Cells */
export function isReadWriteCell(cell: GridCell): cell is ReadWriteGridCell {
if (!isEditableGridCell(cell) || cell.kind === GridCellKind.Image) return false;
if (
cell.kind === GridCellKind.Text ||
cell.kind === GridCellKind.Number ||
cell.kind === GridCellKind.Markdown ||
cell.kind === GridCellKind.Uri ||
cell.kind === GridCellKind.Custom ||
cell.kind === GridCellKind.Boolean
) {
return cell.readonly !== true;
}
assertNever(cell, "A cell was passed with an invalid kind");
}
/** @category Cells */
export type GridCell =
| EditableGridCell
| BubbleCell
| RowIDCell
| LoadingCell
| ProtectedCell
| DrilldownCell
| CustomCell;
type InnerOnlyGridCell = NewRowCell | MarkerCell;
/** @category Cells */
export type InnerGridCell = GridCell | InnerOnlyGridCell;
/** @category Cells */
export type CellList = readonly Item[];
/** @category Types */
export interface Rectangle {
x: number;
y: number;
width: number;
height: number;
}
export function isRectangleEqual(a: Rectangle | undefined, b: Rectangle | undefined): boolean {
if (a === b) return true;
if (a === undefined || b === undefined) return false;
return a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
}
export type CellActiviationBehavior = "double-click" | "single-click" | "second-click";
/** @category Cells */
export interface BaseGridCell {
readonly allowOverlay: boolean;
readonly lastUpdated?: number;
readonly style?: "normal" | "faded";
readonly themeOverride?: Partial<Theme>;
readonly span?: readonly [start: number, end: number];
readonly contentAlign?: "left" | "right" | "center";
readonly cursor?: CSSProperties["cursor"];
readonly copyData?: string;
readonly activationBehaviorOverride?: CellActiviationBehavior;
}
/** @category Cells */
export interface LoadingCell extends BaseGridCell {
readonly kind: GridCellKind.Loading;
readonly skeletonWidth?: number;
readonly skeletonHeight?: number;
readonly skeletonWidthVariability?: number;
}
/** @category Cells */
export interface ProtectedCell extends BaseGridCell {
readonly kind: GridCellKind.Protected;
}
/** @category Cells */
export interface TextCell extends BaseGridCell {
readonly kind: GridCellKind.Text;
readonly displayData: string;
readonly data: string;
readonly readonly?: boolean;
readonly allowWrapping?: boolean;
readonly hoverEffect?: boolean;
}
/** @category Cells */
export interface NumberCell extends BaseGridCell {
readonly kind: GridCellKind.Number;
readonly displayData: string;
readonly data: number | undefined;
readonly readonly?: boolean;
readonly fixedDecimals?: number;
readonly allowNegative?: boolean;
readonly thousandSeparator?: boolean | string;
readonly decimalSeparator?: string;
}
/** @category Cells */
export interface ImageCell extends BaseGridCell {
readonly kind: GridCellKind.Image;
readonly data: string[];
readonly rounding?: number;
readonly displayData?: string[]; // used for small images for faster scrolling
readonly readonly?: boolean;
}
/** @category Cells */
export interface BubbleCell extends BaseGridCell {
readonly kind: GridCellKind.Bubble;
readonly data: string[];
}
/** @category Renderers */
export type SelectionRange = number | readonly [number, number];
/** @category Renderers */
export type ProvideEditorComponent<T extends InnerGridCell> = React.FunctionComponent<{
readonly onChange: (newValue: T) => void;
readonly onFinishedEditing: (newValue?: T, movement?: readonly [-1 | 0 | 1, -1 | 0 | 1]) => void;
readonly isHighlighted: boolean;
readonly value: T;
readonly initialValue?: string;
readonly validatedSelection?: SelectionRange;
readonly imageEditorOverride?: ImageEditorType;
readonly markdownDivCreateNode?: (content: string) => DocumentFragment;
readonly target: Rectangle;
readonly forceEditMode: boolean;
readonly isValid?: boolean;
readonly theme: Theme;
}>;
type ObjectEditorCallbackResult<T extends InnerGridCell> = {
editor: ProvideEditorComponent<T>;
deletedValue?: (toDelete: T) => T;
styleOverride?: CSSProperties;
disablePadding?: boolean;
disableStyling?: boolean;
};
/** @category Renderers */
export type ProvideEditorCallbackResult<T extends InnerGridCell> =
| (ProvideEditorComponent<T> & {
disablePadding?: boolean;
disableStyling?: boolean;
})
| ObjectEditorCallbackResult<T>
| undefined;
/** @category Renderers */
export function isObjectEditorCallbackResult<T extends InnerGridCell>(
obj: ProvideEditorCallbackResult<T>
): obj is ObjectEditorCallbackResult<T> {
return has(obj, "editor");
}
/** @category Renderers */
export type ProvideEditorCallback<T extends InnerGridCell> = (cell: T) => ProvideEditorCallbackResult<T>;
/** @category Cells */
export type ValidatedGridCell = EditableGridCell & {
selectionRange?: SelectionRange;
};
/** @category Cells */
export interface CustomCell<T extends {} = {}> extends BaseGridCell {
readonly kind: GridCellKind.Custom;
readonly data: T;
readonly copyData: string;
readonly readonly?: boolean;
}
/** @category Cells */
export interface DrilldownCellData {
readonly text: string;
readonly img?: string;
}
/** @category Cells */
export interface DrilldownCell extends BaseGridCell {
readonly kind: GridCellKind.Drilldown;
readonly data: readonly DrilldownCellData[];
}
/** @category Cells */
export interface BooleanCell extends BaseGridCell {
readonly kind: GridCellKind.Boolean;
readonly data: boolean | BooleanEmpty | BooleanIndeterminate;
readonly readonly?: boolean;
readonly allowOverlay: false;
readonly maxSize?: number;
readonly hoverEffectIntensity?: number;
}
// Can be written more concisely, not easier to read if more concise.
/** @category Cells */
export function booleanCellIsEditable(cell: BooleanCell): boolean {
return !(cell.readonly ?? false);
}
/** @category Cells */
export interface RowIDCell extends BaseGridCell {
readonly kind: GridCellKind.RowID;
readonly data: string;
readonly readonly?: boolean;
}
/** @category Cells */
export interface MarkdownCell extends BaseGridCell {
readonly kind: GridCellKind.Markdown;
readonly data: string;
readonly readonly?: boolean;
}
/** @category Cells */
export interface UriCell extends BaseGridCell {
readonly kind: GridCellKind.Uri;
readonly data: string;
readonly displayData?: string;
readonly readonly?: boolean;
readonly onClickUri?: (args: BaseGridMouseEventArgs & { readonly preventDefault: () => void }) => void;
readonly hoverEffect?: boolean;
}
/** @category Cells */
export enum InnerGridCellKind {
NewRow = "new-row",
Marker = "marker",
}
export type EditListItem = { location: Item; value: EditableGridCell };
/** @category Cells */
export interface NewRowCell extends BaseGridCell {
readonly kind: InnerGridCellKind.NewRow;
readonly hint: string;
readonly allowOverlay: false;
readonly icon?: string;
}
/** @category Cells */
export interface MarkerCell extends BaseGridCell {
readonly kind: InnerGridCellKind.Marker;
readonly allowOverlay: false;
readonly row: number;
readonly drawHandle: boolean;
readonly checked: boolean;
readonly checkboxStyle: "square" | "circle";
readonly markerKind: "checkbox" | "number" | "both" | "checkbox-visible";
}
/** @category Selection */
export type Slice = [start: number, end: number];
/** @category Selection */
export type CompactSelectionRanges = readonly Slice[];
export type FillHandleDirection = "horizontal" | "vertical" | "orthogonal" | "any";
function mergeRanges(input: CompactSelectionRanges) {
if (input.length === 0) {
return [];
}
const ranges = [...input];
const stack: [number, number][] = [];
ranges.sort(function (a, b) {
return a[0] - b[0];
});
stack.push([...ranges[0]]);
for (const range of ranges.slice(1)) {
const top = stack[stack.length - 1];
if (top[1] < range[0]) {
stack.push([...range]);
} else if (top[1] < range[1]) {
top[1] = range[1];
}
}
return stack;
}
let emptyCompactSelection: CompactSelection | undefined;
/** @category Selection */
export class CompactSelection {
private constructor(private readonly items: CompactSelectionRanges) {}
static empty = (): CompactSelection => {
return emptyCompactSelection ?? (emptyCompactSelection = new CompactSelection([]));
};
static fromSingleSelection = (selection: number | Slice) => {
return CompactSelection.empty().add(selection);
};
public offset(amount: number): CompactSelection {
if (amount === 0) return this;
const newItems = this.items.map(x => [x[0] + amount, x[1] + amount] as Slice);
return new CompactSelection(newItems);
}
public add(selection: number | Slice): CompactSelection {
const slice: Slice = typeof selection === "number" ? [selection, selection + 1] : selection;
const newItems = mergeRanges([...this.items, slice]);
return new CompactSelection(newItems);
}
public remove(selection: number | Slice): CompactSelection {
const items = [...this.items];
const selMin = typeof selection === "number" ? selection : selection[0];
const selMax = typeof selection === "number" ? selection + 1 : selection[1];
for (const [i, slice] of items.entries()) {
const [start, end] = slice;
// Remove part of slice that intersects removed selection.
if (start <= selMax && selMin <= end) {
const toAdd: Slice[] = [];
if (start < selMin) {
toAdd.push([start, selMin]);
}
if (selMax < end) {
toAdd.push([selMax, end]);
}
items.splice(i, 1, ...toAdd);
}
}
return new CompactSelection(items);
}
public first(): number | undefined {
if (this.items.length === 0) return undefined;
return this.items[0][0];
}
public last(): number | undefined {
if (this.items.length === 0) return undefined;
return this.items.slice(-1)[0][1] - 1;
}
public hasIndex(index: number): boolean {
for (let i = 0; i < this.items.length; i++) {
const [start, end] = this.items[i];
if (index >= start && index < end) return true;
}
return false;
}
public hasAll(index: Slice): boolean {
for (let x = index[0]; x < index[1]; x++) {
if (!this.hasIndex(x)) return false;
}
return true;
}
public some(predicate: (index: number) => boolean): boolean {
for (const i of this) {
if (predicate(i)) return true;
}
return false;
}
public equals(other: CompactSelection): boolean {
if (other === this) return true;
if (other.items.length !== this.items.length) return false;
for (let i = 0; i < this.items.length; i++) {
const left = other.items[i];
const right = this.items[i];
if (left[0] !== right[0] || left[1] !== right[1]) return false;
}
return true;
}
// Really old JS wont have access to the iterator and babel will stop people using it
// when trying to support browsers so old we don't support them anyway. What goes on
// between an engineer and their bundler in the privacy of their CI server is none of
// my business anyway.
public toArray(): number[] {
const result: number[] = [];
for (const [start, end] of this.items) {
for (let x = start; x < end; x++) {
result.push(x);
}
}
return result;
}
get length(): number {
let len = 0;
for (const [start, end] of this.items) {
len += end - start;
}
return len;
}
*[Symbol.iterator]() {
for (const [start, end] of this.items) {
for (let x = start; x < end; x++) {
yield x;
}
}
}
}