UNPKG

react-intlayer

Version:

Easily internationalize i18n your React applications with type-safe multilingual content management.

88 lines 2.85 kB
"use client"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; import { IntlayerEventListener } from "@intlayer/api"; import configuration from "@intlayer/config/built"; import { EditorProvider, useChangedContentActions, useCrossURLPathSetter, useEditorEnabled, useIframeClickInterceptor } from "@intlayer/editor-react"; import { useEffect } from "react"; const IntlayerEditorHooksEnabled = () => { useCrossURLPathSetter(); useIframeClickInterceptor(); return /* @__PURE__ */ jsx(Fragment, {}); }; const { editor } = configuration; const IntlayerEditorHook = () => { const { enabled } = useEditorEnabled(); const { setChangedContent } = useChangedContentActions(); useEffect(() => { if (!editor.hotReload) return; if (!editor.clientId) return; if (!editor.clientSecret) return; const eventSource = new IntlayerEventListener(); try { eventSource.initialize().then(() => { eventSource.onDictionaryChange = (dictionary) => setChangedContent(dictionary.key, dictionary.content); }); } catch (error) { console.error("Error initializing IntlayerEventListener:", error); } return () => eventSource.cleanup(); }, []); return enabled ? /* @__PURE__ */ jsx(IntlayerEditorHooksEnabled, {}) : /* @__PURE__ */ jsx(Fragment, {}); }; const IntlayerEditorProvider = ({ children }) => { return /* @__PURE__ */ jsxs( EditorProvider, { postMessage: (data) => { if (typeof window === "undefined") return; const isInIframe = window.self !== window.top; if (!isInIframe) return; if (editor.applicationURL.length > 0) { window?.postMessage( data, // Use to restrict the origin of the editor for security reasons. // Correspond to the current application URL to synchronize the locales states. editor.applicationURL ); } if (editor.editorURL.length > 0) { window.parent?.postMessage( data, // Use to restrict the origin of the editor for security reasons. // Correspond to the editor URL to synchronize the locales states. editor.editorURL ); } if (editor.cmsURL.length > 0) { window.parent?.postMessage( data, // Use to restrict the origin of the CMS for security reasons. // Correspond to the CMS URL. editor.cmsURL ); } }, allowedOrigins: [ editor?.editorURL, editor?.cmsURL, editor?.applicationURL ], mode: "client", configuration, children: [ /* @__PURE__ */ jsx(IntlayerEditorHook, {}), children ] } ); }; export { IntlayerEditorProvider }; //# sourceMappingURL=IntlayerEditorProvider.mjs.map