@canonical/prism 0.0.0-stage → 0.51.0-experimental.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 (191) hide show
  1. package/README.md +357 -2
  2. package/dist/esm/index.js +22 -0
  3. package/dist/esm/lib/Card/Card.js +16 -0
  4. package/dist/esm/lib/Card/CardView.js +19 -0
  5. package/dist/esm/lib/Card/index.js +2 -0
  6. package/dist/esm/lib/Card/styles.css +68 -0
  7. package/dist/esm/lib/Card/types.js +1 -0
  8. package/dist/esm/lib/Chip/Chip.js +16 -0
  9. package/dist/esm/lib/Chip/ChipView.js +14 -0
  10. package/dist/esm/lib/Chip/index.js +2 -0
  11. package/dist/esm/lib/Chip/styles.css +11 -0
  12. package/dist/esm/lib/Chip/types.js +1 -0
  13. package/dist/esm/lib/CommandPalette/CommandPalette.js +166 -0
  14. package/dist/esm/lib/CommandPalette/ThingOptions.js +38 -0
  15. package/dist/esm/lib/CommandPalette/index.js +1 -0
  16. package/dist/esm/lib/CommandPalette/styles.css +91 -0
  17. package/dist/esm/lib/CommandPalette/types.js +1 -0
  18. package/dist/esm/lib/Fact/FactValue.js +12 -0
  19. package/dist/esm/lib/Fact/index.js +1 -0
  20. package/dist/esm/lib/Fact/types.js +1 -0
  21. package/dist/esm/lib/Navigation/Navigation.js +46 -0
  22. package/dist/esm/lib/Navigation/index.js +1 -0
  23. package/dist/esm/lib/Navigation/styles.css +44 -0
  24. package/dist/esm/lib/Navigation/types.js +1 -0
  25. package/dist/esm/lib/PageHeader/PageHeader.js +16 -0
  26. package/dist/esm/lib/PageHeader/PageHeaderView.js +18 -0
  27. package/dist/esm/lib/PageHeader/index.js +2 -0
  28. package/dist/esm/lib/PageHeader/readParents.js +19 -0
  29. package/dist/esm/lib/PageHeader/styles.css +67 -0
  30. package/dist/esm/lib/PageHeader/types.js +1 -0
  31. package/dist/esm/lib/Prism/Prism.js +14 -0
  32. package/dist/esm/lib/Prism/context.js +10 -0
  33. package/dist/esm/lib/Prism/index.js +2 -0
  34. package/dist/esm/lib/Prism/types.js +1 -0
  35. package/dist/esm/lib/Prose/Prose.js +26 -0
  36. package/dist/esm/lib/Prose/findMention.js +22 -0
  37. package/dist/esm/lib/Prose/index.js +2 -0
  38. package/dist/esm/lib/Prose/styles.css +10 -0
  39. package/dist/esm/lib/Prose/types.js +1 -0
  40. package/dist/esm/lib/Row/Row.js +16 -0
  41. package/dist/esm/lib/Row/RowView.js +18 -0
  42. package/dist/esm/lib/Row/index.js +2 -0
  43. package/dist/esm/lib/Row/styles.css +28 -0
  44. package/dist/esm/lib/Row/types.js +1 -0
  45. package/dist/esm/lib/SearchBoundary/SearchBoundary.js +37 -0
  46. package/dist/esm/lib/SearchBoundary/index.js +1 -0
  47. package/dist/esm/lib/SearchBoundary/types.js +1 -0
  48. package/dist/esm/lib/SearchPage/SearchPage.js +35 -0
  49. package/dist/esm/lib/SearchPage/index.js +1 -0
  50. package/dist/esm/lib/SearchPage/styles.css +62 -0
  51. package/dist/esm/lib/SearchPage/types.js +1 -0
  52. package/dist/esm/lib/SiteLayout/SiteLayout.js +20 -0
  53. package/dist/esm/lib/SiteLayout/index.js +1 -0
  54. package/dist/esm/lib/SiteLayout/styles.css +112 -0
  55. package/dist/esm/lib/SiteLayout/types.js +1 -0
  56. package/dist/esm/lib/ThingLink/ThingLink.js +10 -0
  57. package/dist/esm/lib/ThingLink/index.js +1 -0
  58. package/dist/esm/lib/ThingLink/types.js +1 -0
  59. package/dist/esm/lib/ThingResults/ThingList.js +11 -0
  60. package/dist/esm/lib/ThingResults/ThingResults.js +36 -0
  61. package/dist/esm/lib/ThingResults/index.js +1 -0
  62. package/dist/esm/lib/ThingResults/types.js +1 -0
  63. package/dist/esm/lib/graphql/PrismCard.js +35 -0
  64. package/dist/esm/lib/graphql/PrismChip.js +17 -0
  65. package/dist/esm/lib/graphql/PrismClasses.js +33 -0
  66. package/dist/esm/lib/graphql/PrismFact.js +17 -0
  67. package/dist/esm/lib/graphql/PrismPageHeader.js +59 -0
  68. package/dist/esm/lib/graphql/PrismRow.js +32 -0
  69. package/dist/esm/lib/graphql/__generated__/PrismCard_node.graphql.js +136 -0
  70. package/dist/esm/lib/graphql/__generated__/PrismChip_node.graphql.js +64 -0
  71. package/dist/esm/lib/graphql/__generated__/PrismClasses_node.graphql.js +92 -0
  72. package/dist/esm/lib/graphql/__generated__/PrismFact_fact.graphql.js +46 -0
  73. package/dist/esm/lib/graphql/__generated__/PrismPageHeader_node.graphql.js +237 -0
  74. package/dist/esm/lib/graphql/__generated__/PrismRow_node.graphql.js +127 -0
  75. package/dist/esm/lib/graphql/index.js +6 -0
  76. package/dist/esm/lib/lens/constants.js +2 -0
  77. package/dist/esm/lib/lens/index.js +3 -0
  78. package/dist/esm/lib/lens/isShortcutKey.js +22 -0
  79. package/dist/esm/lib/lens/readLenses.js +106 -0
  80. package/dist/esm/lib/lens/types.js +1 -0
  81. package/dist/esm/lib/projection/findNearest.js +12 -0
  82. package/dist/esm/lib/projection/index.js +5 -0
  83. package/dist/esm/lib/projection/orderClasses.js +14 -0
  84. package/dist/esm/lib/projection/readHomes.js +43 -0
  85. package/dist/esm/lib/projection/types.js +1 -0
  86. package/dist/esm/lib/projection/useProjection.js +34 -0
  87. package/dist/esm/lib/projection/withLabel.js +9 -0
  88. package/dist/esm/lib/renderPage/index.js +1 -0
  89. package/dist/esm/lib/renderPage/renderPage.js +125 -0
  90. package/dist/esm/lib/renderPage/types.js +1 -0
  91. package/dist/esm/lib/search/filterLenses.js +11 -0
  92. package/dist/esm/lib/search/index.js +3 -0
  93. package/dist/esm/lib/search/searchVariables.js +11 -0
  94. package/dist/esm/lib/search/warmSearch.js +14 -0
  95. package/dist/esm/lib/searchQuery/PrismSearchQuery.js +23 -0
  96. package/dist/esm/lib/searchQuery/__generated__/PrismSearchQuery.graphql.js +304 -0
  97. package/dist/esm/lib/searchQuery/index.js +1 -0
  98. package/dist/esm/lib/warmQuery/index.js +1 -0
  99. package/dist/esm/lib/warmQuery/warmQuery.js +43 -0
  100. package/dist/esm/server.js +6 -0
  101. package/dist/types/index.d.ts +24 -0
  102. package/dist/types/lib/Card/Card.d.ts +8 -0
  103. package/dist/types/lib/Card/CardView.d.ts +11 -0
  104. package/dist/types/lib/Card/index.d.ts +3 -0
  105. package/dist/types/lib/Card/types.d.ts +31 -0
  106. package/dist/types/lib/Chip/Chip.d.ts +8 -0
  107. package/dist/types/lib/Chip/ChipView.d.ts +9 -0
  108. package/dist/types/lib/Chip/index.d.ts +3 -0
  109. package/dist/types/lib/Chip/types.d.ts +24 -0
  110. package/dist/types/lib/CommandPalette/CommandPalette.d.ts +16 -0
  111. package/dist/types/lib/CommandPalette/ThingOptions.d.ts +7 -0
  112. package/dist/types/lib/CommandPalette/index.d.ts +2 -0
  113. package/dist/types/lib/CommandPalette/types.d.ts +45 -0
  114. package/dist/types/lib/Fact/FactValue.d.ts +7 -0
  115. package/dist/types/lib/Fact/index.d.ts +2 -0
  116. package/dist/types/lib/Fact/types.d.ts +5 -0
  117. package/dist/types/lib/Navigation/Navigation.d.ts +15 -0
  118. package/dist/types/lib/Navigation/index.d.ts +2 -0
  119. package/dist/types/lib/Navigation/types.d.ts +6 -0
  120. package/dist/types/lib/PageHeader/PageHeader.d.ts +8 -0
  121. package/dist/types/lib/PageHeader/PageHeaderView.d.ts +10 -0
  122. package/dist/types/lib/PageHeader/index.d.ts +3 -0
  123. package/dist/types/lib/PageHeader/readParents.d.ts +13 -0
  124. package/dist/types/lib/PageHeader/types.d.ts +32 -0
  125. package/dist/types/lib/Prism/Prism.d.ts +8 -0
  126. package/dist/types/lib/Prism/context.d.ts +7 -0
  127. package/dist/types/lib/Prism/index.d.ts +3 -0
  128. package/dist/types/lib/Prism/types.d.ts +67 -0
  129. package/dist/types/lib/Prose/Prose.d.ts +11 -0
  130. package/dist/types/lib/Prose/findMention.d.ts +8 -0
  131. package/dist/types/lib/Prose/index.d.ts +3 -0
  132. package/dist/types/lib/Prose/types.d.ts +7 -0
  133. package/dist/types/lib/Row/Row.d.ts +8 -0
  134. package/dist/types/lib/Row/RowView.d.ts +12 -0
  135. package/dist/types/lib/Row/index.d.ts +3 -0
  136. package/dist/types/lib/Row/types.d.ts +25 -0
  137. package/dist/types/lib/SearchBoundary/SearchBoundary.d.ts +17 -0
  138. package/dist/types/lib/SearchBoundary/index.d.ts +2 -0
  139. package/dist/types/lib/SearchBoundary/types.d.ts +22 -0
  140. package/dist/types/lib/SearchPage/SearchPage.d.ts +15 -0
  141. package/dist/types/lib/SearchPage/index.d.ts +2 -0
  142. package/dist/types/lib/SearchPage/types.d.ts +10 -0
  143. package/dist/types/lib/SiteLayout/SiteLayout.d.ts +17 -0
  144. package/dist/types/lib/SiteLayout/index.d.ts +2 -0
  145. package/dist/types/lib/SiteLayout/types.d.ts +11 -0
  146. package/dist/types/lib/ThingLink/ThingLink.d.ts +8 -0
  147. package/dist/types/lib/ThingLink/index.d.ts +2 -0
  148. package/dist/types/lib/ThingLink/types.d.ts +9 -0
  149. package/dist/types/lib/ThingResults/ThingList.d.ts +4 -0
  150. package/dist/types/lib/ThingResults/ThingResults.d.ts +16 -0
  151. package/dist/types/lib/ThingResults/index.d.ts +2 -0
  152. package/dist/types/lib/ThingResults/types.d.ts +14 -0
  153. package/dist/types/lib/graphql/PrismCard.d.ts +6 -0
  154. package/dist/types/lib/graphql/PrismChip.d.ts +3 -0
  155. package/dist/types/lib/graphql/PrismClasses.d.ts +8 -0
  156. package/dist/types/lib/graphql/PrismFact.d.ts +6 -0
  157. package/dist/types/lib/graphql/PrismPageHeader.d.ts +8 -0
  158. package/dist/types/lib/graphql/PrismRow.d.ts +6 -0
  159. package/dist/types/lib/graphql/__generated__/PrismCard_node.graphql.d.ts +38 -0
  160. package/dist/types/lib/graphql/__generated__/PrismChip_node.graphql.d.ts +23 -0
  161. package/dist/types/lib/graphql/__generated__/PrismClasses_node.graphql.d.ts +33 -0
  162. package/dist/types/lib/graphql/__generated__/PrismFact_fact.graphql.d.ts +20 -0
  163. package/dist/types/lib/graphql/__generated__/PrismPageHeader_node.graphql.d.ts +60 -0
  164. package/dist/types/lib/graphql/__generated__/PrismRow_node.graphql.d.ts +35 -0
  165. package/dist/types/lib/graphql/index.d.ts +6 -0
  166. package/dist/types/lib/lens/constants.d.ts +2 -0
  167. package/dist/types/lib/lens/index.d.ts +4 -0
  168. package/dist/types/lib/lens/isShortcutKey.d.ts +6 -0
  169. package/dist/types/lib/lens/readLenses.d.ts +27 -0
  170. package/dist/types/lib/lens/types.d.ts +48 -0
  171. package/dist/types/lib/projection/findNearest.d.ts +6 -0
  172. package/dist/types/lib/projection/index.d.ts +6 -0
  173. package/dist/types/lib/projection/orderClasses.d.ts +19 -0
  174. package/dist/types/lib/projection/readHomes.d.ts +11 -0
  175. package/dist/types/lib/projection/types.d.ts +15 -0
  176. package/dist/types/lib/projection/useProjection.d.ts +9 -0
  177. package/dist/types/lib/projection/withLabel.d.ts +8 -0
  178. package/dist/types/lib/renderPage/index.d.ts +2 -0
  179. package/dist/types/lib/renderPage/renderPage.d.ts +21 -0
  180. package/dist/types/lib/renderPage/types.d.ts +49 -0
  181. package/dist/types/lib/search/filterLenses.d.ts +6 -0
  182. package/dist/types/lib/search/index.d.ts +3 -0
  183. package/dist/types/lib/search/searchVariables.d.ts +9 -0
  184. package/dist/types/lib/search/warmSearch.d.ts +9 -0
  185. package/dist/types/lib/searchQuery/PrismSearchQuery.d.ts +8 -0
  186. package/dist/types/lib/searchQuery/__generated__/PrismSearchQuery.graphql.d.ts +26 -0
  187. package/dist/types/lib/searchQuery/index.d.ts +1 -0
  188. package/dist/types/lib/warmQuery/index.d.ts +1 -0
  189. package/dist/types/lib/warmQuery/warmQuery.d.ts +19 -0
  190. package/dist/types/server.d.ts +6 -0
  191. package/package.json +107 -4
