@intlayer/editor-react 8.1.2 → 8.1.3-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.
Files changed (57) hide show
  1. package/dist/cjs/CommunicatorContext.cjs +1 -42
  2. package/dist/cjs/CommunicatorContext.cjs.map +1 -1
  3. package/dist/cjs/ConfigurationContext.cjs +1 -25
  4. package/dist/cjs/ConfigurationContext.cjs.map +1 -1
  5. package/dist/cjs/DictionariesRecordContext.cjs +1 -48
  6. package/dist/cjs/DictionariesRecordContext.cjs.map +1 -1
  7. package/dist/cjs/EditedContentContext.cjs +1 -169
  8. package/dist/cjs/EditedContentContext.cjs.map +1 -1
  9. package/dist/cjs/EditorEnabledContext.cjs +1 -33
  10. package/dist/cjs/EditorEnabledContext.cjs.map +1 -1
  11. package/dist/cjs/EditorProvider.cjs +1 -67
  12. package/dist/cjs/EditorProvider.cjs.map +1 -1
  13. package/dist/cjs/FocusDictionaryContext.cjs +1 -55
  14. package/dist/cjs/FocusDictionaryContext.cjs.map +1 -1
  15. package/dist/cjs/_virtual/_rolldown/runtime.cjs +1 -29
  16. package/dist/cjs/index.cjs +1 -53
  17. package/dist/cjs/useCrossFrameMessageListener.cjs +1 -78
  18. package/dist/cjs/useCrossFrameMessageListener.cjs.map +1 -1
  19. package/dist/cjs/useCrossFrameState.cjs +1 -108
  20. package/dist/cjs/useCrossFrameState.cjs.map +1 -1
  21. package/dist/cjs/useCrossURLPathState.cjs +1 -67
  22. package/dist/cjs/useCrossURLPathState.cjs.map +1 -1
  23. package/dist/cjs/useEditorLocale.cjs +1 -23
  24. package/dist/cjs/useEditorLocale.cjs.map +1 -1
  25. package/dist/cjs/useFocusUnmergedDictionary.cjs +1 -51
  26. package/dist/cjs/useFocusUnmergedDictionary.cjs.map +1 -1
  27. package/dist/cjs/useIframeClickInterceptor.cjs +1 -24
  28. package/dist/cjs/useIframeClickInterceptor.cjs.map +1 -1
  29. package/dist/esm/CommunicatorContext.mjs +1 -38
  30. package/dist/esm/CommunicatorContext.mjs.map +1 -1
  31. package/dist/esm/ConfigurationContext.mjs +1 -21
  32. package/dist/esm/ConfigurationContext.mjs.map +1 -1
  33. package/dist/esm/DictionariesRecordContext.mjs +1 -44
  34. package/dist/esm/DictionariesRecordContext.mjs.map +1 -1
  35. package/dist/esm/EditedContentContext.mjs +1 -163
  36. package/dist/esm/EditedContentContext.mjs.map +1 -1
  37. package/dist/esm/EditorEnabledContext.mjs +1 -27
  38. package/dist/esm/EditorEnabledContext.mjs.map +1 -1
  39. package/dist/esm/EditorProvider.mjs +1 -65
  40. package/dist/esm/EditorProvider.mjs.map +1 -1
  41. package/dist/esm/FocusDictionaryContext.mjs +1 -51
  42. package/dist/esm/FocusDictionaryContext.mjs.map +1 -1
  43. package/dist/esm/index.mjs +1 -17
  44. package/dist/esm/useCrossFrameMessageListener.mjs +1 -76
  45. package/dist/esm/useCrossFrameMessageListener.mjs.map +1 -1
  46. package/dist/esm/useCrossFrameState.mjs +1 -106
  47. package/dist/esm/useCrossFrameState.mjs.map +1 -1
  48. package/dist/esm/useCrossURLPathState.mjs +1 -64
  49. package/dist/esm/useCrossURLPathState.mjs.map +1 -1
  50. package/dist/esm/useEditorLocale.mjs +1 -20
  51. package/dist/esm/useEditorLocale.mjs.map +1 -1
  52. package/dist/esm/useFocusUnmergedDictionary.mjs +1 -49
  53. package/dist/esm/useFocusUnmergedDictionary.mjs.map +1 -1
  54. package/dist/esm/useIframeClickInterceptor.mjs +1 -21
  55. package/dist/esm/useIframeClickInterceptor.mjs.map +1 -1
  56. package/dist/types/EditedContentContext.d.ts.map +1 -1
  57. package/package.json +6 -6
@@ -1,39 +1,2 @@
1
- 'use client';
2
-
3
- import configuration from "@intlayer/config/built";
4
- import { createContext, useContext, useMemo, useRef } from "react";
5
- import { jsx } from "react/jsx-runtime";
6
-
7
- //#region src/CommunicatorContext.tsx
8
- const randomUUID = () => Math.random().toString(36).slice(2);
9
- const { editor } = configuration ?? {};
10
- const CommunicatorContext = createContext({
11
- postMessage: () => null,
12
- allowedOrigins: [
13
- editor?.applicationURL,
14
- editor?.editorURL,
15
- editor?.cmsURL
16
- ],
17
- senderId: ""
18
- });
19
- const CommunicatorProvider = ({ children, allowedOrigins, postMessage }) => {
20
- const senderIdRef = useRef(randomUUID());
21
- const value = useMemo(() => ({
22
- postMessage,
23
- allowedOrigins,
24
- senderId: senderIdRef.current
25
- }), [
26
- postMessage,
27
- allowedOrigins,
28
- senderIdRef.current
29
- ]);
30
- return /* @__PURE__ */ jsx(CommunicatorContext.Provider, {
31
- value,
32
- children
33
- });
34
- };
35
- const useCommunicator = () => useContext(CommunicatorContext);
36
-
37
- //#endregion
38
- export { CommunicatorProvider, useCommunicator };
1
+ "use client";import e from"@intlayer/config/built";import{createContext as t,useContext as n,useMemo as r,useRef as i}from"react";import{jsx as a}from"react/jsx-runtime";const o=()=>Math.random().toString(36).slice(2),{editor:s}=e??{},c=t({postMessage:()=>null,allowedOrigins:[s?.applicationURL,s?.editorURL,s?.cmsURL],senderId:``}),l=({children:e,allowedOrigins:t,postMessage:n})=>{let s=i(o()),l=r(()=>({postMessage:n,allowedOrigins:t,senderId:s.current}),[n,t,s.current]);return a(c.Provider,{value:l,children:e})},u=()=>n(c);export{l as CommunicatorProvider,u as useCommunicator};
39
2
  //# sourceMappingURL=CommunicatorContext.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"CommunicatorContext.mjs","names":[],"sources":["../../src/CommunicatorContext.tsx"],"sourcesContent":["'use client';\n\nimport configuration from '@intlayer/config/built';\nimport {\n createContext,\n type FC,\n type PropsWithChildren,\n useContext,\n useMemo,\n useRef,\n} from 'react';\n\nconst randomUUID = () => Math.random().toString(36).slice(2);\n\nexport type UseCrossPlatformStateProps = {\n postMessage: typeof window.postMessage;\n allowedOrigins?: string[];\n senderId: string;\n};\n\nconst { editor } = configuration ?? {};\n\nconst CommunicatorContext = createContext<UseCrossPlatformStateProps>({\n postMessage: () => null,\n allowedOrigins: [\n editor?.applicationURL,\n editor?.editorURL,\n editor?.cmsURL,\n ] as string[],\n senderId: '',\n});\n\nexport type CommunicatorProviderProps = PropsWithChildren<\n Omit<UseCrossPlatformStateProps, 'senderId'>\n>;\n\nexport const CommunicatorProvider: FC<CommunicatorProviderProps> = ({\n children,\n allowedOrigins,\n postMessage,\n}) => {\n // Create a stable, unique ID for the lifetime of this app/iframe instance.\n const senderIdRef = useRef(randomUUID());\n\n const value = useMemo(\n () => ({ postMessage, allowedOrigins, senderId: senderIdRef.current }),\n [postMessage, allowedOrigins, senderIdRef.current]\n );\n\n return (\n <CommunicatorContext.Provider value={value}>\n {children}\n </CommunicatorContext.Provider>\n );\n};\n\nexport const useCommunicator = () => useContext(CommunicatorContext);\n"],"mappings":";;;;;;;AAYA,MAAM,mBAAmB,KAAK,QAAQ,CAAC,SAAS,GAAG,CAAC,MAAM,EAAE;AAQ5D,MAAM,EAAE,WAAW,iBAAiB,EAAE;AAEtC,MAAM,sBAAsB,cAA0C;CACpE,mBAAmB;CACnB,gBAAgB;EACd,QAAQ;EACR,QAAQ;EACR,QAAQ;EACT;CACD,UAAU;CACX,CAAC;AAMF,MAAa,wBAAuD,EAClE,UACA,gBACA,kBACI;CAEJ,MAAM,cAAc,OAAO,YAAY,CAAC;CAExC,MAAM,QAAQ,eACL;EAAE;EAAa;EAAgB,UAAU,YAAY;EAAS,GACrE;EAAC;EAAa;EAAgB,YAAY;EAAQ,CACnD;AAED,QACE,oBAAC,oBAAoB;EAAgB;EAClC;GAC4B;;AAInC,MAAa,wBAAwB,WAAW,oBAAoB"}
