@dxos/react-ui-table 0.7.5-main.499c70c → 0.7.5-main.937ce75

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.
@@ -37,7 +37,7 @@ var translations_default = [
37
37
 
38
38
  // packages/ui/react-ui-table/src/components/Table/Table.tsx
39
39
  import React7, { forwardRef, useCallback as useCallback4, useImperativeHandle, useState as useState2, useMemo as useMemo4 } from "react";
40
- import { getValue as getValue3 } from "@dxos/echo-schema";
40
+ import { getValue as getValue4 } from "@dxos/echo-schema";
41
41
  import { invariant as invariant3 } from "@dxos/invariant";
42
42
  import { Filter, getSpace as getSpace4, fullyQualifiedId } from "@dxos/react-client/echo";
43
43
  import { useTranslation as useTranslation3 } from "@dxos/react-ui";
@@ -165,7 +165,9 @@ var CreateRefPanel = ({ model, modals, __gridScope }) => {
165
165
  if (!space || state?.type !== "createRefPanel") {
166
166
  return;
167
167
  }
168
- const schema2 = space.db.schemaRegistry.getSchema(state.typename)?.schema;
168
+ const [schema2] = space.db.schemaRegistry.query({
169
+ typename: state.typename
170
+ }).runSync();
169
171
  if (schema2) {
170
172
  const omit = S.omit("id");
171
173
  return omit(schema2);
@@ -178,7 +180,9 @@ var CreateRefPanel = ({ model, modals, __gridScope }) => {
178
180
  if (!model || !space || state?.type !== "createRefPanel") {
179
181
  return;
180
182
  }
181
- const schema2 = space.db.schemaRegistry.getSchema(state.typename)?.schema;
183
+ const [schema2] = space.db.schemaRegistry.query({
184
+ typename: state.typename
185
+ }).runSync();
182
186
  if (schema2) {
183
187
  const obj = space.db.add(create(schema2, values));
184
188
  state.onCreate?.(obj);
@@ -282,14 +286,13 @@ var RowActionsMenu = ({ model, modals }) => {
282
286
  };
283
287
 
284
288
  // packages/ui/react-ui-table/src/model/table-model.ts
285
- import { computed as computed2, effect as effect2, signal as signal2 } from "@preact/signals-core";
286
- import orderBy from "lodash.orderby";
289
+ import { computed as computed3, effect as effect2, signal as signal3 } from "@preact/signals-core";
287
290
  import { Resource as Resource2 } from "@dxos/context";
288
- import { getValue, setValue, FormatEnum as FormatEnum2 } from "@dxos/echo-schema";
291
+ import { getValue as getValue2, setValue, FormatEnum as FormatEnum3 } from "@dxos/echo-schema";
289
292
  import { invariant } from "@dxos/invariant";
290
293
  import { isReactiveObject, makeRef as makeRef2 } from "@dxos/live-object";
291
294
  import { PublicKey as PublicKey2 } from "@dxos/react-client";
292
- import { formatForDisplay, formatForEditing, parseValue } from "@dxos/react-ui-form";
295
+ import { formatForEditing, parseValue } from "@dxos/react-ui-form";
293
296
 
294
297
  // packages/ui/react-ui-table/src/model/selection-model.ts
295
298
  import { computed, effect, signal } from "@preact/signals-core";
@@ -593,6 +596,99 @@ var SelectionModel = class extends Resource {
593
596
  }
594
597
  };
595
598
 
599
+ // packages/ui/react-ui-table/src/model/table-sorting.ts
600
+ import { computed as computed2, signal as signal2 } from "@preact/signals-core";
601
+ import orderBy from "lodash.orderby";
602
+ import { getValue, FormatEnum as FormatEnum2, TypeEnum as TypeEnum2 } from "@dxos/echo-schema";
603
+ import { formatForDisplay } from "@dxos/react-ui-form";
604
+ var TableSorting = class {
605
+ constructor(rows, _table, _projection) {
606
+ this._table = _table;
607
+ this._projection = _projection;
608
+ this._displayToDataIndex = /* @__PURE__ */ new Map();
609
+ this._sorting = signal2(void 0);
610
+ this._rows = rows;
611
+ this.sortedRows = this.initialiseSortedRows();
612
+ }
613
+ /**
614
+ * @reactive
615
+ */
616
+ get sorting() {
617
+ return this._sorting.value;
618
+ }
619
+ getDataIndex(displayIndex) {
620
+ return this._displayToDataIndex.get(displayIndex) ?? displayIndex;
621
+ }
622
+ setSort(fieldId, direction) {
623
+ this._sorting.value = {
624
+ fieldId,
625
+ direction
626
+ };
627
+ }
628
+ //
629
+ // Initialisation.
630
+ //
631
+ initialiseSortedRows() {
632
+ return computed2(() => {
633
+ this._displayToDataIndex.clear();
634
+ const sort = this._sorting.value;
635
+ if (!sort) {
636
+ return this._rows.value;
637
+ }
638
+ const field = this._table.view?.target?.fields.find((f) => f.id === sort.fieldId);
639
+ if (!field) {
640
+ return this._rows.value;
641
+ }
642
+ const { props } = this._projection.getFieldProjection(field.id);
643
+ const dataWithIndices = this._rows.value.map((item, index) => {
644
+ const sortValue = this.getSortValue(props, field, item);
645
+ const isEmpty = !(props.type === TypeEnum2.Boolean) && (sortValue == null || sortValue === "");
646
+ return {
647
+ item,
648
+ index,
649
+ value: sortValue,
650
+ isEmpty
651
+ };
652
+ });
653
+ const ordered = orderBy(dataWithIndices, [
654
+ "isEmpty",
655
+ "value"
656
+ ], [
657
+ "asc",
658
+ sort.direction
659
+ ]);
660
+ ordered.forEach(({ index: dataIndex }, displayIndex) => {
661
+ if (displayIndex !== dataIndex) {
662
+ this._displayToDataIndex.set(displayIndex, dataIndex);
663
+ }
664
+ });
665
+ return ordered.map(({ item }) => item);
666
+ });
667
+ }
668
+ getSortValue(props, field, item) {
669
+ const { type, format } = props;
670
+ const value = getValue(item, field.path);
671
+ if (type === TypeEnum2.Boolean) {
672
+ return !!value;
673
+ }
674
+ if (type === TypeEnum2.Number) {
675
+ return value;
676
+ }
677
+ if (value == null) {
678
+ return value;
679
+ }
680
+ if (format === FormatEnum2.Ref && field.referencePath) {
681
+ const refValue = getValue(value.target, field.referencePath);
682
+ return refValue;
683
+ }
684
+ return formatForDisplay({
685
+ type,
686
+ format,
687
+ value
688
+ });
689
+ }
690
+ };
691
+
596
692
  // packages/ui/react-ui-table/src/model/table-model.ts
597
693
  var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-table/src/model/table-model.ts";
598
694
  var TableModel = class extends Resource2 {
@@ -602,7 +698,7 @@ var TableModel = class extends Resource2 {
602
698
  }, onCellUpdate, onDeleteColumn, onDeleteRows, onInsertRow, onRowOrderChanged }) {
603
699
  super();
604
700
  this.id = `table-model-${PublicKey2.random().truncate()}`;
605
- this._visibleRange = signal2({
701
+ this._visibleRange = signal3({
606
702
  start: {
607
703
  row: 0,
608
704
  col: 0
@@ -612,14 +708,7 @@ var TableModel = class extends Resource2 {
612
708
  col: 0
613
709
  }
614
710
  });
615
- /**
616
- * Maps display indices to data indices.
617
- * Used for translating between sorted/displayed order and original data order.
618
- * Keys are display indices, values are corresponding data indices.
619
- */
620
- this._displayToDataIndex = /* @__PURE__ */ new Map();
621
- this._sorting = signal2(void 0);
622
- this._rows = signal2([]);
711
+ this._rows = signal3([]);
623
712
  //
624
713
  // Data
625
714
  //
@@ -629,11 +718,11 @@ var TableModel = class extends Resource2 {
629
718
  this.getRowCount = () => this._rows.value.length;
630
719
  this.getColumnCount = () => this.table.view?.target?.fields.length ?? 0;
631
720
  this.insertRow = (rowIndex) => {
632
- const row = rowIndex !== void 0 ? this._displayToDataIndex.get(rowIndex) ?? rowIndex : this._rows.value.length;
721
+ const row = rowIndex !== void 0 ? this._sorting.getDataIndex(rowIndex) : this._rows.value.length;
633
722
  this._onInsertRow?.(row);
634
723
  };
635
724
  this.deleteRow = (rowIndex) => {
636
- const row = this._displayToDataIndex.get(rowIndex) ?? rowIndex;
725
+ const row = this._sorting.getDataIndex(rowIndex);
637
726
  const obj = this._rows.value[row];
638
727
  const objectsToDelete = [];
639
728
  if (this._selection.hasSelection.value) {
@@ -650,18 +739,18 @@ var TableModel = class extends Resource2 {
650
739
  return void 0;
651
740
  }
652
741
  const field = fields[col];
653
- const dataIndex = this._displayToDataIndex.get(row) ?? row;
654
- const value = getValue(this._rows.value[dataIndex], field.path);
742
+ const dataIndex = this._sorting.getDataIndex(row);
743
+ const value = getValue2(this._rows.value[dataIndex], field.path);
655
744
  if (value == null) {
656
745
  return "";
657
746
  }
658
747
  const { props } = this._projection.getFieldProjection(field.id);
659
748
  switch (props.format) {
660
- case FormatEnum2.Ref: {
749
+ case FormatEnum3.Ref: {
661
750
  if (!field.referencePath) {
662
751
  return "";
663
752
  }
664
- return getValue(value.target, field.referencePath);
753
+ return getValue2(value.target, field.referencePath);
665
754
  }
666
755
  default: {
667
756
  return formatForEditing({
@@ -673,7 +762,7 @@ var TableModel = class extends Resource2 {
673
762
  }
674
763
  };
675
764
  this.setCellData = ({ col, row }, value) => {
676
- const rowIdx = this._displayToDataIndex.get(row) ?? row;
765
+ const rowIdx = this._sorting.getDataIndex(row);
677
766
  const fields = this.table.view?.target?.fields ?? [];
678
767
  if (col < 0 || col >= fields.length) {
679
768
  return;
@@ -681,7 +770,7 @@ var TableModel = class extends Resource2 {
681
770
  const field = fields[col];
682
771
  const { props } = this._projection.getFieldProjection(field.id);
683
772
  switch (props.format) {
684
- case FormatEnum2.Ref: {
773
+ case FormatEnum3.Ref: {
685
774
  if (isReactiveObject(value)) {
686
775
  setValue(this._rows.value[rowIdx], field.path, makeRef2(value));
687
776
  }
@@ -699,7 +788,11 @@ var TableModel = class extends Resource2 {
699
788
  };
700
789
  this._table = table;
701
790
  this._projection = projection;
702
- this._sorting.value = sorting.at(0);
791
+ this._sorting = new TableSorting(this._rows, table, projection);
792
+ if (sorting.length > 0) {
793
+ const [sort] = sorting;
794
+ this._sorting.setSort(sort.fieldId, sort.direction);
795
+ }
703
796
  this._pinnedRows = pinnedRows;
704
797
  this._onInsertRow = onInsertRow;
705
798
  this._onDeleteRows = onDeleteRows;
@@ -714,7 +807,13 @@ var TableModel = class extends Resource2 {
714
807
  return this._projection;
715
808
  }
716
809
  get rows() {
717
- return this._sortedRows;
810
+ return this._sorting.sortedRows;
811
+ }
812
+ /**
813
+ * @reactive
814
+ */
815
+ get sorting() {
816
+ return this._sorting.sorting;
718
817
  }
719
818
  get pinnedRows() {
720
819
  return this._pinnedRows;
@@ -731,9 +830,6 @@ var TableModel = class extends Resource2 {
731
830
  });
732
831
  return this._columnMeta;
733
832
  }
734
- get sorting() {
735
- return this._sorting.value;
736
- }
737
833
  get selection() {
738
834
  return this._selection;
739
835
  }
@@ -742,13 +838,12 @@ var TableModel = class extends Resource2 {
742
838
  //
743
839
  async _open() {
744
840
  this.initializeColumnMeta();
745
- this.initializeSorting();
746
841
  this.initializeEffects();
747
- this._selection = new SelectionModel(this._sortedRows, () => this._onRowOrderChanged?.());
842
+ this._selection = new SelectionModel(this._sorting.sortedRows, () => this._onRowOrderChanged?.());
748
843
  await this._selection.open(this._ctx);
749
844
  }
750
845
  initializeColumnMeta() {
751
- this._columnMeta = computed2(() => {
846
+ this._columnMeta = computed3(() => {
752
847
  const fields = this._table.view?.target?.fields ?? [];
753
848
  const meta = Object.fromEntries(fields.map((field, index) => [
754
849
  index,
@@ -774,60 +869,9 @@ var TableModel = class extends Resource2 {
774
869
  };
775
870
  });
776
871
  }
777
- initializeSorting() {
778
- this._sortedRows = computed2(() => {
779
- this._displayToDataIndex.clear();
780
- const sort = this._sorting.value;
781
- if (!sort) {
782
- return this._rows.value;
783
- }
784
- const field = this._table.view?.target?.fields.find((field2) => field2.id === sort.fieldId);
785
- if (!field) {
786
- return this._rows.value;
787
- }
788
- const { props } = this._projection.getFieldProjection(field.id);
789
- const dataWithIndices = this._rows.value.map((item, index) => {
790
- const rawValue = getValue(item, field.path);
791
- let sortValue = rawValue;
792
- if (props.type.startsWith("boolean")) {
793
- sortValue = !!rawValue;
794
- } else if (rawValue != null) {
795
- if (props.format === FormatEnum2.Ref && field.referencePath) {
796
- sortValue = getValue(rawValue, field.referencePath);
797
- } else {
798
- sortValue = formatForDisplay({
799
- type: props.type,
800
- format: props.format,
801
- value: rawValue
802
- });
803
- }
804
- }
805
- return {
806
- item,
807
- index,
808
- value: sortValue,
809
- isEmpty: !props.type.startsWith("boolean") && (sortValue == null || sortValue === "")
810
- };
811
- });
812
- const sorted = orderBy(dataWithIndices, [
813
- "isEmpty",
814
- "value"
815
- ], [
816
- "asc",
817
- sort.direction
818
- ]);
819
- for (let displayIndex = 0; displayIndex < sorted.length; displayIndex++) {
820
- const { index: dataIndex } = sorted[displayIndex];
821
- if (displayIndex !== dataIndex) {
822
- this._displayToDataIndex.set(displayIndex, dataIndex);
823
- }
824
- }
825
- return sorted.map(({ item }) => item);
826
- });
827
- }
828
872
  initializeEffects() {
829
873
  const rowOrderWatcher = effect2(() => {
830
- touch(this._sortedRows.value);
874
+ touch(this._sorting.sortedRows.value);
831
875
  this._onRowOrderChanged?.();
832
876
  });
833
877
  this._ctx.onDispose(rowOrderWatcher);
@@ -836,8 +880,8 @@ var TableModel = class extends Resource2 {
836
880
  const rowEffects = [];
837
881
  for (let row = start.row; row <= end.row; row++) {
838
882
  rowEffects.push(effect2(() => {
839
- const obj = this._sortedRows.value[row];
840
- this?._table?.view?.target?.fields.forEach((field) => touch(getValue(obj, field.path)));
883
+ const obj = this._sorting.sortedRows.value[row];
884
+ this?._table?.view?.target?.fields.forEach((field) => touch(getValue2(obj, field.path)));
841
885
  this._onCellUpdate?.({
842
886
  row,
843
887
  col: start.col,
@@ -855,10 +899,10 @@ var TableModel = class extends Resource2 {
855
899
  * @param {(value: any) => any} update - A function that takes the current value and returns the updated value.
856
900
  */
857
901
  updateCellData({ col, row }, update) {
858
- const dataRow = this._displayToDataIndex.get(row) ?? row;
902
+ const dataRow = this._sorting.getDataIndex(row);
859
903
  const fields = this.table.view?.target?.fields ?? [];
860
904
  const field = fields[col];
861
- const value = getValue(this._rows.value[dataRow], field.path);
905
+ const value = getValue2(this._rows.value[dataRow], field.path);
862
906
  const updatedValue = update(value);
863
907
  setValue(this._rows.value[dataRow], field.path, updatedValue);
864
908
  }
@@ -871,9 +915,6 @@ var TableModel = class extends Resource2 {
871
915
  }
872
916
  const field = this.table.view.target?.fields.find((field2) => field2.id === fieldId);
873
917
  if (field && this._onDeleteColumn) {
874
- if (this._sorting.value?.fieldId === fieldId) {
875
- this.clearSort();
876
- }
877
918
  this._onDeleteColumn(field.id);
878
919
  }
879
920
  }
@@ -894,13 +935,10 @@ var TableModel = class extends Resource2 {
894
935
  // Sorting
895
936
  //
896
937
  setSort(fieldId, direction) {
897
- this._sorting.value = {
898
- fieldId,
899
- direction
900
- };
938
+ this._sorting.setSort(fieldId, direction);
901
939
  }
902
940
  clearSort() {
903
- this._sorting.value = void 0;
941
+ this._sorting.setSort("", "asc");
904
942
  }
905
943
  //
906
944
  // Pinning
@@ -916,12 +954,12 @@ var TableModel = class extends Resource2 {
916
954
  };
917
955
 
918
956
  // packages/ui/react-ui-table/src/model/modal-controller.ts
919
- import { signal as signal3 } from "@preact/signals-core";
957
+ import { signal as signal4 } from "@preact/signals-core";
920
958
  import { log as log2 } from "@dxos/log";
921
959
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-table/src/model/modal-controller.ts";
922
960
  var ModalController = class {
923
961
  constructor() {
924
- this._state = signal3({
962
+ this._state = signal4({
925
963
  type: "closed"
926
964
  });
927
965
  this._triggerRef = {
@@ -1004,14 +1042,14 @@ var ModalController = class {
1004
1042
  };
1005
1043
 
1006
1044
  // packages/ui/react-ui-table/src/model/table-presentation.ts
1007
- import { signal as signal4 } from "@preact/signals-core";
1008
- import { FormatEnum as FormatEnum3, getValue as getValue2 } from "@dxos/echo-schema";
1045
+ import { signal as signal5 } from "@preact/signals-core";
1046
+ import { FormatEnum as FormatEnum4, getValue as getValue3 } from "@dxos/echo-schema";
1009
1047
  import { cellClassesForFieldType, cellClassesForRowSelection, formatForDisplay as formatForDisplay2 } from "@dxos/react-ui-form";
1010
1048
  import { toPlaneCellIndex } from "@dxos/react-ui-grid";
1011
1049
  import { mx } from "@dxos/react-ui-theme";
1012
1050
  import { VIEW_FIELD_LIMIT } from "@dxos/schema";
1013
1051
  var TablePresentation = class {
1014
- constructor(model, _visibleRange = signal4({
1052
+ constructor(model, _visibleRange = signal5({
1015
1053
  start: {
1016
1054
  row: 0,
1017
1055
  col: 0
@@ -1050,19 +1088,19 @@ var TablePresentation = class {
1050
1088
  const { props } = this.model.projection.getFieldProjection(field.id);
1051
1089
  const cell = {
1052
1090
  get value() {
1053
- const value = getValue2(obj, field.path);
1091
+ const value = getValue3(obj, field.path);
1054
1092
  if (value == null) {
1055
1093
  return "";
1056
1094
  }
1057
1095
  switch (props.format) {
1058
- case FormatEnum3.Boolean: {
1096
+ case FormatEnum4.Boolean: {
1059
1097
  return "";
1060
1098
  }
1061
- case FormatEnum3.Ref: {
1099
+ case FormatEnum4.Ref: {
1062
1100
  if (!field.referencePath) {
1063
1101
  return "";
1064
1102
  }
1065
- return getValue2(value.target, field.referencePath);
1103
+ return getValue3(value.target, field.referencePath);
1066
1104
  }
1067
1105
  default: {
1068
1106
  return formatForDisplay2({
@@ -1089,8 +1127,8 @@ var TablePresentation = class {
1089
1127
  if (classes.length > 0) {
1090
1128
  cell.className = mx(classes.flat());
1091
1129
  }
1092
- if (props.format === FormatEnum3.Ref && props.referenceSchema) {
1093
- const targetObj = getValue2(obj, field.path)?.target;
1130
+ if (props.format === FormatEnum4.Ref && props.referenceSchema) {
1131
+ const targetObj = getValue3(obj, field.path)?.target;
1094
1132
  if (targetObj) {
1095
1133
  cell.accessoryHtml = tableButtons.referencedCell.render({
1096
1134
  targetId: targetObj.id,
@@ -1098,8 +1136,8 @@ var TablePresentation = class {
1098
1136
  });
1099
1137
  }
1100
1138
  }
1101
- if (props.format === FormatEnum3.Boolean) {
1102
- const value = getValue2(obj, field.path);
1139
+ if (props.format === FormatEnum4.Boolean) {
1140
+ const value = getValue3(obj, field.path);
1103
1141
  cell.accessoryHtml = tableControls.switch.render({
1104
1142
  colIndex,
1105
1143
  rowIndex: displayIndex,
@@ -1139,7 +1177,8 @@ var TablePresentation = class {
1139
1177
  resizeHandle: "col",
1140
1178
  accessoryHtml: tableButtons.columnSettings.render({
1141
1179
  fieldId: field.id
1142
- })
1180
+ }),
1181
+ className: "!bg-gridHeader"
1143
1182
  };
1144
1183
  }
1145
1184
  return cells;
@@ -1189,13 +1228,14 @@ var TablePresentation = class {
1189
1228
  col: 0,
1190
1229
  row: 0
1191
1230
  })]: {
1192
- value: "",
1193
1231
  accessoryHtml: tableControls.checkbox.render({
1194
1232
  rowIndex: 0,
1195
1233
  header: true,
1196
1234
  checked: this.model.selection.allRowsSeleted.value
1197
1235
  }),
1198
- readonly: true
1236
+ className: "!bg-gridHeader",
1237
+ readonly: true,
1238
+ value: ""
1199
1239
  }
1200
1240
  };
1201
1241
  }
@@ -1205,11 +1245,12 @@ var TablePresentation = class {
1205
1245
  col: 0,
1206
1246
  row: 0
1207
1247
  })]: {
1208
- value: "",
1209
1248
  accessoryHtml: tableButtons.addColumn.render({
1210
1249
  disabled: (this.model.table.view?.target?.fields?.length ?? 0) >= VIEW_FIELD_LIMIT
1211
1250
  }),
1212
- readonly: true
1251
+ className: "!bg-gridHeader",
1252
+ readonly: true,
1253
+ value: ""
1213
1254
  }
1214
1255
  };
1215
1256
  }
@@ -1217,7 +1258,7 @@ var TablePresentation = class {
1217
1258
 
1218
1259
  // packages/ui/react-ui-table/src/components/TableCellEditor/TableCellEditor.tsx
1219
1260
  import React6, { useCallback as useCallback3, useMemo as useMemo3, useRef } from "react";
1220
- import { FormatEnum as FormatEnum4 } from "@dxos/echo-schema";
1261
+ import { FormatEnum as FormatEnum5 } from "@dxos/echo-schema";
1221
1262
  import { invariant as invariant2 } from "@dxos/invariant";
1222
1263
  import { cellQuery, editorKeys, parseCellIndex, useGridContext as useGridContext2, GridCellEditor } from "@dxos/react-ui-grid";
1223
1264
 
@@ -1353,7 +1394,7 @@ var TableCellEditor = ({ model, modals, onEnter, onFocus, onQuery, __gridScope }
1353
1394
  ];
1354
1395
  if (onQuery) {
1355
1396
  switch (fieldProjection.props.format) {
1356
- case FormatEnum4.Ref: {
1397
+ case FormatEnum5.Ref: {
1357
1398
  extension2.push([
1358
1399
  completion({
1359
1400
  onQuery: (text) => onQuery(fieldProjection, text),
@@ -1609,7 +1650,7 @@ var TableMain = /* @__PURE__ */ forwardRef(({ model, presentation, ignoreAttenti
1609
1650
  if (schema) {
1610
1651
  const { objects } = await space.db.query(Filter.schema(schema)).run();
1611
1652
  const options = objects.map((obj) => {
1612
- const value = getValue3(obj, field.referencePath);
1653
+ const value = getValue4(obj, field.referencePath);
1613
1654
  if (!value || typeof value !== "string") {
1614
1655
  return void 0;
1615
1656
  }