@dxos/react-ui-table 0.8.4-main.fd6878d → 0.9.0

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