alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
50 lines (48 loc) • 1.79 kB
JavaScript
import {
useAtomValue
} from "../../../chunks/chunk-WF77DMLN.js";
import "../../../chunks/chunk-OBOPLPUQ.js";
import {
zlibSync
} from "../../../chunks/chunk-SBZY6HII.js";
import "../../../chunks/chunk-U5RRZUYZ.js";
// src/dashboard/view/entry/EntryPreview.tsx
import { base64url } from "alinea/core/util/Encoding";
import { Suspense, useEffect, useState } from "react";
import { BrowserPreview } from "../preview/BrowserPreview.js";
import { jsx } from "react/jsx-runtime";
function EntryPreview({ editor, preview }) {
if (!preview)
return null;
if (typeof preview === "string")
return /* @__PURE__ */ jsx(EntryPreviewUrl, { editor, preview });
return /* @__PURE__ */ jsx(Suspense, { children: /* @__PURE__ */ jsx(EntryPreviewComponent, { editor, preview }) });
}
function EntryPreviewUrl({ editor, preview }) {
const selectedPhase = useAtomValue(editor.selectedPhase);
const previewSearch = `?token=${editor.previewToken}&entryId=${editor.entryId}&realm=${selectedPhase}`;
const [api, setApi] = useState(void 0);
const yUpdate = useAtomValue(editor.yUpdate);
useEffect(() => {
if (!api)
return;
const compressed = zlibSync(yUpdate, { level: 9 });
const update = base64url.stringify(compressed);
api.preview({
entryId: editor.entryId,
phase: selectedPhase,
update
});
}, [api, yUpdate]);
const base = new URL(preview, location.href);
const url = new URL(previewSearch, base);
return /* @__PURE__ */ jsx(BrowserPreview, { url: url.toString(), registerLivePreview: setApi });
}
function EntryPreviewComponent({ editor, preview }) {
const entry = useAtomValue(editor.draftEntry);
const Tag = preview;
return /* @__PURE__ */ jsx(Tag, { entry, previewToken: editor.previewToken });
}
export {
EntryPreview
};