@kidecms/core 0.26.0 → 0.26.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -383,6 +383,12 @@ type Props = {
|
|
|
383
383
|
fullscreen?: boolean;
|
|
384
384
|
};
|
|
385
385
|
|
|
386
|
+
// Stable identities: BubbleMenu dispatches an "updateOptions" transaction whenever
|
|
387
|
+
// these props change, and that transaction would trigger forceTick and a new render.
|
|
388
|
+
const BUBBLE_MENU_OPTIONS = { placement: "top" as const };
|
|
389
|
+
const shouldShowBubbleMenu: React.ComponentProps<typeof BubbleMenu>["shouldShow"] = ({ editor, from, to }) =>
|
|
390
|
+
from !== to && !editor.isActive(BLOCK_NODE_NAME) && !editor.isActive("image");
|
|
391
|
+
|
|
386
392
|
export default function ContentEditor({
|
|
387
393
|
name,
|
|
388
394
|
label,
|
|
@@ -653,10 +659,8 @@ export default function ContentEditor({
|
|
|
653
659
|
{/* Selection toolbar — appears when text is highlighted */}
|
|
654
660
|
<BubbleMenu
|
|
655
661
|
editor={editor}
|
|
656
|
-
options={
|
|
657
|
-
shouldShow={
|
|
658
|
-
from !== to && !ed.isActive(BLOCK_NODE_NAME) && !ed.isActive("image")
|
|
659
|
-
}
|
|
662
|
+
options={BUBBLE_MENU_OPTIONS}
|
|
663
|
+
shouldShow={shouldShowBubbleMenu}
|
|
660
664
|
className="bg-popover flex items-center gap-0.5 rounded-md border p-1 shadow-md"
|
|
661
665
|
>
|
|
662
666
|
<ToolbarButton
|
package/client/preview.ts
CHANGED
|
@@ -23,7 +23,7 @@ if (new URLSearchParams(location.search).has("preview")) {
|
|
|
23
23
|
})
|
|
24
24
|
.then((r) => (r.ok ? r.text() : null))
|
|
25
25
|
.then((html) => {
|
|
26
|
-
//
|
|
26
|
+
// Drop stale responses that finish after a newer edit's request.
|
|
27
27
|
if (html != null && id === pending) els.forEach((el) => (el.innerHTML = html));
|
|
28
28
|
})
|
|
29
29
|
.catch(() => {});
|
package/internals/integration.ts
CHANGED
|
@@ -339,14 +339,12 @@ export default function cmsIntegration(options?: CmsIntegrationOptions): AstroIn
|
|
|
339
339
|
pattern: "/api/cms/locks/[...path]",
|
|
340
340
|
entrypoint: new URL("../routes/api/locks/[...path].ts", import.meta.url),
|
|
341
341
|
});
|
|
342
|
-
//
|
|
343
|
-
//
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
});
|
|
349
|
-
}
|
|
342
|
+
// A page partial (not an endpoint using the Container API, which doesn't
|
|
343
|
+
// bundle for production) so live preview works in every deployed build.
|
|
344
|
+
injectRoute({
|
|
345
|
+
pattern: "/api/cms/preview/render",
|
|
346
|
+
entrypoint: new URL("../routes/api/preview/render.astro", import.meta.url),
|
|
347
|
+
});
|
|
350
348
|
injectRoute({
|
|
351
349
|
pattern: "/api/cms/references/[collection]/[id]",
|
|
352
350
|
entrypoint: new URL("../routes/api/references/[collection]/[id].ts", import.meta.url),
|
package/package.json
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
---
|
|
2
|
+
// Live-preview render endpoint: the preview tab POSTs unsaved rich text /
|
|
3
|
+
// blocks / content JSON and swaps the returned HTML into `[data-cms]` slots.
|
|
4
|
+
//
|
|
5
|
+
// A page partial rather than an API route on purpose: rendering Astro
|
|
6
|
+
// components from an endpoint needs the Container API, which leans on Vite
|
|
7
|
+
// internals and doesn't bundle for production (Cloudflare Workers in
|
|
8
|
+
// particular). A partial is an ordinary on-demand page — no doctype, no
|
|
9
|
+
// layout, just the component's HTML — so it works identically in dev and in
|
|
10
|
+
// every adapter's production build.
|
|
11
|
+
import BlockRenderer from "virtual:kide/block-renderer";
|
|
12
|
+
import ContentRenderer from "virtual:kide/content-renderer";
|
|
13
|
+
import { parseBlocks, renderRichText } from "../../../core";
|
|
14
|
+
|
|
15
|
+
export const prerender = false;
|
|
16
|
+
export const partial = true;
|
|
17
|
+
|
|
18
|
+
if (Astro.request.method !== "POST") {
|
|
19
|
+
return new Response(null, { status: 405, headers: { Allow: "POST" } });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let type: unknown;
|
|
23
|
+
let data: unknown;
|
|
24
|
+
try {
|
|
25
|
+
({ type, data } = await Astro.request.json());
|
|
26
|
+
} catch {
|
|
27
|
+
return new Response("Invalid JSON body", { status: 400 });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Editors send rich text / content documents either as objects or as the
|
|
31
|
+
// serialized form-field string; blocks arrive serialized and parseBlocks
|
|
32
|
+
// handles both.
|
|
33
|
+
let parsed: unknown = data;
|
|
34
|
+
if ((type === "content" || type === "richText") && typeof data === "string") {
|
|
35
|
+
try {
|
|
36
|
+
parsed = JSON.parse(data);
|
|
37
|
+
} catch {
|
|
38
|
+
return new Response("Invalid document JSON", { status: 400 });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (type !== "blocks" && type !== "content" && type !== "richText") {
|
|
43
|
+
return new Response("Unknown preview type", { status: 400 });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
Astro.response.headers.set("Cache-Control", "no-store");
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
{type === "blocks" && <BlockRenderer blocks={parseBlocks(data)} status="any" />}
|
|
50
|
+
{type === "content" && <ContentRenderer content={parsed as any} status="any" />}
|
|
51
|
+
{type === "richText" && <Fragment set:html={renderRichText(parsed as any) ?? ""} />}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { APIRoute } from "astro";
|
|
2
|
-
import { experimental_AstroContainer as AstroContainer } from "astro/container";
|
|
3
|
-
import { loadRenderers } from "astro:container";
|
|
4
|
-
import { getContainerRenderer } from "@astrojs/react/container-renderer";
|
|
5
|
-
|
|
6
|
-
import BlockRenderer from "virtual:kide/block-renderer";
|
|
7
|
-
import ContentRenderer from "virtual:kide/content-renderer";
|
|
8
|
-
import { renderRichText, parseBlocks } from "../../../core";
|
|
9
|
-
|
|
10
|
-
export const prerender = false;
|
|
11
|
-
|
|
12
|
-
let containerPromise: Promise<AstroContainer> | null = null;
|
|
13
|
-
const getContainer = () => {
|
|
14
|
-
if (!containerPromise) {
|
|
15
|
-
containerPromise = (async () => {
|
|
16
|
-
const renderers = await loadRenderers([getContainerRenderer()]);
|
|
17
|
-
return AstroContainer.create({ renderers });
|
|
18
|
-
})();
|
|
19
|
-
}
|
|
20
|
-
return containerPromise;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export const POST: APIRoute = async ({ request }) => {
|
|
24
|
-
const { type, data } = await request.json();
|
|
25
|
-
|
|
26
|
-
let html = "";
|
|
27
|
-
if (type === "blocks") {
|
|
28
|
-
const blocks = parseBlocks(data);
|
|
29
|
-
const container = await getContainer();
|
|
30
|
-
html = await container.renderToString(BlockRenderer, { props: { blocks, status: "any" } });
|
|
31
|
-
} else if (type === "content") {
|
|
32
|
-
const parsed = typeof data === "string" ? JSON.parse(data) : data;
|
|
33
|
-
const container = await getContainer();
|
|
34
|
-
html = await container.renderToString(ContentRenderer, { props: { content: parsed, status: "any" } });
|
|
35
|
-
} else if (type === "richText") {
|
|
36
|
-
const parsed = typeof data === "string" ? JSON.parse(data) : data;
|
|
37
|
-
html = renderRichText(parsed) ?? "";
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return new Response(html, { headers: { "Content-Type": "text/html" } });
|
|
41
|
-
};
|