@panneau/core 5.0.0-alpha.3 → 5.0.0-alpha.31

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 (78) hide show
  1. package/README.md +5 -3
  2. package/dist/commands.d.ts +38 -0
  3. package/dist/commands.d.ts.map +1 -0
  4. package/dist/commands.js +22 -0
  5. package/dist/commands.js.map +1 -0
  6. package/dist/definition/abilities.d.ts +33 -5
  7. package/dist/definition/abilities.d.ts.map +1 -1
  8. package/dist/definition/abilities.js +44 -6
  9. package/dist/definition/abilities.js.map +1 -1
  10. package/dist/definition/form.d.ts +16 -7
  11. package/dist/definition/form.d.ts.map +1 -1
  12. package/dist/definition/form.js +39 -6
  13. package/dist/definition/form.js.map +1 -1
  14. package/dist/definition/itemActions.d.ts +53 -0
  15. package/dist/definition/itemActions.d.ts.map +1 -0
  16. package/dist/definition/itemActions.js +64 -0
  17. package/dist/definition/itemActions.js.map +1 -0
  18. package/dist/definition/list.d.ts +42 -1
  19. package/dist/definition/list.d.ts.map +1 -1
  20. package/dist/definition/list.js +103 -16
  21. package/dist/definition/list.js.map +1 -1
  22. package/dist/definition/resource.d.ts +3 -2
  23. package/dist/definition/resource.d.ts.map +1 -1
  24. package/dist/definition/resource.js +3 -2
  25. package/dist/definition/resource.js.map +1 -1
  26. package/dist/form/form.d.ts +8 -3
  27. package/dist/form/form.d.ts.map +1 -1
  28. package/dist/form/form.js +1 -0
  29. package/dist/form/form.js.map +1 -1
  30. package/dist/form/messages.d.ts.map +1 -1
  31. package/dist/form/messages.js +11 -1
  32. package/dist/form/messages.js.map +1 -1
  33. package/dist/form/problem.d.ts +1 -1
  34. package/dist/form/problem.js.map +1 -1
  35. package/dist/form/selectors.d.ts +1 -1
  36. package/dist/form/selectors.js.map +1 -1
  37. package/dist/form/store.d.ts +1 -1
  38. package/dist/form/store.js.map +1 -1
  39. package/dist/index.d.ts +23 -16
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +8 -3
  42. package/dist/list/calendar.d.ts +15 -0
  43. package/dist/list/calendar.d.ts.map +1 -0
  44. package/dist/list/calendar.js +42 -0
  45. package/dist/list/calendar.js.map +1 -0
  46. package/dist/list/list.d.ts +40 -3
  47. package/dist/list/list.d.ts.map +1 -1
  48. package/dist/list/list.js +152 -16
  49. package/dist/list/list.js.map +1 -1
  50. package/dist/list/router.js.map +1 -1
  51. package/dist/list/viewStore.d.ts +36 -0
  52. package/dist/list/viewStore.d.ts.map +1 -0
  53. package/dist/list/viewStore.js +54 -0
  54. package/dist/list/viewStore.js.map +1 -0
  55. package/dist/plugins.d.ts +22 -2
  56. package/dist/plugins.d.ts.map +1 -1
  57. package/dist/plugins.js +5 -2
  58. package/dist/plugins.js.map +1 -1
  59. package/dist/preview.d.ts +14 -0
  60. package/dist/preview.d.ts.map +1 -0
  61. package/dist/react/leaveGuards.d.ts +34 -0
  62. package/dist/react/leaveGuards.d.ts.map +1 -0
  63. package/dist/react/leaveGuards.js +84 -0
  64. package/dist/react/leaveGuards.js.map +1 -0
  65. package/dist/react/provider.d.ts +4 -4
  66. package/dist/react/provider.js +3 -1
  67. package/dist/react/provider.js.map +1 -1
  68. package/dist/react/useForm.d.ts +1 -1
  69. package/dist/react/useForm.js.map +1 -1
  70. package/dist/react/useList.d.ts +1 -1
  71. package/dist/react/useList.js.map +1 -1
  72. package/dist/values.d.ts +1 -1
  73. package/dist/values.d.ts.map +1 -1
  74. package/dist/values.js +5 -1
  75. package/dist/values.js.map +1 -1
  76. package/dist/widget.d.ts +15 -0
  77. package/dist/widget.d.ts.map +1 -0
  78. package/package.json +4 -4