1
+ {"version":3,"file":"CommunicatorContext.mjs","names":[],"sources":["../../src/CommunicatorContext.tsx"],"sourcesContent":["'use client';\n\nimport configuration from '@intlayer/config/built';\nimport {\n createContext,\n type FC,\n type PropsWithChildren,\n useContext,\n useMemo,\n useRef,\n} from 'react';\n\nconst randomUUID = () => Math.random().toString(36).slice(2);\n\nexport type UseCrossPlatformStateProps = {\n postMessage: typeof window.postMessage;\n allowedOrigins?: string[];\n senderId: string;\n};\n\nconst { editor } = configuration ?? {};\n\nconst CommunicatorContext = createContext<UseCrossPlatformStateProps>({\n postMessage: () => null,\n allowedOrigins: [\n editor?.applicationURL,\n editor?.editorURL,\n editor?.cmsURL,\n ] as string[],\n senderId: '',\n});\n\nexport type CommunicatorProviderProps = PropsWithChildren<\n Omit<UseCrossPlatformStateProps, 'senderId'>\n>;\n\nexport const CommunicatorProvider: FC<CommunicatorProviderProps> = ({\n children,\n allowedOrigins,\n postMessage,\n}) => {\n // Create a stable, unique ID for the lifetime of this app/iframe instance.\n const senderIdRef = useRef(randomUUID());\n\n const value = useMemo(\n () => ({ postMessage, allowedOrigins, senderId: senderIdRef.current }),\n [postMessage, allowedOrigins, senderIdRef.current]\n );\n\n return (\n <CommunicatorContext.Provider value={value}>\n {children}\n </CommunicatorContext.Provider>\n );\n};\n\nexport const useCommunicator = () => useContext(CommunicatorContext);\n"],"mappings":"0KAYA,MAAM,MAAmB,KAAK,QAAQ,CAAC,SAAS,GAAG,CAAC,MAAM,EAAE,CAQtD,CAAE,UAAW,GAAiB,EAAE,CAEhC,EAAsB,EAA0C,CACpE,gBAAmB,KACnB,eAAgB,CACd,GAAQ,eACR,GAAQ,UACR,GAAQ,OACT,CACD,SAAU,GACX,CAAC,CAMW,GAAuD,CAClE,WACA,iBACA,iBACI,CAEJ,IAAM,EAAc,EAAO,GAAY,CAAC,CAElC,EAAQ,OACL,CAAE,cAAa,iBAAgB,SAAU,EAAY,QAAS,EACrE,CAAC,EAAa,EAAgB,EAAY,QAAQ,CACnD,CAED,OACE,EAAC,EAAoB,SAAA,CAAgB,QAClC,YAC4B,EAItB,MAAwB,EAAW,EAAoB"}
@@ -1,22 +1,2 @@
1
- 'use client';
2
-
3
- import { useCrossFrameState } from "./useCrossFrameState.mjs";
4
- import { createContext, useContext } from "react";
5
- import { jsx } from "react/jsx-runtime";
6
- import { MessageKey } from "@intlayer/editor";
7
-
8
- //#region src/ConfigurationContext.tsx
9
- const ConfigurationStatesContext = createContext(void 0);
10
- const useConfigurationState = () => useCrossFrameState(MessageKey.INTLAYER_CONFIGURATION, void 0, {
11
- receive: false,
12
- emit: true
13
- });
14
- const ConfigurationProvider = ({ children, configuration }) => /* @__PURE__ */ jsx(ConfigurationStatesContext.Provider, {
15
- value: configuration,
16
- children
17
- });
18
- const useConfiguration = () => useContext(ConfigurationStatesContext);
19
-
20
- //#endregion
21
- export { ConfigurationProvider, useConfiguration, useConfigurationState };
1
+ "use client";import{useCrossFrameState as e}from"./useCrossFrameState.mjs";import{createContext as t,useContext as n}from"react";import{jsx as r}from"react/jsx-runtime";import{MessageKey as i}from"@intlayer/editor";const a=t(void 0),o=()=>e(i.INTLAYER_CONFIGURATION,void 0,{receive:!1,emit:!0}),s=({children:e,configuration:t})=>r(a.Provider,{value:t,children:e}),c=()=>n(a);export{s as ConfigurationProvider,c as useConfiguration,o as useConfigurationState};
22
2
  //# sourceMappingURL=ConfigurationContext.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"ConfigurationContext.mjs","names":[],"sources":["../../src/ConfigurationContext.tsx"],"sourcesContent":["'use client';\n\nimport { MessageKey } from '@intlayer/editor';\nimport type { IntlayerConfig } from '@intlayer/types';\nimport {\n createContext,\n type FC,\n type PropsWithChildren,\n useContext,\n} from 'react';\nimport { useCrossFrameState } from './useCrossFrameState';\n\nconst ConfigurationStatesContext = createContext<IntlayerConfig | undefined>(\n undefined\n);\n\nexport const useConfigurationState = () =>\n useCrossFrameState<IntlayerConfig>(\n MessageKey.INTLAYER_CONFIGURATION,\n undefined,\n {\n receive: false,\n emit: true,\n }\n );\n\nexport type ConfigurationProviderProps = {\n configuration?: IntlayerConfig;\n};\n\nexport const ConfigurationProvider: FC<\n PropsWithChildren<ConfigurationProviderProps>\n> = ({ children, configuration }) => (\n <ConfigurationStatesContext.Provider value={configuration}>\n {children}\n </ConfigurationStatesContext.Provider>\n);\n\nexport const useConfiguration = () => useContext(ConfigurationStatesContext);\n"],"mappings":";;;;;;;;AAYA,MAAM,6BAA6B,cACjC,OACD;AAED,MAAa,8BACX,mBACE,WAAW,wBACX,QACA;CACE,SAAS;CACT,MAAM;CACP,CACF;AAMH,MAAa,yBAER,EAAE,UAAU,oBACf,oBAAC,2BAA2B;CAAS,OAAO;CACzC;EACmC;AAGxC,MAAa,yBAAyB,WAAW,2BAA2B"}
1
+ {"version":3,"file":"ConfigurationContext.mjs","names":[],"sources":["../../src/ConfigurationContext.tsx"],"sourcesContent":["'use client';\n\nimport { MessageKey } from '@intlayer/editor';\nimport type { IntlayerConfig } from '@intlayer/types';\nimport {\n createContext,\n type FC,\n type PropsWithChildren,\n useContext,\n} from 'react';\nimport { useCrossFrameState } from './useCrossFrameState';\n\nconst ConfigurationStatesContext = createContext<IntlayerConfig | undefined>(\n undefined\n);\n\nexport const useConfigurationState = () =>\n useCrossFrameState<IntlayerConfig>(\n MessageKey.INTLAYER_CONFIGURATION,\n undefined,\n {\n receive: false,\n emit: true,\n }\n );\n\nexport type ConfigurationProviderProps = {\n configuration?: IntlayerConfig;\n};\n\nexport const ConfigurationProvider: FC<\n PropsWithChildren<ConfigurationProviderProps>\n> = ({ children, configuration }) => (\n <ConfigurationStatesContext.Provider value={configuration}>\n {children}\n </ConfigurationStatesContext.Provider>\n);\n\nexport const useConfiguration = () => useContext(ConfigurationStatesContext);\n"],"mappings":"uNAYA,MAAM,EAA6B,EACjC,IAAA,GACD,CAEY,MACX,EACE,EAAW,uBACX,IAAA,GACA,CACE,QAAS,GACT,KAAM,GACP,CACF,CAMU,GAER,CAAE,WAAU,mBACf,EAAC,EAA2B,SAAA,CAAS,MAAO,EACzC,YACmC,CAG3B,MAAyB,EAAW,EAA2B"}
@@ -1,45 +1,2 @@
1
- 'use client';
2
-
3
- import { useCrossFrameState } from "./useCrossFrameState.mjs";
4
- import { createContext, useContext, useMemo } from "react";
5
- import { jsx } from "react/jsx-runtime";
6
- import { MessageKey } from "@intlayer/editor";
7
-
8
- //#region src/DictionariesRecordContext.tsx
9
- const DictionariesRecordStatesContext = createContext(void 0);
10
- const DictionariesRecordActionsContext = createContext(void 0);
11
- const DictionariesRecordProvider = ({ children }) => {
12
- const [localeDictionaries, setLocaleDictionaries] = useCrossFrameState(MessageKey.INTLAYER_LOCALE_DICTIONARIES_CHANGED, void 0);
13
- const stateValue = useMemo(() => ({ localeDictionaries: localeDictionaries ?? {} }), [localeDictionaries]);
14
- const actionValue = useMemo(() => ({
15
- setLocaleDictionaries,
16
- setLocaleDictionary: (dictionary) => {
17
- if (!dictionary.localId) return;
18
- setLocaleDictionaries((dictionaries) => ({
19
- ...dictionaries,
20
- [dictionary.localId]: dictionary
21
- }));
22
- }
23
- }), [setLocaleDictionaries]);
24
- return /* @__PURE__ */ jsx(DictionariesRecordStatesContext.Provider, {
25
- value: stateValue,
26
- children: /* @__PURE__ */ jsx(DictionariesRecordActionsContext.Provider, {
27
- value: actionValue,
28
- children
29
- })
30
- });
31
- };
32
- const useDictionariesRecordActions = () => useContext(DictionariesRecordActionsContext);
33
- const useDictionariesRecord = () => {
34
- const actionsContext = useDictionariesRecordActions();
35
- const statesContext = useContext(DictionariesRecordStatesContext);
36
- if (!statesContext) throw new Error("useDictionariesRecordStates must be used within a DictionariesRecordProvider");
37
- return {
38
- ...statesContext,
39
- ...actionsContext
40
- };
41
- };
42
-
43
- //#endregion
44
- export { DictionariesRecordProvider, useDictionariesRecord, useDictionariesRecordActions };
1
+ "use client";import{useCrossFrameState as e}from"./useCrossFrameState.mjs";import{createContext as t,useContext as n,useMemo as r}from"react";import{jsx as i}from"react/jsx-runtime";import{MessageKey as a}from"@intlayer/editor";const o=t(void 0),s=t(void 0),c=({children:t})=>{let[n,c]=e(a.INTLAYER_LOCALE_DICTIONARIES_CHANGED,void 0),l=r(()=>({localeDictionaries:n??{}}),[n]),u=r(()=>({setLocaleDictionaries:c,setLocaleDictionary:e=>{e.localId&&c(t=>({...t,[e.localId]:e}))}}),[c]);return i(o.Provider,{value:l,children:i(s.Provider,{value:u,children:t})})},l=()=>n(s),u=()=>{let e=l(),t=n(o);if(!t)throw Error(`useDictionariesRecordStates must be used within a DictionariesRecordProvider`);return{...t,...e}};export{c as DictionariesRecordProvider,u as useDictionariesRecord,l as useDictionariesRecordActions};
45
2
  //# sourceMappingURL=DictionariesRecordContext.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"DictionariesRecordContext.mjs","names":[],"sources":["../../src/DictionariesRecordContext.tsx"],"sourcesContent":["'use client';\n\nimport { MessageKey } from '@intlayer/editor';\nimport type { Dictionary, LocalDictionaryId } from '@intlayer/types';\nimport {\n createContext,\n type Dispatch,\n type FC,\n type PropsWithChildren,\n type SetStateAction,\n useContext,\n useMemo,\n} from 'react';\nimport { useCrossFrameState } from './useCrossFrameState';\n\nexport type DictionaryContent = Record<LocalDictionaryId, Dictionary>;\n\ntype DictionariesRecordStatesContextType = {\n localeDictionaries: DictionaryContent;\n};\ntype DictionariesRecordActionsContextType = {\n setLocaleDictionaries: Dispatch<SetStateAction<DictionaryContent>>;\n setLocaleDictionary: (dictionary: Dictionary) => void;\n};\n\nconst DictionariesRecordStatesContext = createContext<\n DictionariesRecordStatesContextType | undefined\n>(undefined);\nconst DictionariesRecordActionsContext = createContext<\n DictionariesRecordActionsContextType | undefined\n>(undefined);\n\nexport const DictionariesRecordProvider: FC<PropsWithChildren> = ({\n children,\n}) => {\n const [localeDictionaries, setLocaleDictionaries] =\n useCrossFrameState<DictionaryContent>(\n MessageKey.INTLAYER_LOCALE_DICTIONARIES_CHANGED,\n undefined\n );\n\n const stateValue = useMemo(\n () => ({\n localeDictionaries: localeDictionaries ?? {},\n }),\n [localeDictionaries]\n );\n\n const actionValue = useMemo(\n () => ({\n setLocaleDictionaries,\n setLocaleDictionary: (dictionary: Dictionary) => {\n if (!dictionary.localId) return;\n\n setLocaleDictionaries((dictionaries) => ({\n ...dictionaries,\n [dictionary.localId as LocalDictionaryId]: dictionary,\n }));\n },\n }),\n [setLocaleDictionaries]\n );\n\n return (\n <DictionariesRecordStatesContext.Provider value={stateValue}>\n <DictionariesRecordActionsContext.Provider value={actionValue}>\n {children}\n </DictionariesRecordActionsContext.Provider>\n </DictionariesRecordStatesContext.Provider>\n );\n};\n\nexport const useDictionariesRecordActions = () =>\n useContext(DictionariesRecordActionsContext);\n\nexport const useDictionariesRecord = () => {\n const actionsContext = useDictionariesRecordActions();\n const statesContext = useContext(DictionariesRecordStatesContext);\n\n if (!statesContext) {\n throw new Error(\n 'useDictionariesRecordStates must be used within a DictionariesRecordProvider'\n );\n }\n\n return { ...statesContext, ...actionsContext };\n};\n"],"mappings":";;;;;;;;AAyBA,MAAM,kCAAkC,cAEtC,OAAU;AACZ,MAAM,mCAAmC,cAEvC,OAAU;AAEZ,MAAa,8BAAqD,EAChE,eACI;CACJ,MAAM,CAAC,oBAAoB,yBACzB,mBACE,WAAW,sCACX,OACD;CAEH,MAAM,aAAa,eACV,EACL,oBAAoB,sBAAsB,EAAE,EAC7C,GACD,CAAC,mBAAmB,CACrB;CAED,MAAM,cAAc,eACX;EACL;EACA,sBAAsB,eAA2B;AAC/C,OAAI,CAAC,WAAW,QAAS;AAEzB,0BAAuB,kBAAkB;IACvC,GAAG;KACF,WAAW,UAA+B;IAC5C,EAAE;;EAEN,GACD,CAAC,sBAAsB,CACxB;AAED,QACE,oBAAC,gCAAgC;EAAS,OAAO;YAC/C,oBAAC,iCAAiC;GAAS,OAAO;GAC/C;IACyC;GACH;;AAI/C,MAAa,qCACX,WAAW,iCAAiC;AAE9C,MAAa,8BAA8B;CACzC,MAAM,iBAAiB,8BAA8B;CACrD,MAAM,gBAAgB,WAAW,gCAAgC;AAEjE,KAAI,CAAC,cACH,OAAM,IAAI,MACR,+EACD;AAGH,QAAO;EAAE,GAAG;EAAe,GAAG;EAAgB"}
