foldkit-data-grid 0.5.0 → 0.7.0

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.
package/dist/grid.js CHANGED
@@ -1,5 +1,5 @@
1
- import { Equal, Option, Result, Schema } from "effect";
2
- import { CellEditor, Columns, editorOf } from "./columns.js";
1
+ import { Equal, Match, Option, Result, Schema } from "effect";
2
+ import { CellEditor, Columns, editorOf, filterKindOf } from "./columns.js";
3
3
  import { defineTaggedUnion } from "foldkit/schema";
4
4
  import { defineMessageUnion } from "foldkit/message";
5
5
  import { modifyFields } from "foldkit/struct";
@@ -7,6 +7,7 @@ import { Bundle } from "foldkit-bundle";
7
7
  import { GridProjection } from "./projection.js";
8
8
  import { ColumnState } from "./columnState.js";
9
9
  import { Fill } from "./fill.js";
10
+ import { ColumnFilter, FilterForm, emptyForm, filterSchemaOf, formOf, readForm, testOf } from "./filter.js";
10
11
  import { GridFocus } from "./focus.js";
11
12
  import { GridSelection, RowSelection } from "./selection.js";
12
13
  import { GridViewport } from "./viewport.js";
@@ -40,6 +41,37 @@ const make = (options) => {
40
41
  const focus = GridFocus.make(options.columns);
41
42
  const columnState = ColumnState.make(options.columns);
42
43
  const Column = focus.Address.fields.column;
44
+ const filterKinds = /* @__PURE__ */ new Map();
45
+ const filterSchemas = /* @__PURE__ */ new Map();
46
+ for (const id of options.columns.ids) {
47
+ const filter = options.columns.byId[id].filter;
48
+ if (filter === void 0) continue;
49
+ const kind = filter === true ? filterKindOf(options.columns.byId[id].edit?.schema) : filter;
50
+ filterKinds.set(id, kind);
51
+ filterSchemas.set(id, filterSchemaOf(kind));
52
+ }
53
+ const filterIds = [...filterKinds.keys()];
54
+ /** One filtering column's filter, or none, as the application stores it. */
55
+ const filterFields = Object.fromEntries(filterIds.map((id) => [id, Schema.OptionFromNullOr(filterSchemas.get(id))]));
56
+ const filterKeys = new Set(filterIds);
57
+ /**
58
+ * Every filtering column's filter as stored: each its own variant, never
59
+ * empty, and no other key.
60
+ */
61
+ const Filters = Schema.Record(Schema.String, Schema.Unknown).check(Schema.makeFilter((stored) => Object.keys(stored).every((key) => filterKeys.has(key)), { expected: "only the filtering columns" })).pipe(Schema.decodeTo(Schema.Struct(filterFields)));
62
+ const changes = filterIds.map((id) => Schema.Struct({
63
+ column: Schema.Literal(id),
64
+ filter: Schema.OptionFromNullOr(filterSchemas.get(id))
65
+ }));
66
+ const FilterChangeSchema = changes.length === 0 ? Schema.Never : Schema.Union(changes);
67
+ /**
68
+ * What a stale frame drew: the application's filter, and the form's
69
+ * generation when that frame was drawn.
70
+ */
71
+ const Stale = Schema.OptionFromNullOr(Schema.Struct({
72
+ drawn: Schema.OptionFromNullOr(ColumnFilter),
73
+ generation: Schema.Number
74
+ }));
43
75
  const CellText = Schema.Struct({
44
76
  row: Schema.String,
45
77
  column: Column,
@@ -101,6 +133,22 @@ const make = (options) => {
101
133
  from: Schema.String,
102
134
  draft: Schema.String,
103
135
  error: Schema.OptionFromNullOr(Schema.String)
136
+ })),
137
+ /**
138
+ * The column whose filter form is open, the form as typed, and the filter
139
+ * last reported (or the application's, when the form opened): a change is
140
+ * reported when the form reads as a valid filter other than this one.
141
+ */
142
+ filtering: Schema.OptionFromNullOr(Schema.Struct({
143
+ column: Column,
144
+ form: FilterForm,
145
+ reported: Schema.OptionFromNullOr(ColumnFilter),
146
+ /**
147
+ * Counts the form's reports and reseeds: a stale frame's change reseeds
148
+ * only while it is the one the frame was drawn at, so a second change
149
+ * from that frame adds to the first.
150
+ */
151
+ generation: Schema.Number
104
152
  }))
105
153
  });
