viewkitjs 0.32.0 → 0.34.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 (285) hide show
  1. package/CHANGELOG.md +98 -0
  2. package/fields/dist/index.d.ts +1 -1
  3. package/fields/dist/index.js +1 -1
  4. package/fields/dist/query/evaluate.d.ts +7 -0
  5. package/fields/dist/query/exact-number.d.ts +8 -2
  6. package/fields/dist/query/exact-number.js +14 -1
  7. package/fields/dist/query/sort.js +13 -10
  8. package/fields/dist/registry.js +5 -1
  9. package/fields/dist/types.d.ts +4 -1
  10. package/fields/dist/zod.d.ts +5 -0
  11. package/fields/dist/zod.js +10 -1
  12. package/fields/docs/guides/contact-values.md +12 -0
  13. package/fields/docs/guides/field-types.md +1 -1
  14. package/fields/docs/guides/queries.md +3 -1
  15. package/fields/docs/reference/fields.md +1 -0
  16. package/package.json +1 -1
  17. package/postgres/dist/check.js +4 -1
  18. package/postgres/dist/definitions/formula-forms.js +3 -1
  19. package/postgres/dist/definitions/money.js +2 -1
  20. package/postgres/dist/page.js +6 -3
  21. package/postgres/dist/rules.d.ts +15 -1
  22. package/postgres/dist/rules.js +16 -0
  23. package/postgres/dist/sort.js +7 -2
  24. package/postgres/docs/guides/definitions.md +1 -1
  25. package/postgres/docs/guides/field-map.md +1 -1
  26. package/postgres/docs/reference/definitions.md +1 -1
  27. package/postgres/docs/reference/postgres.md +1 -1
  28. package/views/dist/atoms/ToolbarButton.d.ts +1 -1
  29. package/views/dist/atoms/ToolbarButton.js +4 -3
  30. package/views/dist/atoms/frame-parts.d.ts +6 -0
  31. package/views/dist/atoms/frame-parts.js +15 -0
  32. package/views/dist/atoms/types.d.ts +16 -7
  33. package/views/dist/cells/cell-title.d.ts +3 -1
  34. package/views/dist/cells/cell-title.js +2 -2
  35. package/views/dist/cells/cell-values.js +2 -2
  36. package/views/dist/contract.d.ts +46 -11
  37. package/views/dist/contract.js +157 -0
  38. package/views/dist/controls/alert/alert.js +2 -1
  39. package/views/dist/controls/alert-dialog/alert-dialog.js +3 -2
  40. package/views/dist/controls/bulk-bar/bulk-bar.js +25 -22
  41. package/views/dist/controls/button/map-button.js +2 -0
  42. package/views/dist/controls/checkbox/checkbox.d.ts +3 -2
  43. package/views/dist/controls/checkbox/checkbox.js +2 -2
  44. package/views/dist/controls/compound-editor/compound-editor.d.ts +1 -1
  45. package/views/dist/controls/compound-editor/name-parts.js +4 -4
  46. package/views/dist/controls/compound-editor/place-parts.js +19 -19
  47. package/views/dist/controls/compound-editor/range-parts.js +14 -14
  48. package/views/dist/controls/compound-editor/row.d.ts +3 -1
  49. package/views/dist/controls/compound-editor/row.js +2 -2
  50. package/views/dist/controls/controls.d.ts +4 -0
  51. package/views/dist/controls/controls.js +4 -0
  52. package/views/dist/controls/customize/customize.js +43 -39
  53. package/views/dist/controls/customize/map-customize-body.d.ts +2 -0
  54. package/views/dist/controls/customize/map-customize-body.js +7 -2
  55. package/views/dist/controls/date-editor/date-days.js +2 -2
  56. package/views/dist/controls/date-editor/date-editor.d.ts +3 -0
  57. package/views/dist/controls/date-editor/date-editor.js +27 -21
  58. package/views/dist/controls/date-editor/date-parts.js +6 -6
  59. package/views/dist/controls/date-editor/map-calendar.js +3 -1
  60. package/views/dist/controls/date-editor/map-date-body.d.ts +1 -1
  61. package/views/dist/controls/date-editor/map-date-body.js +7 -3
  62. package/views/dist/controls/date-editor.css +2 -2
  63. package/views/dist/controls/field/field.d.ts +3 -0
  64. package/views/dist/controls/field/field.js +6 -6
  65. package/views/dist/controls/filter-editor/block.d.ts +2 -2
  66. package/views/dist/controls/filter-editor/block.js +5 -5
  67. package/views/dist/controls/filter-editor/filter-editor.js +31 -33
  68. package/views/dist/controls/filter-editor/how.d.ts +3 -1
  69. package/views/dist/controls/filter-editor/map-filter-body.d.ts +3 -0
  70. package/views/dist/controls/filter-editor/map-filter-body.js +11 -4
  71. package/views/dist/controls/filter-editor/map-rules.js +1 -1
  72. package/views/dist/controls/filter-editor/parts.d.ts +3 -1
  73. package/views/dist/controls/filter-editor/parts.js +8 -6
  74. package/views/dist/controls/group-editor/group-editor.js +17 -14
  75. package/views/dist/controls/group-editor/map-group-body.d.ts +1 -0
  76. package/views/dist/controls/group-editor/map-group-body.js +5 -1
  77. package/views/dist/controls/index.d.ts +2 -0
  78. package/views/dist/controls/index.js +1 -0
  79. package/views/dist/controls/input/input.js +4 -3
  80. package/views/dist/controls/input/map-input.js +2 -0
  81. package/views/dist/controls/internal/editor-parts.js +1 -1
  82. package/views/dist/controls/internal/head-button.d.ts +9 -0
  83. package/views/dist/controls/internal/head-button.js +13 -0
  84. package/views/dist/controls/internal/map-panel-controls.d.ts +8 -0
  85. package/views/dist/controls/internal/map-panel-controls.js +20 -0
  86. package/views/dist/controls/internal/panel-controls.d.ts +58 -0
  87. package/views/dist/controls/internal/panel-controls.js +240 -0
  88. package/views/dist/controls/internal/part-slot.d.ts +12 -0
  89. package/views/dist/controls/internal/part-slot.js +5 -0
  90. package/views/dist/controls/links-editor/links-editor.d.ts +1 -1
  91. package/views/dist/controls/links-editor/links-editor.js +27 -27
  92. package/views/dist/controls/menu/map-menu.d.ts +2 -2
  93. package/views/dist/controls/menu/map-menu.js +15 -12
  94. package/views/dist/controls/menu/menu.d.ts +3 -0
  95. package/views/dist/controls/menu/menu.js +5 -5
  96. package/views/dist/controls/menu-list/menu-list.d.ts +14 -4
  97. package/views/dist/controls/menu-list/menu-list.js +23 -22
  98. package/views/dist/controls/modal/modal.js +5 -3
  99. package/views/dist/controls/notice/notice.d.ts +3 -1
  100. package/views/dist/controls/notice/notice.js +2 -2
  101. package/views/dist/controls/number-editor/map-number-body.js +5 -1
  102. package/views/dist/controls/number-editor/number-editor.d.ts +1 -1
  103. package/views/dist/controls/number-editor/number-editor.js +16 -16
  104. package/views/dist/controls/number-field/number-field.js +5 -3
  105. package/views/dist/controls/pane/map-pane.d.ts +3 -0
  106. package/views/dist/controls/pane/map-pane.js +5 -0
  107. package/views/dist/controls/pane/pane.d.ts +4 -2
  108. package/views/dist/controls/pane/pane.js +4 -4
  109. package/views/dist/controls/picker/map-fields-body.js +25 -1
  110. package/views/dist/controls/picker/picker.d.ts +2 -0
  111. package/views/dist/controls/picker/picker.js +37 -36
  112. package/views/dist/controls/picker/rows.d.ts +4 -0
  113. package/views/dist/controls/picker.css +10 -1
  114. package/views/dist/controls/popover/map-popover.d.ts +1 -1
  115. package/views/dist/controls/popover/map-popover.js +4 -2
  116. package/views/dist/controls/quick-filters/map-quick-filters-body.d.ts +2 -1
  117. package/views/dist/controls/quick-filters/map-quick-filters-body.js +7 -3
  118. package/views/dist/controls/quick-filters/quick-filters.js +8 -6
  119. package/views/dist/controls/radio/radio.d.ts +3 -0
  120. package/views/dist/controls/radio/radio.js +4 -3
  121. package/views/dist/controls/select/map-select.d.ts +3 -0
  122. package/views/dist/controls/select/map-select.js +8 -0
  123. package/views/dist/controls/select/select.d.ts +3 -0
  124. package/views/dist/controls/select/select.js +3 -3
  125. package/views/dist/controls/setting-row/looks.js +5 -5
  126. package/views/dist/controls/setting-row/setting-row.d.ts +3 -1
  127. package/views/dist/controls/setting-row/setting-row.js +3 -1
  128. package/views/dist/controls/setting-row.css +3 -2
  129. package/views/dist/controls/sheet/bottom-sheet.d.ts +8 -2
  130. package/views/dist/controls/sheet/bottom-sheet.js +4 -4
  131. package/views/dist/controls/sheet/map-sheet.js +1 -1
  132. package/views/dist/controls/sort-editor/map-sort-body.d.ts +1 -0
  133. package/views/dist/controls/sort-editor/map-sort-body.js +8 -4
  134. package/views/dist/controls/sort-editor/sort-editor.js +24 -26
  135. package/views/dist/controls/sort-editor.css +5 -0
  136. package/views/dist/controls/text-editor/map-text-body.js +5 -1
  137. package/views/dist/controls/text-editor/text-editor.d.ts +1 -1
  138. package/views/dist/controls/text-editor/text-editor.js +11 -10
  139. package/views/dist/controls/toolbar/toolbar.d.ts +4 -2
  140. package/views/dist/controls/toolbar/toolbar.js +2 -2
  141. package/views/dist/controls/toolbar-button/atom-toolbar-button.d.ts +3 -3
  142. package/views/dist/controls/toolbar-button/atom-toolbar-button.js +7 -6
  143. package/views/dist/controls/values-editor/values-editor.d.ts +1 -1
  144. package/views/dist/controls/values-editor/values-editor.js +22 -22
  145. package/views/dist/controls/views-list/map-view-menu.d.ts +3 -2
  146. package/views/dist/controls/views-list/map-view-menu.js +7 -3
  147. package/views/dist/controls/views-list/map-views-list-body.d.ts +2 -1
  148. package/views/dist/controls/views-list/map-views-list-body.js +6 -5
  149. package/views/dist/controls/views-list/view-menu.js +9 -10
  150. package/views/dist/controls/views-list/views-list.js +6 -6
  151. package/views/dist/editors/EditorPanel.js +4 -4
  152. package/views/dist/field-row/FieldRow.js +13 -0
  153. package/views/dist/fields/money.d.ts +5 -2
  154. package/views/dist/fields/money.js +14 -2
  155. package/views/dist/grid/chrome/GridTable.js +4 -1
  156. package/views/dist/grid/chrome/cell-clipboard.d.ts +4 -2
  157. package/views/dist/grid/chrome/cell-clipboard.js +2 -2
  158. package/views/dist/grid/chrome/cells.js +3 -1
  159. package/views/dist/grid/chrome/grid-row-menu-items.js +1 -1
  160. package/views/dist/grid/chrome/useGridKeyboard.d.ts +4 -1
  161. package/views/dist/grid/chrome/useGridKeyboard.js +29 -6
  162. package/views/dist/grid/chrome/useGridTableInteractionModel.js +9 -2
  163. package/views/dist/grid/core/clipboard-model.d.ts +3 -1
  164. package/views/dist/grid/core/clipboard-model.js +9 -8
  165. package/views/dist/grid/core/editing-controller.d.ts +3 -0
  166. package/views/dist/grid/core/editing-controller.js +6 -0
  167. package/views/dist/grid/core/edits-sink.d.ts +19 -0
  168. package/views/dist/grid/core/edits-sink.js +2 -0
  169. package/views/dist/grid/core/spreadsheet-operations.js +1 -1
  170. package/views/dist/grid/core/useGridEditing.d.ts +5 -0
  171. package/views/dist/grid/core/useGridEditing.js +6 -0
  172. package/views/dist/list/answer.d.ts +40 -0
  173. package/views/dist/list/answer.js +43 -0
  174. package/views/dist/list/bulk-bar.d.ts +7 -0
  175. package/views/dist/list/bulk-bar.js +10 -0
  176. package/views/dist/list/columns.d.ts +24 -0
  177. package/views/dist/list/columns.js +27 -0
  178. package/views/dist/list/edits.d.ts +19 -0
  179. package/views/dist/list/edits.js +56 -0
  180. package/views/dist/list/grid.d.ts +11 -0
  181. package/views/dist/list/grid.js +76 -0
  182. package/views/dist/list/index.d.ts +14 -1
  183. package/views/dist/list/index.js +4 -1
  184. package/views/dist/list/link.d.ts +8 -0
  185. package/views/dist/list/link.js +78 -0
  186. package/views/dist/list/list.d.ts +74 -0
  187. package/views/dist/list/list.js +18 -0
  188. package/views/dist/list/parts.js +11 -3
  189. package/views/dist/list/root-base.d.ts +46 -0
  190. package/views/dist/list/root-base.js +137 -0
  191. package/views/dist/list/root.d.ts +35 -34
  192. package/views/dist/list/root.js +153 -113
  193. package/views/dist/list/source-answers.d.ts +39 -0
  194. package/views/dist/list/source-answers.js +225 -0
  195. package/views/dist/list/store.d.ts +37 -5
  196. package/views/dist/list/store.js +31 -0
  197. package/views/dist/list/toolbar.d.ts +3 -2
  198. package/views/dist/list/toolbar.js +23 -5
  199. package/views/dist/list/views-axis.js +4 -2
  200. package/views/dist/list/views.d.ts +7 -2
  201. package/views/dist/list/views.js +16 -5
  202. package/views/dist/model/column-capabilities.d.ts +13 -0
  203. package/views/dist/model/column-capabilities.js +23 -2
  204. package/views/dist/model/column-schema.d.ts +11 -1
  205. package/views/dist/model/view-rules.js +1 -0
  206. package/views/dist/panels/model.d.ts +2 -0
  207. package/views/dist/panels/react/toolbar/ReadyToolbar.d.ts +21 -2
  208. package/views/dist/panels/react/toolbar/ReadyToolbar.js +2 -2
  209. package/views/dist/panels/react/toolbar/Toolbar.d.ts +12 -1
  210. package/views/dist/panels/react/toolbar/Toolbar.js +20 -6
  211. package/views/dist/panels/react/toolbar/ToolbarAdvancedFilters.js +4 -3
  212. package/views/dist/panels/react/toolbar/ToolbarAsideFilter.js +2 -2
  213. package/views/dist/panels/react/toolbar/ToolbarBarFilter.js +4 -4
  214. package/views/dist/panels/react/toolbar/ToolbarButton.js +4 -1
  215. package/views/dist/panels/react/toolbar/ToolbarFilter.js +1 -1
  216. package/views/dist/panels/react/toolbar/ToolbarFilterAdd.js +4 -3
  217. package/views/dist/panels/react/toolbar/ToolbarFilterSheet.js +1 -1
  218. package/views/dist/panels/react/toolbar/ToolbarFilters.js +5 -4
  219. package/views/dist/panels/react/toolbar/ToolbarNew.js +1 -1
  220. package/views/dist/panels/react/toolbar/ToolbarOptions.js +1 -1
  221. package/views/dist/panels/react/toolbar/ToolbarQuickFilters.js +1 -1
  222. package/views/dist/panels/react/toolbar/ToolbarSearch.js +1 -1
  223. package/views/dist/panels/react/toolbar/ToolbarSort.js +2 -2
  224. package/views/dist/panels/react/toolbar/ToolbarSortPill.js +8 -5
  225. package/views/dist/panels/react/toolbar/context.d.ts +31 -1
  226. package/views/dist/panels/react/toolbar/context.js +4 -0
  227. package/views/dist/panels/react/toolbar/pane-host.d.ts +6 -0
  228. package/views/dist/panels/react/toolbar/pane-host.js +17 -0
  229. package/views/dist/panels/react/toolbar/panel-part.d.ts +4 -1
  230. package/views/dist/panels/react/toolbar/panel-part.js +7 -4
  231. package/views/dist/panels/react.d.ts +1 -1
  232. package/views/dist/panels/settings/filter-groups.js +6 -1
  233. package/views/dist/provider/ViewsProvider.d.ts +7 -2
  234. package/views/dist/provider/ViewsProvider.js +18 -1
  235. package/views/dist/provider/components.d.ts +90 -2
  236. package/views/dist/provider/components.js +1 -1
  237. package/views/dist/provider/context.d.ts +4 -1
  238. package/views/dist/provider/own-control.d.ts +4 -0
  239. package/views/dist/provider/own-control.js +7 -0
  240. package/views/dist/provider/select-items.d.ts +3 -0
  241. package/views/dist/provider/select-items.js +6 -0
  242. package/views/dist/react.d.ts +4 -2
  243. package/views/dist/react.js +2 -0
  244. package/views/dist/styles.css +107 -80
  245. package/views/dist/styles.unlayered.css +107 -80
  246. package/views/docs/contract.md +310 -7
  247. package/views/docs/controls/alert-dialog.md +4 -0
  248. package/views/docs/controls/alert.md +4 -0
  249. package/views/docs/controls/bulk-bar.md +4 -0
  250. package/views/docs/controls/compound-editor.md +4 -0
  251. package/views/docs/controls/customize.md +8 -0
  252. package/views/docs/controls/date-editor.md +6 -0
  253. package/views/docs/controls/field.md +4 -0
  254. package/views/docs/controls/filter-editor.md +8 -0
  255. package/views/docs/controls/group-editor.md +8 -0
  256. package/views/docs/controls/input.md +4 -0
  257. package/views/docs/controls/links-editor.md +4 -0
  258. package/views/docs/controls/menu-list.md +8 -0
  259. package/views/docs/controls/menu.md +6 -0
  260. package/views/docs/controls/modal.md +4 -0
  261. package/views/docs/controls/notice.md +4 -0
  262. package/views/docs/controls/number-editor.md +6 -0
  263. package/views/docs/controls/pane.md +4 -0
  264. package/views/docs/controls/picker.md +8 -0
  265. package/views/docs/controls/quick-filters.md +8 -0
  266. package/views/docs/controls/radio.md +4 -0
  267. package/views/docs/controls/select.md +8 -0
  268. package/views/docs/controls/sheet.md +4 -0
  269. package/views/docs/controls/sort-editor.md +8 -0
  270. package/views/docs/controls/text-editor.md +6 -0
  271. package/views/docs/controls/toolbar.md +4 -0
  272. package/views/docs/controls/values-editor.md +4 -0
  273. package/views/docs/controls/views-list.md +8 -0
  274. package/views/docs/guides/the-grid.md +2 -2
  275. package/views/docs/guides/the-provider.md +19 -2
  276. package/views/docs/reference/controls.md +11 -7
  277. package/views/docs/reference/field-row.md +1 -1
  278. package/views/docs/reference/grid.md +1 -1
  279. package/views/docs/reference/list.md +143 -11
  280. package/views/docs/reference/looks.md +1 -1
  281. package/views/docs/reference/panels-react.md +6 -5
  282. package/views/docs/reference/react.md +19 -12
  283. package/views/docs/reference/views.md +11 -5
  284. package/views/docs/reshaping.md +17 -1
  285. package/views/skills/views/SKILL.md +1 -1