1
+ {"version":3,"file":"DictionariesRecordContext.mjs","names":[],"sources":["../../src/DictionariesRecordContext.tsx"],"sourcesContent":["'use client';\n\nimport { MessageKey } from '@intlayer/editor';\nimport type { Dictionary, LocalDictionaryId } from '@intlayer/types';\nimport {\n createContext,\n type Dispatch,\n type FC,\n type PropsWithChildren,\n type SetStateAction,\n useContext,\n useMemo,\n} from 'react';\nimport { useCrossFrameState } from './useCrossFrameState';\n\nexport type DictionaryContent = Record<LocalDictionaryId, Dictionary>;\n\ntype DictionariesRecordStatesContextType = {\n localeDictionaries: DictionaryContent;\n};\ntype DictionariesRecordActionsContextType = {\n setLocaleDictionaries: Dispatch<SetStateAction<DictionaryContent>>;\n setLocaleDictionary: (dictionary: Dictionary) => void;\n};\n\nconst DictionariesRecordStatesContext = createContext<\n DictionariesRecordStatesContextType | undefined\n>(undefined);\nconst DictionariesRecordActionsContext = createContext<\n DictionariesRecordActionsContextType | undefined\n>(undefined);\n\nexport const DictionariesRecordProvider: FC<PropsWithChildren> = ({\n children,\n}) => {\n const [localeDictionaries, setLocaleDictionaries] =\n useCrossFrameState<DictionaryContent>(\n MessageKey.INTLAYER_LOCALE_DICTIONARIES_CHANGED,\n undefined\n );\n\n const stateValue = useMemo(\n () => ({\n localeDictionaries: localeDictionaries ?? {},\n }),\n [localeDictionaries]\n );\n\n const actionValue = useMemo(\n () => ({\n setLocaleDictionaries,\n setLocaleDictionary: (dictionary: Dictionary) => {\n if (!dictionary.localId) return;\n\n setLocaleDictionaries((dictionaries) => ({\n ...dictionaries,\n [dictionary.localId as LocalDictionaryId]: dictionary,\n }));\n },\n }),\n [setLocaleDictionaries]\n );\n\n return (\n <DictionariesRecordStatesContext.Provider value={stateValue}>\n <DictionariesRecordActionsContext.Provider value={actionValue}>\n {children}\n </DictionariesRecordActionsContext.Provider>\n </DictionariesRecordStatesContext.Provider>\n );\n};\n\nexport const useDictionariesRecordActions = () =>\n useContext(DictionariesRecordActionsContext);\n\nexport const useDictionariesRecord = () => {\n const actionsContext = useDictionariesRecordActions();\n const statesContext = useContext(DictionariesRecordStatesContext);\n\n if (!statesContext) {\n throw new Error(\n 'useDictionariesRecordStates must be used within a DictionariesRecordProvider'\n );\n }\n\n return { ...statesContext, ...actionsContext };\n};\n"],"mappings":"oOAyBA,MAAM,EAAkC,EAEtC,IAAA,GAAU,CACN,EAAmC,EAEvC,IAAA,GAAU,CAEC,GAAqD,CAChE,cACI,CACJ,GAAM,CAAC,EAAoB,GACzB,EACE,EAAW,qCACX,IAAA,GACD,CAEG,EAAa,OACV,CACL,mBAAoB,GAAsB,EAAE,CAC7C,EACD,CAAC,EAAmB,CACrB,CAEK,EAAc,OACX,CACL,wBACA,oBAAsB,GAA2B,CAC1C,EAAW,SAEhB,EAAuB,IAAkB,CACvC,GAAG,GACF,EAAW,SAA+B,EAC5C,EAAE,EAEN,EACD,CAAC,EAAsB,CACxB,CAED,OACE,EAAC,EAAgC,SAAA,CAAS,MAAO,WAC/C,EAAC,EAAiC,SAAA,CAAS,MAAO,EAC/C,YACyC,EACH,EAIlC,MACX,EAAW,EAAiC,CAEjC,MAA8B,CACzC,IAAM,EAAiB,GAA8B,CAC/C,EAAgB,EAAW,EAAgC,CAEjE,GAAI,CAAC,EACH,MAAU,MACR,+EACD,CAGH,MAAO,CAAE,GAAG,EAAe,GAAG,EAAgB"}
@@ -1,164 +1,2 @@
1
- 'use client';
2
-
3
- import { useCrossFrameMessageListener } from "./useCrossFrameMessageListener.mjs";
4
- import { useCrossFrameState } from "./useCrossFrameState.mjs";
5
- import { useDictionariesRecord } from "./DictionariesRecordContext.mjs";
6
- import { useEditorLocale } from "./useEditorLocale.mjs";
7
- import { createContext, useContext } from "react";
8
- import { jsx } from "react/jsx-runtime";
9
- import { MessageKey } from "@intlayer/editor";
10
- import { editDictionaryByKeyPath, getContentNodeByKeyPath, renameContentNodeByKeyPath } from "@intlayer/core";
11
- import { NodeType } from "@intlayer/types";
12
-
13
- //#region src/EditedContentContext.tsx
14
- const EditedContentStateContext = createContext(void 0);
15
- const usePostEditedContentState = (onEventTriggered) => useCrossFrameMessageListener(`${MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/post`, onEventTriggered);
16
- const useGetEditedContentState = (onEventTriggered) => useCrossFrameMessageListener(`${MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/get`, onEventTriggered);
17
- const EditedContentActionsContext = createContext(void 0);
18
- const resolveState = (state, prevState) => typeof state === "function" ? state(prevState) : state;
19
- const EditedContentProvider = ({ children }) => {
20
- const { localeDictionaries } = useDictionariesRecord();
21
- const currentLocale = useEditorLocale();
22
- const [editedContent, setEditedContentState] = useCrossFrameState(MessageKey.INTLAYER_EDITED_CONTENT_CHANGED);
23
- const setEditedDictionary = (newValue) => {
24
- let updatedDictionaries = resolveState(newValue);
25
- setEditedContentState((prev) => {
26
- if (!updatedDictionaries.localId) {
27
- console.error("no localId", updatedDictionaries);
28
- return prev;
29
- }
30
- updatedDictionaries = resolveState(newValue, prev?.[updatedDictionaries.localId]);
31
- return {
32
- ...prev,
33
- [updatedDictionaries.localId]: updatedDictionaries
34
- };
35
- });
36
- return updatedDictionaries;
37
- };
38
- const setEditedContent = (localDictionaryId, newValue) => {
39
- setEditedContentState((prev) => ({
40
- ...prev,
41
- [localDictionaryId]: {
42
- ...prev?.[localDictionaryId],
43
- content: newValue
44
- }
45
- }));
46
- };
47
- const addEditedContent = (localDictionaryId, newValue, keyPath = [], overwrite = true) => {
48
- setEditedContentState((prev) => {
49
- const originalContent = localeDictionaries[localDictionaryId]?.content;
50
- const currentContent = structuredClone(prev?.[localDictionaryId]?.content ?? originalContent);
51
- let newKeyPath = keyPath;
52
- if (!overwrite) {
53
- let index = 0;
54
- const otherKeyPath = keyPath.slice(0, -1);
55
- const lastKeyPath = keyPath[keyPath.length - 1];
56
- let finalKey = lastKeyPath.key;
57
- while (typeof getContentNodeByKeyPath(currentContent, newKeyPath) !== "undefined") {
58
- index++;
59
- finalKey = index === 0 ? lastKeyPath.key : `${lastKeyPath.key} (${index})`;
60
- newKeyPath = [...otherKeyPath, {
61
- ...lastKeyPath,
62
- key: finalKey
63
- }];
64
- }
65
- }
66
- const updatedContent = editDictionaryByKeyPath(currentContent, newKeyPath, newValue);
67
- return {
68
- ...prev,
69
- [localDictionaryId]: {
70
- ...prev?.[localDictionaryId],
71
- content: updatedContent
72
- }
73
- };
74
- });
75
- };
76
- const renameEditedContent = (localDictionaryId, newKey, keyPath = []) => {
77
- setEditedContentState((prev) => {
78
- const originalContent = localeDictionaries[localDictionaryId]?.content;
79
- const contentWithNewField = renameContentNodeByKeyPath(structuredClone(prev?.[localDictionaryId]?.content ?? originalContent), newKey, keyPath);
80
- return {
81
- ...prev,
82
- [localDictionaryId]: {
83
- ...prev?.[localDictionaryId],
84
- content: contentWithNewField
85
- }
86
- };
87
- });
88
- };
89
- const removeEditedContent = (localDictionaryId, keyPath) => {
90
- setEditedContentState((prev) => {
91
- const originalContent = localeDictionaries[localDictionaryId]?.content;
92
- const restoredContent = editDictionaryByKeyPath(structuredClone(prev?.[localDictionaryId]?.content ?? originalContent), keyPath, getContentNodeByKeyPath(originalContent, keyPath));
93
- return {
94
- ...prev,
95
- [localDictionaryId]: {
96
- ...prev?.[localDictionaryId],
97
- content: restoredContent
98
- }
99
- };
100
- });
101
- };
102
- const restoreEditedContent = (localDictionaryId) => {
103
- setEditedContentState((prev) => {
104
- const updated = { ...prev };
105
- delete updated[localDictionaryId];
106
- return updated;
107
- });
108
- };
109
- const clearEditedDictionaryContent = (localDictionaryId) => {
110
- setEditedContentState((prev) => {
111
- return Object.entries(prev).reduce((acc, [key, value]) => {
112
- if (key === localDictionaryId) return acc;
113
- return {
114
- ...acc,
115
- [key]: value
116
- };
117
- }, {});
118
- });
119
- };
120
- const clearEditedContent = () => {
121
- setEditedContentState({});
122
- };
123
- const getEditedContentValue = (localDictionaryIdOrKey, keyPath) => {
124
- if (!editedContent) return void 0;
125
- const filteredKeyPath = keyPath.filter((key) => key.type !== NodeType.Translation);
126
- if (localDictionaryIdOrKey.includes(":local:") || localDictionaryIdOrKey.includes(":remote:")) return getContentNodeByKeyPath(editedContent?.[localDictionaryIdOrKey]?.content ?? {}, filteredKeyPath, currentLocale);
127
- const filteredDictionariesLocalId = Object.keys(editedContent).filter((key) => key.startsWith(`${localDictionaryIdOrKey}:`));
128
- for (const localDictionaryId of filteredDictionariesLocalId) {
129
- const contentNode = getContentNodeByKeyPath(editedContent?.[localDictionaryId]?.content ?? {}, filteredKeyPath, currentLocale);
130
- if (contentNode) return contentNode;
131
- }
132
- };
133
- return /* @__PURE__ */ jsx(EditedContentStateContext.Provider, {
134
- value: { editedContent },
135
- children: /* @__PURE__ */ jsx(EditedContentActionsContext.Provider, {
136
- value: {
137
- setEditedContentState,
138
- setEditedDictionary,
139
- setEditedContent,
140
- addEditedContent,
141
- renameEditedContent,
142
- removeEditedContent,
143
- restoreEditedContent,
144
- clearEditedDictionaryContent,
145
- clearEditedContent,
146
- getEditedContentValue
147
- },
148
- children
149
- })
150
- });
151
- };
152
- const useEditedContentActions = () => useContext(EditedContentActionsContext);
153
- const useEditedContent = () => {
154
- const stateContext = useContext(EditedContentStateContext);
155
- const actionContext = useEditedContentActions();
156
- return {
157
- ...stateContext,
158
- ...actionContext
159
- };
160
- };
161
-
162
- //#endregion
163
- export { EditedContentProvider, useEditedContent, useEditedContentActions, useGetEditedContentState, usePostEditedContentState };
1
+ "use client";import{useCrossFrameMessageListener as e}from"./useCrossFrameMessageListener.mjs";import{useCrossFrameState as t}from"./useCrossFrameState.mjs";import{useDictionariesRecord as n}from"./DictionariesRecordContext.mjs";import{useEditorLocale as r}from"./useEditorLocale.mjs";import{createContext as i,useContext as a}from"react";import{jsx as o}from"react/jsx-runtime";import{MessageKey as s}from"@intlayer/editor";import{editDictionaryByKeyPath as c,getContentNodeByKeyPath as l,renameContentNodeByKeyPath as u}from"@intlayer/core/dictionaryManipulator";import{NodeType as d}from"@intlayer/types";const f=i(void 0),p=t=>e(`${s.INTLAYER_EDITED_CONTENT_CHANGED}/post`,t),m=t=>e(`${s.INTLAYER_EDITED_CONTENT_CHANGED}/get`,t),h=i(void 0),g=(e,t)=>typeof e==`function`?e(t):e,_=({children:e})=>{let{localeDictionaries:i}=n(),a=r(),[p,m]=t(s.INTLAYER_EDITED_CONTENT_CHANGED);return o(f.Provider,{value:{editedContent:p},children:o(h.Provider,{value:{setEditedContentState:m,setEditedDictionary:e=>{let t=g(e);return m(n=>t.localId?(t=g(e,n?.[t.localId]),{...n,[t.localId]:t}):(console.error(`no localId`,t),n)),t},setEditedContent:(e,t)=>{m(n=>({...n,[e]:{...n?.[e],content:t}}))},addEditedContent:(e,t,n=[],r=!0)=>{m(a=>{let o=i[e]?.content,s=structuredClone(a?.[e]?.content??o),u=n;if(!r){let e=0,t=n.slice(0,-1),r=n[n.length-1],i=r.key;for(;l(s,u)!==void 0;)e++,i=e===0?r.key:`${r.key} (${e})`,u=[...t,{...r,key:i}]}let d=c(s,u,t);return{...a,[e]:{...a?.[e],content:d}}})},renameEditedContent:(e,t,n=[])=>{m(r=>{let a=i[e]?.content,o=u(structuredClone(r?.[e]?.content??a),t,n);return{...r,[e]:{...r?.[e],content:o}}})},removeEditedContent:(e,t)=>{m(n=>{let r=i[e]?.content,a=c(structuredClone(n?.[e]?.content??r),t,l(r,t));return{...n,[e]:{...n?.[e],content:a}}})},restoreEditedContent:e=>{m(t=>{let n={...t};return delete n[e],n})},clearEditedDictionaryContent:e=>{m(t=>{let n={...t};return delete n[e],n})},clearEditedContent:()=>{m({})},getEditedContentValue:(e,t)=>{if(!p)return;let n=t.filter(e=>e.type!==d.Translation);if(e.includes(`:local:`)||e.includes(`:remote:`))return l(p?.[e]?.content??{},n,a);let r=Object.keys(p).filter(t=>t.startsWith(`${e}:`));for(let e of r){let t=l(p?.[e]?.content??{},n,a);if(t)return t}}},children:e})})},v=()=>a(h),y=()=>{let e=a(f),t=v();return{...e,...t}};export{_ as EditedContentProvider,y as useEditedContent,v as useEditedContentActions,m as useGetEditedContentState,p as usePostEditedContentState};
164
2
  //# sourceMappingURL=EditedContentContext.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"EditedContentContext.mjs","names":[],"sources":["../../src/EditedContentContext.tsx"],"sourcesContent":["'use client';\n\nimport {\n editDictionaryByKeyPath,\n getContentNodeByKeyPath,\n renameContentNodeByKeyPath,\n} from '@intlayer/core';\nimport { MessageKey } from '@intlayer/editor';\nimport {\n type ContentNode,\n type Dictionary,\n type KeyPath,\n type LocalDictionaryId,\n NodeType,\n} from '@intlayer/types';\nimport {\n createContext,\n type Dispatch,\n type FC,\n type PropsWithChildren,\n type SetStateAction,\n useContext,\n} from 'react';\nimport {\n type DictionaryContent,\n useDictionariesRecord,\n} from './DictionariesRecordContext';\nimport { useCrossFrameMessageListener } from './useCrossFrameMessageListener';\nimport { useCrossFrameState } from './useCrossFrameState';\nimport { useEditorLocale } from './useEditorLocale';\n\ntype EditedContentStateContextType = {\n editedContent: Record<LocalDictionaryId, Dictionary> | undefined;\n};\n\nconst EditedContentStateContext = createContext<\n EditedContentStateContextType | undefined\n>(undefined);\n\nexport const usePostEditedContentState = <S,>(\n onEventTriggered?: (data: S) => void\n) =>\n useCrossFrameMessageListener(\n `${MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/post`,\n onEventTriggered\n );\n\nexport const useGetEditedContentState = <S,>(\n onEventTriggered?: (data: S) => void\n) =>\n useCrossFrameMessageListener(\n `${MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/get`,\n onEventTriggered\n );\n\ntype EditedContentActionsContextType = {\n setEditedContentState: (editedContent: DictionaryContent) => void;\n setEditedDictionary: Dispatch<SetStateAction<Dictionary>>;\n setEditedContent: (\n localDictionaryId: LocalDictionaryId,\n newValue: Dictionary['content']\n ) => void;\n addEditedContent: (\n localDictionaryId: LocalDictionaryId,\n newValue: ContentNode<any>,\n keyPath?: KeyPath[],\n overwrite?: boolean\n ) => void;\n renameEditedContent: (\n localDictionaryId: LocalDictionaryId,\n newKey: KeyPath['key'],\n keyPath?: KeyPath[]\n ) => void;\n removeEditedContent: (\n localDictionaryId: LocalDictionaryId,\n keyPath: KeyPath[]\n ) => void;\n restoreEditedContent: (localDictionaryId: LocalDictionaryId) => void;\n clearEditedDictionaryContent: (localDictionaryId: LocalDictionaryId) => void;\n clearEditedContent: () => void;\n getEditedContentValue: (\n localDictionaryIdOrKey: LocalDictionaryId | Dictionary['key'] | string,\n keyPath: KeyPath[]\n ) => ContentNode | undefined;\n};\n\nconst EditedContentActionsContext = createContext<\n EditedContentActionsContextType | undefined\n>(undefined);\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\nexport const EditedContentProvider: FC<PropsWithChildren> = ({ children }) => {\n const { localeDictionaries } = useDictionariesRecord();\n const currentLocale = useEditorLocale();\n\n const [editedContent, setEditedContentState] =\n useCrossFrameState<DictionaryContent>(\n MessageKey.INTLAYER_EDITED_CONTENT_CHANGED\n );\n\n const setEditedDictionary: Dispatch<SetStateAction<Dictionary>> = (\n newValue\n ) => {\n let updatedDictionaries: Dictionary = resolveState(newValue);\n\n setEditedContentState((prev) => {\n if (!updatedDictionaries.localId) {\n console.error('no localId', updatedDictionaries);\n\n return prev;\n }\n\n updatedDictionaries = resolveState(\n newValue,\n prev?.[updatedDictionaries.localId]\n );\n\n return {\n ...prev,\n [updatedDictionaries.localId as LocalDictionaryId]: updatedDictionaries,\n };\n });\n\n return updatedDictionaries;\n };\n\n const setEditedContent = (\n localDictionaryId: LocalDictionaryId,\n newValue: Dictionary['content']\n ) => {\n setEditedContentState((prev) => ({\n ...prev,\n [localDictionaryId]: {\n ...prev?.[localDictionaryId],\n content: newValue,\n },\n }));\n };\n\n const addEditedContent = (\n localDictionaryId: LocalDictionaryId,\n newValue: ContentNode,\n keyPath: KeyPath[] = [],\n overwrite: boolean = true\n ) => {\n setEditedContentState((prev) => {\n // Get the starting content: edited version if available, otherwise a deep copy of the original\n const originalContent = localeDictionaries[localDictionaryId]?.content;\n const currentContent = structuredClone(\n prev?.[localDictionaryId]?.content ?? originalContent\n );\n\n let newKeyPath = keyPath;\n if (!overwrite) {\n // Find a unique key based on the keyPath provided\n let index = 0;\n\n const otherKeyPath = keyPath.slice(0, -1);\n const lastKeyPath: KeyPath = keyPath[keyPath.length - 1];\n\n let finalKey = lastKeyPath.key;\n\n // Loop until we find a key that does not exist\n while (\n typeof getContentNodeByKeyPath(currentContent, newKeyPath) !==\n 'undefined'\n ) {\n index++;\n finalKey =\n index === 0 ? lastKeyPath.key : `${lastKeyPath.key} (${index})`;\n newKeyPath = [\n ...otherKeyPath,\n { ...lastKeyPath, key: finalKey } as KeyPath,\n ];\n }\n }\n\n const updatedContent = editDictionaryByKeyPath(\n currentContent,\n newKeyPath,\n newValue\n );\n\n return {\n ...prev,\n [localDictionaryId]: {\n ...prev?.[localDictionaryId],\n content: updatedContent as Dictionary['content'],\n },\n };\n });\n };\n\n const renameEditedContent = (\n localDictionaryId: LocalDictionaryId,\n newKey: KeyPath['key'],\n keyPath: KeyPath[] = []\n ) => {\n setEditedContentState((prev) => {\n // Retrieve the base content: use edited version if available, otherwise deep copy of original\n const originalContent = localeDictionaries[localDictionaryId]?.content;\n const currentContent = structuredClone(\n prev?.[localDictionaryId]?.content ?? originalContent\n );\n\n const contentWithNewField = renameContentNodeByKeyPath(\n currentContent,\n newKey,\n keyPath\n );\n\n return {\n ...prev,\n [localDictionaryId]: {\n ...prev?.[localDictionaryId],\n content: contentWithNewField as Dictionary['content'],\n },\n };\n });\n };\n\n const removeEditedContent = (\n localDictionaryId: LocalDictionaryId,\n keyPath: KeyPath[]\n ) => {\n setEditedContentState((prev) => {\n // Retrieve the original content as reference\n const originalContent = localeDictionaries[localDictionaryId]?.content;\n const currentContent = structuredClone(\n prev?.[localDictionaryId]?.content ?? originalContent\n );\n\n // Get the initial value from the original dictionary content\n const initialContent = getContentNodeByKeyPath(originalContent, keyPath);\n\n // Restore the value at the given keyPath\n const restoredContent = editDictionaryByKeyPath(\n currentContent,\n keyPath,\n initialContent\n );\n\n return {\n ...prev,\n [localDictionaryId]: {\n ...prev?.[localDictionaryId],\n content: restoredContent as Dictionary['content'],\n },\n };\n });\n };\n\n const restoreEditedContent = (localDictionaryId: LocalDictionaryId) => {\n setEditedContentState((prev) => {\n const updated = { ...prev };\n delete updated[localDictionaryId];\n return updated;\n });\n };\n\n const clearEditedDictionaryContent = (\n localDictionaryId: LocalDictionaryId\n ) => {\n setEditedContentState((prev) => {\n const filtered = Object.entries(prev).reduce((acc, [key, value]) => {\n if (key === localDictionaryId) {\n return acc;\n }\n return { ...acc, [key]: value };\n }, {} as DictionaryContent);\n return filtered;\n });\n };\n\n const clearEditedContent = () => {\n setEditedContentState({});\n };\n\n const getEditedContentValue = (\n localDictionaryIdOrKey: LocalDictionaryId | Dictionary['key'] | string,\n keyPath: KeyPath[]\n ): ContentNode | undefined => {\n if (!editedContent) return undefined;\n\n const filteredKeyPath = keyPath.filter(\n (key) => key.type !== NodeType.Translation\n );\n\n const isDictionaryId =\n localDictionaryIdOrKey.includes(':local:') ||\n localDictionaryIdOrKey.includes(':remote:');\n\n if (isDictionaryId) {\n const currentContent =\n editedContent?.[localDictionaryIdOrKey as LocalDictionaryId]?.content ??\n {};\n\n const contentNode = getContentNodeByKeyPath(\n currentContent,\n filteredKeyPath,\n currentLocale\n );\n\n return contentNode;\n }\n\n const filteredDictionariesLocalId = Object.keys(editedContent).filter(\n (key) => key.startsWith(`${localDictionaryIdOrKey}:`)\n );\n\n for (const localDictionaryId of filteredDictionariesLocalId) {\n const currentContent =\n editedContent?.[localDictionaryId as LocalDictionaryId]?.content ?? {};\n const contentNode = getContentNodeByKeyPath(\n currentContent,\n filteredKeyPath,\n currentLocale\n );\n\n if (contentNode) return contentNode;\n }\n\n return undefined;\n };\n\n return (\n <EditedContentStateContext.Provider\n value={{\n editedContent,\n }}\n >\n <EditedContentActionsContext.Provider\n value={{\n setEditedContentState,\n setEditedDictionary,\n setEditedContent,\n addEditedContent,\n renameEditedContent,\n removeEditedContent,\n restoreEditedContent,\n clearEditedDictionaryContent,\n clearEditedContent,\n getEditedContentValue,\n }}\n >\n {children}\n </EditedContentActionsContext.Provider>\n </EditedContentStateContext.Provider>\n );\n};\n\nexport const useEditedContentActions = () =>\n useContext(EditedContentActionsContext);\n\nexport const useEditedContent = () => {\n const stateContext = useContext(EditedContentStateContext);\n const actionContext = useEditedContentActions();\n\n return { ...stateContext, ...actionContext };\n};\n"],"mappings":"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"}
