drupal-canvas 0.6.0 → 0.7.1
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.
- package/README.md +177 -45
- package/dist/context-Cb4HbPbR.d.ts +32 -0
- package/dist/drupal-utils-CUaK4jOk.d.ts +117 -0
- package/dist/drupal-utils.d.ts +2 -79
- package/dist/drupal-utils.js +23 -0
- package/dist/index.d.ts +101 -87
- package/dist/index.js +68 -134
- package/dist/json-render-utils.d.ts +3 -3
- package/dist/jsonapi-client-BAQ3V5CC.d.ts +329 -0
- package/dist/jsonapi-client-CejP62bM.js +539 -0
- package/dist/jsonapi-client.d.ts +2 -2
- package/dist/jsonapi-client.js +2 -17
- package/dist/migration-CPBzW78_.js +63 -0
- package/dist/react.d.ts +25 -0
- package/dist/react.js +158 -0
- package/package.json +11 -1
- package/dist/jsonapi-client-Q84JvzCD.d.ts +0 -13
package/README.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
Utilities and base components for building Drupal Canvas Code Components.
|
|
4
4
|
|
|
5
|
+
React context hooks/providers are exported from `drupal-canvas/react`, together
|
|
6
|
+
with `CanvasContextProviderProps` and `JsonApiClientProviderProps`. Context data
|
|
7
|
+
and client types remain at the root. Existing components (`FormattedText`,
|
|
8
|
+
`Image`, `Region`, `RegionsProvider`), utilities and legacy subpaths are
|
|
9
|
+
unchanged; authoring helpers remain on `drupal-canvas/json-render-utils`.
|
|
10
|
+
|
|
5
11
|
## Utilities
|
|
6
12
|
|
|
7
13
|
### `cn`
|
|
@@ -33,37 +39,72 @@ const ControlDots = ({ className }) => (
|
|
|
33
39
|
);
|
|
34
40
|
```
|
|
35
41
|
|
|
36
|
-
### `
|
|
42
|
+
### `usePageContext` and `useSiteContext`
|
|
37
43
|
|
|
38
|
-
|
|
44
|
+
Read the current page and site data in a React Code Component. Both hooks work
|
|
45
|
+
in Drupal-rendered Code Components, Canvas Workbench previews, and React-based
|
|
46
|
+
headless frontends: the rendering integration establishes the provider, and the
|
|
47
|
+
hooks read it synchronously. They return `null`, with one actionable console
|
|
48
|
+
warning, when no provider is mounted or the integration supplied no such data.
|
|
39
49
|
|
|
40
|
-
```
|
|
41
|
-
import {
|
|
50
|
+
```jsx
|
|
51
|
+
import { usePageContext, useSiteContext } from 'drupal-canvas/react';
|
|
42
52
|
|
|
43
|
-
|
|
44
|
-
const
|
|
53
|
+
export default function PageHeader() {
|
|
54
|
+
const page = usePageContext();
|
|
55
|
+
const site = useSiteContext();
|
|
56
|
+
|
|
57
|
+
if (!page || !site) return null;
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<header>
|
|
61
|
+
<a href={site.branding.homeUrl}>{site.branding.siteName}</a>
|
|
62
|
+
<h1>{page.pageTitle}</h1>
|
|
63
|
+
</header>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
45
66
|
```
|
|
46
67
|
|
|
47
|
-
|
|
68
|
+
- Page context (`PageContext`): `pageTitle`, `breadcrumbs`, and `mainEntity`
|
|
69
|
+
(the primary Drupal entity, `null` on routes without one), including language
|
|
70
|
+
and translation information. See
|
|
71
|
+
[Main entity metadata](https://project.pages.drupalcode.org/canvas/code-components/data-fetching#main-entity-metadata).
|
|
72
|
+
- Site context (`SiteContext`): `branding`, the Drupal `baseUrl`, and
|
|
73
|
+
`themeAssets`. Headless frontends receive empty theme asset URLs; that is
|
|
74
|
+
valid site context, so shared components must handle empty asset URLs.
|
|
48
75
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
are on with `getPageData`. This can be used to construct JSON:API parameters for
|
|
52
|
-
requests. `mainEntity.translations` lists every enabled site language
|
|
53
|
-
(`langcode`, `name`, `nativeName`, `url`, `translationAvailable`, `current`) for
|
|
54
|
-
building a language switcher, alongside `requestedLanguage` and
|
|
55
|
-
`renderedLanguage`.
|
|
56
|
-
[View documentation and example here.](https://project.pages.drupalcode.org/canvas/code-components/data-fetching#main-entity-metadata)
|
|
76
|
+
Call the hooks unconditionally at the top level of a function component or
|
|
77
|
+
custom hook (React's rules of hooks) and handle `null` results.
|
|
57
78
|
|
|
58
|
-
|
|
79
|
+
To reach components outside a Canvas tree, such as a site header in a headless
|
|
80
|
+
application, wrap them in `CanvasContextProvider`:
|
|
59
81
|
|
|
60
|
-
|
|
82
|
+
```jsx
|
|
83
|
+
import { CanvasContextProvider } from 'drupal-canvas/react';
|
|
84
|
+
|
|
85
|
+
<CanvasContextProvider context={page.context}>
|
|
86
|
+
<SiteHeader />
|
|
87
|
+
</CanvasContextProvider>;
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The `CanvasContext` type describes the provider's value:
|
|
91
|
+
`{ page: PageContext | null; site: SiteContext | null }`.
|
|
92
|
+
|
|
93
|
+
### `getPageData` and `getSiteData` (deprecated)
|
|
94
|
+
|
|
95
|
+
`getPageData()` and `getSiteData()` are deprecated in favor of
|
|
96
|
+
`usePageContext()` and `useSiteContext()`. They keep working in Drupal-rendered
|
|
97
|
+
Code Components and Canvas Workbench previews, where they read `drupalSettings`,
|
|
98
|
+
and they still report their data to the code editor's "Component data" panel.
|
|
99
|
+
Outside those environments they throw an error that names the replacement API.
|
|
100
|
+
Components used in both frontend modes must migrate to the hooks; `canvas pull`
|
|
101
|
+
migrates safe calls automatically (see the Canvas CLI).
|
|
61
102
|
|
|
62
103
|
```js
|
|
63
|
-
import { getSiteData } from 'drupal-canvas';
|
|
104
|
+
import { getPageData, getSiteData } from 'drupal-canvas';
|
|
64
105
|
|
|
106
|
+
const { pageTitle, breadcrumbs, mainEntity } = getPageData();
|
|
65
107
|
const { baseUrl, branding } = getSiteData();
|
|
66
|
-
const { homeUrl, siteName, siteSlogan } = branding;
|
|
67
108
|
```
|
|
68
109
|
|
|
69
110
|
### `sortLinksetMenu`
|
|
@@ -111,38 +152,33 @@ const { data } = useSWR(['menu_items', 'main'], ([type, resourceId]) =>
|
|
|
111
152
|
const menu = sortMenu(data);
|
|
112
153
|
```
|
|
113
154
|
|
|
114
|
-
### `
|
|
155
|
+
### `useJsonApiClient`
|
|
115
156
|
|
|
157
|
+
Read a configured
|
|
116
158
|
[JSON:API client](https://www.npmjs.com/package/@drupal-api-client/json-api-client)
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
[Drupal core's JSON:API module](https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module)
|
|
122
|
-
must be enabled to use this client.
|
|
159
|
+
from context. The Drupal island renderer, the headless React renderer, and both
|
|
160
|
+
Workbench preview paths provide it; the hook never fetches data or creates a
|
|
161
|
+
client on render. Use it with SWR or another fetching library:
|
|
123
162
|
|
|
124
163
|
```jsx
|
|
125
|
-
import {
|
|
164
|
+
import { useJsonApiClient } from 'drupal-canvas/react';
|
|
126
165
|
import { DrupalJsonApiParams } from 'drupal-jsonapi-params';
|
|
127
166
|
import useSWR from 'swr';
|
|
128
167
|
|
|
129
|
-
const client = new JsonApiClient();
|
|
130
|
-
|
|
131
168
|
export default function List() {
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
},
|
|
140
|
-
],
|
|
141
|
-
([type, options]) => client.getCollection(type, options),
|
|
169
|
+
const client = useJsonApiClient();
|
|
170
|
+
const { data, error } = useSWR(client ? 'articles' : null, () =>
|
|
171
|
+
client.getCollection('node--article', {
|
|
172
|
+
queryString: new DrupalJsonApiParams()
|
|
173
|
+
.addInclude(['field_tags'])
|
|
174
|
+
.getQueryString(),
|
|
175
|
+
}),
|
|
142
176
|
);
|
|
143
177
|
|
|
144
178
|
if (error) return 'An error has occurred.';
|
|
145
|
-
|
|
179
|
+
// Test for data, not `isLoading`: with prefetched SWR fallback data the
|
|
180
|
+
// data is present while SWR still reports loading during revalidation.
|
|
181
|
+
if (!data) return 'Loading...';
|
|
146
182
|
return (
|
|
147
183
|
<ul>
|
|
148
184
|
{data.map((article) => (
|
|
@@ -153,6 +189,93 @@ export default function List() {
|
|
|
153
189
|
}
|
|
154
190
|
```
|
|
155
191
|
|
|
192
|
+
The hook returns `null`, with one console warning, when no provider is mounted.
|
|
193
|
+
In Drupal previews the client reads working copies (the `rel:working-copy`
|
|
194
|
+
resource version) through the editor's session; in headless browsers it reaches
|
|
195
|
+
Drupal through the application's same-origin proxy, authenticated from the draft
|
|
196
|
+
preview session. Replacing the provided client does not clear SWR caches.
|
|
197
|
+
|
|
198
|
+
For portable components, use this hook rather than Drupal globals or a client
|
|
199
|
+
with hardcoded URLs or credentials. Drupal, Workbench, and headless React
|
|
200
|
+
integrations supply the client; the hook does not detect the environment. See
|
|
201
|
+
[Writing portable components](../../docs/user/src/content/docs/code-components/data-fetching.mdx#writing-portable-components)
|
|
202
|
+
for an SWR example and server-rendering guidance.
|
|
203
|
+
|
|
204
|
+
[Drupal core's JSON:API module](https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module)
|
|
205
|
+
must be enabled.
|
|
206
|
+
|
|
207
|
+
Provide a client explicitly for components outside a Canvas tree with
|
|
208
|
+
`JsonApiClientProvider`:
|
|
209
|
+
|
|
210
|
+
```jsx
|
|
211
|
+
import { JsonApiClientProvider } from 'drupal-canvas/react';
|
|
212
|
+
|
|
213
|
+
<JsonApiClientProvider client={client}>
|
|
214
|
+
<SiteHeader />
|
|
215
|
+
</JsonApiClientProvider>;
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### `createJsonApiClient` (`drupal-canvas/jsonapi-client`)
|
|
219
|
+
|
|
220
|
+
The shared, framework-agnostic client implementation rendering integrations and
|
|
221
|
+
the Canvas Headless SDK build on. It extends
|
|
222
|
+
`@drupal-api-client/json-api-client` with `DefaultSerializer`, draft reads at a
|
|
223
|
+
configured resource version (collection items are hydrated with their working
|
|
224
|
+
copies before serialization; raw responses bypass this), mapping of browser
|
|
225
|
+
requests to a same-origin proxy (including absolute pagination links and the
|
|
226
|
+
Decoupled Router endpoint), `DraftSessionError` for rejected preview sessions
|
|
227
|
+
(the proxy's session error, or a 401 answered to a direct request that carried
|
|
228
|
+
the session credentials), and caches separated by resource version and session
|
|
229
|
+
scope: a client that may carry a session (authentication, cookies — explicit or
|
|
230
|
+
a browser's same-origin default — the proxy, a custom `fetch` transport, a
|
|
231
|
+
preview or a resource version) caches only with an explicit `cacheScope`, so the
|
|
232
|
+
legacy client and any transport-injected client share no cache without a
|
|
233
|
+
caller-provided scope. A 401 is a rejected session when the request carried the
|
|
234
|
+
configured credentials (the Authorization header, cookies, or a transport
|
|
235
|
+
declared with `fetchAuthenticates`); `disableAuthentication` opts a request out,
|
|
236
|
+
and failing to obtain or renew credentials is a rejected session too. Draft
|
|
237
|
+
collection reads hydrate each item with its working copy, keeping the read's
|
|
238
|
+
locale, sparse fieldsets, includes, and an explicitly selected resource version;
|
|
239
|
+
the working copies' included resources are merged into the document, and a
|
|
240
|
+
resource that is itself primary is never duplicated in `included`, so
|
|
241
|
+
relationships resolve to the selected working copy. URLs keep the backend's site
|
|
242
|
+
path (`https://host/sub/fr/jsonapi/...`, also for the Decoupled Router and index
|
|
243
|
+
lookups); `apiUrl` under `baseUrl` is a prefix override, and `apiUrl` on another
|
|
244
|
+
site is a foreign JSON:API base: with `apiSiteUrl` (that site's base URL,
|
|
245
|
+
install path included) a locale prefix goes between them
|
|
246
|
+
(`https://api.example/mount/fr/api`), without it no locale prefix applies; the
|
|
247
|
+
Decoupled Router stays under `baseUrl` either way.
|
|
248
|
+
|
|
249
|
+
```js
|
|
250
|
+
import { createJsonApiClient } from 'drupal-canvas/jsonapi-client';
|
|
251
|
+
|
|
252
|
+
const client = createJsonApiClient({
|
|
253
|
+
baseUrl: 'https://drupal.example',
|
|
254
|
+
apiPrefix: 'jsonapi',
|
|
255
|
+
// Browser clients in headless apps go through the app's proxy.
|
|
256
|
+
proxyUrl: '/api/canvas/jsonapi',
|
|
257
|
+
resourceVersion: 'rel:working-copy',
|
|
258
|
+
preview: true,
|
|
259
|
+
});
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
`DefaultSerializer` and `createCache` are re-exported from
|
|
263
|
+
`drupal-canvas/jsonapi-client` unchanged.
|
|
264
|
+
|
|
265
|
+
### `JsonApiClient` (deprecated)
|
|
266
|
+
|
|
267
|
+
`new JsonApiClient()` is deprecated in favor of `useJsonApiClient()` in React
|
|
268
|
+
Code Components and the Headless SDK's `getClient()` in headless server code. It
|
|
269
|
+
keeps working in Drupal-rendered Code Components and Canvas Workbench previews,
|
|
270
|
+
where it is configured from `drupalSettings`, and throws an error naming the
|
|
271
|
+
replacement APIs elsewhere, even when a base URL is supplied.
|
|
272
|
+
|
|
273
|
+
```jsx
|
|
274
|
+
import { JsonApiClient } from 'drupal-canvas';
|
|
275
|
+
|
|
276
|
+
const client = new JsonApiClient();
|
|
277
|
+
```
|
|
278
|
+
|
|
156
279
|
You can override the `baseUrl` and any default options:
|
|
157
280
|
|
|
158
281
|
```js
|
|
@@ -162,10 +285,15 @@ const client = new JsonApiClient('https://drupal-api-demo.party', {
|
|
|
162
285
|
});
|
|
163
286
|
```
|
|
164
287
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
288
|
+
### Migrating from the deprecated APIs
|
|
289
|
+
|
|
290
|
+
Replace `getPageData()`, `getSiteData()`, and `new JsonApiClient()` with
|
|
291
|
+
`usePageContext()`, `useSiteContext()`, and `useJsonApiClient()` in function
|
|
292
|
+
components or custom hooks. Call hooks unconditionally at the top level before
|
|
293
|
+
any possible return; handle missing context or clients. Outside components and
|
|
294
|
+
custom hooks, use the Headless SDK's page context data or `getClient()` in
|
|
295
|
+
headless server code; pass data or a client to browser helpers. Preserve output,
|
|
296
|
+
types, hook order, and access controls. Never expose credentials.
|
|
169
297
|
|
|
170
298
|
### json-render Utils
|
|
171
299
|
|
|
@@ -384,7 +512,11 @@ process, an SVG image for example. Such an image is rendered as-is, without a
|
|
|
384
512
|
`srcset`. An SVG image that specifies neither its dimensions nor a `viewBox` is
|
|
385
513
|
rendered without `width` and `height` too, and is then sized by the browser.
|
|
386
514
|
|
|
387
|
-
### Region / RegionsProvider
|
|
515
|
+
### Region / RegionsProvider (deprecated)
|
|
516
|
+
|
|
517
|
+
Deprecated: theme-global regions were replaced by page variants, which compose a
|
|
518
|
+
page from a single component tree. Both components keep working for
|
|
519
|
+
compatibility, but no new region integration is added.
|
|
388
520
|
|
|
389
521
|
Render Drupal Canvas global regions inside a layout component.
|
|
390
522
|
`<Region name="..." />` slots in the region whose machine name matches `name`,
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { d as PageContext, f as SiteContext, u as CanvasContext } from "./drupal-utils-CUaK4jOk.js";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
//#region src/context.d.ts
|
|
4
|
+
interface CanvasContextProviderProps {
|
|
5
|
+
context: CanvasContext;
|
|
6
|
+
children?: ReactNode;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Makes page and site context available to every React descendant. Adds no
|
|
10
|
+
* DOM wrapper. Rendering integrations use it internally; applications can use
|
|
11
|
+
* it to reach components outside the Canvas tree, such as a site header.
|
|
12
|
+
*/
|
|
13
|
+
declare function CanvasContextProvider({ context, children }: CanvasContextProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
14
|
+
/**
|
|
15
|
+
* Returns the current page context, or `null` when no provider supplies one.
|
|
16
|
+
* Emits a deduplicated developer warning in that case; a provider that
|
|
17
|
+
* carries valid empty page data is not a missing-context condition.
|
|
18
|
+
*/
|
|
19
|
+
declare function usePageContext(): PageContext | null;
|
|
20
|
+
/**
|
|
21
|
+
* Returns the current site context, or `null` when no provider supplies one.
|
|
22
|
+
* Emits a deduplicated developer warning in that case.
|
|
23
|
+
*/
|
|
24
|
+
declare function useSiteContext(): SiteContext | null;
|
|
25
|
+
/**
|
|
26
|
+
* Whether a `CanvasContextProvider` is mounted above the calling component.
|
|
27
|
+
* Rendering integrations use it to decide whether to inherit an outer
|
|
28
|
+
* provider instead of adding their own.
|
|
29
|
+
*/
|
|
30
|
+
declare function useHasCanvasContext(): boolean;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { useSiteContext as a, usePageContext as i, CanvasContextProviderProps as n, useHasCanvasContext as r, CanvasContextProvider as t };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
//#region src/context-types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The page context: the same shape `getPageData()` returns. The primary
|
|
4
|
+
* entity can be `null` on routes without one.
|
|
5
|
+
*/
|
|
6
|
+
type PageContext = PageData;
|
|
7
|
+
/**
|
|
8
|
+
* The site context: the same shape `getSiteData()` returns. Headless
|
|
9
|
+
* frontends receive empty theme asset URLs; that is valid site context, not
|
|
10
|
+
* missing context.
|
|
11
|
+
*/
|
|
12
|
+
type SiteContext = SiteData;
|
|
13
|
+
/** The context a rendering integration supplies to a component tree. */
|
|
14
|
+
interface CanvasContext {
|
|
15
|
+
page: PageContext | null;
|
|
16
|
+
site: SiteContext | null;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/drupal-utils.d.ts
|
|
20
|
+
interface Linkset {
|
|
21
|
+
linkset: LinksetItem[];
|
|
22
|
+
}
|
|
23
|
+
interface LinksetItem {
|
|
24
|
+
anchor: string;
|
|
25
|
+
item: LinksetMenuItem[];
|
|
26
|
+
}
|
|
27
|
+
interface LinksetMenuItem {
|
|
28
|
+
href: string;
|
|
29
|
+
hierarchy: string[];
|
|
30
|
+
_children?: LinksetMenuItem[];
|
|
31
|
+
_hasSubmenu: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Sort menu items from core's menu API into a tree with additional
|
|
35
|
+
* _children and _hasSubmenu properties.
|
|
36
|
+
*
|
|
37
|
+
* @param linkset
|
|
38
|
+
*
|
|
39
|
+
* @return menuItems
|
|
40
|
+
*/
|
|
41
|
+
declare function sortMenu(linkset: Linkset): LinksetMenuItem[];
|
|
42
|
+
interface BreadcrumbLink {
|
|
43
|
+
key: string;
|
|
44
|
+
text: string;
|
|
45
|
+
url: string;
|
|
46
|
+
}
|
|
47
|
+
interface EntityMetadata {
|
|
48
|
+
bundle: string;
|
|
49
|
+
entityTypeId: string;
|
|
50
|
+
uuid: string;
|
|
51
|
+
requestedLanguage: string;
|
|
52
|
+
renderedLanguage: string;
|
|
53
|
+
/**
|
|
54
|
+
* All enabled site languages for building a language switcher.
|
|
55
|
+
*
|
|
56
|
+
* Includes default and untranslated languages
|
|
57
|
+
* (`translationAvailable: false`). Untranslated entries still include a URL
|
|
58
|
+
* that resolves to the fallback translation. Empty when the entity type does
|
|
59
|
+
* not support translation or the site is monolingual.
|
|
60
|
+
*/
|
|
61
|
+
translations: TranslationMetadata[];
|
|
62
|
+
}
|
|
63
|
+
interface TranslationMetadata {
|
|
64
|
+
langcode: string;
|
|
65
|
+
name: string;
|
|
66
|
+
nativeName: string;
|
|
67
|
+
url: string;
|
|
68
|
+
translationAvailable: boolean;
|
|
69
|
+
current: boolean;
|
|
70
|
+
}
|
|
71
|
+
interface PageData {
|
|
72
|
+
pageTitle: string;
|
|
73
|
+
breadcrumbs: Array<BreadcrumbLink>;
|
|
74
|
+
mainEntity: EntityMetadata | null;
|
|
75
|
+
}
|
|
76
|
+
interface ThemeAssets {
|
|
77
|
+
logo: {
|
|
78
|
+
url: string;
|
|
79
|
+
};
|
|
80
|
+
favicon: {
|
|
81
|
+
url: string;
|
|
82
|
+
mimeType: string;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
interface SiteData {
|
|
86
|
+
branding: {
|
|
87
|
+
homeUrl: string;
|
|
88
|
+
siteName: string;
|
|
89
|
+
siteSlogan: string;
|
|
90
|
+
};
|
|
91
|
+
baseUrl: string;
|
|
92
|
+
themeAssets: ThemeAssets;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Reads the current page data from `drupalSettings`.
|
|
96
|
+
*
|
|
97
|
+
* Supported in Drupal-rendered Code Components and Canvas Workbench previews
|
|
98
|
+
* only; elsewhere it throws an actionable migration error.
|
|
99
|
+
*
|
|
100
|
+
* @deprecated Use `usePageContext()` in React Code Components, or read
|
|
101
|
+
* `page.context.page` from the Headless SDK's page response outside
|
|
102
|
+
* components.
|
|
103
|
+
*/
|
|
104
|
+
declare const getPageData: () => PageData;
|
|
105
|
+
/**
|
|
106
|
+
* Reads the site data from `drupalSettings`.
|
|
107
|
+
*
|
|
108
|
+
* Supported in Drupal-rendered Code Components and Canvas Workbench previews
|
|
109
|
+
* only; elsewhere it throws an actionable migration error.
|
|
110
|
+
*
|
|
111
|
+
* @deprecated Use `useSiteContext()` in React Code Components, or read
|
|
112
|
+
* `page.context.site` from the Headless SDK's page response outside
|
|
113
|
+
* components.
|
|
114
|
+
*/
|
|
115
|
+
declare const getSiteData: () => SiteData;
|
|
116
|
+
//#endregion
|
|
117
|
+
export { ThemeAssets as a, getSiteData as c, PageContext as d, SiteContext as f, SiteData as i, sortMenu as l, EntityMetadata as n, TranslationMetadata as o, PageData as r, getPageData as s, BreadcrumbLink as t, CanvasContext as u };
|
package/dist/drupal-utils.d.ts
CHANGED
|
@@ -1,79 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
linkset: LinksetItem[];
|
|
4
|
-
}
|
|
5
|
-
interface LinksetItem {
|
|
6
|
-
anchor: string;
|
|
7
|
-
item: LinksetMenuItem[];
|
|
8
|
-
}
|
|
9
|
-
interface LinksetMenuItem {
|
|
10
|
-
href: string;
|
|
11
|
-
hierarchy: string[];
|
|
12
|
-
_children?: LinksetMenuItem[];
|
|
13
|
-
_hasSubmenu: boolean;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Sort menu items from core's menu API into a tree with additional
|
|
17
|
-
* _children and _hasSubmenu properties.
|
|
18
|
-
*
|
|
19
|
-
* @param linkset
|
|
20
|
-
*
|
|
21
|
-
* @return menuItems
|
|
22
|
-
*/
|
|
23
|
-
declare function sortMenu(linkset: Linkset): LinksetMenuItem[];
|
|
24
|
-
interface BreadcrumbLink {
|
|
25
|
-
key: string;
|
|
26
|
-
text: string;
|
|
27
|
-
url: string;
|
|
28
|
-
}
|
|
29
|
-
interface EntityMetadata {
|
|
30
|
-
bundle: string;
|
|
31
|
-
entityTypeId: string;
|
|
32
|
-
uuid: string;
|
|
33
|
-
requestedLanguage: string;
|
|
34
|
-
renderedLanguage: string;
|
|
35
|
-
/**
|
|
36
|
-
* All enabled site languages for building a language switcher.
|
|
37
|
-
*
|
|
38
|
-
* Includes default and untranslated languages
|
|
39
|
-
* (`translationAvailable: false`). Untranslated entries still include a URL
|
|
40
|
-
* that resolves to the fallback translation. Empty when the entity type does
|
|
41
|
-
* not support translation or the site is monolingual.
|
|
42
|
-
*/
|
|
43
|
-
translations: TranslationMetadata[];
|
|
44
|
-
}
|
|
45
|
-
interface TranslationMetadata {
|
|
46
|
-
langcode: string;
|
|
47
|
-
name: string;
|
|
48
|
-
nativeName: string;
|
|
49
|
-
url: string;
|
|
50
|
-
translationAvailable: boolean;
|
|
51
|
-
current: boolean;
|
|
52
|
-
}
|
|
53
|
-
interface PageData {
|
|
54
|
-
pageTitle: string;
|
|
55
|
-
breadcrumbs: Array<BreadcrumbLink>;
|
|
56
|
-
mainEntity: EntityMetadata | null;
|
|
57
|
-
}
|
|
58
|
-
interface ThemeAssets {
|
|
59
|
-
logo: {
|
|
60
|
-
url: string;
|
|
61
|
-
};
|
|
62
|
-
favicon: {
|
|
63
|
-
url: string;
|
|
64
|
-
mimeType: string;
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
interface SiteData {
|
|
68
|
-
branding: {
|
|
69
|
-
homeUrl: string;
|
|
70
|
-
siteName: string;
|
|
71
|
-
siteSlogan: string;
|
|
72
|
-
};
|
|
73
|
-
baseUrl: string;
|
|
74
|
-
themeAssets: ThemeAssets;
|
|
75
|
-
}
|
|
76
|
-
declare const getPageData: () => PageData;
|
|
77
|
-
declare const getSiteData: () => SiteData;
|
|
78
|
-
//#endregion
|
|
79
|
-
export { getPageData, getSiteData, sortMenu };
|
|
1
|
+
import { a as ThemeAssets, c as getSiteData, d as PageContext, f as SiteContext, i as SiteData, l as sortMenu, n as EntityMetadata, o as TranslationMetadata, r as PageData, s as getPageData, t as BreadcrumbLink, u as CanvasContext } from "./drupal-utils-CUaK4jOk.js";
|
|
2
|
+
export { BreadcrumbLink, type CanvasContext, EntityMetadata, type PageContext, PageData, type SiteContext, SiteData, ThemeAssets, TranslationMetadata, getPageData, getSiteData, sortMenu };
|
package/dist/drupal-utils.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { r as assertLegacyRuntime } from "./migration-CPBzW78_.js";
|
|
1
2
|
//#region src/drupal-utils.ts
|
|
2
3
|
/**
|
|
3
4
|
* Sort menu items from core's menu API into a tree with additional
|
|
@@ -35,7 +36,18 @@ function sortMenu(linkset) {
|
|
|
35
36
|
});
|
|
36
37
|
return menu;
|
|
37
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Reads the current page data from `drupalSettings`.
|
|
41
|
+
*
|
|
42
|
+
* Supported in Drupal-rendered Code Components and Canvas Workbench previews
|
|
43
|
+
* only; elsewhere it throws an actionable migration error.
|
|
44
|
+
*
|
|
45
|
+
* @deprecated Use `usePageContext()` in React Code Components, or read
|
|
46
|
+
* `page.context.page` from the Headless SDK's page response outside
|
|
47
|
+
* components.
|
|
48
|
+
*/
|
|
38
49
|
const getPageData = () => {
|
|
50
|
+
assertLegacyRuntime("getPageData()");
|
|
39
51
|
const pageData = {
|
|
40
52
|
pageTitle: globalThis?.drupalSettings?.canvasData?.v0?.pageTitle || "",
|
|
41
53
|
breadcrumbs: globalThis?.drupalSettings?.canvasData?.v0?.breadcrumbs || [],
|
|
@@ -48,7 +60,18 @@ const getPageData = () => {
|
|
|
48
60
|
});
|
|
49
61
|
return pageData;
|
|
50
62
|
};
|
|
63
|
+
/**
|
|
64
|
+
* Reads the site data from `drupalSettings`.
|
|
65
|
+
*
|
|
66
|
+
* Supported in Drupal-rendered Code Components and Canvas Workbench previews
|
|
67
|
+
* only; elsewhere it throws an actionable migration error.
|
|
68
|
+
*
|
|
69
|
+
* @deprecated Use `useSiteContext()` in React Code Components, or read
|
|
70
|
+
* `page.context.site` from the Headless SDK's page response outside
|
|
71
|
+
* components.
|
|
72
|
+
*/
|
|
51
73
|
const getSiteData = () => {
|
|
74
|
+
assertLegacyRuntime("getSiteData()");
|
|
52
75
|
const siteData = {
|
|
53
76
|
branding: globalThis?.drupalSettings?.canvasData?.v0?.branding || {
|
|
54
77
|
homeUrl: "",
|