@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,284 @@
|
|
|
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
|
+
|
|
48
|
+
export interface DiscoveredWrappers {
|
|
49
|
+
/** The element wrapping the page's prose content (h1-h6, p, etc.).
|
|
50
|
+
* For Starlight: `.sl-markdown-content`. For Tailwind Typography:
|
|
51
|
+
* `.prose`. Null when no prose wrapper exists on the page. */
|
|
52
|
+
content: ContentWrapperInfo | null;
|
|
53
|
+
/** Ordered chain of classed wrappers between the content wrapper
|
|
54
|
+
* and the `<pre>`, outermost first. For Starlight:
|
|
55
|
+
* `[{ div, 'expressive-code' }, { figure, 'frame has-title …' }]`.
|
|
56
|
+
* Null when the page has no code blocks or no classed wrappers
|
|
57
|
+
* between the prose wrapper and the `<pre>`. */
|
|
58
|
+
codeBlock: ContentWrapperInfo[] | null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Tags whose presence signals "this element holds prose content."
|
|
62
|
+
* We walk up from the first such element we find inside `<main>` to
|
|
63
|
+
* locate the wrapper. Headings + paragraphs cover every prose-styling
|
|
64
|
+
* convention we've seen — every CSS framework scopes typography
|
|
65
|
+
* through descendant selectors on `h1`–`h6` and `p`. */
|
|
66
|
+
const PROSE_TAGS = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p']);
|
|
67
|
+
|
|
68
|
+
/** Tag that anchors code-block wrapper discovery. The host's code-
|
|
69
|
+
* block library (Starlight's expressive-code, Tailwind's `pre`
|
|
70
|
+
* styling, etc.) always renders a `<pre>` containing the code,
|
|
71
|
+
* often wrapped in additional classed elements. */
|
|
72
|
+
const CODE_BLOCK_ANCHOR = 'pre';
|
|
73
|
+
|
|
74
|
+
/** HTML5 void elements — never push onto the open-tag stack since they
|
|
75
|
+
* have no closing tag. */
|
|
76
|
+
const VOID_TAGS = new Set([
|
|
77
|
+
'area',
|
|
78
|
+
'base',
|
|
79
|
+
'br',
|
|
80
|
+
'col',
|
|
81
|
+
'embed',
|
|
82
|
+
'hr',
|
|
83
|
+
'img',
|
|
84
|
+
'input',
|
|
85
|
+
'link',
|
|
86
|
+
'meta',
|
|
87
|
+
'source',
|
|
88
|
+
'track',
|
|
89
|
+
'wbr',
|
|
90
|
+
]);
|
|
91
|
+
|
|
92
|
+
interface OpenTag {
|
|
93
|
+
tag: string;
|
|
94
|
+
className: string;
|
|
95
|
+
attrs: Map<string, string>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Locate the host's content + code-block wrappers in raw HTML. Single-
|
|
100
|
+
* pass regex walk with a stack so we can recover ancestor chains for
|
|
101
|
+
* both prose tags and `<pre>` tags without a full DOM parser dep.
|
|
102
|
+
*/
|
|
103
|
+
export function findContentWrapper(html: string): DiscoveredWrappers {
|
|
104
|
+
// Strip comments and scripts so opening-tag-shaped strings inside
|
|
105
|
+
// them don't pollute the walk.
|
|
106
|
+
const cleaned = html
|
|
107
|
+
.replace(/<!--[\s\S]*?-->/g, '')
|
|
108
|
+
.replace(/<script[\s\S]*?<\/script>/gi, '')
|
|
109
|
+
.replace(/<style[\s\S]*?<\/style>/gi, '');
|
|
110
|
+
|
|
111
|
+
const tagRe = /<(\/?)([a-zA-Z][a-zA-Z0-9-]*)((?:\s+[^>]*)?)\s*(\/?)>/g;
|
|
112
|
+
const stack: OpenTag[] = [];
|
|
113
|
+
|
|
114
|
+
let explicit: ContentWrapperInfo | null = null;
|
|
115
|
+
let mainEl: OpenTag | null = null;
|
|
116
|
+
const proseAncestorChains: OpenTag[][] = [];
|
|
117
|
+
const preAncestorChains: OpenTag[][] = [];
|
|
118
|
+
|
|
119
|
+
for (const match of cleaned.matchAll(tagRe)) {
|
|
120
|
+
const isClose = match[1] === '/';
|
|
121
|
+
const tagName = match[2].toLowerCase();
|
|
122
|
+
const attrBlob = match[3] ?? '';
|
|
123
|
+
const explicitlySelfClosing = match[4] === '/';
|
|
124
|
+
|
|
125
|
+
if (isClose) {
|
|
126
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
127
|
+
if (stack[i].tag === tagName) {
|
|
128
|
+
stack.length = i;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const attrs = parseAttributes(attrBlob);
|
|
136
|
+
const className = attrs.get('class') ?? '';
|
|
137
|
+
const el: OpenTag = { tag: tagName, className, attrs };
|
|
138
|
+
|
|
139
|
+
// Priority 1 (content only): explicit opt-in. Capture and keep
|
|
140
|
+
// walking — we still need to discover the code-block wrapper.
|
|
141
|
+
if (!explicit && attrs.has('data-conloca-content-root')) {
|
|
142
|
+
explicit = { tagName, className };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (!mainEl && tagName === 'main') {
|
|
146
|
+
mainEl = el;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (mainEl && PROSE_TAGS.has(tagName)) {
|
|
150
|
+
const mainIdx = stack.indexOf(mainEl);
|
|
151
|
+
if (mainIdx >= 0) {
|
|
152
|
+
proseAncestorChains.push(stack.slice(mainIdx + 1));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (mainEl && tagName === CODE_BLOCK_ANCHOR) {
|
|
157
|
+
const mainIdx = stack.indexOf(mainEl);
|
|
158
|
+
if (mainIdx >= 0) {
|
|
159
|
+
preAncestorChains.push(stack.slice(mainIdx + 1));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const isVoid = VOID_TAGS.has(tagName);
|
|
164
|
+
if (!isVoid && !explicitlySelfClosing) {
|
|
165
|
+
stack.push(el);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const content = resolveContentWrapper(explicit, mainEl, proseAncestorChains);
|
|
170
|
+
const codeBlock = resolveCodeBlockWrapper(content, preAncestorChains);
|
|
171
|
+
|
|
172
|
+
return { content, codeBlock };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Pick the content wrapper from the walk's collected data.
|
|
177
|
+
*
|
|
178
|
+
* Priority: explicit opt-in > majority-prose-wrapper-inside-main >
|
|
179
|
+
* `<main>` fallback > null.
|
|
180
|
+
*/
|
|
181
|
+
function resolveContentWrapper(
|
|
182
|
+
explicit: ContentWrapperInfo | null,
|
|
183
|
+
mainEl: OpenTag | null,
|
|
184
|
+
proseAncestorChains: OpenTag[][],
|
|
185
|
+
): ContentWrapperInfo | null {
|
|
186
|
+
if (explicit) return explicit;
|
|
187
|
+
if (!mainEl) return null;
|
|
188
|
+
|
|
189
|
+
if (proseAncestorChains.length === 0) {
|
|
190
|
+
return { tagName: 'main', className: mainEl.className };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Tally classed-ancestor occurrences across all prose chains.
|
|
194
|
+
const scores = new Map<OpenTag, number>();
|
|
195
|
+
for (const chain of proseAncestorChains) {
|
|
196
|
+
for (const ancestor of chain) {
|
|
197
|
+
if (!ancestor.className) continue;
|
|
198
|
+
scores.set(ancestor, (scores.get(ancestor) ?? 0) + 1);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Majority threshold rules out partial-coverage classed ancestors
|
|
203
|
+
// (single Cards holding one of many prose blocks).
|
|
204
|
+
const threshold = proseAncestorChains.length / 2;
|
|
205
|
+
const candidates = [...scores.entries()].filter(([, count]) => count > threshold);
|
|
206
|
+
|
|
207
|
+
if (candidates.length === 0) {
|
|
208
|
+
return { tagName: 'main', className: mainEl.className };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Tie-break by depth: the candidate that appears deepest in any
|
|
212
|
+
// chain is the most specific (closest to the prose tags).
|
|
213
|
+
const maxChainIndex = (el: OpenTag) => Math.max(...proseAncestorChains.map((c) => c.indexOf(el)));
|
|
214
|
+
candidates.sort((a, b) => {
|
|
215
|
+
if (b[1] !== a[1]) return b[1] - a[1];
|
|
216
|
+
return maxChainIndex(b[0]) - maxChainIndex(a[0]);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const winner = candidates[0][0];
|
|
220
|
+
return { tagName: winner.tag, className: winner.className };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Derive the code-block wrapper chain from a representative `<pre>`'s
|
|
225
|
+
* ancestor chain. We strip everything at or above the content wrapper
|
|
226
|
+
* (its styles are already handled by the content-wrapper mirror) and
|
|
227
|
+
* return the remaining classed elements in nesting order, outermost
|
|
228
|
+
* first.
|
|
229
|
+
*
|
|
230
|
+
* Returning a chain (instead of one collapsed element) preserves the
|
|
231
|
+
* shape host CSS expects:
|
|
232
|
+
*
|
|
233
|
+
* - Single-class selectors (`.frame { ... }`) hit on whichever link
|
|
234
|
+
* carries that class.
|
|
235
|
+
* - Descendant selectors (`.expressive-code .frame { ... }`,
|
|
236
|
+
* `.frame .header > .title`) hit because the editor's renderer
|
|
237
|
+
* materialises the chain as nested elements with the host's
|
|
238
|
+
* expected tag names.
|
|
239
|
+
*
|
|
240
|
+
* The editor's code-block frame component (`code-block-frame.tsx` in
|
|
241
|
+
* `@conloca/mdx`) treats the LAST link as the "frame" element (where
|
|
242
|
+
* figcaption/pre/copy children render) and wraps it with the earlier
|
|
243
|
+
* links from inside out. This matches the host's authoring shape for
|
|
244
|
+
* libraries like ExpressiveCode (`.expressive-code > figure.frame >
|
|
245
|
+
* figcaption + pre + .copy`).
|
|
246
|
+
*/
|
|
247
|
+
function resolveCodeBlockWrapper(
|
|
248
|
+
content: ContentWrapperInfo | null,
|
|
249
|
+
preAncestorChains: OpenTag[][],
|
|
250
|
+
): ContentWrapperInfo[] | null {
|
|
251
|
+
if (preAncestorChains.length === 0) return null;
|
|
252
|
+
|
|
253
|
+
// Pick the first <pre>'s chain as representative. Code blocks on a
|
|
254
|
+
// single page are uniformly wrapped, so chains are interchangeable.
|
|
255
|
+
const chain = preAncestorChains[0];
|
|
256
|
+
|
|
257
|
+
// Strip everything at-or-above the content wrapper; the rest is
|
|
258
|
+
// code-block-specific chrome that this endpoint is responsible for.
|
|
259
|
+
const contentIdx = content
|
|
260
|
+
? chain.findIndex((el) => el.tag === content.tagName && el.className === content.className)
|
|
261
|
+
: -1;
|
|
262
|
+
const codeBlockChain = chain.slice(contentIdx + 1);
|
|
263
|
+
|
|
264
|
+
const classedAncestors = codeBlockChain.filter((el) => el.className);
|
|
265
|
+
if (classedAncestors.length === 0) return null;
|
|
266
|
+
|
|
267
|
+
return classedAncestors.map((el) => ({ tagName: el.tag, className: el.className }));
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Parse the attribute blob inside an opening tag. Handles `name="v"`,
|
|
272
|
+
* `name='v'`, `name=v`, and boolean (no-value) attributes. Doesn't
|
|
273
|
+
* handle entity decoding (`&` → `&`) since the values we read
|
|
274
|
+
* (tag name, class list, the marker attribute) don't carry entities
|
|
275
|
+
* in practice.
|
|
276
|
+
*/
|
|
277
|
+
function parseAttributes(s: string) {
|
|
278
|
+
const out = new Map<string, string>();
|
|
279
|
+
const re = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/g;
|
|
280
|
+
for (const match of s.matchAll(re)) {
|
|
281
|
+
out.set(match[1], match[2] ?? match[3] ?? match[4] ?? '');
|
|
282
|
+
}
|
|
283
|
+
return out;
|
|
284
|
+
}
|
package/src/data-schemas.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { useEffect, useState } from 'react';
|
|
2
|
-
import type {
|
|
2
|
+
import type { HostSchema } from './host-schema.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Type for a collection of data schemas.
|
|
6
|
-
* Each key is a collection name, value is the
|
|
6
|
+
* Each key is a collection name, value is the {@link HostSchema} (generated
|
|
7
|
+
* Typia validator + JSON Schema metadata) for that collection's data payload.
|
|
7
8
|
*/
|
|
8
|
-
export type DataSchemas = Record<string,
|
|
9
|
+
export type DataSchemas = Record<string, HostSchema>;
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Shared state for data schemas across module instances.
|
|
@@ -0,0 +1,258 @@
|
|
|
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 {
|
|
5
|
+
type Call,
|
|
6
|
+
type ControlMessage,
|
|
7
|
+
type FrameEvent,
|
|
8
|
+
type Result,
|
|
9
|
+
type RpcErrorCode,
|
|
10
|
+
readControlMessage,
|
|
11
|
+
type WireError,
|
|
12
|
+
} from './protocol.js';
|
|
13
|
+
|
|
14
|
+
/** The part of a `MessagePort` the channel uses, so tests can hand it a fake. */
|
|
15
|
+
export type FramePort = Pick<
|
|
16
|
+
MessagePort,
|
|
17
|
+
'postMessage' | 'addEventListener' | 'removeEventListener' | 'start' | 'close'
|
|
18
|
+
>;
|
|
19
|
+
|
|
20
|
+
/** An error the channel itself raises, such as a call the other side does not answer. */
|
|
21
|
+
export class RpcError extends Error {
|
|
22
|
+
readonly code: RpcErrorCode;
|
|
23
|
+
|
|
24
|
+
constructor(message: string, code: RpcErrorCode) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = 'RpcError';
|
|
27
|
+
this.code = code;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Turns anything thrown into a cloneable description the other side rebuilds with {@link decodeWireError}. */
|
|
32
|
+
export function encodeWireError(error: unknown): WireError {
|
|
33
|
+
if (error instanceof ContentWriteRefusedError) {
|
|
34
|
+
return {
|
|
35
|
+
name: error.name,
|
|
36
|
+
message: error.message,
|
|
37
|
+
method: error.method,
|
|
38
|
+
refusal: { reason: error.refusal.reason },
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
if (error instanceof StaleWriteError) {
|
|
42
|
+
return { name: error.name, message: error.message, code: error.code, details: error.details, data: error.data };
|
|
43
|
+
}
|
|
44
|
+
if (error instanceof APIClientError) {
|
|
45
|
+
return { name: error.name, message: error.message, code: error.code, details: error.details };
|
|
46
|
+
}
|
|
47
|
+
if (error instanceof RpcError) return { name: error.name, message: error.message, code: error.code };
|
|
48
|
+
if (error instanceof Error) return { name: error.name, message: error.message };
|
|
49
|
+
return { name: 'Error', message: String(error) };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const ERROR_CODES: ReadonlySet<string> = new Set(Object.values(ErrorCodes));
|
|
53
|
+
|
|
54
|
+
function isErrorCode(code: string | undefined): code is APIClientError['code'] {
|
|
55
|
+
return code !== undefined && ERROR_CODES.has(code);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function isRpcErrorCode(code: string | undefined): code is RpcErrorCode {
|
|
59
|
+
return code === 'method_not_allowed' || code === 'bad_arguments' || code === 'rate_limited';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Rebuilds the error class {@link encodeWireError} described, so `instanceof` checks keep working. */
|
|
63
|
+
export function decodeWireError(wire: WireError): Error {
|
|
64
|
+
const { message } = wire;
|
|
65
|
+
// A code this side does not know still rebuilds the class; it only loses the code.
|
|
66
|
+
const code = isErrorCode(wire.code) ? wire.code : ErrorCodes.INTERNAL_ERROR;
|
|
67
|
+
switch (wire.name) {
|
|
68
|
+
case 'ContentWriteRefusedError':
|
|
69
|
+
return new ContentWriteRefusedError(wire.method ?? '', { reason: wire.refusal?.reason ?? message });
|
|
70
|
+
case 'StaleWriteError': {
|
|
71
|
+
const { data } = wire;
|
|
72
|
+
const currentEtag =
|
|
73
|
+
isPlainObject(data) && 'currentEtag' in data && typeof data.currentEtag === 'string'
|
|
74
|
+
? data.currentEtag
|
|
75
|
+
: undefined;
|
|
76
|
+
return new StaleWriteError({ code, message, currentEtag });
|
|
77
|
+
}
|
|
78
|
+
case 'APIClientError':
|
|
79
|
+
return new APIClientError(message, code, wire.details);
|
|
80
|
+
case 'RpcError':
|
|
81
|
+
if (isRpcErrorCode(wire.code)) return new RpcError(message, wire.code);
|
|
82
|
+
break;
|
|
83
|
+
default:
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
const error = new Error(message);
|
|
87
|
+
error.name = wire.name;
|
|
88
|
+
return error;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function isPlainObject(value: unknown): value is object {
|
|
92
|
+
if (typeof value !== 'object' || value === null) return false;
|
|
93
|
+
const proto = Object.getPrototypeOf(value);
|
|
94
|
+
return proto === Object.prototype || proto === null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Reads a {@link WireError} that arrived on the port; `undefined` when `value` is not one. */
|
|
98
|
+
function readWireError(value: unknown): WireError | undefined {
|
|
99
|
+
if (!isPlainObject(value) || !('name' in value) || !('message' in value)) return undefined;
|
|
100
|
+
const { name, message } = value;
|
|
101
|
+
if (typeof name !== 'string' || typeof message !== 'string') return undefined;
|
|
102
|
+
const wire: WireError = { name, message };
|
|
103
|
+
if ('code' in value && typeof value.code === 'string') wire.code = value.code;
|
|
104
|
+
if ('details' in value) wire.details = value.details;
|
|
105
|
+
if ('data' in value) wire.data = value.data;
|
|
106
|
+
if ('method' in value && typeof value.method === 'string') wire.method = value.method;
|
|
107
|
+
if ('refusal' in value && isPlainObject(value.refusal) && 'reason' in value.refusal) {
|
|
108
|
+
const { reason } = value.refusal;
|
|
109
|
+
if (typeof reason === 'string') wire.refusal = { reason };
|
|
110
|
+
}
|
|
111
|
+
return wire;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/* oxlint-disable no-underscore-dangle -- `__wireError` is the protocol's marker for an error inside a value. */
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Swaps every `Error` inside a value for `{ __wireError }`, walking only arrays and plain objects, so a Blob or File
|
|
118
|
+
* travels untouched. Structured cloning would otherwise flatten the errors' classes.
|
|
119
|
+
* shortcut: no cycle detection, values are content-API results; add a seen-set if a cyclic value ever crosses.
|
|
120
|
+
*/
|
|
121
|
+
export function encodeWireValue(value: unknown): unknown {
|
|
122
|
+
if (value instanceof Error) {
|
|
123
|
+
// oxlint-disable-next-line anti-slop/no-known-value-widening -- an encoded value is opaque on the wire; the receiving side decodes it.
|
|
124
|
+
return { __wireError: encodeWireError(value) };
|
|
125
|
+
}
|
|
126
|
+
if (Array.isArray(value)) return value.map(encodeWireValue);
|
|
127
|
+
if (isPlainObject(value)) {
|
|
128
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, encodeWireValue(item)]));
|
|
129
|
+
}
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Reverses {@link encodeWireValue}. */
|
|
134
|
+
export function decodeWireValue(value: unknown): unknown {
|
|
135
|
+
if (Array.isArray(value)) return value.map(decodeWireValue);
|
|
136
|
+
if (isPlainObject(value)) {
|
|
137
|
+
const wire = '__wireError' in value ? readWireError(value.__wireError) : undefined;
|
|
138
|
+
if (wire) return decodeWireError(wire);
|
|
139
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, decodeWireValue(item)]));
|
|
140
|
+
}
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/* oxlint-enable no-underscore-dangle */
|
|
145
|
+
|
|
146
|
+
export interface FrameChannelHandlers {
|
|
147
|
+
/** Answers a call from the other side; without it every call answers `method_not_allowed`. */
|
|
148
|
+
call?(method: string, args: unknown[]): Promise<unknown>;
|
|
149
|
+
event?(name: string, payload: unknown): void;
|
|
150
|
+
control?(message: ControlMessage): void;
|
|
151
|
+
/** Receives every message whose shape the channel does not recognise. */
|
|
152
|
+
malformed?(data: unknown): void;
|
|
153
|
+
/** Receives the error of an `emit` or `post` the port could not send; without it the error goes to `console.error`. */
|
|
154
|
+
sendFailed?(error: unknown): void;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface FrameChannel {
|
|
158
|
+
call(method: string, args?: unknown[]): Promise<unknown>;
|
|
159
|
+
emit(name: string, payload?: unknown): void;
|
|
160
|
+
post(message: ControlMessage): void;
|
|
161
|
+
/** Stops listening and closes the port; calls still waiting for a result never settle. */
|
|
162
|
+
close(): void;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Carries calls, results, events and control messages over one port between the editor frame and its parent. */
|
|
166
|
+
export function openFrameChannel(port: FramePort, handlers: FrameChannelHandlers): FrameChannel {
|
|
167
|
+
let nextId = 1;
|
|
168
|
+
const pending = new Map<number, { resolve(value: unknown): void; reject(error: Error): void }>();
|
|
169
|
+
|
|
170
|
+
async function answer(id: number, method: string, args: unknown[]): Promise<void> {
|
|
171
|
+
let reply: Result;
|
|
172
|
+
try {
|
|
173
|
+
if (!handlers.call) throw new RpcError(`Method not allowed: ${method}`, 'method_not_allowed');
|
|
174
|
+
reply = { kind: 'result', id, ok: true, value: encodeWireValue(await handlers.call(method, args)) };
|
|
175
|
+
} catch (error) {
|
|
176
|
+
reply = { kind: 'result', id, ok: false, error: encodeWireError(error) };
|
|
177
|
+
}
|
|
178
|
+
try {
|
|
179
|
+
port.postMessage(reply);
|
|
180
|
+
} catch (error) {
|
|
181
|
+
// The value or error could not be cloned; the caller still needs an answer. A plain Error keeps the call error
|
|
182
|
+
// codes to the protocol's own.
|
|
183
|
+
const message = `Result of ${method} could not be sent: ${error instanceof Error ? error.message : String(error)}`;
|
|
184
|
+
port.postMessage({ kind: 'result', id, ok: false, error: { name: 'Error', message } } satisfies Result);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Handles `data` and returns whether it was a message the channel knows. */
|
|
189
|
+
function receive(data: unknown): boolean {
|
|
190
|
+
if (!isPlainObject(data) || !('kind' in data)) return false;
|
|
191
|
+
const { kind } = data;
|
|
192
|
+
const id = 'id' in data ? data.id : undefined;
|
|
193
|
+
if (kind === 'call' && typeof id === 'number' && 'method' in data && 'args' in data) {
|
|
194
|
+
const { method, args } = data;
|
|
195
|
+
if (typeof method !== 'string' || !Array.isArray(args)) return false;
|
|
196
|
+
void answer(id, method, args);
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
if (kind === 'result' && typeof id === 'number' && 'ok' in data) {
|
|
200
|
+
const error = data.ok === false && 'error' in data ? readWireError(data.error) : undefined;
|
|
201
|
+
if (data.ok !== true && !error) return false;
|
|
202
|
+
const entry = pending.get(id);
|
|
203
|
+
pending.delete(id);
|
|
204
|
+
if (error) entry?.reject(decodeWireError(error));
|
|
205
|
+
else entry?.resolve(decodeWireValue('value' in data ? data.value : undefined));
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
if (kind === 'event' && 'name' in data && typeof data.name === 'string') {
|
|
209
|
+
handlers.event?.(data.name, 'payload' in data ? data.payload : undefined);
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
const control = readControlMessage(data);
|
|
213
|
+
if (control) handlers.control?.(control);
|
|
214
|
+
return control !== undefined;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const listener = (event: MessageEvent) => {
|
|
218
|
+
const { data } = event;
|
|
219
|
+
if (!receive(data)) handlers.malformed?.(data);
|
|
220
|
+
};
|
|
221
|
+
port.addEventListener('message', listener);
|
|
222
|
+
port.start();
|
|
223
|
+
|
|
224
|
+
/** Sends a message nobody waits on, so a failure goes to `sendFailed` instead of the code that happened to send. */
|
|
225
|
+
function send(message: FrameEvent | ControlMessage): void {
|
|
226
|
+
try {
|
|
227
|
+
port.postMessage(message);
|
|
228
|
+
} catch (error) {
|
|
229
|
+
if (handlers.sendFailed) handlers.sendFailed(error);
|
|
230
|
+
else console.error('[conloca] frame message could not be sent', error);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
call(method, args = []) {
|
|
236
|
+
const id = nextId++;
|
|
237
|
+
const promise = new Promise<unknown>((resolve, reject) => pending.set(id, { resolve, reject }));
|
|
238
|
+
try {
|
|
239
|
+
port.postMessage({ kind: 'call', id, method, args } satisfies Call);
|
|
240
|
+
} catch (error) {
|
|
241
|
+
pending.delete(id);
|
|
242
|
+
return Promise.reject(error);
|
|
243
|
+
}
|
|
244
|
+
return promise;
|
|
245
|
+
},
|
|
246
|
+
emit(name, payload) {
|
|
247
|
+
send({ kind: 'event', name, payload } satisfies FrameEvent);
|
|
248
|
+
},
|
|
249
|
+
post(message) {
|
|
250
|
+
send(message);
|
|
251
|
+
},
|
|
252
|
+
close() {
|
|
253
|
+
port.removeEventListener('message', listener);
|
|
254
|
+
port.close();
|
|
255
|
+
pending.clear();
|
|
256
|
+
},
|
|
257
|
+
};
|
|
258
|
+
}
|