@@ -0,0 +1,127 @@
1
+ /**
2
+ * @generated SignedSource<<cf76fbf26467e2b6af5c7d2212890e21>>
3
+ * @lightSyntaxTransform
4
+ */
5
+ const node = (function () {
6
+ var v0 = {
7
+ "alias": null,
8
+ "args": null,
9
+ "kind": "ScalarField",
10
+ "name": "curie",
11
+ "storageKey": null
12
+ }, v1 = {
13
+ "alias": null,
14
+ "args": null,
15
+ "kind": "ScalarField",
16
+ "name": "label",
17
+ "storageKey": null
18
+ };
19
+ return {
20
+ "argumentDefinitions": [],
21
+ "kind": "Fragment",
22
+ "metadata": null,
23
+ "name": "PrismRow_node",
24
+ "selections": [
25
+ {
26
+ "alias": null,
27
+ "args": null,
28
+ "kind": "ScalarField",
29
+ "name": "uri",
30
+ "storageKey": null
31
+ },
32
+ {
33
+ "args": null,
34
+ "kind": "FragmentSpread",
35
+ "name": "PrismClasses_node"
36
+ },
37
+ {
38
+ "alias": null,
39
+ "args": null,
40
+ "concreteType": "EntityMeta",
41
+ "kind": "LinkedField",
42
+ "name": "_meta",
43
+ "plural": false,
44
+ "selections": [
45
+ (v0 /*:: as any*/),
46
+ {
47
+ "alias": null,
48
+ "args": null,
49
+ "concreteType": "EntitySlots",
50
+ "kind": "LinkedField",
51
+ "name": "slots",
52
+ "plural": false,
53
+ "selections": [
54
+ (v1 /*:: as any*/),
55
+ {
56
+ "alias": null,
57
+ "args": null,
58
+ "kind": "ScalarField",
59
+ "name": "category",
60
+ "storageKey": null
61
+ },
62
+ {
63
+ "alias": null,
64
+ "args": null,
65
+ "concreteType": "EntityFact",
66
+ "kind": "LinkedField",
67
+ "name": "status",
68
+ "plural": false,
69
+ "selections": [
70
+ {
71
+ "alias": null,
72
+ "args": null,
73
+ "kind": "ScalarField",
74
+ "name": "value",
75
+ "storageKey": null
76
+ },
77
+ {
78
+ "alias": null,
79
+ "args": null,
80
+ "concreteType": null,
81
+ "kind": "LinkedField",
82
+ "name": "node",
83
+ "plural": false,
84
+ "selections": [
85
+ {
86
+ "alias": null,
87
+ "args": null,
88
+ "concreteType": "EntityMeta",
89
+ "kind": "LinkedField",
90
+ "name": "_meta",
91
+ "plural": false,
92
+ "selections": [
93
+ (v0 /*:: as any*/),
94
+ {
95
+ "alias": null,
96
+ "args": null,
97
+ "concreteType": "EntitySlots",
98
+ "kind": "LinkedField",
99
+ "name": "slots",
100
+ "plural": false,
101
+ "selections": [
102
+ (v1 /*:: as any*/)
103
+ ],
104
+ "storageKey": null
105
+ }
106
+ ],
107
+ "storageKey": null
108
+ }
109
+ ],
110
+ "storageKey": null
111
+ }
112
+ ],
113
+ "storageKey": null
114
+ }
115
+ ],
116
+ "storageKey": null
117
+ }
118
+ ],
119
+ "storageKey": null
120
+ }
121
+ ],
122
+ "type": "Node",
123
+ "abstractKey": "__isNode"
124
+ };
125
+ })();
126
+ node.hash = "b77bb8e84d9b7dcc2bc364073fb6cf29";
127
+ export default node;
@@ -0,0 +1,6 @@
1
+ export * from "./PrismCard.js";
2
+ export * from "./PrismChip.js";
3
+ export * from "./PrismClasses.js";
4
+ export * from "./PrismFact.js";
5
+ export * from "./PrismPageHeader.js";
6
+ export * from "./PrismRow.js";
@@ -0,0 +1,2 @@
1
+ /** The key that opens the command palette; no lens may claim it. */
2
+ export const PALETTE_KEY = "/";
@@ -0,0 +1,3 @@
1
+ export { PALETTE_KEY } from "./constants.js";
2
+ export { default as isShortcutKey } from "./isShortcutKey.js";
3
+ export { checkLenses, readCurrentLens, readLenses, readLensOf, } from "./readLenses.js";
@@ -0,0 +1,22 @@
1
+ function isEditable(target) {
2
+ if (!(target instanceof HTMLElement))
3
+ return false;
4
+ return (target.isContentEditable ||
5
+ target instanceof HTMLInputElement ||
6
+ target instanceof HTMLTextAreaElement ||
7
+ target instanceof HTMLSelectElement);
8
+ }
9
+ /**
10
+ * Whether a key press may act as a single-key shortcut: no modifier, not
11
+ * part of a composition or a held key, and not typed into a field, where the
12
+ * key belongs to the text.
13
+ */
14
+ export default function isShortcutKey(event) {
15
+ return !(event.altKey ||
16
+ event.ctrlKey ||
17
+ event.metaKey ||
18
+ event.shiftKey ||
19
+ event.isComposing ||
20
+ event.repeat ||
21
+ isEditable(event.target));
22
+ }
@@ -0,0 +1,106 @@
1
+ import { isValidElement } from "react";
2
+ import { PALETTE_KEY } from "./constants.js";
3
+ function isRecord(value) {
4
+ return typeof value === "object" && value !== null;
5
+ }
6
+ /**
7
+ * The `meta.lens` of a route, read whatever meta type the site declared.
8
+ *
9
+ * @throws When `meta.lens` is there but is not a lens.
10
+ */
11
+ function parseLens(name, route) {
12
+ const lens = isRecord(route.meta) ? route.meta.lens : undefined;
13
+ if (lens === undefined)
14
+ return undefined;
15
+ if (!isRecord(lens) ||
16
+ typeof lens.label !== "string" ||
17
+ typeof lens.order !== "number" ||
18
+ !Number.isFinite(lens.order) ||
19
+ (lens.icon !== undefined && !isValidElement(lens.icon)) ||
20
+ (lens.shortcut !== undefined && typeof lens.shortcut !== "string")) {
21
+ throw new Error(`route "${name}" has a meta.lens without a label and an order`);
22
+ }
23
+ return {
24
+ label: lens.label,
25
+ order: lens.order,
26
+ ...(isValidElement(lens.icon) ? { icon: lens.icon } : {}),
27
+ ...(typeof lens.shortcut === "string" ? { shortcut: lens.shortcut } : {}),
28
+ };
29
+ }
30
+ /** The `meta.lensOf` of a route, read whatever meta type the site declared. */
31
+ export function readLensOf(route) {
32
+ const lensOf = isRecord(route?.meta) ? route.meta.lensOf : undefined;
33
+ return typeof lensOf === "string" ? lensOf : undefined;
34
+ }
35
+ /**
36
+ * Whether a route's address takes parameters (a segment starting with `:`)
37
+ * or a wildcard; a colon inside a segment is a literal.
38
+ */
39
+ function takesParams(url) {
40
+ return /(^|\/):|\*/.test(url);
41
+ }
42
+ const cache = new WeakMap();
43
+ /**
44
+ * The site's lenses, read and checked once per route table: every route
45
+ * carrying `meta.lens`, in the order the lenses state.
46
+ *
47
+ * @throws On a mistake in the route table, so the site fails on its first
48
+ * render or request rather than drawing a wrong navigation: a malformed
49
+ * `meta.lens`; a lens whose list route takes parameters; two lenses in one
50
+ * place or on one shortcut; a shortcut on the palette's key; a `lensOf`
51
+ * that names no lens.
52
+ */
53
+ export function checkLenses(routes) {
54
+ const cached = cache.get(routes);
55
+ if (cached)
56
+ return cached;
57
+ const lenses = [];
58
+ for (const [name, route] of Object.entries(routes)) {
59
+ const lens = parseLens(name, route);
60
+ if (!lens)
61
+ continue;
62
+ if (takesParams(route.url)) {
63
+ throw new Error(`lens "${name}" takes params (${route.url}); a lens is its list route`);
64
+ }
65
+ if (lens.shortcut === PALETTE_KEY) {
66
+ throw new Error(`lens "${name}" claims "${PALETTE_KEY}", the command palette's key`);
67
+ }
68
+ for (const other of lenses) {
69
+ if (other.order === lens.order) {
70
+ throw new Error(`lenses "${other.name}" and "${name}" share the order ${lens.order}`);
71
+ }
72
+ if (lens.shortcut !== undefined && other.shortcut === lens.shortcut) {
73
+ throw new Error(`shortcut "${lens.shortcut}" is claimed by both "${other.name}" and "${name}"`);
74
+ }
75
+ }
76
+ lenses.push({ ...lens, name });
77
+ }
78
+ for (const [name, route] of Object.entries(routes)) {
79
+ const lensOf = readLensOf(route);
80
+ if (lensOf !== undefined && !lenses.some((lens) => lens.name === lensOf)) {
81
+ throw new Error(`route "${name}" names "${lensOf}" in meta.lensOf, which is not a lens`);
82
+ }
83
+ }
84
+ lenses.sort((left, right) => left.order - right.order);
85
+ cache.set(routes, lenses);
86
+ return lenses;
87
+ }
88
+ /**
89
+ * The site's lenses with their addresses, built by the caller's router: the
90
+ * checked list is cached by route table, the addresses are not.
91
+ */
92
+ export function readLenses(routes, buildPath) {
93
+ return checkLenses(routes).map((lens) => ({
94
+ ...lens,
95
+ href: buildPath(lens.name),
96
+ }));
97
+ }
98
+ /**
99
+ * The name of the lens the current page belongs to: the matched route's
100
+ * `meta.lensOf`, or the matched route itself.
101
+ */
102
+ export function readCurrentLens(routes, matchedName) {
103
+ if (matchedName === undefined)
104
+ return undefined;
105
+ return readLensOf(routes[matchedName]) ?? matchedName;
106
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The first answer `lookup` gives along a chain of classes, nearest first,
3
+ * asking each class by its IRI and then by its compact name.
4
+ */
5
+ export default function findNearest(chain, lookup) {
6
+ for (const { uri, curie } of chain) {
7
+ const answer = lookup(uri) ?? lookup(curie);
8
+ if (answer !== undefined)
9
+ return answer;
10
+ }
11
+ return undefined;
12
+ }
@@ -0,0 +1,5 @@
1
+ export { default as findNearest } from "./findNearest.js";
2
+ export { default as orderClasses } from "./orderClasses.js";
3
+ export { default as readHomes } from "./readHomes.js";
4
+ export { default as useProjection } from "./useProjection.js";
5
+ export { default as withLabel } from "./withLabel.js";
@@ -0,0 +1,14 @@
1
+ /**
2
+ * A thing's classes, nearest first: its own class, then its ancestors, each
3
+ * after every class below it. An ancestor of a class has strictly fewer
4
+ * ancestors than that class, so ordering by how many ancestors each has puts
5
+ * every class before its superclasses, whatever order the graph lists them
6
+ * in; ancestors on separate branches keep the graph's order.
7
+ */
8
+ export default function orderClasses(type) {
9
+ const ancestors = [...type.superclasses].sort((a, b) => b.superclasses.length - a.superclasses.length);
10
+ return [type, ...ancestors].map((entry) => ({
11
+ uri: entry.uri,
12
+ curie: entry._meta.curie,
13
+ }));
14
+ }
@@ -0,0 +1,43 @@
1
+ /** The home routes of each route table read so far, read once each. */
2
+ const read = new WeakMap();
3
+ /** The names of a route URL's params, in order. */
4
+ function paramsOf(url) {
5
+ return [...url.matchAll(/:([A-Za-z_$][\w$]*)/g)].map(([, name]) => name ?? "");
6
+ }
7
+ /**
8
+ * The home routes of a site, by the class each one shows: the routes whose
9
+ * `meta.home` names a class. Read once per route table, however many
10
+ * projections ask.
11
+ *
12
+ * @throws When a home route takes any param but `:uri`, or when two routes
13
+ * claim the same class: both are mistakes in the site's route table,
14
+ * found on the first render.
15
+ */
16
+ export default function readHomes(routes) {
17
+ const cached = read.get(routes);
18
+ if (cached)
19
+ return cached;
20
+ const homes = new Map();
21
+ for (const [name, route] of Object.entries(routes)) {
22
+ const meta = route.meta;
23
+ const home = typeof meta === "object" && meta !== null && "home" in meta
24
+ ? meta.home
25
+ : undefined;
26
+ if (home === undefined)
27
+ continue;
28
+ if (typeof home !== "string") {
29
+ throw new Error(`route "${name}" has a meta.home that is not a class name`);
30
+ }
31
+ const params = paramsOf(route.url);
32
+ if (params.length !== 1 || params[0] !== "uri") {
33
+ throw new Error(`route "${name}" is home to "${home}" but must take exactly one param, :uri (${route.url})`);
34
+ }
35
+ const owner = homes.get(home);
36
+ if (owner !== undefined) {
37
+ throw new Error(`class "${home}" is claimed by both "${owner}" and "${name}"`);
38
+ }
39
+ homes.set(home, name);
40
+ }
41
+ read.set(routes, homes);
42
+ return homes;
43
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,34 @@
1
+ import { useRouter } from "@canonical/router-react";
2
+ import { useContext } from "react";
3
+ import { useFragment } from "react-relay";
4
+ import { prismClassesFragment, } from "../graphql/index.js";
5
+ import { PrismContext } from "../Prism/index.js";
6
+ import findNearest from "./findNearest.js";
7
+ import orderClasses from "./orderClasses.js";
8
+ import readHomes from "./readHomes.js";
9
+ /**
10
+ * Find, along a thing's classes, the site's replacement for one projection
11
+ * and the route that shows the thing. Both come from the nearest class that
12
+ * has one, among the classes the graph serves with the thing.
13
+ */
14
+ export default function useProjection(node, key) {
15
+ const { uri, _meta } = useFragment(prismClassesFragment, node);
16
+ const { overrides } = useContext(PrismContext);
17
+ const router = useRouter();
18
+ const homes = readHomes(router.routes);
19
+ const chain = orderClasses(_meta.type);
20
+ // The route table names each class once; the graph is what knows that an
21
+ // IRI and a compact name are the same class.
22
+ for (const { uri: classUri, curie } of chain) {
23
+ const byIri = homes.get(classUri);
24
+ const byName = homes.get(curie);
25
+ if (byIri !== undefined && byName !== undefined && byIri !== byName) {
26
+ throw new Error(`class "${curie}" is claimed by both "${byName}" and "${byIri}", once by its IRI`);
27
+ }
28
+ }
29
+ return {
30
+ uri,
31
+ Override: findNearest(chain, (name) => overrides[name]?.[key]),
32
+ home: findNearest(chain, (name) => homes.get(name)),
33
+ };
34
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * A thing's slots with a label that can name it: the graph's label when it
3
+ * has any text, else the thing's compact name, else its IRI. A link or a
4
+ * heading with no text has no accessible name.
5
+ */
6
+ export default function withLabel(slots, curie, uri) {
7
+ const label = slots.label.trim() || curie.trim() || uri;
8
+ return label === slots.label ? slots : { ...slots, label };
9
+ }
@@ -0,0 +1 @@
1
+ export { default as renderPage } from "./renderPage.js";
@@ -0,0 +1,125 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { extractPreferences } from "@canonical/react-hooks";
3
+ import { JSXRenderer, } from "@canonical/react-ssr/renderer";
4
+ import { getRequestUrl } from "@canonical/react-ssr/server";
5
+ import { createRouter, resolveRoute, warmRoute } from "@canonical/router-core";
6
+ import { checkLenses } from "../lens/index.js";
7
+ /**
8
+ * How long a page may take, from the request to the end of its stream:
9
+ * fetching the routes' data and rendering share this one budget.
10
+ */
11
+ const DEADLINE_MS = 5_000;
12
+ /** The methods a page answers; any other gets a 405. */
13
+ const ALLOWED_METHODS = "GET, HEAD";
14
+ /**
15
+ * Pages render per request, with the reader's stored theme: a browser may
16
+ * keep one but must ask again, and no shared cache may keep it.
17
+ */
18
+ const CACHE_CONTROL = "private, no-cache";
19
+ /**
20
+ * The whole document around the site. `<html>` carries the stored theme, so
21
+ * the first paint does not flash; React hydrates only `#root`, so the
22
+ * browser's theme switch on `<html>` never mismatches.
23
+ */
24
+ function Document({ lang, otherHeadElements, scriptElements, linkElements, theme, shell, }) {
25
+ return (_jsxs("html", { className: theme, lang: lang, children: [_jsxs("head", { children: [otherHeadElements, scriptElements, linkElements] }), _jsx("body", { children: _jsx("div", { id: "root", children: shell }) })] }));
26
+ }
27
+ /**
28
+ * Render the page for a request, as one step of the site's server: decide
29
+ * the redirect or the status from the route table, wait for the matched
30
+ * routes' `warm`, then stream the document with the router's state and the
31
+ * site's `data` embedded for the browser entry.
32
+ *
33
+ * One deadline bounds the whole answer. When the routes' `warm` has not
34
+ * settled by then, the page fails closed with a 503: a `warm` still running
35
+ * may be a guard about to redirect, and the page it guards must not render.
36
+ * When the deadline passes while the page streams, the stream is cut.
37
+ *
38
+ * @returns The redirect, the 405 for a method other than GET and HEAD, the
39
+ * 503 for a deadline missed while fetching, or the streamed document with
40
+ * its status: 404 for an address no route matches, 400 for one that does
41
+ * not decode.
42
+ * @throws The request's abort reason when the client has gone before the
43
+ * page renders, for the server to drop.
44
+ */
45
+ export default async function renderPage(request, { routes, notFound, middleware, Shell, context, data, html, }) {
46
+ if (request.method !== "GET" && request.method !== "HEAD") {
47
+ return new Response("Method not allowed", {
48
+ status: 405,
49
+ headers: { allow: ALLOWED_METHODS },
50
+ });
51
+ }
52
+ const url = getRequestUrl(request);
53
+ const deadline = AbortSignal.timeout(DEADLINE_MS);
54
+ const signal = AbortSignal.any([request.signal, deadline]);
55
+ // No adapter: one would start loading the address, and running every
56
+ // `warm`, as soon as the router exists.
57
+ const router = createRouter(routes, { notFound, middleware, context });
58
+ // A mistake in the lenses' meta fails the first request, not a render.
59
+ checkLenses(routes);
60
+ const answer = await warmRoute(resolveRoute(router, url), {
61
+ signal,
62
+ context,
63
+ onError: (error) => {
64
+ console.warn(`Data for ${url} failed; rendering without it.`, error);
65
+ },
66
+ });
67
+ request.signal.throwIfAborted();
68
+ if (answer.kind === "redirect") {
69
+ return new Response(null, {
70
+ status: answer.status,
71
+ headers: { location: answer.location },
72
+ });
73
+ }
74
+ if (deadline.aborted) {
75
+ return new Response("Service unavailable", {
76
+ status: 503,
77
+ headers: { "retry-after": "5" },
78
+ });
79
+ }
80
+ // An address that does not decode, or a search its route rejects, matches
81
+ // nothing: with a not-found route, the page shows it, with the status the
82
+ // answer decided.
83
+ const state = answer.state ??
84
+ (notFound
85
+ ? {
86
+ href: notFound.url,
87
+ kind: "not-found",
88
+ routeId: null,
89
+ status: answer.status,
90
+ }
91
+ : undefined);
92
+ if (state)
93
+ router.hydrate(state);
94
+ const headers = {
95
+ "content-type": "text/html; charset=utf-8",
96
+ "cache-control": CACHE_CONTROL,
97
+ };
98
+ // HEAD gets GET's status and headers, decided by the same `warm`, without
99
+ // rendering a body nobody will read.
100
+ if (request.method === "HEAD") {
101
+ return new Response(null, { status: answer.status, headers });
102
+ }
103
+ const { theme } = extractPreferences(request.headers.get("cookie"));
104
+ const page = {
105
+ router: state ? { ...state } : undefined,
106
+ data: data(),
107
+ };
108
+ const shell = _jsx(Shell, { context: context, router: router });
109
+ const knownTheme = theme === "light" || theme === "dark" ? theme : undefined;
110
+ const renderer = new JSXRenderer(({ lang, otherHeadElements, scriptElements, linkElements, }) => (_jsx(Document, { lang: lang, linkElements: linkElements, otherHeadElements: otherHeadElements, scriptElements: scriptElements, shell: shell, theme: knownTheme })), page, { htmlString: html, statusCode: answer.status });
111
+ const stream = await renderer.renderToReadableStream(signal);
112
+ // The deadline passed while the shell rendered: the renderer answered its
113
+ // error page, but the page is late, not broken.
114
+ if (deadline.aborted && renderer.statusCode === 500) {
115
+ await stream.cancel();
116
+ return new Response("Service unavailable", {
117
+ status: 503,
118
+ headers: { "retry-after": "5" },
119
+ });
120
+ }
121
+ return new Response(stream, {
122
+ status: renderer.statusCode,
123
+ headers: { ...headers, "content-type": renderer.contentType },
124
+ });
125
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The lenses whose label contains every word of `query`, ignoring case, in
3
+ * the order given. An empty query keeps them all.
4
+ */
5
+ export default function filterLenses(lenses, query) {
6
+ const words = query.toLocaleLowerCase().split(/\s+/).filter(Boolean);
7
+ return lenses.filter(({ label }) => {
8
+ const text = label.toLocaleLowerCase();
9
+ return words.every((word) => text.includes(word));
10
+ });
11
+ }
@@ -0,0 +1,3 @@
1
+ export { default as filterLenses } from "./filterLenses.js";
2
+ export { default as searchVariables, SEARCH_LIMIT } from "./searchVariables.js";
3
+ export { default as warmSearch } from "./warmSearch.js";
@@ -0,0 +1,11 @@
1
+ /** How many things one search shows, in the palette and on the search page. */
2
+ export const SEARCH_LIMIT = 20;
3
+ /**
4
+ * The search query's variables for what the reader typed, the same for the
5
+ * palette, the search page and a route's `warm`, so all three share one
6
+ * answer in the store. `undefined` when there is nothing to search for.
7
+ */
8
+ export default function searchVariables(query) {
9
+ const text = query.trim();
10
+ return text ? { text, first: SEARCH_LIMIT } : undefined;
11
+ }
@@ -0,0 +1,14 @@
1
+ import { prismSearchQuery, } from "../searchQuery/index.js";
2
+ import { warmQuery } from "../warmQuery/index.js";
3
+ import searchVariables from "./searchVariables.js";
4
+ /**
5
+ * Fetch the things matching a query into a Relay environment ahead of the
6
+ * search page: the body of the search route's `warm`, for a site whose
7
+ * search page finds things in the graph. It does nothing for an empty query.
8
+ */
9
+ export default function warmSearch(environment, query, options) {
10
+ const variables = searchVariables(query);
11
+ return variables
12
+ ? warmQuery(environment, prismSearchQuery, variables, options)
13
+ : Promise.resolve();
14
+ }
@@ -0,0 +1,23 @@
1
+ import { graphql } from "react-relay";
2
+ import node from "./__generated__/PrismSearchQuery.graphql.js";
3
+ /**
4
+ * The things whose text matches the reader's words, each drawn as a row. It
5
+ * selects the graph's `search` field, which the contract does not have yet,
6
+ * so it sits apart from the projections' fragments: only a site that turns
7
+ * `graph` on compiles it.
8
+ */
9
+ export const prismSearchQuery = node;
10
+ // Read by relay-compiler only; see ../graphql/PrismClasses.ts.
11
+ void (() => graphql `
12
+ query PrismSearchQuery($text: String!, $first: Int!) {
13
+ search(text: $text, first: $first) {
14
+ edges {
15
+ node {
16
+ uri
17
+ ...PrismClasses_node
18
+ ...PrismRow_node
19
+ }
20
+ }
21
+ }
22
+ }
23
+ `);