foldkit-data-grid 0.6.0 → 0.8.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,28 @@ 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
+ * Every filter the form opened with or reported since it was last
148
+ * seeded: the application's filter is the form's own echo while it is
149
+ * one of these (see `isEcho`), whenever and however cleaned it lands.
150
+ */
151
+ sent: Schema.Array(Schema.OptionFromNullOr(ColumnFilter)),
152
+ /**
153
+ * Counts the form's reports and reseeds: a stale frame's change reseeds
154
+ * only while it is the one the frame was drawn at, so a second change
155
+ * from that frame adds to the first.
156
+ */
157
+ generation: Schema.Number
104
158
  }))
105
159
  });
106
160
  const Region = Schema.Literals([
@@ -118,6 +172,15 @@ const make = (options) => {
118
172
  Hide: {},
119
173
  Show: { column: Column }
120
174
  });
175
+ /**
176
+ * What toggled a checkbox: a press on its box, or a key on the cell the
177
+ * frame was drawn focusing, with the focus that frame was drawn from.
178
+ */
179
+ const Toggle = defineTaggedUnion({
180
+ Press: {},
181
+ Key: { drawn: Schema.OptionFromNullOr(focus.Address) }
182
+ });
183
+ const sameFocus = Option.makeEquivalence((a, b) => sameCell(a, b));
121
184
  const Message = defineMessageUnion({
122
185
  /** The pointer or the browser focused a cell. */
123
186
  Focused: { address: focus.Address },
@@ -232,7 +295,25 @@ const make = (options) => {
232
295
  * test resolves them with a Message the runtime sends too.
233
296
  */
234
297
  MountStarted: {},
235
- /** An edit began on an editable cell, with the text it starts from. */
298
+ /**
299
+ * The checkbox in the cell whose DOM id is `cell`, drawn `checked`, was
300
+ * pressed (`Toggle.Press`), or Space or Enter was pressed on the cell a
301
+ * frame drew as the tab stop (`Toggle.Key`, with the focus that frame was
302
+ * drawn from): the column's text for the other value is committed at once
303
+ * and reported as `Out.Edited`, with no draft. The id comes from the page,
304
+ * so a cell that is not a checkbox of this grid, or a toggle while an edit
305
+ * is open, changes nothing; nor does a key whose frame was drawn before
306
+ * focus moved, which would toggle the cell focus left.
307
+ */
308
+ EditToggled: {
309
+ cell: Schema.String,
310
+ checked: Schema.Boolean,
311
+ by: Toggle
312
+ },
313
+ /**
314
+ * An edit began on an editable cell, with the text it starts from. A
315
+ * checkbox's cell opens none: it is toggled (`EditToggled`).
316
+ */
236
317
  EditStarted: {
237
318
  address: focus.Address,
238
319
  draft: Schema.String
@@ -292,7 +373,51 @@ const make = (options) => {
292
373
  /** The pointer, filling, is over the cell whose DOM id is `cell`. */
293
374
  FillDragged: { cell: Schema.String },
294
375
  /** The fill handle was let go, or the drag cancelled (`completed` false). */
295
- FillEnded: { completed: Schema.Boolean }
376
+ FillEnded: { completed: Schema.Boolean },
377
+ /**
378
+ * A column's filter form opened, from its button or Alt+ArrowDown on its
379
+ * header, seeded with the application's filter as the view `drawn` it.
380
+ * Focus goes to the header, so closing returns there. A column that does
381
+ * not filter or is hidden, or a `drawn` its column refuses, opens nothing.
382
+ */
383
+ FilterOpened: {
384
+ column: Column,
385
+ drawn: Schema.OptionFromNullOr(ColumnFilter)
386
+ },
387
+ /**
388
+ * A text field of the open form was typed in. `stale` is set by a frame
389
+ * drawn after the application's filter changed to one the form never sent
390
+ * (a saved view, say): the form is seeded again from it first, unless the
391
+ * form has reported since that frame. A field another kind owns changes nothing.
392
+ */
393
+ FilterTyped: {
394
+ field: Schema.Literals([
395
+ "text",
396
+ "min",
397
+ "max",
398
+ "from",
399
+ "to"
400
+ ]),
401
+ text: Schema.String,
402
+ stale: Stale
403
+ },
404
+ /** A choice's option was ticked or unticked; one the column does not offer changes nothing. */
405
+ FilterOptionToggled: {
406
+ option: Schema.String,
407
+ stale: Stale
408
+ },
409
+ /** A checkbox's filter was set: any value, checked, or not. */
410
+ FilterChecked: {
411
+ state: Schema.Literals([
412
+ "any",
413
+ "true",
414
+ "false"
415
+ ]),
416
+ stale: Stale
417
+ },
418
+ /** The open form was emptied: none is reported if a filter held. */
419
+ FilterCleared: { stale: Stale },
420
+ FilterClosed: {}
296
421
  });
297
422
  /**
298
423
  * What the grid reports to its parent: one cell's committed text, or a
@@ -301,7 +426,7 @@ const make = (options) => {
301
426
  * `RedoRequested` carry nothing, since what to undo is the application's
302
427
  * history, read when the request arrives.
303
428
  */
304
- const Out = defineTaggedUnion({
429
+ const outCases = {
305
430
  Edited: {
306
431
  row: Schema.String,
307
432
  column: Column,
@@ -331,7 +456,16 @@ const make = (options) => {
331
456
  from: Schema.String,
332
457
  to: Schema.String
333
458
  }
459
+ };
460
+ const Out = defineTaggedUnion({
461
+ ...outCases,
462
+ /**
463
+ * A column's filter changed, or is none: the application's to store
464
+ * (`setFilter`) and apply (`keeps`, or a query's input).
465
+ */
466
+ Filtered: { change: FilterChangeSchema }
334
467
  });
468
+ const Unfiltered = defineTaggedUnion(outCases);
335
469
  const focusTo = (model, address) => {
336
470
  const next = focus.bundle.update(model.focus, focus.Message.Focused({ address }), void 0).model;
337
471
  return next === model.focus ? model : modifyFields(model, { focus: () => next });
@@ -384,12 +518,20 @@ const make = (options) => {
384
518
  };
385
519
  const decoders = /* @__PURE__ */ new Map();
386
520
  const editors = /* @__PURE__ */ new Map();
521
+ const toggles = /* @__PURE__ */ new Map();
387
522
  for (const id of options.columns.ids) {
388
523
  const edit = options.columns.byId[id].edit;
389
524
  if (edit === void 0) continue;
390
525
  decoders.set(id, Schema.decodeUnknownResult(edit.schema ?? Schema.String));
391
- editors.set(id, editorOf(edit.schema));
526
+ const editor = editorOf(edit.schema);
527
+ editors.set(id, editor);
528
+ if (edit.schema !== void 0 && CellEditor.guards.Checkbox(editor)) {
529
+ const encode = Schema.encodeUnknownResult(edit.schema);
530
+ toggles.set(id, (value) => Result.getSuccess(encode(value)));
531
+ }
392
532
  }
533
+ /** Whether a column's cells open a draft to edit: they edit, and are no checkbox. */
534
+ const drafts = (column) => options.columns.byId[column].edit !== void 0 && !toggles.has(column);
393
535
  /** How a column's cells are edited: as text, a number, or a choice; none when it does not edit. */
394
536
  const editorFor = (column) => Option.fromUndefinedOr(editors.get(column));
395
537
  /** Why a column refuses a text, or none when it takes it. */
@@ -432,6 +574,7 @@ const make = (options) => {
432
574
  Text: () => Option.none(),
433
575
  Number: () => Option.none(),
434
576
  Date: () => Option.none(),
577
+ Checkbox: () => Option.none(),
435
578
  Choice: ({ options }) => Option.some(options)
436
579
  }));
437
580
  /** The option after `current` that starts with `key`, ignoring case, wrapping round; else `current`. */
@@ -613,280 +756,394 @@ const make = (options) => {
613
756
  anchor: Option.none(),
614
757
  cells: Option.none()
615
758
  },
616
- editing: Option.none()
759
+ editing: Option.none(),
760
+ filtering: Option.none()
617
761
  } }),
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)),
762
+ update: (model, message) => guarded(model, message)
763
+ });
764
+ /** A form whose column a write hid (a restored layout): it is closed, as hiding closes it. */
765
+ const drawnForm = (model) => Option.exists(model.filtering, (open) => model.columns.hidden.includes(open.column)) ? modifyFields(model, { filtering: () => Option.none() }) : model;
766
+ const guarded = (given, message) => {
767
+ const model = drawnForm(given);
768
+ 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)));
769
+ };
770
+ const filterOf = (filtering, stale) => Option.match(stale, {
771
+ onNone: () => filtering,
772
+ onSome: ({ drawn, generation }) => generation === filtering.generation ? {
773
+ ...filtering,
774
+ form: formOf(drawn),
775
+ reported: drawn,
776
+ sent: [drawn],
777
+ generation: generation + 1
778
+ } : filtering
779
+ });
780
+ /**
781
+ * One field of the open form changed, from what the frame drew: `change`
782
+ * gives the form, or none when the field is not this kind's. A form that
783
+ * reads as a valid filter other than the last reported is reported.
784
+ */
785
+ const filterEdited = (model, stale, change) => Option.match(model.filtering, {
786
+ onNone: () => ({ model }),
787
+ onSome: (open) => {
788
+ const kind = filterKinds.get(open.column);
789
+ const base = filterOf(open, stale);
790
+ const form = Option.getOrElse(change(base.form, kind), () => base.form);
791
+ if (base === open && Equal.equals(form, open.form)) return { model };
792
+ const reading = readForm(kind, form);
793
+ const reports = reading.invalid.length === 0 && !Equal.equals(reading.filter, base.reported);
794
+ const next = reports ? {
795
+ ...base,
796
+ form,
797
+ reported: reading.filter,
798
+ sent: [...base.sent, reading.filter],
799
+ generation: base.generation + 1
800
+ } : {
801
+ ...base,
802
+ form
803
+ };
804
+ return {
805
+ model: modifyFields(model, { filtering: () => Option.some(next) }),
806
+ ...reports ? { outMessage: Out.Filtered({ change: {
807
+ column: open.column,
808
+ filter: reading.filter
809
+ } }) } : {}
810
+ };
811
+ }
812
+ });
813
+ const updated = (model, message) => Message.match(message, {
814
+ Focused: ({ address }) => afterEdit(model, (settled) => ({ model: clearCells(focusTo(settled, address)) })),
815
+ Moved: ({ address, reveal }) => ({
816
+ model: clearCells(focusTo(model, address)),
817
+ commands: Option.match(reveal, {
818
+ onNone: () => [],
819
+ onSome: (offsets) => [GridViewport.scrollTo(options.id, offsets)]
820
+ })
821
+ }),
822
+ HeaderFocused: ({ column, reveal }) => {
823
+ const next = focus.bundle.update(model.focus, focus.Message.HeaderFocused({ column }), void 0).model;
824
+ return {
825
+ model: next === model.focus ? model : modifyFields(model, { focus: () => next }),
622
826
  commands: Option.match(reveal, {
623
827
  onNone: () => [],
624
828
  onSome: (offsets) => [GridViewport.scrollTo(options.id, offsets)]
625
829
  })
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({
830
+ };
831
+ },
832
+ Measured: (fields) => ({ model: viewportBy(model, GridViewport.Message.Measured(fields)) }),
833
+ Revealed: (fields) => ({ model: viewportBy(model, GridViewport.Message.Revealed(fields)) }),
834
+ ColumnResized: ({ column, width }) => ({ model: columnsBy(model, columnState.resize(model.columns, column, width)) }),
835
+ ColumnHidden: ({ column }) => {
836
+ const hidden = columnsBy(model, columnState.hide(model.columns, column));
837
+ if (hidden === model) return { model };
838
+ const off = (on) => Option.contains(on, column);
839
+ return { model: modifyFields(hidden, {
840
+ editing: (editing) => off(Option.map(editing, (each) => each.address.column)) ? Option.none() : editing,
841
+ focus: (current) => off(current.header) ? {
842
+ ...current,
843
+ header: Option.none()
844
+ } : current,
845
+ menu: (menu) => off(Option.map(menu, (each) => each.column)) ? Option.none() : menu,
846
+ filtering: (filtering) => off(Option.map(filtering, (each) => each.column)) ? Option.none() : filtering
847
+ }) };
848
+ },
849
+ ColumnShown: ({ column }) => ({ model: columnsBy(model, columnState.show(model.columns, column)) }),
850
+ ColumnMoved: ({ column, region, index }) => ({ model: columnsBy(model, columnState.move(model.columns, column, region, index)) }),
851
+ ResizeStarted: ({ header }) => ({ model: Option.match(Option.filter(columnOf(header), (column) => options.columns.byId[column].resizable !== false), {
852
+ onNone: () => model,
853
+ onSome: (column) => modifyFields(model, { resizing: () => Option.some({
854
+ column,
855
+ from: columnState.widthOf(model.columns)(column)
856
+ }) })
857
+ }) }),
858
+ ResizeMoved: ({ delta }) => ({ model: Option.match(model.resizing, {
859
+ onNone: () => model,
860
+ onSome: ({ column, from }) => columnsBy(model, columnState.resize(model.columns, column, from + delta))
861
+ }) }),
862
+ MenuOpened: ({ column }) => model.columns.hidden.includes(column) ? { model } : afterEdit(model, (settled) => {
863
+ const focused = focus.bundle.update(settled.focus, focus.Message.HeaderFocused({ column }), void 0).model;
864
+ return { model: modifyFields(settled, {
865
+ focus: () => focused,
866
+ menu: () => Option.some({
658
867
  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)
868
+ active: 0
692
869
  })
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, {
870
+ }) };
871
+ }),
872
+ MenuMoved: ({ active }) => ({ model: Option.match(model.menu, {
873
+ onNone: () => model,
874
+ onSome: (menu) => {
875
+ const last = menuItems(model.columns, menu.column).length - 1;
876
+ const clamped = Math.min(Math.max(0, Math.trunc(active)), Math.max(0, last));
877
+ return clamped === menu.active ? model : modifyFields(model, { menu: () => Option.some({
878
+ ...menu,
879
+ active: clamped
880
+ }) });
881
+ }
882
+ }) }),
883
+ MenuChosen: ({ index }) => Option.match(model.menu, {
884
+ onNone: () => ({ model }),
885
+ onSome: ({ column }) => Option.match(Option.fromUndefinedOr(menuItems(model.columns, column)[index]), {
886
+ onNone: () => ({ model: closeMenu(model) }),
887
+ onSome: (item) => updated(closeMenu(model), messageOf(model.columns, column, item))
888
+ })
889
+ }),
890
+ MenuClosed: () => ({ model: closeMenu(model) }),
891
+ ColumnDragStarted: ({ header }) => ({ model: Option.match(Option.filter(columnOf(header), (column) => !model.columns.hidden.includes(column)), {
892
+ onNone: () => model,
893
+ onSome: (column) => modifyFields(model, { dragging: () => Option.some({
894
+ column,
895
+ delta: 0
896
+ }) })
897
+ }) }),
898
+ ColumnDragged: ({ delta }) => ({ model: Option.match(model.dragging, {
899
+ onNone: () => model,
900
+ onSome: (dragging) => dragging.delta === delta ? model : modifyFields(model, { dragging: () => Option.some({
901
+ ...dragging,
902
+ delta
903
+ }) })
904
+ }) }),
905
+ ColumnDragEnded: ({ completed }) => ({ model: Option.match(model.dragging, {
906
+ onNone: () => model,
907
+ onSome: ({ column, delta }) => {
908
+ const dropped = completed ? columnsBy(model, columnState.move(model.columns, column, columnState.regionOf(model.columns, column), columnState.dropAt(model.columns, column, delta))) : model;
909
+ return modifyFields(dropped, { dragging: () => Option.none() });
910
+ }
911
+ }) }),
912
+ RowSelected: ({ row }) => ({ model: options.rowSelection === void 0 ? model : selectionBy(model, choose(model.selection, row)) }),
913
+ RowsExtended: ({ rows, to }) => {
914
+ if (options.rowSelection !== "multiple") return { model };
915
+ const added = GridSelection.add(model.selection.rows, rows);
916
+ const anchor = Option.orElse(model.selection.anchor, () => Option.some(to));
917
+ return { model: added === model.selection.rows && Option.isSome(model.selection.anchor) ? model : selectionBy(model, {
728
918
  ...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)]
919
+ rows: added,
920
+ anchor
921
+ }) };
922
+ },
923
+ AllRowsSelected: () => ({ model: options.rowSelection !== "multiple" ? model : selectionBy(model, {
924
+ ...model.selection,
925
+ rows: RowSelection.AllExcept({ except: [] })
926
+ }) }),
927
+ RowsCleared: () => {
928
+ return { model: RowSelection.match(model.selection.rows, {
929
+ Keys: ({ keys }) => keys.length === 0,
930
+ AllExcept: () => false
931
+ }) && Option.isNone(model.selection.anchor) ? model : selectionBy(model, {
932
+ ...model.selection,
933
+ rows: GridSelection.none,
934
+ anchor: Option.none()
935
+ }) };
936
+ },
937
+ CellsSelected: ({ anchor, focus: far, reveal }) => options.cellSelection === true ? {
938
+ model: selectionBy(model, {
939
+ ...model.selection,
940
+ cells: Option.some({
941
+ anchor,
942
+ focus: far
752
943
  })
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
944
  }),
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,
945
+ commands: Option.match(reveal, {
946
+ onNone: () => [],
947
+ onSome: (offsets) => [GridViewport.scrollTo(options.id, offsets)]
948
+ })
949
+ } : { model },
950
+ CellPressed: ({ cell, shiftKey, toggleKey }) => Option.match(addressOf(cell), {
951
+ onNone: () => ({ model }),
952
+ onSome: (address) => Option.exists(model.editing, (editing) => sameCell(editing.address, address)) ? { model } : afterEdit(model, (settled) => pressed(settled, address, shiftKey, toggleKey))
953
+ }),
954
+ EditToggled: ({ cell, checked, by }) => Option.match(Option.flatMap(Option.filter(addressOf(cell), () => Option.isNone(model.editing) && Toggle.match(by, {
955
+ Press: () => true,
956
+ Key: ({ drawn }) => sameFocus(drawn, model.focus.current)
957
+ })), (address) => Option.flatMap(Option.fromUndefinedOr(toggles.get(address.column)), (toggle) => Option.map(toggle(!checked), (text) => ({
958
+ address,
959
+ text
960
+ })))), {
961
+ onNone: () => ({ model }),
962
+ onSome: ({ address, text }) => ({
963
+ model: clearCells(focusTo(model, address)),
964
+ outMessage: Out.Edited({
965
+ row: address.row,
966
+ column: address.column,
967
+ text
968
+ })
969
+ })
970
+ }),
971
+ EditStarted: ({ address, draft }) => !drafts(address.column) ? { model } : { model: modifyFields(clearCells(focusTo(model, address)), { editing: () => Option.some({
972
+ address,
973
+ from: draft,
974
+ draft,
975
+ error: Option.none()
976
+ }) }) },
977
+ EditTyped: ({ address, text, from }) => {
978
+ if (!drafts(address.column)) return { model };
979
+ const open = Option.filter(model.editing, (editing) => sameCell(editing.address, address));
980
+ const typed = (draft) => Option.match(optionsOf(address.column), {
981
+ onNone: () => draft + text,
982
+ onSome: (choices) => typedOption(choices, draft, text)
983
+ });
984
+ const editing = Option.match(open, {
985
+ onNone: () => ({
986
+ address,
987
+ from,
988
+ draft: Option.isSome(optionsOf(address.column)) ? typed(from) : text,
989
+ error: Option.none()
990
+ }),
991
+ onSome: (edit) => ({
992
+ ...edit,
993
+ draft: typed(edit.draft),
791
994
  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
995
  })
807
- }),
808
- EditChosen: ({ draft }) => Option.match(model.editing, {
996
+ });
997
+ return { model: modifyFields(clearCells(focusTo(model, address)), { editing: () => Option.some(editing) }) };
998
+ },
999
+ EditChanged: ({ draft }) => ({ model: Option.match(model.editing, {
1000
+ onNone: () => model,
1001
+ onSome: (editing) => editing.draft === draft && Option.isNone(editing.error) ? model : modifyFields(model, { editing: () => Option.some({
1002
+ ...editing,
1003
+ draft,
1004
+ error: Option.none()
1005
+ }) })
1006
+ }) }),
1007
+ EditStepped: ({ by }) => Option.match(model.editing, {
1008
+ onNone: () => ({ model }),
1009
+ onSome: (editing) => Option.match(optionsOf(editing.address.column), {
809
1010
  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 } : {
1011
+ onSome: (choices) => {
1012
+ const draft = choices[steppedIndex(by, choices.indexOf(editing.draft), choices.length)];
1013
+ return draft === void 0 || draft === editing.draft ? { model } : { model: modifyFields(model, { editing: () => Option.some({
1014
+ ...editing,
1015
+ draft,
1016
+ error: Option.none()
1017
+ }) }) };
1018
+ }
1019
+ })
1020
+ }),
1021
+ EditChosen: ({ draft }) => Option.match(model.editing, {
1022
+ onNone: () => ({ model }),
1023
+ onSome: (editing) => committed(modifyFields(model, { editing: () => Option.some({
1024
+ ...editing,
1025
+ draft
1026
+ }) }), Option.none(), Option.none())
1027
+ }),
1028
+ EditCommitted: ({ next, reveal }) => committed(model, next, reveal),
1029
+ Pasted: ({ cells }) => {
1030
+ if (Option.isSome(model.editing) || cells.length === 0) return { model };
1031
+ const { accepted, refused } = judged(cells);
1032
+ return accepted.length === 0 && refused.length === 0 ? { model } : {
828
1033
  model,
829
- outMessage: Out.Filled({
830
- source: request.source,
831
- to: request.to
1034
+ outMessage: Out.Pasted({
1035
+ accepted,
1036
+ refused
832
1037
  })
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
- })), {
1038
+ };
1039
+ },
1040
+ FillRequested: (request) => Option.isSome(model.editing) ? { model } : {
1041
+ model,
1042
+ outMessage: Out.Filled({
1043
+ source: request.source,
1044
+ to: request.to
1045
+ })
1046
+ },
1047
+ FillStarted: () => {
1048
+ if (Option.isSome(model.editing)) return { model };
1049
+ const source = Option.orElse(model.selection.cells, () => Option.map(model.focus.current, (address) => ({
1050
+ anchor: address,
1051
+ focus: address
1052
+ })));
1053
+ return { model: Option.match(source, {
852
1054
  onNone: () => model,
853
- onSome: ({ filling, to }) => sameCell(filling.to, to) ? model : modifyFields(model, { filling: () => Option.some({
854
- ...filling,
855
- to
1055
+ onSome: (range) => modifyFields(model, { filling: () => Option.some({
1056
+ source: range,
1057
+ to: range.focus
856
1058
  }) })
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
- }
1059
+ }) };
1060
+ },
1061
+ FillDragged: ({ cell }) => ({ model: Option.match(Option.zipWith(model.filling, addressOf(cell), (filling, to) => ({
1062
+ filling,
1063
+ to
1064
+ })), {
1065
+ onNone: () => model,
1066
+ onSome: ({ filling, to }) => sameCell(filling.to, to) ? model : modifyFields(model, { filling: () => Option.some({
1067
+ ...filling,
1068
+ to
888
1069
  }) })
889
- })
1070
+ }) }),
1071
+ FillEnded: ({ completed }) => Option.match(model.filling, {
1072
+ onNone: () => ({ model }),
1073
+ onSome: ({ source, to }) => {
1074
+ const ended = modifyFields(model, { filling: () => Option.none() });
1075
+ return completed ? {
1076
+ model: ended,
1077
+ outMessage: Out.Filled({
1078
+ source,
1079
+ to
1080
+ })
1081
+ } : { model: ended };
1082
+ }
1083
+ }),
1084
+ UndoRequested: () => Option.isSome(model.editing) ? { model } : {
1085
+ model,
1086
+ outMessage: Out.UndoRequested()
1087
+ },
1088
+ RedoRequested: () => Option.isSome(model.editing) ? { model } : {
1089
+ model,
1090
+ outMessage: Out.RedoRequested()
1091
+ },
1092
+ EditCancelled: () => ({ model: Option.isNone(model.editing) ? model : modifyFields(model, { editing: () => Option.none() }) }),
1093
+ CellsCleared: () => ({ model: clearCells(model) }),
1094
+ MountStarted: () => ({ model }),
1095
+ FilterOpened: ({ column, drawn }) => {
1096
+ const kind = filterKinds.get(column);
1097
+ const accepted = Option.match(drawn, {
1098
+ onNone: () => true,
1099
+ onSome: (filter) => Schema.is(filterSchemas.get(column))(filter)
1100
+ });
1101
+ if (kind === void 0 || model.columns.hidden.includes(column) || !accepted) return { model };
1102
+ return afterEdit(model, (settled) => ({ model: modifyFields(settled, {
1103
+ focus: (current) => focus.bundle.update(current, focus.Message.HeaderFocused({ column }), void 0).model,
1104
+ menu: () => Option.none(),
1105
+ filtering: () => Option.some({
1106
+ column,
1107
+ form: formOf(drawn),
1108
+ reported: drawn,
1109
+ sent: [drawn],
1110
+ generation: 0
1111
+ })
1112
+ }) }));
1113
+ },
1114
+ FilterTyped: ({ field, text, stale }) => filterEdited(model, stale, (form, kind) => {
1115
+ return CellEditor.match(kind, {
1116
+ Text: () => field === "text",
1117
+ Number: () => field === "min" || field === "max",
1118
+ Date: () => field === "from" || field === "to",
1119
+ Choice: () => false,
1120
+ Checkbox: () => false
1121
+ }) ? Option.some({
1122
+ ...form,
1123
+ [field]: text
1124
+ }) : Option.none();
1125
+ }),
1126
+ FilterOptionToggled: ({ option, stale }) => filterEdited(model, stale, (form, kind) => Option.flatMap(Option.liftPredicate(kind, CellEditor.guards.Choice), ({ options: offered }) => {
1127
+ const ticked = new Set(form.options);
1128
+ const on = !ticked.has(option);
1129
+ return Option.some({
1130
+ ...form,
1131
+ options: offered.filter((each) => each === option ? on : ticked.has(each))
1132
+ });
1133
+ })),
1134
+ FilterChecked: ({ state, stale }) => filterEdited(model, stale, (form, kind) => CellEditor.guards.Checkbox(kind) ? Option.some({
1135
+ ...form,
1136
+ checked: state
1137
+ }) : Option.none()),
1138
+ FilterCleared: ({ stale }) => filterEdited(model, stale, () => Option.some(emptyForm)),
1139
+ FilterClosed: () => ({ model: Option.isNone(model.filtering) ? model : modifyFields(model, { filtering: () => Option.none() }) }),
1140
+ ResizeEnded: ({ completed }) => ({ model: Option.match(model.resizing, {
1141
+ onNone: () => model,
1142
+ onSome: ({ column, from }) => {
1143
+ const settled = completed ? model : columnsBy(model, columnState.resize(model.columns, column, from));
1144
+ return modifyFields(settled, { resizing: () => Option.none() });
1145
+ }
1146
+ }) })
890
1147
  });
891
1148
  const projections = /* @__PURE__ */ new WeakMap();
892
1149
  /** The text an edit of a cell begins from: its column's `draft`, or the text it shows. */
@@ -958,12 +1215,46 @@ const make = (options) => {
958
1215
  viewport: input.state.viewport,
959
1216
  ...input.overscan === void 0 ? {} : { overscan: input.overscan }
960
1217
  });
