@dxos/react-ui-table 0.8.4-main.ead640a → 0.8.4-main.effb148878

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.
Files changed (169) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  3. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  4. package/dist/lib/browser/chunk-KC62K4WS.mjs +1696 -0
  5. package/dist/lib/browser/chunk-KC62K4WS.mjs.map +7 -0
  6. package/dist/lib/browser/chunk-VWQWF3CP.mjs +67 -0
  7. package/dist/lib/browser/chunk-VWQWF3CP.mjs.map +7 -0
  8. package/dist/lib/browser/index.mjs +1128 -1044
  9. package/dist/lib/browser/index.mjs.map +4 -4
  10. package/dist/lib/browser/meta.json +1 -1
  11. package/dist/lib/browser/testing/index.mjs +35 -46
  12. package/dist/lib/browser/testing/index.mjs.map +3 -3
  13. package/dist/lib/browser/translations.mjs +36 -0
  14. package/dist/lib/browser/translations.mjs.map +7 -0
  15. package/dist/lib/browser/types/index.mjs +4 -3
  16. package/dist/lib/node-esm/chunk-62FOU4M7.mjs +1698 -0
  17. package/dist/lib/node-esm/chunk-62FOU4M7.mjs.map +7 -0
  18. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  19. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
  20. package/dist/lib/node-esm/chunk-UBXOF64L.mjs +68 -0
  21. package/dist/lib/node-esm/chunk-UBXOF64L.mjs.map +7 -0
  22. package/dist/lib/node-esm/index.mjs +1128 -1044
  23. package/dist/lib/node-esm/index.mjs.map +4 -4
  24. package/dist/lib/node-esm/meta.json +1 -1
  25. package/dist/lib/node-esm/testing/index.mjs +35 -46
  26. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  27. package/dist/lib/node-esm/translations.mjs +37 -0
  28. package/dist/lib/node-esm/translations.mjs.map +7 -0
  29. package/dist/lib/node-esm/types/index.mjs +4 -3
  30. package/dist/types/src/components/Table/ColumnActionsMenu.d.ts +1 -2
  31. package/dist/types/src/components/Table/ColumnActionsMenu.d.ts.map +1 -1
  32. package/dist/types/src/components/Table/ColumnSettings.d.ts +2 -2
  33. package/dist/types/src/components/Table/ColumnSettings.d.ts.map +1 -1
  34. package/dist/types/src/components/Table/DynamicTable.d.ts +6 -7
  35. package/dist/types/src/components/Table/DynamicTable.d.ts.map +1 -1
  36. package/dist/types/src/components/Table/DynamicTable.stories.d.ts +22 -37
  37. package/dist/types/src/components/Table/DynamicTable.stories.d.ts.map +1 -1
  38. package/dist/types/src/components/Table/Relations.stories.d.ts +85 -81
  39. package/dist/types/src/components/Table/Relations.stories.d.ts.map +1 -1
  40. package/dist/types/src/components/Table/RowActionsMenu.d.ts.map +1 -1
  41. package/dist/types/src/components/Table/Table.d.ts +20 -21
  42. package/dist/types/src/components/Table/Table.d.ts.map +1 -1
  43. package/dist/types/src/components/Table/Table.stories.d.ts +93 -26
  44. package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
  45. package/dist/types/src/components/Table/TableContent.d.ts +18 -0
  46. package/dist/types/src/components/Table/TableContent.d.ts.map +1 -0
  47. package/dist/types/src/components/Table/TableToolbar.d.ts +21 -0
  48. package/dist/types/src/components/Table/TableToolbar.d.ts.map +1 -0
  49. package/dist/types/src/components/TableCellEditor/CellValidationMessage.d.ts +1 -1
  50. package/dist/types/src/components/TableCellEditor/CellValidationMessage.d.ts.map +1 -1
  51. package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts +10 -12
  52. package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts.map +1 -1
  53. package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts +5 -7
  54. package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts.map +1 -1
  55. package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts +24 -26
  56. package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts.map +1 -1
  57. package/dist/types/src/components/index.d.ts +0 -1
  58. package/dist/types/src/components/index.d.ts.map +1 -1
  59. package/dist/types/src/components/testing.d.ts +22 -15
  60. package/dist/types/src/components/testing.d.ts.map +1 -1
  61. package/dist/types/src/hooks/index.d.ts +2 -1
  62. package/dist/types/src/hooks/index.d.ts.map +1 -1
  63. package/dist/types/src/hooks/useAddRow.d.ts +5 -6
  64. package/dist/types/src/hooks/useAddRow.d.ts.map +1 -1
  65. package/dist/types/src/hooks/useProjectionModel.d.ts +6 -0
  66. package/dist/types/src/hooks/useProjectionModel.d.ts.map +1 -0
  67. package/dist/types/src/hooks/useTableModel.d.ts +9 -9
  68. package/dist/types/src/hooks/useTableModel.d.ts.map +1 -1
  69. package/dist/types/src/index.d.ts +0 -2
  70. package/dist/types/src/index.d.ts.map +1 -1
  71. package/dist/types/src/model/modal-controller.d.ts +5 -1
  72. package/dist/types/src/model/modal-controller.d.ts.map +1 -1
  73. package/dist/types/src/model/selection-model.d.ts +18 -9
  74. package/dist/types/src/model/selection-model.d.ts.map +1 -1
  75. package/dist/types/src/model/table-model.d.ts +104 -25
  76. package/dist/types/src/model/table-model.d.ts.map +1 -1
  77. package/dist/types/src/model/table-presentation.d.ts +3 -1
  78. package/dist/types/src/model/table-presentation.d.ts.map +1 -1
  79. package/dist/types/src/playwright/TableManager.d.ts +2 -1
  80. package/dist/types/src/playwright/TableManager.d.ts.map +1 -1
  81. package/dist/types/src/playwright/playwright.config.d.ts.map +1 -1
  82. package/dist/types/src/testing/useTestTableModel.d.ts +18 -15
  83. package/dist/types/src/testing/useTestTableModel.d.ts.map +1 -1
  84. package/dist/types/src/translations.d.ts +21 -23
  85. package/dist/types/src/translations.d.ts.map +1 -1
  86. package/dist/types/src/types/Table.d.ts +32 -0
  87. package/dist/types/src/types/Table.d.ts.map +1 -0
  88. package/dist/types/src/types/index.d.ts +1 -1
  89. package/dist/types/src/types/index.d.ts.map +1 -1
  90. package/dist/types/src/util/dynamic-table.d.ts +14 -11
  91. package/dist/types/src/util/dynamic-table.d.ts.map +1 -1
  92. package/dist/types/src/util/dynamic-table.test.d.ts +2 -0
  93. package/dist/types/src/util/dynamic-table.test.d.ts.map +1 -0
  94. package/dist/types/src/util/index.d.ts +2 -0
  95. package/dist/types/src/util/index.d.ts.map +1 -1
  96. package/dist/types/src/util/query-ast.d.ts +7 -0
  97. package/dist/types/src/util/query-ast.d.ts.map +1 -0
  98. package/dist/types/src/util/schema.d.ts.map +1 -1
  99. package/dist/types/src/util/signals.d.ts.map +1 -1
  100. package/dist/types/src/util/sort.d.ts +12 -0
  101. package/dist/types/src/util/sort.d.ts.map +1 -0
  102. package/dist/types/src/util/table-buttons.d.ts +20 -20
  103. package/dist/types/src/util/table-buttons.d.ts.map +1 -1
  104. package/dist/types/src/util/table-controls.d.ts +4 -4
  105. package/dist/types/src/util/table-controls.d.ts.map +1 -1
  106. package/dist/types/src/util/tag.d.ts.map +1 -1
  107. package/dist/types/tsconfig.tsbuildinfo +1 -1
  108. package/package.json +69 -78
  109. package/src/components/Table/ColumnActionsMenu.tsx +17 -12
  110. package/src/components/Table/ColumnSettings.tsx +8 -9
  111. package/src/components/Table/DynamicTable.stories.tsx +32 -32
  112. package/src/components/Table/DynamicTable.tsx +32 -33
  113. package/src/components/Table/Relations.stories.tsx +116 -82
  114. package/src/components/Table/RowActionsMenu.tsx +27 -17
  115. package/src/components/Table/Table.stories.tsx +129 -116
  116. package/src/components/Table/Table.tsx +43 -407
  117. package/src/components/Table/TableContent.tsx +426 -0
  118. package/src/components/Table/TableToolbar.tsx +111 -0
  119. package/src/components/TableCellEditor/CellValidationMessage.tsx +5 -6
  120. package/src/components/TableCellEditor/FormCellEditor.tsx +119 -135
  121. package/src/components/TableCellEditor/TableCellEditor.stories.tsx +29 -22
  122. package/src/components/TableCellEditor/TableCellEditor.tsx +35 -33
  123. package/src/components/index.ts +0 -1
  124. package/src/components/testing.ts +38 -17
  125. package/src/hooks/index.ts +2 -1
  126. package/src/hooks/useAddRow.ts +8 -10
  127. package/src/hooks/useProjectionModel.ts +38 -0
  128. package/src/hooks/useTableModel.ts +36 -34
  129. package/src/index.ts +0 -4
  130. package/src/model/modal-controller.ts +23 -12
  131. package/src/model/selection-model.ts +75 -44
  132. package/src/model/table-model.test.ts +77 -55
  133. package/src/model/table-model.ts +437 -171
  134. package/src/model/table-presentation.test.ts +54 -50
  135. package/src/model/table-presentation.ts +44 -40
  136. package/src/playwright/TableManager.ts +13 -6
  137. package/src/playwright/playwright.config.ts +1 -1
  138. package/src/playwright/smoke.spec.ts +59 -13
  139. package/src/testing/useTestTableModel.ts +49 -43
  140. package/src/translations.ts +20 -22
  141. package/src/types/Table.ts +81 -0
  142. package/src/types/index.ts +1 -1
  143. package/src/util/dynamic-table.test.ts +63 -0
  144. package/src/util/dynamic-table.ts +49 -35
  145. package/src/util/index.ts +2 -0
  146. package/src/util/query-ast.ts +22 -0
  147. package/src/util/sort.ts +90 -0
  148. package/src/util/table-buttons.ts +1 -1
  149. package/src/util/table-controls.ts +2 -2
  150. package/dist/lib/browser/chunk-4K7JPALT.mjs +0 -66
  151. package/dist/lib/browser/chunk-4K7JPALT.mjs.map +0 -7
  152. package/dist/lib/browser/chunk-OZTXLZGM.mjs +0 -1804
  153. package/dist/lib/browser/chunk-OZTXLZGM.mjs.map +0 -7
  154. package/dist/lib/node-esm/chunk-MK6OVUF6.mjs +0 -67
  155. package/dist/lib/node-esm/chunk-MK6OVUF6.mjs.map +0 -7
  156. package/dist/lib/node-esm/chunk-S56Z4V4X.mjs +0 -1805
  157. package/dist/lib/node-esm/chunk-S56Z4V4X.mjs.map +0 -7
  158. package/dist/types/src/components/TableToolbar/TableToolbar.d.ts +0 -21
  159. package/dist/types/src/components/TableToolbar/TableToolbar.d.ts.map +0 -1
  160. package/dist/types/src/components/TableToolbar/index.d.ts +0 -2
  161. package/dist/types/src/components/TableToolbar/index.d.ts.map +0 -1
  162. package/dist/types/src/model/table-sorting.d.ts +0 -52
  163. package/dist/types/src/model/table-sorting.d.ts.map +0 -1
  164. package/dist/types/src/types/schema.d.ts +0 -28
  165. package/dist/types/src/types/schema.d.ts.map +0 -1
  166. package/src/components/TableToolbar/TableToolbar.tsx +0 -102
  167. package/src/components/TableToolbar/index.ts +0 -5
  168. package/src/model/table-sorting.ts +0 -225
  169. package/src/types/schema.ts +0 -73