@@ -1,128 +1,168 @@
1
1
  "use client";
2
- import { createContext, createElement, useCallback, useContext, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore, } from "react";
3
- import { LIST_SETTINGS_DEFAULTS } from "viewkitjs/fields/query";
4
- import { ListRadioContext, ToolbarRadioContext, useToolbarRadioState } from "../panels/react/toolbar/context.js";
5
- import { ListRegistryContext } from "./registry.js";
2
+ import { createElement, useContext, useLayoutEffect, useMemo, useReducer, useRef, useState } from "react";
3
+ import { presentedDef } from "viewkitjs/fields";
4
+ import { bucketRuleFor, } from "viewkitjs/fields/query";
6
5
  import { ViewsContext } from "../provider/context.js";
7
- import { browserViewsStore } from "./browser-views-store.js";
8
- import { createListStore } from "./store.js";
9
- import { createListViews } from "./views.js";
10
- const ListStoreContext = createContext(null);
11
- export function ListRoot({ id, children, ...props }) {
12
- const mode = useStoreMode(id, props);
13
- const extra = { sortFilter: props.sortFilter, bar: props.bar };
14
- const sync = mode ? mode.views.sync(extra) : props;
15
- const [store] = useState(() => createListStore(id, sync));
16
- store.sync(sync);
17
- useLayoutEffect(() => store.flush());
18
- useStoreViews(mode, store, extra);
19
- const registry = useContext(ListRegistryContext);
20
- useLayoutEffect(() => registry?.addStore(store.get().id, store), [registry, store]);
21
- const radio = useToolbarRadioState();
22
- return createElement(ListStoreContext.Provider, { value: store }, createElement(ListRadioContext.Provider, { value: radio }, createElement(ToolbarRadioContext.Provider, { value: radio }, children)));
6
+ import { deriveGridSelectValues } from "../grid/core/column-values.js";
7
+ import { createListAnswers, requestSettingsOf } from "./source-answers.js";
8
+ import { answerInMemory } from "./answer.js";
9
+ import { columnsOf } from "./columns.js";
10
+ import { ListCommitsContext, ListEditsSinkContext, useListCommits, useListEditsSink } from "./edits.js";
11
+ import { ListRootBase, useList, useListStore } from "./root-base.js";
12
+ export { useList, useListPanel, useListStore } from "./root-base.js";
13
+ export function ListRoot({ fields, rows, source, pageSize, quickFilters, columns, save, can, children, ...base }) {
14
+ const [defaults] = useState(() => base.settings === undefined && base.defaults === undefined && fields ? columnsOf(fields, columns, false).defaults : undefined);
15
+ return createElement(ListRootBase, { ...base, ...(defaults && { defaults }), ...(can?.share === false && { share: false }) }, createElement((RootData), { fields, rows, source, pageSize, quickFilters, columns, save, can }, children));
23
16
  }
24
- export function useListStore() {
25
- const store = useContext(ListStoreContext);
26
- if (!store)
27
- throw new Error("useList reads a list's state, and needs a ListRoot around it");
28
- return store;
17
+ function RootData({ fields, rows, source, pageSize, quickFilters, columns: overrides, save, can, children, }) {
18
+ const store = useListStore();
19
+ const settings = useList((s) => s.view.settings);
20
+ const conventions = useContext(ViewsContext)?.conventions;
21
+ const writable = !!save && can?.edit !== false;
22
+ const hold = quickFilters?.hold;
23
+ const sourced = useSourceAnswers(store, source, pageSize, settings, hold, fields, conventions);
24
+ const { bindings, ...data } = useListData(fields, sourced ? undefined : rows, overrides, settings, conventions, writable, hold);
25
+ const parts = useSourceParts(store, sourced?.answers ?? null, quickFilters, fields, settings, sourced?.state.total ?? null);
26
+ const holdKey = JSON.stringify(hold?.length ? hold : null);
27
+ const heldKey = useRef(holdKey);
28
+ useLayoutEffect(() => {
29
+ if (heldKey.current === holdKey)
30
+ return;
31
+ heldKey.current = holdKey;
32
+ store.get().select([]);
33
+ }, [store, holdKey]);
34
+ const fromSource = useMemo(() => (sourced && data.columns ? withParts(sourced.state, data.columns, parts) : null), [sourced, data.columns, parts]);
35
+ store.syncData(fromSource ? { ...data, answer: fromSource } : data);
36
+ useLayoutEffect(() => store.flush());
37
+ const commits = useListCommits(save, writable, bindings);
38
+ const sink = useListEditsSink(store);
39
+ return createElement(ListCommitsContext.Provider, { value: commits }, createElement(ListEditsSinkContext.Provider, { value: sink }, children));
29
40
  }
30
- export function useList(...args) {
31
- const byId = typeof args[0] === "string" ? args[0] : null;
32
- const [select, equal = Object.is] = (byId === null ? args : args.slice(1));
33
- const inside = useContext(ListStoreContext);
34
- const registry = useContext(ListRegistryContext);
35
- if (byId === null && !inside)
36
- throw new Error("useList reads a list's state, and needs a ListRoot around it");
37
- if (byId !== null && !registry)
38
- throw new Error("useList(id, select) reads a list by its id, and needs a ViewsProvider around it");
39
- const storeOf = () => (byId === null ? (inside ?? undefined) : registry?.store(byId));
40
- const subscribe = useCallback((listener) => {
41
- if (byId === null || !registry)
42
- return inside ? inside.subscribe(listener) : () => { };
43
- let off = registry.store(byId)?.subscribe(listener) ?? (() => { });
44
- const offRegistry = registry.subscribe(() => {
45
- off();
46
- off = registry.store(byId)?.subscribe(listener) ?? (() => { });
47
- listener();
48
- });
49
- return () => {
50
- offRegistry();
51
- off();
52
- };
53
- }, [byId, registry, inside]);
54
- const memo = useRef(null);
55
- const read = () => {
56
- const state = storeOf()?.get();
57
- if (!state) {
58
- memo.current = null;
59
- return undefined;
60
- }
61
- const last = memo.current;
62
- if (last && last.state === state && last.select === select)
63
- return last.value;
64
- const value = select(state);
65
- if (last && equal(last.value, value)) {
66
- memo.current = { state, select, value: last.value };
67
- return last.value;
68
- }
69
- memo.current = { state, select, value };
70
- return value;
71
- };
72
- return useSyncExternalStore(subscribe, read, read);
41
+ function useListData(fields, rows, overrides, settings, conventions, writable, hold) {
42
+ const of = useMemo(() => (fields ? columnsOf(fields, overrides, writable) : null), [fields, overrides, writable]);
43
+ const answer = useMemo(() => (fields && rows && of ? answerInMemory(rows, settings, fields, of.columns, conventions, of.bindings, hold) : null), [fields, rows, of, settings, conventions, hold]);
44
+ return useMemo(() => ({
45
+ columns: (of?.columns ?? null),
46
+ answer: answer,
47
+ bindings: of?.bindings ?? null,
48
+ }), [of, answer]);
73
49
  }
74
- export function useListPanel(key) {
75
- const store = useListStore();
76
- const open = useList((s) => s.panels.open === key);
77
- const page = useList((s) => s.panels.pages[key] ?? null);
78
- const onOpenChange = useCallback((next) => (next ? store.get().openPanel(key) : store.get().closePanel(key)), [store, key]);
79
- const onPageChange = useCallback((next) => store.get().setPage(key, next), [store, key]);
80
- return useMemo(() => ({ open, onOpenChange, page, onPageChange }), [open, onOpenChange, page, onPageChange]);
50
+ function useSourceAnswers(store, source, pageSize, settings, hold, fields, conventions) {
51
+ const toast = useContext(ViewsContext)?.ports.toast.error;
52
+ const latest = useRef({ source, toast, fields, conventions });
53
+ useLayoutEffect(() => {
54
+ latest.current = { source, toast, fields, conventions };
55
+ });
56
+ const [, changed] = useReducer((n) => n + 1, 0);
57
+ const [answers] = useState(() => (source ? answersOver(latest, pageSize, changed) : null));
58
+ useLayoutEffect(() => {
59
+ answers?.ask(settings, hold);
60
+ }, [answers, settings, hold]);
61
+ useLayoutEffect(() => {
62
+ if (!answers)
63
+ return;
64
+ store.wire({ loadMore: answers.loadMore, loadGroup: answers.loadGroup });
65
+ return () => store.wire({ loadMore: undefined, loadGroup: undefined });
66
+ }, [answers, store]);
67
+ const state = answers?.state();
68
+ return useMemo(() => (answers && state ? { state, answers } : null), [answers, state]);
81
69
  }
82
- function useStoreMode(id, props) {
83
- const [mode] = useState(() => {
84
- if (props.settings !== undefined)
85
- return null;
86
- const own = props;
87
- const given = own.views;
88
- const options = given && "list" in given ? { store: given } : (given ?? {});
89
- const store = options.store ?? browserViewsStore(options.seed ? { seed: { [id]: options.seed } } : undefined);
90
- let on = { change: () => { }, error: () => { } };
91
- const views = createListViews({
92
- id,
93
- store,
94
- defaults: own.defaults ?? LIST_SETTINGS_DEFAULTS,
95
- ...(own.link && { link: own.link }),
96
- ...(options.records !== undefined && { records: options.records }),
97
- onChange: () => on.change(),
98
- onError: (message) => on.error(message),
99
- });
100
- return { views, wire: (next) => void (on = next) };
70
+ function answersOver(latest, pageSize, changed) {
71
+ return createListAnswers({
72
+ source: (request) => latest.current.source(request),
73
+ ...(pageSize !== undefined && { pageSize }),
74
+ onChange: changed,
75
+ onError: (message) => latest.current.toast?.(message),
76
+ nameGroup: (group, key) => groupName(latest.current.fields, latest.current.conventions, group, key),
101
77
  });
102
- return mode;
103
78
  }
104
- function useStoreViews(mode, store, extra) {
105
- const toast = useContext(ViewsContext)?.ports.toast.error;
79
+ function groupName(fields, conventions, group, key) {
80
+ const field = fields?.[group.fieldKey];
81
+ if (!field)
82
+ return null;
83
+ return bucketRuleFor(field, group.by)?.labelOf(presentedDef(field), key, group.granularity, conventions) ?? null;
84
+ }
85
+ const FILTER_PANELS = new Set(["filter", "filter-add", "options"]);
86
+ const NO_VALUES = {};
87
+ function useSourceParts(store, answers, quickFilters, fields, settings, total) {
88
+ const views = useList((s) => s.views);
89
+ const open = useList((s) => s.panels.open);
90
+ const show = settings.showCounts !== false;
91
+ const [viewCounts, setViewCounts] = useState(null);
92
+ const [holdCounts, setHoldCounts] = useState(null);
93
+ const [values, setValues] = useState(NO_VALUES);
94
+ const totalRef = useRef(total);
95
+ useLayoutEffect(() => {
96
+ totalRef.current = total;
97
+ });
106
98
  useLayoutEffect(() => {
107
- if (!mode)
99
+ if (!answers)
108
100
  return;
109
- mode.wire({
110
- change: () => {
111
- store.sync(mode.views.sync(extra));
112
- store.patch({ views: mode.views.state() });
113
- store.flush();
101
+ store.wire({
102
+ selectAll: () => {
103
+ const before = store.get().selection;
104
+ const pending = { ids: [], all: true, count: totalRef.current?.count ?? null };
105
+ store.patch({ selection: pending });
106
+ void answers.ids().then((ids) => {
107
+ if (ids)
108
+ return store.get().select(ids);
109
+ if (store.get().selection === pending)
110
+ store.patch({ selection: before });
111
+ });
114
112
  },
115
- error: (message) => toast?.(message),
116
113
  });
117
- });
118
- const [, settle] = useState(0);
114
+ return () => store.wire({ selectAll: undefined });
115
+ }, [answers, store]);
116
+ const hold = quickFilters?.hold;
117
+ const asked = JSON.stringify([requestSettingsOf(settings), hold?.length ? hold : null]);
118
+ const askedViews = JSON.stringify([{ ...requestSettingsOf(settings), filter: null }, hold?.length ? hold : null]);
119
+ const viewsList = views?.ready ? views.list : null;
120
+ const viewsKey = JSON.stringify(viewsList?.map((v) => [v.id, v.settings.filter]) ?? []);
121
+ useLayoutEffect(() => {
122
+ const counted = JSON.parse(viewsKey);
123
+ if (!answers || counted.length === 0 || !show)
124
+ return;
125
+ let current = true;
126
+ void answers.viewCounts(counted.map(([id, filter]) => ({ id, filter }))).then((counts) => {
127
+ if (current)
128
+ setViewCounts(counts);
129
+ });
130
+ return () => void (current = false);
131
+ }, [answers, viewsKey, show, askedViews]);
132
+ const quickKeys = (quickFilters?.fields ?? []).map((f) => f.key).join("\n");
133
+ const quickOpen = open === "quick";
134
+ useLayoutEffect(() => {
135
+ if (!answers || !quickOpen || quickKeys === "")
136
+ return;
137
+ let current = true;
138
+ void answers.counts("hold", quickKeys.split("\n")).then((counts) => {
139
+ if (current && counts)
140
+ setHoldCounts(Object.fromEntries(Object.entries(counts).map(([key, c]) => [key, c.counts])));
141
+ });
142
+ return () => void (current = false);
143
+ }, [answers, quickOpen, quickKeys, asked]);
144
+ const filterOpen = open !== null && FILTER_PANELS.has(open);
145
+ const textKeys = Object.values(fields ?? {}).filter((field) => field.type === "text").map((field) => field.key).join("\n");
119
146
  useLayoutEffect(() => {
120
- if (!mode)
147
+ if (!answers || !filterOpen || textKeys === "")
121
148
  return;
122
- store.patch({ views: mode.views.state() });
123
- const stop = mode.views.start();
124
- if (mode.views.state().ready)
125
- settle((n) => n + 1);
126
- return stop;
127
- }, [mode, store]);
149
+ let current = true;
150
+ const keys = textKeys.split("\n");
151
+ void Promise.all(keys.map((key) => answers.values(key).then((answer) => [key, answer?.values ?? []]))).then((pairs) => {
152
+ if (current)
153
+ setValues(Object.fromEntries(pairs));
154
+ });
155
+ return () => void (current = false);
156
+ }, [answers, filterOpen, textKeys, asked]);
157
+ return useMemo(() => ({ viewCounts: show ? viewCounts : null, holdCounts, values }), [show, viewCounts, holdCounts, values]);
158
+ }
159
+ function withParts(answer, columns, parts) {
160
+ const rows = answer.groups.flatMap((group) => group.items);
161
+ const inHand = deriveGridSelectValues(columns, rows);
162
+ return {
163
+ ...answer,
164
+ viewCounts: parts.viewCounts,
165
+ holdCounts: parts.holdCounts,
166
+ filterValues: { ...answer.filterValues, selectValues: { ...inHand, ...parts.values } },
167
+ };
128
168
  }
@@ -0,0 +1,39 @@
1
+ import { type FilterTree, type ListAnswerOf, type ListHold, type ListRequestSettings, type ListRow, type ListSettings } from "viewkitjs/fields/query";
2
+ import type { ListAnswerState } from "./answer.js";
3
+ import type { ListSource } from "./source.js";
4
+ export type ListAnswersController<Row extends ListRow> = {
5
+ /** The settings (and quick filters' hold) the rows answer; a change to their request parts asks again. */
6
+ ask: (settings: ListSettings, hold: ListHold | undefined) => void;
7
+ /** The next page of the ungrouped list; nothing while one is out or there is none. */
8
+ loadMore: () => void;
9
+ /** The next page of one group (its first, where its rows weren't sent); nothing while one is out or there is none. */
10
+ loadGroup: (key: string) => void;
11
+ /** Every matching row's id (the `ids` part); null on a refusal, a throw, or an answer a newer ask has outrun. */
12
+ ids: () => Promise<string[] | null>;
13
+ /** Counts of a quick filter's or the filter's values (the `counts` part). */
14
+ counts: (of: "filter" | "hold", fields: readonly string[]) => Promise<ListAnswerOf<"counts">["counts"] | null>;
15
+ /** One field's values and their labels (the `values` part). */
16
+ values: (field: string) => Promise<{
17
+ values: string[];
18
+ labels: Record<string, string>;
19
+ } | null>;
20
+ /** Each view's count under its own filter (the `view-counts` part). */
21
+ viewCounts: (views: readonly {
22
+ id: string;
23
+ filter: FilterTree;
24
+ }[]) => Promise<Record<string, number> | null>;
25
+ state: () => ListAnswerState<Row>;
26
+ };
27
+ /** The settings' parts a request carries: the filter, the sorts and the group, the search where there is one, and the
28
+ * row order where the settings hold one. */
29
+ export declare function requestSettingsOf(settings: ListSettings): ListRequestSettings;
30
+ export declare function createListAnswers<Row extends ListRow>(o: {
31
+ source: ListSource<Row>;
32
+ pageSize?: number;
33
+ /** Told after each change of `state()`. */
34
+ onChange: () => void;
35
+ /** A source that threw or rejected, or answered another part, in words a person reads. */
36
+ onError: (message: string) => void;
37
+ /** A group's name where the source sent none: the grouped field's label for its key, or null. */
38
+ nameGroup?: (group: NonNullable<ListRequestSettings["group"]>, key: string) => string | null;
39
+ }): ListAnswersController<Row>;
@@ -0,0 +1,225 @@
1
+ import { answerFor, isRefused, LIST_REQUEST_LIMITS, LIST_SETTINGS_DEFAULTS, LIST_REQUEST_VERSION, } from "viewkitjs/fields/query";
2
+ const OPTIONAL_PARTS = ["rowOrderMode", "itemPositions"];
3
+ export function requestSettingsOf(settings) {
4
+ const out = { filter: settings.filter, sorts: settings.sorts, group: settings.group };
5
+ if (settings.search?.trim())
6
+ out.search = settings.search;
7
+ for (const key of OPTIONAL_PARTS)
8
+ if (settings[key] !== undefined)
9
+ out[key] = settings[key];
10
+ return out;
11
+ }
12
+ const pageSizeOf = (size) => Math.min(LIST_REQUEST_LIMITS.pageSize, Math.max(1, Math.floor(Number.isFinite(size) ? size : 50)));
13
+ const messageOf = (error) => error instanceof Error ? error.message : String(error);
14
+ const NO_FILTER_VALUES = { selectValues: {}, relationValues: {} };
15
+ export function createListAnswers(o) {
16
+ const size = pageSizeOf(o.pageSize);
17
+ let generation = 0;
18
+ let asked = null;
19
+ let base = {
20
+ version: LIST_REQUEST_VERSION,
21
+ settings: requestSettingsOf(LIST_SETTINGS_DEFAULTS),
22
+ };
23
+ let rows = [];
24
+ let next = null;
25
+ let moreOut = false;
26
+ let groups = null;
27
+ let groupedBy = null;
28
+ let total = null;
29
+ let refused = null;
30
+ let landed = 0;
31
+ let first = false;
32
+ let shown = null;
33
+ const changed = () => {
34
+ shown = null;
35
+ o.onChange();
36
+ };
37
+ const send = async (request) => {
38
+ let answer;
39
+ try {
40
+ answer = await o.source(request);
41
+ }
42
+ catch (error) {
43
+ o.onError(messageOf(error));
44
+ return null;
45
+ }
46
+ try {
47
+ return answerFor(request, answer);
48
+ }
49
+ catch (error) {
50
+ o.onError(messageOf(error));
51
+ return null;
52
+ }
53
+ };
54
+ const tracked = async (request, land, after) => {
55
+ const at = generation;
56
+ changed();
57
+ const answer = await send(request);
58
+ if (at !== generation)
59
+ return;
60
+ if (answer)
61
+ land(answer);
62
+ after?.();
63
+ changed();
64
+ };
65
+ const ask = (settings, hold) => {
66
+ const parts = requestSettingsOf(settings);
67
+ const held = hold?.length ? hold : null;
68
+ const key = JSON.stringify([parts, held]);
69
+ if (key === asked)
70
+ return;
71
+ asked = key;
72
+ generation += 1;
73
+ first = true;
74
+ base = { version: LIST_REQUEST_VERSION, settings: parts, ...(held && { hold: held }) };
75
+ moreOut = false;
76
+ const at = generation;
77
+ void tracked({ ...base, part: "rows", page: { size } }, (answer) => {
78
+ landed = at;
79
+ groupedBy = parts.group;
80
+ if (isRefused(answer)) {
81
+ refused = answer.refused.message;
82
+ rows = [];
83
+ next = null;
84
+ groups = null;
85
+ total = null;
86
+ return;
87
+ }
88
+ refused = null;
89
+ total = answer.total;
90
+ if (answer.groups) {
91
+ rows = [];
92
+ next = null;
93
+ groups = answer.groups.map((g) => ({
94
+ key: g.key,
95
+ label: g.label,
96
+ count: g.count,
97
+ hidden: g.hidden,
98
+ rows: g.rows,
99
+ next: g.next ?? null,
100
+ out: false,
101
+ }));
102
+ }
103
+ else {
104
+ groups = null;
105
+ rows = answer.rows;
106
+ next = answer.next;
107
+ }
108
+ }, () => {
109
+ first = false;
110
+ });
111
+ };
112
+ const loadMore = () => {
113
+ if (landed !== generation || groups || next === null || moreOut)
114
+ return;
115
+ moreOut = true;
116
+ void tracked({ ...base, part: "rows", page: { size, after: next } }, (answer) => {
117
+ if (isRefused(answer)) {
118
+ refused = answer.refused.message;
119
+ next = null;
120
+ return;
121
+ }
122
+ rows = [...rows, ...answer.rows];
123
+ next = answer.next;
124
+ }, () => {
125
+ moreOut = false;
126
+ });
127
+ };
128
+ const loadGroup = (key) => {
129
+ const group = landed === generation ? groups?.find((g) => g.key === key) : undefined;
130
+ if (!group || group.out || (group.rows !== undefined && group.next === null))
131
+ return;
132
+ group.out = true;
133
+ void tracked({
134
+ ...base,
135
+ part: "group",
136
+ group: { key },
137
+ page: { size, after: group.rows === undefined ? null : group.next },
138
+ }, (answer) => {
139
+ if (isRefused(answer)) {
140
+ refused = answer.refused.message;
141
+ group.next = null;
142
+ group.rows ??= [];
143
+ return;
144
+ }
145
+ if (answer.key !== key) {
146
+ o.onError(`answered group ${answer.key} to a group ${key} request`);
147
+ return;
148
+ }
149
+ group.rows = [...(group.rows ?? []), ...answer.rows];
150
+ group.next = answer.next;
151
+ }, () => {
152
+ group.out = false;
153
+ });
154
+ };
155
+ const once = async (request) => {
156
+ const answer = await send(request);
157
+ return answer && !isRefused(answer) ? answer : null;
158
+ };
159
+ const labelOf = (g) => g.label ?? (groupedBy && o.nameGroup?.(groupedBy, g.key)) ?? null;
160
+ const state = () => {
161
+ if (shown)
162
+ return shown;
163
+ const current = landed === generation;
164
+ const drawn = groups
165
+ ? groups
166
+ .filter((g) => !g.hidden)
167
+ .map((g) => ({ key: g.key, label: labelOf(g), items: g.rows ?? [] }))
168
+ : [{ key: "all", label: null, items: rows }];
169
+ shown = {
170
+ groups: drawn,
171
+ grouped: !!groups,
172
+ groupHeads: (groups ?? []).map((g) => ({
173
+ key: g.key,
174
+ label: labelOf(g) ?? g.key,
175
+ count: g.count,
176
+ hidden: g.hidden,
177
+ })),
178
+ ids: drawn.flatMap((g) => g.items.map((row) => row.id)),
179
+ total,
180
+ filterValues: NO_FILTER_VALUES,
181
+ loading: first,
182
+ refused,
183
+ paged: true,
184
+ viewCounts: null,
185
+ holdCounts: null,
186
+ more: {
187
+ rows: current && !groups && next !== null,
188
+ groups: Object.fromEntries((groups ?? []).map((g) => [g.key, current && g.count > 0 && (g.rows === undefined || g.next !== null)])),
189
+ },
190
+ };
191
+ return shown;
192
+ };
193
+ return {
194
+ ask,
195
+ loadMore,
196
+ loadGroup,
197
+ ids: async () => {
198
+ const at = generation;
199
+ const answer = await send({ ...base, part: "ids" });
200
+ if (!answer || at !== generation)
201
+ return null;
202
+ if (isRefused(answer)) {
203
+ o.onError(answer.refused.message);
204
+ return null;
205
+ }
206
+ return answer.ids;
207
+ },
208
+ counts: async (of, fields) => (await once({
209
+ ...base,
210
+ part: "counts",
211
+ counts: { of, fields: [...fields] },
212
+ }))?.counts ?? null,
213
+ values: async (field) => {
214
+ const answer = await once({ ...base, part: "values", field });
215
+ return answer ? { values: answer.values, labels: answer.labels } : null;
216
+ },
217
+ viewCounts: async (views) => (await once({
218
+ ...base,
219
+ settings: { ...base.settings, filter: LIST_SETTINGS_DEFAULTS.filter },
220
+ part: "view-counts",
221
+ views: views.map((v) => ({ id: v.id, filter: v.filter })),
222
+ }))?.counts ?? null,
223
+ state,
224
+ };
225
+ }
@@ -1,7 +1,10 @@
1
- import type { ListPersonalLayer, ListSettings, ListSettingsChange } from "viewkitjs/fields/query";
1
+ import type { ListPersonalLayer, ListRow, ListSettings, ListSettingsChange } from "viewkitjs/fields/query";
2
2
  import type { ActivePanel } from "../panels/model.js";
3
3
  import type { Pending } from "../panels/react/filter/editor-ctx.js";
4
4
  import type { CollectionViewState } from "../panels/view-state.js";
5
+ import type { GridEditsState } from "../grid/core/edits-sink.js";
6
+ import type { ListColumn } from "../model/column-schema.js";
7
+ import type { ListAnswerState } from "./answer.js";
5
8
  import type { ListViewsState } from "./views.js";
6
9
  /** One list's state, as a part selects it (`useList((s) => …)`). */
7
10
  export type ListState = {
@@ -37,6 +40,12 @@ export type ListState = {
37
40
  };
38
41
  /** The list's saved views where the root keeps them through a store (VK-196); null in memory mode. */
39
42
  views: ListViewsState | null;
43
+ /** The list's columns, from its fields (the root's `fields`); null where the root holds none. */
44
+ columns: ListColumn<ListRow>[] | null;
45
+ /** The rows the list shows; null where the root holds none (no `rows`). */
46
+ answer: ListAnswerState | null;
47
+ /** The grid's edits: a write still out, and whether an undo or a redo has anything to take. */
48
+ edits: GridEditsState;
40
49
  /** A change to the settings, handed to the app. */
41
50
  setSettings: (change: ListSettingsChange) => void;
42
51
  /** A patch to the person's layer, handed to the app. */
@@ -56,6 +65,12 @@ export type ListState = {
56
65
  resetView: () => void;
57
66
  /** Takes back the list's last edit; nothing until the list holds edits. */
58
67
  undo: () => void;
68
+ /** Puts back the last edit undone; nothing until there is one. */
69
+ redo: () => void;
70
+ /** The list's next page, where its rows come from a source and there is one; nothing in memory. */
71
+ loadMore: () => void;
72
+ /** A group's next page, likewise. */
73
+ loadGroup: (key: string) => void;
59
74
  };
60
75
  /** What the app hands the root: synced in on every render. */
61
76
  export type ListSync = {
@@ -73,15 +88,32 @@ export type ListStore = {
73
88
  /** Takes the app's latest props without telling anyone; `flush` tells. */
74
89
  sync: (props: ListSync) => void;
75
90
  flush: () => void;
91
+ /** Takes the root's latest columns and answer, worked out as it renders, without telling anyone; `flush` tells. */
92
+ syncData: (data: {
93
+ columns: ListColumn<ListRow>[] | null;
94
+ answer: ListAnswerState | null;
95
+ }) => void;
76
96
  /** The toolbar's published bar (`barShown`, `footHere`), and the root's saved views. The ready List widens it with its
77
97
  * answer and edits. */
78
98
  patch: (patch: {
79
99
  panels?: Partial<Pick<ListState["panels"], "barShown" | "footHere">>;
80
100
  views?: ListViewsState | null;
101
+ columns?: ListColumn<ListRow>[] | null;
102
+ answer?: ListAnswerState | null;
103
+ edits?: GridEditsState;
104
+ selection?: ListState["selection"];
81
105
  }) => void;
82
- /** The ready List wires its edits' undo here. */
83
- wire: (handlers: {
84
- undo?: () => void;
85
- }) => void;
106
+ /** The grid wires its edits' undo and redo here, and its selection's `select`. */
107
+ wire: (handlers: Wired) => void;
108
+ };
109
+ type Wired = {
110
+ undo?: () => void;
111
+ redo?: () => void;
112
+ select?: (ids: readonly string[] | "all") => void;
113
+ /** Every matching row picked, where the rows in hand aren't all of them (a source's). */
114
+ selectAll?: () => void;
115
+ loadMore?: () => void;
116
+ loadGroup?: (key: string) => void;
86
117
  };
87
118
  export declare function createListStore(id: string, props: ListSync): ListStore;
119
+ export {};