1
+ {"version":3,"file":"EditedContentContext.mjs","names":[],"sources":["../../src/EditedContentContext.tsx"],"sourcesContent":["'use client';\n\nimport {\n editDictionaryByKeyPath,\n getContentNodeByKeyPath,\n renameContentNodeByKeyPath,\n} from '@intlayer/core/dictionaryManipulator';\nimport { MessageKey } from '@intlayer/editor';\nimport {\n type ContentNode,\n type Dictionary,\n type KeyPath,\n type LocalDictionaryId,\n NodeType,\n} from '@intlayer/types';\nimport {\n createContext,\n type Dispatch,\n type FC,\n type PropsWithChildren,\n type SetStateAction,\n useContext,\n} from 'react';\nimport {\n type DictionaryContent,\n useDictionariesRecord,\n} from './DictionariesRecordContext';\nimport { useCrossFrameMessageListener } from './useCrossFrameMessageListener';\nimport { useCrossFrameState } from './useCrossFrameState';\nimport { useEditorLocale } from './useEditorLocale';\n\ntype EditedContentStateContextType = {\n editedContent: Record<LocalDictionaryId, Dictionary> | undefined;\n};\n\nconst EditedContentStateContext = createContext<\n EditedContentStateContextType | undefined\n>(undefined);\n\nexport const usePostEditedContentState = <S,>(\n onEventTriggered?: (data: S) => void\n) =>\n useCrossFrameMessageListener(\n `${MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/post`,\n onEventTriggered\n );\n\nexport const useGetEditedContentState = <S,>(\n onEventTriggered?: (data: S) => void\n) =>\n useCrossFrameMessageListener(\n `${MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/get`,\n onEventTriggered\n );\n\ntype EditedContentActionsContextType = {\n setEditedContentState: (editedContent: DictionaryContent) => void;\n setEditedDictionary: Dispatch<SetStateAction<Dictionary>>;\n setEditedContent: (\n localDictionaryId: LocalDictionaryId,\n newValue: Dictionary['content']\n ) => void;\n addEditedContent: (\n localDictionaryId: LocalDictionaryId,\n newValue: ContentNode<any>,\n keyPath?: KeyPath[],\n overwrite?: boolean\n ) => void;\n renameEditedContent: (\n localDictionaryId: LocalDictionaryId,\n newKey: KeyPath['key'],\n keyPath?: KeyPath[]\n ) => void;\n removeEditedContent: (\n localDictionaryId: LocalDictionaryId,\n keyPath: KeyPath[]\n ) => void;\n restoreEditedContent: (localDictionaryId: LocalDictionaryId) => void;\n clearEditedDictionaryContent: (localDictionaryId: LocalDictionaryId) => void;\n clearEditedContent: () => void;\n getEditedContentValue: (\n localDictionaryIdOrKey: LocalDictionaryId | Dictionary['key'] | string,\n keyPath: KeyPath[]\n ) => ContentNode | undefined;\n};\n\nconst EditedContentActionsContext = createContext<\n EditedContentActionsContextType | undefined\n>(undefined);\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\nexport const EditedContentProvider: FC<PropsWithChildren> = ({ children }) => {\n const { localeDictionaries } = useDictionariesRecord();\n const currentLocale = useEditorLocale();\n\n const [editedContent, setEditedContentState] =\n useCrossFrameState<DictionaryContent>(\n MessageKey.INTLAYER_EDITED_CONTENT_CHANGED\n );\n\n const setEditedDictionary: Dispatch<SetStateAction<Dictionary>> = (\n newValue\n ) => {\n let updatedDictionaries: Dictionary = resolveState(newValue);\n\n setEditedContentState((prev) => {\n if (!updatedDictionaries.localId) {\n console.error('no localId', updatedDictionaries);\n\n return prev;\n }\n\n updatedDictionaries = resolveState(\n newValue,\n prev?.[updatedDictionaries.localId]\n );\n\n return {\n ...prev,\n [updatedDictionaries.localId as LocalDictionaryId]: updatedDictionaries,\n };\n });\n\n return updatedDictionaries;\n };\n\n const setEditedContent = (\n localDictionaryId: LocalDictionaryId,\n newValue: Dictionary['content']\n ) => {\n setEditedContentState((prev) => ({\n ...prev,\n [localDictionaryId]: {\n ...prev?.[localDictionaryId],\n content: newValue,\n },\n }));\n };\n\n const addEditedContent = (\n localDictionaryId: LocalDictionaryId,\n newValue: ContentNode,\n keyPath: KeyPath[] = [],\n overwrite: boolean = true\n ) => {\n setEditedContentState((prev) => {\n // Get the starting content: edited version if available, otherwise a deep copy of the original\n const originalContent = localeDictionaries[localDictionaryId]?.content;\n const currentContent = structuredClone(\n prev?.[localDictionaryId]?.content ?? originalContent\n );\n\n let newKeyPath = keyPath;\n if (!overwrite) {\n // Find a unique key based on the keyPath provided\n let index = 0;\n\n const otherKeyPath = keyPath.slice(0, -1);\n const lastKeyPath: KeyPath = keyPath[keyPath.length - 1];\n\n let finalKey = lastKeyPath.key;\n\n // Loop until we find a key that does not exist\n while (\n typeof getContentNodeByKeyPath(currentContent, newKeyPath) !==\n 'undefined'\n ) {\n index++;\n finalKey =\n index === 0 ? lastKeyPath.key : `${lastKeyPath.key} (${index})`;\n newKeyPath = [\n ...otherKeyPath,\n { ...lastKeyPath, key: finalKey } as KeyPath,\n ];\n }\n }\n\n const updatedContent = editDictionaryByKeyPath(\n currentContent,\n newKeyPath,\n newValue\n );\n\n return {\n ...prev,\n [localDictionaryId]: {\n ...prev?.[localDictionaryId],\n content: updatedContent as Dictionary['content'],\n },\n };\n });\n };\n\n const renameEditedContent = (\n localDictionaryId: LocalDictionaryId,\n newKey: KeyPath['key'],\n keyPath: KeyPath[] = []\n ) => {\n setEditedContentState((prev) => {\n // Retrieve the base content: use edited version if available, otherwise deep copy of original\n const originalContent = localeDictionaries[localDictionaryId]?.content;\n const currentContent = structuredClone(\n prev?.[localDictionaryId]?.content ?? originalContent\n );\n\n const contentWithNewField = renameContentNodeByKeyPath(\n currentContent,\n newKey,\n keyPath\n );\n\n return {\n ...prev,\n [localDictionaryId]: {\n ...prev?.[localDictionaryId],\n content: contentWithNewField as Dictionary['content'],\n },\n };\n });\n };\n\n const removeEditedContent = (\n localDictionaryId: LocalDictionaryId,\n keyPath: KeyPath[]\n ) => {\n setEditedContentState((prev) => {\n // Retrieve the original content as reference\n const originalContent = localeDictionaries[localDictionaryId]?.content;\n const currentContent = structuredClone(\n prev?.[localDictionaryId]?.content ?? originalContent\n );\n\n // Get the initial value from the original dictionary content\n const initialContent = getContentNodeByKeyPath(originalContent, keyPath);\n\n // Restore the value at the given keyPath\n const restoredContent = editDictionaryByKeyPath(\n currentContent,\n keyPath,\n initialContent\n );\n\n return {\n ...prev,\n [localDictionaryId]: {\n ...prev?.[localDictionaryId],\n content: restoredContent as Dictionary['content'],\n },\n };\n });\n };\n\n const restoreEditedContent = (localDictionaryId: LocalDictionaryId) => {\n setEditedContentState((prev) => {\n const updated = { ...prev };\n delete updated[localDictionaryId];\n return updated;\n });\n };\n\n const clearEditedDictionaryContent = (\n localDictionaryId: LocalDictionaryId\n ) => {\n setEditedContentState((prev) => {\n const filtered = { ...prev };\n delete filtered[localDictionaryId];\n return filtered;\n });\n };\n\n const clearEditedContent = () => {\n setEditedContentState({});\n };\n\n const getEditedContentValue = (\n localDictionaryIdOrKey: LocalDictionaryId | Dictionary['key'] | string,\n keyPath: KeyPath[]\n ): ContentNode | undefined => {\n if (!editedContent) return undefined;\n\n const filteredKeyPath = keyPath.filter(\n (key) => key.type !== NodeType.Translation\n );\n\n const isDictionaryId =\n localDictionaryIdOrKey.includes(':local:') ||\n localDictionaryIdOrKey.includes(':remote:');\n\n if (isDictionaryId) {\n const currentContent =\n editedContent?.[localDictionaryIdOrKey as LocalDictionaryId]?.content ??\n {};\n\n const contentNode = getContentNodeByKeyPath(\n currentContent,\n filteredKeyPath,\n currentLocale\n );\n\n return contentNode;\n }\n\n const filteredDictionariesLocalId = Object.keys(editedContent).filter(\n (key) => key.startsWith(`${localDictionaryIdOrKey}:`)\n );\n\n for (const localDictionaryId of filteredDictionariesLocalId) {\n const currentContent =\n editedContent?.[localDictionaryId as LocalDictionaryId]?.content ?? {};\n const contentNode = getContentNodeByKeyPath(\n currentContent,\n filteredKeyPath,\n currentLocale\n );\n\n if (contentNode) return contentNode;\n }\n\n return undefined;\n };\n\n return (\n <EditedContentStateContext.Provider\n value={{\n editedContent,\n }}\n >\n <EditedContentActionsContext.Provider\n value={{\n setEditedContentState,\n setEditedDictionary,\n setEditedContent,\n addEditedContent,\n renameEditedContent,\n removeEditedContent,\n restoreEditedContent,\n clearEditedDictionaryContent,\n clearEditedContent,\n getEditedContentValue,\n }}\n >\n {children}\n </EditedContentActionsContext.Provider>\n </EditedContentStateContext.Provider>\n );\n};\n\nexport const useEditedContentActions = () =>\n useContext(EditedContentActionsContext);\n\nexport const useEditedContent = () => {\n const stateContext = useContext(EditedContentStateContext);\n const actionContext = useEditedContentActions();\n\n return { ...stateContext, ...actionContext };\n};\n"],"mappings":"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"}
@@ -1,28 +1,2 @@
1
- 'use client';
2
-
3
- import { useCrossFrameMessageListener } from "./useCrossFrameMessageListener.mjs";
4
- import { useCrossFrameState } from "./useCrossFrameState.mjs";
5
- import { createContext, useContext } from "react";
6
- import { jsx } from "react/jsx-runtime";
7
- import { MessageKey } from "@intlayer/editor";
8
-
9
- //#region src/EditorEnabledContext.tsx
10
- const EditorEnabledContext = createContext({ enabled: false });
11
- const useEditorEnabledState = (options) => useCrossFrameState(MessageKey.INTLAYER_EDITOR_ENABLED, false, options);
12
- const usePostEditorEnabledState = (onEventTriggered) => useCrossFrameMessageListener(`${MessageKey.INTLAYER_EDITOR_ENABLED}/post`, onEventTriggered);
13
- const useGetEditorEnabledState = (onEventTriggered) => useCrossFrameMessageListener(`${MessageKey.INTLAYER_EDITOR_ENABLED}/get`, onEventTriggered);
14
- const EditorEnabledProvider = ({ children }) => {
15
- const [isEnabled] = useEditorEnabledState({
16
- emit: false,
17
- receive: true
18
- });
19
- return /* @__PURE__ */ jsx(EditorEnabledContext.Provider, {
20
- value: { enabled: isEnabled },
21
- children
22
- });
23
- };
24
- const useEditorEnabled = () => useContext(EditorEnabledContext);
25
-
26
- //#endregion
27
- export { EditorEnabledProvider, useEditorEnabled, useEditorEnabledState, useGetEditorEnabledState, usePostEditorEnabledState };
1
+ "use client";import{useCrossFrameMessageListener as e}from"./useCrossFrameMessageListener.mjs";import{useCrossFrameState as t}from"./useCrossFrameState.mjs";import{createContext as n,useContext as r}from"react";import{jsx as i}from"react/jsx-runtime";import{MessageKey as a}from"@intlayer/editor";const o=n({enabled:!1}),s=e=>t(a.INTLAYER_EDITOR_ENABLED,!1,e),c=t=>e(`${a.INTLAYER_EDITOR_ENABLED}/post`,t),l=t=>e(`${a.INTLAYER_EDITOR_ENABLED}/get`,t),u=({children:e})=>{let[t]=s({emit:!1,receive:!0});return i(o.Provider,{value:{enabled:t},children:e})},d=()=>r(o);export{u as EditorEnabledProvider,d as useEditorEnabled,s as useEditorEnabledState,l as useGetEditorEnabledState,c as usePostEditorEnabledState};
28
2
  //# sourceMappingURL=EditorEnabledContext.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"EditorEnabledContext.mjs","names":[],"sources":["../../src/EditorEnabledContext.tsx"],"sourcesContent":["'use client';\n\nimport { MessageKey } from '@intlayer/editor';\nimport {\n createContext,\n type FC,\n type PropsWithChildren,\n useContext,\n} from 'react';\nimport { useCrossFrameMessageListener } from './useCrossFrameMessageListener';\nimport {\n type CrossFrameStateOptions,\n useCrossFrameState,\n} from './useCrossFrameState';\n\nexport type EditorEnabledStateProps = {\n enabled: boolean;\n};\n\nconst EditorEnabledContext = createContext<EditorEnabledStateProps>({\n enabled: false,\n});\n\nexport const useEditorEnabledState = (options?: CrossFrameStateOptions) =>\n useCrossFrameState(MessageKey.INTLAYER_EDITOR_ENABLED, false, options);\n\nexport const usePostEditorEnabledState = <S,>(\n onEventTriggered?: (data: S) => void\n) =>\n useCrossFrameMessageListener(\n `${MessageKey.INTLAYER_EDITOR_ENABLED}/post`,\n onEventTriggered\n );\n\nexport const useGetEditorEnabledState = <S,>(\n onEventTriggered?: (data: S) => void\n) =>\n useCrossFrameMessageListener(\n `${MessageKey.INTLAYER_EDITOR_ENABLED}/get`,\n onEventTriggered\n );\n\nexport const EditorEnabledProvider: FC<PropsWithChildren> = ({ children }) => {\n const [isEnabled] = useEditorEnabledState({\n emit: false,\n receive: true,\n });\n\n return (\n <EditorEnabledContext.Provider value={{ enabled: isEnabled }}>\n {children}\n </EditorEnabledContext.Provider>\n );\n};\n\nexport const useEditorEnabled = () => useContext(EditorEnabledContext);\n"],"mappings":";;;;;;;;;AAmBA,MAAM,uBAAuB,cAAuC,EAClE,SAAS,OACV,CAAC;AAEF,MAAa,yBAAyB,YACpC,mBAAmB,WAAW,yBAAyB,OAAO,QAAQ;AAExE,MAAa,6BACX,qBAEA,6BACE,GAAG,WAAW,wBAAwB,QACtC,iBACD;AAEH,MAAa,4BACX,qBAEA,6BACE,GAAG,WAAW,wBAAwB,OACtC,iBACD;AAEH,MAAa,yBAAgD,EAAE,eAAe;CAC5E,MAAM,CAAC,aAAa,sBAAsB;EACxC,MAAM;EACN,SAAS;EACV,CAAC;AAEF,QACE,oBAAC,qBAAqB;EAAS,OAAO,EAAE,SAAS,WAAW;EACzD;GAC6B;;AAIpC,MAAa,yBAAyB,WAAW,qBAAqB"}