@@ -1,1804 +0,0 @@
1
- import {
2
- schema_exports
3
- } from "./chunk-4K7JPALT.mjs";
4
-
5
- // src/util/dynamic-table.ts
6
- import { Filter, Obj, Query } from "@dxos/echo";
7
- import { getTypename, toJsonSchema } from "@dxos/echo/internal";
8
- import { ProjectionModel, createView, getSchemaFromPropertyDefinitions } from "@dxos/schema";
9
- var getBaseSchema = ({ typename, properties, jsonSchema, schema }) => {
10
- if (typename && properties) {
11
- const schema2 = getSchemaFromPropertyDefinitions(typename, properties);
12
- return {
13
- typename: schema2.typename,
14
- jsonSchema: schema2.jsonSchema
15
- };
16
- } else if (schema) {
17
- return {
18
- typename: getTypename(schema),
19
- jsonSchema: toJsonSchema(schema)
20
- };
21
- } else if (typename && jsonSchema) {
22
- return {
23
- typename,
24
- jsonSchema
25
- };
26
- } else {
27
- throw new Error("invalid properties");
28
- }
29
- };
30
- var makeDynamicTable = ({ jsonSchema, properties }) => {
31
- const table = Obj.make(schema_exports.Table, {
32
- sizes: {}
33
- });
34
- const view = createView({
35
- query: Query.select(Filter.everything()),
36
- jsonSchema,
37
- presentation: table,
38
- ...properties && {
39
- fields: properties.map((property) => property.name)
40
- }
41
- });
42
- const projection = new ProjectionModel(jsonSchema, view.projection);
43
- projection.normalizeView();
44
- if (properties && projection.fields) {
45
- setProperties(view, projection, table, properties);
46
- }
47
- return {
48
- projection,
49
- view
50
- };
51
- };
52
- var setProperties = (view, projection, table, properties) => {
53
- for (const property of properties) {
54
- const field = projection.fields.find((field2) => field2.path === property.name);
55
- if (field) {
56
- if (property.size !== void 0) {
57
- table.sizes[field.path] = property.size;
58
- }
59
- if (property.title !== void 0) {
60
- const fieldProjection = projection.getFieldProjection(field.id);
61
- projection.setFieldProjection({
62
- ...fieldProjection,
63
- props: {
64
- ...fieldProjection.props,
65
- title: property.title
66
- }
67
- });
68
- }
69
- if (property.sort) {
70
- const fieldId = field.id;
71
- view.sort = [
72
- {
73
- fieldId,
74
- direction: property.sort
75
- }
76
- ];
77
- }
78
- }
79
- }
80
- };
81
-
82
- // src/util/signals.ts
83
- var touch = (value) => value;
84
-
85
- // src/util/table-buttons.ts
86
- var BUTTON_IDENTIFIERS = {
87
- columnSettings: "data-table-column-settings-button",
88
- newColumn: "data-table-new-column-button",
89
- referencedCell: "data-table-ref-cell-button",
90
- rowMenu: "data-table-row-menu-button",
91
- saveDraftRow: "data-table-save-draft-row-button",
92
- sort: "data-table-sort-button"
93
- };
94
- var createButton = ({ attr, data, disabled = false, icon, testId, type: _type = "primary" }) => {
95
- const dataAttrs = Object.entries(data).map(([k, v]) => `${k}="${v}"`).join(" ");
96
- return `<button ${attr} data-testid="${testId}" class="dx-button is-6 aspect-square pli-0.5 min-bs-0" ${dataAttrs} ${disabled ? "disabled" : ""} data-dx-grid-action="accessory"><svg data-size="4"><use href="/icons.svg#${icon}"/></svg></button>`;
97
- };
98
- var addColumnButton = {
99
- attr: BUTTON_IDENTIFIERS.newColumn,
100
- icon: "ph--plus--regular",
101
- render: ({ disabled }) => {
102
- return createButton({
103
- attr: BUTTON_IDENTIFIERS.newColumn,
104
- icon: addColumnButton.icon,
105
- disabled,
106
- data: {},
107
- testId: "table-new-column-button"
108
- });
109
- },
110
- getData: (_el) => ({
111
- type: "newColumn"
112
- })
113
- };
114
- var columnSettingsButton = {
115
- attr: BUTTON_IDENTIFIERS.columnSettings,
116
- icon: "ph--caret-down--regular",
117
- render: ({ fieldId }) => {
118
- return createButton({
119
- attr: BUTTON_IDENTIFIERS.columnSettings,
120
- icon: columnSettingsButton.icon,
121
- data: {
122
- "data-field-id": fieldId,
123
- "data-variant": "ghost"
124
- },
125
- testId: "table-column-settings-button"
126
- });
127
- },
128
- getData: (el) => ({
129
- type: "columnSettings",
130
- fieldId: el.getAttribute("data-field-id")
131
- })
132
- };
133
- var rowMenuButton = {
134
- attr: BUTTON_IDENTIFIERS.rowMenu,
135
- icon: "ph--dots-three--regular",
136
- render: ({ rowIndex }) => {
137
- return createButton({
138
- attr: BUTTON_IDENTIFIERS.rowMenu,
139
- icon: rowMenuButton.icon,
140
- data: {
141
- "data-row-index": rowIndex.toString()
142
- },
143
- testId: "table-row-menu-button"
144
- });
145
- },
146
- getData: (el) => ({
147
- type: "rowMenu",
148
- rowIndex: Number(el.getAttribute("data-row-index"))
149
- })
150
- };
151
- var sortButton = {
152
- attr: BUTTON_IDENTIFIERS.sort,
153
- icon: "ph--sort-ascending--regular",
154
- render: ({ fieldId, direction }) => {
155
- return createButton({
156
- attr: BUTTON_IDENTIFIERS.sort,
157
- icon: direction === "desc" ? "ph--sort-descending--regular" : "ph--sort-ascending--regular",
158
- data: {
159
- "data-field-id": fieldId,
160
- "data-direction": direction ?? ""
161
- },
162
- testId: "table-sort-button",
163
- // TODO(ZaymonFC): All buttons should be rendered into an absolutely positioned flex container.
164
- // This is a bit hacky.
165
- type: "secondary"
166
- });
167
- },
168
- getData: (el) => ({
169
- type: "sort",
170
- fieldId: el.getAttribute("data-field-id"),
171
- direction: el.getAttribute("data-direction")
172
- })
173
- };
174
- var saveDraftRowButton = {
175
- attr: BUTTON_IDENTIFIERS.saveDraftRow,
176
- icon: "ph--floppy-disk--regular",
177
- render: ({ rowIndex, disabled }) => {
178
- return createButton({
179
- attr: BUTTON_IDENTIFIERS.saveDraftRow,
180
- icon: saveDraftRowButton.icon,
181
- disabled,
182
- data: {
183
- "data-row-index": rowIndex.toString(),
184
- "data-variant": "primary"
185
- },
186
- testId: "table-save-draft-row-button"
187
- });
188
- },
189
- getData: (el) => ({
190
- type: "saveDraftRow",
191
- rowIndex: Number(el.getAttribute("data-row-index"))
192
- })
193
- };
194
- var tableButtons = {
195
- addColumn: addColumnButton,
196
- columnSettings: columnSettingsButton,
197
- rowMenu: rowMenuButton,
198
- saveDraftRow: saveDraftRowButton,
199
- sort: sortButton
200
- };
201
-
202
- // src/util/table-controls.ts
203
- import { accessoryHandlesPointerdownAttrs } from "@dxos/lit-grid";
204
- var CONTROL_IDENTIFIERS = {
205
- checkbox: "data-table-checkbox",
206
- switch: "data-table-switch"
207
- };
208
- var BASE_CLASSES = {
209
- checkbox: "absolute inset-block-[.375rem] inline-end-[.375rem] dx-checkbox",
210
- switch: "absolute inset-block-[.25rem] inline-end-[.25rem] dx-checkbox--switch"
211
- };
212
- var renderAttributes = (data) => {
213
- return Object.entries(data).map(([k, v]) => `${k}="${v}"`).join(" ");
214
- };
215
- var renderInput = (baseClass, attrs, checked = false, disabled = false, preventToggle = false, testId) => {
216
- return `<input type="checkbox" class="${baseClass}" ${renderAttributes(attrs)} ${checked ? "checked" : ""} ${preventToggle ? 'onclick="return false"' : ""} ${disabled ? "disabled" : ""} data-testid="${testId}" data-dx-grid-action="accessory"/>`;
217
- };
218
- var CheckboxStory = ({ rowIndex, header = false, checked = false, disabled = false }) => {
219
- const attrs = {
220
- [CONTROL_IDENTIFIERS.checkbox]: "",
221
- "data-row-index": rowIndex.toString(),
222
- ...header && {
223
- "data-header": ""
224
- }
225
- };
226
- return renderInput(BASE_CLASSES.checkbox, attrs, checked, disabled, true, "table-selection");
227
- };
228
- var SwitchStory = ({ colIndex, rowIndex, checked = false, disabled = false }) => {
229
- const attrs = {
230
- [CONTROL_IDENTIFIERS.switch]: "",
231
- "data-row-index": rowIndex.toString(),
232
- "data-col-index": colIndex.toString(),
233
- ...accessoryHandlesPointerdownAttrs
234
- };
235
- return renderInput(BASE_CLASSES.switch, attrs, checked, disabled, true, "table-switch");
236
- };
237
- var tableControls = {
238
- checkbox: {
239
- attr: CONTROL_IDENTIFIERS.checkbox,
240
- render: CheckboxStory,
241
- getData: (el) => ({
242
- type: "checkbox",
243
- rowIndex: Number(el.getAttribute("data-row-index")),
244
- header: el.hasAttribute("data-header")
245
- })
246
- },
247
- switch: {
248
- attr: CONTROL_IDENTIFIERS.switch,
249
- render: SwitchStory,
250
- getData: (el) => ({
251
- type: "switch",
252
- rowIndex: Number(el.getAttribute("data-row-index")),
253
- colIndex: Number(el.getAttribute("data-col-index"))
254
- })
255
- }
256
- };
257
-
258
- // src/util/schema.ts
259
- import * as Schema from "effect/Schema";
260
- import * as SchemaAST from "effect/SchemaAST";
261
- var narrowSchema = (schema, paths) => {
262
- const ast = schema?.ast;
263
- if (SchemaAST.isTypeLiteral(ast)) {
264
- const propertySignatures = ast.propertySignatures.filter((signature) => paths.includes(signature.name.toString()));
265
- if (propertySignatures.length > 0) {
266
- const narrowType = new SchemaAST.TypeLiteral(propertySignatures, []);
267
- return Schema.make(narrowType);
268
- }
269
- }
270
- return void 0;
271
- };
272
-
273
- // src/model/table-model.ts
274
- import { computed as computed3, effect as effect2, signal as signal3 } from "@preact/signals-core";
275
- import { Resource as Resource2 } from "@dxos/context";
276
- import { Obj as Obj2, Ref } from "@dxos/echo";
277
- import { FormatEnum as FormatEnum2, getSchema, getValue as getValue2, setValue, toEffectSchema } from "@dxos/echo/internal";
278
- import { invariant } from "@dxos/invariant";
279
- import { ObjectId } from "@dxos/keys";
280
- import { getSnapshot, isLiveObject } from "@dxos/live-object";
281
- import { fullyQualifiedId } from "@dxos/react-client/echo";
282
- import { formatForEditing, parseValue } from "@dxos/react-ui-form";
283
- import { ProjectionModel as ProjectionModel2, validateSchema } from "@dxos/schema";
284
-
285
- // src/util/tag.ts
286
- var TAG_PATTERN_REGEX = /\[([^\]]+)\]\(([^)]+)\)/g;
287
- var extractTagIds = (value) => {
288
- if (typeof value !== "string") {
289
- return void 0;
290
- }
291
- const ids = [];
292
- let match;
293
- while ((match = TAG_PATTERN_REGEX.exec(value)) !== null) {
294
- if (match[2]) {
295
- ids.push(match[2]);
296
- }
297
- }
298
- return ids.length > 0 ? ids : void 0;
299
- };
300
-
301
- // src/model/selection-model.ts
302
- import { computed, effect, signal } from "@preact/signals-core";
303
- import { Resource } from "@dxos/context";
304
- function _define_property(obj, key, value) {
305
- if (key in obj) {
306
- Object.defineProperty(obj, key, {
307
- value,
308
- enumerable: true,
309
- configurable: true,
310
- writable: true
311
- });
312
- } else {
313
- obj[key] = value;
314
- }
315
- return obj;
316
- }
317
- var SelectionModel = class extends Resource {
318
- async _open() {
319
- const selectionWatcher = effect(() => {
320
- touch(this._selection.value);
321
- this._onSelectionChanged?.();
322
- });
323
- this._ctx.onDispose(selectionWatcher);
324
- }
325
- //
326
- // Getters
327
- //
328
- get selection() {
329
- return this._selection;
330
- }
331
- get hasSelection() {
332
- return this._hasSelection;
333
- }
334
- get allRowsSeleted() {
335
- return this._allSelected;
336
- }
337
- get selectionMode() {
338
- return this._selectionMode;
339
- }
340
- constructor(_rows, _selectionMode, _initialSelection, _onSelectionChanged) {
341
- super(), _define_property(this, "_rows", void 0), _define_property(this, "_selectionMode", void 0), _define_property(this, "_initialSelection", void 0), _define_property(this, "_onSelectionChanged", void 0), _define_property(this, "_selection", void 0), _define_property(this, "_validSelectedIds", void 0), _define_property(this, "_hasSelection", void 0), _define_property(this, "_allSelected", void 0), _define_property(this, "getSelectedRows", void 0), _define_property(this, "isRowIndexSelected", void 0), _define_property(this, "isObjectSelected", void 0), //
342
- // Manipulation
343
- //
344
- _define_property(this, "toggleSelectionForRowIndex", void 0), _define_property(this, "setSelection", void 0), this._rows = _rows, this._selectionMode = _selectionMode, this._initialSelection = _initialSelection, this._onSelectionChanged = _onSelectionChanged, this._validSelectedIds = computed(() => {
345
- const rows = this._rows.value;
346
- if (!rows) {
347
- return /* @__PURE__ */ new Set();
348
- }
349
- const validIds = new Set(rows.map((row) => row.id));
350
- return new Set([
351
- ...this._selection.value
352
- ].filter((id) => validIds.has(id)));
353
- }), this._hasSelection = computed(() => this._validSelectedIds.value.size > 0), this._allSelected = computed(() => {
354
- const rows = this._rows.value;
355
- if (rows.length === 0) {
356
- return false;
357
- }
358
- return rows.every((row) => this._selection.value.has(row.id));
359
- }), this.getSelectedRows = () => {
360
- const selectedIds = this._validSelectedIds.value;
361
- return this._rows.value.filter((row) => selectedIds.has(row.id));
362
- }, this.isRowIndexSelected = (rowIndex) => {
363
- const row = this._rows.value[rowIndex];
364
- return this._selection.value.has(row.id);
365
- }, this.isObjectSelected = (object) => {
366
- return this._selection.value.has(object.id);
367
- }, this.toggleSelectionForRowIndex = (rowIndex) => {
368
- const row = this._rows.value[rowIndex];
369
- const newSelection = new Set(this._selection.value);
370
- if (newSelection.has(row.id)) {
371
- newSelection.delete(row.id);
372
- } else {
373
- if (this._selectionMode === "single") {
374
- newSelection.clear();
375
- }
376
- newSelection.add(row.id);
377
- }
378
- this._selection.value = newSelection;
379
- }, this.setSelection = (mode) => {
380
- switch (mode) {
381
- case "all": {
382
- this._selection.value = new Set(this._rows.value.map((row) => row.id));
383
- break;
384
- }
385
- case "none": {
386
- this._selection.value = /* @__PURE__ */ new Set();
387
- break;
388
- }
389
- }
390
- };
391
- this._selection = signal(new Set(this._initialSelection));
392
- }
393
- };
394
-
395
- // src/model/table-sorting.ts
396
- import { computed as computed2, signal as signal2 } from "@preact/signals-core";
397
- import orderBy from "lodash.orderby";
398
- import { FormatEnum, TypeEnum, getValue } from "@dxos/echo/internal";
399
- import { formatForDisplay } from "@dxos/react-ui-form";
400
- function _define_property2(obj, key, value) {
401
- if (key in obj) {
402
- Object.defineProperty(obj, key, {
403
- value,
404
- enumerable: true,
405
- configurable: true,
406
- writable: true
407
- });
408
- } else {
409
- obj[key] = value;
410
- }
411
- return obj;
412
- }
413
- var TableSorting = class {
414
- /**
415
- * @reactive
416
- * @returns Local sort if present, falls back to view sort.
417
- */
418
- get sorting() {
419
- const local = this._localSort.value;
420
- if (local?.type === "cleared") {
421
- return void 0;
422
- }
423
- if (local?.type === "active") {
424
- return local.sort;
425
- }
426
- return this._view.sort?.[0];
427
- }
428
- /**
429
- * @reactive
430
- * @returns Whether local sort differs from view sort.
431
- */
432
- get isDirty() {
433
- return this._isDirty.value;
434
- }
435
- getDataIndex(displayIndex) {
436
- return this._displayToDataIndex.get(displayIndex) ?? displayIndex;
437
- }
438
- setSort(fieldId, direction) {
439
- this._localSort.value = {
440
- type: "active",
441
- sort: {
442
- fieldId,
443
- direction
444
- }
445
- };
446
- }
447
- toggleSort(fieldId) {
448
- if (!this.sorting || this.sorting.fieldId !== fieldId) {
449
- return;
450
- }
451
- switch (this.sorting.direction) {
452
- case "asc": {
453
- this.setSort(fieldId, "desc");
454
- break;
455
- }
456
- case "desc": {
457
- this.setSort(fieldId, "asc");
458
- break;
459
- }
460
- }
461
- }
462
- clearSort() {
463
- this._localSort.value = {
464
- type: "cleared"
465
- };
466
- }
467
- save() {
468
- if (this._localSort.value !== void 0) {
469
- if (this._localSort.value.type === "active") {
470
- this._view.sort = [
471
- this._localSort.value.sort
472
- ];
473
- } else {
474
- this._view.sort = [];
475
- }
476
- this._localSort.value = void 0;
477
- }
478
- }
479
- //
480
- // Initialisation.
481
- //
482
- initialiseSortedRows() {
483
- return computed2(() => {
484
- this._displayToDataIndex.clear();
485
- const sort = this.sorting;
486
- if (!sort) {
487
- return this._rows.value;
488
- }
489
- const field = this._projection.fields.find((f) => f.id === sort.fieldId);
490
- if (!field) {
491
- return this._rows.value;
492
- }
493
- const { props } = this._projection.getFieldProjection(field.id);
494
- const dataWithIndices = this._rows.value.map((item, index) => {
495
- const sortValue = this.getSortValue(props, field, item);
496
- const isEmpty = !(props.type === TypeEnum.Boolean) && (sortValue == null || sortValue === "");
497
- return {
498
- item,
499
- index,
500
- value: sortValue,
501
- isEmpty
502
- };
503
- });
504
- const ordered = orderBy(dataWithIndices, [
505
- "isEmpty",
506
- "value"
507
- ], [
508
- "asc",
509
- sort.direction
510
- ]);
511
- ordered.forEach(({ index: dataIndex }, displayIndex) => {
512
- if (displayIndex !== dataIndex) {
513
- this._displayToDataIndex.set(displayIndex, dataIndex);
514
- }
515
- });
516
- return ordered.map(({ item }) => item);
517
- });
518
- }
519
- getSortValue(props, field, item) {
520
- const { type, format } = props;
521
- const value = getValue(item, field.path);
522
- if (type === TypeEnum.Boolean) {
523
- return !!value;
524
- }
525
- if (type === TypeEnum.Number) {
526
- return value;
527
- }
528
- if (value == null) {
529
- return value;
530
- }
531
- if (format === FormatEnum.SingleSelect) {
532
- const options = props.options;
533
- if (!options) {
534
- return void 0;
535
- }
536
- const option = options.find((option2) => option2.id === value);
537
- return option ? options.indexOf(option) : void 0;
538
- }
539
- if (format === FormatEnum.Ref && field.referencePath) {
540
- const refValue = getValue(value.target, field.referencePath);
541
- return refValue;
542
- }
543
- return formatForDisplay({
544
- type,
545
- format,
546
- value
547
- });
548
- }
549
- constructor(rows, _view, _projection) {
550
- _define_property2(this, "_view", void 0);
551
- _define_property2(this, "_projection", void 0);
552
- _define_property2(this, "_displayToDataIndex", void 0);
553
- _define_property2(this, "_rows", void 0);
554
- _define_property2(this, "_localSort", void 0);
555
- _define_property2(this, "_isDirty", void 0);
556
- _define_property2(this, "sortedRows", void 0);
557
- this._view = _view;
558
- this._projection = _projection;
559
- this._displayToDataIndex = /* @__PURE__ */ new Map();
560
- this._localSort = signal2(void 0);
561
- this._rows = rows;
562
- this._isDirty = computed2(() => {
563
- const local = this._localSort.value;
564
- const viewSort = this._view.sort?.[0];
565
- if (local?.type === "cleared") {
566
- return viewSort !== void 0;
567
- }
568
- if (local?.type === "active") {
569
- if (!viewSort) {
570
- return true;
571
- }
572
- return local.sort.fieldId !== viewSort.fieldId || local.sort.direction !== viewSort.direction;
573
- }
574
- return false;
575
- });
576
- this.sortedRows = this.initialiseSortedRows();
577
- }
578
- };
579
-
580
- // src/model/table-model.ts
581
- function _define_property3(obj, key, value) {
582
- if (key in obj) {
583
- Object.defineProperty(obj, key, {
584
- value,
585
- enumerable: true,
586
- configurable: true,
587
- writable: true
588
- });
589
- } else {
590
- obj[key] = value;
591
- }
592
- return obj;
593
- }
594
- var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-table/src/model/table-model.ts";
595
- var defaultFeatures = {
596
- selection: {
597
- enabled: true,
598
- mode: "multiple"
599
- },
600
- dataEditable: false,
601
- schemaEditable: false
602
- };
603
- var TableModel = class extends Resource2 {
604
- get id() {
605
- return fullyQualifiedId(this._view);
606
- }
607
- get view() {
608
- return this._view;
609
- }
610
- get table() {
611
- invariant(this._table, "Model not initialized", {
612
- F: __dxlog_file,
613
- L: 187,
614
- S: this,
615
- A: [
616
- "this._table",
617
- "'Model not initialized'"
618
- ]
619
- });
620
- return this._table;
621
- }
622
- get projection() {
623
- return this._projection;
624
- }
625
- get rows() {
626
- return this.sorting.sortedRows;
627
- }
628
- get features() {
629
- return this._features;
630
- }
631
- /**
632
- * @reactive
633
- */
634
- get sorting() {
635
- return this._sorting;
636
- }
637
- /**
638
- * Gets the row data at the specified display index.
639
- */
640
- getRowAt(displayIndex) {
641
- if (displayIndex < 0 || displayIndex >= this.sorting.sortedRows.value.length) {
642
- return void 0;
643
- }
644
- return this.sorting.sortedRows.value[displayIndex];
645
- }
646
- get pinnedRows() {
647
- return this._pinnedRows;
648
- }
649
- get columnMeta() {
650
- invariant(this._columnMeta, void 0, {
651
- F: __dxlog_file,
652
- L: 226,
653
- S: this,
654
- A: [
655
- "this._columnMeta",
656
- ""
657
- ]
658
- });
659
- return this._columnMeta;
660
- }
661
- get selection() {
662
- return this._selection;
663
- }
664
- /** @reactive */
665
- get isViewDirty() {
666
- return this.sorting.isDirty;
667
- }
668
- get rowActions() {
669
- return this._rowActions;
670
- }
671
- //
672
- // Initialisation
673
- //
674
- async _open() {
675
- const presentation = this._view.presentation.target ?? await this._view.presentation.load();
676
- invariant(Obj2.instanceOf(schema_exports.Table, presentation), void 0, {
677
- F: __dxlog_file,
678
- L: 249,
679
- S: this,
680
- A: [
681
- "Obj.instanceOf(Table.Table, presentation)",
682
- ""
683
- ]
684
- });
685
- this._table = presentation;
686
- this.initializeColumnMeta();
687
- this.initializeEffects();
688
- await this._selection.open(this._ctx);
689
- }
690
- initializeColumnMeta() {
691
- this._columnMeta = computed3(() => {
692
- const fields = this._projection?.fields ?? [];
693
- const meta = Object.fromEntries(fields.map((field, index) => [
694
- index,
695
- {
696
- size: this.table.sizes[field.path] ?? 256,
697
- resizeable: true
698
- }
699
- ]));
700
- return {
701
- grid: meta,
702
- frozenColsStart: {
703
- 0: {
704
- size: 30,
705
- resizeable: false
706
- }
707
- },
708
- frozenColsEnd: {
709
- 0: {
710
- size: 32,
711
- resizeable: false
712
- }
713
- }
714
- };
715
- });
716
- }
717
- initializeEffects() {
718
- const rowOrderWatcher = effect2(() => {
719
- touch(this.sorting.sortedRows.value);
720
- this._onRowOrderChange?.();
721
- });
722
- this._ctx.onDispose(rowOrderWatcher);
723
- const rowEffectManager = effect2(() => {
724
- const { start, end } = touch(this._visibleRange.value);
725
- const rowEffects = [];
726
- for (let row = start.row; row <= end.row; row++) {
727
- rowEffects.push(effect2(() => {
728
- const obj = this.sorting.sortedRows.value[row];
729
- this._projection?.fields.forEach((field) => touch(getValue2(obj, field.path)));
730
- this._onCellUpdate?.({
731
- row,
732
- col: start.col,
733
- plane: "grid"
734
- });
735
- }));
736
- }
737
- return () => rowEffects.forEach((cleanup) => cleanup());
738
- });
739
- this._ctx.onDispose(rowEffectManager);
740
- const draftRowsWatcher = effect2(() => {
741
- touch(this._draftRows.value);
742
- this._onRowOrderChange?.();
743
- });
744
- this._ctx.onDispose(draftRowsWatcher);
745
- }
746
- // TODO(ZaymonFC): Return .value instead of exposing the signal.
747
- get draftRows() {
748
- return this._draftRows;
749
- }
750
- /**
751
- * Checks if a specific field path has validation errors for a given draft row.
752
- * @param draftRowIndex - The index of the draft row to check
753
- * @param fieldPath - The path of the field to check for validation errors
754
- * @returns true if the field has validation errors, false otherwise
755
- */
756
- hasDraftRowValidationError(draftRowIndex, fieldPath) {
757
- if (draftRowIndex < 0 || draftRowIndex >= this._draftRows.value.length) {
758
- return false;
759
- }
760
- const draftRow = this._draftRows.value[draftRowIndex];
761
- const validationErrors = draftRow.validationErrors || [];
762
- return validationErrors.some((error) => error.path === fieldPath);
763
- }
764
- createDraftRow() {
765
- if (!this._projection) {
766
- return;
767
- }
768
- const draftData = {
769
- id: ObjectId.random()
770
- };
771
- const validationErrors = this.validateDraftRowData(draftData);
772
- const draftRow = {
773
- data: draftData,
774
- valid: validationErrors.length === 0,
775
- validationErrors
776
- };
777
- this._draftRows.value = [
778
- ...this._draftRows.value,
779
- draftRow
780
- ];
781
- }
782
- validateDraftRow(draftRowIndex) {
783
- if (draftRowIndex < 0 || draftRowIndex >= this._draftRows.value.length) {
784
- return;
785
- }
786
- const draftRow = this._draftRows.value[draftRowIndex];
787
- const validationErrors = this.validateDraftRowData(draftRow.data);
788
- const updatedDraftRow = {
789
- ...draftRow,
790
- valid: validationErrors.length === 0,
791
- validationErrors
792
- };
793
- const newDraftRows = [
794
- ...this._draftRows.value
795
- ];
796
- newDraftRows[draftRowIndex] = updatedDraftRow;
797
- this._draftRows.value = newDraftRows;
798
- }
799
- validateDraftRowData(data) {
800
- const schema = toEffectSchema(this._projection.baseSchema);
801
- return validateSchema(schema, data) || [];
802
- }
803
- /**
804
- * Updates a cell's value using a transform function.
805
- * @param {DxGridPlanePosition} position - The position of the cell to update.
806
- * @param {(value: any) => any} update - A function that takes the current value and returns the updated value.
807
- */
808
- updateCellData({ col, row }, update) {
809
- const dataRow = this._sorting.getDataIndex(row);
810
- const fields = this._projection?.fields ?? [];
811
- const field = fields[col];
812
- const value = getValue2(this._rows.value[dataRow], field.path);
813
- const updatedValue = update(value);
814
- setValue(this._rows.value[dataRow], field.path, updatedValue);
815
- }
816
- /**
817
- * Gets the domain-appropriate cell type for a given cell position.
818
- * This abstracts away the low-level grid plane concepts and provides domain-specific types.
819
- * @param cell - The cell position to classify
820
- * @returns The domain-appropriate cell type
821
- */
822
- getCellType(cell) {
823
- switch (cell.plane) {
824
- case "frozenRowsEnd":
825
- return "draft";
826
- case "frozenRowsStart":
827
- return "header";
828
- case "grid":
829
- default:
830
- return "standard";
831
- }
832
- }
833
- /**
834
- * Convenience method to check if a cell is a draft cell.
835
- * @param cell - The cell position to check
836
- * @returns true if the cell is a draft cell, false otherwise
837
- */
838
- isDraftCell(cell) {
839
- return this.getCellType(cell) === "draft";
840
- }
841
- //
842
- // Column Operations
843
- //
844
- deleteColumn(fieldId) {
845
- if (!this._projection) {
846
- return;
847
- }
848
- const field = this._projection?.fields.find((field2) => field2.id === fieldId);
849
- if (field && this._onDeleteColumn) {
850
- this._onDeleteColumn(field.id);
851
- }
852
- }
853
- //
854
- // Resize
855
- //
856
- setColumnWidth(columnIndex, width) {
857
- const fields = this._projection?.fields ?? [];
858
- if (columnIndex < fields.length) {
859
- const newWidth = Math.max(0, width);
860
- const field = fields[columnIndex];
861
- if (field) {
862
- this.table.sizes[field.path] = newWidth;
863
- }
864
- }
865
- }
866
- //
867
- // Pinning
868
- //
869
- // TODO(burdon): Change to setPinned(on/off).
870
- pinRow(rowIndex, side) {
871
- this._pinnedRows[side].push(rowIndex);
872
- }
873
- unpinRow(rowIndex) {
874
- this._pinnedRows.top = this._pinnedRows.top.filter((index) => index !== rowIndex);
875
- this._pinnedRows.bottom = this._pinnedRows.bottom.filter((index) => index !== rowIndex);
876
- }
877
- //
878
- // View operations
879
- //
880
- saveView() {
881
- this._sorting.save();
882
- }
883
- constructor({ view, schema, projection, features = {}, sorting = [], initialSelection = [], pinnedRows = {
884
- top: [],
885
- bottom: []
886
- }, rowActions = [], onCellUpdate, onDeleteColumn, onDeleteRows, onInsertRow, onRowOrderChange, onRowAction }) {
887
- super(), _define_property3(this, "_view", void 0), _define_property3(this, "_projection", void 0), _define_property3(this, "_table", void 0), _define_property3(this, "_visibleRange", signal3({
888
- start: {
889
- row: 0,
890
- col: 0
891
- },
892
- end: {
893
- row: 0,
894
- col: 0
895
- }
896
- })), _define_property3(this, "_onInsertRow", void 0), _define_property3(this, "_onDeleteRows", void 0), _define_property3(this, "_onDeleteColumn", void 0), _define_property3(this, "_onCellUpdate", void 0), _define_property3(this, "_onRowOrderChange", void 0), _define_property3(this, "_onRowAction", void 0), _define_property3(this, "_rowActions", void 0), _define_property3(this, "_features", void 0), _define_property3(this, "_rows", signal3([])), _define_property3(this, "_draftRows", signal3([])), _define_property3(this, "_sorting", void 0), _define_property3(this, "_pinnedRows", void 0), _define_property3(this, "_selection", void 0), _define_property3(this, "_columnMeta", void 0), //
897
- // Data
898
- //
899
- _define_property3(this, "setRows", (obj) => {
900
- this._rows.value = obj;
901
- }), _define_property3(this, "getRowCount", () => this._rows.value.length), /**
902
- * @reactive
903
- */
904
- _define_property3(this, "getDraftRowCount", () => this._draftRows.value.length), _define_property3(this, "getColumnCount", () => this._projection?.fields.length ?? 0), _define_property3(this, "insertRow", () => {
905
- const result = this._onInsertRow?.();
906
- if (result === "draft" && this._draftRows.value.length === 0) {
907
- this.createDraftRow();
908
- }
909
- return result ?? "final";
910
- }), _define_property3(this, "commitDraftRow", (draftRowIndex) => {
911
- if (draftRowIndex < 0 || draftRowIndex >= this._draftRows.value.length) {
912
- return false;
913
- }
914
- const draftRow = this._draftRows.value[draftRowIndex];
915
- if (!draftRow.valid) {
916
- return false;
917
- }
918
- const insertRowResult = this._onInsertRow?.(draftRow.data);
919
- if (insertRowResult === "final") {
920
- const newDraftRows = this._draftRows.value.filter((_, index) => index !== draftRowIndex);
921
- this._draftRows.value = newDraftRows;
922
- }
923
- return insertRowResult === "final";
924
- }), _define_property3(this, "deleteRow", (rowIndex) => {
925
- const row = this._sorting.getDataIndex(rowIndex);
926
- const obj = this._rows.value[row];
927
- const objectsToDelete = [];
928
- if (this.features.selection.enabled && this._selection.hasSelection.value) {
929
- const selectedRows = this._selection.getSelectedRows();
930
- objectsToDelete.push(...selectedRows);
931
- } else {
932
- objectsToDelete.push(obj);
933
- }
934
- this._onDeleteRows?.(row, objectsToDelete);
935
- }), _define_property3(this, "handleRowAction", (actionId, rowIndex) => {
936
- if (!this._onRowAction) {
937
- return;
938
- }
939
- const row = this._sorting.getDataIndex(rowIndex);
940
- const data = this._rows.value[row];
941
- if (data) {
942
- this._onRowAction(actionId, data);
943
- }
944
- }), _define_property3(this, "getCellData", (cell) => {
945
- const { col, row, plane } = cell;
946
- const fields = this._projection?.fields ?? [];
947
- if (col < 0 || col >= fields.length) {
948
- return void 0;
949
- }
950
- const field = fields[col];
951
- let value;
952
- if (plane === "frozenRowsEnd") {
953
- if (row >= 0 && row < this._draftRows.value.length) {
954
- value = getValue2(this._draftRows.value[row].data, field.path);
955
- } else {
956
- return void 0;
957
- }
958
- } else {
959
- const dataIndex = this._sorting.getDataIndex(row);
960
- value = getValue2(this._rows.value[dataIndex], field.path);
961
- }
962
- if (value == null) {
963
- return "";
964
- }
965
- const { props } = this._projection.getFieldProjection(field.id);
966
- switch (props.format) {
967
- case FormatEnum2.Ref: {
968
- if (!field.referencePath) {
969
- return "";
970
- }
971
- return getValue2(value.target, field.referencePath);
972
- }
973
- default: {
974
- return formatForEditing({
975
- type: props.type,
976
- format: props.format,
977
- value
978
- });
979
- }
980
- }
981
- }), _define_property3(this, "validateCellData", async ({ col, row, plane }, value) => {
982
- const fields = this._projection?.fields ?? [];
983
- if (col < 0 || col >= fields.length) {
984
- return {
985
- valid: false,
986
- error: "Invalid column index"
987
- };
988
- }
989
- const field = fields[col];
990
- const { props } = this._projection.getFieldProjection(field.id);
991
- const transformedValue = editorTextToCellValue(props, value);
992
- const isDraftRow = plane === "frozenRowsEnd";
993
- if (isDraftRow) {
994
- const isInvalidDraftRowIndex = row < 0 || row >= this._draftRows.value.length;
995
- if (isInvalidDraftRowIndex) {
996
- return {
997
- valid: false,
998
- error: "Invalid draft row index"
999
- };
1000
- }
1001
- const draftRow = this._draftRows.value[row];
1002
- const snapshot = {
1003
- ...draftRow.data
1004
- };
1005
- setValue(snapshot, field.path, transformedValue);
1006
- const validationErrors = this.validateDraftRowData(snapshot);
1007
- if (validationErrors.length > 0) {
1008
- const error = validationErrors.find((err) => err.path === field.path);
1009
- if (error) {
1010
- return {
1011
- valid: false,
1012
- error: error.message
1013
- };
1014
- }
1015
- }
1016
- return {
1017
- valid: true
1018
- };
1019
- } else {
1020
- const rowIdx = this._sorting.getDataIndex(row);
1021
- const currentRow = this._rows.value[rowIdx];
1022
- invariant(currentRow, "Invalid row index", {
1023
- F: __dxlog_file,
1024
- L: 522,
1025
- S: this,
1026
- A: [
1027
- "currentRow",
1028
- "'Invalid row index'"
1029
- ]
1030
- });
1031
- const snapshot = getSnapshot(currentRow);
1032
- setValue(snapshot, field.path, transformedValue);
1033
- const schema2 = getSchema(currentRow);
1034
- invariant(schema2, void 0, {
1035
- F: __dxlog_file,
1036
- L: 528,
1037
- S: this,
1038
- A: [
1039
- "schema",
1040
- ""
1041
- ]
1042
- });
1043
- const validationResult = validateSchema(schema2, snapshot);
1044
- if (validationResult && validationResult.length > 0) {
1045
- const error = validationResult[0];
1046
- invariant(error.path === field.path, void 0, {
1047
- F: __dxlog_file,
1048
- L: 533,
1049
- S: this,
1050
- A: [
1051
- "error.path === field.path",
1052
- ""
1053
- ]
1054
- });
1055
- return {
1056
- valid: false,
1057
- error: error.message
1058
- };
1059
- }
1060
- return {
1061
- valid: true
1062
- };
1063
- }
1064
- }), _define_property3(this, "setCellData", (cell, value) => {
1065
- const { col, row, plane } = cell;
1066
- const fields = this._projection?.fields ?? [];
1067
- if (col < 0 || col >= fields.length) {
1068
- return;
1069
- }
1070
- const field = fields[col];
1071
- const { props } = this._projection.getFieldProjection(field.id);
1072
- const transformedValue = editorTextToCellValue(props, value);
1073
- if (props.format === FormatEnum2.Ref && !isLiveObject(value)) {
1074
- return;
1075
- }
1076
- if (plane === "frozenRowsEnd") {
1077
- if (row >= 0 && row < this._draftRows.value.length) {
1078
- setValue(this._draftRows.value[row].data, field.path, transformedValue);
1079
- this.validateDraftRow(row);
1080
- }
1081
- } else {
1082
- const rowIdx = this._sorting.getDataIndex(row);
1083
- setValue(this._rows.value[rowIdx], field.path, transformedValue);
1084
- }
1085
- });
1086
- this._view = view;
1087
- this._projection = projection ?? new ProjectionModel2(schema, view.projection);
1088
- this._projection.normalizeView();
1089
- this._features = {
1090
- ...defaultFeatures,
1091
- ...features
1092
- };
1093
- invariant(!(this._features.dataEditable && this._features.selection.enabled && this._features.selection.mode === "single"), "Single selection is not compatible with editable tables.", {
1094
- F: __dxlog_file,
1095
- L: 149,
1096
- S: this,
1097
- A: [
1098
- "!(this._features.dataEditable && this._features.selection.enabled && this._features.selection.mode === 'single')",
1099
- "'Single selection is not compatible with editable tables.'"
1100
- ]
1101
- });
1102
- this._sorting = new TableSorting(this._rows, this._view, this._projection);
1103
- if (sorting.length > 0) {
1104
- const [sort] = sorting;
1105
- this._sorting.setSort(sort.fieldId, sort.direction);
1106
- }
1107
- this._selection = new SelectionModel(this._sorting.sortedRows, this._features.selection.mode ?? "multiple", initialSelection, () => this._onRowOrderChange?.());
1108
- this._pinnedRows = pinnedRows;
1109
- this._rowActions = rowActions;
1110
- this._onInsertRow = onInsertRow;
1111
- this._onDeleteRows = onDeleteRows;
1112
- this._onDeleteColumn = onDeleteColumn;
1113
- this._onCellUpdate = onCellUpdate;
1114
- this._onRowOrderChange = onRowOrderChange;
1115
- this._onRowAction = onRowAction;
1116
- }
1117
- };
1118
- var editorTextToCellValue = (props, value) => {
1119
- switch (props.format) {
1120
- case FormatEnum2.Ref: {
1121
- if (isLiveObject(value)) {
1122
- return Ref.make(value);
1123
- } else {
1124
- return value;
1125
- }
1126
- }
1127
- case FormatEnum2.SingleSelect: {
1128
- const ids = extractTagIds(value);
1129
- if (ids && ids.length > 0) {
1130
- return ids[0];
1131
- } else {
1132
- return value;
1133
- }
1134
- }
1135
- case FormatEnum2.MultiSelect: {
1136
- const ids = extractTagIds(value);
1137
- return ids || value;
1138
- }
1139
- default: {
1140
- return parseValue({
1141
- type: props.type,
1142
- format: props.format,
1143
- value
1144
- });
1145
- }
1146
- }
1147
- };
1148
-
1149
- // src/model/modal-controller.ts
1150
- import { signal as signal4 } from "@preact/signals-core";
1151
- function _define_property4(obj, key, value) {
1152
- if (key in obj) {
1153
- Object.defineProperty(obj, key, {
1154
- value,
1155
- enumerable: true,
1156
- configurable: true,
1157
- writable: true
1158
- });
1159
- } else {
1160
- obj[key] = value;
1161
- }
1162
- return obj;
1163
- }
1164
- var ModalController = class {
1165
- get state() {
1166
- return this._state;
1167
- }
1168
- get trigger() {
1169
- return this._triggerRef;
1170
- }
1171
- constructor() {
1172
- _define_property4(this, "_state", signal4({
1173
- type: "closed"
1174
- }));
1175
- _define_property4(this, "_triggerRef", {
1176
- current: null
1177
- });
1178
- _define_property4(this, "setTrigger", (trigger) => {
1179
- this._triggerRef.current = trigger;
1180
- });
1181
- _define_property4(this, "showRowMenu", (rowIndex) => {
1182
- this._state.value = {
1183
- type: "row",
1184
- rowIndex
1185
- };
1186
- });
1187
- _define_property4(this, "showColumnMenu", (fieldId) => {
1188
- this._state.value = {
1189
- type: "column",
1190
- fieldId
1191
- };
1192
- });
1193
- _define_property4(this, "showColumnCreator", () => {
1194
- this._state.value = {
1195
- type: "columnSettings",
1196
- mode: {
1197
- type: "create"
1198
- }
1199
- };
1200
- });
1201
- _define_property4(this, "showReferencePanel", (targetId, schemaId) => {
1202
- this._state.value = {
1203
- type: "refPanel",
1204
- targetId,
1205
- typename: schemaId
1206
- };
1207
- });
1208
- _define_property4(this, "openColumnSettings", () => {
1209
- if (this._state.value.type === "column") {
1210
- const fieldId = this._state.value.fieldId;
1211
- requestAnimationFrame(() => {
1212
- this._state.value = {
1213
- type: "columnSettings",
1214
- mode: {
1215
- type: "edit",
1216
- fieldId
1217
- }
1218
- };
1219
- });
1220
- }
1221
- });
1222
- _define_property4(this, "close", () => {
1223
- this._state.value = {
1224
- type: "closed"
1225
- };
1226
- });
1227
- }
1228
- };
1229
-
1230
- // src/model/table-presentation.ts
1231
- import { signal as signal5 } from "@preact/signals-core";
1232
- import * as Predicate from "effect/Predicate";
1233
- import { Obj as Obj3 } from "@dxos/echo";
1234
- import { FormatEnum as FormatEnum3, TypeEnum as TypeEnum2, getValue as getValue3 } from "@dxos/echo/internal";
1235
- import { cellClassesForFieldType, formatForDisplay as formatForDisplay2 } from "@dxos/react-ui-form";
1236
- import { toPlaneCellIndex } from "@dxos/react-ui-grid";
1237
- import { mx } from "@dxos/react-ui-theme";
1238
- import { VIEW_FIELD_LIMIT } from "@dxos/schema";
1239
- function _define_property5(obj, key, value) {
1240
- if (key in obj) {
1241
- Object.defineProperty(obj, key, {
1242
- value,
1243
- enumerable: true,
1244
- configurable: true,
1245
- writable: true
1246
- });
1247
- } else {
1248
- obj[key] = value;
1249
- }
1250
- return obj;
1251
- }
1252
- var TablePresentation = class {
1253
- getCells(range, plane) {
1254
- this.fieldProjectionCache.clear();
1255
- let cells;
1256
- switch (plane) {
1257
- case "grid":
1258
- this._visibleRange.value = range;
1259
- cells = this.getMainGridCells(range);
1260
- break;
1261
- case "frozenRowsStart":
1262
- cells = this.getHeaderCells(range);
1263
- break;
1264
- case "frozenColsStart":
1265
- cells = this.getSelectionColumnCells(range);
1266
- break;
1267
- case "frozenColsEnd":
1268
- cells = this.getActionColumnCells(range);
1269
- break;
1270
- case "fixedStartStart":
1271
- cells = this.getSelectAllCell();
1272
- break;
1273
- case "fixedStartEnd":
1274
- cells = this.getNewColumnCell();
1275
- break;
1276
- case "frozenRowsEnd":
1277
- cells = this.getDraftRowCells(range);
1278
- break;
1279
- case "fixedEndStart":
1280
- cells = this.getDraftIconCells(range);
1281
- break;
1282
- case "fixedEndEnd":
1283
- cells = this.getDraftActionCells(range);
1284
- break;
1285
- default:
1286
- cells = {};
1287
- }
1288
- if (plane === "grid" && this.model.features.dataEditable === false) {
1289
- Object.values(cells).forEach((cell) => {
1290
- cell.readonly = "text-select";
1291
- });
1292
- }
1293
- return cells;
1294
- }
1295
- /**
1296
- * Gets a field projection, using cache if available within the current render pass.
1297
- */
1298
- getFieldProjection(fieldId) {
1299
- let projection = this.fieldProjectionCache.get(fieldId);
1300
- if (!projection) {
1301
- projection = this.model.projection.getFieldProjection(fieldId);
1302
- this.fieldProjectionCache.set(fieldId, projection);
1303
- }
1304
- return projection;
1305
- }
1306
- createDataCell(cells, obj, field, colIndex, displayIndex) {
1307
- const { props } = this.getFieldProjection(field.id);
1308
- const cell = {
1309
- get value() {
1310
- const value = getValue3(obj, field.path);
1311
- if (value == null) {
1312
- return "";
1313
- }
1314
- if (props.type === "array") {
1315
- return "";
1316
- }
1317
- switch (props.format) {
1318
- case FormatEnum3.Boolean:
1319
- case FormatEnum3.SingleSelect:
1320
- case FormatEnum3.MultiSelect: {
1321
- return "";
1322
- }
1323
- case FormatEnum3.Ref: {
1324
- if (!field.referencePath) {
1325
- return "";
1326
- }
1327
- return getValue3(value.target, field.referencePath);
1328
- }
1329
- default: {
1330
- return formatForDisplay2({
1331
- type: props.type,
1332
- format: props.format,
1333
- value
1334
- });
1335
- }
1336
- }
1337
- }
1338
- };
1339
- const classes = [];
1340
- const formatClasses = cellClassesForFieldType(props);
1341
- if (formatClasses) {
1342
- classes.push(formatClasses);
1343
- }
1344
- const rowSelectionClasses = cellClassesForRowSelection(this.model.selection.isObjectSelected(obj), this.model.selection.selectionMode);
1345
- if (rowSelectionClasses) {
1346
- classes.push(rowSelectionClasses);
1347
- }
1348
- cell.className = mx(classes);
1349
- if (props.type === TypeEnum2.Array) {
1350
- const targetArray = getValue3(obj, field.path);
1351
- if (targetArray && Array.isArray(targetArray)) {
1352
- const getLabel = (value) => {
1353
- if (!value) {
1354
- return void 0;
1355
- }
1356
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
1357
- return String(value);
1358
- }
1359
- if (Array.isArray(value)) {
1360
- return "Array";
1361
- }
1362
- if (typeof value === "object" && value.name) {
1363
- return value.name;
1364
- }
1365
- if (typeof value === "object" && value.value) {
1366
- return `${value.value}`;
1367
- }
1368
- return "Object";
1369
- };
1370
- const tags = targetArray.map(getLabel).filter(Predicate.isNotNullable).map((title) => {
1371
- return `<span class="dx-tag" data-hue="neutral">${title}</span>`;
1372
- }).join("");
1373
- cell.accessoryHtml = `<div role='none' class="flex flex-row gap-1 overflow-x-auto">${tags}</div>`;
1374
- }
1375
- }
1376
- if (props.format === FormatEnum3.Ref && props.referenceSchema) {
1377
- const targetObj = getValue3(obj, field.path)?.target;
1378
- if (targetObj) {
1379
- const dxn = Obj3.getDXN(targetObj)?.toString();
1380
- cell.accessoryHtml = `<div role="none" class="absolute inline-end-0 inset-block-0 p-[--dx-grid-cell-content-padding-block]"><dx-anchor refId=${dxn} class="dx-button is-6 aspect-square min-bs-0" data-dx-grid-action="accessory"><dx-icon icon="ph--link-simple--regular"/></dx-anchor></div>`;
1381
- }
1382
- }
1383
- if (props.format === FormatEnum3.Boolean) {
1384
- const value = getValue3(obj, field.path);
1385
- cell.accessoryHtml = tableControls.switch.render({
1386
- colIndex,
1387
- rowIndex: displayIndex,
1388
- checked: value ?? false
1389
- });
1390
- cell.readonly = "no-text-select";
1391
- }
1392
- if (props.format === FormatEnum3.SingleSelect) {
1393
- const value = getValue3(obj, field.path);
1394
- const options = this.model.projection.getFieldProjection(field.id).props.options;
1395
- if (options) {
1396
- const option = options.find((o) => o.id === value);
1397
- if (option) {
1398
- cell.accessoryHtml = `<span class="dx-tag" data-hue="${option.color}">${option.title}</span>`;
1399
- }
1400
- }
1401
- }
1402
- if (props.format === FormatEnum3.MultiSelect) {
1403
- const values = getValue3(obj, field.path);
1404
- const options = this.model.projection.getFieldProjection(field.id).props.options;
1405
- if (options && values && values.length > 0) {
1406
- const tags = values.map((value) => {
1407
- const option = options.find((o) => o.id === value);
1408
- if (option) {
1409
- return `<span class="dx-tag" data-hue="${option.color}">${option.title}</span>`;
1410
- }
1411
- return null;
1412
- }).filter(Boolean).join("");
1413
- if (tags) {
1414
- cell.accessoryHtml = tags;
1415
- }
1416
- }
1417
- }
1418
- const idx = toPlaneCellIndex({
1419
- col: colIndex,
1420
- row: displayIndex
1421
- });
1422
- cells[idx] = cell;
1423
- }
1424
- getMainGridCells(range) {
1425
- const cells = {};
1426
- const fields = this.model.projection?.fields ?? [];
1427
- for (let row = range.start.row; row <= range.end.row && row < this.model.getRowCount(); row++) {
1428
- for (let col = range.start.col; col <= range.end.col && col < fields.length; col++) {
1429
- const field = fields[col];
1430
- if (!field) {
1431
- continue;
1432
- }
1433
- this.createDataCell(cells, this.model.rows.value[row], field, col, row);
1434
- }
1435
- }
1436
- return cells;
1437
- }
1438
- getDraftRowCells(range) {
1439
- const cells = {};
1440
- const fields = this.model.projection?.fields ?? [];
1441
- const draftRows = this.model.draftRows.value;
1442
- if (draftRows.length === 0) {
1443
- for (let col = range.start.col; col <= range.end.col && col < fields.length; col++) {
1444
- cells[toPlaneCellIndex({
1445
- col,
1446
- row: 0
1447
- })] = {
1448
- value: "",
1449
- readonly: true,
1450
- className: "dx-grid__row--cta__cell"
1451
- };
1452
- }
1453
- } else {
1454
- for (let row = range.start.row; row <= range.end.row && row < draftRows.length; row++) {
1455
- const draftRow = draftRows[row];
1456
- for (let col = range.start.col; col <= range.end.col && col < fields.length; col++) {
1457
- const cellIndex = toPlaneCellIndex({
1458
- col,
1459
- row
1460
- });
1461
- const field = fields[col];
1462
- if (!field) {
1463
- continue;
1464
- }
1465
- this.createDataCell(cells, draftRow.data, field, col, row);
1466
- if (this.model.hasDraftRowValidationError(row, field.path)) {
1467
- const cellValue = cells[cellIndex];
1468
- if (cellValue) {
1469
- const existingClasses = cellValue.className || "";
1470
- const draftClasses = "dx-grid__cell--flagged";
1471
- cellValue.className = existingClasses ? `${existingClasses} ${draftClasses}` : draftClasses;
1472
- }
1473
- }
1474
- }
1475
- }
1476
- }
1477
- return cells;
1478
- }
1479
- getHeaderCells(range) {
1480
- const cells = {};
1481
- const fields = this.model.projection?.fields ?? [];
1482
- for (let col = range.start.col; col <= range.end.col && col < fields.length; col++) {
1483
- const { field, props } = this.model.projection.getFieldProjection(fields[col].id);
1484
- const sorting = this.model.sorting?.sorting;
1485
- const direction = sorting?.fieldId === field.id ? sorting.direction : void 0;
1486
- cells[toPlaneCellIndex({
1487
- col,
1488
- row: 0
1489
- })] = {
1490
- // TODO(burdon): Use same logic as form for fallback title.
1491
- value: "",
1492
- resizeHandle: "col",
1493
- accessoryHtml: `
1494
- <span class="grow min-is-0 truncate">${props.title ?? field.path}</span>
1495
- ${direction !== void 0 ? tableButtons.sort.render({
1496
- fieldId: field.id,
1497
- direction
1498
- }) : ""}
1499
- ${tableButtons.columnSettings.render({
1500
- fieldId: field.id
1501
- })}
1502
- `,
1503
- className: "!bg-toolbarSurface !text-description [&>div]:flex [&>div]:items-stretch"
1504
- };
1505
- }
1506
- return cells;
1507
- }
1508
- getSelectionColumnCells(range) {
1509
- const cells = {};
1510
- for (let row = range.start.row; row <= range.end.row && row < this.model.getRowCount(); row++) {
1511
- const isSelected = this.model.selection.isRowIndexSelected(row);
1512
- const classes = cellClassesForRowSelection(isSelected, this.model.selection.selectionMode);
1513
- cells[toPlaneCellIndex({
1514
- col: 0,
1515
- row
1516
- })] = {
1517
- value: "",
1518
- readonly: "no-text-select",
1519
- className: classes ? mx(classes) : void 0,
1520
- accessoryHtml: tableControls.checkbox.render({
1521
- rowIndex: row,
1522
- checked: isSelected
1523
- })
1524
- };
1525
- }
1526
- return cells;
1527
- }
1528
- getActionColumnCells(range) {
1529
- const cells = {};
1530
- for (let row = range.start.row; row <= range.end.row && row < this.model.getRowCount(); row++) {
1531
- const isSelected = this.model.selection.isRowIndexSelected(row);
1532
- const classes = cellClassesForRowSelection(isSelected, this.model.selection.selectionMode);
1533
- cells[toPlaneCellIndex({
1534
- col: 0,
1535
- row
1536
- })] = {
1537
- value: "",
1538
- readonly: true,
1539
- className: classes ? mx(classes) : void 0,
1540
- accessoryHtml: tableButtons.rowMenu.render({
1541
- rowIndex: row
1542
- })
1543
- };
1544
- }
1545
- return cells;
1546
- }
1547
- getSelectAllCell() {
1548
- if (!this.model.features.selection.enabled || this.model.selection.selectionMode === "single") {
1549
- return {
1550
- [toPlaneCellIndex({
1551
- col: 0,
1552
- row: 0
1553
- })]: {
1554
- className: "!bg-toolbarSurface",
1555
- readonly: true,
1556
- value: ""
1557
- }
1558
- };
1559
- }
1560
- return {
1561
- [toPlaneCellIndex({
1562
- col: 0,
1563
- row: 0
1564
- })]: {
1565
- accessoryHtml: tableControls.checkbox.render({
1566
- rowIndex: 0,
1567
- header: true,
1568
- checked: this.model.selection.allRowsSeleted.value
1569
- }),
1570
- className: "!bg-toolbarSurface",
1571
- readonly: true,
1572
- value: ""
1573
- }
1574
- };
1575
- }
1576
- getNewColumnCell() {
1577
- return {
1578
- [toPlaneCellIndex({
1579
- col: 0,
1580
- row: 0
1581
- })]: {
1582
- accessoryHtml: this.model.features.schemaEditable ? tableButtons.addColumn.render({
1583
- disabled: (this.model.projection?.fields?.length ?? 0) >= VIEW_FIELD_LIMIT
1584
- }) : void 0,
1585
- className: "!bg-toolbarSurface",
1586
- readonly: true,
1587
- value: ""
1588
- }
1589
- };
1590
- }
1591
- getDraftActionCells(range) {
1592
- const cells = {};
1593
- const draftRows = this.model.draftRows.value;
1594
- if (draftRows.length === 0) {
1595
- cells[toPlaneCellIndex({
1596
- col: 0,
1597
- row: 0
1598
- })] = {
1599
- value: "",
1600
- className: "dx-grid__row--cta__cell",
1601
- readonly: true
1602
- };
1603
- } else {
1604
- for (let row = range.start.row; row <= range.end.row && row < draftRows.length; row++) {
1605
- const draftRow = draftRows[row];
1606
- const disabled = !draftRow.valid;
1607
- cells[toPlaneCellIndex({
1608
- col: 0,
1609
- row
1610
- })] = {
1611
- value: "",
1612
- readonly: true,
1613
- accessoryHtml: tableButtons.saveDraftRow.render({
1614
- rowIndex: row,
1615
- disabled
1616
- })
1617
- };
1618
- }
1619
- }
1620
- return cells;
1621
- }
1622
- getDraftIconCells(range) {
1623
- const cells = {};
1624
- const draftRows = this.model.draftRows.value;
1625
- for (let row = range.start.row; row <= range.end.row; row++) {
1626
- cells[toPlaneCellIndex({
1627
- col: 0,
1628
- row
1629
- })] = {
1630
- value: "",
1631
- readonly: true,
1632
- accessoryHtml: '<dx-icon icon="ph--plus--regular" class="contents"></dx-icon>',
1633
- className: mx("[&>div]:grid [&>div]:place-content-center", draftRows.length < 1 && "dx-grid__row--cta__cell")
1634
- };
1635
- }
1636
- return cells;
1637
- }
1638
- constructor(model, _visibleRange = signal5({
1639
- start: {
1640
- row: 0,
1641
- col: 0
1642
- },
1643
- end: {
1644
- row: 0,
1645
- col: 0
1646
- }
1647
- })) {
1648
- _define_property5(this, "model", void 0);
1649
- _define_property5(this, "_visibleRange", void 0);
1650
- _define_property5(this, "fieldProjectionCache", void 0);
1651
- this.model = model;
1652
- this._visibleRange = _visibleRange;
1653
- this.fieldProjectionCache = /* @__PURE__ */ new Map();
1654
- }
1655
- };
1656
- var cellClassesForRowSelection = (selected, selectionMode) => {
1657
- if (!selected) {
1658
- if (selectionMode === "single") {
1659
- return [
1660
- "!cursor-pointer"
1661
- ];
1662
- } else {
1663
- return void 0;
1664
- }
1665
- }
1666
- switch (selectionMode) {
1667
- case "single":
1668
- return [
1669
- "!bg-currentRelated dx-grid__cell--no-focus-unfurl hover:bg-hoverSurface !cursor-pointer"
1670
- ];
1671
- case "multiple":
1672
- return [
1673
- "!bg-gridCellSelected"
1674
- ];
1675
- }
1676
- };
1677
-
1678
- // src/hooks/useTableModel.ts
1679
- import { useSignals as _useSignals } from "@preact-signals/safe-react/tracking";
1680
- import { effect as effect3 } from "@preact/signals-core";
1681
- import orderBy2 from "lodash.orderby";
1682
- import { useEffect, useMemo, useState } from "react";
1683
- import { fullyQualifiedId as fullyQualifiedId2 } from "@dxos/react-client/echo";
1684
- import { useSelected, useSelectionActions } from "@dxos/react-ui-attention";
1685
- import { isNonNullable } from "@dxos/util";
1686
- var useTableModel = ({ view, schema, projection, rows, rowActions, features, onSelectionChanged, onRowAction, ...props }) => {
1687
- var _effect = _useSignals();
1688
- try {
1689
- const selected = useSelected(view && fullyQualifiedId2(view), "multi");
1690
- const initialSelection = useMemo(() => selected, [
1691
- view
1692
- ]);
1693
- const [model, setModel] = useState();
1694
- useEffect(() => {
1695
- if (!view || !schema) {
1696
- return;
1697
- }
1698
- let model2;
1699
- const t = setTimeout(async () => {
1700
- model2 = new TableModel({
1701
- view,
1702
- schema,
1703
- projection,
1704
- features,
1705
- rowActions,
1706
- initialSelection,
1707
- onRowAction,
1708
- ...props
1709
- });
1710
- await model2.open();
1711
- setModel(model2);
1712
- });
1713
- return () => {
1714
- clearTimeout(t);
1715
- void model2?.close();
1716
- };
1717
- }, [
1718
- view,
1719
- schema,
1720
- projection,
1721
- features,
1722
- rowActions,
1723
- initialSelection
1724
- ]);
1725
- useEffect(() => {
1726
- if (rows) {
1727
- const sortedRows = orderBy2(rows, [
1728
- (row) => String(row.id)
1729
- ], [
1730
- "asc"
1731
- ]);
1732
- model?.setRows(sortedRows);
1733
- }
1734
- }, [
1735
- model,
1736
- rows
1737
- ]);
1738
- const { multiSelect, clear } = useSelectionActions([
1739
- model?.id
1740
- ].filter(isNonNullable));
1741
- useEffect(() => {
1742
- if (!model) {
1743
- return;
1744
- }
1745
- const unsubscribe = effect3(() => {
1746
- const selectedItems = [
1747
- ...model.selection.selection.value
1748
- ];
1749
- multiSelect(selectedItems);
1750
- onSelectionChanged?.(selectedItems);
1751
- });
1752
- return () => {
1753
- clear();
1754
- unsubscribe();
1755
- };
1756
- }, [
1757
- model,
1758
- onSelectionChanged
1759
- ]);
1760
- return model;
1761
- } finally {
1762
- _effect.f();
1763
- }
1764
- };
1765
-
1766
- // src/hooks/useAddRow.ts
1767
- import { useCallback } from "react";
1768
- import { Obj as Obj4 } from "@dxos/echo";
1769
- var useAddRow = ({ space, schema }) => {
1770
- return useCallback((data) => {
1771
- if (space && schema) {
1772
- try {
1773
- space.db.add(Obj4.make(schema, data ?? {}));
1774
- return "final";
1775
- } catch (error) {
1776
- return "draft";
1777
- }
1778
- }
1779
- return "final";
1780
- }, [
1781
- space,
1782
- schema
1783
- ]);
1784
- };
1785
-
1786
- export {
1787
- getBaseSchema,
1788
- makeDynamicTable,
1789
- touch,
1790
- BUTTON_IDENTIFIERS,
1791
- tableButtons,
1792
- CONTROL_IDENTIFIERS,
1793
- CheckboxStory,
1794
- SwitchStory,
1795
- tableControls,
1796
- narrowSchema,
1797
- TableModel,
1798
- ModalController,
1799
- TablePresentation,
1800
- cellClassesForRowSelection,
1801
- useTableModel,
1802
- useAddRow
1803
- };
1804
- //# sourceMappingURL=chunk-OZTXLZGM.mjs.map