package/README.md CHANGED
@@ -28,7 +28,7 @@ function EditPage({ item, onSave }) {
28
28
 
29
29
  ## Plugins
30
30
 
31
- `definePlugin()` declares what a plugin adds, keyed by the names the definition uses: `fields`, `displays`, `listLayouts`, `actions` (handlers of `client` actions), `pages`, `widgets`, `components` (layout nodes), `locales` and `themes`.
31
+ `definePlugin()` declares what a plugin adds, keyed by the names the definition uses: `fields`, `displays`, `listLayouts`, `actions` (handlers of `client` actions), `pages`, `widgets`, `components` (layout nodes), `locales` and `themes`. A widget of a plugin gets `WidgetProps`: the widget, its data from `GET /dashboard/widgets/{widget}`, and the definition; the dashboard draws its card, skeleton and error ([decision 0064](../../docs/decisions/0064-dashboard-widgets.md)).
32
32
 
33
33
  `resolveTheme(definition.theme, registry.themes)` returns the theme of the admin: the themes of plugins that the definition's theme `extends`, by id, then its own tokens, tones and variants. `Theme` is the theme of `@panneau/ui` as plain data, the `Theme` schema of `@panneau/protocol`.
34
34
 
@@ -36,7 +36,7 @@ function EditPage({ item, onSave }) {
36
36
 
37
37
  ## Definition engine
38
38
 
39
- - **`resolveForm(resource, { mode, type })`**: the fields of the form and its layout. Field nodes that name unknown fields are dropped, and fields that no node references are appended at the end. `create` drops `updateOnly` fields; `update` makes `createOnly` fields read-only. `type` adds the fields of an item type.
39
+ - **`resolveForm(resource, { mode, type })`**: the fields of the form and its layout. Field nodes that name unknown fields are dropped, and fields that no node references are appended at the end. `notInForm` fields are dropped, even when the layout names them. `create` drops `updateOnly` fields; `update` makes `createOnly` fields read-only. `type` adds the fields of an item type.
40
40
  - **`resolveList(resource)`**: columns (label, display and sortability from their field), filters, sorts, default sort, layouts, page size, and the actions the user can run, by placement. Without columns, the list shows the item title and the next fields.
41
41
  - **Abilities:** `can(abilities, 'create')`, `isActionAllowedOn(action, item)`, `partitionByAction(action, items)`. Without abilities, everything is allowed and the backend decides; with abilities, only what is `true`.
42
42
  - **`useResource(id)`** resolves a resource of the provider's definition.
@@ -79,7 +79,9 @@ The form's methods that read the state (`form.getValue()`, `form.getErrors()`, `
79
79
  `createList(options)`, or `useList(options)`, manages:
80
80
 
81
81
  - **The query**: page, page size, sort, filters and search, with the list's defaults. Changing filters, search, sort or page size goes back to the first page.
82
+ - **The view**: the layout (`setLayout()`), the columns shown, in their order (`setColumns()`, `resetColumns()`), and the month of a calendar (`setMonth()`). It isn't sent to the backend. In the `calendar` layout of a list with a calendar, the query is the month's: its `date-range` filter set to the days around the month (`getCalendarRange()`), without a page ([0044](../../docs/decisions/0044-gallery-and-calendar.md)).
82
83
  - **The selection**: some ids, kept across pages, or every item matching the query except some (`selectAllMatching()`). `getSelection()` returns the protocol's `Selection` for an action. Changing filters or search clears it.
83
- - **URL sync** through a `Router`: `getSearch()`, `setSearch()` and `subscribe()`. Only values that differ from the defaults are written, next to the other parameters. `createHistoryRouter()` uses `window.history`; `createMemoryRouter()` keeps it in memory. `useList()` uses the provider's router unless given one, or `router: null`.
84
+ - **URL sync** through a `Router`: `getSearch()`, `setSearch()` and `subscribe()`. Only values that differ from the defaults are written, next to the other parameters. `createHistoryRouter()` uses `window.history`; `createMemoryRouter()` keeps it in memory. `useList()` uses the provider's router unless given one, or `router: null`. The view is in the URL too (`layout`, `columns`).
85
+ - **The remembered view**, with `remember: { key }`: the filters, sort, page size, layout and columns the user last chose, in `localStorage` (`createStorageViewStore()`) or another `ListViewStore`. A list that opens without any of its parameters in the URL shows it, and writes it to the URL; a URL with some opens as it is ([0042](../../docs/decisions/0042-list-view-columns-and-remembered-view.md)).
84
86
 
85
87
  The package layout, scripts and API reports are explained in [`docs/packages.md`](../../docs/packages.md).
@@ -0,0 +1,38 @@
1
+ import type { Definition, Resource } from '@panneau/protocol';
2
+ /**
3
+ * A command of the "Go to…" palette that a plugin adds (decision 0029): a link to follow or a
4
+ * function to run, found by its label, description and keywords.
5
+ */
6
+ export interface Command {
7
+ /** Unique among the commands of the palette. */
8
+ id: string;
9
+ label: string;
10
+ /** The group it is listed in. Defaults to the palette's group of commands. */
11
+ group?: string;
12
+ description?: string;
13
+ /** More words it is found by. */
14
+ keywords?: readonly string[];
15
+ /** A Lucide icon name (`"send"`). */
16
+ icon?: string;
17
+ /** A path of the app, or a URL, to go to. */
18
+ href?: string;
19
+ /** What it does, without an `href`. */
20
+ perform?: () => void;
21
+ }
22
+ /** What a plugin's commands can depend on. */
23
+ export interface CommandContext {
24
+ /** The definition for the current user, or `null` outside of the app. */
25
+ definition: Definition | null;
26
+ }
27
+ /** Commands, or a function that makes them for the current definition. */
28
+ export type CommandSource = readonly Command[] | ((context: CommandContext) => readonly Command[]);
29
+ /** The commands of every source, in order. */
30
+ export declare function getCommands(sources: readonly CommandSource[], context: CommandContext): Command[];
31
+ /**
32
+ * Whether a resource is in the global search: its `features.search`, or, without it, whether its
33
+ * list has a search (`list.search`), so one setting serves both (decision 0029).
34
+ */
35
+ export declare function isSearchable(resource: Resource): boolean;
36
+ /** The resources of the global search that the user can list. */
37
+ export declare function getSearchableResources(definition: Definition): Resource[];
38
+ //# sourceMappingURL=commands.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commands.d.ts","sourceRoot":"","sources":["../src/commands.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAK9D;;;GAGG;AACH,MAAM,WAAW,OAAO;IACpB,gDAAgD;IAChD,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,8EAA8E;IAC9E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iCAAiC;IACjC,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC7B,qCAAqC;IACrC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,6CAA6C;IAC7C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,uCAAuC;IACvC,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACxB;AAED,8CAA8C;AAC9C,MAAM,WAAW,cAAc;IAC3B,yEAAyE;IACzE,UAAU,EAAE,UAAU,GAAG,IAAI,CAAC;CACjC;AAED,0EAA0E;AAC1E,MAAM,MAAM,aAAa,GAAG,SAAS,OAAO,EAAE,GAAG,CAAC,CAAC,OAAO,EAAE,cAAc,KAAK,SAAS,OAAO,EAAE,CAAC,CAAC;AAEnG,8CAA8C;AAC9C,wBAAgB,WAAW,CAAC,OAAO,EAAE,SAAS,aAAa,EAAE,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,EAAE,CAEjG;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAExD;AAED,iEAAiE;AACjE,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,UAAU,GAAG,QAAQ,EAAE,CAIzE"}
@@ -0,0 +1,22 @@
1
+ import { can } from "./definition/abilities.js";
2
+ import { isSearchableResource } from "@panneau/protocol/runtime";
3
+ //#region src/commands.ts
4
+ /** The commands of every source, in order. */
5
+ function getCommands(sources, context) {
6
+ return sources.flatMap((source) => typeof source === "function" ? source(context) : source);
7
+ }
8
+ /**
9
+ * Whether a resource is in the global search: its `features.search`, or, without it, whether its
10
+ * list has a search (`list.search`), so one setting serves both (decision 0029).
11
+ */
12
+ function isSearchable(resource) {
13
+ return isSearchableResource(resource);
14
+ }
15
+ /** The resources of the global search that the user can list. */
16
+ function getSearchableResources(definition) {
17
+ return definition.resources.filter((resource) => isSearchable(resource) && can(resource.abilities, "viewAny"));
18
+ }
19
+ //#endregion
20
+ export { getCommands, getSearchableResources, isSearchable };
21
+
22
+ //# sourceMappingURL=commands.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commands.js","names":["Definition","Resource","isSearchableResource","can","Command","id","label","group","description","keywords","icon","href","perform","CommandContext","definition","CommandSource","context","getCommands","sources","flatMap","source","isSearchable","resource","getSearchableResources","resources","filter","abilities"],"sources":["../src/commands.ts"],"sourcesContent":["import type { Definition, Resource } from '@panneau/protocol';\nimport { isSearchableResource } from '@panneau/protocol/runtime';\n\nimport { can } from './definition/abilities';\n\n/**\n * A command of the \"Go to…\" palette that a plugin adds (decision 0029): a link to follow or a\n * function to run, found by its label, description and keywords.\n */\nexport interface Command {\n /** Unique among the commands of the palette. */\n id: string;\n label: string;\n /** The group it is listed in. Defaults to the palette's group of commands. */\n group?: string;\n description?: string;\n /** More words it is found by. */\n keywords?: readonly string[];\n /** A Lucide icon name (`\"send\"`). */\n icon?: string;\n /** A path of the app, or a URL, to go to. */\n href?: string;\n /** What it does, without an `href`. */\n perform?: () => void;\n}\n\n/** What a plugin's commands can depend on. */\nexport interface CommandContext {\n /** The definition for the current user, or `null` outside of the app. */\n definition: Definition | null;\n}\n\n/** Commands, or a function that makes them for the current definition. */\nexport type CommandSource = readonly Command[] | ((context: CommandContext) => readonly Command[]);\n\n/** The commands of every source, in order. */\nexport function getCommands(sources: readonly CommandSource[], context: CommandContext): Command[] {\n return sources.flatMap((source) => (typeof source === 'function' ? source(context) : source));\n}\n\n/**\n * Whether a resource is in the global search: its `features.search`, or, without it, whether its\n * list has a search (`list.search`), so one setting serves both (decision 0029).\n */\nexport function isSearchable(resource: Resource): boolean {\n return isSearchableResource(resource);\n}\n\n/** The resources of the global search that the user can list. */\nexport function getSearchableResources(definition: Definition): Resource[] {\n return definition.resources.filter(\n (resource) => isSearchable(resource) && can(resource.abilities, 'viewAny'),\n );\n}\n"],"mappings":";;;;AAoCA,SAAgBiB,YAAYC,SAAmCF,SAAoC;CAC/F,OAAOE,QAAQC,SAASC,WAAY,OAAOA,WAAW,aAAaA,OAAOJ,OAAO,IAAII,MAAO;AAChG;;;;;AAMA,SAAgBC,aAAaC,UAA6B;CACtD,OAAOpB,qBAAqBoB,QAAQ;AACxC;;AAGA,SAAgBC,uBAAuBT,YAAoC;CACvE,OAAOA,WAAWU,UAAUC,QACvBH,aAAaD,aAAaC,QAAQ,KAAKnB,IAAImB,SAASI,WAAW,SAAS,CAC7E;AACJ"}
@@ -1,21 +1,49 @@
1
- import type { Abilities, Action, ActionPlacement, Item } from '@panneau/protocol';
1
+ import type { Abilities, Action, ActionPlacement, Field, Item, Page } from '@panneau/protocol';
2
2
  /**
3
3
  * Whether a set of abilities allows something. Without abilities (a backend that sends none),
4
4
  * everything is allowed and the backend decides. With abilities, only what is `true` is allowed.
5
5
  */
6
6
  export declare function can(abilities: Abilities | undefined, ability: string): boolean;
7
+ /**
8
+ * Whether the current user can see a field (decision 0046): unless its `abilities.view` is
9
+ * `false`. A hidden field is left out of forms, details and lists.
10
+ */
11
+ export declare function canViewField(field: Pick<Field, 'abilities'>): boolean;
12
+ /**
13
+ * Whether the current user can edit a field (decision 0046): they can see it, and its
14
+ * `abilities.update` isn't `false`. A field they can't edit is read-only.
15
+ */
16
+ export declare function canUpdateField(field: Pick<Field, 'abilities'>): boolean;
17
+ /** The fields the current user can see, in their order. */
18
+ export declare function getVisibleFields<T extends Pick<Field, 'abilities'>>(fields: readonly T[]): T[];
19
+ /**
20
+ * Whether the current user can open a page of the definition (decision 0046): unless its
21
+ * `abilities.view` is `false`.
22
+ */
23
+ export declare function canViewPage(page: Pick<Page, 'abilities'>): boolean;
7
24
  /** Whether the current user can run an action on the resource (`abilities.run`). */
8
25
  export declare function canRunAction(action: Action): boolean;
9
26
  /**
10
- * Whether an action is available on an item: the user can run it, and the item lists its id in
11
- * its abilities. Links to show and edit are allowed unless the item says otherwise.
27
+ * Whether an action is available on an item: the user can run it, and the item allows it.
28
+ * - The built-in item actions need their ability (decision 0040): `view` for `show`, `update` for
29
+ * `edit`, `duplicate` and `delete`. A missing or false ability hides them, and so does their
30
+ * own id set to `false` (`"edit": false`). An item without abilities shows none of them.
31
+ * - Other links are allowed unless the item says otherwise (`"site": false`).
32
+ * - Other actions need their id in the abilities of the item (`"publish": true`).
33
+ * - On an item without abilities (a product's own records), other actions are allowed: the
34
+ * backend decides.
12
35
  */
13
- export declare function isActionAllowedOn(action: Action, item: Item): boolean;
36
+ export declare function isActionAllowedOn(action: Action, item: {
37
+ readonly abilities?: Abilities | undefined;
38
+ }): boolean;
14
39
  /** Splits the items of a selection by whether an action is allowed on them. */
15
40
  export declare function partitionByAction(action: Action, items: readonly Item[]): {
16
41
  allowed: Item[];
17
42
  skipped: Item[];
18
43
  };
19
- /** Where an action appears when it doesn't say: rows and forms for items, the toolbar otherwise. */
44
+ /**
45
+ * Where an action appears when it doesn't say: rows and forms (the header of an item, in its
46
+ * panel and its form) for items, the toolbar otherwise.
47
+ */
20
48
  export declare function getActionPlacements(action: Action): ActionPlacement[];
21
49
  //# sourceMappingURL=abilities.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"abilities.d.ts","sourceRoot":"","sources":["../../src/definition/abilities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAElF;;;GAGG;AACH,wBAAgB,GAAG,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAE9E;AAED,oFAAoF;AACpF,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEpD;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAQrE;AAED,+EAA+E;AAC/E,wBAAgB,iBAAiB,CAC7B,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,SAAS,IAAI,EAAE,GACvB;IAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAAC,OAAO,EAAE,IAAI,EAAE,CAAA;CAAE,CAKtC;AAED,oGAAoG;AACpG,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,eAAe,EAAE,CAcrE"}
1
+ {"version":3,"file":"abilities.d.ts","sourceRoot":"","sources":["../../src/definition/abilities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,eAAe,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAG/F;;;GAGG;AACH,wBAAgB,GAAG,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAE9E;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,WAAW,CAAC,GAAG,OAAO,CAErE;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,WAAW,CAAC,GAAG,OAAO,CAEvE;AAED,2DAA2D;AAC3D,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,IAAI,CAAC,KAAK,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,GAAG,CAAC,EAAE,CAE9F;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,WAAW,CAAC,GAAG,OAAO,CAElE;AAED,oFAAoF;AACpF,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEpD;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAC7B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE;IAAE,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,GAAG,SAAS,CAAA;CAAE,GACrD,OAAO,CAiBT;AAED,+EAA+E;AAC/E,wBAAgB,iBAAiB,CAC7B,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,SAAS,IAAI,EAAE,GACvB;IAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAAC,OAAO,EAAE,IAAI,EAAE,CAAA;CAAE,CAKtC;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,eAAe,EAAE,CAcrE"}
@@ -1,3 +1,4 @@
1
+ import { ITEM_ACTION_ABILITIES, isBuiltInItemAction } from "@panneau/protocol/runtime";
1
2
  //#region src/definition/abilities.ts
2
3
  /**
3
4
  * Whether a set of abilities allows something. Without abilities (a backend that sends none),
@@ -6,18 +7,52 @@
6
7
  function can(abilities, ability) {
7
8
  return abilities === void 0 || abilities[ability] === true;
8
9
  }
10
+ /**
11
+ * Whether the current user can see a field (decision 0046): unless its `abilities.view` is
12
+ * `false`. A hidden field is left out of forms, details and lists.
13
+ */
14
+ function canViewField(field) {
15
+ return field.abilities?.view !== false;
16
+ }
17
+ /**
18
+ * Whether the current user can edit a field (decision 0046): they can see it, and its
19
+ * `abilities.update` isn't `false`. A field they can't edit is read-only.
20
+ */
21
+ function canUpdateField(field) {
22
+ return canViewField(field) && field.abilities?.update !== false;
23
+ }
24
+ /** The fields the current user can see, in their order. */
25
+ function getVisibleFields(fields) {
26
+ return fields.filter(canViewField);
27
+ }
28
+ /**
29
+ * Whether the current user can open a page of the definition (decision 0046): unless its
30
+ * `abilities.view` is `false`.
31
+ */
32
+ function canViewPage(page) {
33
+ return page.abilities?.view !== false;
34
+ }
9
35
  /** Whether the current user can run an action on the resource (`abilities.run`). */
10
36
  function canRunAction(action) {
11
37
  return action.abilities?.run !== false;
12
38
  }
13
39
  /**
14
- * Whether an action is available on an item: the user can run it, and the item lists its id in
15
- * its abilities. Links to show and edit are allowed unless the item says otherwise.
40
+ * Whether an action is available on an item: the user can run it, and the item allows it.
41
+ * - The built-in item actions need their ability (decision 0040): `view` for `show`, `update` for
42
+ * `edit`, `duplicate` and `delete`. A missing or false ability hides them, and so does their
43
+ * own id set to `false` (`"edit": false`). An item without abilities shows none of them.
44
+ * - Other links are allowed unless the item says otherwise (`"site": false`).
45
+ * - Other actions need their id in the abilities of the item (`"publish": true`).
46
+ * - On an item without abilities (a product's own records), other actions are allowed: the
47
+ * backend decides.
16
48
  */
17
49
  function isActionAllowedOn(action, item) {
18
50
  if (!canRunAction(action)) return false;
19
- if (action.kind === "link") return item.abilities[action.id] !== false;
20
- return item.abilities[action.id] === true;
51
+ const { abilities } = item;
52
+ if (isBuiltInItemAction(action.id)) return abilities?.[ITEM_ACTION_ABILITIES[action.id]] === true && abilities[action.id] !== false;
53
+ if (abilities === void 0) return true;
54
+ if (action.kind === "link") return abilities[action.id] !== false;
55
+ return abilities[action.id] === true;
21
56
  }
22
57
  /** Splits the items of a selection by whether an action is allowed on them. */
23
58
  function partitionByAction(action, items) {
@@ -29,7 +64,10 @@ function partitionByAction(action, items) {
29
64
  skipped
30
65
  };
31
66
  }
32
- /** Where an action appears when it doesn't say: rows and forms for items, the toolbar otherwise. */
67
+ /**
68
+ * Where an action appears when it doesn't say: rows and forms (the header of an item, in its
69
+ * panel and its form) for items, the toolbar otherwise.
70
+ */
33
71
  function getActionPlacements(action) {
34
72
  if (action.placement !== void 0) return action.placement;
35
73
  const placements = /* @__PURE__ */ new Set();
@@ -42,6 +80,6 @@ function getActionPlacements(action) {
42
80
  return [...placements];
43
81
  }
44
82
  //#endregion
45
- export { can, canRunAction, getActionPlacements, isActionAllowedOn, partitionByAction };
83
+ export { can, canRunAction, canUpdateField, canViewField, canViewPage, getActionPlacements, getVisibleFields, isActionAllowedOn, partitionByAction };
46
84
 
47
85
  //# sourceMappingURL=abilities.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"abilities.js","names":[],"sources":["../../src/definition/abilities.ts"],"sourcesContent":["import type { Abilities, Action, ActionPlacement, Item } from '@panneau/protocol';\n\n/**\n * Whether a set of abilities allows something. Without abilities (a backend that sends none),\n * everything is allowed and the backend decides. With abilities, only what is `true` is allowed.\n */\nexport function can(abilities: Abilities | undefined, ability: string): boolean {\n return abilities === undefined || abilities[ability] === true;\n}\n\n/** Whether the current user can run an action on the resource (`abilities.run`). */\nexport function canRunAction(action: Action): boolean {\n return action.abilities?.run !== false;\n}\n\n/**\n * Whether an action is available on an item: the user can run it, and the item lists its id in\n * its abilities. Links to show and edit are allowed unless the item says otherwise.\n */\nexport function isActionAllowedOn(action: Action, item: Item): boolean {\n if (!canRunAction(action)) {\n return false;\n }\n if (action.kind === 'link') {\n return item.abilities[action.id] !== false;\n }\n return item.abilities[action.id] === true;\n}\n\n/** Splits the items of a selection by whether an action is allowed on them. */\nexport function partitionByAction(\n action: Action,\n items: readonly Item[],\n): { allowed: Item[]; skipped: Item[] } {\n const allowed: Item[] = [];\n const skipped: Item[] = [];\n items.forEach((item) => (isActionAllowedOn(action, item) ? allowed : skipped).push(item));\n return { allowed, skipped };\n}\n\n/** Where an action appears when it doesn't say: rows and forms for items, the toolbar otherwise. */\nexport function getActionPlacements(action: Action): ActionPlacement[] {\n if (action.placement !== undefined) {\n return action.placement;\n }\n const placements = new Set<ActionPlacement>();\n action.scope.forEach((scope) => {\n if (scope === 'item') {\n placements.add('row');\n placements.add('form');\n } else {\n placements.add('toolbar');\n }\n });\n return [...placements];\n}\n"],"mappings":";;;;;AAMA,SAAgB,IAAI,WAAkC,SAA0B;CAC5E,OAAO,cAAc,KAAA,KAAa,UAAU,aAAa;AAC7D;;AAGA,SAAgB,aAAa,QAAyB;CAClD,OAAO,OAAO,WAAW,QAAQ;AACrC;;;;;AAMA,SAAgB,kBAAkB,QAAgB,MAAqB;CACnE,IAAI,CAAC,aAAa,MAAM,GACpB,OAAO;CAEX,IAAI,OAAO,SAAS,QAChB,OAAO,KAAK,UAAU,OAAO,QAAQ;CAEzC,OAAO,KAAK,UAAU,OAAO,QAAQ;AACzC;;AAGA,SAAgB,kBACZ,QACA,OACoC;CACpC,MAAM,UAAkB,CAAA;CACxB,MAAM,UAAkB,CAAA;CACxB,MAAM,SAAS,UAAU,kBAAkB,QAAQ,IAAI,IAAI,UAAU,QAAA,CAAS,KAAK,IAAI,CAAC;CACxF,OAAO;EAAE;EAAS;CAAQ;AAC9B;;AAGA,SAAgB,oBAAoB,QAAmC;CACnE,IAAI,OAAO,cAAc,KAAA,GACrB,OAAO,OAAO;CAElB,MAAM,6BAAa,IAAI,IAAqB;CAC5C,OAAO,MAAM,SAAS,UAAU;EAC5B,IAAI,UAAU,QAAQ;GAClB,WAAW,IAAI,KAAK;GACpB,WAAW,IAAI,MAAM;EACzB,OACI,WAAW,IAAI,SAAS;CAEhC,CAAC;CACD,OAAO,CAAC,GAAG,UAAU;AACzB"}
1
+ {"version":3,"file":"abilities.js","names":["Abilities","Action","ActionPlacement","Field","Item","Page","ITEM_ACTION_ABILITIES","isBuiltInItemAction","can","abilities","ability","undefined","canViewField","field","Pick","view","canUpdateField","update","getVisibleFields","fields","T","filter","canViewPage","page","canRunAction","action","run","isActionAllowedOn","item","id","kind","partitionByAction","items","allowed","skipped","forEach","push","getActionPlacements","placement","placements","Set","scope","add"],"sources":["../../src/definition/abilities.ts"],"sourcesContent":["import type { Abilities, Action, ActionPlacement, Field, Item, Page } from '@panneau/protocol';\nimport { ITEM_ACTION_ABILITIES, isBuiltInItemAction } from '@panneau/protocol/runtime';\n\n/**\n * Whether a set of abilities allows something. Without abilities (a backend that sends none),\n * everything is allowed and the backend decides. With abilities, only what is `true` is allowed.\n */\nexport function can(abilities: Abilities | undefined, ability: string): boolean {\n return abilities === undefined || abilities[ability] === true;\n}\n\n/**\n * Whether the current user can see a field (decision 0046): unless its `abilities.view` is\n * `false`. A hidden field is left out of forms, details and lists.\n */\nexport function canViewField(field: Pick<Field, 'abilities'>): boolean {\n return field.abilities?.view !== false;\n}\n\n/**\n * Whether the current user can edit a field (decision 0046): they can see it, and its\n * `abilities.update` isn't `false`. A field they can't edit is read-only.\n */\nexport function canUpdateField(field: Pick<Field, 'abilities'>): boolean {\n return canViewField(field) && field.abilities?.update !== false;\n}\n\n/** The fields the current user can see, in their order. */\nexport function getVisibleFields<T extends Pick<Field, 'abilities'>>(fields: readonly T[]): T[] {\n return fields.filter(canViewField);\n}\n\n/**\n * Whether the current user can open a page of the definition (decision 0046): unless its\n * `abilities.view` is `false`.\n */\nexport function canViewPage(page: Pick<Page, 'abilities'>): boolean {\n return page.abilities?.view !== false;\n}\n\n/** Whether the current user can run an action on the resource (`abilities.run`). */\nexport function canRunAction(action: Action): boolean {\n return action.abilities?.run !== false;\n}\n\n/**\n * Whether an action is available on an item: the user can run it, and the item allows it.\n * - The built-in item actions need their ability (decision 0040): `view` for `show`, `update` for\n * `edit`, `duplicate` and `delete`. A missing or false ability hides them, and so does their\n * own id set to `false` (`\"edit\": false`). An item without abilities shows none of them.\n * - Other links are allowed unless the item says otherwise (`\"site\": false`).\n * - Other actions need their id in the abilities of the item (`\"publish\": true`).\n * - On an item without abilities (a product's own records), other actions are allowed: the\n * backend decides.\n */\nexport function isActionAllowedOn(\n action: Action,\n item: { readonly abilities?: Abilities | undefined },\n): boolean {\n if (!canRunAction(action)) {\n return false;\n }\n const { abilities } = item;\n if (isBuiltInItemAction(action.id)) {\n return (\n abilities?.[ITEM_ACTION_ABILITIES[action.id]] === true && abilities[action.id] !== false\n );\n }\n if (abilities === undefined) {\n return true;\n }\n if (action.kind === 'link') {\n return abilities[action.id] !== false;\n }\n return abilities[action.id] === true;\n}\n\n/** Splits the items of a selection by whether an action is allowed on them. */\nexport function partitionByAction(\n action: Action,\n items: readonly Item[],\n): { allowed: Item[]; skipped: Item[] } {\n const allowed: Item[] = [];\n const skipped: Item[] = [];\n items.forEach((item) => (isActionAllowedOn(action, item) ? allowed : skipped).push(item));\n return { allowed, skipped };\n}\n\n/**\n * Where an action appears when it doesn't say: rows and forms (the header of an item, in its\n * panel and its form) for items, the toolbar otherwise.\n */\nexport function getActionPlacements(action: Action): ActionPlacement[] {\n if (action.placement !== undefined) {\n return action.placement;\n }\n const placements = new Set<ActionPlacement>();\n action.scope.forEach((scope) => {\n if (scope === 'item') {\n placements.add('row');\n placements.add('form');\n } else {\n placements.add('toolbar');\n }\n });\n return [...placements];\n}\n"],"mappings":";;;;;;AAOA,SAAgBQ,IAAIC,WAAkCC,SAA0B;CAC5E,OAAOD,cAAcE,KAAAA,KAAaF,UAAUC,aAAa;AAC7D;;;;;AAMA,SAAgBE,aAAaC,OAA0C;CACnE,OAAOA,MAAMJ,WAAWM,SAAS;AACrC;;;;;AAMA,SAAgBC,eAAeH,OAA0C;CACrE,OAAOD,aAAaC,KAAK,KAAKA,MAAMJ,WAAWQ,WAAW;AAC9D;;AAGA,SAAgBC,iBAAqDC,QAA2B;CAC5F,OAAOA,OAAOE,OAAOT,YAAY;AACrC;;;;;AAMA,SAAgBU,YAAYC,MAAwC;CAChE,OAAOA,KAAKd,WAAWM,SAAS;AACpC;;AAGA,SAAgBS,aAAaC,QAAyB;CAClD,OAAOA,OAAOhB,WAAWiB,QAAQ;AACrC;;;;;;;;;;;AAYA,SAAgBC,kBACZF,QACAG,MACO;CACP,IAAI,CAACJ,aAAaC,MAAM,GACpB,OAAO;CAEX,MAAM,EAAEhB,cAAcmB;CACtB,IAAIrB,oBAAoBkB,OAAOI,EAAE,GAC7B,OACIpB,YAAYH,sBAAsBmB,OAAOI,SAAS,QAAQpB,UAAUgB,OAAOI,QAAQ;CAG3F,IAAIpB,cAAcE,KAAAA,GACd,OAAO;CAEX,IAAIc,OAAOK,SAAS,QAChB,OAAOrB,UAAUgB,OAAOI,QAAQ;CAEpC,OAAOpB,UAAUgB,OAAOI,QAAQ;AACpC;;AAGA,SAAgBE,kBACZN,QACAO,OACoC;CACpC,MAAMC,UAAkB,CAAA;CACxB,MAAMC,UAAkB,CAAA;CACxBF,MAAMG,SAASP,UAAUD,kBAAkBF,QAAQG,IAAI,IAAIK,UAAUC,QAAAA,CAASE,KAAKR,IAAI,CAAC;CACxF,OAAO;EAAEK;EAASC;CAAQ;AAC9B;;;;;AAMA,SAAgBG,oBAAoBZ,QAAmC;CACnE,IAAIA,OAAOa,cAAc3B,KAAAA,GACrB,OAAOc,OAAOa;CAElB,MAAMC,6BAAa,IAAIC,IAAqB;CAC5Cf,OAAOgB,MAAMN,SAASM,UAAU;EAC5B,IAAIA,UAAU,QAAQ;GAClBF,WAAWG,IAAI,KAAK;GACpBH,WAAWG,IAAI,MAAM;EACzB,OACIH,WAAWG,IAAI,SAAS;CAEhC,CAAC;CACD,OAAO,CAAC,GAAGH,UAAU;AACzB"}
@@ -8,21 +8,30 @@ export interface ResolvedForm {
8
8
  /** The fields, by name. */
9
9
  fieldsByName: ReadonlyMap<string, Field>;
10
10
  /**
11
- * The layout tree. Field nodes that name an unknown field are removed, and fields that no
12
- * node references are appended at the end, as `field` nodes.
11
+ * The layout tree. Field nodes that name an unknown field, or a field left out of the form,
12
+ * are removed, and fields that no node references are appended at the end, as `field` nodes.
13
13
  */
14
14
  layout: LayoutNode[];
15
15
  }
16
16
  export interface ResolveFormOptions {
17
17
  /**
18
- * `create` removes `updateOnly` fields; `update` makes `createOnly` fields read-only. Without a
19
- * mode, every field is kept as is.
18
+ * `create` removes `updateOnly` fields; `update` makes `createOnly` fields read-only. Rules
19
+ * limited to a mode (`{ "rule": "required", "mode": "create" }`) are kept, without their
20
+ * `mode`, in that mode only, in nested fields too. Without a mode, every field is kept as is.
20
21
  */
21
22
  mode?: FormMode;
22
- /** The type of the item, among the resource's `types`: its fields are added. */
23
- type?: string;
23
+ /**
24
+ * The type of the item, among the resource's `types` (decision 0077): its fields come after
25
+ * those of the resource. The layout's nodes that name the fields of other types are removed.
26
+ */
27
+ type?: string | undefined;
24
28
  }
25
- /** Resolves fields and a layout into a form. */
29
+ /**
30
+ * Resolves fields and a layout into a form. Fields the current user can't see
31
+ * (`abilities.view: false`) are left out, and those they can't edit (`abilities.update: false`)
32
+ * are read-only (decision 0046). Fields that only items give (`notInForm`) are left out too, even
33
+ * when the layout names them (decision 0071).
34
+ */
26
35
  export declare function resolveFields(fields: readonly Field[], layout?: readonly LayoutNode[], { mode }?: Pick<ResolveFormOptions, 'mode'>): ResolvedForm;
27
36
  /** Resolves the form of a resource: its fields (plus those of the item's type) and its layout. */
28
37
  export declare function resolveForm(resource: Resource, options?: ResolveFormOptions): ResolvedForm;
@@ -1 +1 @@
1
- {"version":3,"file":"form.d.ts","sourceRoot":"","sources":["../../src/definition/form.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAErE,qDAAqD;AACrD,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAE3C,yFAAyF;AACzF,MAAM,WAAW,YAAY;IACzB,mDAAmD;IACnD,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,2BAA2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AS,CAAC;CAC7B;AA2DD;;;;;GAKG;AACH,wBAAgB,aAAa,CACzB,MAAM,EAAE,SAAS,KAAK,EAAE,EACxB,MAAM,GAAE,SAAS,UAAU,EAAO,EAClC,EAAE,IAAI,EAAE,GAAE,IAAI,CAAC,kBAAkB,EAAE,MAAM,CAAM,GAChD,YAAY,CAyBd;AAED,kGAAkG;AAClG,wBAAgB,WAAW,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,GAAE,kBAAuB,GAAG,YAAY,CAM9F;AAED,qDAAqD;AACrD,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,SAAS,UAAU,EAAE,GAAG,MAAM,EAAE,CAE3E"}
@@ -1,3 +1,5 @@
1
+ import { canUpdateField, canViewField } from "./abilities.js";
2
+ import { getTypeFields } from "@panneau/protocol/runtime";
1
3
  //#region src/definition/form.ts
2
4
  function referencedNames(nodes, names = /* @__PURE__ */ new Set()) {
3
5
  nodes.forEach((node) => {
@@ -16,15 +18,47 @@ function pruneLayout(nodes, fields) {
16
18
  }];
17
19
  });
18
20
  }
19
- /** Resolves fields and a layout into a form. */
21
+ /** The rules that apply in the mode, without their `mode`. */
22
+ function resolveRules(rules, mode) {
23
+ return rules.flatMap((rule) => {
24
+ if (rule.mode === void 0) return [rule];
25
+ if (rule.mode !== mode) return [];
26
+ const { mode: _mode, ...resolved } = rule;
27
+ return [resolved];
28
+ });
29
+ }
30
+ /** A field with the rules of the mode, and those of its nested fields and block types. */
31
+ function resolveFieldRules(field, mode) {
32
+ const resolved = { ...field };
33
+ if (field.rules !== void 0) resolved.rules = resolveRules(field.rules, mode);
34
+ if (field.fields !== void 0) resolved.fields = field.fields.map((nested) => resolveFieldRules(nested, mode));
35
+ if (field.types !== void 0) resolved.types = field.types.map((type) => ({
36
+ ...type,
37
+ fields: type.fields.map((nested) => resolveFieldRules(nested, mode))
38
+ }));
39
+ return resolved;
40
+ }
41
+ /**
42
+ * Resolves fields and a layout into a form. Fields the current user can't see
43
+ * (`abilities.view: false`) are left out, and those they can't edit (`abilities.update: false`)
44
+ * are read-only (decision 0046). Fields that only items give (`notInForm`) are left out too, even
45
+ * when the layout names them (decision 0071).
46
+ */
20
47
  function resolveFields(fields, layout = [], { mode } = {}) {
21
- const resolved = fields.flatMap((field) => {
48
+ const resolved = fields.flatMap((declared) => {
49
+ if (!canViewField(declared) || declared.notInForm === true) return [];
50
+ const field = canUpdateField(declared) ? declared : {
51
+ ...declared,
52
+ readOnly: true
53
+ };
54
+ if (mode === void 0) return [field];
22
55
  if (mode === "create" && field.updateOnly === true) return [];
56
+ const withRules = resolveFieldRules(field, mode);
23
57
  if (mode === "update" && field.createOnly === true) return [{
24
- ...field,
58
+ ...withRules,
25
59
  readOnly: true
26
60
  }];
27
- return [field];
61
+ return [withRules];
28
62
  });
29
63
  const fieldsByName = new Map(resolved.map((field) => [field.name, field]));
30
64
  const pruned = pruneLayout(layout, fieldsByName);
@@ -41,8 +75,7 @@ function resolveFields(fields, layout = [], { mode } = {}) {
41
75
  }
42
76
  /** Resolves the form of a resource: its fields (plus those of the item's type) and its layout. */
43
77
  function resolveForm(resource, options = {}) {
44
- const typeFields = options.type !== void 0 ? resource.types?.find((type) => type.id === options.type)?.fields ?? [] : [];
45
- return resolveFields([...resource.fields, ...typeFields], resource.form?.layout ?? [], options);
78
+ return resolveFields(getTypeFields(resource, options.type), resource.form?.layout ?? [], options);
46
79
  }
47
80
  /** The field names a layout references, in order. */
48
81
  function getLayoutFieldNames(layout) {
@@ -1 +1 @@
1
- {"version":3,"file":"form.js","names":[],"sources":["../../src/definition/form.ts"],"sourcesContent":["import type { Field, LayoutNode, Resource } from '@panneau/protocol';\n\n/** Whether a form creates an item or updates one. */\nexport type FormMode = 'create' | 'update';\n\n/** A form ready to render: its fields and a layout that references every one of them. */\nexport interface ResolvedForm {\n /** The fields of the form, in definition order. */\n fields: Field[];\n /** The fields, by name. */\n fieldsByName: ReadonlyMap<string, Field>;\n /**\n * The layout tree. Field nodes that name an unknown field are removed, and fields that no\n * node references are appended at the end, as `field` nodes.\n */\n layout: LayoutNode[];\n}\n\nexport interface ResolveFormOptions {\n /**\n * `create` removes `updateOnly` fields; `update` makes `createOnly` fields read-only. Without a\n * mode, every field is kept as is.\n */\n mode?: FormMode;\n /** The type of the item, among the resource's `types`: its fields are added. */\n type?: string;\n}\n\nfunction referencedNames(nodes: readonly LayoutNode[], names = new Set<string>()): Set<string> {\n nodes.forEach((node) => {\n if (node.type === 'field' && node.name !== undefined) {\n names.add(node.name);\n }\n referencedNames(node.children ?? [], names);\n });\n return names;\n}\n\nfunction pruneLayout(\n nodes: readonly LayoutNode[],\n fields: ReadonlyMap<string, Field>,\n): LayoutNode[] {\n return nodes.flatMap((node) => {\n if (node.type === 'field' && (node.name === undefined || !fields.has(node.name))) {\n return [];\n }\n if (node.children === undefined) {\n return [node];\n }\n return [{ ...node, children: pruneLayout(node.children, fields) }];\n });\n}\n\n/** Resolves fields and a layout into a form. */\nexport function resolveFields(\n fields: readonly Field[],\n layout: readonly LayoutNode[] = [],\n { mode }: Pick<ResolveFormOptions, 'mode'> = {},\n): ResolvedForm {\n const resolved = fields.flatMap((field) => {\n if (mode === 'create' && field.updateOnly === true) {\n return [];\n }\n if (mode === 'update' && field.createOnly === true) {\n return [{ ...field, readOnly: true }];\n }\n return [field];\n });\n const fieldsByName = new Map(resolved.map((field) => [field.name, field]));\n const pruned = pruneLayout(layout, fieldsByName);\n const referenced = referencedNames(pruned);\n const appended: LayoutNode[] = resolved\n .filter((field) => !referenced.has(field.name))\n .map((field) => ({ type: 'field', name: field.name }));\n return { fields: resolved, fieldsByName, layout: [...pruned, ...appended] };\n}\n\n/** Resolves the form of a resource: its fields (plus those of the item's type) and its layout. */\nexport function resolveForm(resource: Resource, options: ResolveFormOptions = {}): ResolvedForm {\n const typeFields =\n options.type !== undefined\n ? (resource.types?.find((type) => type.id === options.type)?.fields ?? [])\n : [];\n return resolveFields([...resource.fields, ...typeFields], resource.form?.layout ?? [], options);\n}\n\n/** The field names a layout references, in order. */\nexport function getLayoutFieldNames(layout: readonly LayoutNode[]): string[] {\n return [...referencedNames(layout)];\n}\n"],"mappings":";AA4BA,SAAS,gBAAgB,OAA8B,wBAAQ,IAAI,IAAY,GAAgB;CAC3F,MAAM,SAAS,SAAS;EACpB,IAAI,KAAK,SAAS,WAAW,KAAK,SAAS,KAAA,GACvC,MAAM,IAAI,KAAK,IAAI;EAEvB,gBAAgB,KAAK,YAAY,CAAA,GAAI,KAAK;CAC9C,CAAC;CACD,OAAO;AACX;AAEA,SAAS,YACL,OACA,QACY;CACZ,OAAO,MAAM,SAAS,SAAS;EAC3B,IAAI,KAAK,SAAS,YAAY,KAAK,SAAS,KAAA,KAAa,CAAC,OAAO,IAAI,KAAK,IAAI,IAC1E,OAAO,CAAA;EAEX,IAAI,KAAK,aAAa,KAAA,GAClB,OAAO,CAAC,IAAI;EAEhB,OAAO,CAAC;GAAE,GAAG;GAAM,UAAU,YAAY,KAAK,UAAU,MAAM;EAAE,CAAC;CACrE,CAAC;AACL;;AAGA,SAAgB,cACZ,QACA,SAAgC,CAAA,GAChC,EAAE,SAA2C,CAAC,GAClC;CACZ,MAAM,WAAW,OAAO,SAAS,UAAU;EACvC,IAAI,SAAS,YAAY,MAAM,eAAe,MAC1C,OAAO,CAAA;EAEX,IAAI,SAAS,YAAY,MAAM,eAAe,MAC1C,OAAO,CAAC;GAAE,GAAG;GAAO,UAAU;EAAK,CAAC;EAExC,OAAO,CAAC,KAAK;CACjB,CAAC;CACD,MAAM,eAAe,IAAI,IAAI,SAAS,KAAK,UAAU,CAAC,MAAM,MAAM,KAAK,CAAC,CAAC;CACzE,MAAM,SAAS,YAAY,QAAQ,YAAY;CAC/C,MAAM,aAAa,gBAAgB,MAAM;CACzC,MAAM,WAAyB,SAC1B,QAAQ,UAAU,CAAC,WAAW,IAAI,MAAM,IAAI,CAAC,CAAC,CAC9C,KAAK,WAAW;EAAE,MAAM;EAAS,MAAM,MAAM;CAAK,EAAE;CACzD,OAAO;EAAE,QAAQ;EAAU;EAAc,QAAQ,CAAC,GAAG,QAAQ,GAAG,QAAQ;CAAE;AAC9E;;AAGA,SAAgB,YAAY,UAAoB,UAA8B,CAAC,GAAiB;CAC5F,MAAM,aACF,QAAQ,SAAS,KAAA,IACV,SAAS,OAAO,MAAM,SAAS,KAAK,OAAO,QAAQ,IAAI,CAAC,EAAE,UAAU,CAAA,IACrE,CAAA;CACV,OAAO,cAAc,CAAC,GAAG,SAAS,QAAQ,GAAG,UAAU,GAAG,SAAS,MAAM,UAAU,CAAA,GAAI,OAAO;AAClG;;AAGA,SAAgB,oBAAoB,QAAyC;CACzE,OAAO,CAAC,GAAG,gBAAgB,MAAM,CAAC;AACtC"}
1
+ {"version":3,"file":"form.js","names":["Field","LayoutNode","Resource","Rule","getTypeFields","canUpdateField","canViewField","FormMode","ResolvedForm","fields","fieldsByName","ReadonlyMap","layout","ResolveFormOptions","mode","type","referencedNames","nodes","names","Set","forEach","node","name","undefined","add","children","pruneLayout","flatMap","has","resolveRules","rules","rule","_mode","resolved","resolveFieldRules","field","map","nested","types","resolveFields","Pick","declared","notInForm","readOnly","updateOnly","withRules","createOnly","Map","pruned","referenced","appended","filter","resolveForm","resource","options","form","getLayoutFieldNames"],"sources":["../../src/definition/form.ts"],"sourcesContent":["import type { Field, LayoutNode, Resource, Rule } from '@panneau/protocol';\nimport { getTypeFields } from '@panneau/protocol/runtime';\n\nimport { canUpdateField, canViewField } from './abilities';\n\n/** Whether a form creates an item or updates one. */\nexport type FormMode = 'create' | 'update';\n\n/** A form ready to render: its fields and a layout that references every one of them. */\nexport interface ResolvedForm {\n /** The fields of the form, in definition order. */\n fields: Field[];\n /** The fields, by name. */\n fieldsByName: ReadonlyMap<string, Field>;\n /**\n * The layout tree. Field nodes that name an unknown field, or a field left out of the form,\n * are removed, and fields that no node references are appended at the end, as `field` nodes.\n */\n layout: LayoutNode[];\n}\n\nexport interface ResolveFormOptions {\n /**\n * `create` removes `updateOnly` fields; `update` makes `createOnly` fields read-only. Rules\n * limited to a mode (`{ \"rule\": \"required\", \"mode\": \"create\" }`) are kept, without their\n * `mode`, in that mode only, in nested fields too. Without a mode, every field is kept as is.\n */\n mode?: FormMode;\n /**\n * The type of the item, among the resource's `types` (decision 0077): its fields come after\n * those of the resource. The layout's nodes that name the fields of other types are removed.\n */\n type?: string | undefined;\n}\n\nfunction referencedNames(nodes: readonly LayoutNode[], names = new Set<string>()): Set<string> {\n nodes.forEach((node) => {\n if (node.type === 'field' && node.name !== undefined) {\n names.add(node.name);\n }\n referencedNames(node.children ?? [], names);\n });\n return names;\n}\n\nfunction pruneLayout(\n nodes: readonly LayoutNode[],\n fields: ReadonlyMap<string, Field>,\n): LayoutNode[] {\n return nodes.flatMap((node) => {\n if (node.type === 'field' && (node.name === undefined || !fields.has(node.name))) {\n return [];\n }\n if (node.children === undefined) {\n return [node];\n }\n return [{ ...node, children: pruneLayout(node.children, fields) }];\n });\n}\n\n/** The rules that apply in the mode, without their `mode`. */\nfunction resolveRules(rules: readonly Rule[], mode: FormMode): Rule[] {\n return rules.flatMap((rule) => {\n if (rule.mode === undefined) {\n return [rule];\n }\n if (rule.mode !== mode) {\n return [];\n }\n const { mode: _mode, ...resolved } = rule;\n return [resolved as Rule];\n });\n}\n\n/** A field with the rules of the mode, and those of its nested fields and block types. */\nfunction resolveFieldRules(field: Field, mode: FormMode): Field {\n const resolved: Field = { ...field };\n if (field.rules !== undefined) {\n resolved.rules = resolveRules(field.rules, mode);\n }\n if (field.fields !== undefined) {\n resolved.fields = field.fields.map((nested) => resolveFieldRules(nested, mode));\n }\n if (field.types !== undefined) {\n resolved.types = field.types.map((type) => ({\n ...type,\n fields: type.fields.map((nested) => resolveFieldRules(nested, mode)),\n }));\n }\n return resolved;\n}\n\n/**\n * Resolves fields and a layout into a form. Fields the current user can't see\n * (`abilities.view: false`) are left out, and those they can't edit (`abilities.update: false`)\n * are read-only (decision 0046). Fields that only items give (`notInForm`) are left out too, even\n * when the layout names them (decision 0071).\n */\nexport function resolveFields(\n fields: readonly Field[],\n layout: readonly LayoutNode[] = [],\n { mode }: Pick<ResolveFormOptions, 'mode'> = {},\n): ResolvedForm {\n const resolved = fields.flatMap((declared): Field[] => {\n if (!canViewField(declared) || declared.notInForm === true) {\n return [];\n }\n const field = canUpdateField(declared) ? declared : { ...declared, readOnly: true };\n if (mode === undefined) {\n return [field];\n }\n if (mode === 'create' && field.updateOnly === true) {\n return [];\n }\n const withRules = resolveFieldRules(field, mode);\n if (mode === 'update' && field.createOnly === true) {\n return [{ ...withRules, readOnly: true }];\n }\n return [withRules];\n });\n const fieldsByName = new Map(resolved.map((field) => [field.name, field]));\n const pruned = pruneLayout(layout, fieldsByName);\n const referenced = referencedNames(pruned);\n const appended: LayoutNode[] = resolved\n .filter((field) => !referenced.has(field.name))\n .map((field) => ({ type: 'field', name: field.name }));\n return { fields: resolved, fieldsByName, layout: [...pruned, ...appended] };\n}\n\n/** Resolves the form of a resource: its fields (plus those of the item's type) and its layout. */\nexport function resolveForm(resource: Resource, options: ResolveFormOptions = {}): ResolvedForm {\n return resolveFields(\n getTypeFields(resource, options.type),\n resource.form?.layout ?? [],\n options,\n );\n}\n\n/** The field names a layout references, in order. */\nexport function getLayoutFieldNames(layout: readonly LayoutNode[]): string[] {\n return [...referencedNames(layout)];\n}\n"],"mappings":";;;AAmCA,SAASgB,gBAAgBC,OAA8BC,wBAAQ,IAAIC,IAAY,GAAgB;CAC3FF,MAAMG,SAASC,SAAS;EACpB,IAAIA,KAAKN,SAAS,WAAWM,KAAKC,SAASC,KAAAA,GACvCL,MAAMM,IAAIH,KAAKC,IAAI;EAEvBN,gBAAgBK,KAAKI,YAAY,CAAA,GAAIP,KAAK;CAC9C,CAAC;CACD,OAAOA;AACX;AAEA,SAASQ,YACLT,OACAR,QACY;CACZ,OAAOQ,MAAMU,SAASN,SAAS;EAC3B,IAAIA,KAAKN,SAAS,YAAYM,KAAKC,SAASC,KAAAA,KAAa,CAACd,OAAOmB,IAAIP,KAAKC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F,IAAI7B,SAASS,KAAAA,GACT,OAAO,CAACY,KAAK;EAEjB,IAAIrB,SAAS,YAAYqB,MAAMS,eAAe,MAC1C,OAAO,CAAA;EAEX,MAAMC,YAAYX,kBAAkBC,OAAOrB,IAAI;EAC/C,IAAIA,SAAS,YAAYqB,MAAMW,eAAe,MAC1C,OAAO,CAAC;GAAE,GAAGD;GAAWF,UAAU;EAAK,CAAC;EAE5C,OAAO,CAACE,SAAS;CACrB,CAAC;CACD,MAAMnC,eAAe,IAAIqC,IAAId,SAASG,KAAKD,UAAU,CAACA,MAAMb,MAAMa,KAAK,CAAC,CAAC;CACzE,MAAMa,SAAStB,YAAYd,QAAQF,YAAY;CAC/C,MAAMuC,aAAajC,gBAAgBgC,MAAM;CACzC,MAAME,WAAyBjB,SAC1BkB,QAAQhB,UAAU,CAACc,WAAWrB,IAAIO,MAAMb,IAAI,CAAC,CAAC,CAC9Cc,KAAKD,WAAW;EAAEpB,MAAM;EAASO,MAAMa,MAAMb;CAAK,EAAE;CACzD,OAAO;EAAEb,QAAQwB;EAAUvB;EAAcE,QAAQ,CAAC,GAAGoC,QAAQ,GAAGE,QAAQ;CAAE;AAC9E;;AAGA,SAAgBE,YAAYC,UAAoBC,UAA8B,CAAC,GAAiB;CAC5F,OAAOf,cACHnC,cAAciD,UAAUC,QAAQvC,IAAI,GACpCsC,SAASE,MAAM3C,UAAU,CAAA,GACzB0C,OACJ;AACJ;;AAGA,SAAgBE,oBAAoB5C,QAAyC;CACzE,OAAO,CAAC,GAAGI,gBAAgBJ,MAAM,CAAC;AACtC"}
@@ -0,0 +1,53 @@
1
+ import type { Abilities, Action, Resource } from '@panneau/protocol';
2
+ /**
3
+ * Where an item's actions are shown (decision 0040): the buttons of its row in a list, the
4
+ * overflow menu of its row, the header of its panel (the show route), the header of its form.
5
+ */
6
+ export type ItemSurface = 'row' | 'menu' | 'panel' | 'form';
7
+ /** An item as its actions need it: its id, its abilities, and its values for URL templates. */
8
+ export interface ActionItem {
9
+ id: string;
10
+ /**
11
+ * What the user can do with the item. Without abilities, the built-in item actions are hidden,
12
+ * and the other actions are allowed: the backend decides.
13
+ */
14
+ abilities?: Abilities | undefined;
15
+ [key: string]: unknown;
16
+ }
17
+ /** An action of an item, ready to show: a link, or an action to run. */
18
+ export interface ItemAction {
19
+ action: Action;
20
+ /** The link of a `link` action, or `null` for an action that runs (with the action runner). */
21
+ href: string | null;
22
+ }
23
+ export interface ItemActionsOptions {
24
+ /**
25
+ * The link of a `link` action without a `url`: the built-in `show` and `edit`, and actions
26
+ * with a `route`, which the caller resolves to its routes. A built-in link it doesn't resolve
27
+ * is left out; an action with a `route` then runs, and the action runner navigates.
28
+ */
29
+ getLinkHref?: ((action: Action, item: ActionItem) => string | null) | undefined;
30
+ /** Leaves out the actions the caller can't run, such as `duplicate` without an adapter for it. */
31
+ filter?: ((action: Action) => boolean) | undefined;
32
+ }
33
+ /**
34
+ * The URL template of a `link` action with the values of an item: `{id}`, and other `{field}`
35
+ * with a string or a number. `null` without a `url`.
36
+ */
37
+ export declare function fillActionUrl(action: Action, item?: ActionItem): string | null;
38
+ /**
39
+ * The actions of an item on a surface, from actions already placed there (a group of
40
+ * `resolveList()`'s actions): those that apply to items, that the user can run and that the item
41
+ * allows, without the one that leads to the surface, each with its link. `getItemActions()` is
42
+ * this function for a resource; lists use it with the actions they are given.
43
+ */
44
+ export declare function selectItemActions(actions: readonly Action[], item: ActionItem, surface: ItemSurface, { getLinkHref, filter }?: ItemActionsOptions): ItemAction[];
45
+ /**
46
+ * The actions of an item on a surface (decision 0040), in their order: the built-in item actions
47
+ * of the resource that the item's abilities allow, then its declared actions, by their placement
48
+ * and the item's abilities. Rows, their menu, the panel and the form show the same actions for
49
+ * the same item, but for the one that leads to the surface: no `show` in the panel, no `edit` in
50
+ * the form. The panel and the form show the actions placed in `form`.
51
+ */
52
+ export declare function getItemActions(resource: Resource, item: ActionItem, surface: ItemSurface, options?: ItemActionsOptions): ItemAction[];
53
+ //# sourceMappingURL=item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CAC1B,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,WAAW,EACpB,OAAO,GAAE,kBAAuB,GACjC,UAAU,EAAE,CAGd"}
@@ -0,0 +1,64 @@
1
+ import { isActionAllowedOn } from "./abilities.js";
2
+ import { getResourceActions, groupActions } from "./list.js";
3
+ import { isBuiltInLink } from "@panneau/protocol/runtime";
4
+ //#region src/definition/itemActions.ts
5
+ /** The placement of the actions of each surface: the panel and the form share the item's header. */
6
+ var SURFACE_PLACEMENTS = {
7
+ row: "row",
8
+ menu: "menu",
9
+ panel: "form",
10
+ form: "form"
11
+ };
12
+ /** The action that leads to a surface, which isn't shown on it. */
13
+ var SELF_ACTIONS = {
14
+ panel: "show",
15
+ form: "edit"
16
+ };
17
+ /**
18
+ * The URL template of a `link` action with the values of an item: `{id}`, and other `{field}`
19
+ * with a string or a number. `null` without a `url`.
20
+ */
21
+ function fillActionUrl(action, item) {
22
+ if (action.kind !== "link" || action.url === void 0) return null;
23
+ return action.url.replace(/\{([^}]+)\}/g, (match, name) => {
24
+ const value = item?.[name];
25
+ return typeof value === "string" || typeof value === "number" ? encodeURIComponent(String(value)) : match;
26
+ });
27
+ }
28
+ /**
29
+ * The actions of an item on a surface, from actions already placed there (a group of
30
+ * `resolveList()`'s actions): those that apply to items, that the user can run and that the item
31
+ * allows, without the one that leads to the surface, each with its link. `getItemActions()` is
32
+ * this function for a resource; lists use it with the actions they are given.
33
+ */
34
+ function selectItemActions(actions, item, surface, { getLinkHref, filter } = {}) {
35
+ return actions.flatMap((action) => {
36
+ const allowed = isActionAllowedOn(action, item);
37
+ if (!action.scope.includes("item") || action.id === SELF_ACTIONS[surface] || !allowed || filter !== void 0 && !filter(action)) return [];
38
+ if (action.kind !== "link") return [{
39
+ action,
40
+ href: null
41
+ }];
42
+ const href = fillActionUrl(action, item) ?? getLinkHref?.(action, item) ?? null;
43
+ if (href === null && action.route === void 0 && isBuiltInLink(action.id)) return [];
44
+ return [{
45
+ action,
46
+ href
47
+ }];
48
+ });
49
+ }
50
+ /**
51
+ * The actions of an item on a surface (decision 0040), in their order: the built-in item actions
52
+ * of the resource that the item's abilities allow, then its declared actions, by their placement
53
+ * and the item's abilities. Rows, their menu, the panel and the form show the same actions for
54
+ * the same item, but for the one that leads to the surface: no `show` in the panel, no `edit` in
55
+ * the form. The panel and the form show the actions placed in `form`.
56
+ */
57
+ function getItemActions(resource, item, surface, options = {}) {
58
+ const placed = groupActions(getResourceActions(resource))[SURFACE_PLACEMENTS[surface]];
59
+ return selectItemActions(placed, item, surface, options);
60
+ }
61
+ //#endregion
62
+ export { fillActionUrl, getItemActions, selectItemActions };
63
+
64
+ //# sourceMappingURL=itemActions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"itemActions.js","names":["Abilities","Action","ActionPlacement","Resource","isBuiltInLink","isActionAllowedOn","getResourceActions","groupActions","ItemSurface","ActionItem","id","abilities","key","ItemAction","action","href","ItemActionsOptions","getLinkHref","item","filter","SURFACE_PLACEMENTS","Readonly","Record","row","menu","panel","form","SELF_ACTIONS","Partial","fillActionUrl","kind","url","undefined","replace","match","name","value","encodeURIComponent","String","selectItemActions","actions","surface","flatMap","allowed","scope","includes","route","getItemActions","resource","options","placed"],"sources":["../../src/definition/itemActions.ts"],"sourcesContent":["import type { Abilities, Action, ActionPlacement, Resource } from '@panneau/protocol';\nimport { isBuiltInLink } from '@panneau/protocol/runtime';\n\nimport { isActionAllowedOn } from './abilities';\nimport { getResourceActions, groupActions } from './list';\n\n/**\n * Where an item's actions are shown (decision 0040): the buttons of its row in a list, the\n * overflow menu of its row, the header of its panel (the show route), the header of its form.\n */\nexport type ItemSurface = 'row' | 'menu' | 'panel' | 'form';\n\n/** An item as its actions need it: its id, its abilities, and its values for URL templates. */\nexport interface ActionItem {\n id: string;\n /**\n * What the user can do with the item. Without abilities, the built-in item actions are hidden,\n * and the other actions are allowed: the backend decides.\n */\n abilities?: Abilities | undefined;\n [key: string]: unknown;\n}\n\n/** An action of an item, ready to show: a link, or an action to run. */\nexport interface ItemAction {\n action: Action;\n /** The link of a `link` action, or `null` for an action that runs (with the action runner). */\n href: string | null;\n}\n\nexport interface ItemActionsOptions {\n /**\n * The link of a `link` action without a `url`: the built-in `show` and `edit`, and actions\n * with a `route`, which the caller resolves to its routes. A built-in link it doesn't resolve\n * is left out; an action with a `route` then runs, and the action runner navigates.\n */\n getLinkHref?: ((action: Action, item: ActionItem) => string | null) | undefined;\n /** Leaves out the actions the caller can't run, such as `duplicate` without an adapter for it. */\n filter?: ((action: Action) => boolean) | undefined;\n}\n\n/** The placement of the actions of each surface: the panel and the form share the item's header. */\nconst SURFACE_PLACEMENTS: Readonly<Record<ItemSurface, ActionPlacement>> = {\n row: 'row',\n menu: 'menu',\n panel: 'form',\n form: 'form',\n};\n\n/** The action that leads to a surface, which isn't shown on it. */\nconst SELF_ACTIONS: Readonly<Partial<Record<ItemSurface, string>>> = {\n panel: 'show',\n form: 'edit',\n};\n\n/**\n * The URL template of a `link` action with the values of an item: `{id}`, and other `{field}`\n * with a string or a number. `null` without a `url`.\n */\nexport function fillActionUrl(action: Action, item?: ActionItem): string | null {\n if (action.kind !== 'link' || action.url === undefined) {\n return null;\n }\n return action.url.replace(/\\{([^}]+)\\}/g, (match, name: string) => {\n const value = item?.[name];\n return typeof value === 'string' || typeof value === 'number'\n ? encodeURIComponent(String(value))\n : match;\n });\n}\n\n/**\n * The actions of an item on a surface, from actions already placed there (a group of\n * `resolveList()`'s actions): those that apply to items, that the user can run and that the item\n * allows, without the one that leads to the surface, each with its link. `getItemActions()` is\n * this function for a resource; lists use it with the actions they are given.\n */\nexport function selectItemActions(\n actions: readonly Action[],\n item: ActionItem,\n surface: ItemSurface,\n { getLinkHref, filter }: ItemActionsOptions = {},\n): ItemAction[] {\n return actions.flatMap((action): ItemAction[] => {\n const allowed = isActionAllowedOn(action, item);\n if (\n !action.scope.includes('item') ||\n action.id === SELF_ACTIONS[surface] ||\n !allowed ||\n (filter !== undefined && !filter(action))\n ) {\n return [];\n }\n if (action.kind !== 'link') {\n return [{ action, href: null }];\n }\n const href = fillActionUrl(action, item) ?? getLinkHref?.(action, item) ?? null;\n if (href === null && action.route === undefined && isBuiltInLink(action.id)) {\n // A built-in link that nothing resolves goes nowhere\n return [];\n }\n return [{ action, href }];\n });\n}\n\n/**\n * The actions of an item on a surface (decision 0040), in their order: the built-in item actions\n * of the resource that the item's abilities allow, then its declared actions, by their placement\n * and the item's abilities. Rows, their menu, the panel and the form show the same actions for\n * the same item, but for the one that leads to the surface: no `show` in the panel, no `edit` in\n * the form. The panel and the form show the actions placed in `form`.\n */\nexport function getItemActions(\n resource: Resource,\n item: ActionItem,\n surface: ItemSurface,\n options: ItemActionsOptions = {},\n): ItemAction[] {\n const placed = groupActions(getResourceActions(resource))[SURFACE_PLACEMENTS[surface]];\n return selectItemActions(placed, item, surface, options);\n}\n"],"mappings":";;;;;AA0CA,IAAMoB,qBAAqE;CACvEG,KAAK;CACLC,MAAM;CACNC,OAAO;CACPC,MAAM;AACV;;AAGA,IAAMC,eAA+D;CACjEF,OAAO;CACPC,MAAM;AACV;;;;;AAMA,SAAgBG,cAAcf,QAAgBI,MAAkC;CAC5E,IAAIJ,OAAOgB,SAAS,UAAUhB,OAAOiB,QAAQC,KAAAA,GACzC,OAAO;CAEX,OAAOlB,OAAOiB,IAAIE,QAAQ,iBAAiBC,OAAOC,SAAiB;EAC/D,MAAMC,QAAQlB,OAAOiB;EACrB,OAAO,OAAOC,UAAU,YAAY,OAAOA,UAAU,WAC/CC,mBAAmBC,OAAOF,KAAK,CAAC,IAChCF;CACV,CAAC;AACL;;;;;;;AAQA,SAAgBK,kBACZC,SACAtB,MACAuB,SACA,EAAExB,aAAaE,WAA+B,CAAC,GACnC;CACZ,OAAOqB,QAAQE,SAAS5B,WAAyB;EAC7C,MAAM6B,UAAUtC,kBAAkBS,QAAQI,IAAI;EAC9C,IACI,CAACJ,OAAO8B,MAAMC,SAAS,MAAM,KAC7B/B,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