@conloca/cms-spa 0.2.1 → 0.2.3
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 +26 -0
- package/dist/spa/main.css +240 -4
- package/dist/ts/.build-inputs.json +178 -0
- package/dist/ts/App.d.ts +10 -2
- package/dist/ts/App.d.ts.map +1 -1
- package/dist/ts/App.js +20 -8
- package/dist/ts/App.js.map +1 -1
- package/dist/ts/CmsSpaApp.d.ts.map +1 -1
- package/dist/ts/CmsSpaApp.js +4 -1
- package/dist/ts/CmsSpaApp.js.map +1 -1
- package/dist/ts/components/CMSDashboard.d.ts.map +1 -1
- package/dist/ts/components/CMSDashboard.js +6 -2
- package/dist/ts/components/CMSDashboard.js.map +1 -1
- package/dist/ts/components/RouteErrorPage.d.ts +11 -0
- package/dist/ts/components/RouteErrorPage.d.ts.map +1 -0
- package/dist/ts/components/RouteErrorPage.js +44 -0
- package/dist/ts/components/RouteErrorPage.js.map +1 -0
- package/dist/ts/components/dashboard/RecentActivity.d.ts.map +1 -1
- package/dist/ts/components/dashboard/RecentActivity.js +2 -8
- package/dist/ts/components/dashboard/RecentActivity.js.map +1 -1
- package/dist/ts/components/dialogs/CreateDataDialog.js +2 -2
- package/dist/ts/components/dialogs/CreateDataDialog.js.map +1 -1
- package/dist/ts/components/dialogs/CreateFolderDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/CreateFolderDialog.js +2 -1
- package/dist/ts/components/dialogs/CreateFolderDialog.js.map +1 -1
- package/dist/ts/components/dialogs/CreatePageFlow.d.ts +13 -0
- package/dist/ts/components/dialogs/CreatePageFlow.d.ts.map +1 -0
- package/dist/ts/components/dialogs/CreatePageFlow.js +100 -0
- package/dist/ts/components/dialogs/CreatePageFlow.js.map +1 -0
- package/dist/ts/components/dialogs/DataPropertiesDialog.d.ts +1 -1
- package/dist/ts/components/dialogs/DataPropertiesDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/DataPropertiesDialog.js +2 -2
- package/dist/ts/components/dialogs/DataPropertiesDialog.js.map +1 -1
- package/dist/ts/components/dialogs/FragmentPropertiesDialog.d.ts +1 -1
- package/dist/ts/components/dialogs/FragmentPropertiesDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/FragmentPropertiesDialog.js +2 -2
- package/dist/ts/components/dialogs/FragmentPropertiesDialog.js.map +1 -1
- package/dist/ts/components/dialogs/PageMetadataDialog.d.ts +2 -12
- package/dist/ts/components/dialogs/PageMetadataDialog.d.ts.map +1 -1
- package/dist/ts/components/dialogs/PageMetadataDialog.js +3 -21
- package/dist/ts/components/dialogs/PageMetadataDialog.js.map +1 -1
- package/dist/ts/components/editor/CMSMDXEditor.d.ts.map +1 -1
- package/dist/ts/components/editor/CMSMDXEditor.js +46 -55
- package/dist/ts/components/editor/CMSMDXEditor.js.map +1 -1
- package/dist/ts/components/editor/DrawerItemOverride.d.ts +2 -0
- package/dist/ts/components/editor/DrawerItemOverride.d.ts.map +1 -1
- package/dist/ts/components/editor/DrawerItemOverride.js +4 -3
- package/dist/ts/components/editor/DrawerItemOverride.js.map +1 -1
- package/dist/ts/components/editor/PageEditor.d.ts +6 -7
- package/dist/ts/components/editor/PageEditor.d.ts.map +1 -1
- package/dist/ts/components/editor/PageEditor.js +124 -34
- package/dist/ts/components/editor/PageEditor.js.map +1 -1
- package/dist/ts/components/editor/PageEditorHeaderActions.js +1 -1
- package/dist/ts/components/editor/PageEditorHeaderActions.js.map +1 -1
- package/dist/ts/components/editor/PageEditorWrapper.d.ts.map +1 -1
- package/dist/ts/components/editor/PageEditorWrapper.js +48 -12
- package/dist/ts/components/editor/PageEditorWrapper.js.map +1 -1
- package/dist/ts/components/editor/draft-media-config.d.ts +22 -0
- package/dist/ts/components/editor/draft-media-config.d.ts.map +1 -0
- package/dist/ts/components/editor/draft-media-config.js +186 -0
- package/dist/ts/components/editor/draft-media-config.js.map +1 -0
- package/dist/ts/components/editor/preview-availability.d.ts +6 -5
- package/dist/ts/components/editor/preview-availability.d.ts.map +1 -1
- package/dist/ts/components/editor/preview-availability.js +6 -5
- package/dist/ts/components/editor/preview-availability.js.map +1 -1
- package/dist/ts/components/editors/DataEditor.d.ts +2 -2
- package/dist/ts/components/editors/DataEditor.d.ts.map +1 -1
- package/dist/ts/components/forms/HintForm.js +8 -5
- package/dist/ts/components/forms/HintForm.js.map +1 -1
- package/dist/ts/components/forms/SchemaForm.d.ts +17 -5
- package/dist/ts/components/forms/SchemaForm.d.ts.map +1 -1
- package/dist/ts/components/forms/SchemaForm.js +16 -9
- package/dist/ts/components/forms/SchemaForm.js.map +1 -1
- package/dist/ts/components/media/AssetCard.d.ts.map +1 -1
- package/dist/ts/components/media/AssetCard.js +3 -2
- package/dist/ts/components/media/AssetCard.js.map +1 -1
- package/dist/ts/components/media/AssetDetailSidebar.d.ts.map +1 -1
- package/dist/ts/components/media/AssetDetailSidebar.js +4 -2
- package/dist/ts/components/media/AssetDetailSidebar.js.map +1 -1
- package/dist/ts/components/media/FolderTreeSidebar.d.ts.map +1 -1
- package/dist/ts/components/media/FolderTreeSidebar.js +4 -1
- package/dist/ts/components/media/FolderTreeSidebar.js.map +1 -1
- package/dist/ts/components/pages/PageList.d.ts.map +1 -1
- package/dist/ts/components/pages/PageList.js +13 -99
- package/dist/ts/components/pages/PageList.js.map +1 -1
- package/dist/ts/components/sandbox/ContentSandbox.d.ts.map +1 -1
- package/dist/ts/components/sandbox/ContentSandbox.js +36 -5
- package/dist/ts/components/sandbox/ContentSandbox.js.map +1 -1
- package/dist/ts/content-form-fields.d.ts +41 -0
- package/dist/ts/content-form-fields.d.ts.map +1 -0
- package/dist/ts/content-form-fields.js +70 -0
- package/dist/ts/content-form-fields.js.map +1 -0
- package/dist/ts/content-wrapper.d.ts +65 -0
- package/dist/ts/content-wrapper.d.ts.map +1 -0
- package/dist/ts/content-wrapper.js +190 -0
- package/dist/ts/content-wrapper.js.map +1 -0
- package/dist/ts/data-schemas.d.ts +4 -3
- package/dist/ts/data-schemas.d.ts.map +1 -1
- package/dist/ts/data-schemas.js.map +1 -1
- package/dist/ts/frame/channel.d.ts +40 -0
- package/dist/ts/frame/channel.d.ts.map +1 -0
- package/dist/ts/frame/channel.js +233 -0
- package/dist/ts/frame/channel.js.map +1 -0
- package/dist/ts/frame/editor-frame.d.ts +23 -0
- package/dist/ts/frame/editor-frame.d.ts.map +1 -0
- package/dist/ts/frame/editor-frame.js +154 -0
- package/dist/ts/frame/editor-frame.js.map +1 -0
- package/dist/ts/frame/media-cache.d.ts +26 -0
- package/dist/ts/frame/media-cache.d.ts.map +1 -0
- package/dist/ts/frame/media-cache.js +123 -0
- package/dist/ts/frame/media-cache.js.map +1 -0
- package/dist/ts/frame/memory-router.d.ts +10 -0
- package/dist/ts/frame/memory-router.d.ts.map +1 -0
- package/dist/ts/frame/memory-router.js +39 -0
- package/dist/ts/frame/memory-router.js.map +1 -0
- package/dist/ts/frame/parent-hello.d.ts +11 -0
- package/dist/ts/frame/parent-hello.d.ts.map +1 -0
- package/dist/ts/frame/parent-hello.js +53 -0
- package/dist/ts/frame/parent-hello.js.map +1 -0
- package/dist/ts/frame/protocol.d.ts +201 -0
- package/dist/ts/frame/protocol.d.ts.map +1 -0
- package/dist/ts/frame/protocol.js +195 -0
- package/dist/ts/frame/protocol.js.map +1 -0
- package/dist/ts/frame/rpc-content-client.d.ts +10 -0
- package/dist/ts/frame/rpc-content-client.d.ts.map +1 -0
- package/dist/ts/frame/rpc-content-client.js +51 -0
- package/dist/ts/frame/rpc-content-client.js.map +1 -0
- package/dist/ts/frame/save-shortcut.d.ts +8 -0
- package/dist/ts/frame/save-shortcut.d.ts.map +1 -0
- package/dist/ts/frame/save-shortcut.js +26 -0
- package/dist/ts/frame/save-shortcut.js.map +1 -0
- package/dist/ts/host-schema.d.ts +73 -0
- package/dist/ts/host-schema.d.ts.map +1 -0
- package/dist/ts/host-schema.js +66 -0
- package/dist/ts/host-schema.js.map +1 -0
- package/dist/ts/host-style-probe.d.ts +32 -37
- package/dist/ts/host-style-probe.d.ts.map +1 -1
- package/dist/ts/host-style-probe.js +102 -68
- package/dist/ts/host-style-probe.js.map +1 -1
- package/dist/ts/index.d.ts +4 -2
- package/dist/ts/index.d.ts.map +1 -1
- package/dist/ts/index.js +6 -2
- package/dist/ts/index.js.map +1 -1
- package/dist/ts/main-spa.d.ts.map +1 -1
- package/dist/ts/main-spa.js +2 -1
- package/dist/ts/main-spa.js.map +1 -1
- package/dist/ts/page-schema-define.d.ts +14 -12
- package/dist/ts/page-schema-define.d.ts.map +1 -1
- package/dist/ts/page-schema-define.js +8 -5
- package/dist/ts/page-schema-define.js.map +1 -1
- package/dist/ts/prose-surface-css.d.ts +3 -5
- package/dist/ts/prose-surface-css.d.ts.map +1 -1
- package/dist/ts/prose-surface-css.js +3 -5
- package/dist/ts/prose-surface-css.js.map +1 -1
- package/dist/ts/site-styles.d.ts +4 -21
- package/dist/ts/site-styles.d.ts.map +1 -1
- package/dist/ts/site-styles.js.map +1 -1
- package/dist/ts/ui-config.d.ts +52 -1
- package/dist/ts/ui-config.d.ts.map +1 -1
- package/dist/ts/ui-config.js +4 -0
- package/dist/ts/ui-config.js.map +1 -1
- package/dist/ts/utils/asset-url.d.ts +2 -0
- package/dist/ts/utils/asset-url.d.ts.map +1 -1
- package/dist/ts/utils/asset-url.js +7 -0
- package/dist/ts/utils/asset-url.js.map +1 -1
- package/dist/ts/utils/pageMetadata.d.ts.map +1 -1
- package/dist/ts/utils/pageMetadata.js +27 -10
- package/dist/ts/utils/pageMetadata.js.map +1 -1
- package/dist/ts/utils/schema-fields.d.ts +23 -0
- package/dist/ts/utils/schema-fields.d.ts.map +1 -0
- package/dist/ts/utils/schema-fields.js +82 -0
- package/dist/ts/utils/schema-fields.js.map +1 -0
- package/package.json +56 -18
- package/src/App.tsx +35 -14
- package/src/CmsSpaApp.tsx +5 -1
- package/src/components/CMSDashboard.tsx +12 -4
- package/src/components/RouteErrorPage.tsx +75 -0
- package/src/components/dashboard/RecentActivity.tsx +3 -7
- package/src/components/dialogs/CreateDataDialog.tsx +2 -2
- package/src/components/dialogs/CreateFolderDialog.tsx +2 -1
- package/src/components/dialogs/CreatePageFlow.tsx +117 -0
- package/src/components/dialogs/DataPropertiesDialog.tsx +3 -2
- package/src/components/dialogs/FragmentPropertiesDialog.tsx +3 -2
- package/src/components/dialogs/PageMetadataDialog.tsx +5 -28
- package/src/components/editor/CMSMDXEditor.tsx +49 -69
- package/src/components/editor/DrawerItemOverride.tsx +14 -8
- package/src/components/editor/PageEditor.tsx +135 -36
- package/src/components/editor/PageEditorHeaderActions.tsx +1 -1
- package/src/components/editor/PageEditorWrapper.tsx +51 -12
- package/src/components/editor/draft-media-config.ts +197 -0
- package/src/components/editor/preview-availability.ts +6 -5
- package/src/components/editors/DataEditor.tsx +2 -2
- package/src/components/forms/HintForm.tsx +7 -5
- package/src/components/forms/SchemaForm.tsx +41 -12
- package/src/components/media/AssetCard.tsx +10 -7
- package/src/components/media/AssetDetailSidebar.tsx +6 -6
- package/src/components/media/FolderTreeSidebar.tsx +3 -1
- package/src/components/pages/PageList.tsx +17 -119
- package/src/components/sandbox/ContentSandbox.tsx +34 -5
- package/src/content-form-fields.ts +79 -0
- package/src/content-wrapper.ts +284 -0
- package/src/data-schemas.ts +4 -3
- package/src/frame/channel.ts +258 -0
- package/src/frame/editor-frame.ts +180 -0
- package/src/frame/media-cache.ts +144 -0
- package/src/frame/memory-router.ts +39 -0
- package/src/frame/parent-hello.ts +62 -0
- package/src/frame/protocol.ts +323 -0
- package/src/frame/rpc-content-client.ts +58 -0
- package/src/frame/save-shortcut.ts +25 -0
- package/src/host-schema.ts +122 -0
- package/src/host-style-probe.ts +120 -90
- package/src/index.ts +7 -2
- package/src/main-spa.tsx +2 -0
- package/src/main.css +12 -8
- package/src/page-schema-define.ts +29 -17
- package/src/prose-surface-css.ts +3 -5
- package/src/site-styles.ts +4 -22
- package/src/ui-config.ts +54 -1
- package/src/utils/asset-url.ts +8 -0
- package/src/utils/pageMetadata.ts +27 -10
- package/src/utils/schema-fields.ts +106 -0
- package/dist/ts/host-discovery.d.ts +0 -40
- package/dist/ts/host-discovery.d.ts.map +0 -1
- package/dist/ts/host-discovery.js +0 -39
- package/dist/ts/host-discovery.js.map +0 -1
- package/dist/ts/utils/zodIntrospect.d.ts +0 -19
- package/dist/ts/utils/zodIntrospect.d.ts.map +0 -1
- package/dist/ts/utils/zodIntrospect.js +0 -95
- package/dist/ts/utils/zodIntrospect.js.map +0 -1
- package/src/host-discovery.ts +0 -77
- package/src/utils/zodIntrospect.ts +0 -136
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Form definitions for the metadata the CMS itself owns.
|
|
3
|
+
*
|
|
4
|
+
* These were Zod objects built only so a Zod introspector could read a
|
|
5
|
+
* {@link FormFields} record back out of them — the schemas were never parsed
|
|
6
|
+
* with. They are the records directly now; validation of the same metadata is
|
|
7
|
+
* the generated validators in `@conloca/content-api/schemas`.
|
|
8
|
+
*
|
|
9
|
+
* Every entry below is pinned as a literal table in
|
|
10
|
+
* `test/content-form-fields.test.ts`, which also checks each field's
|
|
11
|
+
* type/optional/nullable against the Typia-generated metadata of the matching
|
|
12
|
+
* content-api type. Each record keeps its literal keys (`satisfies`), so a
|
|
13
|
+
* dialog can prove at compile time which fields it renders.
|
|
14
|
+
*/
|
|
15
|
+
const requiredString = (description) => ({
|
|
16
|
+
type: 'string',
|
|
17
|
+
description,
|
|
18
|
+
isOptional: false,
|
|
19
|
+
isNullable: false,
|
|
20
|
+
});
|
|
21
|
+
const optionalString = (description) => ({
|
|
22
|
+
type: 'string',
|
|
23
|
+
description,
|
|
24
|
+
isOptional: true,
|
|
25
|
+
isNullable: false,
|
|
26
|
+
});
|
|
27
|
+
const optionalStringArray = (description) => ({
|
|
28
|
+
type: 'array',
|
|
29
|
+
description,
|
|
30
|
+
isOptional: true,
|
|
31
|
+
isNullable: false,
|
|
32
|
+
});
|
|
33
|
+
const optionalNullableDate = (description) => ({
|
|
34
|
+
type: 'date',
|
|
35
|
+
format: 'date-time',
|
|
36
|
+
description,
|
|
37
|
+
isOptional: true,
|
|
38
|
+
isNullable: true,
|
|
39
|
+
});
|
|
40
|
+
/** Pathname — the page-settings core section in every mode (the dialog renders the title itself). */
|
|
41
|
+
export const pathnameOnlyFields = {
|
|
42
|
+
pathname: requiredString('URL path (e.g., /about)'),
|
|
43
|
+
};
|
|
44
|
+
/** Publishing window only — the page-settings trailing section in `minimal` mode. */
|
|
45
|
+
export const publishWindowFields = {
|
|
46
|
+
publishAt: optionalNullableDate('When the page goes live. Page lists that sort by date use this date, or the date the page was created when it is empty.'),
|
|
47
|
+
unpublishAt: optionalNullableDate('Schedule unpublish date/time'),
|
|
48
|
+
};
|
|
49
|
+
/** SEO + publishing window — the page-settings trailing section in `full` mode. */
|
|
50
|
+
export const seoPublishingFields = {
|
|
51
|
+
description: optionalString('Meta description for search engines'),
|
|
52
|
+
robots: optionalString('Robots meta tag (e.g., index, follow)'),
|
|
53
|
+
canonical: { ...optionalString('Canonical URL for duplicate content'), format: 'url' },
|
|
54
|
+
...publishWindowFields,
|
|
55
|
+
};
|
|
56
|
+
/** Editable fragment metadata — replaces `fragmentEditableSchema`. */
|
|
57
|
+
export const fragmentEditableFields = {
|
|
58
|
+
title: requiredString('Display name for the fragment'),
|
|
59
|
+
description: optionalString('Brief description of this fragment'),
|
|
60
|
+
category: optionalString('Category for organizing fragments (e.g., headers, cta, content)'),
|
|
61
|
+
tags: optionalStringArray('Tags for filtering and organization'),
|
|
62
|
+
};
|
|
63
|
+
/** Editable data-entry metadata — replaces `dataEditableSchema`. */
|
|
64
|
+
export const dataEditableFields = {
|
|
65
|
+
title: requiredString('Display name for the entry'),
|
|
66
|
+
description: optionalString('Brief description of this entry'),
|
|
67
|
+
category: optionalString('Category for organizing entries'),
|
|
68
|
+
tags: optionalStringArray('Tags for filtering and organization'),
|
|
69
|
+
};
|
|
70
|
+
//# sourceMappingURL=content-form-fields.js.map
|
|
@@ -0,0 +1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AE,GAAG,cAAc,CAAC,qCAAqC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE;IACtF,GAAG,mBAAmB;CACF,CAAC;AAEvB,sEAAsE;AACtE,MAAM,CAAC,MAAM,sBAAsB,GAAG;IACpC,KAAK,EAAE,cAAc,CAAC,+BAA+B,CAAC;IACtD,WAAW,EAAE,cAAc,CAAC,oCAAoC,CAAC;IACjE,QAAQ,EAAE,cAAc,CAAC,iEAAiE,CAAC;IAC3F,IAAI,EAAE,mBAAmB,CAAC,qCAAqC,CAAC;CAC5C,CAAC;AAEvB,oEAAoE;AACpE,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,KAAK,EAAE,cAAc,CAAC,4BAA4B,CAAC;IACnD,WAAW,EAAE,cAAc,CAAC,iCAAiC,CAAC;IAC9D,QAAQ,EAAE,cAAc,CAAC,iCAAiC,CAAC;IAC3D,IAAI,EAAE,mBAAmB,CAAC,qCAAqC,CAAC;CAC5C,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content-wrapper discovery from a page's HTML. The site's dev endpoint runs it
|
|
3
|
+
* on the HTML it serves; the host-style probe runs it on the page it loaded,
|
|
4
|
+
* which is the only discovery an editor without that endpoint has.
|
|
5
|
+
*
|
|
6
|
+
* Returned shape:
|
|
7
|
+
*
|
|
8
|
+
* {
|
|
9
|
+
* content: { tagName, className } | null,
|
|
10
|
+
* codeBlock: { tagName, className }[] | null,
|
|
11
|
+
* }
|
|
12
|
+
*
|
|
13
|
+
* - `content` is the wrapper for prose (h1-h6, p, lists, etc.). The
|
|
14
|
+
* editor wraps its contenteditable in a clone so the host's prose
|
|
15
|
+
* CSS reaches via the cascade.
|
|
16
|
+
* - `codeBlock` is the ORDERED chain of classed wrappers around the
|
|
17
|
+
* host's `<pre>`, outermost first. For Starlight's expressive-code:
|
|
18
|
+
* `[{ div, 'expressive-code' }, { figure, 'frame has-title …' }]`.
|
|
19
|
+
* The editor's CodeMirror frame renders each link as a nested
|
|
20
|
+
* element (outer→inner) so the host's code-block CSS reaches via
|
|
21
|
+
* the cascade including descendant selectors (`.expressive-code
|
|
22
|
+
* .frame .header`, etc.) — not just single-class targets.
|
|
23
|
+
*
|
|
24
|
+
* Discovery priorities (content):
|
|
25
|
+
*
|
|
26
|
+
* 1. `[data-conloca-content-root]` — explicit opt-in.
|
|
27
|
+
* 2. The smallest classed wrapper inside `<main>` containing the
|
|
28
|
+
* majority of prose tags.
|
|
29
|
+
* 3. `<main>` itself.
|
|
30
|
+
* 4. `null`.
|
|
31
|
+
*
|
|
32
|
+
* Discovery (codeBlock): walk up from a `<pre>` inside `<main>` and
|
|
33
|
+
* collect classed ancestors between the content wrapper and `<pre>`,
|
|
34
|
+
* preserving their nesting order. Returns null when no `<pre>` exists,
|
|
35
|
+
* or when there are no classed wrappers between the prose wrapper and
|
|
36
|
+
* the `<pre>`.
|
|
37
|
+
*/
|
|
38
|
+
export interface ContentWrapperInfo {
|
|
39
|
+
/** Lower-case HTML tag name as it appeared in source, eg `'article'`. */
|
|
40
|
+
tagName: string;
|
|
41
|
+
/** Space-separated class list from the source element. Preserved
|
|
42
|
+
* verbatim so Astro-scoped hashes (`astro-e3flfouy`) survive — host
|
|
43
|
+
* scoped CSS only matches when those hashes are present on the
|
|
44
|
+
* element. */
|
|
45
|
+
className: string;
|
|
46
|
+
}
|
|
47
|
+
export interface DiscoveredWrappers {
|
|
48
|
+
/** The element wrapping the page's prose content (h1-h6, p, etc.).
|
|
49
|
+
* For Starlight: `.sl-markdown-content`. For Tailwind Typography:
|
|
50
|
+
* `.prose`. Null when no prose wrapper exists on the page. */
|
|
51
|
+
content: ContentWrapperInfo | null;
|
|
52
|
+
/** Ordered chain of classed wrappers between the content wrapper
|
|
53
|
+
* and the `<pre>`, outermost first. For Starlight:
|
|
54
|
+
* `[{ div, 'expressive-code' }, { figure, 'frame has-title …' }]`.
|
|
55
|
+
* Null when the page has no code blocks or no classed wrappers
|
|
56
|
+
* between the prose wrapper and the `<pre>`. */
|
|
57
|
+
codeBlock: ContentWrapperInfo[] | null;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Locate the host's content + code-block wrappers in raw HTML. Single-
|
|
61
|
+
* pass regex walk with a stack so we can recover ancestor chains for
|
|
62
|
+
* both prose tags and `<pre>` tags without a full DOM parser dep.
|
|
63
|
+
*/
|
|
64
|
+
export declare function findContentWrapper(html: string): DiscoveredWrappers;
|
|
65
|
+
//# sourceMappingURL=content-wrapper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content-wrapper.d.ts","sourceRoot":"","sources":["../../src/content-wrapper.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,MAAM,WAAW,kBAAkB;IACjC,yEAAyE;IACzE,OAAO,EAAE,MAAM,CAAC;IAChB;;;kBAGc;IACd,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,kBAAkB;IACjC;;kEAE8D;IAC9D,OAAO,EAAE,kBAAkB,GAAG,IAAI,CAAC;IACnC;;;;oDAIgD;IAChD,SAAS,EAAE,kBAAkB,EAAE,GAAG,IAAI,CAAC;CACxC;AAuCD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,kBAAkB,CAsEnE"}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/** Tags whose presence signals "this element holds prose content."
|
|
2
|
+
* We walk up from the first such element we find inside `<main>` to
|
|
3
|
+
* locate the wrapper. Headings + paragraphs cover every prose-styling
|
|
4
|
+
* convention we've seen — every CSS framework scopes typography
|
|
5
|
+
* through descendant selectors on `h1`–`h6` and `p`. */
|
|
6
|
+
const PROSE_TAGS = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p']);
|
|
7
|
+
/** Tag that anchors code-block wrapper discovery. The host's code-
|
|
8
|
+
* block library (Starlight's expressive-code, Tailwind's `pre`
|
|
9
|
+
* styling, etc.) always renders a `<pre>` containing the code,
|
|
10
|
+
* often wrapped in additional classed elements. */
|
|
11
|
+
const CODE_BLOCK_ANCHOR = 'pre';
|
|
12
|
+
/** HTML5 void elements — never push onto the open-tag stack since they
|
|
13
|
+
* have no closing tag. */
|
|
14
|
+
const VOID_TAGS = new Set([
|
|
15
|
+
'area',
|
|
16
|
+
'base',
|
|
17
|
+
'br',
|
|
18
|
+
'col',
|
|
19
|
+
'embed',
|
|
20
|
+
'hr',
|
|
21
|
+
'img',
|
|
22
|
+
'input',
|
|
23
|
+
'link',
|
|
24
|
+
'meta',
|
|
25
|
+
'source',
|
|
26
|
+
'track',
|
|
27
|
+
'wbr',
|
|
28
|
+
]);
|
|
29
|
+
/**
|
|
30
|
+
* Locate the host's content + code-block wrappers in raw HTML. Single-
|
|
31
|
+
* pass regex walk with a stack so we can recover ancestor chains for
|
|
32
|
+
* both prose tags and `<pre>` tags without a full DOM parser dep.
|
|
33
|
+
*/
|
|
34
|
+
export function findContentWrapper(html) {
|
|
35
|
+
// Strip comments and scripts so opening-tag-shaped strings inside
|
|
36
|
+
// them don't pollute the walk.
|
|
37
|
+
const cleaned = html
|
|
38
|
+
.replace(/<!--[\s\S]*?-->/g, '')
|
|
39
|
+
.replace(/<script[\s\S]*?<\/script>/gi, '')
|
|
40
|
+
.replace(/<style[\s\S]*?<\/style>/gi, '');
|
|
41
|
+
const tagRe = /<(\/?)([a-zA-Z][a-zA-Z0-9-]*)((?:\s+[^>]*)?)\s*(\/?)>/g;
|
|
42
|
+
const stack = [];
|
|
43
|
+
let explicit = null;
|
|
44
|
+
let mainEl = null;
|
|
45
|
+
const proseAncestorChains = [];
|
|
46
|
+
const preAncestorChains = [];
|
|
47
|
+
for (const match of cleaned.matchAll(tagRe)) {
|
|
48
|
+
const isClose = match[1] === '/';
|
|
49
|
+
const tagName = match[2].toLowerCase();
|
|
50
|
+
const attrBlob = match[3] ?? '';
|
|
51
|
+
const explicitlySelfClosing = match[4] === '/';
|
|
52
|
+
if (isClose) {
|
|
53
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
54
|
+
if (stack[i].tag === tagName) {
|
|
55
|
+
stack.length = i;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const attrs = parseAttributes(attrBlob);
|
|
62
|
+
const className = attrs.get('class') ?? '';
|
|
63
|
+
const el = { tag: tagName, className, attrs };
|
|
64
|
+
// Priority 1 (content only): explicit opt-in. Capture and keep
|
|
65
|
+
// walking — we still need to discover the code-block wrapper.
|
|
66
|
+
if (!explicit && attrs.has('data-conloca-content-root')) {
|
|
67
|
+
explicit = { tagName, className };
|
|
68
|
+
}
|
|
69
|
+
if (!mainEl && tagName === 'main') {
|
|
70
|
+
mainEl = el;
|
|
71
|
+
}
|
|
72
|
+
if (mainEl && PROSE_TAGS.has(tagName)) {
|
|
73
|
+
const mainIdx = stack.indexOf(mainEl);
|
|
74
|
+
if (mainIdx >= 0) {
|
|
75
|
+
proseAncestorChains.push(stack.slice(mainIdx + 1));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (mainEl && tagName === CODE_BLOCK_ANCHOR) {
|
|
79
|
+
const mainIdx = stack.indexOf(mainEl);
|
|
80
|
+
if (mainIdx >= 0) {
|
|
81
|
+
preAncestorChains.push(stack.slice(mainIdx + 1));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const isVoid = VOID_TAGS.has(tagName);
|
|
85
|
+
if (!isVoid && !explicitlySelfClosing) {
|
|
86
|
+
stack.push(el);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const content = resolveContentWrapper(explicit, mainEl, proseAncestorChains);
|
|
90
|
+
const codeBlock = resolveCodeBlockWrapper(content, preAncestorChains);
|
|
91
|
+
return { content, codeBlock };
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Pick the content wrapper from the walk's collected data.
|
|
95
|
+
*
|
|
96
|
+
* Priority: explicit opt-in > majority-prose-wrapper-inside-main >
|
|
97
|
+
* `<main>` fallback > null.
|
|
98
|
+
*/
|
|
99
|
+
function resolveContentWrapper(explicit, mainEl, proseAncestorChains) {
|
|
100
|
+
if (explicit)
|
|
101
|
+
return explicit;
|
|
102
|
+
if (!mainEl)
|
|
103
|
+
return null;
|
|
104
|
+
if (proseAncestorChains.length === 0) {
|
|
105
|
+
return { tagName: 'main', className: mainEl.className };
|
|
106
|
+
}
|
|
107
|
+
// Tally classed-ancestor occurrences across all prose chains.
|
|
108
|
+
const scores = new Map();
|
|
109
|
+
for (const chain of proseAncestorChains) {
|
|
110
|
+
for (const ancestor of chain) {
|
|
111
|
+
if (!ancestor.className)
|
|
112
|
+
continue;
|
|
113
|
+
scores.set(ancestor, (scores.get(ancestor) ?? 0) + 1);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// Majority threshold rules out partial-coverage classed ancestors
|
|
117
|
+
// (single Cards holding one of many prose blocks).
|
|
118
|
+
const threshold = proseAncestorChains.length / 2;
|
|
119
|
+
const candidates = [...scores.entries()].filter(([, count]) => count > threshold);
|
|
120
|
+
if (candidates.length === 0) {
|
|
121
|
+
return { tagName: 'main', className: mainEl.className };
|
|
122
|
+
}
|
|
123
|
+
// Tie-break by depth: the candidate that appears deepest in any
|
|
124
|
+
// chain is the most specific (closest to the prose tags).
|
|
125
|
+
const maxChainIndex = (el) => Math.max(...proseAncestorChains.map((c) => c.indexOf(el)));
|
|
126
|
+
candidates.sort((a, b) => {
|
|
127
|
+
if (b[1] !== a[1])
|
|
128
|
+
return b[1] - a[1];
|
|
129
|
+
return maxChainIndex(b[0]) - maxChainIndex(a[0]);
|
|
130
|
+
});
|
|
131
|
+
const winner = candidates[0][0];
|
|
132
|
+
return { tagName: winner.tag, className: winner.className };
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Derive the code-block wrapper chain from a representative `<pre>`'s
|
|
136
|
+
* ancestor chain. We strip everything at or above the content wrapper
|
|
137
|
+
* (its styles are already handled by the content-wrapper mirror) and
|
|
138
|
+
* return the remaining classed elements in nesting order, outermost
|
|
139
|
+
* first.
|
|
140
|
+
*
|
|
141
|
+
* Returning a chain (instead of one collapsed element) preserves the
|
|
142
|
+
* shape host CSS expects:
|
|
143
|
+
*
|
|
144
|
+
* - Single-class selectors (`.frame { ... }`) hit on whichever link
|
|
145
|
+
* carries that class.
|
|
146
|
+
* - Descendant selectors (`.expressive-code .frame { ... }`,
|
|
147
|
+
* `.frame .header > .title`) hit because the editor's renderer
|
|
148
|
+
* materialises the chain as nested elements with the host's
|
|
149
|
+
* expected tag names.
|
|
150
|
+
*
|
|
151
|
+
* The editor's code-block frame component (`code-block-frame.tsx` in
|
|
152
|
+
* `@conloca/mdx`) treats the LAST link as the "frame" element (where
|
|
153
|
+
* figcaption/pre/copy children render) and wraps it with the earlier
|
|
154
|
+
* links from inside out. This matches the host's authoring shape for
|
|
155
|
+
* libraries like ExpressiveCode (`.expressive-code > figure.frame >
|
|
156
|
+
* figcaption + pre + .copy`).
|
|
157
|
+
*/
|
|
158
|
+
function resolveCodeBlockWrapper(content, preAncestorChains) {
|
|
159
|
+
if (preAncestorChains.length === 0)
|
|
160
|
+
return null;
|
|
161
|
+
// Pick the first <pre>'s chain as representative. Code blocks on a
|
|
162
|
+
// single page are uniformly wrapped, so chains are interchangeable.
|
|
163
|
+
const chain = preAncestorChains[0];
|
|
164
|
+
// Strip everything at-or-above the content wrapper; the rest is
|
|
165
|
+
// code-block-specific chrome that this endpoint is responsible for.
|
|
166
|
+
const contentIdx = content
|
|
167
|
+
? chain.findIndex((el) => el.tag === content.tagName && el.className === content.className)
|
|
168
|
+
: -1;
|
|
169
|
+
const codeBlockChain = chain.slice(contentIdx + 1);
|
|
170
|
+
const classedAncestors = codeBlockChain.filter((el) => el.className);
|
|
171
|
+
if (classedAncestors.length === 0)
|
|
172
|
+
return null;
|
|
173
|
+
return classedAncestors.map((el) => ({ tagName: el.tag, className: el.className }));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Parse the attribute blob inside an opening tag. Handles `name="v"`,
|
|
177
|
+
* `name='v'`, `name=v`, and boolean (no-value) attributes. Doesn't
|
|
178
|
+
* handle entity decoding (`&` → `&`) since the values we read
|
|
179
|
+
* (tag name, class list, the marker attribute) don't carry entities
|
|
180
|
+
* in practice.
|
|
181
|
+
*/
|
|
182
|
+
function parseAttributes(s) {
|
|
183
|
+
const out = new Map();
|
|
184
|
+
const re = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/g;
|
|
185
|
+
for (const match of s.matchAll(re)) {
|
|
186
|
+
out.set(match[1], match[2] ?? match[3] ?? match[4] ?? '');
|
|
187
|
+
}
|
|
188
|
+
return out;
|
|
189
|
+
}
|
|
190
|
+
//# sourceMappingURL=content-wrapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content-wrapper.js","sourceRoot":"","sources":["../../src/content-wrapper.ts"],"names":[],"mappings":"AA4DA;;;;wDAIwD;AACxD,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC;AAEtE;;;mDAGmD;AACnD,MAAM,iBAAiB,GAAG,KAAK,CAAC;AAEhC;0BAC0B;AAC1B,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC;IACxB,MAAM;IACN,MAAM;IACN,IAAI;IACJ,KAAK;IACL,OAAO;IACP,IAAI;IACJ,KAAK;IACL,OAAO;IACP,MAAM;IACN,MAAM;IACN,QAAQ;IACR,OAAO;IACP,KAAK;CACN,CAAC,CAAC;AAQH;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,kEAAkE;IAClE,+BAA+B;IAC/B,MAAM,OAAO,GAAG,I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AAC,KAAK,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;IAEnD,MAAM,gBAAgB,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC;IACrE,IAAI,gBAAgB,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAE/C,OAAO,gBAAgB,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC,GAAG,EAAE,SAAS,EAAE,EAAE,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;AACtF,CAAC;AAED;;;;;;GAMG;AACH,SAAS,eAAe,CAAC,CAAS;IAChC,MAAM,GAAG,GAAG,IAAI,GAAG,EAAkB,CAAC;IACtC,MAAM,EAAE,GAAG,wEAAwE,CAAC;IACpF,KAAK,MAAM,KAAK,IAAI,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;QACnC,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5D,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { HostSchema } from './host-schema.js';
|
|
2
2
|
/**
|
|
3
3
|
* Type for a collection of data schemas.
|
|
4
|
-
* Each key is a collection name, value is the
|
|
4
|
+
* Each key is a collection name, value is the {@link HostSchema} (generated
|
|
5
|
+
* Typia validator + JSON Schema metadata) for that collection's data payload.
|
|
5
6
|
*/
|
|
6
|
-
export type DataSchemas = Record<string,
|
|
7
|
+
export type DataSchemas = Record<string, HostSchema>;
|
|
7
8
|
/**
|
|
8
9
|
* Shared state for data schemas across module instances.
|
|
9
10
|
* Uses window to ensure virtual modules and bundled code share the same state.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-schemas.d.ts","sourceRoot":"","sources":["../../src/data-schemas.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"data-schemas.d.ts","sourceRoot":"","sources":["../../src/data-schemas.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAErD;;;GAGG;AAEH,UAAU,eAAe;IACvB,OAAO,EAAE,WAAW,CAAC;IACrB,WAAW,EAAE,GAAG,CAAC,CAAC,OAAO,EAAE,WAAW,KAAK,IAAI,CAAC,CAAC;CAClD;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,sBAAsB,CAAC,EAAE,eAAe,CAAC;KAC1C;CACF;AAgBD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,WAAW,GAAG,IAAI,CAMzD;AAED;;GAEG;AACH,wBAAgB,cAAc,IAAI,WAAW,CAE5C;AAED;;;GAGG;AACH,wBAAgB,cAAc,IAAI,WAAW,CAkB5C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-schemas.js","sourceRoot":"","sources":["../../src/data-schemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"data-schemas.js","sourceRoot":"","sources":["../../src/data-schemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AA0B5C,MAAM,cAAc,GAAG,GAAoB,EAAE;IAC3C,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,IAAI,CAAC,MAAM,CAAC,sBA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,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type ControlMessage, type RpcErrorCode, type WireError } from './protocol.js';
|
|
2
|
+
/** The part of a `MessagePort` the channel uses, so tests can hand it a fake. */
|
|
3
|
+
export type FramePort = Pick<MessagePort, 'postMessage' | 'addEventListener' | 'removeEventListener' | 'start' | 'close'>;
|
|
4
|
+
/** An error the channel itself raises, such as a call the other side does not answer. */
|
|
5
|
+
export declare class RpcError extends Error {
|
|
6
|
+
readonly code: RpcErrorCode;
|
|
7
|
+
constructor(message: string, code: RpcErrorCode);
|
|
8
|
+
}
|
|
9
|
+
/** Turns anything thrown into a cloneable description the other side rebuilds with {@link decodeWireError}. */
|
|
10
|
+
export declare function encodeWireError(error: unknown): WireError;
|
|
11
|
+
/** Rebuilds the error class {@link encodeWireError} described, so `instanceof` checks keep working. */
|
|
12
|
+
export declare function decodeWireError(wire: WireError): Error;
|
|
13
|
+
/**
|
|
14
|
+
* Swaps every `Error` inside a value for `{ __wireError }`, walking only arrays and plain objects, so a Blob or File
|
|
15
|
+
* travels untouched. Structured cloning would otherwise flatten the errors' classes.
|
|
16
|
+
* shortcut: no cycle detection, values are content-API results; add a seen-set if a cyclic value ever crosses.
|
|
17
|
+
*/
|
|
18
|
+
export declare function encodeWireValue(value: unknown): unknown;
|
|
19
|
+
/** Reverses {@link encodeWireValue}. */
|
|
20
|
+
export declare function decodeWireValue(value: unknown): unknown;
|
|
21
|
+
export interface FrameChannelHandlers {
|
|
22
|
+
/** Answers a call from the other side; without it every call answers `method_not_allowed`. */
|
|
23
|
+
call?(method: string, args: unknown[]): Promise<unknown>;
|
|
24
|
+
event?(name: string, payload: unknown): void;
|
|
25
|
+
control?(message: ControlMessage): void;
|
|
26
|
+
/** Receives every message whose shape the channel does not recognise. */
|
|
27
|
+
malformed?(data: unknown): void;
|
|
28
|
+
/** Receives the error of an `emit` or `post` the port could not send; without it the error goes to `console.error`. */
|
|
29
|
+
sendFailed?(error: unknown): void;
|
|
30
|
+
}
|
|
31
|
+
export interface FrameChannel {
|
|
32
|
+
call(method: string, args?: unknown[]): Promise<unknown>;
|
|
33
|
+
emit(name: string, payload?: unknown): void;
|
|
34
|
+
post(message: ControlMessage): void;
|
|
35
|
+
/** Stops listening and closes the port; calls still waiting for a result never settle. */
|
|
36
|
+
close(): void;
|
|
37
|
+
}
|
|
38
|
+
/** Carries calls, results, events and control messages over one port between the editor frame and its parent. */
|
|
39
|
+
export declare function openFrameChannel(port: FramePort, handlers: FrameChannelHandlers): FrameChannel;
|
|
40
|
+
//# sourceMappingURL=channel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../../../src/frame/channel.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,cAAc,EAGnB,KAAK,YAAY,EAEjB,KAAK,SAAS,EACf,MAAM,eAAe,CAAC;AAEvB,iFAAiF;AACjF,MAAM,MAAM,SAAS,GAAG,IAAI,CAC1B,WAAW,EACX,aAAa,GAAG,kBAAkB,GAAG,qBAAqB,GAAG,OAAO,GAAG,OAAO,CAC/E,CAAC;AAEF,yFAAyF;AACzF,qBAAa,QAAS,SAAQ,KAAK;IACjC,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAE5B,YAAY,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,EAI9C;CACF;AAED,+GAA+G;AAC/G,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,SAAS,CAkBzD;AAYD,uGAAuG;AACvG,wBAAgB,eAAe,CAAC,IAAI,EAAE,SAAS,GAAG,KAAK,CA0BtD;AA2BD;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAUvD;AAED,wCAAwC;AACxC,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAQvD;AAID,MAAM,WAAW,oBAAoB;IACnC,8FAA8F;IAC9F,IAAI,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACzD,KAAK,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;IAC7C,OAAO,CAAC,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,CAAC;IACxC,yEAAyE;IACzE,SAAS,CAAC,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,CAAC;IAChC,uHAAuH;IACvH,UAAU,CAAC,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;CACnC;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACzD,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAC5C,IAAI,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,CAAC;IACpC,0FAA0F;IAC1F,KAAK,IAAI,IAAI,CAAC;CACf;AAED,iHAAiH;AACjH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,oBAAoB,GAAG,YAAY,CA4F9F"}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/* oxlint-disable anti-slop/no-unknown-parameters, anti-slop/no-unknown-returns -- the channel carries content values of any shape; the methods on either side give them their types. */
|
|
2
|
+
import { APIClientError, ErrorCodes, StaleWriteError } from '@conloca/content-api-client';
|
|
3
|
+
import { ContentWriteRefusedError } from '../write-access.js';
|
|
4
|
+
import { readControlMessage, } from './protocol.js';
|
|
5
|
+
/** An error the channel itself raises, such as a call the other side does not answer. */
|
|
6
|
+
export class RpcError extends Error {
|
|
7
|
+
code;
|
|
8
|
+
constructor(message, code) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'RpcError';
|
|
11
|
+
this.code = code;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** Turns anything thrown into a cloneable description the other side rebuilds with {@link decodeWireError}. */
|
|
15
|
+
export function encodeWireError(error) {
|
|
16
|
+
if (error instanceof ContentWriteRefusedError) {
|
|
17
|
+
return {
|
|
18
|
+
name: error.name,
|
|
19
|
+
message: error.message,
|
|
20
|
+
method: error.method,
|
|
21
|
+
refusal: { reason: error.refusal.reason },
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
if (error instanceof StaleWriteError) {
|
|
25
|
+
return { name: error.name, message: error.message, code: error.code, details: error.details, data: error.data };
|
|
26
|
+
}
|
|
27
|
+
if (error instanceof APIClientError) {
|
|
28
|
+
return { name: error.name, message: error.message, code: error.code, details: error.details };
|
|
29
|
+
}
|
|
30
|
+
if (error instanceof RpcError)
|
|
31
|
+
return { name: error.name, message: error.message, code: error.code };
|
|
32
|
+
if (error instanceof Error)
|
|
33
|
+
return { name: error.name, message: error.message };
|
|
34
|
+
return { name: 'Error', message: String(error) };
|
|
35
|
+
}
|
|
36
|
+
const ERROR_CODES = new Set(Object.values(ErrorCodes));
|
|
37
|
+
function isErrorCode(code) {
|
|
38
|
+
return code !== undefined && ERROR_CODES.has(code);
|
|
39
|
+
}
|
|
40
|
+
function isRpcErrorCode(code) {
|
|
41
|
+
return code === 'method_not_allowed' || code === 'bad_arguments' || code === 'rate_limited';
|
|
42
|
+
}
|
|
43
|
+
/** Rebuilds the error class {@link encodeWireError} described, so `instanceof` checks keep working. */
|
|
44
|
+
export function decodeWireError(wire) {
|
|
45
|
+
const { message } = wire;
|
|
46
|
+
// A code this side does not know still rebuilds the class; it only loses the code.
|
|
47
|
+
const code = isErrorCode(wire.code) ? wire.code : ErrorCodes.INTERNAL_ERROR;
|
|
48
|
+
switch (wire.name) {
|
|
49
|
+
case 'ContentWriteRefusedError':
|
|
50
|
+
return new ContentWriteRefusedError(wire.method ?? '', { reason: wire.refusal?.reason ?? message });
|
|
51
|
+
case 'StaleWriteError': {
|
|
52
|
+
const { data } = wire;
|
|
53
|
+
const currentEtag = isPlainObject(data) && 'currentEtag' in data && typeof data.currentEtag === 'string'
|
|
54
|
+
? data.currentEtag
|
|
55
|
+
: undefined;
|
|
56
|
+
return new StaleWriteError({ code, message, currentEtag });
|
|
57
|
+
}
|
|
58
|
+
case 'APIClientError':
|
|
59
|
+
return new APIClientError(message, code, wire.details);
|
|
60
|
+
case 'RpcError':
|
|
61
|
+
if (isRpcErrorCode(wire.code))
|
|
62
|
+
return new RpcError(message, wire.code);
|
|
63
|
+
break;
|
|
64
|
+
default:
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
const error = new Error(message);
|
|
68
|
+
error.name = wire.name;
|
|
69
|
+
return error;
|
|
70
|
+
}
|
|
71
|
+
function isPlainObject(value) {
|
|
72
|
+
if (typeof value !== 'object' || value === null)
|
|
73
|
+
return false;
|
|
74
|
+
const proto = Object.getPrototypeOf(value);
|
|
75
|
+
return proto === Object.prototype || proto === null;
|
|
76
|
+
}
|
|
77
|
+
/** Reads a {@link WireError} that arrived on the port; `undefined` when `value` is not one. */
|
|
78
|
+
function readWireError(value) {
|
|
79
|
+
if (!isPlainObject(value) || !('name' in value) || !('message' in value))
|
|
80
|
+
return undefined;
|
|
81
|
+
const { name, message } = value;
|
|
82
|
+
if (typeof name !== 'string' || typeof message !== 'string')
|
|
83
|
+
return undefined;
|
|
84
|
+
const wire = { name, message };
|
|
85
|
+
if ('code' in value && typeof value.code === 'string')
|
|
86
|
+
wire.code = value.code;
|
|
87
|
+
if ('details' in value)
|
|
88
|
+
wire.details = value.details;
|
|
89
|
+
if ('data' in value)
|
|
90
|
+
wire.data = value.data;
|
|
91
|
+
if ('method' in value && typeof value.method === 'string')
|
|
92
|
+
wire.method = value.method;
|
|
93
|
+
if ('refusal' in value && isPlainObject(value.refusal) && 'reason' in value.refusal) {
|
|
94
|
+
const { reason } = value.refusal;
|
|
95
|
+
if (typeof reason === 'string')
|
|
96
|
+
wire.refusal = { reason };
|
|
97
|
+
}
|
|
98
|
+
return wire;
|
|
99
|
+
}
|
|
100
|
+
/* oxlint-disable no-underscore-dangle -- `__wireError` is the protocol's marker for an error inside a value. */
|
|
101
|
+
/**
|
|
102
|
+
* Swaps every `Error` inside a value for `{ __wireError }`, walking only arrays and plain objects, so a Blob or File
|
|
103
|
+
* travels untouched. Structured cloning would otherwise flatten the errors' classes.
|
|
104
|
+
* shortcut: no cycle detection, values are content-API results; add a seen-set if a cyclic value ever crosses.
|
|
105
|
+
*/
|
|
106
|
+
export function encodeWireValue(value) {
|
|
107
|
+
if (value instanceof Error) {
|
|
108
|
+
// oxlint-disable-next-line anti-slop/no-known-value-widening -- an encoded value is opaque on the wire; the receiving side decodes it.
|
|
109
|
+
return { __wireError: encodeWireError(value) };
|
|
110
|
+
}
|
|
111
|
+
if (Array.isArray(value))
|
|
112
|
+
return value.map(encodeWireValue);
|
|
113
|
+
if (isPlainObject(value)) {
|
|
114
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, encodeWireValue(item)]));
|
|
115
|
+
}
|
|
116
|
+
return value;
|
|
117
|
+
}
|
|
118
|
+
/** Reverses {@link encodeWireValue}. */
|
|
119
|
+
export function decodeWireValue(value) {
|
|
120
|
+
if (Array.isArray(value))
|
|
121
|
+
return value.map(decodeWireValue);
|
|
122
|
+
if (isPlainObject(value)) {
|
|
123
|
+
const wire = '__wireError' in value ? readWireError(value.__wireError) : undefined;
|
|
124
|
+
if (wire)
|
|
125
|
+
return decodeWireError(wire);
|
|
126
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, decodeWireValue(item)]));
|
|
127
|
+
}
|
|
128
|
+
return value;
|
|
129
|
+
}
|
|
130
|
+
/** Carries calls, results, events and control messages over one port between the editor frame and its parent. */
|
|
131
|
+
export function openFrameChannel(port, handlers) {
|
|
132
|
+
let nextId = 1;
|
|
133
|
+
const pending = new Map();
|
|
134
|
+
async function answer(id, method, args) {
|
|
135
|
+
let reply;
|
|
136
|
+
try {
|
|
137
|
+
if (!handlers.call)
|
|
138
|
+
throw new RpcError(`Method not allowed: ${method}`, 'method_not_allowed');
|
|
139
|
+
reply = { kind: 'result', id, ok: true, value: encodeWireValue(await handlers.call(method, args)) };
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
reply = { kind: 'result', id, ok: false, error: encodeWireError(error) };
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
port.postMessage(reply);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
// The value or error could not be cloned; the caller still needs an answer. A plain Error keeps the call error
|
|
149
|
+
// codes to the protocol's own.
|
|
150
|
+
const message = `Result of ${method} could not be sent: ${error instanceof Error ? error.message : String(error)}`;
|
|
151
|
+
port.postMessage({ kind: 'result', id, ok: false, error: { name: 'Error', message } });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/** Handles `data` and returns whether it was a message the channel knows. */
|
|
155
|
+
function receive(data) {
|
|
156
|
+
if (!isPlainObject(data) || !('kind' in data))
|
|
157
|
+
return false;
|
|
158
|
+
const { kind } = data;
|
|
159
|
+
const id = 'id' in data ? data.id : undefined;
|
|
160
|
+
if (kind === 'call' && typeof id === 'number' && 'method' in data && 'args' in data) {
|
|
161
|
+
const { method, args } = data;
|
|
162
|
+
if (typeof method !== 'string' || !Array.isArray(args))
|
|
163
|
+
return false;
|
|
164
|
+
void answer(id, method, args);
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
if (kind === 'result' && typeof id === 'number' && 'ok' in data) {
|
|
168
|
+
const error = data.ok === false && 'error' in data ? readWireError(data.error) : undefined;
|
|
169
|
+
if (data.ok !== true && !error)
|
|
170
|
+
return false;
|
|
171
|
+
const entry = pending.get(id);
|
|
172
|
+
pending.delete(id);
|
|
173
|
+
if (error)
|
|
174
|
+
entry?.reject(decodeWireError(error));
|
|
175
|
+
else
|
|
176
|
+
entry?.resolve(decodeWireValue('value' in data ? data.value : undefined));
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
if (kind === 'event' && 'name' in data && typeof data.name === 'string') {
|
|
180
|
+
handlers.event?.(data.name, 'payload' in data ? data.payload : undefined);
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
const control = readControlMessage(data);
|
|
184
|
+
if (control)
|
|
185
|
+
handlers.control?.(control);
|
|
186
|
+
return control !== undefined;
|
|
187
|
+
}
|
|
188
|
+
const listener = (event) => {
|
|
189
|
+
const { data } = event;
|
|
190
|
+
if (!receive(data))
|
|
191
|
+
handlers.malformed?.(data);
|
|
192
|
+
};
|
|
193
|
+
port.addEventListener('message', listener);
|
|
194
|
+
port.start();
|
|
195
|
+
/** Sends a message nobody waits on, so a failure goes to `sendFailed` instead of the code that happened to send. */
|
|
196
|
+
function send(message) {
|
|
197
|
+
try {
|
|
198
|
+
port.postMessage(message);
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
if (handlers.sendFailed)
|
|
202
|
+
handlers.sendFailed(error);
|
|
203
|
+
else
|
|
204
|
+
console.error('[conloca] frame message could not be sent', error);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
call(method, args = []) {
|
|
209
|
+
const id = nextId++;
|
|
210
|
+
const promise = new Promise((resolve, reject) => pending.set(id, { resolve, reject }));
|
|
211
|
+
try {
|
|
212
|
+
port.postMessage({ kind: 'call', id, method, args });
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
pending.delete(id);
|
|
216
|
+
return Promise.reject(error);
|
|
217
|
+
}
|
|
218
|
+
return promise;
|
|
219
|
+
},
|
|
220
|
+
emit(name, payload) {
|
|
221
|
+
send({ kind: 'event', name, payload });
|
|
222
|
+
},
|
|
223
|
+
post(message) {
|
|
224
|
+
send(message);
|
|
225
|
+
},
|
|
226
|
+
close() {
|
|
227
|
+
port.removeEventListener('message', listener);
|
|
228
|
+
port.close();
|
|
229
|
+
pending.clear();
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
//# sourceMappingURL=channel.js.map
|