1218
+ /** A column's filter kind; none for a column that does not filter. */
1219
+ const filterFor = (column) => Option.fromUndefinedOr(filterKinds.get(column));
1220
+ /** Every filtering column none: the application's starting value. */
1221
+ const noFilters = Object.fromEntries(filterIds.map((id) => [id, Option.none()]));
1222
+ /** `filters` with one `Out.Filtered`'s change written in. */
1223
+ const setFilter = (filters, change) => ({
1224
+ ...filters,
1225
+ [change.column]: change.filter
1226
+ });
1227
+ /**
1228
+ * A predicate over held rows: a row is kept when every given filter keeps
1229
+ * the value `read` names for its column, the application's own field
1230
+ * (typed by the column's filter), never the text a cell shows. Build it
1231
+ * once per `filters`.
1232
+ */
1233
+ const keeps = (filters, read) => {
1234
+ const given = filters;
1235
+ const reads = read;
1236
+ const tests = filterIds.flatMap((id) => Option.match(given[id], {
1237
+ onNone: () => [],
1238
+ onSome: (filter) => {
1239
+ const test = testOf(filter);
1240
+ const at = reads[id];
1241
+ return [(row) => test(at(row))];
1242
+ }
1243
+ }));
1244
+ return (row) => tests.every((test) => test(row));
1245
+ };
961
1246
  return {
962
1247
  id: options.id,
1248
+ filterFor,
1249
+ Filters,
1250
+ noFilters,
1251
+ setFilter,
1252
+ keeps,
963
1253
  matchEdit,
964
1254
  editorFor,
965
1255
  MenuItem,
966
1256
  menuItems,
1257
+ Toggle,
967
1258
  columns: options.columns,
968
1259
  rowSelection: options.rowSelection,
969
1260
  cellSelection: options.cellSelection === true,
@@ -974,7 +1265,7 @@ const make = (options) => {
974
1265
  fill,
975
1266
  extendRows,
976
1267
  draftOf,
977
- Out,
1268
+ Out: filterIds.length === 0 ? Unfiltered : Out,
978
1269
  Model,
979
1270
  Message,
980
1271
  bundle