react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
59 lines (56 loc) • 2.08 kB
JavaScript
'use client';
import { useEffect } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { EditorProvider, useCrossURLPathSetter, useDictionariesRecordActions, useEditorEnabled, useIframeClickInterceptor } from "@intlayer/editor-react";
import configuration from "@intlayer/config/built";
//#region src/editor/IntlayerEditorProvider.tsx
const IntlayerEditorHooksEnabled = () => {
/**
* URL Messages
*/
useCrossURLPathSetter();
/**
* Click Messages
*/
useIframeClickInterceptor();
/**
* Sent local dictionaries to editor
*/
const { setLocaleDictionaries } = useDictionariesRecordActions();
useEffect(() => {
import("@intlayer/unmerged-dictionaries-entry").then((mod) => {
const unmergedDictionaries = mod.getUnmergedDictionaries();
const dictionariesList = Object.fromEntries(Object.values(unmergedDictionaries).flat().map((dictionary) => [dictionary.localId, dictionary]));
setLocaleDictionaries?.(dictionariesList);
});
}, []);
return /* @__PURE__ */ jsx(Fragment, {});
};
const { editor } = configuration ?? {};
const IntlayerEditorHook = () => {
const { enabled } = useEditorEnabled();
return enabled ? /* @__PURE__ */ jsx(IntlayerEditorHooksEnabled, {}) : /* @__PURE__ */ jsx(Fragment, {});
};
const IntlayerEditorProvider = ({ children }) => {
return /* @__PURE__ */ jsxs(EditorProvider, {
postMessage: (data) => {
if (typeof window === "undefined") return;
if (!editor) return;
if (!(window.self !== window.top)) return;
if (editor.applicationURL.length > 0) window?.postMessage(data, editor.applicationURL);
if (editor.editorURL.length > 0) window.parent?.postMessage(data, editor.editorURL);
if (editor.cmsURL.length > 0) window.parent?.postMessage(data, editor.cmsURL);
},
allowedOrigins: [
editor?.editorURL,
editor?.cmsURL,
editor?.applicationURL
].filter(Boolean),
mode: "client",
configuration,
children: [/* @__PURE__ */ jsx(IntlayerEditorHook, {}), children]
});
};
//#endregion
export { IntlayerEditorProvider };
//# sourceMappingURL=IntlayerEditorProvider.mjs.map