@intlayer/editor-react 6.1.6 → 7.0.0-canary.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.
Files changed (75) hide show
  1. package/dist/cjs/CommunicatorContext.cjs +43 -65
  2. package/dist/cjs/CommunicatorContext.cjs.map +1 -1
  3. package/dist/cjs/ConfigurationContext.cjs +26 -47
  4. package/dist/cjs/ConfigurationContext.cjs.map +1 -1
  5. package/dist/cjs/DictionariesRecordContext.cjs +48 -74
  6. package/dist/cjs/DictionariesRecordContext.cjs.map +1 -1
  7. package/dist/cjs/EditedContentContext.cjs +164 -225
  8. package/dist/cjs/EditedContentContext.cjs.map +1 -1
  9. package/dist/cjs/EditorEnabledContext.cjs +34 -59
  10. package/dist/cjs/EditorEnabledContext.cjs.map +1 -1
  11. package/dist/cjs/EditorProvider.cjs +65 -66
  12. package/dist/cjs/EditorProvider.cjs.map +1 -1
  13. package/dist/cjs/FocusDictionaryContext.cjs +51 -75
  14. package/dist/cjs/FocusDictionaryContext.cjs.map +1 -1
  15. package/dist/cjs/_virtual/rolldown_runtime.cjs +25 -0
  16. package/dist/cjs/index.cjs +46 -44
  17. package/dist/cjs/useCrossFrameMessageListener.cjs +73 -50
  18. package/dist/cjs/useCrossFrameMessageListener.cjs.map +1 -1
  19. package/dist/cjs/useCrossFrameState.cjs +106 -73
  20. package/dist/cjs/useCrossFrameState.cjs.map +1 -1
  21. package/dist/cjs/useCrossURLPathState.cjs +67 -66
  22. package/dist/cjs/useCrossURLPathState.cjs.map +1 -1
  23. package/dist/cjs/useIframeClickInterceptor.cjs +24 -47
  24. package/dist/cjs/useIframeClickInterceptor.cjs.map +1 -1
  25. package/dist/esm/CommunicatorContext.mjs +34 -32
  26. package/dist/esm/CommunicatorContext.mjs.map +1 -1
  27. package/dist/esm/ConfigurationContext.mjs +19 -23
  28. package/dist/esm/ConfigurationContext.mjs.map +1 -1
  29. package/dist/esm/DictionariesRecordContext.mjs +37 -47
  30. package/dist/esm/DictionariesRecordContext.mjs.map +1 -1
  31. package/dist/esm/EditedContentContext.mjs +150 -201
  32. package/dist/esm/EditedContentContext.mjs.map +1 -1
  33. package/dist/esm/EditorEnabledContext.mjs +22 -32
  34. package/dist/esm/EditorEnabledContext.mjs.map +1 -1
  35. package/dist/esm/EditorProvider.mjs +60 -54
  36. package/dist/esm/EditorProvider.mjs.map +1 -1
  37. package/dist/esm/FocusDictionaryContext.mjs +41 -48
  38. package/dist/esm/FocusDictionaryContext.mjs.map +1 -1
  39. package/dist/esm/index.mjs +15 -13
  40. package/dist/esm/useCrossFrameMessageListener.mjs +69 -25
  41. package/dist/esm/useCrossFrameMessageListener.mjs.map +1 -1
  42. package/dist/esm/useCrossFrameState.mjs +102 -47
  43. package/dist/esm/useCrossFrameState.mjs.map +1 -1
  44. package/dist/esm/useCrossURLPathState.mjs +61 -41
  45. package/dist/esm/useCrossURLPathState.mjs.map +1 -1
  46. package/dist/esm/useIframeClickInterceptor.mjs +19 -21
  47. package/dist/esm/useIframeClickInterceptor.mjs.map +1 -1
  48. package/dist/types/CommunicatorContext.d.ts +12 -8
  49. package/dist/types/CommunicatorContext.d.ts.map +1 -1
  50. package/dist/types/ConfigurationContext.d.ts +12 -7
  51. package/dist/types/ConfigurationContext.d.ts.map +1 -1
  52. package/dist/types/DictionariesRecordContext.d.ts +15 -12
  53. package/dist/types/DictionariesRecordContext.d.ts.map +1 -1
  54. package/dist/types/EditedContentContext.d.ts +33 -30
  55. package/dist/types/EditedContentContext.d.ts.map +1 -1
  56. package/dist/types/EditorEnabledContext.d.ts +14 -9
  57. package/dist/types/EditorEnabledContext.d.ts.map +1 -1
  58. package/dist/types/EditorProvider.d.ts +10 -6
  59. package/dist/types/EditorProvider.d.ts.map +1 -1
  60. package/dist/types/FocusDictionaryContext.d.ts +18 -15
  61. package/dist/types/FocusDictionaryContext.d.ts.map +1 -1
  62. package/dist/types/index.d.ts +13 -13
  63. package/dist/types/useCrossFrameMessageListener.d.ts +7 -2
  64. package/dist/types/useCrossFrameMessageListener.d.ts.map +1 -1
  65. package/dist/types/useCrossFrameState.d.ts +10 -6
  66. package/dist/types/useCrossFrameState.d.ts.map +1 -1
  67. package/dist/types/useCrossURLPathState.d.ts +8 -3
  68. package/dist/types/useCrossURLPathState.d.ts.map +1 -1
  69. package/dist/types/useIframeClickInterceptor.d.ts +5 -2
  70. package/dist/types/useIframeClickInterceptor.d.ts.map +1 -1
  71. package/package.json +34 -40
  72. package/LICENSE +0 -202
  73. package/dist/cjs/index.cjs.map +0 -1
  74. package/dist/esm/index.mjs.map +0 -1
  75. package/dist/types/index.d.ts.map +0 -1
@@ -1,61 +1,67 @@
1
- "use client";
2
- import { jsx } from "react/jsx-runtime";
3
- import {
4
- useEffect,
5
- useState
6
- } from "react";
7
- import {
8
- CommunicatorProvider
9
- } from "./CommunicatorContext.mjs";
10
- import {
11
- ConfigurationProvider
12
- } from "./ConfigurationContext.mjs";
1
+ 'use client';
2
+
3
+
4
+ import { CommunicatorProvider } from "./CommunicatorContext.mjs";
5
+ import { ConfigurationProvider } from "./ConfigurationContext.mjs";
13
6
  import { DictionariesRecordProvider } from "./DictionariesRecordContext.mjs";
14
- import {
15
- EditedContentProvider,
16
- useGetEditedContentState
17
- } from "./EditedContentContext.mjs";
18
- import {
19
- EditorEnabledProvider,
20
- useEditorEnabled,
21
- useGetEditorEnabledState
22
- } from "./EditorEnabledContext.mjs";
7
+ import { EditedContentProvider, useGetEditedContentState } from "./EditedContentContext.mjs";
8
+ import { EditorEnabledProvider, useEditorEnabled, useGetEditorEnabledState } from "./EditorEnabledContext.mjs";
23
9
  import { FocusDictionaryProvider } from "./FocusDictionaryContext.mjs";
10
+ import { useEffect, useState } from "react";
11
+ import { jsx } from "react/jsx-runtime";
12
+
13
+ //#region src/EditorProvider.tsx
14
+ /**
15
+ * This component add all the providers needed by the editor.
16
+ * It is used to wrap the application, or the editor to work together.
17
+ */
24
18
  const EditorProvidersWrapper = ({ children }) => {
25
- const getEditedContentState = useGetEditedContentState();
26
- useEffect(() => {
27
- getEditedContentState();
28
- }, []);
29
- return /* @__PURE__ */ jsx(DictionariesRecordProvider, { children: /* @__PURE__ */ jsx(EditedContentProvider, { children: /* @__PURE__ */ jsx(FocusDictionaryProvider, { children }) }) });
30
- };
31
- const EditorEnabledCheckRenderer = ({
32
- children,
33
- fallback
34
- }) => {
35
- const getEditorEnabled = useGetEditorEnabledState();
36
- const { enabled } = useEditorEnabled();
37
- useEffect(() => {
38
- if (enabled) return;
39
- getEditorEnabled();
40
- }, [enabled]);
41
- return enabled ? children : fallback;
19
+ const getEditedContentState = useGetEditedContentState();
20
+ useEffect(() => {
21
+ getEditedContentState();
22
+ }, []);
23
+ return /* @__PURE__ */ jsx(DictionariesRecordProvider, { children: /* @__PURE__ */ jsx(EditedContentProvider, { children: /* @__PURE__ */ jsx(FocusDictionaryProvider, { children }) }) });
42
24
  };
