drupal-canvas 0.1.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.
- package/README.md +213 -0
- package/dist/FormattedText-B8Lu3ByU.js +12 -0
- package/dist/FormattedText-CcQ03VEY.d.ts +16 -0
- package/dist/FormattedText.d.ts +2 -0
- package/dist/FormattedText.js +3 -0
- package/dist/drupal-utils-CqLfxx9F.d.ts +44 -0
- package/dist/drupal-utils-Dfvj3HlO.js +68 -0
- package/dist/drupal-utils.d.ts +2 -0
- package/dist/drupal-utils.js +3 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/jsonapi-client-BamGkTjn.d.ts +9 -0
- package/dist/jsonapi-client-jLXdE88j.js +20 -0
- package/dist/jsonapi-client.d.ts +3 -0
- package/dist/jsonapi-client.js +5 -0
- package/dist/jsonapi-utils-co82I5eL.d.ts +33 -0
- package/dist/jsonapi-utils-oywSkEdN.js +43 -0
- package/dist/jsonapi-utils.d.ts +2 -0
- package/dist/jsonapi-utils.js +3 -0
- package/dist/next-image-standalone-B4uHVOA8.js +41 -0
- package/dist/next-image-standalone-CHJW9_qx.d.ts +8 -0
- package/dist/next-image-standalone.d.ts +2 -0
- package/dist/next-image-standalone.js +3 -0
- package/dist/utils-Cn7jrjo0.d.ts +6 -0
- package/dist/utils-DG-hPO0N.js +10 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +3 -0
- package/package.json +70 -0
package/README.md
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# Drupal Canvas Code Components Utils
|
|
2
|
+
|
|
3
|
+
Utilities and base components for building Drupal Canvas Code Components.
|
|
4
|
+
|
|
5
|
+
## Utilities
|
|
6
|
+
|
|
7
|
+
### `cn`
|
|
8
|
+
|
|
9
|
+
Helper for combining Tailwind CSS classes using
|
|
10
|
+
[`clsx`](https://www.npmjs.com/package/clsx) and
|
|
11
|
+
[`tailwind-merge`](https://www.npmjs.com/package/tailwind-merge). Implementation
|
|
12
|
+
[borrowed from shadcn/ui](https://ui.shadcn.com/docs/installation/manual#add-a-cn-helper).
|
|
13
|
+
|
|
14
|
+
```jsx
|
|
15
|
+
import { cn } from 'drupal-canvas';
|
|
16
|
+
|
|
17
|
+
export default function Example() {
|
|
18
|
+
return <ControlDots className="absolute top-4 left-4 stroke-white" />;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ControlDots = ({ className }) => (
|
|
22
|
+
<svg
|
|
23
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
24
|
+
viewBox="0 0 31 9"
|
|
25
|
+
fill="none"
|
|
26
|
+
strokeWidth="2"
|
|
27
|
+
className={cn('w-12', className)}
|
|
28
|
+
>
|
|
29
|
+
<ellipse cx="4.13" cy="4.97" rx="3.13" ry="2.97" />
|
|
30
|
+
<ellipse cx="15.16" cy="4.97" rx="3.13" ry="2.97" />
|
|
31
|
+
<ellipse cx="26.19" cy="4.97" rx="3.13" ry="2.97" />
|
|
32
|
+
</svg>
|
|
33
|
+
);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### `getPageData`
|
|
37
|
+
|
|
38
|
+
Access information about the current page.
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
import { getPageData } from 'drupal-canvas';
|
|
42
|
+
|
|
43
|
+
const { pageTitle, breadcrumbs } = getPageData();
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### `getSiteData`
|
|
47
|
+
|
|
48
|
+
Access information about the site.
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
import { getSiteData } from 'drupal-canvas';
|
|
52
|
+
|
|
53
|
+
const { baseUrl, branding } = getSiteData();
|
|
54
|
+
const { homeUrl, siteName, siteSlogan } = branding;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### `sortLinksetMenu`
|
|
58
|
+
|
|
59
|
+
Sort a menu linkset returned by
|
|
60
|
+
[Drupal core's linkset endpoint](https://www.drupal.org/docs/develop/decoupled-drupal/decoupled-menus/decoupled-menus-overview):
|
|
61
|
+
|
|
62
|
+
```jsx
|
|
63
|
+
import { sortLinksetMenu } from 'drupal-canvas';
|
|
64
|
+
|
|
65
|
+
const { data } = useSWR('/system/menu/main/linkset', async (url) => {
|
|
66
|
+
const response = await fetch(url);
|
|
67
|
+
return response.json();
|
|
68
|
+
});
|
|
69
|
+
const menu = sortLinksetMenu(data);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### `getNodePath`
|
|
73
|
+
|
|
74
|
+
Given a node returned from `JSON:API`, return either the path alias or fall back
|
|
75
|
+
to the `/node/[nid]` path.
|
|
76
|
+
|
|
77
|
+
```jsx
|
|
78
|
+
import { getNodePath } from 'drupal-canvas';
|
|
79
|
+
|
|
80
|
+
const articles = data.map((article) => ({
|
|
81
|
+
...article,
|
|
82
|
+
_path: getNodePath(article),
|
|
83
|
+
}));
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### `sortMenu`
|
|
87
|
+
|
|
88
|
+
Sort menu items from the
|
|
89
|
+
[JSON:API Menu Items](https://www.drupal.org/project/jsonapi_menu_items) module
|
|
90
|
+
into a tree with additional `_children` and `_hasSubmenu` properties.
|
|
91
|
+
|
|
92
|
+
```jsx
|
|
93
|
+
import { JsonApiClient, sortMenu } from 'drupal-canvas';
|
|
94
|
+
|
|
95
|
+
const client = new JsonApiClient();
|
|
96
|
+
const { data } = useSWR(['menu_items', 'main'], ([type, resourceId]) =>
|
|
97
|
+
client.getResource(type, resourceId),
|
|
98
|
+
);
|
|
99
|
+
const menu = sortMenu(data);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### `JsonApiClient`
|
|
103
|
+
|
|
104
|
+
[JSON:API client](https://www.npmjs.com/package/@drupal-api-client/json-api-client)
|
|
105
|
+
automatically configured with a `baseUrl` as well as
|
|
106
|
+
[Jsona](https://www.npmjs.com/package/jsona) for
|
|
107
|
+
[deserialization](https://project.pages.drupalcode.org/api_client/jsonapi-tutorial/deserializing-data/).
|
|
108
|
+
|
|
109
|
+
[Drupal core's JSON:API module](https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module)
|
|
110
|
+
must be enabled to use this client.
|
|
111
|
+
|
|
112
|
+
```jsx
|
|
113
|
+
import { JsonApiClient } from 'drupal-canvas';
|
|
114
|
+
import { DrupalJsonApiParams } from 'drupal-jsonapi-params';
|
|
115
|
+
import useSWR from 'swr';
|
|
116
|
+
|
|
117
|
+
const client = new JsonApiClient();
|
|
118
|
+
|
|
119
|
+
export default function List() {
|
|
120
|
+
const { data, error, isLoading } = useSWR(
|
|
121
|
+
[
|
|
122
|
+
'node--article',
|
|
123
|
+
{
|
|
124
|
+
queryString: new DrupalJsonApiParams()
|
|
125
|
+
.addInclude(['field_tags'])
|
|
126
|
+
.getQueryString(),
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
([type, options]) => client.getCollection(type, options),
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
if (error) return 'An error has occurred.';
|
|
133
|
+
if (isLoading) return 'Loading...';
|
|
134
|
+
return (
|
|
135
|
+
<ul>
|
|
136
|
+
{data.map((article) => (
|
|
137
|
+
<li key={article.id}>{article.title}</li>
|
|
138
|
+
))}
|
|
139
|
+
</ul>
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
You can override the `baseUrl` and any default options:
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
const client = new JsonApiClient('https://drupal-api-demo.party', {
|
|
148
|
+
serializer: undefined,
|
|
149
|
+
cache: undefined,
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
If working outside of Drupal Canvas, you can use the
|
|
154
|
+
[`@drupal-canvas/vite-plugin`](https://www.npmjs.com/package/@drupal-api-client/json-api-client)
|
|
155
|
+
to automatically configure the base URL for you. Otherwise you must explicitly
|
|
156
|
+
provide a base URL.
|
|
157
|
+
|
|
158
|
+
## Base Components
|
|
159
|
+
|
|
160
|
+
### FormattedText
|
|
161
|
+
|
|
162
|
+
A built-in component to render text with trusted HTML using
|
|
163
|
+
[`dangerouslySetInnerHTML`](https://react.dev/reference/react-dom/components/common#dangerously-setting-the-inner-html).
|
|
164
|
+
The content is safe when processed through Drupal's filter system that is
|
|
165
|
+
[correctly configured](https://www.drupal.org/docs/administering-a-drupal-site/security-in-drupal/configuring-text-formats-aka-input-formats-for-security).
|
|
166
|
+
|
|
167
|
+
```jsx
|
|
168
|
+
import { FormattedText } from 'drupal-canvas';
|
|
169
|
+
|
|
170
|
+
export default function Example() {
|
|
171
|
+
return (
|
|
172
|
+
<FormattedText>
|
|
173
|
+
<em>Hello, world!</em>
|
|
174
|
+
</FormattedText>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Image
|
|
180
|
+
|
|
181
|
+
A built-in component for automatic image optimization, responsive behavior, and
|
|
182
|
+
modern loading techniques for code components.
|
|
183
|
+
|
|
184
|
+
The `Image` component is a wrapper around the
|
|
185
|
+
[next-image-standalone](https://www.npmjs.com/package/next-image-standalone)
|
|
186
|
+
library, preconfigured with a loader to work with the zero-config dynamic image
|
|
187
|
+
style in Drupal Canvas.
|
|
188
|
+
|
|
189
|
+
```jsx
|
|
190
|
+
import { Image } from 'drupal-canvas';
|
|
191
|
+
|
|
192
|
+
export default function MyComponent({ photo }) {
|
|
193
|
+
return (
|
|
194
|
+
<Image
|
|
195
|
+
src={photo.src}
|
|
196
|
+
alt={photo.alt}
|
|
197
|
+
width={photo.width}
|
|
198
|
+
height={photo.height}
|
|
199
|
+
/>
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Development
|
|
205
|
+
|
|
206
|
+
The following scripts are available for developing this package:
|
|
207
|
+
|
|
208
|
+
| Command | Description |
|
|
209
|
+
| ------------ | ------------------------------------------------------------------------ |
|
|
210
|
+
| `build` | Compile to the `dist` folder for production use. |
|
|
211
|
+
| `dev` | Compile to the `dist` folder for development while watching for changes. |
|
|
212
|
+
| `type-check` | Run TypeScript type checking without emitting files. |
|
|
213
|
+
| `test` | Run tests. |
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
|
|
3
|
+
//#region src/FormattedText.tsx
|
|
4
|
+
function FormattedText({ children, as = "div", ...props }) {
|
|
5
|
+
return /* @__PURE__ */ jsx(as, {
|
|
6
|
+
dangerouslySetInnerHTML: { __html: children },
|
|
7
|
+
...props
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
export { FormattedText as t };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/FormattedText.d.ts
|
|
2
|
+
type FormattedTextProps = {
|
|
3
|
+
children: string;
|
|
4
|
+
as?: 'div' | 'span';
|
|
5
|
+
className?: string;
|
|
6
|
+
id?: string;
|
|
7
|
+
style?: Record<string, string>;
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
};
|
|
10
|
+
declare function FormattedText({
|
|
11
|
+
children,
|
|
12
|
+
as,
|
|
13
|
+
...props
|
|
14
|
+
}: FormattedTextProps): any;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { FormattedText as t };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//#region src/drupal-utils.d.ts
|
|
2
|
+
interface Linkset {
|
|
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 PageData {
|
|
30
|
+
pageTitle: string;
|
|
31
|
+
breadcrumbs: Array<BreadcrumbLink>;
|
|
32
|
+
}
|
|
33
|
+
interface SiteData {
|
|
34
|
+
branding: {
|
|
35
|
+
homeUrl: string;
|
|
36
|
+
siteName: string;
|
|
37
|
+
siteSlogan: string;
|
|
38
|
+
};
|
|
39
|
+
baseUrl: string;
|
|
40
|
+
}
|
|
41
|
+
declare const getPageData: () => PageData;
|
|
42
|
+
declare const getSiteData: () => SiteData;
|
|
43
|
+
//#endregion
|
|
44
|
+
export { getSiteData as n, sortMenu as r, getPageData as t };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
//#region src/drupal-utils.ts
|
|
2
|
+
/**
|
|
3
|
+
* Sort menu items from core's menu API into a tree with additional
|
|
4
|
+
* _children and _hasSubmenu properties.
|
|
5
|
+
*
|
|
6
|
+
* @param linkset
|
|
7
|
+
*
|
|
8
|
+
* @return menuItems
|
|
9
|
+
*/
|
|
10
|
+
function sortMenu(linkset) {
|
|
11
|
+
const menuItemsMap = /* @__PURE__ */ new Map();
|
|
12
|
+
const menu = [];
|
|
13
|
+
if (!linkset.linkset?.length) return [];
|
|
14
|
+
linkset.linkset[0].item.forEach((item) => {
|
|
15
|
+
const hierarchyKey = item.hierarchy.join("|");
|
|
16
|
+
menuItemsMap.set(hierarchyKey, {
|
|
17
|
+
...item,
|
|
18
|
+
id: hierarchyKey,
|
|
19
|
+
_children: [],
|
|
20
|
+
_hasSubmenu: false
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
linkset.linkset[0].item.forEach((item) => {
|
|
24
|
+
const hierarchyKey = item.hierarchy.join("|");
|
|
25
|
+
const currentItem = menuItemsMap.get(hierarchyKey);
|
|
26
|
+
if (item.hierarchy.length === 1) menu.push(currentItem);
|
|
27
|
+
else {
|
|
28
|
+
const parentKey = item.hierarchy.slice(0, -1).join("|");
|
|
29
|
+
const parent = menuItemsMap.get(parentKey);
|
|
30
|
+
if (parent) {
|
|
31
|
+
parent._children.push(currentItem);
|
|
32
|
+
parent._hasSubmenu = true;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return menu;
|
|
37
|
+
}
|
|
38
|
+
const getPageData = () => {
|
|
39
|
+
const pageData = {
|
|
40
|
+
pageTitle: window.drupalSettings?.canvasData?.v0?.pageTitle || "",
|
|
41
|
+
breadcrumbs: window.drupalSettings?.canvasData?.v0?.breadcrumbs || []
|
|
42
|
+
};
|
|
43
|
+
window.parent.postMessage({
|
|
44
|
+
type: "_canvas_useswr_data_fetch",
|
|
45
|
+
id: "getPageData()",
|
|
46
|
+
data: pageData
|
|
47
|
+
});
|
|
48
|
+
return pageData;
|
|
49
|
+
};
|
|
50
|
+
const getSiteData = () => {
|
|
51
|
+
const siteData = {
|
|
52
|
+
branding: window.drupalSettings?.canvasData?.v0?.branding || {
|
|
53
|
+
homeUrl: "",
|
|
54
|
+
siteName: "",
|
|
55
|
+
siteSlogan: ""
|
|
56
|
+
},
|
|
57
|
+
baseUrl: window.drupalSettings?.canvasData?.v0?.baseUrl || "/"
|
|
58
|
+
};
|
|
59
|
+
window.parent.postMessage({
|
|
60
|
+
type: "_canvas_useswr_data_fetch",
|
|
61
|
+
id: "getSiteData()",
|
|
62
|
+
data: siteData
|
|
63
|
+
});
|
|
64
|
+
return siteData;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
//#endregion
|
|
68
|
+
export { getSiteData as n, sortMenu as r, getPageData as t };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { t as FormattedText } from "./FormattedText-CcQ03VEY.js";
|
|
2
|
+
import { n as getSiteData, r as sortMenu, t as getPageData } from "./drupal-utils-CqLfxx9F.js";
|
|
3
|
+
import { t as CanvasJsonApiClient } from "./jsonapi-client-BamGkTjn.js";
|
|
4
|
+
import { n as sortMenu$1, t as getNodePath } from "./jsonapi-utils-co82I5eL.js";
|
|
5
|
+
import { t as Image } from "./next-image-standalone-CHJW9_qx.js";
|
|
6
|
+
import { t as cn } from "./utils-Cn7jrjo0.js";
|
|
7
|
+
export { FormattedText, Image, CanvasJsonApiClient as JsonApiClient, cn, getNodePath, getPageData, getSiteData, sortMenu as sortLinksetMenu, sortMenu$1 as sortMenu };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { n as getSiteData, r as sortMenu, t as getPageData } from "./drupal-utils-Dfvj3HlO.js";
|
|
2
|
+
import { t as FormattedText } from "./FormattedText-B8Lu3ByU.js";
|
|
3
|
+
import { t as CanvasJsonApiClient } from "./jsonapi-client-jLXdE88j.js";
|
|
4
|
+
import { n as sortMenu$1, t as getNodePath } from "./jsonapi-utils-oywSkEdN.js";
|
|
5
|
+
import { t as Image } from "./next-image-standalone-B4uHVOA8.js";
|
|
6
|
+
import { t as cn } from "./utils-DG-hPO0N.js";
|
|
7
|
+
|
|
8
|
+
export { FormattedText, Image, CanvasJsonApiClient as JsonApiClient, cn, getNodePath, getPageData, getSiteData, sortMenu as sortLinksetMenu, sortMenu$1 as sortMenu };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { JsonApiClient, JsonApiClientOptions } from "@drupal-api-client/json-api-client";
|
|
2
|
+
import { BaseUrl } from "@drupal-api-client/api-client";
|
|
3
|
+
|
|
4
|
+
//#region src/jsonapi-client.d.ts
|
|
5
|
+
declare class CanvasJsonApiClient extends JsonApiClient {
|
|
6
|
+
constructor(baseUrl?: BaseUrl, options?: JsonApiClientOptions);
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { CanvasJsonApiClient as t };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Jsona } from "jsona";
|
|
2
|
+
import { JsonApiClient } from "@drupal-api-client/json-api-client";
|
|
3
|
+
|
|
4
|
+
//#region src/jsonapi-client.ts
|
|
5
|
+
var CanvasJsonApiClient = class extends JsonApiClient {
|
|
6
|
+
constructor(baseUrl, options) {
|
|
7
|
+
if (window.drupalSettings?.canvasData?.v0?.jsonapiSettings === null) throw new Error("The JSON:API module is not installed. Please install it to use JsonApiClient.");
|
|
8
|
+
const clientBaseUrl = baseUrl || window.drupalSettings?.canvasData?.v0?.baseUrl;
|
|
9
|
+
if (!baseUrl && !clientBaseUrl) throw new Error("Could not determine your site's base URL for the JSON:API client. If working outside of Drupal Canvas, you can use the @drupal-canvas/vite-plugin to automatically configure it for you. Otherwise you must explicitly provide a base URL, i.e. `const client = new JsonApiClient(\"https://...\")`");
|
|
10
|
+
const clientOptions = {
|
|
11
|
+
apiPrefix: window.drupalSettings?.canvasData?.v0?.jsonapiSettings?.apiPrefix,
|
|
12
|
+
serializer: new Jsona(),
|
|
13
|
+
...options
|
|
14
|
+
};
|
|
15
|
+
super(clientBaseUrl, clientOptions);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { CanvasJsonApiClient as t };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//#region src/jsonapi-utils.d.ts
|
|
2
|
+
interface Node {
|
|
3
|
+
path?: {
|
|
4
|
+
alias?: string;
|
|
5
|
+
};
|
|
6
|
+
drupal_internal__nid?: number;
|
|
7
|
+
}
|
|
8
|
+
interface MenuItem {
|
|
9
|
+
id: string | number;
|
|
10
|
+
parent?: string | number;
|
|
11
|
+
_children?: MenuItem[];
|
|
12
|
+
_hasSubmenu: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Given a node returned from JSON:API, return either the path alias or fall back
|
|
16
|
+
* to /node/x path.
|
|
17
|
+
*
|
|
18
|
+
* @param node
|
|
19
|
+
*
|
|
20
|
+
* @return string
|
|
21
|
+
*/
|
|
22
|
+
declare function getNodePath(node: Node): string;
|
|
23
|
+
/**
|
|
24
|
+
* Sort menu items from jsonapi_menu_items module into a tree with additional
|
|
25
|
+
* _children and _hasSubmenu properties.
|
|
26
|
+
*
|
|
27
|
+
* @param menuItems
|
|
28
|
+
*
|
|
29
|
+
* @return menuItems
|
|
30
|
+
*/
|
|
31
|
+
declare function sortMenu(menuItems: MenuItem[]): MenuItem[];
|
|
32
|
+
//#endregion
|
|
33
|
+
export { sortMenu as n, getNodePath as t };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//#region src/jsonapi-utils.ts
|
|
2
|
+
/**
|
|
3
|
+
* Given a node returned from JSON:API, return either the path alias or fall back
|
|
4
|
+
* to /node/x path.
|
|
5
|
+
*
|
|
6
|
+
* @param node
|
|
7
|
+
*
|
|
8
|
+
* @return string
|
|
9
|
+
*/
|
|
10
|
+
function getNodePath(node) {
|
|
11
|
+
return node.path?.alias || (node.drupal_internal__nid ? `/node/${node.drupal_internal__nid}` : "#");
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Sort menu items from jsonapi_menu_items module into a tree with additional
|
|
15
|
+
* _children and _hasSubmenu properties.
|
|
16
|
+
*
|
|
17
|
+
* @param menuItems
|
|
18
|
+
*
|
|
19
|
+
* @return menuItems
|
|
20
|
+
*/
|
|
21
|
+
function sortMenu(menuItems) {
|
|
22
|
+
const menuItemsMap = /* @__PURE__ */ new Map();
|
|
23
|
+
const menu = [];
|
|
24
|
+
menuItems.forEach((menuItem) => {
|
|
25
|
+
menuItemsMap.set(menuItem.id, {
|
|
26
|
+
...menuItem,
|
|
27
|
+
_children: [],
|
|
28
|
+
_hasSubmenu: false
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
menuItems.forEach((menuItem) => {
|
|
32
|
+
const node = menuItemsMap.get(menuItem.id);
|
|
33
|
+
if (menuItem.parent && menuItemsMap.has(menuItem.parent)) {
|
|
34
|
+
const parent = menuItemsMap.get(menuItem.parent);
|
|
35
|
+
parent._children.push(node);
|
|
36
|
+
parent._hasSubmenu = true;
|
|
37
|
+
} else menu.push(node);
|
|
38
|
+
});
|
|
39
|
+
return menu;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
export { sortMenu as n, getNodePath as t };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import NextImage from "next-image-standalone";
|
|
3
|
+
|
|
4
|
+
//#region src/next-image-standalone.tsx
|
|
5
|
+
function Image(props) {
|
|
6
|
+
const { src, loader } = props;
|
|
7
|
+
if (!loader) {
|
|
8
|
+
const defaultLoader = ({ width, imageProps }) => {
|
|
9
|
+
try {
|
|
10
|
+
let result = new URL(src, "https://example.com").searchParams.get("alternateWidths");
|
|
11
|
+
if (!result) throw new Error("Responsive image generation requires an `alternateWidths` query parameter in the image URL.");
|
|
12
|
+
result = result.replace("{width}", width.toString());
|
|
13
|
+
if (result.includes("{height}")) {
|
|
14
|
+
const { width: intrinsicWidth, height: intrinsicHeight } = imageProps;
|
|
15
|
+
const height = Math.round(width / (intrinsicWidth / intrinsicHeight));
|
|
16
|
+
result = result.replace("{height}", height.toString());
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
} catch (error) {
|
|
20
|
+
console.error("Responsive image generation failed. To fix this:\n1. Provide a custom `loader` function, or\n2. Ensure your image `src` includes an `alternateWidths` query parameter\n Example: ?alternateWidths=/path/to/responsive/{width}/image.jpg", {
|
|
21
|
+
src,
|
|
22
|
+
error
|
|
23
|
+
});
|
|
24
|
+
return src;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
return /* @__PURE__ */ jsx(NextImage, {
|
|
28
|
+
...props,
|
|
29
|
+
loader: defaultLoader,
|
|
30
|
+
sizes: props.sizes || "auto 100vw"
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return /* @__PURE__ */ jsx(NextImage, {
|
|
34
|
+
...props,
|
|
35
|
+
loader,
|
|
36
|
+
sizes: props.sizes || "auto 100vw"
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
//#endregion
|
|
41
|
+
export { Image as t };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ImageLoaderParams, ImageProps } from "next-image-standalone";
|
|
2
|
+
|
|
3
|
+
//#region src/next-image-standalone.d.ts
|
|
4
|
+
declare function Image(props: Omit<ImageProps, 'loader'> & {
|
|
5
|
+
loader?: (params: ImageLoaderParams) => string;
|
|
6
|
+
}): any;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { Image as t };
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "drupal-canvas",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Utilities and base components for building Drupal Canvas Code Components",
|
|
5
|
+
"license": "GPL-2.0-or-later",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://git.drupalcode.org/project/canvas.git",
|
|
9
|
+
"directory": "packages/drupal-canvas"
|
|
10
|
+
},
|
|
11
|
+
"bugs": "https://www.drupal.org/project/issues/canvas",
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
},
|
|
18
|
+
"./FormattedText": {
|
|
19
|
+
"import": "./dist/FormattedText.js",
|
|
20
|
+
"types": "./dist/FormattedText.d.ts"
|
|
21
|
+
},
|
|
22
|
+
"./drupal-utils": {
|
|
23
|
+
"import": "./dist/drupal-utils.js",
|
|
24
|
+
"types": "./dist/drupal-utils.d.ts"
|
|
25
|
+
},
|
|
26
|
+
"./jsonapi-client": {
|
|
27
|
+
"import": "./dist/jsonapi-client.js",
|
|
28
|
+
"types": "./dist/jsonapi-client.d.ts"
|
|
29
|
+
},
|
|
30
|
+
"./jsonapi-utils": {
|
|
31
|
+
"import": "./dist/jsonapi-utils.js",
|
|
32
|
+
"types": "./dist/jsonapi-utils.d.ts"
|
|
33
|
+
},
|
|
34
|
+
"./next-image-standalone": {
|
|
35
|
+
"import": "./dist/next-image-standalone.js",
|
|
36
|
+
"types": "./dist/next-image-standalone.d.ts"
|
|
37
|
+
},
|
|
38
|
+
"./utils": {
|
|
39
|
+
"import": "./dist/utils.js",
|
|
40
|
+
"types": "./dist/utils.d.ts"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"type": "module",
|
|
44
|
+
"files": [
|
|
45
|
+
"dist",
|
|
46
|
+
"README.md"
|
|
47
|
+
],
|
|
48
|
+
"keywords": [
|
|
49
|
+
"canvas",
|
|
50
|
+
"drupal"
|
|
51
|
+
],
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@drupal-api-client/json-api-client": "^1.3.0",
|
|
54
|
+
"clsx": "^2.1.1",
|
|
55
|
+
"jsona": "^1.12.1",
|
|
56
|
+
"next-image-standalone": "^0.1.3",
|
|
57
|
+
"tailwind-merge": "^3.0.2"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"tsdown": "^0.17.0-beta.5",
|
|
61
|
+
"typescript": "^5.9.3",
|
|
62
|
+
"vitest": "^3.2.3"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"build": "NODE_ENV=production tsdown",
|
|
66
|
+
"dev": "NODE_ENV=development tsdown --watch",
|
|
67
|
+
"type-check": "tsc --noEmit --pretty",
|
|
68
|
+
"test": "vitest run"
|
|
69
|
+
}
|
|
70
|
+
}
|