@parhelia/editor-bridge-react 0.1.12889

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 ADDED
@@ -0,0 +1,41 @@
1
+ # Parhelia Editor Bridge React
2
+
3
+ React helper for adding the Parhelia editor bridge script to rendering hosts.
4
+
5
+ Render the full bridge script directly in Parhelia editing mode:
6
+
7
+ ```tsx
8
+ import { ParheliaBridgeScript } from "@parhelia/editor-bridge-react";
9
+
10
+ <ParheliaBridgeScript searchParams={searchParams} />
11
+ ```
12
+
13
+ By default, `ParheliaBridgeScript` renders only for `?parhelia=1` and reads
14
+ the Sitecore origin from `PARHELIA_SITECORE_ORIGIN` or
15
+ `NEXT_PUBLIC_PARHELIA_SITECORE_ORIGIN`. When those are not set, it falls back
16
+ to the managed Sitecore/XM Cloud host values `SITECORE_API_HOST` and
17
+ `NEXT_PUBLIC_SITECORE_API_HOST`.
18
+
19
+ You can pass the origin explicitly when env lookup is not available:
20
+
21
+ ```tsx
22
+ <ParheliaBridgeScript
23
+ sitecoreOrigin="https://sitecore-authoring.example"
24
+ searchParams={searchParams}
25
+ />
26
+ ```
27
+
28
+ For non-React editing-render routes, reuse the same predicate:
29
+
30
+ ```ts
31
+ import { shouldRenderParheliaBridgeScript } from "@parhelia/editor-bridge-react";
32
+
33
+ if (shouldRenderParheliaBridgeScript({ requestUrl: request.url })) {
34
+ // Inject /parhelia/editor-bridge/v1/parhelia-bridge.js from the Sitecore origin.
35
+ }
36
+ ```
37
+
38
+ The Sitecore origin should come from trusted server config or fixed application
39
+ code. Do not source it from the query string or a parent-window message. For
40
+ cross-origin rendering hosts, allow the Sitecore origin that serves this script
41
+ in `script-src`.
@@ -0,0 +1,24 @@
1
+ import { type ReactElement, type ScriptHTMLAttributes } from "react";
2
+ export type ParheliaBridgeSearchParams = URLSearchParams | Record<string, string | string[] | null | undefined>;
3
+ export type ParheliaBridgeActivationOptions = {
4
+ enabled?: boolean;
5
+ queryParam?: string;
6
+ queryValue?: string | null;
7
+ requestUrl?: string | URL;
8
+ searchParams?: ParheliaBridgeSearchParams;
9
+ };
10
+ export declare const PARHELIA_BRIDGE_PATH = "/parhelia/editor-bridge/v1/parhelia-bridge.js";
11
+ export type ParheliaBridgeScriptProps = ParheliaBridgeActivationOptions & {
12
+ sitecoreOrigin?: string;
13
+ id?: string;
14
+ nonce?: string;
15
+ async?: boolean;
16
+ defer?: boolean;
17
+ crossOrigin?: ScriptHTMLAttributes<HTMLScriptElement>["crossOrigin"];
18
+ integrity?: string;
19
+ referrerPolicy?: ScriptHTMLAttributes<HTMLScriptElement>["referrerPolicy"];
20
+ };
21
+ export declare function shouldRenderParheliaBridgeScript({ enabled, queryParam, queryValue, requestUrl, searchParams, }?: ParheliaBridgeActivationOptions): boolean;
22
+ export declare function ParheliaBridgeScript({ sitecoreOrigin, id, nonce, async, defer, crossOrigin, integrity, referrerPolicy, ...activation }: ParheliaBridgeScriptProps): ReactElement | null;
23
+ export declare function resolveParheliaBridgeUrl(sitecoreOrigin?: string): string | undefined;
24
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,oBAAoB,EAC1B,MAAM,OAAO,CAAC;AAEf,MAAM,MAAM,0BAA0B,GAClC,eAAe,GACf,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;AAEzD,MAAM,MAAM,+BAA+B,GAAG;IAC5C,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;IAC1B,YAAY,CAAC,EAAE,0BAA0B,CAAC;CAC3C,CAAC;AAEF,eAAO,MAAM,oBAAoB,kDACgB,CAAC;AAElD,MAAM,MAAM,yBAAyB,GAAG,+BAA+B,GAAG;IACxE,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,WAAW,CAAC,EAAE,oBAAoB,CAAC,iBAAiB,CAAC,CAAC,aAAa,CAAC,CAAC;IACrE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,oBAAoB,CAAC,iBAAiB,CAAC,CAAC,gBAAgB,CAAC,CAAC;CAC5E,CAAC;AAMF,wBAAgB,gCAAgC,CAAC,EAC/C,OAAO,EACP,UAAuB,EACvB,UAAgB,EAChB,UAAU,EACV,YAAY,GACb,GAAE,+BAAoC,GAAG,OAAO,CAmBhD;AAED,wBAAgB,oBAAoB,CAAC,EACnC,cAAc,EACd,EAAE,EACF,KAAK,EACL,KAAa,EACb,KAAY,EACZ,WAAW,EACX,SAAS,EACT,cAAc,EACd,GAAG,UAAU,EACd,EAAE,yBAAyB,GAAG,YAAY,GAAG,IAAI,CAqBjD;AAED,wBAAgB,wBAAwB,CAAC,cAAc,CAAC,EAAE,MAAM,sBAW/D"}
package/dist/index.js ADDED
@@ -0,0 +1,87 @@
1
+ import { createElement, } from "react";
2
+ export const PARHELIA_BRIDGE_PATH = "/parhelia/editor-bridge/v1/parhelia-bridge.js";
3
+ export function shouldRenderParheliaBridgeScript({ enabled, queryParam = "parhelia", queryValue = "1", requestUrl, searchParams, } = {}) {
4
+ if (enabled !== undefined) {
5
+ return enabled;
6
+ }
7
+ const params = resolveSearchParams(requestUrl, searchParams);
8
+ if (!params) {
9
+ return false;
10
+ }
11
+ if (!params.has(queryParam)) {
12
+ return false;
13
+ }
14
+ if (queryValue === null) {
15
+ return true;
16
+ }
17
+ return params.getAll(queryParam).some((value) => value === queryValue);
18
+ }
19
+ export function ParheliaBridgeScript({ sitecoreOrigin, id, nonce, async = false, defer = true, crossOrigin, integrity, referrerPolicy, ...activation }) {
20
+ if (!shouldRenderParheliaBridgeScript(activation)) {
21
+ return null;
22
+ }
23
+ const bridgeUrl = resolveParheliaBridgeUrl(sitecoreOrigin);
24
+ if (!bridgeUrl) {
25
+ return null;
26
+ }
27
+ return createElement("script", {
28
+ id,
29
+ src: bridgeUrl,
30
+ async,
31
+ defer,
32
+ nonce,
33
+ crossOrigin,
34
+ integrity,
35
+ referrerPolicy,
36
+ "data-parhelia-editor-bridge": "runtime",
37
+ });
38
+ }
39
+ export function resolveParheliaBridgeUrl(sitecoreOrigin) {
40
+ const origin = sitecoreOrigin || readSitecoreOriginFromEnvironment();
41
+ if (!origin) {
42
+ return undefined;
43
+ }
44
+ try {
45
+ return new URL(PARHELIA_BRIDGE_PATH, origin).toString();
46
+ }
47
+ catch {
48
+ return undefined;
49
+ }
50
+ }
51
+ function resolveSearchParams(requestUrl, searchParams) {
52
+ if (searchParams instanceof URLSearchParams) {
53
+ return searchParams;
54
+ }
55
+ if (searchParams) {
56
+ const params = new URLSearchParams();
57
+ Object.entries(searchParams).forEach(([key, value]) => {
58
+ if (Array.isArray(value)) {
59
+ value.forEach((entry) => params.append(key, entry));
60
+ return;
61
+ }
62
+ if (value !== undefined && value !== null) {
63
+ params.append(key, value);
64
+ }
65
+ });
66
+ return params;
67
+ }
68
+ if (requestUrl) {
69
+ try {
70
+ return new URL(requestUrl).searchParams;
71
+ }
72
+ catch {
73
+ return new URL(requestUrl, "http://parhelia.local").searchParams;
74
+ }
75
+ }
76
+ if (typeof window !== "undefined") {
77
+ return new URLSearchParams(window.location.search);
78
+ }
79
+ return undefined;
80
+ }
81
+ function readSitecoreOriginFromEnvironment() {
82
+ const env = globalThis.process?.env;
83
+ return (env?.PARHELIA_SITECORE_ORIGIN ||
84
+ env?.NEXT_PUBLIC_PARHELIA_SITECORE_ORIGIN ||
85
+ env?.SITECORE_API_HOST ||
86
+ env?.NEXT_PUBLIC_SITECORE_API_HOST);
87
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@parhelia/editor-bridge-react",
3
+ "version": "0.1.12889",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist/",
18
+ "README.md",
19
+ "LICENSE"
20
+ ],
21
+ "type": "module",
22
+ "scripts": {
23
+ "build": "tsc -p tsconfig.build.json",
24
+ "build:native": "tsgo -p tsconfig.build.json",
25
+ "check-types": "tsc --noEmit -p tsconfig.build.json",
26
+ "check-types:native": "tsgo --noEmit -p tsconfig.build.json",
27
+ "test": "vitest run"
28
+ },
29
+ "peerDependencies": {
30
+ "react": ">=18"
31
+ },
32
+ "devDependencies": {
33
+ "@repo/typescript-config": "*",
34
+ "@types/react": "19.2.14",
35
+ "typescript": "5.8.2",
36
+ "vitest": "^4.1.5"
37
+ }
38
+ }