@intlayer/editor-react 6.1.6 → 7.0.0-canary.0
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.
- package/dist/cjs/CommunicatorContext.cjs +43 -65
- package/dist/cjs/CommunicatorContext.cjs.map +1 -1
- package/dist/cjs/ConfigurationContext.cjs +26 -47
- package/dist/cjs/ConfigurationContext.cjs.map +1 -1
- package/dist/cjs/DictionariesRecordContext.cjs +48 -74
- package/dist/cjs/DictionariesRecordContext.cjs.map +1 -1
- package/dist/cjs/EditedContentContext.cjs +164 -225
- package/dist/cjs/EditedContentContext.cjs.map +1 -1
- package/dist/cjs/EditorEnabledContext.cjs +34 -59
- package/dist/cjs/EditorEnabledContext.cjs.map +1 -1
- package/dist/cjs/EditorProvider.cjs +65 -66
- package/dist/cjs/EditorProvider.cjs.map +1 -1
- package/dist/cjs/FocusDictionaryContext.cjs +51 -75
- package/dist/cjs/FocusDictionaryContext.cjs.map +1 -1
- package/dist/cjs/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/cjs/index.cjs +46 -44
- package/dist/cjs/useCrossFrameMessageListener.cjs +73 -50
- package/dist/cjs/useCrossFrameMessageListener.cjs.map +1 -1
- package/dist/cjs/useCrossFrameState.cjs +106 -73
- package/dist/cjs/useCrossFrameState.cjs.map +1 -1
- package/dist/cjs/useCrossURLPathState.cjs +67 -66
- package/dist/cjs/useCrossURLPathState.cjs.map +1 -1
- package/dist/cjs/useIframeClickInterceptor.cjs +24 -47
- package/dist/cjs/useIframeClickInterceptor.cjs.map +1 -1
- package/dist/esm/CommunicatorContext.mjs +34 -32
- package/dist/esm/CommunicatorContext.mjs.map +1 -1
- package/dist/esm/ConfigurationContext.mjs +19 -23
- package/dist/esm/ConfigurationContext.mjs.map +1 -1
- package/dist/esm/DictionariesRecordContext.mjs +37 -47
- package/dist/esm/DictionariesRecordContext.mjs.map +1 -1
- package/dist/esm/EditedContentContext.mjs +150 -201
- package/dist/esm/EditedContentContext.mjs.map +1 -1
- package/dist/esm/EditorEnabledContext.mjs +22 -32
- package/dist/esm/EditorEnabledContext.mjs.map +1 -1
- package/dist/esm/EditorProvider.mjs +60 -54
- package/dist/esm/EditorProvider.mjs.map +1 -1
- package/dist/esm/FocusDictionaryContext.mjs +41 -48
- package/dist/esm/FocusDictionaryContext.mjs.map +1 -1
- package/dist/esm/index.mjs +15 -13
- package/dist/esm/useCrossFrameMessageListener.mjs +69 -25
- package/dist/esm/useCrossFrameMessageListener.mjs.map +1 -1
- package/dist/esm/useCrossFrameState.mjs +102 -47
- package/dist/esm/useCrossFrameState.mjs.map +1 -1
- package/dist/esm/useCrossURLPathState.mjs +61 -41
- package/dist/esm/useCrossURLPathState.mjs.map +1 -1
- package/dist/esm/useIframeClickInterceptor.mjs +19 -21
- package/dist/esm/useIframeClickInterceptor.mjs.map +1 -1
- package/dist/types/CommunicatorContext.d.ts +12 -8
- package/dist/types/CommunicatorContext.d.ts.map +1 -1
- package/dist/types/ConfigurationContext.d.ts +12 -7
- package/dist/types/ConfigurationContext.d.ts.map +1 -1
- package/dist/types/DictionariesRecordContext.d.ts +15 -12
- package/dist/types/DictionariesRecordContext.d.ts.map +1 -1
- package/dist/types/EditedContentContext.d.ts +33 -30
- package/dist/types/EditedContentContext.d.ts.map +1 -1
- package/dist/types/EditorEnabledContext.d.ts +14 -9
- package/dist/types/EditorEnabledContext.d.ts.map +1 -1
- package/dist/types/EditorProvider.d.ts +10 -6
- package/dist/types/EditorProvider.d.ts.map +1 -1
- package/dist/types/FocusDictionaryContext.d.ts +18 -15
- package/dist/types/FocusDictionaryContext.d.ts.map +1 -1
- package/dist/types/index.d.ts +13 -13
- package/dist/types/useCrossFrameMessageListener.d.ts +7 -2
- package/dist/types/useCrossFrameMessageListener.d.ts.map +1 -1
- package/dist/types/useCrossFrameState.d.ts +10 -6
- package/dist/types/useCrossFrameState.d.ts.map +1 -1
- package/dist/types/useCrossURLPathState.d.ts +8 -3
- package/dist/types/useCrossURLPathState.d.ts.map +1 -1
- package/dist/types/useIframeClickInterceptor.d.ts +5 -2
- package/dist/types/useIframeClickInterceptor.d.ts.map +1 -1
- package/package.json +34 -40
- package/LICENSE +0 -202
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
|
@@ -1,61 +1,67 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}) => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
5
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
|
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"}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
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
|
|
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
|
-
|
|
2
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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 {
|
|
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"}
|