1
+ {"version":3,"file":"EditorEnabledContext.mjs","names":[],"sources":["../../src/EditorEnabledContext.tsx"],"sourcesContent":["'use client';\n\nimport { MessageKey } from '@intlayer/editor';\nimport {\n createContext,\n type FC,\n type PropsWithChildren,\n useContext,\n} from 'react';\nimport { useCrossFrameMessageListener } from './useCrossFrameMessageListener';\nimport {\n type CrossFrameStateOptions,\n useCrossFrameState,\n} from './useCrossFrameState';\n\nexport type EditorEnabledStateProps = {\n enabled: boolean;\n};\n\nconst EditorEnabledContext = createContext<EditorEnabledStateProps>({\n enabled: false,\n});\n\nexport const useEditorEnabledState = (options?: CrossFrameStateOptions) =>\n useCrossFrameState(MessageKey.INTLAYER_EDITOR_ENABLED, false, options);\n\nexport const usePostEditorEnabledState = <S,>(\n onEventTriggered?: (data: S) => void\n) =>\n useCrossFrameMessageListener(\n `${MessageKey.INTLAYER_EDITOR_ENABLED}/post`,\n onEventTriggered\n );\n\nexport const useGetEditorEnabledState = <S,>(\n onEventTriggered?: (data: S) => void\n) =>\n useCrossFrameMessageListener(\n `${MessageKey.INTLAYER_EDITOR_ENABLED}/get`,\n onEventTriggered\n );\n\nexport const EditorEnabledProvider: FC<PropsWithChildren> = ({ children }) => {\n const [isEnabled] = useEditorEnabledState({\n emit: false,\n receive: true,\n });\n\n return (\n <EditorEnabledContext.Provider value={{ enabled: isEnabled }}>\n {children}\n </EditorEnabledContext.Provider>\n );\n};\n\nexport const useEditorEnabled = () => useContext(EditorEnabledContext);\n"],"mappings":"ySAmBA,MAAM,EAAuB,EAAuC,CAClE,QAAS,GACV,CAAC,CAEW,EAAyB,GACpC,EAAmB,EAAW,wBAAyB,GAAO,EAAQ,CAE3D,EACX,GAEA,EACE,GAAG,EAAW,wBAAwB,OACtC,EACD,CAEU,EACX,GAEA,EACE,GAAG,EAAW,wBAAwB,MACtC,EACD,CAEU,GAAgD,CAAE,cAAe,CAC5E,GAAM,CAAC,GAAa,EAAsB,CACxC,KAAM,GACN,QAAS,GACV,CAAC,CAEF,OACE,EAAC,EAAqB,SAAA,CAAS,MAAO,CAAE,QAAS,EAAW,CACzD,YAC6B,EAIvB,MAAyB,EAAW,EAAqB"}
@@ -1,66 +1,2 @@
1
- 'use client';
2
-
3
- import { CommunicatorProvider } from "./CommunicatorContext.mjs";
4
- import { ConfigurationProvider } from "./ConfigurationContext.mjs";
5
- import { DictionariesRecordProvider } from "./DictionariesRecordContext.mjs";
6
- import { EditedContentProvider, useGetEditedContentState } from "./EditedContentContext.mjs";
7
- import { EditorEnabledProvider, useEditorEnabled, useGetEditorEnabledState } from "./EditorEnabledContext.mjs";
8
- import { FocusDictionaryProvider } from "./FocusDictionaryContext.mjs";
9
- import { useEffect, useState } from "react";
10
- import { jsx } from "react/jsx-runtime";
11
-
12
- //#region src/EditorProvider.tsx
13
- /**
14
- * This component add all the providers needed by the editor.
15
- * It is used to wrap the application, or the editor to work together.
16
- */
17
- const EditorProvidersWrapper = ({ children }) => {
18
- const getEditedContentState = useGetEditedContentState();
19
- useEffect(() => {
20
- getEditedContentState();
21
- }, []);
22
- return /* @__PURE__ */ jsx(DictionariesRecordProvider, { children: /* @__PURE__ */ jsx(EditedContentProvider, { children: /* @__PURE__ */ jsx(FocusDictionaryProvider, { children }) }) });
23
- };
24
- /**
25
- * This component check if the editor is enabled to render the editor providers.
26
- */
27
- const EditorEnabledCheckRenderer = ({ children, fallback }) => {
28
- const getEditorEnabled = useGetEditorEnabledState();
29
- const { enabled } = useEditorEnabled();
30
- useEffect(() => {
31
- if (enabled) return;
32
- getEditorEnabled();
33
- }, [enabled]);
34
- return enabled ? children : fallback;
35
- };
36
- /**
37
- * This component is used to check if the editor is wrapping the application.
38
- * It avoid to send window.postMessage to the application if the editor is not wrapping the application.
39
- */
40
- const IframeCheckRenderer = ({ children, fallback }) => {
41
- const [isInIframe, setIsInIframe] = useState(false);
42
- useEffect(() => {
43
- setIsInIframe(window.self !== window.top);
44
- }, []);
45
- return isInIframe ? children : fallback;
46
- };
47
- const EditorProvider = ({ children, configuration, ...props }) => /* @__PURE__ */ jsx(EditorEnabledProvider, { children: /* @__PURE__ */ jsx(ConfigurationProvider, {
48
- configuration,
49
- children: props.mode === "editor" ? /* @__PURE__ */ jsx(CommunicatorProvider, {
50
- ...props,
51
- children: /* @__PURE__ */ jsx(EditorProvidersWrapper, { children })
52
- }) : /* @__PURE__ */ jsx(IframeCheckRenderer, {
53
- fallback: children,
54
- children: /* @__PURE__ */ jsx(CommunicatorProvider, {
55
- ...props,
56
- children: /* @__PURE__ */ jsx(EditorEnabledCheckRenderer, {
57
- fallback: children,
58
- children: /* @__PURE__ */ jsx(EditorProvidersWrapper, { children })
59
- })
60
- })
61
- })
62
- }) });
63
-
64
- //#endregion
65
- export { EditorProvider };
1
+ "use client";import{CommunicatorProvider as e}from"./CommunicatorContext.mjs";import{ConfigurationProvider as t}from"./ConfigurationContext.mjs";import{DictionariesRecordProvider as n}from"./DictionariesRecordContext.mjs";import{EditedContentProvider as r,useGetEditedContentState as i}from"./EditedContentContext.mjs";import{EditorEnabledProvider as a,useEditorEnabled as o,useGetEditorEnabledState as s}from"./EditorEnabledContext.mjs";import{FocusDictionaryProvider as c}from"./FocusDictionaryContext.mjs";import{useEffect as l,useState as u}from"react";import{jsx as d}from"react/jsx-runtime";const f=({children:e})=>{let t=i();return l(()=>{t()},[]),d(n,{children:d(r,{children:d(c,{children:e})})})},p=({children:e,fallback:t})=>{let n=s(),{enabled:r}=o();return l(()=>{r||n()},[r]),r?e:t},m=({children:e,fallback:t})=>{let[n,r]=u(!1);return l(()=>{r(window.self!==window.top)},[]),n?e:t},h=({children:n,configuration:r,...i})=>d(a,{children:d(t,{configuration:r,children:i.mode===`editor`?d(e,{...i,children:d(f,{children:n})}):d(m,{fallback:n,children:d(e,{...i,children:d(p,{fallback:n,children:d(f,{children:n})})})})})});export{h as EditorProvider};
66
2
  //# sourceMappingURL=EditorProvider.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"EditorProvider.mjs","names":[],"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,MAAM,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,MAAM,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,MAAM,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,MAAa,kBAA8D,EACzE,UACA,eACA,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
