drupal-canvas 0.7.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/dist/index.d.ts +1 -97
- package/dist/index.js +1 -143
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -6,102 +6,6 @@ import { getNodePath, sortMenu as sortMenu$1 } from "./jsonapi-utils.js";
|
|
|
6
6
|
import Image from "./next-image-standalone.js";
|
|
7
7
|
import { cn } from "./utils.js";
|
|
8
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
9
|
//#region src/drupal-settings.d.ts
|
|
106
10
|
/** The subset of `drupalSettings.canvasData.v0` the context API reads. */
|
|
107
11
|
interface CanvasDataV0 {
|
|
@@ -224,4 +128,4 @@ declare function getCanvasRuntime(): CanvasRuntimeEnvironment | null;
|
|
|
224
128
|
*/
|
|
225
129
|
declare function isLegacyRuntimeSupported(): boolean;
|
|
226
130
|
//#endregion
|
|
227
|
-
export { AGENT_MIGRATION_PROMPT, type BreadcrumbLink, type CanvasContext, type CanvasDataV0, type
|
|
131
|
+
export { AGENT_MIGRATION_PROMPT, type BreadcrumbLink, type CanvasContext, type CanvasDataV0, 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, 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
|
@@ -7,148 +7,6 @@ import Image from "./next-image-standalone.js";
|
|
|
7
7
|
import { cn } from "./utils.js";
|
|
8
8
|
import { Fragment, jsx } from "react/jsx-runtime";
|
|
9
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
10
|
//#region src/drupal-settings.ts
|
|
153
11
|
/** Reads `drupalSettings.canvasData.v0` from a settings object or the global. */
|
|
154
12
|
function readCanvasDataV0(settings = globalThis.drupalSettings) {
|
|
@@ -237,4 +95,4 @@ function Region({ name, fallback = null }) {
|
|
|
237
95
|
return /* @__PURE__ */ jsx(Fragment, { children: node ?? fallback });
|
|
238
96
|
}
|
|
239
97
|
//#endregion
|
|
240
|
-
export { AGENT_MIGRATION_PROMPT, DraftSessionError, FormattedText, Image, LegacyJsonApiClient as JsonApiClient, LEGACY_API_GUIDANCE, Region, RegionsProvider,
|
|
98
|
+
export { AGENT_MIGRATION_PROMPT, DraftSessionError, FormattedText, Image, LegacyJsonApiClient as JsonApiClient, LEGACY_API_GUIDANCE, Region, RegionsProvider, cn, createJsonApiClient, declareCanvasRuntime, drupalSettingsToCanvasContext, drupalSettingsToJsonApiRuntimeConfig, formatLegacyApiError, getCanvasRuntime, getNodePath, getPageData, getSiteData, isDraftSessionError, isLegacyRuntimeSupported, readCanvasDataV0, sortMenu as sortLinksetMenu, sortMenu$1 as sortMenu };
|