106
154
  const Region = Schema.Literals([
@@ -118,6 +166,15 @@ const make = (options) => {
118
166
  Hide: {},
119
167
  Show: { column: Column }
120
168
  });
169
+ /**
170
+ * What toggled a checkbox: a press on its box, or a key on the cell the
171
+ * frame was drawn focusing, with the focus that frame was drawn from.
172
+ */
173
+ const Toggle = defineTaggedUnion({
174
+ Press: {},
175
+ Key: { drawn: Schema.OptionFromNullOr(focus.Address) }
176
+ });
177
+ const sameFocus = Option.makeEquivalence((a, b) => sameCell(a, b));
121
178
  const Message = defineMessageUnion({
122
179
  /** The pointer or the browser focused a cell. */
123
180
  Focused: { address: focus.Address },
@@ -232,7 +289,25 @@ const make = (options) => {
232
289
  * test resolves them with a Message the runtime sends too.
233
290
  */
234
291
  MountStarted: {},
235
- /** An edit began on an editable cell, with the text it starts from. */
292
+ /**
293
+ * The checkbox in the cell whose DOM id is `cell`, drawn `checked`, was
294
+ * pressed (`Toggle.Press`), or Space or Enter was pressed on the cell a
295
+ * frame drew as the tab stop (`Toggle.Key`, with the focus that frame was
296
+ * drawn from): the column's text for the other value is committed at once
297
+ * and reported as `Out.Edited`, with no draft. The id comes from the page,
298
+ * so a cell that is not a checkbox of this grid, or a toggle while an edit
299
+ * is open, changes nothing; nor does a key whose frame was drawn before
300
+ * focus moved, which would toggle the cell focus left.
301
+ */
302
+ EditToggled: {
303
+ cell: Schema.String,
304
+ checked: Schema.Boolean,
305
+ by: Toggle
306
+ },
307
+ /**
308
+ * An edit began on an editable cell, with the text it starts from. A
309
+ * checkbox's cell opens none: it is toggled (`EditToggled`).
310
+ */
236
311
  EditStarted: {
237
312
  address: focus.Address,
238
313
  draft: Schema.String
@@ -292,7 +367,51 @@ const make = (options) => {
292
367
  /** The pointer, filling, is over the cell whose DOM id is `cell`. */
293
368
  FillDragged: { cell: Schema.String },
294
369
  /** The fill handle was let go, or the drag cancelled (`completed` false). */
295
- FillEnded: { completed: Schema.Boolean }
370
+ FillEnded: { completed: Schema.Boolean },
371
+ /**
372
+ * A column's filter form opened, from its button or Alt+ArrowDown on its
373
+ * header, seeded with the application's filter as the view `drawn` it.
374
+ * Focus goes to the header, so closing returns there. A column that does
375
+ * not filter or is hidden, or a `drawn` its column refuses, opens nothing.
376
+ */
377
+ FilterOpened: {
378
+ column: Column,
379
+ drawn: Schema.OptionFromNullOr(ColumnFilter)
380
+ },
381
+ /**
382
+ * A text field of the open form was typed in. `stale` is set by a frame
383
+ * drawn after the application's filter changed from outside the form:
384
+ * the form is seeded again from it first, unless the form has reported
385
+ * since that frame. A field another kind owns changes nothing.
386
+ */
387
+ FilterTyped: {
388
+ field: Schema.Literals([
389
+ "text",
390
+ "min",
391
+ "max",
392
+ "from",
393
+ "to"
394
+ ]),
395
+ text: Schema.String,
396
+ stale: Stale
397
+ },
398
+ /** A choice's option was ticked or unticked; one the column does not offer changes nothing. */
399
+ FilterOptionToggled: {
400
+ option: Schema.String,
401
+ stale: Stale
402
+ },
403
+ /** A checkbox's filter was set: any value, checked, or not. */
404
+ FilterChecked: {
405
+ state: Schema.Literals([
406
+ "any",
407
+ "true",
408
+ "false"
409
+ ]),
410
+ stale: Stale
411
+ },
412
+ /** The open form was emptied: none is reported if a filter held. */
413
+ FilterCleared: { stale: Stale },
414
+ FilterClosed: {}
296
415
  });
297
416
  /**
298
417
  * What the grid reports to its parent: one cell's committed text, or a
@@ -301,7 +420,7 @@ const make = (options) => {
301
420
  * `RedoRequested` carry nothing, since what to undo is the application's
302
421
  * history, read when the request arrives.
303
422
  */
304
- const Out = defineTaggedUnion({
423
+ const outCases = {
305
424
  Edited: {
306
425
  row: Schema.String,
307
426
  column: Column,
@@ -331,7 +450,16 @@ const make = (options) => {
331
450
  from: Schema.String,
332
451
  to: Schema.String
333
452
  }
453
+ };
454
+ const Out = defineTaggedUnion({
455
+ ...outCases,
456
+ /**
457
+ * A column's filter changed, or is none: the application's to store
458
+ * (`setFilter`) and apply (`keeps`, or a query's input).
459
+ */
460
+ Filtered: { change: FilterChangeSchema }
334
461
  });
462
+ const Unfiltered = defineTaggedUnion(outCases);
335
463
  const focusTo = (model, address) => {
336
464
  const next = focus.bundle.update(model.focus, focus.Message.Focused({ address }), void 0).model;
337
465
  return next === model.focus ? model : modifyFields(model, { focus: () => next });
@@ -384,12 +512,20 @@ const make = (options) => {
384
512
  };
385
513
  const decoders = /* @__PURE__ */ new Map();
386
514
  const editors = /* @__PURE__ */ new Map();
515
+ const toggles = /* @__PURE__ */ new Map();
387
516
  for (const id of options.columns.ids) {
388
517
  const edit = options.columns.byId[id].edit;
389
518
  if (edit === void 0) continue;
390
519
  decoders.set(id, Schema.decodeUnknownResult(edit.schema ?? Schema.String));
391
- editors.set(id, editorOf(edit.schema));
520
+ const editor = editorOf(edit.schema);
521
+ editors.set(id, editor);
522
+ if (edit.schema !== void 0 && CellEditor.guards.Checkbox(editor)) {
523
+ const encode = Schema.encodeUnknownResult(edit.schema);
524
+ toggles.set(id, (value) => Result.getSuccess(encode(value)));
525
+ }
392
526
  }
527
+ /** Whether a column's cells open a draft to edit: they edit, and are no checkbox. */
528
+ const drafts = (column) => options.columns.byId[column].edit !== void 0 && !toggles.has(column);
393
529
  /** How a column's cells are edited: as text, a number, or a choice; none when it does not edit. */
394
530
  const editorFor = (column) => Option.fromUndefinedOr(editors.get(column));
395
531
  /** Why a column refuses a text, or none when it takes it. */
@@ -432,6 +568,7 @@ const make = (options) => {
432
568
  Text: () => Option.none(),
433
569
  Number: () => Option.none(),
434
570
  Date: () => Option.none(),
571
+ Checkbox: () => Option.none(),
435
572
  Choice: ({ options }) => Option.some(options)
436
573
  }));
437
574
  /** The option after `current` that starts with `key`, ignoring case, wrapping round; else `current`. */
@@ -613,280 +750,391 @@ const make = (options) => {
613
750
  anchor: Option.none(),
614
751
  cells: Option.none()
615
752
  },
616
- editing: Option.none()
753
+ editing: Option.none(),
754
+ filtering: Option.none()
617
755
  } }),
618
- update: (model, message) => Message.match(message, {
619
- Focused: ({ address }) => afterEdit(model, (settled) => ({ model: clearCells(focusTo(settled, address)) })),
620
- Moved: ({ address, reveal }) => ({
621
- model: clearCells(focusTo(model, address)),
756
+ update: (model, message) => guarded(model, message)
757
+ });
758
+ /** A form whose column a write hid (a restored layout): it is closed, as hiding closes it. */
759
+ const drawnForm = (model) => Option.exists(model.filtering, (open) => model.columns.hidden.includes(open.column)) ? modifyFields(model, { filtering: () => Option.none() }) : model;
760
+ const guarded = (given, message) => {
761
+ const model = drawnForm(given);
762
+ return Option.isNone(model.filtering) ? updated(model, message) : Match.value(message).pipe(Match.tag("Pasted", "FillRequested", "UndoRequested", "RedoRequested", () => ({ model })), Match.tag("Focused", "Moved", "CellPressed", "EditStarted", "EditTyped", "EditToggled", "MenuOpened", "FillStarted", () => updated(modifyFields(model, { filtering: () => Option.none() }), message)), Match.orElse(() => updated(model, message)));
763
+ };
764
+ const filterOf = (filtering, stale) => Option.match(stale, {
765
+ onNone: () => filtering,
766
+ onSome: ({ drawn, generation }) => generation === filtering.generation ? {
767
+ ...filtering,
768
+ form: formOf(drawn),
769
+ reported: drawn,
770
+ generation: generation + 1
771
+ } : filtering
772
+ });
773
+ /**
774
+ * One field of the open form changed, from what the frame drew: `change`
775
+ * gives the form, or none when the field is not this kind's. A form that
776
+ * reads as a valid filter other than the last reported is reported.
777
+ */
778
+ const filterEdited = (model, stale, change) => Option.match(model.filtering, {
779
+ onNone: () => ({ model }),
780
+ onSome: (open) => {
781
+ const kind = filterKinds.get(open.column);
782
+ const base = filterOf(open, stale);
783
+ const form = Option.getOrElse(change(base.form, kind), () => base.form);
784
+ if (base === open && Equal.equals(form, open.form)) return { model };
785
+ const reading = readForm(kind, form);
786
+ const reports = reading.invalid.length === 0 && !Equal.equals(reading.filter, base.reported);
787
+ const next = reports ? {
788
+ ...base,
789
+ form,
790
+ reported: reading.filter,
791
+ generation: base.generation + 1
792
+ } : {
793
+ ...base,
794
+ form
795
+ };
796
+ return {
797
+ model: modifyFields(model, { filtering: () => Option.some(next) }),
798
+ ...reports ? { outMessage: Out.Filtered({ change: {
799
+ column: open.column,
800
+ filter: reading.filter
801
+ } }) } : {}
802
+ };
803
+ }
804
+ });
805
+ const updated = (model, message) => Message.match(message, {
806
+ Focused: ({ address }) => afterEdit(model, (settled) => ({ model: clearCells(focusTo(settled, address)) })),
807
+ Moved: ({ address, reveal }) => ({
808
+ model: clearCells(focusTo(model, address)),
809
+ commands: Option.match(reveal, {
810
+ onNone: () => [],
811
+ onSome: (offsets) => [GridViewport.scrollTo(options.id, offsets)]
812
+ })
813
+ }),
814
+ HeaderFocused: ({ column, reveal }) => {
815
+ const next = focus.bundle.update(model.focus, focus.Message.HeaderFocused({ column }), void 0).model;
816
+ return {
817
+ model: next === model.focus ? model : modifyFields(model, { focus: () => next }),
622
818
  commands: Option.match(reveal, {
623
819
  onNone: () => [],
624
820
  onSome: (offsets) => [GridViewport.scrollTo(options.id, offsets)]
625
821
  })
626
- }),
627
- HeaderFocused: ({ column, reveal }) => {
628
- const next = focus.bundle.update(model.focus, focus.Message.HeaderFocused({ column }), void 0).model;
629
- return {
630
- model: next === model.focus ? model : modifyFields(model, { focus: () => next }),
631
- commands: Option.match(reveal, {
632
- onNone: () => [],
633
- onSome: (offsets) => [GridViewport.scrollTo(options.id, offsets)]
634
- })
635
- };
636
- },
637
- Measured: (fields) => ({ model: viewportBy(model, GridViewport.Message.Measured(fields)) }),
638
- Revealed: (fields) => ({ model: viewportBy(model, GridViewport.Message.Revealed(fields)) }),
639
- ColumnResized: ({ column, width }) => ({ model: columnsBy(model, columnState.resize(model.columns, column, width)) }),
640
- ColumnHidden: ({ column }) => {
641
- const hidden = columnsBy(model, columnState.hide(model.columns, column));
642
- if (hidden === model) return { model };
643
- const off = (on) => Option.contains(on, column);
644
- return { model: modifyFields(hidden, {
645
- editing: (editing) => off(Option.map(editing, (each) => each.address.column)) ? Option.none() : editing,
646
- focus: (current) => off(current.header) ? {
647
- ...current,
648
- header: Option.none()
649
- } : current,
650
- menu: (menu) => off(Option.map(menu, (each) => each.column)) ? Option.none() : menu
651
- }) };
652
- },
653
- ColumnShown: ({ column }) => ({ model: columnsBy(model, columnState.show(model.columns, column)) }),
654
- ColumnMoved: ({ column, region, index }) => ({ model: columnsBy(model, columnState.move(model.columns, column, region, index)) }),
655
- ResizeStarted: ({ header }) => ({ model: Option.match(Option.filter(columnOf(header), (column) => options.columns.byId[column].resizable !== false), {
656
- onNone: () => model,
657
- onSome: (column) => modifyFields(model, { resizing: () => Option.some({
822
+ };
823
+ },
824
+ Measured: (fields) => ({ model: viewportBy(model, GridViewport.Message.Measured(fields)) }),
825
+ Revealed: (fields) => ({ model: viewportBy(model, GridViewport.Message.Revealed(fields)) }),
826
+ ColumnResized: ({ column, width }) => ({ model: columnsBy(model, columnState.resize(model.columns, column, width)) }),
827
+ ColumnHidden: ({ column }) => {
828
+ const hidden = columnsBy(model, columnState.hide(model.columns, column));
829
+ if (hidden === model) return { model };
830
+ const off = (on) => Option.contains(on, column);
831
+ return { model: modifyFields(hidden, {
832
+ editing: (editing) => off(Option.map(editing, (each) => each.address.column)) ? Option.none() : editing,
833
+ focus: (current) => off(current.header) ? {
834
+ ...current,
835
+ header: Option.none()
836
+ } : current,
837
+ menu: (menu) => off(Option.map(menu, (each) => each.column)) ? Option.none() : menu,
838
+ filtering: (filtering) => off(Option.map(filtering, (each) => each.column)) ? Option.none() : filtering
839
+ }) };
840
+ },
841
+ ColumnShown: ({ column }) => ({ model: columnsBy(model, columnState.show(model.columns, column)) }),
842
+ ColumnMoved: ({ column, region, index }) => ({ model: columnsBy(model, columnState.move(model.columns, column, region, index)) }),
843
+ ResizeStarted: ({ header }) => ({ model: Option.match(Option.filter(columnOf(header), (column) => options.columns.byId[column].resizable !== false), {
844
+ onNone: () => model,
845
+ onSome: (column) => modifyFields(model, { resizing: () => Option.some({
846
+ column,
847
+ from: columnState.widthOf(model.columns)(column)
848
+ }) })
849
+ }) }),
850
+ ResizeMoved: ({ delta }) => ({ model: Option.match(model.resizing, {
851
+ onNone: () => model,
852
+ onSome: ({ column, from }) => columnsBy(model, columnState.resize(model.columns, column, from + delta))
853
+ }) }),
854
+ MenuOpened: ({ column }) => model.columns.hidden.includes(column) ? { model } : afterEdit(model, (settled) => {
855
+ const focused = focus.bundle.update(settled.focus, focus.Message.HeaderFocused({ column }), void 0).model;
856
+ return { model: modifyFields(settled, {
857
+ focus: () => focused,
858
+ menu: () => Option.some({
658
859
  column,
659
- from: columnState.widthOf(model.columns)(column)
660
- }) })
661
- }) }),
662
- ResizeMoved: ({ delta }) => ({ model: Option.match(model.resizing, {
663
- onNone: () => model,
664
- onSome: ({ column, from }) => columnsBy(model, columnState.resize(model.columns, column, from + delta))
665
- }) }),
666
- MenuOpened: ({ column }) => model.columns.hidden.includes(column) ? { model } : afterEdit(model, (settled) => {
667
- const focused = focus.bundle.update(settled.focus, focus.Message.HeaderFocused({ column }), void 0).model;
668
- return { model: modifyFields(settled, {
669
- focus: () => focused,
670
- menu: () => Option.some({
671
- column,
672
- active: 0
673
- })
674
- }) };
675
- }),
676
- MenuMoved: ({ active }) => ({ model: Option.match(model.menu, {
677
- onNone: () => model,
678
- onSome: (menu) => {
679
- const last = menuItems(model.columns, menu.column).length - 1;
680
- const clamped = Math.min(Math.max(0, Math.trunc(active)), Math.max(0, last));
681
- return clamped === menu.active ? model : modifyFields(model, { menu: () => Option.some({
682
- ...menu,
683
- active: clamped
684
- }) });
685
- }
686
- }) }),
687
- MenuChosen: ({ index }) => Option.match(model.menu, {
688
- onNone: () => ({ model }),
689
- onSome: ({ column }) => Option.match(Option.fromUndefinedOr(menuItems(model.columns, column)[index]), {
690
- onNone: () => ({ model: closeMenu(model) }),
691
- onSome: (item) => bundle.update(closeMenu(model), messageOf(model.columns, column, item), void 0)
860
+ active: 0
692
861
  })
693
- }),
694
- MenuClosed: () => ({ model: closeMenu(model) }),
695
- ColumnDragStarted: ({ header }) => ({ model: Option.match(Option.filter(columnOf(header), (column) => !model.columns.hidden.includes(column)), {
696
- onNone: () => model,
697
- onSome: (column) => modifyFields(model, { dragging: () => Option.some({
698
- column,
699
- delta: 0
700
- }) })
701
- }) }),
702
- ColumnDragged: ({ delta }) => ({ model: Option.match(model.dragging, {
703
- onNone: () => model,
704
- onSome: (dragging) => dragging.delta === delta ? model : modifyFields(model, { dragging: () => Option.some({
705
- ...dragging,
706
- delta
707
- }) })
708
- }) }),
709
- ColumnDragEnded: ({ completed }) => ({ model: Option.match(model.dragging, {
710
- onNone: () => model,
711
- onSome: ({ column, delta }) => {
712
- const dropped = completed ? columnsBy(model, columnState.move(model.columns, column, columnState.regionOf(model.columns, column), columnState.dropAt(model.columns, column, delta))) : model;
713
- return modifyFields(dropped, { dragging: () => Option.none() });
714
- }
715
- }) }),
716
- RowSelected: ({ row }) => ({ model: options.rowSelection === void 0 ? model : selectionBy(model, choose(model.selection, row)) }),
717
- RowsExtended: ({ rows, to }) => {
718
- if (options.rowSelection !== "multiple") return { model };
719
- const added = GridSelection.add(model.selection.rows, rows);
720
- const anchor = Option.orElse(model.selection.anchor, () => Option.some(to));
721
- return { model: added === model.selection.rows && Option.isSome(model.selection.anchor) ? model : selectionBy(model, {
722
- ...model.selection,
723
- rows: added,
724
- anchor
725
- }) };
726
- },
727
- AllRowsSelected: () => ({ model: options.rowSelection !== "multiple" ? model : selectionBy(model, {
862
+ }) };
863
+ }),
864
+ MenuMoved: ({ active }) => ({ model: Option.match(model.menu, {
865
+ onNone: () => model,
866
+ onSome: (menu) => {
867
+ const last = menuItems(model.columns, menu.column).length - 1;
868
+ const clamped = Math.min(Math.max(0, Math.trunc(active)), Math.max(0, last));
869
+ return clamped === menu.active ? model : modifyFields(model, { menu: () => Option.some({
870
+ ...menu,
871
+ active: clamped
872
+ }) });
873
+ }
874
+ }) }),
875
+ MenuChosen: ({ index }) => Option.match(model.menu, {
876
+ onNone: () => ({ model }),
877
+ onSome: ({ column }) => Option.match(Option.fromUndefinedOr(menuItems(model.columns, column)[index]), {
878
+ onNone: () => ({ model: closeMenu(model) }),
879
+ onSome: (item) => updated(closeMenu(model), messageOf(model.columns, column, item))
880
+ })
881
+ }),
882
+ MenuClosed: () => ({ model: closeMenu(model) }),
883
+ ColumnDragStarted: ({ header }) => ({ model: Option.match(Option.filter(columnOf(header), (column) => !model.columns.hidden.includes(column)), {
884
+ onNone: () => model,
885
+ onSome: (column) => modifyFields(model, { dragging: () => Option.some({
886
+ column,
887
+ delta: 0
888
+ }) })
889
+ }) }),
890
+ ColumnDragged: ({ delta }) => ({ model: Option.match(model.dragging, {
891
+ onNone: () => model,
892
+ onSome: (dragging) => dragging.delta === delta ? model : modifyFields(model, { dragging: () => Option.some({
893
+ ...dragging,
894
+ delta
895
+ }) })
896
+ }) }),
897
+ ColumnDragEnded: ({ completed }) => ({ model: Option.match(model.dragging, {
898
+ onNone: () => model,
899
+ onSome: ({ column, delta }) => {
900
+ const dropped = completed ? columnsBy(model, columnState.move(model.columns, column, columnState.regionOf(model.columns, column), columnState.dropAt(model.columns, column, delta))) : model;
901
+ return modifyFields(dropped, { dragging: () => Option.none() });
902
+ }
903
+ }) }),
904
+ RowSelected: ({ row }) => ({ model: options.rowSelection === void 0 ? model : selectionBy(model, choose(model.selection, row)) }),
905
+ RowsExtended: ({ rows, to }) => {
906
+ if (options.rowSelection !== "multiple") return { model };
907
+ const added = GridSelection.add(model.selection.rows, rows);
908
+ const anchor = Option.orElse(model.selection.anchor, () => Option.some(to));
909
+ return { model: added === model.selection.rows && Option.isSome(model.selection.anchor) ? model : selectionBy(model, {
728
910
  ...model.selection,
729
- rows: RowSelection.AllExcept({ except: [] })
730
- }) }),
731
- RowsCleared: () => {
732
- return { model: RowSelection.match(model.selection.rows, {
733
- Keys: ({ keys }) => keys.length === 0,
734
- AllExcept: () => false
735
- }) && Option.isNone(model.selection.anchor) ? model : selectionBy(model, {
736
- ...model.selection,
737
- rows: GridSelection.none,
738
- anchor: Option.none()
739
- }) };
740
- },
741
- CellsSelected: ({ anchor, focus: far, reveal }) => options.cellSelection === true ? {
742
- model: selectionBy(model, {
743
- ...model.selection,
744
- cells: Option.some({
745
- anchor,
746
- focus: far
747
- })
748
- }),
749
- commands: Option.match(reveal, {
750
- onNone: () => [],
751
- onSome: (offsets) => [GridViewport.scrollTo(options.id, offsets)]
911
+ rows: added,
912
+ anchor
913
+ }) };
914
+ },
915
+ AllRowsSelected: () => ({ model: options.rowSelection !== "multiple" ? model : selectionBy(model, {
916
+ ...model.selection,
917
+ rows: RowSelection.AllExcept({ except: [] })
918
+ }) }),
919
+ RowsCleared: () => {
920
+ return { model: RowSelection.match(model.selection.rows, {
921
+ Keys: ({ keys }) => keys.length === 0,
922
+ AllExcept: () => false
923
+ }) && Option.isNone(model.selection.anchor) ? model : selectionBy(model, {
924
+ ...model.selection,
925
+ rows: GridSelection.none,
926
+ anchor: Option.none()
927
+ }) };
928
+ },
929
+ CellsSelected: ({ anchor, focus: far, reveal }) => options.cellSelection === true ? {
930
+ model: selectionBy(model, {
931
+ ...model.selection,
932
+ cells: Option.some({
933
+ anchor,
934
+ focus: far
752
935
  })
753
- } : { model },
754
- CellPressed: ({ cell, shiftKey, toggleKey }) => Option.match(addressOf(cell), {
755
- onNone: () => ({ model }),
756
- onSome: (address) => Option.exists(model.editing, (editing) => sameCell(editing.address, address)) ? { model } : afterEdit(model, (settled) => pressed(settled, address, shiftKey, toggleKey))
757
936
  }),
758
- EditStarted: ({ address, draft }) => options.columns.byId[address.column].edit === void 0 ? { model } : { model: modifyFields(clearCells(focusTo(model, address)), { editing: () => Option.some({
759
- address,
760
- from: draft,
761
- draft,
762
- error: Option.none()
763
- }) }) },
764
- EditTyped: ({ address, text, from }) => {
765
- if (options.columns.byId[address.column].edit === void 0) return { model };
766
- const open = Option.filter(model.editing, (editing) => sameCell(editing.address, address));
767
- const typed = (draft) => Option.match(optionsOf(address.column), {
768
- onNone: () => draft + text,
769
- onSome: (choices) => typedOption(choices, draft, text)
770
- });
771
- const editing = Option.match(open, {
772
- onNone: () => ({
773
- address,
774
- from,
775
- draft: Option.isSome(optionsOf(address.column)) ? typed(from) : text,
776
- error: Option.none()
777
- }),
778
- onSome: (edit) => ({
779
- ...edit,
780
- draft: typed(edit.draft),
781
- error: Option.none()
782
- })
783
- });
784
- return { model: modifyFields(clearCells(focusTo(model, address)), { editing: () => Option.some(editing) }) };
785
- },
786
- EditChanged: ({ draft }) => ({ model: Option.match(model.editing, {
787
- onNone: () => model,
788
- onSome: (editing) => editing.draft === draft && Option.isNone(editing.error) ? model : modifyFields(model, { editing: () => Option.some({
789
- ...editing,
790
- draft,
937
+ commands: Option.match(reveal, {
938
+ onNone: () => [],
939
+ onSome: (offsets) => [GridViewport.scrollTo(options.id, offsets)]
940
+ })
941
+ } : { model },
942
+ CellPressed: ({ cell, shiftKey, toggleKey }) => Option.match(addressOf(cell), {
943
+ onNone: () => ({ model }),
944
+ onSome: (address) => Option.exists(model.editing, (editing) => sameCell(editing.address, address)) ? { model } : afterEdit(model, (settled) => pressed(settled, address, shiftKey, toggleKey))
945
+ }),
946
+ EditToggled: ({ cell, checked, by }) => Option.match(Option.flatMap(Option.filter(addressOf(cell), () => Option.isNone(model.editing) && Toggle.match(by, {
947
+ Press: () => true,
948
+ Key: ({ drawn }) => sameFocus(drawn, model.focus.current)
949
+ })), (address) => Option.flatMap(Option.fromUndefinedOr(toggles.get(address.column)), (toggle) => Option.map(toggle(!checked), (text) => ({
950
+ address,
951
+ text
952
+ })))), {
953
+ onNone: () => ({ model }),
954
+ onSome: ({ address, text }) => ({
955
+ model: clearCells(focusTo(model, address)),
956
+ outMessage: Out.Edited({
957
+ row: address.row,
958
+ column: address.column,
959
+ text
960
+ })
961
+ })
962
+ }),
963
+ EditStarted: ({ address, draft }) => !drafts(address.column) ? { model } : { model: modifyFields(clearCells(focusTo(model, address)), { editing: () => Option.some({
964
+ address,
965
+ from: draft,
966
+ draft,
967
+ error: Option.none()
968
+ }) }) },
969
+ EditTyped: ({ address, text, from }) => {
970
+ if (!drafts(address.column)) return { model };
971
+ const open = Option.filter(model.editing, (editing) => sameCell(editing.address, address));
972
+ const typed = (draft) => Option.match(optionsOf(address.column), {
973
+ onNone: () => draft + text,
974
+ onSome: (choices) => typedOption(choices, draft, text)
975
+ });
976
+ const editing = Option.match(open, {
977
+ onNone: () => ({
978
+ address,
979
+ from,
980
+ draft: Option.isSome(optionsOf(address.column)) ? typed(from) : text,
981
+ error: Option.none()
982
+ }),
983
+ onSome: (edit) => ({
984
+ ...edit,
985
+ draft: typed(edit.draft),
791
986
  error: Option.none()
792
- }) })
793
- }) }),
794
- EditStepped: ({ by }) => Option.match(model.editing, {
795
- onNone: () => ({ model }),
796
- onSome: (editing) => Option.match(optionsOf(editing.address.column), {
797
- onNone: () => ({ model }),
798
- onSome: (choices) => {
799
- const draft = choices[steppedIndex(by, choices.indexOf(editing.draft), choices.length)];
800
- return draft === void 0 || draft === editing.draft ? { model } : { model: modifyFields(model, { editing: () => Option.some({
801
- ...editing,
802
- draft,
803
- error: Option.none()
804
- }) }) };
805
- }
806
987
  })
807
- }),
808
- EditChosen: ({ draft }) => Option.match(model.editing, {
988
+ });
989
+ return { model: modifyFields(clearCells(focusTo(model, address)), { editing: () => Option.some(editing) }) };
990
+ },
991
+ EditChanged: ({ draft }) => ({ model: Option.match(model.editing, {
992
+ onNone: () => model,
993
+ onSome: (editing) => editing.draft === draft && Option.isNone(editing.error) ? model : modifyFields(model, { editing: () => Option.some({
994
+ ...editing,
995
+ draft,
996
+ error: Option.none()
997
+ }) })
998
+ }) }),
999
+ EditStepped: ({ by }) => Option.match(model.editing, {
1000
+ onNone: () => ({ model }),
1001
+ onSome: (editing) => Option.match(optionsOf(editing.address.column), {
809
1002
  onNone: () => ({ model }),
810
- onSome: (editing) => committed(modifyFields(model, { editing: () => Option.some({
811
- ...editing,
812
- draft
813
- }) }), Option.none(), Option.none())
814
- }),
815
- EditCommitted: ({ next, reveal }) => committed(model, next, reveal),
816
- Pasted: ({ cells }) => {
817
- if (Option.isSome(model.editing) || cells.length === 0) return { model };
818
- const { accepted, refused } = judged(cells);
819
- return accepted.length === 0 && refused.length === 0 ? { model } : {
820
- model,
821
- outMessage: Out.Pasted({
822
- accepted,
823
- refused
824
- })
825
- };
826
- },
827
- FillRequested: (request) => Option.isSome(model.editing) ? { model } : {
1003
+ onSome: (choices) => {
1004
+ const draft = choices[steppedIndex(by, choices.indexOf(editing.draft), choices.length)];
1005
+ return draft === void 0 || draft === editing.draft ? { model } : { model: modifyFields(model, { editing: () => Option.some({
1006
+ ...editing,
1007
+ draft,
1008
+ error: Option.none()
1009
+ }) }) };
1010
+ }
1011
+ })
1012
+ }),
1013
+ EditChosen: ({ draft }) => Option.match(model.editing, {
1014
+ onNone: () => ({ model }),
1015
+ onSome: (editing) => committed(modifyFields(model, { editing: () => Option.some({
1016
+ ...editing,
1017
+ draft
1018
+ }) }), Option.none(), Option.none())
1019
+ }),
1020
+ EditCommitted: ({ next, reveal }) => committed(model, next, reveal),
1021
+ Pasted: ({ cells }) => {
1022
+ if (Option.isSome(model.editing) || cells.length === 0) return { model };
1023
+ const { accepted, refused } = judged(cells);
1024
+ return accepted.length === 0 && refused.length === 0 ? { model } : {
828
1025
  model,
829
- outMessage: Out.Filled({
830
- source: request.source,
831
- to: request.to
1026
+ outMessage: Out.Pasted({
1027
+ accepted,
1028
+ refused
832
1029
  })
833
- },
834
- FillStarted: () => {
835
- if (Option.isSome(model.editing)) return { model };
836
- const source = Option.orElse(model.selection.cells, () => Option.map(model.focus.current, (address) => ({
837
- anchor: address,
838
- focus: address
839
- })));
840
- return { model: Option.match(source, {
841
- onNone: () => model,
842
- onSome: (range) => modifyFields(model, { filling: () => Option.some({
843
- source: range,
844
- to: range.focus
845
- }) })
846
- }) };
847
- },
848
- FillDragged: ({ cell }) => ({ model: Option.match(Option.zipWith(model.filling, addressOf(cell), (filling, to) => ({
849
- filling,
850
- to
851
- })), {
1030
+ };
1031
+ },
1032
+ FillRequested: (request) => Option.isSome(model.editing) ? { model } : {
1033
+ model,
1034
+ outMessage: Out.Filled({
1035
+ source: request.source,
1036
+ to: request.to
1037
+ })
1038
+ },
1039
+ FillStarted: () => {
1040
+ if (Option.isSome(model.editing)) return { model };
1041
+ const source = Option.orElse(model.selection.cells, () => Option.map(model.focus.current, (address) => ({
1042
+ anchor: address,
1043
+ focus: address
1044
+ })));
1045
+ return { model: Option.match(source, {
852
1046
  onNone: () => model,
853
- onSome: ({ filling, to }) => sameCell(filling.to, to) ? model : modifyFields(model, { filling: () => Option.some({
854
- ...filling,
855
- to
1047
+ onSome: (range) => modifyFields(model, { filling: () => Option.some({
1048
+ source: range,
1049
+ to: range.focus
856
1050
  }) })
857
- }) }),
858
- FillEnded: ({ completed }) => Option.match(model.filling, {
859
- onNone: () => ({ model }),
860
- onSome: ({ source, to }) => {
861
- const ended = modifyFields(model, { filling: () => Option.none() });
862
- return completed ? {
863
- model: ended,
864
- outMessage: Out.Filled({
865
- source,
866
- to
867
- })
868
- } : { model: ended };
869
- }
870
- }),
871
- UndoRequested: () => Option.isSome(model.editing) ? { model } : {
872
- model,
873
- outMessage: Out.UndoRequested()
874
- },
875
- RedoRequested: () => Option.isSome(model.editing) ? { model } : {
876
- model,
877
- outMessage: Out.RedoRequested()
878
- },
879
- EditCancelled: () => ({ model: Option.isNone(model.editing) ? model : modifyFields(model, { editing: () => Option.none() }) }),
880
- CellsCleared: () => ({ model: clearCells(model) }),
881
- MountStarted: () => ({ model }),
882
- ResizeEnded: ({ completed }) => ({ model: Option.match(model.resizing, {
883
- onNone: () => model,
884
- onSome: ({ column, from }) => {
885
- const settled = completed ? model : columnsBy(model, columnState.resize(model.columns, column, from));
886
- return modifyFields(settled, { resizing: () => Option.none() });
887
- }
1051
+ }) };
1052
+ },
1053
+ FillDragged: ({ cell }) => ({ model: Option.match(Option.zipWith(model.filling, addressOf(cell), (filling, to) => ({
1054
+ filling,
1055
+ to
1056
+ })), {
1057
+ onNone: () => model,
1058
+ onSome: ({ filling, to }) => sameCell(filling.to, to) ? model : modifyFields(model, { filling: () => Option.some({
1059
+ ...filling,
1060
+ to
888
1061
  }) })
889
- })
1062
+ }) }),
1063
+ FillEnded: ({ completed }) => Option.match(model.filling, {
1064
+ onNone: () => ({ model }),
1065
+ onSome: ({ source, to }) => {
1066
+ const ended = modifyFields(model, { filling: () => Option.none() });
1067
+ return completed ? {
1068
+ model: ended,
1069
+ outMessage: Out.Filled({
1070
+ source,
1071
+ to
1072
+ })
1073
+ } : { model: ended };
1074
+ }
1075
+ }),
1076
+ UndoRequested: () => Option.isSome(model.editing) ? { model } : {
1077
+ model,
1078
+ outMessage: Out.UndoRequested()
1079
+ },
1080
+ RedoRequested: () => Option.isSome(model.editing) ? { model } : {
1081
+ model,
1082
+ outMessage: Out.RedoRequested()
1083
+ },
1084
+ EditCancelled: () => ({ model: Option.isNone(model.editing) ? model : modifyFields(model, { editing: () => Option.none() }) }),
1085
+ CellsCleared: () => ({ model: clearCells(model) }),
1086
+ MountStarted: () => ({ model }),
1087
+ FilterOpened: ({ column, drawn }) => {
1088
+ const kind = filterKinds.get(column);
1089
+ const accepted = Option.match(drawn, {
1090
+ onNone: () => true,
1091
+ onSome: (filter) => Schema.is(filterSchemas.get(column))(filter)
1092
+ });
1093
+ if (kind === void 0 || model.columns.hidden.includes(column) || !accepted) return { model };
1094
+ return afterEdit(model, (settled) => ({ model: modifyFields(settled, {
1095
+ focus: (current) => focus.bundle.update(current, focus.Message.HeaderFocused({ column }), void 0).model,
1096
+ menu: () => Option.none(),
1097
+ filtering: () => Option.some({
1098
+ column,
1099
+ form: formOf(drawn),
1100
+ reported: drawn,
1101
+ generation: 0
1102
+ })
1103
+ }) }));
1104
+ },
1105
+ FilterTyped: ({ field, text, stale }) => filterEdited(model, stale, (form, kind) => {
1106
+ return CellEditor.match(kind, {
1107
+ Text: () => field === "text",
1108
+ Number: () => field === "min" || field === "max",
1109
+ Date: () => field === "from" || field === "to",
1110
+ Choice: () => false,
1111
+ Checkbox: () => false
1112
+ }) ? Option.some({
1113
+ ...form,
1114
+ [field]: text
1115
+ }) : Option.none();
1116
+ }),
1117
+ FilterOptionToggled: ({ option, stale }) => filterEdited(model, stale, (form, kind) => Option.flatMap(Option.liftPredicate(kind, CellEditor.guards.Choice), ({ options: offered }) => {
1118
+ const ticked = new Set(form.options);
1119
+ const on = !ticked.has(option);
1120
+ return Option.some({
1121
+ ...form,
1122
+ options: offered.filter((each) => each === option ? on : ticked.has(each))
1123
+ });
1124
+ })),
1125
+ FilterChecked: ({ state, stale }) => filterEdited(model, stale, (form, kind) => CellEditor.guards.Checkbox(kind) ? Option.some({
1126
+ ...form,
1127
+ checked: state
1128
+ }) : Option.none()),
1129
+ FilterCleared: ({ stale }) => filterEdited(model, stale, () => Option.some(emptyForm)),
1130
+ FilterClosed: () => ({ model: Option.isNone(model.filtering) ? model : modifyFields(model, { filtering: () => Option.none() }) }),
1131
+ ResizeEnded: ({ completed }) => ({ model: Option.match(model.resizing, {
1132
+ onNone: () => model,
1133
+ onSome: ({ column, from }) => {
1134
+ const settled = completed ? model : columnsBy(model, columnState.resize(model.columns, column, from));
1135
+ return modifyFields(settled, { resizing: () => Option.none() });
1136
+ }
1137
+ }) })
890
1138
  });
891
1139
  const projections = /* @__PURE__ */ new WeakMap();
892
1140
  /** The text an edit of a cell begins from: its column's `draft`, or the text it shows. */
@@ -958,12 +1206,46 @@ const make = (options) => {
958
1206
  viewport: input.state.viewport,
959
1207
  ...input.overscan === void 0 ? {} : { overscan: input.overscan }
960
1208
  });
1209
+ /** A column's filter kind; none for a column that does not filter. */
1210
+ const filterFor = (column) => Option.fromUndefinedOr(filterKinds.get(column));
1211
+ /** Every filtering column none: the application's starting value. */
1212
+ const noFilters = Object.fromEntries(filterIds.map((id) => [id, Option.none()]));
1213
+ /** `filters` with one `Out.Filtered`'s change written in. */
1214
+ const setFilter = (filters, change) => ({
1215
+ ...filters,
1216
+ [change.column]: change.filter
1217
+ });
1218
+ /**
1219
+ * A predicate over held rows: a row is kept when every given filter keeps
1220
+ * the value `read` names for its column, the application's own field
1221
+ * (typed by the column's filter), never the text a cell shows. Build it
1222
+ * once per `filters`.
1223
+ */
1224
+ const keeps = (filters, read) => {
1225
+ const given = filters;
1226
+ const reads = read;
1227
+ const tests = filterIds.flatMap((id) => Option.match(given[id], {
1228
+ onNone: () => [],
1229
+ onSome: (filter) => {
1230
+ const test = testOf(filter);
1231
+ const at = reads[id];
1232
+ return [(row) => test(at(row))];
1233
+ }
1234
+ }));
1235
+ return (row) => tests.every((test) => test(row));
1236
+ };
961
1237
  return {
962
1238
  id: options.id,
1239
+ filterFor,
1240
+ Filters,
1241
+ noFilters,
1242
+ setFilter,
1243
+ keeps,
963
1244
  matchEdit,
964
1245
  editorFor,
965
1246
  MenuItem,
966
1247
  menuItems,
1248
+ Toggle,
967
1249
  columns: options.columns,
968
1250
  rowSelection: options.rowSelection,
969
1251
  cellSelection: options.cellSelection === true,
@@ -974,7 +1256,7 @@ const make = (options) => {
974
1256
  fill,
975
1257
  extendRows,
976
1258
  draftOf,
977
- Out,
1259
+ Out: filterIds.length === 0 ? Unfiltered : Out,
978
1260
  Model,
979
1261
  Message,
980
1262
  bundle