+ {"version":3,"file":"EditorProvider.mjs","names":[],"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":"qlBAiCA,MAAM,GAAiD,CAAE,cAAe,CACtE,IAAM,EAAwB,GAA0B,CAMxD,OAJA,MAAgB,CACd,GAAuB,EACtB,EAAE,CAAC,CAGJ,EAAC,EAAA,CAAA,SACC,EAAC,EAAA,CAAA,SACC,EAAC,EAAA,CAAyB,WAAA,CAAmC,CAAA,CACvC,CAAA,CACG,EAW3B,GAAoE,CACxE,WACA,cACI,CACJ,IAAM,EAAmB,GAA0B,CAE7C,CAAE,WAAY,GAAkB,CAStC,OAPA,MAAgB,CACV,GAGJ,GAAkB,EACjB,CAAC,EAAQ,CAAC,CAEN,EAAU,EAAW,GAOxB,GAA6D,CACjE,WACA,cACI,CACJ,GAAM,CAAC,EAAY,GAAiB,EAAS,GAAM,CAMnD,OAJA,MAAgB,CACd,EAAc,OAAO,OAAS,OAAO,IAAI,EACxC,EAAE,CAAC,CAEC,EAAa,EAAW,GAQpB,GAA8D,CACzE,WACA,gBACA,GAAG,KAEH,EAAC,EAAA,CAAA,SACC,EAAC,EAAA,CAAqC,yBACnC,EAAM,OAAS,SACd,EAAC,EAAA,CAAqB,GAAI,WACxB,EAAC,EAAA,CAAwB,WAAA,CAAkC,EACtC,CAEvB,EAAC,EAAA,CAAoB,SAAU,WAC7B,EAAC,EAAA,CAAqB,GAAI,WACxB,EAAC,EAAA,CAA2B,SAAU,WACpC,EAAC,EAAA,CAAwB,WAAA,CAAkC,EAChC,EACR,EACH,EAEF,CAAA,CACF"}
@@ -1,52 +1,2 @@
1
- 'use client';
2
-
3
- import { useCrossFrameState } from "./useCrossFrameState.mjs";
4
- import { createContext, useContext } from "react";
5
- import { jsx } from "react/jsx-runtime";
6
- import { MessageKey } from "@intlayer/editor";
7
- import { NodeType } from "@intlayer/types";
8
-
9
- //#region src/FocusDictionaryContext.tsx
10
- const FocusDictionaryStateContext = createContext(void 0);
11
- const FocusDictionaryActionsContext = createContext(void 0);
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
- const filteredKeyPath = keyPath.filter((key) => key.type !== NodeType.Translation);
18
- return {
19
- ...prev,
20
- keyPath: filteredKeyPath
21
- };
22
- });
23
- };
24
- return /* @__PURE__ */ jsx(FocusDictionaryStateContext.Provider, {
25
- value: { focusedContent },
26
- children: /* @__PURE__ */ jsx(FocusDictionaryActionsContext.Provider, {
27
- value: {
28
- setFocusedContent,
29
- setFocusedContentKeyPath
30
- },
31
- children
32
- })
33
- });
34
- };
35
- const useFocusDictionaryActions = () => {
36
- const context = useContext(FocusDictionaryActionsContext);
37
- if (context === void 0) throw new Error("useFocusDictionaryActions must be used within a FocusDictionaryProvider");
38
- return context;
39
- };
40
- const useFocusDictionary = () => {
41
- const actionContext = useFocusDictionaryActions();
42
- const stateContext = useContext(FocusDictionaryStateContext);
43
- if (stateContext === void 0) throw new Error("useFocusDictionaryState must be used within a FocusDictionaryProvider");
44
- return {
45
- ...stateContext,
46
- ...actionContext
47
- };
48
- };
49
-
50
- //#endregion
51
- export { FocusDictionaryProvider, useFocusDictionary, useFocusDictionaryActions };
1
+ "use client";import{useCrossFrameState as e}from"./useCrossFrameState.mjs";import{createContext as t,useContext as n}from"react";import{jsx as r}from"react/jsx-runtime";import{MessageKey as i}from"@intlayer/editor";import{NodeType as a}from"@intlayer/types";const o=t(void 0),s=t(void 0),c=({children:t})=>{let[n,c]=e(i.INTLAYER_FOCUSED_CONTENT_CHANGED,null);return r(o.Provider,{value:{focusedContent:n},children:r(s.Provider,{value:{setFocusedContent:c,setFocusedContentKeyPath:e=>{c(t=>{if(!t)return t;let n=e.filter(e=>e.type!==a.Translation);return{...t,keyPath:n}})}},children:t})})},l=()=>{let e=n(s);if(e===void 0)throw Error(`useFocusDictionaryActions must be used within a FocusDictionaryProvider`);return e},u=()=>{let e=l(),t=n(o);if(t===void 0)throw Error(`useFocusDictionaryState must be used within a FocusDictionaryProvider`);return{...t,...e}};export{c as FocusDictionaryProvider,u as useFocusDictionary,l as useFocusDictionaryActions};
52
2
  //# sourceMappingURL=FocusDictionaryContext.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"FocusDictionaryContext.mjs","names":[],"sources":["../../src/FocusDictionaryContext.tsx"],"sourcesContent":["'use client';\n\nimport { MessageKey } from '@intlayer/editor';\nimport {\n type KeyPath,\n type LocalDictionaryId,\n NodeType,\n} 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?: LocalDictionaryId;\n keyPath?: KeyPath[];\n};\n\nexport type FocusDictionaryState = {\n focusedContent: FileContent | null;\n};\n\nexport type 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\n // Remove translation key path if it exists to make it more flexible with optimization client / editor\n const filteredKeyPath = keyPath.filter(\n (key) => key.type !== NodeType.Translation\n );\n\n return {\n ...prev,\n keyPath: filteredKeyPath,\n };\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":";;;;;;;;;AAiCA,MAAM,8BAA8B,cAElC,OAAU;AACZ,MAAM,gCAAgC,cAEpC,OAAU;AAEZ,MAAa,2BAAkD,EAC7D,eACI;CACJ,MAAM,CAAC,gBAAgB,qBACrB,mBACE,WAAW,kCACX,KACD;CAEH,MAAM,4BAA4B,YAAuB;AACvD,qBAAmB,SAAS;AAC1B,OAAI,CAAC,KACH,QAAO;GAIT,MAAM,kBAAkB,QAAQ,QAC7B,QAAQ,IAAI,SAAS,SAAS,YAChC;AAED,UAAO;IACL,GAAG;IACH,SAAS;IACV;IACD;;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
