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