@dxos/react-ui-table 0.8.4-main.dedc0f3 → 0.8.4-main.dfabb4ec29

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