43
- const IframeCheckRenderer = ({
44
- children,
45
- fallback
46
- }) => {
47
- const [isInIframe, setIsInIframe] = useState(false);
48
- useEffect(() => {
49
- setIsInIframe(window.self !== window.top);
50
- }, []);
51
- return isInIframe ? children : fallback;
25
+ /**
26
+ * This component check if the editor is enabled to render the editor providers.
27
+ */
28
+ const EditorEnabledCheckRenderer = ({ children, fallback }) => {
29
+ const getEditorEnabled = useGetEditorEnabledState();
30
+ const { enabled } = useEditorEnabled();
31
+ useEffect(() => {
32
+ if (enabled) return;
33
+ getEditorEnabled();
34
+ }, [enabled]);
35
+ return enabled ? children : fallback;
52
36
  };
53
- const EditorProvider = ({
54
- children,
55
- configuration,
56
- ...props
57
- }) => /* @__PURE__ */ jsx(EditorEnabledProvider, { children: /* @__PURE__ */ jsx(ConfigurationProvider, { configuration, children: props.mode === "editor" ? /* @__PURE__ */ jsx(CommunicatorProvider, { ...props, children: /* @__PURE__ */ jsx(EditorProvidersWrapper, { children }) }) : /* @__PURE__ */ jsx(IframeCheckRenderer, { fallback: children, children: /* @__PURE__ */ jsx(CommunicatorProvider, { ...props, children: /* @__PURE__ */ jsx(EditorEnabledCheckRenderer, { fallback: children, children: /* @__PURE__ */ jsx(EditorProvidersWrapper, { children }) }) }) }) }) });
58
- export {
59
- EditorProvider
37
+ /**
38
+ * This component is used to check if the editor is wrapping the application.
39
+ * It avoid to send window.postMessage to the application if the editor is not wrapping the application.
40
+ */
41
+ const IframeCheckRenderer = ({ children, fallback }) => {
42
+ const [isInIframe, setIsInIframe] = useState(false);
43
+ useEffect(() => {
44
+ setIsInIframe(window.self !== window.top);
45
+ }, []);
46
+ return isInIframe ? children : fallback;
60
47
  };
48
+ const EditorProvider = ({ children, configuration,...props }) => /* @__PURE__ */ jsx(EditorEnabledProvider, { children: /* @__PURE__ */ jsx(ConfigurationProvider, {
49
+ configuration,
50
+ children: props.mode === "editor" ? /* @__PURE__ */ jsx(CommunicatorProvider, {
51
+ ...props,
52
+ children: /* @__PURE__ */ jsx(EditorProvidersWrapper, { children })
53
+ }) : /* @__PURE__ */ jsx(IframeCheckRenderer, {
54
+ fallback: children,
55
+ children: /* @__PURE__ */ jsx(CommunicatorProvider, {
56
+ ...props,
57
+ children: /* @__PURE__ */ jsx(EditorEnabledCheckRenderer, {
58
+ fallback: children,
59
+ children: /* @__PURE__ */ jsx(EditorProvidersWrapper, { children })
60
+ })
61
+ })
62
+ })
63
+ }) });
64
+
65
+ //#endregion
66
+ export { EditorProvider };
61
67
  //# sourceMappingURL=EditorProvider.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/EditorProvider.tsx"],"sourcesContent":["'use client';\n\nimport {\n type FC,\n type PropsWithChildren,\n ReactNode,\n useEffect,\n useState,\n} from 'react';\nimport {\n type CommunicatorProviderProps,\n CommunicatorProvider,\n} from './CommunicatorContext';\nimport {\n type ConfigurationProviderProps,\n ConfigurationProvider,\n} from './ConfigurationContext';\nimport { DictionariesRecordProvider } from './DictionariesRecordContext';\nimport {\n EditedContentProvider,\n useGetEditedContentState,\n} from './EditedContentContext';\nimport {\n EditorEnabledProvider,\n useEditorEnabled,\n useGetEditorEnabledState,\n} from './EditorEnabledContext';\nimport { FocusDictionaryProvider } from './FocusDictionaryContext';\n\n/**\n * This component add all the providers needed by the editor.\n * It is used to wrap the application, or the editor to work together.\n */\nconst EditorProvidersWrapper: FC<PropsWithChildren> = ({ children }) => {\n const getEditedContentState = useGetEditedContentState();\n\n useEffect(() => {\n getEditedContentState();\n }, []);\n\n return (\n <DictionariesRecordProvider>\n <EditedContentProvider>\n <FocusDictionaryProvider>{children}</FocusDictionaryProvider>\n </EditedContentProvider>\n </DictionariesRecordProvider>\n );\n};\n\ntype FallbackProps = {\n fallback: ReactNode;\n};\n\n/**\n * This component check if the editor is enabled to render the editor providers.\n */\nconst EditorEnabledCheckRenderer: FC<PropsWithChildren<FallbackProps>> = ({\n children,\n fallback,\n}) => {\n const getEditorEnabled = useGetEditorEnabledState();\n\n const { enabled } = useEditorEnabled();\n\n useEffect(() => {\n if (enabled) return;\n\n // Check if the editor is wrapping the application\n getEditorEnabled();\n }, [enabled]);\n\n return enabled ? children : fallback;\n};\n\n/**\n * This component is used to check if the editor is wrapping the application.\n * It avoid to send window.postMessage to the application if the editor is not wrapping the application.\n */\nconst IframeCheckRenderer: FC<PropsWithChildren<FallbackProps>> = ({\n children,\n fallback,\n}) => {\n const [isInIframe, setIsInIframe] = useState(false);\n\n useEffect(() => {\n setIsInIframe(window.self !== window.top);\n }, []);\n\n return isInIframe ? children : fallback;\n};\n\nexport type EditorProviderProps = CommunicatorProviderProps &\n ConfigurationProviderProps & {\n mode: 'editor' | 'client';\n };\n\nexport const EditorProvider: FC<PropsWithChildren<EditorProviderProps>> = ({\n children,\n configuration,\n ...props\n}) => (\n <EditorEnabledProvider>\n <ConfigurationProvider configuration={configuration}>\n {props.mode === 'editor' ? (\n <CommunicatorProvider {...props}>\n <EditorProvidersWrapper>{children}</EditorProvidersWrapper>\n </CommunicatorProvider>\n ) : (\n <IframeCheckRenderer fallback={children}>\n <CommunicatorProvider {...props}>\n <EditorEnabledCheckRenderer fallback={children}>\n <EditorProvidersWrapper>{children}</EditorProvidersWrapper>\n </EditorEnabledCheckRenderer>\n </CommunicatorProvider>\n </IframeCheckRenderer>\n )}\n </ConfigurationProvider>\n </EditorEnabledProvider>\n);\n"],"mappings":";AA2CQ;AAzCR;AAAA,EAIE;AAAA,EACA;AAAA,OACK;AACP;AAAA,EAEE;AAAA,OACK;AACP;AAAA,EAEE;AAAA,OACK;AACP,SAAS,kCAAkC;AAC3C;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,+BAA+B;AAMxC,MAAM,yBAAgD,CAAC,EAAE,SAAS,MAAM;AACtE,QAAM,wBAAwB,yBAAyB;AAEvD,YAAU,MAAM;AACd,0BAAsB;AAAA,EACxB,GAAG,CAAC,CAAC;AAEL,SACE,oBAAC,8BACC,8BAAC,yBACC,8BAAC,2BAAyB,UAAS,GACrC,GACF;AAEJ;AASA,MAAM,6BAAmE,CAAC;AAAA,EACxE;AAAA,EACA;AACF,MAAM;AACJ,QAAM,mBAAmB,yBAAyB;AAElD,QAAM,EAAE,QAAQ,IAAI,iBAAiB;AAErC,YAAU,MAAM;AACd,QAAI,QAAS;AAGb,qBAAiB;AAAA,EACnB,GAAG,CAAC,OAAO,CAAC;AAEZ,SAAO,UAAU,WAAW;AAC9B;AAMA,MAAM,sBAA4D,CAAC;AAAA,EACjE;AAAA,EACA;AACF,MAAM;AACJ,QAAM,CAAC,YAAY,aAAa,IAAI,SAAS,KAAK;AAElD,YAAU,MAAM;AACd,kBAAc,OAAO,SAAS,OAAO,GAAG;AAAA,EAC1C,GAAG,CAAC,CAAC;AAEL,SAAO,aAAa,WAAW;AACjC;AAOO,MAAM,iBAA6D,CAAC;AAAA,EACzE;AAAA,EACA;AAAA,EACA,GAAG;AACL,MACE,oBAAC,yBACC,8BAAC,yBAAsB,eACpB,gBAAM,SAAS,WACd,oBAAC,wBAAsB,GAAG,OACxB,8BAAC,0BAAwB,UAAS,GACpC,IAEA,oBAAC,uBAAoB,UAAU,UAC7B,8BAAC,wBAAsB,GAAG,OACxB,8BAAC,8BAA2B,UAAU,UACpC,8BAAC,0BAAwB,UAAS,GACpC,GACF,GACF,GAEJ,GACF;","names":[]}
1
+ {"version":3,"file":"EditorProvider.mjs","names":["EditorProvidersWrapper: FC<PropsWithChildren>","EditorEnabledCheckRenderer: FC<PropsWithChildren<FallbackProps>>","IframeCheckRenderer: FC<PropsWithChildren<FallbackProps>>","EditorProvider: FC<PropsWithChildren<EditorProviderProps>>"],"sources":["../../src/EditorProvider.tsx"],"sourcesContent":["'use client';\n\nimport {\n type FC,\n type PropsWithChildren,\n type ReactNode,\n useEffect,\n useState,\n} from 'react';\nimport {\n CommunicatorProvider,\n type CommunicatorProviderProps,\n} from './CommunicatorContext';\nimport {\n ConfigurationProvider,\n type ConfigurationProviderProps,\n} from './ConfigurationContext';\nimport { DictionariesRecordProvider } from './DictionariesRecordContext';\nimport {\n EditedContentProvider,\n useGetEditedContentState,\n} from './EditedContentContext';\nimport {\n EditorEnabledProvider,\n useEditorEnabled,\n useGetEditorEnabledState,\n} from './EditorEnabledContext';\nimport { FocusDictionaryProvider } from './FocusDictionaryContext';\n\n/**\n * This component add all the providers needed by the editor.\n * It is used to wrap the application, or the editor to work together.\n */\nconst EditorProvidersWrapper: FC<PropsWithChildren> = ({ children }) => {\n const getEditedContentState = useGetEditedContentState();\n\n useEffect(() => {\n getEditedContentState();\n }, []);\n\n return (\n <DictionariesRecordProvider>\n <EditedContentProvider>\n <FocusDictionaryProvider>{children}</FocusDictionaryProvider>\n </EditedContentProvider>\n </DictionariesRecordProvider>\n );\n};\n\ntype FallbackProps = {\n fallback: ReactNode;\n};\n\n/**\n * This component check if the editor is enabled to render the editor providers.\n */\nconst EditorEnabledCheckRenderer: FC<PropsWithChildren<FallbackProps>> = ({\n children,\n fallback,\n}) => {\n const getEditorEnabled = useGetEditorEnabledState();\n\n const { enabled } = useEditorEnabled();\n\n useEffect(() => {\n if (enabled) return;\n\n // Check if the editor is wrapping the application\n getEditorEnabled();\n }, [enabled]);\n\n return enabled ? children : fallback;\n};\n\n/**\n * This component is used to check if the editor is wrapping the application.\n * It avoid to send window.postMessage to the application if the editor is not wrapping the application.\n */\nconst IframeCheckRenderer: FC<PropsWithChildren<FallbackProps>> = ({\n children,\n fallback,\n}) => {\n const [isInIframe, setIsInIframe] = useState(false);\n\n useEffect(() => {\n setIsInIframe(window.self !== window.top);\n }, []);\n\n return isInIframe ? children : fallback;\n};\n\nexport type EditorProviderProps = CommunicatorProviderProps &\n ConfigurationProviderProps & {\n mode: 'editor' | 'client';\n };\n\nexport const EditorProvider: FC<PropsWithChildren<EditorProviderProps>> = ({\n children,\n configuration,\n ...props\n}) => (\n <EditorEnabledProvider>\n <ConfigurationProvider configuration={configuration}>\n {props.mode === 'editor' ? (\n <CommunicatorProvider {...props}>\n <EditorProvidersWrapper>{children}</EditorProvidersWrapper>\n </CommunicatorProvider>\n ) : (\n <IframeCheckRenderer fallback={children}>\n <CommunicatorProvider {...props}>\n <EditorEnabledCheckRenderer fallback={children}>\n <EditorProvidersWrapper>{children}</EditorProvidersWrapper>\n </EditorEnabledCheckRenderer>\n </CommunicatorProvider>\n </IframeCheckRenderer>\n )}\n </ConfigurationProvider>\n </EditorEnabledProvider>\n);\n"],"mappings":";;;;;;;;;;;;;;;;;AAiCA,MAAMA,0BAAiD,EAAE,eAAe;CACtE,MAAM,wBAAwB,0BAA0B;AAExD,iBAAgB;AACd,yBAAuB;IACtB,EAAE,CAAC;AAEN,QACE,oBAAC,wCACC,oBAAC,mCACC,oBAAC,2BAAyB,WAAmC,GACvC,GACG;;;;;AAWjC,MAAMC,8BAAoE,EACxE,UACA,eACI;CACJ,MAAM,mBAAmB,0BAA0B;CAEnD,MAAM,EAAE,YAAY,kBAAkB;AAEtC,iBAAgB;AACd,MAAI,QAAS;AAGb,oBAAkB;IACjB,CAAC,QAAQ,CAAC;AAEb,QAAO,UAAU,WAAW;;;;;;AAO9B,MAAMC,uBAA6D,EACjE,UACA,eACI;CACJ,MAAM,CAAC,YAAY,iBAAiB,SAAS,MAAM;AAEnD,iBAAgB;AACd,gBAAc,OAAO,SAAS,OAAO,IAAI;IACxC,EAAE,CAAC;AAEN,QAAO,aAAa,WAAW;;AAQjC,MAAaC,kBAA8D,EACzE,UACA,cACA,GAAG,YAEH,oBAAC,mCACC,oBAAC;CAAqC;WACnC,MAAM,SAAS,WACd,oBAAC;EAAqB,GAAI;YACxB,oBAAC,0BAAwB,WAAkC;GACtC,GAEvB,oBAAC;EAAoB,UAAU;YAC7B,oBAAC;GAAqB,GAAI;aACxB,oBAAC;IAA2B,UAAU;cACpC,oBAAC,0BAAwB,WAAkC;KAChC;IACR;GACH;EAEF,GACF"}
@@ -1,58 +1,51 @@
1
- "use client";
1
+ 'use client';
2
+
3
+
4
+ import { useCrossFrameState } from "./useCrossFrameState.mjs";
5
+ import { createContext, useContext } from "react";
2
6
  import { jsx } from "react/jsx-runtime";
3
7
  import { MessageKey } from "@intlayer/editor";
4
- import {
5
- createContext,
6
- useContext
7
- } from "react";
8
- import { useCrossFrameState } from "./useCrossFrameState.mjs";
8
+
9
+ //#region src/FocusDictionaryContext.tsx
9
10
  const FocusDictionaryStateContext = createContext(void 0);
10
11
  const FocusDictionaryActionsContext = createContext(void 0);
11
- const FocusDictionaryProvider = ({
12
- children
13
- }) => {
14
- const [focusedContent, setFocusedContent] = useCrossFrameState(
15
- MessageKey.INTLAYER_FOCUSED_CONTENT_CHANGED,
16
- null
17
- );
18
- const setFocusedContentKeyPath = (keyPath) => {
19
- setFocusedContent((prev) => {
20
- if (!prev) {
21
- return prev;
22
- }
23
- return { ...prev, keyPath };
24
- });
25
- };
26
- return /* @__PURE__ */ jsx(FocusDictionaryStateContext.Provider, { value: { focusedContent }, children: /* @__PURE__ */ jsx(
27
- FocusDictionaryActionsContext.Provider,
28
- {
29
- value: { setFocusedContent, setFocusedContentKeyPath },
30
- children
31
- }
32
- ) });
12
+ const FocusDictionaryProvider = ({ children }) => {
13
+ const [focusedContent, setFocusedContent] = useCrossFrameState(MessageKey.INTLAYER_FOCUSED_CONTENT_CHANGED, null);
14
+ const setFocusedContentKeyPath = (keyPath) => {
15
+ setFocusedContent((prev) => {
16
+ if (!prev) return prev;
17
+ return {
18
+ ...prev,
19
+ keyPath
20
+ };
21
+ });
22
+ };
23
+ return /* @__PURE__ */ jsx(FocusDictionaryStateContext.Provider, {
24
+ value: { focusedContent },
25
+ children: /* @__PURE__ */ jsx(FocusDictionaryActionsContext.Provider, {
26
+ value: {
27
+ setFocusedContent,
28
+ setFocusedContentKeyPath
29
+ },
30
+ children
31
+ })
32
+ });
33
33
  };
34
34
  const useFocusDictionaryActions = () => {
35
- const context = useContext(FocusDictionaryActionsContext);
36
- if (context === void 0) {
37
- throw new Error(
38
- "useFocusDictionaryActions must be used within a FocusDictionaryProvider"
39
- );
40
- }
41
- return context;
35
+ const context = useContext(FocusDictionaryActionsContext);
36
+ if (context === void 0) throw new Error("useFocusDictionaryActions must be used within a FocusDictionaryProvider");
37
+ return context;
42
38
  };
43
39
  const useFocusDictionary = () => {
44
- const actionContext = useFocusDictionaryActions();
45
- const stateContext = useContext(FocusDictionaryStateContext);
46
- if (stateContext === void 0) {
47
- throw new Error(
48
- "useFocusDictionaryState must be used within a FocusDictionaryProvider"
49
- );
50
- }
51
- return { ...stateContext, ...actionContext };
52
- };
53
- export {
54
- FocusDictionaryProvider,
55
- useFocusDictionary,
56
- useFocusDictionaryActions
40
+ const actionContext = useFocusDictionaryActions();
41
+ const stateContext = useContext(FocusDictionaryStateContext);
42
+ if (stateContext === void 0) throw new Error("useFocusDictionaryState must be used within a FocusDictionaryProvider");
43
+ return {
44
+ ...stateContext,
45
+ ...actionContext
46
+ };
57
47
  };
48
+
49
+ //#endregion
50
+ export { FocusDictionaryProvider, useFocusDictionary, useFocusDictionaryActions };
58
51
  //# sourceMappingURL=FocusDictionaryContext.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/FocusDictionaryContext.tsx"],"sourcesContent":["'use client';\n\nimport type { KeyPath } from '@intlayer/core';\nimport { MessageKey } from '@intlayer/editor';\nimport {\n createContext,\n useContext,\n type Dispatch,\n type FC,\n type PropsWithChildren,\n type SetStateAction,\n} from 'react';\nimport { useCrossFrameState } from './useCrossFrameState';\n\nexport type FileContent = {\n dictionaryKey: string;\n dictionaryLocalId?: string;\n keyPath?: KeyPath[];\n};\n\ntype FocusDictionaryState = {\n focusedContent: FileContent | null;\n};\n\ntype FocusDictionaryActions = {\n setFocusedContent: Dispatch<SetStateAction<FileContent | null>>;\n setFocusedContentKeyPath: (keyPath: KeyPath[]) => void;\n};\n\nconst FocusDictionaryStateContext = createContext<\n FocusDictionaryState | undefined\n>(undefined);\nconst FocusDictionaryActionsContext = createContext<\n FocusDictionaryActions | undefined\n>(undefined);\n\nexport const FocusDictionaryProvider: FC<PropsWithChildren> = ({\n children,\n}) => {\n const [focusedContent, setFocusedContent] =\n useCrossFrameState<FileContent | null>(\n MessageKey.INTLAYER_FOCUSED_CONTENT_CHANGED,\n null\n );\n\n const setFocusedContentKeyPath = (keyPath: KeyPath[]) => {\n setFocusedContent((prev) => {\n if (!prev) {\n return prev; // nothing to update if there's no focused content\n }\n return { ...prev, keyPath };\n });\n };\n\n return (\n <FocusDictionaryStateContext.Provider value={{ focusedContent }}>\n <FocusDictionaryActionsContext.Provider\n value={{ setFocusedContent, setFocusedContentKeyPath }}\n >\n {children}\n </FocusDictionaryActionsContext.Provider>\n </FocusDictionaryStateContext.Provider>\n );\n};\n\nexport const useFocusDictionaryActions = () => {\n const context = useContext(FocusDictionaryActionsContext);\n if (context === undefined) {\n throw new Error(\n 'useFocusDictionaryActions must be used within a FocusDictionaryProvider'\n );\n }\n return context;\n};\n\nexport const useFocusDictionary = () => {\n const actionContext = useFocusDictionaryActions();\n const stateContext = useContext(FocusDictionaryStateContext);\n\n if (stateContext === undefined) {\n throw new Error(\n 'useFocusDictionaryState must be used within a FocusDictionaryProvider'\n );\n }\n\n return { ...stateContext, ...actionContext };\n};\n"],"mappings":";AAwDM;AArDN,SAAS,kBAAkB;AAC3B;AAAA,EACE;AAAA,EACA;AAAA,OAKK;AACP,SAAS,0BAA0B;AAiBnC,MAAM,8BAA8B,cAElC,MAAS;AACX,MAAM,gCAAgC,cAEpC,MAAS;AAEJ,MAAM,0BAAiD,CAAC;AAAA,EAC7D;AACF,MAAM;AACJ,QAAM,CAAC,gBAAgB,iBAAiB,IACtC;AAAA,IACE,WAAW;AAAA,IACX;AAAA,EACF;AAEF,QAAM,2BAA2B,CAAC,YAAuB;AACvD,sBAAkB,CAAC,SAAS;AAC1B,UAAI,CAAC,MAAM;AACT,eAAO;AAAA,MACT;AACA,aAAO,EAAE,GAAG,MAAM,QAAQ;AAAA,IAC5B,CAAC;AAAA,EACH;AAEA,SACE,oBAAC,4BAA4B,UAA5B,EAAqC,OAAO,EAAE,eAAe,GAC5D;AAAA,IAAC,8BAA8B;AAAA,IAA9B;AAAA,MACC,OAAO,EAAE,mBAAmB,yBAAyB;AAAA,MAEpD;AAAA;AAAA,EACH,GACF;AAEJ;AAEO,MAAM,4BAA4B,MAAM;AAC7C,QAAM,UAAU,WAAW,6BAA6B;AACxD,MAAI,YAAY,QAAW;AACzB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;AAEO,MAAM,qBAAqB,MAAM;AACtC,QAAM,gBAAgB,0BAA0B;AAChD,QAAM,eAAe,WAAW,2BAA2B;AAE3D,MAAI,iBAAiB,QAAW;AAC9B,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,SAAO,EAAE,GAAG,cAAc,GAAG,cAAc;AAC7C;","names":[]}
1
+ {"version":3,"file":"FocusDictionaryContext.mjs","names":["FocusDictionaryProvider: FC<PropsWithChildren>"],"sources":["../../src/FocusDictionaryContext.tsx"],"sourcesContent":["'use client';\n\nimport { MessageKey } from '@intlayer/editor';\nimport type { KeyPath } from '@intlayer/types';\nimport {\n createContext,\n type Dispatch,\n type FC,\n type PropsWithChildren,\n type SetStateAction,\n useContext,\n} from 'react';\nimport { useCrossFrameState } from './useCrossFrameState';\n\nexport type FileContent = {\n dictionaryKey: string;\n dictionaryLocalId?: string;\n keyPath?: KeyPath[];\n};\n\ntype FocusDictionaryState = {\n focusedContent: FileContent | null;\n};\n\ntype FocusDictionaryActions = {\n setFocusedContent: Dispatch<SetStateAction<FileContent | null>>;\n setFocusedContentKeyPath: (keyPath: KeyPath[]) => void;\n};\n\nconst FocusDictionaryStateContext = createContext<\n FocusDictionaryState | undefined\n>(undefined);\nconst FocusDictionaryActionsContext = createContext<\n FocusDictionaryActions | undefined\n>(undefined);\n\nexport const FocusDictionaryProvider: FC<PropsWithChildren> = ({\n children,\n}) => {\n const [focusedContent, setFocusedContent] =\n useCrossFrameState<FileContent | null>(\n MessageKey.INTLAYER_FOCUSED_CONTENT_CHANGED,\n null\n );\n\n const setFocusedContentKeyPath = (keyPath: KeyPath[]) => {\n setFocusedContent((prev) => {\n if (!prev) {\n return prev; // nothing to update if there's no focused content\n }\n return { ...prev, keyPath };\n });\n };\n\n return (\n <FocusDictionaryStateContext.Provider value={{ focusedContent }}>\n <FocusDictionaryActionsContext.Provider\n value={{ setFocusedContent, setFocusedContentKeyPath }}\n >\n {children}\n </FocusDictionaryActionsContext.Provider>\n </FocusDictionaryStateContext.Provider>\n );\n};\n\nexport const useFocusDictionaryActions = () => {\n const context = useContext(FocusDictionaryActionsContext);\n if (context === undefined) {\n throw new Error(\n 'useFocusDictionaryActions must be used within a FocusDictionaryProvider'\n );\n }\n return context;\n};\n\nexport const useFocusDictionary = () => {\n const actionContext = useFocusDictionaryActions();\n const stateContext = useContext(FocusDictionaryStateContext);\n\n if (stateContext === undefined) {\n throw new Error(\n 'useFocusDictionaryState must be used within a FocusDictionaryProvider'\n );\n }\n\n return { ...stateContext, ...actionContext };\n};\n"],"mappings":";;;;;;;;;AA6BA,MAAM,8BAA8B,cAElC,OAAU;AACZ,MAAM,gCAAgC,cAEpC,OAAU;AAEZ,MAAaA,2BAAkD,EAC7D,eACI;CACJ,MAAM,CAAC,gBAAgB,qBACrB,mBACE,WAAW,kCACX,KACD;CAEH,MAAM,4BAA4B,YAAuB;AACvD,qBAAmB,SAAS;AAC1B,OAAI,CAAC,KACH,QAAO;AAET,UAAO;IAAE,GAAG;IAAM;IAAS;IAC3B;;AAGJ,QACE,oBAAC,4BAA4B;EAAS,OAAO,EAAE,gBAAgB;YAC7D,oBAAC,8BAA8B;GAC7B,OAAO;IAAE;IAAmB;IAA0B;GAErD;IACsC;GACJ;;AAI3C,MAAa,kCAAkC;CAC7C,MAAM,UAAU,WAAW,8BAA8B;AACzD,KAAI,YAAY,OACd,OAAM,IAAI,MACR,0EACD;AAEH,QAAO;;AAGT,MAAa,2BAA2B;CACtC,MAAM,gBAAgB,2BAA2B;CACjD,MAAM,eAAe,WAAW,4BAA4B;AAE5D,KAAI,iBAAiB,OACnB,OAAM,IAAI,MACR,wEACD;AAGH,QAAO;EAAE,GAAG;EAAc,GAAG;EAAe"}
@@ -1,13 +1,15 @@
1
- export * from "@intlayer/editor";
2
- export * from "./CommunicatorContext.mjs";
3
- export * from "./ConfigurationContext.mjs";
4
- export * from "./DictionariesRecordContext.mjs";
5
- export * from "./EditedContentContext.mjs";
6
- export * from "./EditorEnabledContext.mjs";
7
- export * from "./EditorProvider.mjs";
8
- export * from "./FocusDictionaryContext.mjs";
9
- export * from "./useCrossFrameMessageListener.mjs";
10
- export * from "./useCrossFrameState.mjs";
11
- export * from "./useCrossURLPathState.mjs";
12
- export * from "./useIframeClickInterceptor.mjs";
13
- //# sourceMappingURL=index.mjs.map
1
+ import { CommunicatorProvider, useCommunicator } from "./CommunicatorContext.mjs";
2
+ import { useCrossFrameMessageListener } from "./useCrossFrameMessageListener.mjs";
3
+ import { useCrossFrameState } from "./useCrossFrameState.mjs";
4
+ import { ConfigurationProvider, useConfiguration, useConfigurationState } from "./ConfigurationContext.mjs";
5
+ import { DictionariesRecordProvider, useDictionariesRecord, useDictionariesRecordActions } from "./DictionariesRecordContext.mjs";
6
+ import { EditedContentProvider, useEditedContent, useEditedContentActions, useGetEditedContentState, usePostEditedContentState } from "./EditedContentContext.mjs";
7
+ import { EditorEnabledProvider, useEditorEnabled, useEditorEnabledState, useGetEditorEnabledState, usePostEditorEnabledState } from "./EditorEnabledContext.mjs";
8
+ import { FocusDictionaryProvider, useFocusDictionary, useFocusDictionaryActions } from "./FocusDictionaryContext.mjs";
9
+ import { EditorProvider } from "./EditorProvider.mjs";
10
+ import { useCrossURLPathSetter, useCrossURLPathState } from "./useCrossURLPathState.mjs";
11
+ import { useIframeClickInterceptor, useIframeClickMerger } from "./useIframeClickInterceptor.mjs";
12
+
13
+ export * from "@intlayer/editor"
14
+
15
+ export { CommunicatorProvider, ConfigurationProvider, DictionariesRecordProvider, EditedContentProvider, EditorEnabledProvider, EditorProvider, FocusDictionaryProvider, useCommunicator, useConfiguration, useConfigurationState, useCrossFrameMessageListener, useCrossFrameState, useCrossURLPathSetter, useCrossURLPathState, useDictionariesRecord, useDictionariesRecordActions, useEditedContent, useEditedContentActions, useEditorEnabled, useEditorEnabledState, useFocusDictionary, useFocusDictionaryActions, useGetEditedContentState, useGetEditorEnabledState, useIframeClickInterceptor, useIframeClickMerger, usePostEditedContentState, usePostEditorEnabledState };
@@ -1,29 +1,73 @@
1
- "use client";
2
- import { compareUrls } from "@intlayer/editor";
3
- import { useEffect } from "react";
1
+ 'use client';
2
+
3
+
4
4
  import { useCommunicator } from "./CommunicatorContext.mjs";
5
+ import { useEffect } from "react";
6
+ import { compareUrls } from "@intlayer/editor";
7
+
8
+ //#region src/useCrossFrameMessageListener.tsx
9
+ /**
10
+ * useCrossFrameMessageListener
11
+ *
12
+ * This React hook listens for messages sent via the `postMessage` API and triggers a callback
13
+ * whenever a message of the specified type (`key`) is received. It is useful for synchronizing
14
+ * state or events across different windows, iframes, or contexts.
15
+ *
16
+ * @template S - The type of the data payload in the message.
17
+ * @param key - A unique identifier for the message type to listen for.
18
+ * @param [onEventTriggered] - A callback function triggered when a message
19
+ * @param [revalidator] - A function that re-suscribes the listener. Could be usefull if onEventTriggered depend of some state
20
+ * with the specified key is received. The callback receives the message data as its argument.
21
+ *
22
+ * @returns {{ postMessage: (data: S) => void }} An object containing a `postMessage` function
23
+ * that allows broadcasting messages with the specified key and data.
24
+ */
5
25
  const useCrossFrameMessageListener = (key, onEventTriggered, revalidator) => {
6
- const { allowedOrigins, postMessage, senderId } = useCommunicator();
7
- useEffect(() => {
8
- if (onEventTriggered) {
9
- const handleMessage = (event) => {
10
- const { type, data, senderId: msgSenderId } = event.data;
11
- if (type !== key) return;
12
- if (msgSenderId === senderId) return;
13
- if (typeof allowedOrigins === "undefined" || allowedOrigins?.some((url) => compareUrls(url, event.origin)) || allowedOrigins?.includes("*")) {
14
- onEventTriggered(data);
15
- }
16
- };
17
- window.addEventListener("message", handleMessage);
18
- return () => window.removeEventListener("message", handleMessage);
19
- }
20
- }, [allowedOrigins, postMessage, senderId, revalidator]);
21
- const postMessageWrapper = (data) => {
22
- postMessage({ type: key, data, senderId });
23
- };
24
- return postMessageWrapper;
25
- };
26
- export {
27
- useCrossFrameMessageListener
26
+ const { allowedOrigins, postMessage, senderId } = useCommunicator();
27
+ useEffect(() => {
28
+ if (onEventTriggered) {
29
+ /**
30
+ * Message handler to process incoming messages.
31
+ *
32
+ * - **Message Filtering:** Ensures only messages with the specified `key` are processed.
33
+ * - **Origin Validation:** Checks that the origin of the message is within the allowed origins.
34
+ *
35
+ * @param {MessageEvent<{ type: string; data: S }>} event - The incoming message event object.
36
+ */
37
+ const handleMessage = (event) => {
38
+ const { type, data, senderId: msgSenderId } = event.data;
39
+ if (type !== key) return;
40
+ if (msgSenderId === senderId) return;
41
+ if (typeof allowedOrigins === "undefined" || allowedOrigins?.some((url) => compareUrls(url, event.origin)) || allowedOrigins?.includes("*")) onEventTriggered(data);
42
+ };
43
+ window.addEventListener("message", handleMessage);
44
+ return () => window.removeEventListener("message", handleMessage);
45
+ }
46
+ }, [
47
+ allowedOrigins,
48
+ postMessage,
49
+ senderId,
50
+ revalidator
51
+ ]);
52
+ /**
53
+ * A wrapper function around the `postMessage` function to broadcast messages efficiently.
54
+ *
55
+ * - **Encapsulation:** Ensures the `postMessage` function is scoped to the provided key.
56
+ * - **Ease of Use:** Simplifies broadcasting messages with consistent type and format.
57
+ *
58
+ * @param {S} data - The data payload to include in the message.
59
+ * @returns {void}
60
+ */
61
+ const postMessageWrapper = (data) => {
62
+ postMessage({
63
+ type: key,
64
+ data,
65
+ senderId
66
+ });
67
+ };
68
+ return postMessageWrapper;
28
69
  };
70
+
71
+ //#endregion
72
+ export { useCrossFrameMessageListener };
29
73
  //# sourceMappingURL=useCrossFrameMessageListener.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/useCrossFrameMessageListener.tsx"],"sourcesContent":["'use client';\n\nimport { type MessageKey, compareUrls } from '@intlayer/editor';\nimport { useEffect } from 'react';\nimport { useCommunicator } from './CommunicatorContext';\n\n/**\n * useCrossFrameMessageListener\n *\n * This React hook listens for messages sent via the `postMessage` API and triggers a callback\n * whenever a message of the specified type (`key`) is received. It is useful for synchronizing\n * state or events across different windows, iframes, or contexts.\n *\n * @template S - The type of the data payload in the message.\n * @param key - A unique identifier for the message type to listen for.\n * @param [onEventTriggered] - A callback function triggered when a message\n * @param [revalidator] - A function that re-suscribes the listener. Could be usefull if onEventTriggered depend of some state\n * with the specified key is received. The callback receives the message data as its argument.\n *\n * @returns {{ postMessage: (data: S) => void }} An object containing a `postMessage` function\n * that allows broadcasting messages with the specified key and data.\n */\nexport const useCrossFrameMessageListener = <S,>(\n key: `${MessageKey}` | `${MessageKey}/post` | `${MessageKey}/get`,\n onEventTriggered?: (data: S) => void,\n revalidator?: any\n) => {\n const { allowedOrigins, postMessage, senderId } = useCommunicator();\n\n useEffect(() => {\n if (onEventTriggered) {\n /**\n * Message handler to process incoming messages.\n *\n * - **Message Filtering:** Ensures only messages with the specified `key` are processed.\n * - **Origin Validation:** Checks that the origin of the message is within the allowed origins.\n *\n * @param {MessageEvent<{ type: string; data: S }>} event - The incoming message event object.\n */\n const handleMessage = (\n event: MessageEvent<{ type: string; data: S; senderId: string }>\n ) => {\n const { type, data, senderId: msgSenderId } = event.data;\n\n // Ignore messages that do not match the current key\n if (type !== key) return;\n\n // Ignore messages from myself\n if (msgSenderId === senderId) return;\n\n // Check if the message origin is allowed\n if (\n typeof allowedOrigins === 'undefined' ||\n allowedOrigins?.some((url) => compareUrls(url, event.origin)) ||\n allowedOrigins?.includes('*')\n ) {\n // Update the local state with the received data\n onEventTriggered(data);\n }\n };\n\n window.addEventListener('message', handleMessage);\n\n // Clean up the event listener on unmount\n return () => window.removeEventListener('message', handleMessage);\n }\n }, [allowedOrigins, postMessage, senderId, revalidator]);\n\n /**\n * A wrapper function around the `postMessage` function to broadcast messages efficiently.\n *\n * - **Encapsulation:** Ensures the `postMessage` function is scoped to the provided key.\n * - **Ease of Use:** Simplifies broadcasting messages with consistent type and format.\n *\n * @param {S} data - The data payload to include in the message.\n * @returns {void}\n */\n const postMessageWrapper: (data?: S) => void = (data) => {\n postMessage({ type: key, data, senderId });\n };\n\n return postMessageWrapper;\n};\n"],"mappings":";AAEA,SAA0B,mBAAmB;AAC7C,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;AAkBzB,MAAM,+BAA+B,CAC1C,KACA,kBACA,gBACG;AACH,QAAM,EAAE,gBAAgB,aAAa,SAAS,IAAI,gBAAgB;AAElE,YAAU,MAAM;AACd,QAAI,kBAAkB;AASpB,YAAM,gBAAgB,CACpB,UACG;AACH,cAAM,EAAE,MAAM,MAAM,UAAU,YAAY,IAAI,MAAM;AAGpD,YAAI,SAAS,IAAK;AAGlB,YAAI,gBAAgB,SAAU;AAG9B,YACE,OAAO,mBAAmB,eAC1B,gBAAgB,KAAK,CAAC,QAAQ,YAAY,KAAK,MAAM,MAAM,CAAC,KAC5D,gBAAgB,SAAS,GAAG,GAC5B;AAEA,2BAAiB,IAAI;AAAA,QACvB;AAAA,MACF;AAEA,aAAO,iBAAiB,WAAW,aAAa;AAGhD,aAAO,MAAM,OAAO,oBAAoB,WAAW,aAAa;AAAA,IAClE;AAAA,EACF,GAAG,CAAC,gBAAgB,aAAa,UAAU,WAAW,CAAC;AAWvD,QAAM,qBAAyC,CAAC,SAAS;AACvD,gBAAY,EAAE,MAAM,KAAK,MAAM,SAAS,CAAC;AAAA,EAC3C;AAEA,SAAO;AACT;","names":[]}
1
+ {"version":3,"file":"useCrossFrameMessageListener.mjs","names":["postMessageWrapper: (data?: S) => void"],"sources":["../../src/useCrossFrameMessageListener.tsx"],"sourcesContent":["'use client';\n\nimport { compareUrls, type MessageKey } from '@intlayer/editor';\nimport { useEffect } from 'react';\nimport { useCommunicator } from './CommunicatorContext';\n\n/**\n * useCrossFrameMessageListener\n *\n * This React hook listens for messages sent via the `postMessage` API and triggers a callback\n * whenever a message of the specified type (`key`) is received. It is useful for synchronizing\n * state or events across different windows, iframes, or contexts.\n *\n * @template S - The type of the data payload in the message.\n * @param key - A unique identifier for the message type to listen for.\n * @param [onEventTriggered] - A callback function triggered when a message\n * @param [revalidator] - A function that re-suscribes the listener. Could be usefull if onEventTriggered depend of some state\n * with the specified key is received. The callback receives the message data as its argument.\n *\n * @returns {{ postMessage: (data: S) => void }} An object containing a `postMessage` function\n * that allows broadcasting messages with the specified key and data.\n */\nexport const useCrossFrameMessageListener = <S,>(\n key: `${MessageKey}` | `${MessageKey}/post` | `${MessageKey}/get`,\n onEventTriggered?: (data: S) => void,\n revalidator?: any\n) => {\n const { allowedOrigins, postMessage, senderId } = useCommunicator();\n\n useEffect(() => {\n if (onEventTriggered) {\n /**\n * Message handler to process incoming messages.\n *\n * - **Message Filtering:** Ensures only messages with the specified `key` are processed.\n * - **Origin Validation:** Checks that the origin of the message is within the allowed origins.\n *\n * @param {MessageEvent<{ type: string; data: S }>} event - The incoming message event object.\n */\n const handleMessage = (\n event: MessageEvent<{ type: string; data: S; senderId: string }>\n ) => {\n const { type, data, senderId: msgSenderId } = event.data;\n\n // Ignore messages that do not match the current key\n if (type !== key) return;\n\n // Ignore messages from myself\n if (msgSenderId === senderId) return;\n\n // Check if the message origin is allowed\n if (\n typeof allowedOrigins === 'undefined' ||\n allowedOrigins?.some((url) => compareUrls(url, event.origin)) ||\n allowedOrigins?.includes('*')\n ) {\n // Update the local state with the received data\n onEventTriggered(data);\n }\n };\n\n window.addEventListener('message', handleMessage);\n\n // Clean up the event listener on unmount\n return () => window.removeEventListener('message', handleMessage);\n }\n }, [allowedOrigins, postMessage, senderId, revalidator]);\n\n /**\n * A wrapper function around the `postMessage` function to broadcast messages efficiently.\n *\n * - **Encapsulation:** Ensures the `postMessage` function is scoped to the provided key.\n * - **Ease of Use:** Simplifies broadcasting messages with consistent type and format.\n *\n * @param {S} data - The data payload to include in the message.\n * @returns {void}\n */\n const postMessageWrapper: (data?: S) => void = (data) => {\n postMessage({ type: key, data, senderId });\n };\n\n return postMessageWrapper;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAsBA,MAAa,gCACX,KACA,kBACA,gBACG;CACH,MAAM,EAAE,gBAAgB,aAAa,aAAa,iBAAiB;AAEnE,iBAAgB;AACd,MAAI,kBAAkB;;;;;;;;;GASpB,MAAM,iBACJ,UACG;IACH,MAAM,EAAE,MAAM,MAAM,UAAU,gBAAgB,MAAM;AAGpD,QAAI,SAAS,IAAK;AAGlB,QAAI,gBAAgB,SAAU;AAG9B,QACE,OAAO,mBAAmB,eAC1B,gBAAgB,MAAM,QAAQ,YAAY,KAAK,MAAM,OAAO,CAAC,IAC7D,gBAAgB,SAAS,IAAI,CAG7B,kBAAiB,KAAK;;AAI1B,UAAO,iBAAiB,WAAW,cAAc;AAGjD,gBAAa,OAAO,oBAAoB,WAAW,cAAc;;IAElE;EAAC;EAAgB;EAAa;EAAU;EAAY,CAAC;;;;;;;;;;CAWxD,MAAMA,sBAA0C,SAAS;AACvD,cAAY;GAAE,MAAM;GAAK;GAAM;GAAU,CAAC;;AAG5C,QAAO"}
@@ -1,53 +1,108 @@
1
- "use client";
2
- import { useEffect, useState } from "react";
1
+ 'use client';
2
+
3
+
3
4
  import { useCommunicator } from "./CommunicatorContext.mjs";
4
5
  import { useCrossFrameMessageListener } from "./useCrossFrameMessageListener.mjs";
6
+ import { useEffect, useState } from "react";
7
+
8
+ //#region src/useCrossFrameState.tsx
5
9
  const resolveState = (state, prevState) => typeof state === "function" ? state(prevState) : state;
10
+ /**
11
+ * Configuration options for `useCrossFrameState`.
12
+ * @typedef {Object} CrossFrameStateOptions
13
+ * @property {boolean} [emit=true] - Whether to broadcast state changes to other instances.
14
+ * @property {boolean} [receive=true] - Whether to listen for state updates from other instances.
15
+ */
16
+ /**
17
+ * useCrossFrameState
18
+ *
19
+ * This React hook synchronizes state across multiple instances (e.g., different iframes or windows).
20
+ * It uses the `postMessage` API to communicate state changes and updates between instances.
21
+ *
22
+ * @template S - The type of the state.
23
+ * @param key - A unique identifier for the state to synchronize.
24
+ * @param initialState - The initial state value or a function to compute it lazily.
25
+ * @param options - Configuration options to control emitting and receiving messages.
26
+ * - `emit` (default: true): Whether to broadcast state changes to other instances.
27
+ * - `receive` (default: true): Whether to listen for state updates from other instances.
28
+ *
29
+ * @returns {[S, Dispatch<SetStateAction<S>>]} An array containing the current state and a setter function.
30
+ */
6
31
  const useCrossFrameState = (key, initialState, options) => {
7
- const { postMessage, senderId } = useCommunicator();
8
- const { emit, receive } = options ?? { emit: true, receive: true };
9
- const handleStateChange = (state2, prevState) => {
10
- const resolvedState = resolveState(state2, prevState);
11
- if (emit && typeof postMessage === "function" && typeof resolvedState !== "undefined") {
12
- postMessage({ type: `${key}/post`, data: resolvedState, senderId });
13
- }
14
- return resolvedState;
15
- };
16
- const postState = () => {
17
- if (typeof postMessage !== "function") return;
18
- postMessage({ type: `${key}/post`, data: state, senderId });
19
- };
20
- const [state, setState] = useState(() => handleStateChange(initialState));
21
- const setStateWrapper = (valueOrUpdater) => setState((prevState) => handleStateChange(valueOrUpdater, prevState));
22
- useCrossFrameMessageListener(
23
- `${key}/post`,
24
- // Only activate the state listener if the `receive` option is true
25
- receive ? (data) => {
26
- setState(data);
27
- } : void 0
28
- );
29
- const onGetMessage = (_, originSenderId) => {
30
- if (!emit) return;
31
- if (typeof postMessage !== "function") return;
32
- if (originSenderId === senderId) return;
33
- if (typeof state === "undefined") return;
34
- postMessage({ type: `${key}/post`, data: state, senderId });
35
- };
36
- useCrossFrameMessageListener(
37
- `${key}/get`,
38
- // Only activate the state listener if the `emit` option is true
39
- emit ? onGetMessage : void 0,
40
- state
41
- // Revalidate the listener if the state changes
42
- );
43
- useEffect(() => {
44
- if (receive && typeof postMessage === "function" && typeof state === "undefined") {
45
- postMessage({ type: `${key}/get`, senderId });
46
- }
47
- }, []);
48
- return [state, setStateWrapper, postState];
49
- };
50
- export {
51
- useCrossFrameState
32
+ const { postMessage, senderId } = useCommunicator();
33
+ const { emit, receive } = options ?? {
34
+ emit: true,
35
+ receive: true
36
+ };
37
+ const handleStateChange = (state$1, prevState) => {
38
+ const resolvedState = resolveState(state$1, prevState);
39
+ if (emit && typeof postMessage === "function" && typeof resolvedState !== "undefined") postMessage({
40
+ type: `${key}/post`,
41
+ data: resolvedState,
42
+ senderId
43
+ });
44
+ return resolvedState;
45
+ };
46
+ const postState = () => {
47
+ if (typeof postMessage !== "function") return;
48
+ postMessage({
49
+ type: `${key}/post`,
50
+ data: state,
51
+ senderId
52
+ });
53
+ };
54
+ const [state, setState] = useState(() => handleStateChange(initialState));
55
+ /**
56
+ * A wrapper function around the `setState` function to handle messaging efficiently.
57
+ *
58
+ * This approach has several advantages over using an additional `useEffect`:
59
+ * - **Avoid Redundant Re-renders:** By emitting the message directly within the `setState` logic,
60
+ * it prevents the extra render cycle that would be triggered when using `useEffect`.
61
+ * - **Consistency:** Ensures the message is emitted immediately when the state is updated,
62
+ * avoiding potential delays caused by the asynchronous nature of `useEffect`.
63
+ *
64
+ * This function keeps the same API as `setState` and is memoized using `useCallback`
65
+ * to prevent unnecessary re-renders of dependent components.
66
+ *
67
+ * @template S - The type of the state.
68
+ * @param {SetStateAction<S>} valueOrUpdater - The new state or a function to produce it.
69
+ * @returns {void}
70
+ */
71
+ const setStateWrapper = (valueOrUpdater) => setState((prevState) => handleStateChange(valueOrUpdater, prevState));
72
+ /**
73
+ * Listen for messages with the specified key and update the state accordingly.
74
+ */
75
+ useCrossFrameMessageListener(`${key}/post`, receive ? (data) => {
76
+ setState(data);
77
+ } : void 0);
78
+ const onGetMessage = (_, originSenderId) => {
79
+ if (!emit) return;
80
+ if (typeof postMessage !== "function") return;
81
+ if (originSenderId === senderId) return;
82
+ if (typeof state === "undefined") return;
83
+ postMessage({
84
+ type: `${key}/post`,
85
+ data: state,
86
+ senderId
87
+ });
88
+ };
89
+ /**
90
+ * Listen for messages request to get the state content and send it back.
91
+ */
92
+ useCrossFrameMessageListener(`${key}/get`, emit ? onGetMessage : void 0, state);
93
+ useEffect(() => {
94
+ if (receive && typeof postMessage === "function" && typeof state === "undefined") postMessage({
95
+ type: `${key}/get`,
96
+ senderId
97
+ });
98
+ }, []);
99
+ return [
100
+ state,
101
+ setStateWrapper,
102
+ postState
103
+ ];
52
104
  };
105
+
106
+ //#endregion
107
+ export { useCrossFrameState };
53
108
  //# sourceMappingURL=useCrossFrameState.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/useCrossFrameState.tsx"],"sourcesContent":["'use client';\n\nimport { type MessageKey } from '@intlayer/editor';\nimport { useEffect, useState, type Dispatch, type SetStateAction } from 'react';\nimport { useCommunicator } from './CommunicatorContext';\nimport { useCrossFrameMessageListener } from './useCrossFrameMessageListener';\n\nexport type CrossFrameStateOptions = {\n emit?: boolean;\n receive?: boolean;\n};\n\nconst resolveState = <S,>(state?: SetStateAction<S>, prevState?: S): S =>\n typeof state === 'function'\n ? (state as (prevState?: S) => S)(prevState)\n : (state as S);\n\n/**\n * Configuration options for `useCrossFrameState`.\n * @typedef {Object} CrossFrameStateOptions\n * @property {boolean} [emit=true] - Whether to broadcast state changes to other instances.\n * @property {boolean} [receive=true] - Whether to listen for state updates from other instances.\n */\n\n/**\n * useCrossFrameState\n *\n * This React hook synchronizes state across multiple instances (e.g., different iframes or windows).\n * It uses the `postMessage` API to communicate state changes and updates between instances.\n *\n * @template S - The type of the state.\n * @param key - A unique identifier for the state to synchronize.\n * @param initialState - The initial state value or a function to compute it lazily.\n * @param options - Configuration options to control emitting and receiving messages.\n * - `emit` (default: true): Whether to broadcast state changes to other instances.\n * - `receive` (default: true): Whether to listen for state updates from other instances.\n *\n * @returns {[S, Dispatch<SetStateAction<S>>]} An array containing the current state and a setter function.\n */\nexport const useCrossFrameState = <S,>(\n key: `${MessageKey}`,\n initialState?: S | (() => S),\n options?: CrossFrameStateOptions\n): [S, Dispatch<SetStateAction<S>>, typeof postState] => {\n const { postMessage, senderId } = useCommunicator();\n\n const { emit, receive } = options ?? { emit: true, receive: true };\n\n const handleStateChange = (state?: SetStateAction<S>, prevState?: S) => {\n // Initialize state from the provided initial value, if defined\n const resolvedState: S = resolveState(state, prevState);\n\n // Emit the initial state if `emit` is enabled and initial state is defined\n if (\n emit &&\n typeof postMessage === 'function' &&\n typeof resolvedState !== 'undefined'\n ) {\n postMessage({ type: `${key}/post`, data: resolvedState, senderId });\n }\n\n return resolvedState;\n };\n\n const postState = () => {\n if (typeof postMessage !== 'function') return;\n postMessage({ type: `${key}/post`, data: state, senderId });\n };\n\n const [state, setState] = useState<S>(() => handleStateChange(initialState));\n\n /**\n * A wrapper function around the `setState` function to handle messaging efficiently.\n *\n * This approach has several advantages over using an additional `useEffect`:\n * - **Avoid Redundant Re-renders:** By emitting the message directly within the `setState` logic,\n * it prevents the extra render cycle that would be triggered when using `useEffect`.\n * - **Consistency:** Ensures the message is emitted immediately when the state is updated,\n * avoiding potential delays caused by the asynchronous nature of `useEffect`.\n *\n * This function keeps the same API as `setState` and is memoized using `useCallback`\n * to prevent unnecessary re-renders of dependent components.\n *\n * @template S - The type of the state.\n * @param {SetStateAction<S>} valueOrUpdater - The new state or a function to produce it.\n * @returns {void}\n */\n const setStateWrapper: Dispatch<SetStateAction<S>> = (valueOrUpdater) =>\n setState((prevState) => handleStateChange(valueOrUpdater, prevState));\n\n /**\n * Listen for messages with the specified key and update the state accordingly.\n */\n useCrossFrameMessageListener<S>(\n `${key}/post`,\n // Only activate the state listener if the `receive` option is true\n receive\n ? (data) => {\n setState(data);\n }\n : undefined\n );\n\n const onGetMessage = (_: unknown, originSenderId?: string) => {\n if (!emit) return;\n if (typeof postMessage !== 'function') return;\n if (originSenderId === senderId) return;\n if (typeof state === 'undefined') return;\n\n postMessage({ type: `${key}/post`, data: state, senderId });\n };\n\n /**\n * Listen for messages request to get the state content and send it back.\n */\n useCrossFrameMessageListener<S>(\n `${key}/get`,\n // Only activate the state listener if the `emit` option is true\n emit ? onGetMessage : undefined,\n state // Revalidate the listener if the state changes\n );\n\n useEffect(() => {\n // If the component is mounted and the hook in receive mode,\n // Request the state from the other instance\n if (\n receive &&\n typeof postMessage === 'function' &&\n typeof state === 'undefined'\n ) {\n postMessage({ type: `${key}/get`, senderId });\n }\n }, []);\n\n // Return the useState state and setter\n return [state, setStateWrapper, postState];\n};\n"],"mappings":";AAGA,SAAS,WAAW,gBAAoD;AACxE,SAAS,uBAAuB;AAChC,SAAS,oCAAoC;AAO7C,MAAM,eAAe,CAAK,OAA2B,cACnD,OAAO,UAAU,aACZ,MAA+B,SAAS,IACxC;AAwBA,MAAM,qBAAqB,CAChC,KACA,cACA,YACuD;AACvD,QAAM,EAAE,aAAa,SAAS,IAAI,gBAAgB;AAElD,QAAM,EAAE,MAAM,QAAQ,IAAI,WAAW,EAAE,MAAM,MAAM,SAAS,KAAK;AAEjE,QAAM,oBAAoB,CAACA,QAA2B,cAAkB;AAEtE,UAAM,gBAAmB,aAAaA,QAAO,SAAS;AAGtD,QACE,QACA,OAAO,gBAAgB,cACvB,OAAO,kBAAkB,aACzB;AACA,kBAAY,EAAE,MAAM,GAAG,GAAG,SAAS,MAAM,eAAe,SAAS,CAAC;AAAA,IACpE;AAEA,WAAO;AAAA,EACT;AAEA,QAAM,YAAY,MAAM;AACtB,QAAI,OAAO,gBAAgB,WAAY;AACvC,gBAAY,EAAE,MAAM,GAAG,GAAG,SAAS,MAAM,OAAO,SAAS,CAAC;AAAA,EAC5D;AAEA,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAY,MAAM,kBAAkB,YAAY,CAAC;AAkB3E,QAAM,kBAA+C,CAAC,mBACpD,SAAS,CAAC,cAAc,kBAAkB,gBAAgB,SAAS,CAAC;AAKtE;AAAA,IACE,GAAG,GAAG;AAAA;AAAA,IAEN,UACI,CAAC,SAAS;AACR,eAAS,IAAI;AAAA,IACf,IACA;AAAA,EACN;AAEA,QAAM,eAAe,CAAC,GAAY,mBAA4B;AAC5D,QAAI,CAAC,KAAM;AACX,QAAI,OAAO,gBAAgB,WAAY;AACvC,QAAI,mBAAmB,SAAU;AACjC,QAAI,OAAO,UAAU,YAAa;AAElC,gBAAY,EAAE,MAAM,GAAG,GAAG,SAAS,MAAM,OAAO,SAAS,CAAC;AAAA,EAC5D;AAKA;AAAA,IACE,GAAG,GAAG;AAAA;AAAA,IAEN,OAAO,eAAe;AAAA,IACtB;AAAA;AAAA,EACF;AAEA,YAAU,MAAM;AAGd,QACE,WACA,OAAO,gBAAgB,cACvB,OAAO,UAAU,aACjB;AACA,kBAAY,EAAE,MAAM,GAAG,GAAG,QAAQ,SAAS,CAAC;AAAA,IAC9C;AAAA,EACF,GAAG,CAAC,CAAC;AAGL,SAAO,CAAC,OAAO,iBAAiB,SAAS;AAC3C;","names":["state"]}
1
+ {"version":3,"file":"useCrossFrameState.mjs","names":["resolvedState: S","state","setStateWrapper: Dispatch<SetStateAction<S>>"],"sources":["../../src/useCrossFrameState.tsx"],"sourcesContent":["'use client';\n\nimport type { MessageKey } from '@intlayer/editor';\nimport { type Dispatch, type SetStateAction, useEffect, useState } from 'react';\nimport { useCommunicator } from './CommunicatorContext';\nimport { useCrossFrameMessageListener } from './useCrossFrameMessageListener';\n\nexport type CrossFrameStateOptions = {\n emit?: boolean;\n receive?: boolean;\n};\n\nconst resolveState = <S,>(state?: SetStateAction<S>, prevState?: S): S =>\n typeof state === 'function'\n ? (state as (prevState?: S) => S)(prevState)\n : (state as S);\n\n/**\n * Configuration options for `useCrossFrameState`.\n * @typedef {Object} CrossFrameStateOptions\n * @property {boolean} [emit=true] - Whether to broadcast state changes to other instances.\n * @property {boolean} [receive=true] - Whether to listen for state updates from other instances.\n */\n\n/**\n * useCrossFrameState\n *\n * This React hook synchronizes state across multiple instances (e.g., different iframes or windows).\n * It uses the `postMessage` API to communicate state changes and updates between instances.\n *\n * @template S - The type of the state.\n * @param key - A unique identifier for the state to synchronize.\n * @param initialState - The initial state value or a function to compute it lazily.\n * @param options - Configuration options to control emitting and receiving messages.\n * - `emit` (default: true): Whether to broadcast state changes to other instances.\n * - `receive` (default: true): Whether to listen for state updates from other instances.\n *\n * @returns {[S, Dispatch<SetStateAction<S>>]} An array containing the current state and a setter function.\n */\nexport const useCrossFrameState = <S,>(\n key: `${MessageKey}`,\n initialState?: S | (() => S),\n options?: CrossFrameStateOptions\n): [S, Dispatch<SetStateAction<S>>, typeof postState] => {\n const { postMessage, senderId } = useCommunicator();\n\n const { emit, receive } = options ?? { emit: true, receive: true };\n\n const handleStateChange = (state?: SetStateAction<S>, prevState?: S) => {\n // Initialize state from the provided initial value, if defined\n const resolvedState: S = resolveState(state, prevState);\n\n // Emit the initial state if `emit` is enabled and initial state is defined\n if (\n emit &&\n typeof postMessage === 'function' &&\n typeof resolvedState !== 'undefined'\n ) {\n postMessage({ type: `${key}/post`, data: resolvedState, senderId });\n }\n\n return resolvedState;\n };\n\n const postState = () => {\n if (typeof postMessage !== 'function') return;\n postMessage({ type: `${key}/post`, data: state, senderId });\n };\n\n const [state, setState] = useState<S>(() => handleStateChange(initialState));\n\n /**\n * A wrapper function around the `setState` function to handle messaging efficiently.\n *\n * This approach has several advantages over using an additional `useEffect`:\n * - **Avoid Redundant Re-renders:** By emitting the message directly within the `setState` logic,\n * it prevents the extra render cycle that would be triggered when using `useEffect`.\n * - **Consistency:** Ensures the message is emitted immediately when the state is updated,\n * avoiding potential delays caused by the asynchronous nature of `useEffect`.\n *\n * This function keeps the same API as `setState` and is memoized using `useCallback`\n * to prevent unnecessary re-renders of dependent components.\n *\n * @template S - The type of the state.\n * @param {SetStateAction<S>} valueOrUpdater - The new state or a function to produce it.\n * @returns {void}\n */\n const setStateWrapper: Dispatch<SetStateAction<S>> = (valueOrUpdater) =>\n setState((prevState) => handleStateChange(valueOrUpdater, prevState));\n\n /**\n * Listen for messages with the specified key and update the state accordingly.\n */\n useCrossFrameMessageListener<S>(\n `${key}/post`,\n // Only activate the state listener if the `receive` option is true\n receive\n ? (data) => {\n setState(data);\n }\n : undefined\n );\n\n const onGetMessage = (_: unknown, originSenderId?: string) => {\n if (!emit) return;\n if (typeof postMessage !== 'function') return;\n if (originSenderId === senderId) return;\n if (typeof state === 'undefined') return;\n\n postMessage({ type: `${key}/post`, data: state, senderId });\n };\n\n /**\n * Listen for messages request to get the state content and send it back.\n */\n useCrossFrameMessageListener<S>(\n `${key}/get`,\n // Only activate the state listener if the `emit` option is true\n emit ? onGetMessage : undefined,\n state // Revalidate the listener if the state changes\n );\n\n useEffect(() => {\n // If the component is mounted and the hook in receive mode,\n // Request the state from the other instance\n if (\n receive &&\n typeof postMessage === 'function' &&\n typeof state === 'undefined'\n ) {\n postMessage({ type: `${key}/get`, senderId });\n }\n }, []);\n\n // Return the useState state and setter\n return [state, setStateWrapper, postState];\n};\n"],"mappings":";;;;;;;;AAYA,MAAM,gBAAoB,OAA2B,cACnD,OAAO,UAAU,aACZ,MAA+B,UAAU,GACzC;;;;;;;;;;;;;;;;;;;;;;AAwBP,MAAa,sBACX,KACA,cACA,YACuD;CACvD,MAAM,EAAE,aAAa,aAAa,iBAAiB;CAEnD,MAAM,EAAE,MAAM,YAAY,WAAW;EAAE,MAAM;EAAM,SAAS;EAAM;CAElE,MAAM,qBAAqB,SAA2B,cAAkB;EAEtE,MAAMA,gBAAmB,aAAaC,SAAO,UAAU;AAGvD,MACE,QACA,OAAO,gBAAgB,cACvB,OAAO,kBAAkB,YAEzB,aAAY;GAAE,MAAM,GAAG,IAAI;GAAQ,MAAM;GAAe;GAAU,CAAC;AAGrE,SAAO;;CAGT,MAAM,kBAAkB;AACtB,MAAI,OAAO,gBAAgB,WAAY;AACvC,cAAY;GAAE,MAAM,GAAG,IAAI;GAAQ,MAAM;GAAO;GAAU,CAAC;;CAG7D,MAAM,CAAC,OAAO,YAAY,eAAkB,kBAAkB,aAAa,CAAC;;;;;;;;;;;;;;;;;CAkB5E,MAAMC,mBAAgD,mBACpD,UAAU,cAAc,kBAAkB,gBAAgB,UAAU,CAAC;;;;AAKvE,8BACE,GAAG,IAAI,QAEP,WACK,SAAS;AACR,WAAS,KAAK;KAEhB,OACL;CAED,MAAM,gBAAgB,GAAY,mBAA4B;AAC5D,MAAI,CAAC,KAAM;AACX,MAAI,OAAO,gBAAgB,WAAY;AACvC,MAAI,mBAAmB,SAAU;AACjC,MAAI,OAAO,UAAU,YAAa;AAElC,cAAY;GAAE,MAAM,GAAG,IAAI;GAAQ,MAAM;GAAO;GAAU,CAAC;;;;;AAM7D,8BACE,GAAG,IAAI,OAEP,OAAO,eAAe,QACtB,MACD;AAED,iBAAgB;AAGd,MACE,WACA,OAAO,gBAAgB,cACvB,OAAO,UAAU,YAEjB,aAAY;GAAE,MAAM,GAAG,IAAI;GAAO;GAAU,CAAC;IAE9C,EAAE,CAAC;AAGN,QAAO;EAAC;EAAO;EAAiB;EAAU"}