@parhelia/head 0.0.0-stage → 1.0.12951
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/LICENSE +8 -0
- package/README.md +231 -2
- package/package.json +133 -4
- package/src/bridge-core.d.ts +97 -0
- package/src/bridge-core.js +173 -0
- package/src/contentful.d.ts +34 -0
- package/src/contentful.js +213 -0
- package/src/env.d.ts +11 -0
- package/src/env.js +47 -0
- package/src/identity.d.ts +45 -0
- package/src/identity.js +137 -0
- package/src/index.d.ts +6 -0
- package/src/index.js +13 -0
- package/src/magnolia.d.ts +60 -0
- package/src/magnolia.js +199 -0
- package/src/picture-crops.d.ts +168 -0
- package/src/picture-crops.js +355 -0
- package/src/refresh-dom.d.ts +9 -0
- package/src/refresh-dom.js +92 -0
- package/src/refresh.d.ts +14 -0
- package/src/refresh.js +63 -0
- package/src/rich-text-reader.js +263 -0
- package/src/rich-text.d.ts +17 -0
- package/src/rich-text.js +354 -0
- package/src/server.d.ts +38 -0
- package/src/server.js +113 -0
- package/src/settings.cjs +35 -0
- package/src/settings.d.cts +7 -0
- package/src/sitecore.d.ts +53 -0
- package/src/sitecore.js +228 -0
- package/src/slot-markup.d.ts +5 -0
- package/src/slot-markup.js +24 -0
- package/src/typo3.d.ts +34 -0
- package/src/typo3.js +110 -0
- package/src/umbraco.d.ts +51 -0
- package/src/umbraco.js +146 -0
- package/src/vite.d.ts +18 -0
- package/src/vite.js +53 -0
package/LICENSE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,232 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @parhelia/head
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Make a rendering head editable in Parhelia, on any framework and without one: Next.js, TanStack Start, React Router, Astro,
|
|
4
|
+
plain server-rendered HTML. Plain JavaScript, no build step, no React: it can be copied into a head that is built elsewhere.
|
|
5
|
+
|
|
6
|
+
| Entry | What it is for |
|
|
7
|
+
|---|---|
|
|
8
|
+
| `@parhelia/head` | the editing bridge (`loadParheliaBridge` in the browser, `parheliaBridgeScriptProps` for the server's `<script>`), the slot markers, the rendered identity, the in-place refresh (`installParheliaRefresh`, `installParheliaDomRefresh`), the build values (`withBasePath`, `parheliaOrigin`, `configureParhelia`) |
|
|
9
|
+
| `@parhelia/head/server` | `isEditingRequest`: is this request the editor? (server-side only) |
|
|
10
|
+
| `@parhelia/head/identity` | the rendered-identity script |
|
|
11
|
+
| `@parhelia/head/refresh` | the in-place refresh loop |
|
|
12
|
+
| `@parhelia/head/rich-text` | `sanitizeRichTextHtml` for CMS rich text |
|
|
13
|
+
| `@parhelia/head/picture-crops` | a picture's crops and focal point: which crop a variant shows, and the CSS that shows it |
|
|
14
|
+
| `@parhelia/head/<cms>` | a CMS's own helpers: `contentful` (Live Preview origins, `contentfulPictureSources`), `umbraco` (marker names) |
|
|
15
|
+
| `@parhelia/head/vite` | `parhelia()`, the Vite build adapter |
|
|
16
|
+
|
|
17
|
+
The framework adapters build on it:
|
|
18
|
+
|
|
19
|
+
| Package | For |
|
|
20
|
+
|---|---|
|
|
21
|
+
| [`@parhelia/react`](https://www.npmjs.com/package/@parhelia/react) | a React head on any framework: `ParheliaBridge`, `ParheliaBridgeScript`, `ParheliaSlot`, `useParheliaRefresh`, `ParheliaPicture` |
|
|
22
|
+
| [`@parhelia/astro`](https://www.npmjs.com/package/@parhelia/astro) | an Astro head: the integration and the slot, bridge and refresh components |
|
|
23
|
+
| [`@parhelia/next`](https://www.npmjs.com/package/@parhelia/next) | a Next.js head: `withParhelia` (`/config`), re-exporting this package and `@parhelia/react` under the same entries |
|
|
24
|
+
|
|
25
|
+
## The build: base path and editor origin
|
|
26
|
+
|
|
27
|
+
Two values come from the head's build, because the server render and the browser must agree on them: the base path below
|
|
28
|
+
Parhelia's rendering proxy (`PARHELIA_RENDERING_PROXY_PATH`, `/parhelia/proxy/<head name>`) and the Parhelia editor's origin
|
|
29
|
+
(`PARHELIA_ORIGIN`, for a head the editor frames directly). `withBasePath`, `ParheliaBridge` and
|
|
30
|
+
`contentfulLivePreviewOrigins` read them. A framework adapter puts them into the build:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
// vite.config.ts (TanStack Start, React Router, any Vite build): before the framework's own plugin
|
|
34
|
+
import { parhelia } from '@parhelia/head/vite';
|
|
35
|
+
import { tanstackStart } from '@tanstack/react-start/plugin/vite';
|
|
36
|
+
import viteReact from '@vitejs/plugin-react';
|
|
37
|
+
|
|
38
|
+
export default defineConfig({ plugins: [parhelia(), tanstackStart(), viteReact()] });
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`parhelia()` sets Vite's `base` to the proxy path (assets included; TanStack Start derives its router's base path from it),
|
|
42
|
+
inlines the two values into the browser's and the server's code, and bundles this package for the server so the values reach
|
|
43
|
+
it. Only these two: the editor secret and the preview key stay server-side environment variables. A Next.js head uses
|
|
44
|
+
`withParhelia` from `@parhelia/next/config` instead. Any other build calls `configureParhelia({ origin, basePath })` in a
|
|
45
|
+
module both the server and the browser load.
|
|
46
|
+
|
|
47
|
+
A directly framed head also lets the editor frame it: add `PARHELIA_ORIGIN` to the CSP `frame-ancestors` of the head's
|
|
48
|
+
responses (`withParhelia` does this for Next.js's `headers()`).
|
|
49
|
+
|
|
50
|
+
## The editing bridge
|
|
51
|
+
|
|
52
|
+
A head the editor frames directly (its own URL, another origin than the editor's) loads Parhelia's editing bridge itself: a
|
|
53
|
+
cross-origin frame cannot be given the bridge by its parent. Below Parhelia's rendering proxy the editor injects it.
|
|
54
|
+
`loadParheliaBridge()` adds the script in the browser, only in a frame of another origin on `?parhelia=1`;
|
|
55
|
+
`parheliaBridgeScriptProps(options)` is the `<script>`'s attributes for a server that renders it into the HTML (a CSP nonce,
|
|
56
|
+
`integrity`), with `shouldRenderParheliaBridgeScript({ searchParams | requestUrl })` as the request check. The bridge loads from
|
|
57
|
+
`/parhelia/editor-bridge/v1/parhelia-bridge.js` on the Parhelia origin (`origin`, default `parheliaOrigin()`), which is named as
|
|
58
|
+
its parent; a `bridgeOrigin` (a CMS authoring host proxying Parhelia's routes) only serves it. `@parhelia/react` and
|
|
59
|
+
`@parhelia/astro` wrap both as components.
|
|
60
|
+
|
|
61
|
+
The editor loads its edit mode and its preview mode with the same request (`?parhelia=1`, and a CMS preview flag such as
|
|
62
|
+
`?preview=1`), so the head's server cannot tell them apart. The document can: while the editing bridge runs, the root
|
|
63
|
+
element names the editor's mode, `<html data-parhelia-editor-mode="edit|preview|suggestions">`, set as soon as the bridge
|
|
64
|
+
runs and updated in place when the person switches modes. Outside the editor's frames it is absent. Scope what only editing
|
|
65
|
+
shows (an empty field's hint, the box that makes an empty field clickable) to it, and preview shows the page as visitors see
|
|
66
|
+
it:
|
|
67
|
+
|
|
68
|
+
```css
|
|
69
|
+
html[data-parhelia-editor-mode]:not([data-parhelia-editor-mode="preview"]) [data-empty-hint]:empty::before {
|
|
70
|
+
content: attr(data-empty-hint);
|
|
71
|
+
opacity: 0.45;
|
|
72
|
+
}
|
|
73
|
+
/* preview: the empty element takes no space, as on the published page */
|
|
74
|
+
html[data-parhelia-editor-mode="preview"] [data-empty-hint]:empty {
|
|
75
|
+
display: none;
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`PARHELIA_EDITING_SELECTOR` is that selector (edit and suggestions mode) and `PARHELIA_EDITOR_MODE_ATTRIBUTE` the attribute,
|
|
80
|
+
for CSS-in-JS. The same marker is set on every head that loads the bridge, whatever loads it (`ParheliaBridge`, Parhelia's
|
|
81
|
+
proxy, the Umbraco package).
|
|
82
|
+
|
|
83
|
+
## Empty slots
|
|
84
|
+
|
|
85
|
+
A slot's rendering (a reference-array field such as a page's `topSection`) is marked for the editor in Parhelia's frames, also
|
|
86
|
+
when it holds nothing, so the canvas offers a drop zone for it: an inert `<script>` with `slotStartAttributes(name,
|
|
87
|
+
description)` before the slot's content, one with `slotEndAttributes(name)` after it, and for an empty slot a block with
|
|
88
|
+
`emptySlotAttributes(name)` and a `min-height` (`EMPTY_SLOT_MIN_HEIGHT`) between them. `ParheliaSlot` of `@parhelia/react` and
|
|
89
|
+
`@parhelia/astro` renders exactly that.
|
|
90
|
+
|
|
91
|
+
## What a render shows: `@parhelia/head/identity`
|
|
92
|
+
|
|
93
|
+
Parhelia's capture (agent screenshots, DOM captures, component thumbnails) only captures a document whose renderer states
|
|
94
|
+
what it rendered. In Parhelia's frames (`?parhelia=1`, `isParheliaFrameRequest`) render one inert script from the data
|
|
95
|
+
the page itself read, never from the request's query:
|
|
96
|
+
|
|
97
|
+
```tsx
|
|
98
|
+
// getServerSideProps (or a server component): the entry this render shows, by its own id
|
|
99
|
+
import { parheliaStore, renderedIdentityScriptProps } from '@parhelia/head/identity';
|
|
100
|
+
const identity = { store: parheliaStore(), itemId: page.sys.id, language: locale };
|
|
101
|
+
|
|
102
|
+
// the page
|
|
103
|
+
<script {...renderedIdentityScriptProps(identity)} />
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
| Part | Value |
|
|
107
|
+
|---|---|
|
|
108
|
+
| `store` | the Parhelia store the head renders: `PARHELIA_STORE` (e.g. `contentful:<space>/<concrete environment>`) |
|
|
109
|
+
| `itemId` | the item's id as its CMS spells it: a Contentful entry's `sys.id`, a Sitecore item's Guid |
|
|
110
|
+
| `language` | the locale rendered |
|
|
111
|
+
| `version` | the version rendered where the CMS keeps numbered versions; omitted for Contentful |
|
|
112
|
+
|
|
113
|
+
A pages-router head can let `frameRenderedIdentity` do the gating: it answers the identity in Parhelia's frames and null
|
|
114
|
+
elsewhere, when the render produced no item, or when `PARHELIA_STORE` is unset (the server log says so once):
|
|
115
|
+
|
|
116
|
+
```tsx
|
|
117
|
+
// getServerSideProps
|
|
118
|
+
import { frameRenderedIdentity } from '@parhelia/head';
|
|
119
|
+
return { props: { ..., parheliaIdentity: frameRenderedIdentity(query, { itemId: page?.sys.id, language: locale }) } };
|
|
120
|
+
|
|
121
|
+
// _app.tsx
|
|
122
|
+
{pageProps.parheliaIdentity && <script {...renderedIdentityScriptProps(pageProps.parheliaIdentity)} />}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Contentful's marketing starter, made editable in Parhelia, does exactly this. Settings ›
|
|
126
|
+
Rendering Hosts › the head's settings lists the `PARHELIA_STORE` line for a head of a Contentful mount.
|
|
127
|
+
|
|
128
|
+
The module imports nothing (no React, no Next.js), so a head that links this package from a checkout never resolves a
|
|
129
|
+
second React through it; `renderedIdentityScriptHtml` writes the same script as HTML. Without the script a capture is
|
|
130
|
+
refused with "the rendered document did not confirm the exact store, item, locale, and version".
|
|
131
|
+
|
|
132
|
+
## Is this the editor? `@parhelia/head/server`
|
|
133
|
+
|
|
134
|
+
`?parhelia=1` says which editor request a request is, never whether it is one: a visitor can type it. A head's server
|
|
135
|
+
decides with `isEditingRequest` before it renders markers, empty components or the identity script:
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
// getServerSideProps (Pages Router); a route handler or server component passes the Request / headers() and searchParams
|
|
139
|
+
import { isEditingRequest } from '@parhelia/head/server';
|
|
140
|
+
import { readPreviewSessionFor } from '@parhelia/preview-ticket/next';
|
|
141
|
+
|
|
142
|
+
const session = await readPreviewSessionFor(req, res);
|
|
143
|
+
const edit = isEditingRequest(req, query, session !== null);
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
A fetch-handler server (TanStack Start, React Router, Hono) passes its Web `Request` and reads the session with
|
|
147
|
+
`@parhelia/preview-ticket/fetch`:
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import { isEditingRequest } from '@parhelia/head/server';
|
|
151
|
+
import { readPreviewSession } from '@parhelia/preview-ticket/fetch';
|
|
152
|
+
|
|
153
|
+
const session = await readPreviewSession(request);
|
|
154
|
+
const edit = isEditingRequest(request, query, session !== null);
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
It is true for one of Parhelia's frames (`parhelia=1`, which every edit URL the editor builds carries) that either carries the rendering proxy's
|
|
158
|
+
`X-Parhelia-Editor` header equal to `PARHELIA_EDITOR_SECRET` (the host entry's `editorSecret=`; the proxy drops whatever the
|
|
159
|
+
browser sent under that name) or has a live per-person preview session (`@parhelia/preview-ticket`). No secret configured and
|
|
160
|
+
no session: never the editor. `hasEditorSignal(request)` and `editorSignalAccepted(value, secret)` are the parts; the
|
|
161
|
+
comparison is constant-time and runs on Node and the Edge alike.
|
|
162
|
+
|
|
163
|
+
## CMS rich text: `@parhelia/head/rich-text`
|
|
164
|
+
|
|
165
|
+
Below Parhelia's rendering proxy the head is served from the editor's origin, so a script, event handler or `javascript:` link
|
|
166
|
+
in a stored rich-text value would run with the editing person's session. Write CMS rich text only through
|
|
167
|
+
`sanitizeRichTextHtml`:
|
|
168
|
+
|
|
169
|
+
```tsx
|
|
170
|
+
import { sanitizeRichTextHtml } from '@parhelia/head/rich-text';
|
|
171
|
+
|
|
172
|
+
<div {...fieldMarkers} dangerouslySetInnerHTML={{ __html: sanitizeRichTextHtml(storedHtml) }} />;
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
It keeps formatting (headings, paragraphs, lists, tables, links, images, quotes, a fixed set of style properties) and Parhelia's
|
|
176
|
+
own placeholder and internal-link markers; it removes scripts, styles, frames, objects, forms' lists, SVG and MathML with their
|
|
177
|
+
content, every event handler, other `data-*` attributes, URLs other than `http(s)`, `mailto`, `tel` (an image also raster
|
|
178
|
+
`data:image/…`, a link also `parhelia:`), and ids that would shadow a page global. It needs no DOM (a server component,
|
|
179
|
+
`getServerSideProps`) and no dependency. The editing bridge applies the same rule (`richTextElementAction`,
|
|
180
|
+
`sanitizeRichTextAttribute`) to the values it writes into the canvas.
|
|
181
|
+
|
|
182
|
+
## Picture crops: `@parhelia/head/picture-crops`
|
|
183
|
+
|
|
184
|
+
A Contentful entry whose crops field uses the Parhelia app keeps its crops beside the asset link, as JSON (`v: 1`): the
|
|
185
|
+
image it was made on, a focal point, and the variants someone cropped by hand, in fractions of the original. The image service
|
|
186
|
+
cannot cut out a rectangle, so a head shows a crop with CSS:
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
import { cropFor, cropStyle, readPictureCrops, requestWidth } from '@parhelia/head/picture-crops';
|
|
190
|
+
|
|
191
|
+
const read = readPictureCrops(entry.fields.crops);
|
|
192
|
+
const image = { asset: asset.sys.id, file: file.url, size: file.details.image };
|
|
193
|
+
const shown = cropFor(read, { name: 'landscape', ratio: '16:9' }, image); // the stored crop, else one around the focal point
|
|
194
|
+
const style = shown && cropStyle(shown.crop, image.size); // the box's aspect ratio, the image's size and offset in it
|
|
195
|
+
const width = shown && requestWidth(800, shown.crop, image.size, 2); // the `w` to ask the Images API for
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
The box clips (`position: relative; overflow: hidden; aspect-ratio: <style.aspectRatio>`); the image inside is
|
|
199
|
+
`position: absolute; max-width: none` with `style.width`, `style.height`, `style.left` and `style.top`. Put `data-variant` and
|
|
200
|
+
`data-parhelia-crop` (`cropAttribute(shown.crop)`) on the image, so the editor knows which part of it the visitor sees.
|
|
201
|
+
|
|
202
|
+
`contentfulPictureSources` (`@parhelia/head/contentful`) does all of that for a `<picture>` whose variants switch by media query:
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
import { contentfulPictureSources } from '@parhelia/head/contentful';
|
|
206
|
+
|
|
207
|
+
const picture = contentfulPictureSources(
|
|
208
|
+
{ id: asset.sys.id, url: file.url, ...file.details.image },
|
|
209
|
+
entry.fields.crops,
|
|
210
|
+
[
|
|
211
|
+
{ name: 'square', ratio: '1:1', media: '(max-width: 61.99em)', sizes: '50vw' },
|
|
212
|
+
{ name: 'landscape', ratio: '16:9', sizes: '33vw' },
|
|
213
|
+
],
|
|
214
|
+
);
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
It answers the picture's CSS (render it in a `<style>` as raw text; React sets it with `dangerouslySetInnerHTML`, as a child
|
|
218
|
+
its server rendering escapes it), the clipping box's key attribute (`box`; put the field's annotations on the box too), and
|
|
219
|
+
each `<source>` and the `<img>` with its Images API `srcset`, `sizes`, size, variant and crop. The box also gets
|
|
220
|
+
`--parhelia-crop-ratio`, so a site that caps its height can cap its width at the same ratio. `options.focalPoint` (fractions) is
|
|
221
|
+
the point the automatic crops follow where the stored value names none for this file: a head with a focal point of its own
|
|
222
|
+
keeps it as the fallback. A React head renders it with `ParheliaPicture` (`@parhelia/react`).
|
|
223
|
+
|
|
224
|
+
## In-place refresh
|
|
225
|
+
|
|
226
|
+
The editor calls `window.requestRefresh(newUri?)` after a save. `installParheliaRefresh({ refetch, signature, deadlineMs })`
|
|
227
|
+
installs it: `refetch` reads the page again the head's way, `signature()` is a stable string of what the page rendered, and the
|
|
228
|
+
loop reads again once a second until it changed and settled, or the deadline passed. The answer removes it again.
|
|
229
|
+
`useParheliaRefresh` of `@parhelia/react` is the React hook around it. A head without a client framework uses
|
|
230
|
+
`installParheliaDomRefresh()`: it reads the page again and patches what changed into the live document, keeping every node the
|
|
231
|
+
new render left alone (and what the editing bridge added to it), inside the element marked `data-parhelia-refresh-root`;
|
|
232
|
+
`ParheliaRefresh` of `@parhelia/astro` renders that element and installs it.
|
package/package.json
CHANGED
|
@@ -1,6 +1,135 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parhelia/head",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.12951",
|
|
4
|
+
"description": "Make a rendering head editable in Parhelia, on any framework and without one: the editing bridge, slot markers, the in-place refresh (with a DOM-patching one for heads without a client framework), the rendered-content identity capture reads, the editor signal a head's server checks (/server), rich-text sanitising (/rich-text), per-CMS marker helpers (/contentful, /umbraco), a SitecoreAI editing host's canvas render (/sitecore), the build values and the Vite build adapter (/vite). No React: React heads add @parhelia/react, Next.js heads @parhelia/next, Astro heads @parhelia/astro.",
|
|
5
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./src/index.js",
|
|
11
|
+
"types": "./src/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./src/index.d.ts",
|
|
15
|
+
"default": "./src/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./server": {
|
|
18
|
+
"types": "./src/server.d.ts",
|
|
19
|
+
"default": "./src/server.js"
|
|
20
|
+
},
|
|
21
|
+
"./identity": {
|
|
22
|
+
"types": "./src/identity.d.ts",
|
|
23
|
+
"default": "./src/identity.js"
|
|
24
|
+
},
|
|
25
|
+
"./refresh": {
|
|
26
|
+
"types": "./src/refresh.d.ts",
|
|
27
|
+
"default": "./src/refresh.js"
|
|
28
|
+
},
|
|
29
|
+
"./rich-text": {
|
|
30
|
+
"types": "./src/rich-text.d.ts",
|
|
31
|
+
"default": "./src/rich-text.js"
|
|
32
|
+
},
|
|
33
|
+
"./picture-crops": {
|
|
34
|
+
"types": "./src/picture-crops.d.ts",
|
|
35
|
+
"default": "./src/picture-crops.js"
|
|
36
|
+
},
|
|
37
|
+
"./contentful": {
|
|
38
|
+
"types": "./src/contentful.d.ts",
|
|
39
|
+
"default": "./src/contentful.js"
|
|
40
|
+
},
|
|
41
|
+
"./umbraco": {
|
|
42
|
+
"types": "./src/umbraco.d.ts",
|
|
43
|
+
"default": "./src/umbraco.js"
|
|
44
|
+
},
|
|
45
|
+
"./magnolia": {
|
|
46
|
+
"types": "./src/magnolia.d.ts",
|
|
47
|
+
"default": "./src/magnolia.js"
|
|
48
|
+
},
|
|
49
|
+
"./typo3": {
|
|
50
|
+
"types": "./src/typo3.d.ts",
|
|
51
|
+
"default": "./src/typo3.js"
|
|
52
|
+
},
|
|
53
|
+
"./sitecore": {
|
|
54
|
+
"types": "./src/sitecore.d.ts",
|
|
55
|
+
"default": "./src/sitecore.js"
|
|
56
|
+
},
|
|
57
|
+
"./vite": {
|
|
58
|
+
"types": "./src/vite.d.ts",
|
|
59
|
+
"default": "./src/vite.js"
|
|
60
|
+
},
|
|
61
|
+
"./settings": {
|
|
62
|
+
"types": "./src/settings.d.cts",
|
|
63
|
+
"default": "./src/settings.cjs"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"typesVersions": {
|
|
67
|
+
"*": {
|
|
68
|
+
"server": [
|
|
69
|
+
"./src/server.d.ts"
|
|
70
|
+
],
|
|
71
|
+
"identity": [
|
|
72
|
+
"./src/identity.d.ts"
|
|
73
|
+
],
|
|
74
|
+
"refresh": [
|
|
75
|
+
"./src/refresh.d.ts"
|
|
76
|
+
],
|
|
77
|
+
"rich-text": [
|
|
78
|
+
"./src/rich-text.d.ts"
|
|
79
|
+
],
|
|
80
|
+
"picture-crops": [
|
|
81
|
+
"./src/picture-crops.d.ts"
|
|
82
|
+
],
|
|
83
|
+
"contentful": [
|
|
84
|
+
"./src/contentful.d.ts"
|
|
85
|
+
],
|
|
86
|
+
"umbraco": [
|
|
87
|
+
"./src/umbraco.d.ts"
|
|
88
|
+
],
|
|
89
|
+
"magnolia": [
|
|
90
|
+
"./src/magnolia.d.ts"
|
|
91
|
+
],
|
|
92
|
+
"typo3": [
|
|
93
|
+
"./src/typo3.d.ts"
|
|
94
|
+
],
|
|
95
|
+
"sitecore": [
|
|
96
|
+
"./src/sitecore.d.ts"
|
|
97
|
+
],
|
|
98
|
+
"vite": [
|
|
99
|
+
"./src/vite.d.ts"
|
|
100
|
+
],
|
|
101
|
+
"settings": [
|
|
102
|
+
"./src/settings.d.cts"
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
"files": [
|
|
107
|
+
"src/*.js",
|
|
108
|
+
"src/*.cjs",
|
|
109
|
+
"src/*.d.ts",
|
|
110
|
+
"src/*.d.cts",
|
|
111
|
+
"README.md",
|
|
112
|
+
"LICENSE"
|
|
113
|
+
],
|
|
114
|
+
"sideEffects": false,
|
|
115
|
+
"scripts": {
|
|
116
|
+
"check-types": "tsc --noEmit -p tsconfig.json",
|
|
117
|
+
"test": "vitest run"
|
|
118
|
+
},
|
|
119
|
+
"peerDependencies": {
|
|
120
|
+
"vite": ">=5"
|
|
121
|
+
},
|
|
122
|
+
"peerDependenciesMeta": {
|
|
123
|
+
"vite": {
|
|
124
|
+
"optional": true
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
"devDependencies": {
|
|
128
|
+
"@types/node": "*",
|
|
129
|
+
"@types/react": "19.2.18",
|
|
130
|
+
"jsdom": "^30.0.1",
|
|
131
|
+
"typescript": "npm:@typescript/typescript6@6.0.2",
|
|
132
|
+
"vitest": "^4.1.11",
|
|
133
|
+
"vite": "*"
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/** The script's `crossorigin` and `referrerpolicy` values (as the DOM spells them). */
|
|
2
|
+
export type ParheliaBridgeCrossOrigin = "" | "anonymous" | "use-credentials";
|
|
3
|
+
export type ParheliaBridgeReferrerPolicy =
|
|
4
|
+
| ""
|
|
5
|
+
| "no-referrer"
|
|
6
|
+
| "no-referrer-when-downgrade"
|
|
7
|
+
| "origin"
|
|
8
|
+
| "origin-when-cross-origin"
|
|
9
|
+
| "same-origin"
|
|
10
|
+
| "strict-origin"
|
|
11
|
+
| "strict-origin-when-cross-origin"
|
|
12
|
+
| "unsafe-url";
|
|
13
|
+
|
|
14
|
+
export declare const PARHELIA_BRIDGE_PATH: "/parhelia/editor-bridge/v1/parhelia-bridge.js";
|
|
15
|
+
|
|
16
|
+
/** The frame-mode marker the bridge keeps on the framed document's root: the editor's mode (edit, preview, suggestions). */
|
|
17
|
+
export declare const PARHELIA_EDITOR_MODE_ATTRIBUTE: "data-parhelia-editor-mode";
|
|
18
|
+
|
|
19
|
+
/** The selector of a document the editor is editing in (edit or suggestions mode); scope editing-only styling to it. */
|
|
20
|
+
export declare const PARHELIA_EDITING_SELECTOR: 'html[data-parhelia-editor-mode]:not([data-parhelia-editor-mode="preview"])';
|
|
21
|
+
|
|
22
|
+
export type ParheliaBridgeSearchParams = URLSearchParams | Record<string, string | string[] | null | undefined>;
|
|
23
|
+
|
|
24
|
+
/** Which requests are the editor's. */
|
|
25
|
+
export interface ParheliaBridgeActivationOptions {
|
|
26
|
+
/** Decides alone when given, instead of the query. */
|
|
27
|
+
enabled?: boolean;
|
|
28
|
+
/** The frame-URL marker the editor adds (default `parhelia`). */
|
|
29
|
+
queryParam?: string;
|
|
30
|
+
/** Its value (default `1`); `null` accepts any value. */
|
|
31
|
+
queryValue?: string | null;
|
|
32
|
+
/** The request's URL, when no `searchParams` are given. */
|
|
33
|
+
requestUrl?: string | URL;
|
|
34
|
+
/** The request's query (a page's `searchParams`); without it and `requestUrl`, the browser's URL. */
|
|
35
|
+
searchParams?: ParheliaBridgeSearchParams;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Where the bridge loads from, and the script's own attributes. */
|
|
39
|
+
export interface ParheliaBridgeScriptOptions {
|
|
40
|
+
/**
|
|
41
|
+
* The Parhelia editor origin (default: `parheliaOrigin()`, the build's `PARHELIA_ORIGIN`, unless `bridgeOrigin` is
|
|
42
|
+
* given). The bridge loads from it and treats it as its only parent.
|
|
43
|
+
*/
|
|
44
|
+
origin?: string;
|
|
45
|
+
/** Same as `origin` (the name `@parhelia/editor-bridge-react` used). */
|
|
46
|
+
parheliaOrigin?: string;
|
|
47
|
+
/**
|
|
48
|
+
* The origin serving the bridge when it is not the editor itself (e.g. a CMS's authoring host that proxies Parhelia);
|
|
49
|
+
* never named as the bridge's parent. Default: `PARHELIA_BRIDGE_ORIGIN`, then `NEXT_PUBLIC_PARHELIA_BRIDGE_ORIGIN`.
|
|
50
|
+
*/
|
|
51
|
+
bridgeOrigin?: string;
|
|
52
|
+
id?: string;
|
|
53
|
+
/** The response's CSP nonce. */
|
|
54
|
+
nonce?: string;
|
|
55
|
+
async?: boolean;
|
|
56
|
+
defer?: boolean;
|
|
57
|
+
crossOrigin?: ParheliaBridgeCrossOrigin;
|
|
58
|
+
integrity?: string;
|
|
59
|
+
referrerPolicy?: ParheliaBridgeReferrerPolicy;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type ParheliaBridgeScriptProps = ParheliaBridgeActivationOptions & ParheliaBridgeScriptOptions;
|
|
63
|
+
|
|
64
|
+
/** The attributes of the bridge's `<script>`. */
|
|
65
|
+
export interface ParheliaBridgeScriptAttributes {
|
|
66
|
+
id?: string;
|
|
67
|
+
src: string;
|
|
68
|
+
async: boolean;
|
|
69
|
+
defer: boolean;
|
|
70
|
+
nonce?: string;
|
|
71
|
+
crossOrigin?: ParheliaBridgeCrossOrigin;
|
|
72
|
+
integrity?: string;
|
|
73
|
+
referrerPolicy?: ParheliaBridgeReferrerPolicy;
|
|
74
|
+
"data-parhelia-editor-bridge": "runtime";
|
|
75
|
+
/** The Parhelia origin when it serves the bridge; absent for a bridge origin. */
|
|
76
|
+
"data-parhelia-parent-origin"?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The bridge's URL on `origin`; without one, on the Parhelia origin, then on the environment's bridge origin. */
|
|
80
|
+
export declare function resolveParheliaBridgeUrl(origin?: string): string | undefined;
|
|
81
|
+
|
|
82
|
+
/** Whether the request is one of the editor's (`?parhelia=1`); `enabled` decides alone when given. Framing is not looked at. */
|
|
83
|
+
export declare function shouldRenderParheliaBridgeScript(options?: ParheliaBridgeActivationOptions): boolean;
|
|
84
|
+
|
|
85
|
+
/** The bridge script's attributes, or null without an origin to load it from. */
|
|
86
|
+
export declare function parheliaBridgeScriptProps(options?: ParheliaBridgeScriptOptions): ParheliaBridgeScriptAttributes | null;
|
|
87
|
+
|
|
88
|
+
/** Whether this document should load the bridge: in a frame of another origin, on the editor's `?parhelia=1` URL. */
|
|
89
|
+
export declare function shouldLoadParheliaBridge(queryParam?: string): boolean;
|
|
90
|
+
|
|
91
|
+
export interface ParheliaBridgeLoadOptions extends Omit<ParheliaBridgeScriptOptions, "async" | "defer"> {
|
|
92
|
+
/** The frame-URL marker the editor adds (default `parhelia`, value `1`). */
|
|
93
|
+
queryParam?: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Adds the bridge's script when the editor frames this document directly and it has none yet; answers whether it did. */
|
|
97
|
+
export declare function loadParheliaBridge(options?: ParheliaBridgeLoadOptions): boolean;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The editing bridge without a framework: its URL and `<script>` attributes, whether a request or a document is one of
|
|
3
|
+
* the editor's, and `loadParheliaBridge`, which adds the script in the browser. `ParheliaBridgeScript` and `ParheliaBridge` of
|
|
4
|
+
* `@parhelia/react` are React's two ways to use it; a head without React (Astro, plain HTML) uses these directly.
|
|
5
|
+
*/
|
|
6
|
+
import { parheliaOrigin } from "./env.js";
|
|
7
|
+
|
|
8
|
+
export const PARHELIA_BRIDGE_PATH = "/parhelia/editor-bridge/v1/parhelia-bridge.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The frame-mode marker: while the bridge runs, the framed document's root names the editor's mode
|
|
12
|
+
* (`<html data-parhelia-editor-mode="edit|preview|suggestions">`), kept current in place when the person switches modes.
|
|
13
|
+
* Outside the editor's frames it is absent. A request's own flags (`?preview=1`, `?parhelia=1`) are the same in edit and
|
|
14
|
+
* preview mode; this marker is how a head's CSS tells them apart.
|
|
15
|
+
*/
|
|
16
|
+
export const PARHELIA_EDITOR_MODE_ATTRIBUTE = "data-parhelia-editor-mode";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The selector of a document the editor is editing in (edit or suggestions mode, never preview, never outside the editor).
|
|
20
|
+
* Scope editing-only styling to it, such as an empty field's hint and box:
|
|
21
|
+
* `html[data-parhelia-editor-mode]:not([data-parhelia-editor-mode="preview"]) [data-empty-hint]:empty::before`.
|
|
22
|
+
*/
|
|
23
|
+
export const PARHELIA_EDITING_SELECTOR = 'html[data-parhelia-editor-mode]:not([data-parhelia-editor-mode="preview"])';
|
|
24
|
+
|
|
25
|
+
/** @param {() => string | undefined} value */
|
|
26
|
+
const read = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
return value() || "";
|
|
29
|
+
} catch {
|
|
30
|
+
return ""; // no `process` in this bundle and nothing inlined
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The origin serving the bridge when it is not the editor (a CMS's authoring host that proxies Parhelia's routes):
|
|
36
|
+
* `PARHELIA_BRIDGE_ORIGIN`, then the public `NEXT_PUBLIC_PARHELIA_BRIDGE_ORIGIN`.
|
|
37
|
+
*/
|
|
38
|
+
const bridgeOriginFromEnvironment = () =>
|
|
39
|
+
read(() => process.env.PARHELIA_BRIDGE_ORIGIN) || read(() => process.env.NEXT_PUBLIC_PARHELIA_BRIDGE_ORIGIN);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The bridge's URL on `origin`; without one, on the Parhelia origin, then on the environment's bridge origin.
|
|
43
|
+
* @param {string} [origin]
|
|
44
|
+
*/
|
|
45
|
+
export function resolveParheliaBridgeUrl(origin) {
|
|
46
|
+
origin ||= parheliaOrigin() || bridgeOriginFromEnvironment();
|
|
47
|
+
if (!origin) return undefined;
|
|
48
|
+
try {
|
|
49
|
+
return new URL(PARHELIA_BRIDGE_PATH, origin).toString();
|
|
50
|
+
} catch {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param {import("./bridge-core.js").ParheliaBridgeSearchParams | undefined} searchParams
|
|
57
|
+
* @param {string | URL | undefined} requestUrl
|
|
58
|
+
*/
|
|
59
|
+
function resolveSearchParams(searchParams, requestUrl) {
|
|
60
|
+
if (searchParams instanceof URLSearchParams) return searchParams;
|
|
61
|
+
if (searchParams) {
|
|
62
|
+
const params = new URLSearchParams();
|
|
63
|
+
for (const [key, value] of Object.entries(searchParams)) {
|
|
64
|
+
if (Array.isArray(value)) for (const entry of value) params.append(key, entry);
|
|
65
|
+
else if (value !== undefined && value !== null) params.append(key, value);
|
|
66
|
+
}
|
|
67
|
+
return params;
|
|
68
|
+
}
|
|
69
|
+
if (requestUrl) {
|
|
70
|
+
try {
|
|
71
|
+
return new URL(requestUrl).searchParams;
|
|
72
|
+
} catch {
|
|
73
|
+
return new URL(requestUrl, "http://parhelia.local").searchParams;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (typeof window !== "undefined") return new URLSearchParams(window.location.search);
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Whether the request is one of the editor's (`?parhelia=1`): from `searchParams`, else `requestUrl`, else (in the
|
|
82
|
+
* browser) the document's URL. `enabled` decides alone when given. Framing is not looked at (`shouldLoadParheliaBridge`).
|
|
83
|
+
* @param {import("./bridge-core.js").ParheliaBridgeActivationOptions} [options]
|
|
84
|
+
*/
|
|
85
|
+
export function shouldRenderParheliaBridgeScript({
|
|
86
|
+
enabled,
|
|
87
|
+
queryParam = "parhelia",
|
|
88
|
+
queryValue = "1",
|
|
89
|
+
requestUrl,
|
|
90
|
+
searchParams,
|
|
91
|
+
} = {}) {
|
|
92
|
+
if (enabled !== undefined) return enabled;
|
|
93
|
+
const params = resolveSearchParams(searchParams, requestUrl);
|
|
94
|
+
if (!params || !params.has(queryParam)) return false;
|
|
95
|
+
if (queryValue === null) return true;
|
|
96
|
+
return params.getAll(queryParam).some((value) => value === queryValue);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The bridge script's attributes, or null without an origin to load it from. The Parhelia origin (`origin`, else
|
|
101
|
+
* `parheliaOrigin()` unless a `bridgeOrigin` is given) serves the bridge and is named as its parent: an HTTPS editor
|
|
102
|
+
* framing an HTTP head sends no referrer to derive the parent from. A bridge origin (`bridgeOrigin`, else
|
|
103
|
+
* `PARHELIA_BRIDGE_ORIGIN` / `NEXT_PUBLIC_PARHELIA_BRIDGE_ORIGIN`) only serves it and is never named as the parent: the
|
|
104
|
+
* bridge keeps deriving its parent from the referrer. The origins come from trusted configuration, never from the request.
|
|
105
|
+
* @param {import("./bridge-core.js").ParheliaBridgeScriptOptions} [options]
|
|
106
|
+
*/
|
|
107
|
+
export function parheliaBridgeScriptProps({
|
|
108
|
+
origin,
|
|
109
|
+
parheliaOrigin: originAlias,
|
|
110
|
+
bridgeOrigin,
|
|
111
|
+
id,
|
|
112
|
+
nonce,
|
|
113
|
+
async = false,
|
|
114
|
+
defer = true,
|
|
115
|
+
crossOrigin,
|
|
116
|
+
integrity,
|
|
117
|
+
referrerPolicy,
|
|
118
|
+
} = {}) {
|
|
119
|
+
const editorOrigin = origin || originAlias || (bridgeOrigin ? "" : parheliaOrigin());
|
|
120
|
+
const src = resolveParheliaBridgeUrl(editorOrigin || bridgeOrigin);
|
|
121
|
+
if (!src) return null;
|
|
122
|
+
return {
|
|
123
|
+
id,
|
|
124
|
+
src,
|
|
125
|
+
async,
|
|
126
|
+
defer,
|
|
127
|
+
nonce,
|
|
128
|
+
crossOrigin,
|
|
129
|
+
integrity,
|
|
130
|
+
referrerPolicy,
|
|
131
|
+
"data-parhelia-editor-bridge": "runtime",
|
|
132
|
+
"data-parhelia-parent-origin": editorOrigin ? new URL(src).origin : undefined,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const sameOriginParent = () => {
|
|
137
|
+
try {
|
|
138
|
+
return window.parent.location.origin === window.location.origin;
|
|
139
|
+
} catch {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/** Whether this document should load the bridge: in a frame of another origin, on the editor's `?parhelia=1` URL. */
|
|
145
|
+
export function shouldLoadParheliaBridge(queryParam = "parhelia") {
|
|
146
|
+
if (typeof window === "undefined" || window.parent === window || sameOriginParent()) return false;
|
|
147
|
+
return shouldRenderParheliaBridgeScript({ queryParam });
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Adds the bridge's script to the document when the editor frames it directly (`shouldLoadParheliaBridge`) and the
|
|
152
|
+
* document has none yet. Answers whether it added one. Call it after the page's own markup is in place.
|
|
153
|
+
* @param {import("./bridge-core.js").ParheliaBridgeLoadOptions} [options]
|
|
154
|
+
*/
|
|
155
|
+
export function loadParheliaBridge({ queryParam = "parhelia", ...options } = {}) {
|
|
156
|
+
if (!shouldLoadParheliaBridge(queryParam)) return false;
|
|
157
|
+
if (document.querySelector("script[data-parhelia-editor-bridge]")) return false;
|
|
158
|
+
const props = parheliaBridgeScriptProps(options);
|
|
159
|
+
if (!props) return false;
|
|
160
|
+
const script = document.createElement("script");
|
|
161
|
+
script.src = props.src;
|
|
162
|
+
script.async = true;
|
|
163
|
+
if (props.id) script.id = props.id;
|
|
164
|
+
if (props.nonce) script.nonce = props.nonce;
|
|
165
|
+
if (props.crossOrigin !== undefined) script.crossOrigin = props.crossOrigin;
|
|
166
|
+
if (props.integrity) script.integrity = props.integrity;
|
|
167
|
+
if (props.referrerPolicy) script.referrerPolicy = props.referrerPolicy;
|
|
168
|
+
script.setAttribute("data-parhelia-editor-bridge", props["data-parhelia-editor-bridge"]);
|
|
169
|
+
const parent = props["data-parhelia-parent-origin"];
|
|
170
|
+
if (parent) script.setAttribute("data-parhelia-parent-origin", parent);
|
|
171
|
+
document.body.appendChild(script);
|
|
172
|
+
return true;
|
|
173
|
+
}
|