drupal-canvas 0.5.2 → 0.7.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 +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 +209 -3
- package/dist/index.js +220 -2
- package/dist/json-render-utils.d.ts +3 -3
- package/dist/json-render-utils.js +11 -1
- 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/dist/index.d.ts
CHANGED
|
@@ -1,21 +1,227 @@
|
|
|
1
1
|
import FormattedText from "./FormattedText.js";
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
2
|
+
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";
|
|
3
|
+
import "./context-Cb4HbPbR.js";
|
|
4
|
+
import { a as LegacyJsonApiClient, i as JsonApiRuntimeConfig, n as DraftSessionError, o as createJsonApiClient, r as JsonApiClientConfig, s as isDraftSessionError, t as CanvasJsonApiClient } from "./jsonapi-client-BAQ3V5CC.js";
|
|
4
5
|
import { getNodePath, sortMenu as sortMenu$1 } from "./jsonapi-utils.js";
|
|
5
6
|
import Image from "./next-image-standalone.js";
|
|
6
7
|
import { cn } from "./utils.js";
|
|
7
8
|
import { ReactNode } from "react";
|
|
9
|
+
//#region src/date-utils.d.ts
|
|
10
|
+
/**
|
|
11
|
+
* Locale-aware date formatting utilities for Canvas Code Components.
|
|
12
|
+
*
|
|
13
|
+
* These functions format ISO date/time strings using the Drupal-selected
|
|
14
|
+
* langcode from `drupalSettings.canvasData.v0.langcode`. They are opt-in: Canvas passes date
|
|
15
|
+
* props to templates as raw ISO strings by default. Use these utilities when
|
|
16
|
+
* you want to display a date in the user's locale.
|
|
17
|
+
*
|
|
18
|
+
* All functions accept an optional `options` object so callers can override
|
|
19
|
+
* default format styles without losing the Drupal langcode or the UTC timezone
|
|
20
|
+
* guard that prevents stored dates from shifting by the viewer's UTC offset.
|
|
21
|
+
*
|
|
22
|
+
* @example Twig: `{{ my_date_prop|canvasFormatDate }}`
|
|
23
|
+
* @example JSX: `canvasFormatDate(myDateProp)`
|
|
24
|
+
* @example JSX with options: `canvasFormatDate(myDateProp, { dateStyle: 'long' })`
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Options accepted by the Canvas date formatting utilities.
|
|
28
|
+
*
|
|
29
|
+
* All fields are optional and default to `'short'`. Pass an options object to
|
|
30
|
+
* override the format style without losing the Drupal langcode or UTC timezone
|
|
31
|
+
* guard.
|
|
32
|
+
*/
|
|
33
|
+
interface CanvasDateFormatOptions {
|
|
34
|
+
/** Controls the date portion style. Defaults to `'short'`. */
|
|
35
|
+
dateStyle?: Intl.DateTimeFormatOptions['dateStyle'];
|
|
36
|
+
/** Controls the time portion style. Defaults to `'short'`. */
|
|
37
|
+
timeStyle?: Intl.DateTimeFormatOptions['timeStyle'];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Formats an ISO date string using the active Drupal locale's date format.
|
|
41
|
+
*
|
|
42
|
+
* Returns the original `iso` string unchanged if it cannot be parsed, and an
|
|
43
|
+
* empty string for `null` or `undefined` (an optional prop without a value).
|
|
44
|
+
*
|
|
45
|
+
* @param iso
|
|
46
|
+
* ISO date string, e.g. '2026-01-15'.
|
|
47
|
+
* @param options
|
|
48
|
+
* Optional format overrides. Defaults to `{ dateStyle: 'short' }`.
|
|
49
|
+
*
|
|
50
|
+
* @return
|
|
51
|
+
* Locale-formatted date string, e.g. '1/15/26' (en) or '15/01/2026' (fr).
|
|
52
|
+
*/
|
|
53
|
+
declare function canvasFormatDate(iso: string | null | undefined, options?: Pick<CanvasDateFormatOptions, 'dateStyle'>): string;
|
|
54
|
+
/**
|
|
55
|
+
* Formats an ISO datetime string using the active Drupal locale's format.
|
|
56
|
+
*
|
|
57
|
+
* Returns the original `iso` string unchanged if it cannot be parsed, and an
|
|
58
|
+
* empty string for `null` or `undefined` (an optional prop without a value).
|
|
59
|
+
*
|
|
60
|
+
* @param iso
|
|
61
|
+
* ISO datetime string, e.g. '2026-01-15T14:30:00Z'.
|
|
62
|
+
* @param options
|
|
63
|
+
* Optional format overrides. Defaults to
|
|
64
|
+
* `{ dateStyle: 'short', timeStyle: 'short' }`.
|
|
65
|
+
*
|
|
66
|
+
* @return
|
|
67
|
+
* Locale-formatted datetime string.
|
|
68
|
+
*/
|
|
69
|
+
declare function canvasFormatDateTime(iso: string | null | undefined, options?: CanvasDateFormatOptions): string;
|
|
70
|
+
/**
|
|
71
|
+
* Formats an ISO time/datetime string using the active Drupal locale's time format.
|
|
72
|
+
*
|
|
73
|
+
* Returns the original `iso` string unchanged if it cannot be parsed, and an
|
|
74
|
+
* empty string for `null` or `undefined` (an optional prop without a value).
|
|
75
|
+
*
|
|
76
|
+
* @param iso
|
|
77
|
+
* ISO time or datetime string, e.g. '14:30:00' or '2026-01-15T14:30:00Z'.
|
|
78
|
+
* @param options
|
|
79
|
+
* Optional format overrides. Defaults to `{ timeStyle: 'short' }`.
|
|
80
|
+
*
|
|
81
|
+
* @return
|
|
82
|
+
* Locale-formatted time string.
|
|
83
|
+
*/
|
|
84
|
+
declare function canvasFormatTime(iso: string | null | undefined, options?: Pick<CanvasDateFormatOptions, 'timeStyle'>): string;
|
|
85
|
+
/**
|
|
86
|
+
* Formats the `from` and `to` fields of a Canvas date-range prop.
|
|
87
|
+
*
|
|
88
|
+
* Returns `null` or `undefined` unchanged (an optional prop without a value).
|
|
89
|
+
*
|
|
90
|
+
* @param range
|
|
91
|
+
* Canvas date-range object, e.g. `{ from: '2026-01-15', to: '2026-03-20' }`.
|
|
92
|
+
* @param options
|
|
93
|
+
* Optional format overrides passed to each field. Defaults to
|
|
94
|
+
* `{ dateStyle: 'short' }`.
|
|
95
|
+
*
|
|
96
|
+
* @return
|
|
97
|
+
* A new object with the same keys; each string date value is replaced with a
|
|
98
|
+
* formatted string.
|
|
99
|
+
*/
|
|
100
|
+
declare function canvasFormatDateRange<T extends {
|
|
101
|
+
from?: string | null;
|
|
102
|
+
to?: string | null;
|
|
103
|
+
} | null | undefined>(range: T, options?: Pick<CanvasDateFormatOptions, 'dateStyle'>): T;
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/drupal-settings.d.ts
|
|
106
|
+
/** The subset of `drupalSettings.canvasData.v0` the context API reads. */
|
|
107
|
+
interface CanvasDataV0 {
|
|
108
|
+
pageTitle?: string | null;
|
|
109
|
+
breadcrumbs?: CanvasContext['page'] extends (infer P) ? P extends {
|
|
110
|
+
breadcrumbs: infer B;
|
|
111
|
+
} ? B | null : never : never;
|
|
112
|
+
mainEntity?: NonNullable<CanvasContext['page']>['mainEntity'];
|
|
113
|
+
branding?: NonNullable<CanvasContext['site']>['branding'] | null;
|
|
114
|
+
baseUrl?: string | null;
|
|
115
|
+
themeAssets?: NonNullable<CanvasContext['site']>['themeAssets'] | null;
|
|
116
|
+
jsonapiSettings?: {
|
|
117
|
+
apiPrefix?: string;
|
|
118
|
+
} | null;
|
|
119
|
+
}
|
|
120
|
+
interface DrupalSettingsLike {
|
|
121
|
+
canvasData?: {
|
|
122
|
+
v0?: CanvasDataV0 | null;
|
|
123
|
+
} | null;
|
|
124
|
+
}
|
|
125
|
+
/** Reads `drupalSettings.canvasData.v0` from a settings object or the global. */
|
|
126
|
+
declare function readCanvasDataV0(settings?: DrupalSettingsLike | undefined): CanvasDataV0 | null;
|
|
127
|
+
/**
|
|
128
|
+
* Builds the page and site context from `drupalSettings.canvasData.v0`.
|
|
129
|
+
*
|
|
130
|
+
* Page context is available when Drupal attached any page-level key
|
|
131
|
+
* (`pageTitle`, `breadcrumbs`, or `mainEntity`); site context when it attached
|
|
132
|
+
* `branding`. Missing keys inside an available group take the same defaults
|
|
133
|
+
* as the legacy getters.
|
|
134
|
+
*/
|
|
135
|
+
declare function drupalSettingsToCanvasContext(v0: CanvasDataV0 | null | undefined): CanvasContext;
|
|
136
|
+
/**
|
|
137
|
+
* Builds the JSON:API runtime configuration for a Drupal-rendered page from
|
|
138
|
+
* `drupalSettings.canvasData.v0`, or `null` when the settings do not carry a
|
|
139
|
+
* base URL or JSON:API is not installed. Previews read working copies through
|
|
140
|
+
* the editor's Drupal session.
|
|
141
|
+
*/
|
|
142
|
+
declare function drupalSettingsToJsonApiRuntimeConfig(v0: CanvasDataV0 | null | undefined, options?: {
|
|
143
|
+
preview?: boolean;
|
|
144
|
+
}): JsonApiRuntimeConfig | null;
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region src/migration.d.ts
|
|
147
|
+
/**
|
|
148
|
+
* @file
|
|
149
|
+
* Migration guidance shared by the legacy API errors, the hook warnings, and
|
|
150
|
+
* the Canvas CLI's pull codemod report.
|
|
151
|
+
*/
|
|
152
|
+
/**
|
|
153
|
+
* The AI-agent migration prompt included in legacy API errors and printed by
|
|
154
|
+
* the Canvas CLI for components it could not migrate automatically.
|
|
155
|
+
*/
|
|
156
|
+
declare const AGENT_MIGRATION_PROMPT: string;
|
|
157
|
+
/** Replacement guidance per legacy API. */
|
|
158
|
+
declare const LEGACY_API_GUIDANCE: {
|
|
159
|
+
readonly 'getPageData()': string;
|
|
160
|
+
readonly 'getSiteData()': string;
|
|
161
|
+
readonly 'new JsonApiClient()': string;
|
|
162
|
+
};
|
|
163
|
+
type LegacyApiName = keyof typeof LEGACY_API_GUIDANCE;
|
|
164
|
+
/**
|
|
165
|
+
* Builds the error message thrown when a legacy API is invoked outside Drupal
|
|
166
|
+
* and Workbench.
|
|
167
|
+
*/
|
|
168
|
+
declare function formatLegacyApiError(api: LegacyApiName): string;
|
|
169
|
+
//#endregion
|
|
8
170
|
//#region src/Region.d.ts
|
|
9
171
|
type RegionsMap = Record<string, ReactNode>;
|
|
10
172
|
type RegionsProviderProps = {
|
|
11
173
|
regions: RegionsMap;
|
|
12
174
|
children: ReactNode;
|
|
13
175
|
};
|
|
176
|
+
/**
|
|
177
|
+
* Supplies the region node map read by `Region`.
|
|
178
|
+
*
|
|
179
|
+
* @deprecated Theme-global regions were replaced by page variants (ADR 19);
|
|
180
|
+
* compose pages with the component tree instead. Kept for compatibility.
|
|
181
|
+
*/
|
|
14
182
|
declare function RegionsProvider({ regions, children }: RegionsProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
15
183
|
type RegionProps = {
|
|
16
184
|
name: string;
|
|
17
185
|
fallback?: ReactNode;
|
|
18
186
|
};
|
|
187
|
+
/**
|
|
188
|
+
* Renders the region whose machine name matches `name`.
|
|
189
|
+
*
|
|
190
|
+
* @deprecated Theme-global regions were replaced by page variants (ADR 19);
|
|
191
|
+
* compose pages with the component tree instead. Kept for compatibility.
|
|
192
|
+
*/
|
|
19
193
|
declare function Region({ name, fallback }: RegionProps): import("react/jsx-runtime").JSX.Element;
|
|
20
194
|
//#endregion
|
|
21
|
-
|
|
195
|
+
//#region src/runtime.d.ts
|
|
196
|
+
/**
|
|
197
|
+
* @file
|
|
198
|
+
* The runtime marker that identifies the environments in which the legacy
|
|
199
|
+
* `getPageData()`, `getSiteData()`, and `new JsonApiClient()` APIs keep
|
|
200
|
+
* working: Drupal-rendered Code Components and Canvas Workbench previews.
|
|
201
|
+
*
|
|
202
|
+
* The marker is established explicitly by those integrations before any Code
|
|
203
|
+
* Component module is evaluated. It is a compatibility check, not a security
|
|
204
|
+
* boundary: the presence of `window`, a backend URL, or a user-created
|
|
205
|
+
* `drupalSettings` object is deliberately not treated as proof of either
|
|
206
|
+
* environment.
|
|
207
|
+
*
|
|
208
|
+
* @see docs/adr/0021-code-component-runtime-compatibility-across-frontend-modes.md
|
|
209
|
+
*/
|
|
210
|
+
/** The environments that support the legacy runtime APIs. */
|
|
211
|
+
type CanvasRuntimeEnvironment = 'drupal' | 'workbench';
|
|
212
|
+
/**
|
|
213
|
+
* Declares the current runtime environment. Called by Drupal and Workbench
|
|
214
|
+
* integrations, never by Code Components or headless applications.
|
|
215
|
+
*/
|
|
216
|
+
declare function declareCanvasRuntime(environment: CanvasRuntimeEnvironment): void;
|
|
217
|
+
/**
|
|
218
|
+
* Returns the declared runtime environment, or `null` outside Drupal and
|
|
219
|
+
* Workbench.
|
|
220
|
+
*/
|
|
221
|
+
declare function getCanvasRuntime(): CanvasRuntimeEnvironment | null;
|
|
222
|
+
/**
|
|
223
|
+
* Whether the legacy runtime APIs are supported in the current environment.
|
|
224
|
+
*/
|
|
225
|
+
declare function isLegacyRuntimeSupported(): boolean;
|
|
226
|
+
//#endregion
|
|
227
|
+
export { AGENT_MIGRATION_PROMPT, type BreadcrumbLink, type CanvasContext, type CanvasDataV0, type CanvasDateFormatOptions, type CanvasJsonApiClient, type CanvasRuntimeEnvironment, DraftSessionError, type EntityMetadata, FormattedText, Image, LegacyJsonApiClient as JsonApiClient, type JsonApiClientConfig, type JsonApiRuntimeConfig, LEGACY_API_GUIDANCE, type PageContext, type PageData, Region, RegionsProvider, type SiteContext, type SiteData, type ThemeAssets, type TranslationMetadata, canvasFormatDate, canvasFormatDateRange, canvasFormatDateTime, canvasFormatTime, cn, createJsonApiClient, declareCanvasRuntime, drupalSettingsToCanvasContext, drupalSettingsToJsonApiRuntimeConfig, formatLegacyApiError, getCanvasRuntime, getNodePath, getPageData, getSiteData, isDraftSessionError, isLegacyRuntimeSupported, readCanvasDataV0, sortMenu as sortLinksetMenu, sortMenu$1 as sortMenu };
|
package/dist/index.js
CHANGED
|
@@ -1,22 +1,240 @@
|
|
|
1
|
+
import { a as declareCanvasRuntime, i as formatLegacyApiError, n as LEGACY_API_GUIDANCE, o as getCanvasRuntime, s as isLegacyRuntimeSupported, t as AGENT_MIGRATION_PROMPT } from "./migration-CPBzW78_.js";
|
|
1
2
|
import { getPageData, getSiteData, sortMenu } from "./drupal-utils.js";
|
|
2
3
|
import FormattedText from "./FormattedText.js";
|
|
3
|
-
import {
|
|
4
|
+
import { a as isDraftSessionError, i as createJsonApiClient, n as DraftSessionError, r as LegacyJsonApiClient } from "./jsonapi-client-CejP62bM.js";
|
|
4
5
|
import { getNodePath, sortMenu as sortMenu$1 } from "./jsonapi-utils.js";
|
|
5
6
|
import Image from "./next-image-standalone.js";
|
|
6
7
|
import { cn } from "./utils.js";
|
|
7
8
|
import { Fragment, jsx } from "react/jsx-runtime";
|
|
8
9
|
import { createContext, useContext } from "react";
|
|
10
|
+
//#region src/date-utils.ts
|
|
11
|
+
/**
|
|
12
|
+
* Reads the Drupal-selected langcode from Canvas runtime settings when present,
|
|
13
|
+
* falling back to `drupalSettings.langcode` and then 'en'.
|
|
14
|
+
*
|
|
15
|
+
* Canvas attaches `canvasData.v0.langcode` for every code component that imports
|
|
16
|
+
* one of these utilities: the import adds `v0.langcode` to the component's
|
|
17
|
+
* `dataDependencies.drupalSettings`. Drupal core only sets
|
|
18
|
+
* `drupalSettings.langcode` on pages with a machine name form element, so that
|
|
19
|
+
* fallback is not a langcode source on rendered pages.
|
|
20
|
+
*
|
|
21
|
+
* @see ui/src/features/code-editor/utils/ast-utils.ts
|
|
22
|
+
*/
|
|
23
|
+
const getDrupalLangcode = () => globalThis.drupalSettings?.canvasData?.v0?.langcode ?? globalThis.drupalSettings?.langcode ?? "en";
|
|
24
|
+
/**
|
|
25
|
+
* Creates an `Intl.DateTimeFormat` instance, falling back to 'en' locale if
|
|
26
|
+
* the Drupal langcode string is not a valid BCP 47 tag.
|
|
27
|
+
*
|
|
28
|
+
* Drupal langcodes like `zh-hans` or `pt-br` use lowercase language tag
|
|
29
|
+
* components which may throw `RangeError` in strict BCP 47 environments.
|
|
30
|
+
* The try/catch ensures graceful degradation.
|
|
31
|
+
*
|
|
32
|
+
* All formatters use `timeZone: 'UTC'` because Canvas date/datetime props are
|
|
33
|
+
* stored without timezone offset. Formatting in the local browser timezone
|
|
34
|
+
* would silently shift dates — e.g. a UTC midnight date would appear as the
|
|
35
|
+
* previous day in negative-UTC-offset timezones (Americas).
|
|
36
|
+
*/
|
|
37
|
+
const createFormatter = (locale, options) => {
|
|
38
|
+
const utcOptions = {
|
|
39
|
+
timeZone: "UTC",
|
|
40
|
+
...options
|
|
41
|
+
};
|
|
42
|
+
try {
|
|
43
|
+
return new Intl.DateTimeFormat(locale, utcOptions);
|
|
44
|
+
} catch {
|
|
45
|
+
return new Intl.DateTimeFormat("en", utcOptions);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Parses an ISO string to a Date object, returning null on failure.
|
|
50
|
+
*
|
|
51
|
+
* ECMAScript parses a date-time string without a UTC offset as local time,
|
|
52
|
+
* whereas date-only strings are parsed as UTC. Canvas treats every stored
|
|
53
|
+
* value as UTC (the PHP side appends `Z` before formatting), so a `Z` is
|
|
54
|
+
* appended to date-time strings that carry no offset. Without it, the UTC
|
|
55
|
+
* formatter would shift the value by the viewer's UTC offset.
|
|
56
|
+
*/
|
|
57
|
+
const parseIso = (iso) => {
|
|
58
|
+
if (!iso) return null;
|
|
59
|
+
const hasTimePart = /[T ]\d{2}:\d{2}/.test(iso);
|
|
60
|
+
const hasOffset = /(Z|[+-]\d{2}(:?\d{2})?)$/i.test(iso);
|
|
61
|
+
const d = new Date(hasTimePart && !hasOffset ? `${iso}Z` : iso);
|
|
62
|
+
return isNaN(d.getTime()) ? null : d;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Formats an ISO date string using the active Drupal locale's date format.
|
|
66
|
+
*
|
|
67
|
+
* Returns the original `iso` string unchanged if it cannot be parsed, and an
|
|
68
|
+
* empty string for `null` or `undefined` (an optional prop without a value).
|
|
69
|
+
*
|
|
70
|
+
* @param iso
|
|
71
|
+
* ISO date string, e.g. '2026-01-15'.
|
|
72
|
+
* @param options
|
|
73
|
+
* Optional format overrides. Defaults to `{ dateStyle: 'short' }`.
|
|
74
|
+
*
|
|
75
|
+
* @return
|
|
76
|
+
* Locale-formatted date string, e.g. '1/15/26' (en) or '15/01/2026' (fr).
|
|
77
|
+
*/
|
|
78
|
+
function canvasFormatDate(iso, options) {
|
|
79
|
+
if (iso == null) return "";
|
|
80
|
+
const date = parseIso(iso);
|
|
81
|
+
if (!date) return iso;
|
|
82
|
+
return createFormatter(getDrupalLangcode(), { dateStyle: options?.dateStyle ?? "short" }).format(date);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Formats an ISO datetime string using the active Drupal locale's format.
|
|
86
|
+
*
|
|
87
|
+
* Returns the original `iso` string unchanged if it cannot be parsed, and an
|
|
88
|
+
* empty string for `null` or `undefined` (an optional prop without a value).
|
|
89
|
+
*
|
|
90
|
+
* @param iso
|
|
91
|
+
* ISO datetime string, e.g. '2026-01-15T14:30:00Z'.
|
|
92
|
+
* @param options
|
|
93
|
+
* Optional format overrides. Defaults to
|
|
94
|
+
* `{ dateStyle: 'short', timeStyle: 'short' }`.
|
|
95
|
+
*
|
|
96
|
+
* @return
|
|
97
|
+
* Locale-formatted datetime string.
|
|
98
|
+
*/
|
|
99
|
+
function canvasFormatDateTime(iso, options) {
|
|
100
|
+
if (iso == null) return "";
|
|
101
|
+
const date = parseIso(iso);
|
|
102
|
+
if (!date) return iso;
|
|
103
|
+
return createFormatter(getDrupalLangcode(), {
|
|
104
|
+
dateStyle: options?.dateStyle ?? "short",
|
|
105
|
+
timeStyle: options?.timeStyle ?? "short"
|
|
106
|
+
}).format(date);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Formats an ISO time/datetime string using the active Drupal locale's time format.
|
|
110
|
+
*
|
|
111
|
+
* Returns the original `iso` string unchanged if it cannot be parsed, and an
|
|
112
|
+
* empty string for `null` or `undefined` (an optional prop without a value).
|
|
113
|
+
*
|
|
114
|
+
* @param iso
|
|
115
|
+
* ISO time or datetime string, e.g. '14:30:00' or '2026-01-15T14:30:00Z'.
|
|
116
|
+
* @param options
|
|
117
|
+
* Optional format overrides. Defaults to `{ timeStyle: 'short' }`.
|
|
118
|
+
*
|
|
119
|
+
* @return
|
|
120
|
+
* Locale-formatted time string.
|
|
121
|
+
*/
|
|
122
|
+
function canvasFormatTime(iso, options) {
|
|
123
|
+
if (iso == null) return "";
|
|
124
|
+
const normalized = /^\d{2}:\d{2}/.test(iso) ? `1970-01-01T${iso}` : iso;
|
|
125
|
+
const date = parseIso(normalized);
|
|
126
|
+
if (!date) return iso;
|
|
127
|
+
return createFormatter(getDrupalLangcode(), { timeStyle: options?.timeStyle ?? "short" }).format(date);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Formats the `from` and `to` fields of a Canvas date-range prop.
|
|
131
|
+
*
|
|
132
|
+
* Returns `null` or `undefined` unchanged (an optional prop without a value).
|
|
133
|
+
*
|
|
134
|
+
* @param range
|
|
135
|
+
* Canvas date-range object, e.g. `{ from: '2026-01-15', to: '2026-03-20' }`.
|
|
136
|
+
* @param options
|
|
137
|
+
* Optional format overrides passed to each field. Defaults to
|
|
138
|
+
* `{ dateStyle: 'short' }`.
|
|
139
|
+
*
|
|
140
|
+
* @return
|
|
141
|
+
* A new object with the same keys; each string date value is replaced with a
|
|
142
|
+
* formatted string.
|
|
143
|
+
*/
|
|
144
|
+
function canvasFormatDateRange(range, options) {
|
|
145
|
+
if (range == null) return range;
|
|
146
|
+
const result = { ...range };
|
|
147
|
+
if (typeof result.from === "string") result.from = canvasFormatDate(result.from, options);
|
|
148
|
+
if (typeof result.to === "string") result.to = canvasFormatDate(result.to, options);
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/drupal-settings.ts
|
|
153
|
+
/** Reads `drupalSettings.canvasData.v0` from a settings object or the global. */
|
|
154
|
+
function readCanvasDataV0(settings = globalThis.drupalSettings) {
|
|
155
|
+
const v0 = settings?.canvasData?.v0;
|
|
156
|
+
return v0 && typeof v0 === "object" ? v0 : null;
|
|
157
|
+
}
|
|
158
|
+
const EMPTY_THEME_ASSETS = {
|
|
159
|
+
logo: { url: "" },
|
|
160
|
+
favicon: {
|
|
161
|
+
url: "",
|
|
162
|
+
mimeType: ""
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Builds the page and site context from `drupalSettings.canvasData.v0`.
|
|
167
|
+
*
|
|
168
|
+
* Page context is available when Drupal attached any page-level key
|
|
169
|
+
* (`pageTitle`, `breadcrumbs`, or `mainEntity`); site context when it attached
|
|
170
|
+
* `branding`. Missing keys inside an available group take the same defaults
|
|
171
|
+
* as the legacy getters.
|
|
172
|
+
*/
|
|
173
|
+
function drupalSettingsToCanvasContext(v0) {
|
|
174
|
+
if (!v0) return {
|
|
175
|
+
page: null,
|
|
176
|
+
site: null
|
|
177
|
+
};
|
|
178
|
+
const hasPage = "pageTitle" in v0 || "breadcrumbs" in v0 || "mainEntity" in v0;
|
|
179
|
+
const hasSite = "branding" in v0;
|
|
180
|
+
return {
|
|
181
|
+
page: hasPage ? {
|
|
182
|
+
pageTitle: v0.pageTitle || "",
|
|
183
|
+
breadcrumbs: v0.breadcrumbs || [],
|
|
184
|
+
mainEntity: v0.mainEntity || null
|
|
185
|
+
} : null,
|
|
186
|
+
site: hasSite ? {
|
|
187
|
+
branding: v0.branding || {
|
|
188
|
+
homeUrl: "",
|
|
189
|
+
siteName: "",
|
|
190
|
+
siteSlogan: ""
|
|
191
|
+
},
|
|
192
|
+
baseUrl: v0.baseUrl || "/",
|
|
193
|
+
themeAssets: v0.themeAssets || EMPTY_THEME_ASSETS
|
|
194
|
+
} : null
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Builds the JSON:API runtime configuration for a Drupal-rendered page from
|
|
199
|
+
* `drupalSettings.canvasData.v0`, or `null` when the settings do not carry a
|
|
200
|
+
* base URL or JSON:API is not installed. Previews read working copies through
|
|
201
|
+
* the editor's Drupal session.
|
|
202
|
+
*/
|
|
203
|
+
function drupalSettingsToJsonApiRuntimeConfig(v0, options = {}) {
|
|
204
|
+
if (!v0 || typeof v0.baseUrl !== "string" || v0.baseUrl === "") return null;
|
|
205
|
+
if (v0.jsonapiSettings === null) return null;
|
|
206
|
+
const preview = options.preview ?? false;
|
|
207
|
+
return {
|
|
208
|
+
baseUrl: v0.baseUrl,
|
|
209
|
+
...v0.jsonapiSettings?.apiPrefix && { apiPrefix: v0.jsonapiSettings.apiPrefix },
|
|
210
|
+
resourceVersion: preview ? "rel:working-copy" : null,
|
|
211
|
+
preview
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
//#endregion
|
|
9
215
|
//#region src/Region.tsx
|
|
10
216
|
const RegionsContext = createContext({});
|
|
217
|
+
/**
|
|
218
|
+
* Supplies the region node map read by `Region`.
|
|
219
|
+
*
|
|
220
|
+
* @deprecated Theme-global regions were replaced by page variants (ADR 19);
|
|
221
|
+
* compose pages with the component tree instead. Kept for compatibility.
|
|
222
|
+
*/
|
|
11
223
|
function RegionsProvider({ regions, children }) {
|
|
12
224
|
return /* @__PURE__ */ jsx(RegionsContext.Provider, {
|
|
13
225
|
value: regions,
|
|
14
226
|
children
|
|
15
227
|
});
|
|
16
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Renders the region whose machine name matches `name`.
|
|
231
|
+
*
|
|
232
|
+
* @deprecated Theme-global regions were replaced by page variants (ADR 19);
|
|
233
|
+
* compose pages with the component tree instead. Kept for compatibility.
|
|
234
|
+
*/
|
|
17
235
|
function Region({ name, fallback = null }) {
|
|
18
236
|
const node = useContext(RegionsContext)[name];
|
|
19
237
|
return /* @__PURE__ */ jsx(Fragment, { children: node ?? fallback });
|
|
20
238
|
}
|
|
21
239
|
//#endregion
|
|
22
|
-
export { FormattedText, Image,
|
|
240
|
+
export { AGENT_MIGRATION_PROMPT, DraftSessionError, FormattedText, Image, LegacyJsonApiClient as JsonApiClient, LEGACY_API_GUIDANCE, Region, RegionsProvider, canvasFormatDate, canvasFormatDateRange, canvasFormatDateTime, canvasFormatTime, cn, createJsonApiClient, declareCanvasRuntime, drupalSettingsToCanvasContext, drupalSettingsToJsonApiRuntimeConfig, formatLegacyApiError, getCanvasRuntime, getNodePath, getPageData, getSiteData, isDraftSessionError, isLegacyRuntimeSupported, readCanvasDataV0, sortMenu as sortLinksetMenu, sortMenu$1 as sortMenu };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ComponentType } from "react";
|
|
1
|
+
import { ComponentType, ReactNode } from "react";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { Spec } from "@json-render/core";
|
|
4
4
|
import { ComponentMetadata } from "@drupal-canvas/discovery";
|
|
@@ -59,7 +59,7 @@ type ComponentRegistry = Record<string, ComponentType<Record<string, unknown>>>;
|
|
|
59
59
|
* @param registry - Component registry to use for rendering.
|
|
60
60
|
* @see {@link defineComponentRegistry}
|
|
61
61
|
*/
|
|
62
|
-
declare function renderSpec(spec: Spec, registry: ComponentRegistry):
|
|
62
|
+
declare function renderSpec(spec: Spec, registry: ComponentRegistry): ReactNode;
|
|
63
63
|
/**
|
|
64
64
|
* Renders a Drupal Canvas component tree.
|
|
65
65
|
*
|
|
@@ -67,7 +67,7 @@ declare function renderSpec(spec: Spec, registry: ComponentRegistry): React.Reac
|
|
|
67
67
|
* @param registry - Component registry to use for rendering.
|
|
68
68
|
* @see {@link defineComponentRegistry}
|
|
69
69
|
*/
|
|
70
|
-
declare function renderCanvasTree(components: CanvasComponentTree, registry: ComponentRegistry):
|
|
70
|
+
declare function renderCanvasTree(components: CanvasComponentTree, registry: ComponentRegistry): ReactNode;
|
|
71
71
|
/**
|
|
72
72
|
* Defines a component registry by dynamically importing each component's
|
|
73
73
|
* JavaScript entry file from the discovery results.
|
|
@@ -588,6 +588,8 @@ function renderCanvasTree(components, registry) {
|
|
|
588
588
|
* Canvas JSON Schema `$ref` prefix used in component metadata.
|
|
589
589
|
*/
|
|
590
590
|
const CANVAS_REF_PREFIX = "json-schema-definitions://canvas.module/";
|
|
591
|
+
const COLOR_DEF_KEY = "color";
|
|
592
|
+
const COLOR_DEF_LOCAL_REF = "#/$defs/color";
|
|
591
593
|
/**
|
|
592
594
|
* Rewrites Canvas-specific `$ref` URIs to local JSON Pointer refs (`#/$defs/...`)
|
|
593
595
|
* that Zod's `fromJSONSchema` can resolve natively.
|
|
@@ -600,10 +602,18 @@ function rewriteCanvasRefs(node) {
|
|
|
600
602
|
else result[key] = rewriteCanvasRefs(value);
|
|
601
603
|
return result;
|
|
602
604
|
}
|
|
605
|
+
function normalizeCanvasDefsForValidation(defs) {
|
|
606
|
+
const result = { ...defs };
|
|
607
|
+
const colorDef = result[COLOR_DEF_KEY];
|
|
608
|
+
if (!colorDef || typeof colorDef !== "object" || Array.isArray(colorDef)) return result;
|
|
609
|
+
const colorDefRecord = colorDef;
|
|
610
|
+
if (colorDefRecord.$ref === COLOR_DEF_LOCAL_REF) delete colorDefRecord.$ref;
|
|
611
|
+
return result;
|
|
612
|
+
}
|
|
603
613
|
/**
|
|
604
614
|
* Pre-rewritten Canvas $defs with local refs.
|
|
605
615
|
*/
|
|
606
|
-
const canvasDefs = rewriteCanvasRefs($defs);
|
|
616
|
+
const canvasDefs = normalizeCanvasDefsForValidation(rewriteCanvasRefs($defs));
|
|
607
617
|
/**
|
|
608
618
|
* Recursively walks a JSON schema object, stripping `uri-reference` and
|
|
609
619
|
* `iri-reference` format keywords and collecting the property paths where
|