+ {"version":3,"file":"FocusDictionaryContext.mjs","names":[],"sources":["../../src/FocusDictionaryContext.tsx"],"sourcesContent":["'use client';\n\nimport { MessageKey } from '@intlayer/editor';\nimport {\n type KeyPath,\n type LocalDictionaryId,\n NodeType,\n} 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?: LocalDictionaryId;\n keyPath?: KeyPath[];\n};\n\nexport type FocusDictionaryState = {\n focusedContent: FileContent | null;\n};\n\nexport type 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\n // Remove translation key path if it exists to make it more flexible with optimization client / editor\n const filteredKeyPath = keyPath.filter(\n (key) => key.type !== NodeType.Translation\n );\n\n return {\n ...prev,\n keyPath: filteredKeyPath,\n };\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":"kQAiCA,MAAM,EAA8B,EAElC,IAAA,GAAU,CACN,EAAgC,EAEpC,IAAA,GAAU,CAEC,GAAkD,CAC7D,cACI,CACJ,GAAM,CAAC,EAAgB,GACrB,EACE,EAAW,iCACX,KACD,CAoBH,OACE,EAAC,EAA4B,SAAA,CAAS,MAAO,CAAE,iBAAgB,UAC7D,EAAC,EAA8B,SAAA,CAC7B,MAAO,CAAE,oBAAmB,yBArBA,GAAuB,CACvD,EAAmB,GAAS,CAC1B,GAAI,CAAC,EACH,OAAO,EAIT,IAAM,EAAkB,EAAQ,OAC7B,GAAQ,EAAI,OAAS,EAAS,YAChC,CAED,MAAO,CACL,GAAG,EACH,QAAS,EACV,EACD,EAMwD,CAErD,YACsC,EACJ,EAI9B,MAAkC,CAC7C,IAAM,EAAU,EAAW,EAA8B,CACzD,GAAI,IAAY,IAAA,GACd,MAAU,MACR,0EACD,CAEH,OAAO,GAGI,MAA2B,CACtC,IAAM,EAAgB,GAA2B,CAC3C,EAAe,EAAW,EAA4B,CAE5D,GAAI,IAAiB,IAAA,GACnB,MAAU,MACR,wEACD,CAGH,MAAO,CAAE,GAAG,EAAc,GAAG,EAAe"}