@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.
- package/dist/cjs/CommunicatorContext.cjs +1 -42
- package/dist/cjs/CommunicatorContext.cjs.map +1 -1
- package/dist/cjs/ConfigurationContext.cjs +1 -25
- package/dist/cjs/ConfigurationContext.cjs.map +1 -1
- package/dist/cjs/DictionariesRecordContext.cjs +1 -48
- package/dist/cjs/DictionariesRecordContext.cjs.map +1 -1
- package/dist/cjs/EditedContentContext.cjs +1 -169
- package/dist/cjs/EditedContentContext.cjs.map +1 -1
- package/dist/cjs/EditorEnabledContext.cjs +1 -33
- package/dist/cjs/EditorEnabledContext.cjs.map +1 -1
- package/dist/cjs/EditorProvider.cjs +1 -67
- package/dist/cjs/EditorProvider.cjs.map +1 -1
- package/dist/cjs/FocusDictionaryContext.cjs +1 -55
- package/dist/cjs/FocusDictionaryContext.cjs.map +1 -1
- package/dist/cjs/_virtual/_rolldown/runtime.cjs +1 -29
- package/dist/cjs/index.cjs +1 -53
- package/dist/cjs/useCrossFrameMessageListener.cjs +1 -78
- package/dist/cjs/useCrossFrameMessageListener.cjs.map +1 -1
- package/dist/cjs/useCrossFrameState.cjs +1 -108
- package/dist/cjs/useCrossFrameState.cjs.map +1 -1
- package/dist/cjs/useCrossURLPathState.cjs +1 -67
- package/dist/cjs/useCrossURLPathState.cjs.map +1 -1
- package/dist/cjs/useEditorLocale.cjs +1 -23
- package/dist/cjs/useEditorLocale.cjs.map +1 -1
- package/dist/cjs/useFocusUnmergedDictionary.cjs +1 -51
- package/dist/cjs/useFocusUnmergedDictionary.cjs.map +1 -1
- package/dist/cjs/useIframeClickInterceptor.cjs +1 -24
- package/dist/cjs/useIframeClickInterceptor.cjs.map +1 -1
- package/dist/esm/CommunicatorContext.mjs +1 -38
- package/dist/esm/CommunicatorContext.mjs.map +1 -1
- package/dist/esm/ConfigurationContext.mjs +1 -21
- package/dist/esm/ConfigurationContext.mjs.map +1 -1
- package/dist/esm/DictionariesRecordContext.mjs +1 -44
- package/dist/esm/DictionariesRecordContext.mjs.map +1 -1
- package/dist/esm/EditedContentContext.mjs +1 -163
- package/dist/esm/EditedContentContext.mjs.map +1 -1
- package/dist/esm/EditorEnabledContext.mjs +1 -27
- package/dist/esm/EditorEnabledContext.mjs.map +1 -1
- package/dist/esm/EditorProvider.mjs +1 -65
- package/dist/esm/EditorProvider.mjs.map +1 -1
- package/dist/esm/FocusDictionaryContext.mjs +1 -51
- package/dist/esm/FocusDictionaryContext.mjs.map +1 -1
- package/dist/esm/index.mjs +1 -17
- package/dist/esm/useCrossFrameMessageListener.mjs +1 -76
- package/dist/esm/useCrossFrameMessageListener.mjs.map +1 -1
- package/dist/esm/useCrossFrameState.mjs +1 -106
- package/dist/esm/useCrossFrameState.mjs.map +1 -1
- package/dist/esm/useCrossURLPathState.mjs +1 -64
- package/dist/esm/useCrossURLPathState.mjs.map +1 -1
- package/dist/esm/useEditorLocale.mjs +1 -20
- package/dist/esm/useEditorLocale.mjs.map +1 -1
- package/dist/esm/useFocusUnmergedDictionary.mjs +1 -49
- package/dist/esm/useFocusUnmergedDictionary.mjs.map +1 -1
- package/dist/esm/useIframeClickInterceptor.mjs +1 -21
- package/dist/esm/useIframeClickInterceptor.mjs.map +1 -1
- package/dist/types/EditedContentContext.d.ts.map +1 -1
- package/package.json +6 -6
|
@@ -1,43 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
-
let _intlayer_config_built = require("@intlayer/config/built");
|
|
6
|
-
_intlayer_config_built = require_runtime.__toESM(_intlayer_config_built);
|
|
7
|
-
let react = require("react");
|
|
8
|
-
let react_jsx_runtime = require("react/jsx-runtime");
|
|
9
|
-
|
|
10
|
-
//#region src/CommunicatorContext.tsx
|
|
11
|
-
const randomUUID = () => Math.random().toString(36).slice(2);
|
|
12
|
-
const { editor } = _intlayer_config_built.default ?? {};
|
|
13
|
-
const CommunicatorContext = (0, react.createContext)({
|
|
14
|
-
postMessage: () => null,
|
|
15
|
-
allowedOrigins: [
|
|
16
|
-
editor?.applicationURL,
|
|
17
|
-
editor?.editorURL,
|
|
18
|
-
editor?.cmsURL
|
|
19
|
-
],
|
|
20
|
-
senderId: ""
|
|
21
|
-
});
|
|
22
|
-
const CommunicatorProvider = ({ children, allowedOrigins, postMessage }) => {
|
|
23
|
-
const senderIdRef = (0, react.useRef)(randomUUID());
|
|
24
|
-
const value = (0, react.useMemo)(() => ({
|
|
25
|
-
postMessage,
|
|
26
|
-
allowedOrigins,
|
|
27
|
-
senderId: senderIdRef.current
|
|
28
|
-
}), [
|
|
29
|
-
postMessage,
|
|
30
|
-
allowedOrigins,
|
|
31
|
-
senderIdRef.current
|
|
32
|
-
]);
|
|
33
|
-
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CommunicatorContext.Provider, {
|
|
34
|
-
value,
|
|
35
|
-
children
|
|
36
|
-
});
|
|
37
|
-
};
|
|
38
|
-
const useCommunicator = () => (0, react.useContext)(CommunicatorContext);
|
|
39
|
-
|
|
40
|
-
//#endregion
|
|
41
|
-
exports.CommunicatorProvider = CommunicatorProvider;
|
|
42
|
-
exports.useCommunicator = useCommunicator;
|
|
1
|
+
"use client";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./_virtual/_rolldown/runtime.cjs`);let t=require(`@intlayer/config/built`);t=e.__toESM(t);let n=require(`react`),r=require(`react/jsx-runtime`);const i=()=>Math.random().toString(36).slice(2),{editor:a}=t.default??{},o=(0,n.createContext)({postMessage:()=>null,allowedOrigins:[a?.applicationURL,a?.editorURL,a?.cmsURL],senderId:``}),s=({children:e,allowedOrigins:t,postMessage:a})=>{let s=(0,n.useRef)(i()),c=(0,n.useMemo)(()=>({postMessage:a,allowedOrigins:t,senderId:s.current}),[a,t,s.current]);return(0,r.jsx)(o.Provider,{value:c,children:e})},c=()=>(0,n.useContext)(o);exports.CommunicatorProvider=s,exports.useCommunicator=c;
|
|
43
2
|
//# sourceMappingURL=CommunicatorContext.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CommunicatorContext.cjs","names":["configuration"],"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":"
|
|
1
|
+
{"version":3,"file":"CommunicatorContext.cjs","names":["configuration"],"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":"iPAYA,MAAM,MAAmB,KAAK,QAAQ,CAAC,SAAS,GAAG,CAAC,MAAM,EAAE,CAQtD,CAAE,UAAWA,EAAAA,SAAiB,EAAE,CAEhC,GAAA,EAAA,EAAA,eAAgE,CACpE,gBAAmB,KACnB,eAAgB,CACd,GAAQ,eACR,GAAQ,UACR,GAAQ,OACT,CACD,SAAU,GACX,CAAC,CAMW,GAAuD,CAClE,WACA,iBACA,iBACI,CAEJ,IAAM,GAAA,EAAA,EAAA,QAAqB,GAAY,CAAC,CAElC,GAAA,EAAA,EAAA,cACG,CAAE,cAAa,iBAAgB,SAAU,EAAY,QAAS,EACrE,CAAC,EAAa,EAAgB,EAAY,QAAQ,CACnD,CAED,OACE,EAAA,EAAA,KAAC,EAAoB,SAAA,CAAgB,QAClC,YAC4B,EAItB,OAAA,EAAA,EAAA,YAAmC,EAAoB"}
|
|
@@ -1,26 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
-
const require_useCrossFrameState = require('./useCrossFrameState.cjs');
|
|
6
|
-
let react = require("react");
|
|
7
|
-
let react_jsx_runtime = require("react/jsx-runtime");
|
|
8
|
-
let _intlayer_editor = require("@intlayer/editor");
|
|
9
|
-
|
|
10
|
-
//#region src/ConfigurationContext.tsx
|
|
11
|
-
const ConfigurationStatesContext = (0, react.createContext)(void 0);
|
|
12
|
-
const useConfigurationState = () => require_useCrossFrameState.useCrossFrameState(_intlayer_editor.MessageKey.INTLAYER_CONFIGURATION, void 0, {
|
|
13
|
-
receive: false,
|
|
14
|
-
emit: true
|
|
15
|
-
});
|
|
16
|
-
const ConfigurationProvider = ({ children, configuration }) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ConfigurationStatesContext.Provider, {
|
|
17
|
-
value: configuration,
|
|
18
|
-
children
|
|
19
|
-
});
|
|
20
|
-
const useConfiguration = () => (0, react.useContext)(ConfigurationStatesContext);
|
|
21
|
-
|
|
22
|
-
//#endregion
|
|
23
|
-
exports.ConfigurationProvider = ConfigurationProvider;
|
|
24
|
-
exports.useConfiguration = useConfiguration;
|
|
25
|
-
exports.useConfigurationState = useConfigurationState;
|
|
1
|
+
"use client";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`./_virtual/_rolldown/runtime.cjs`);const e=require(`./useCrossFrameState.cjs`);let t=require(`react`),n=require(`react/jsx-runtime`),r=require(`@intlayer/editor`);const i=(0,t.createContext)(void 0),a=()=>e.useCrossFrameState(r.MessageKey.INTLAYER_CONFIGURATION,void 0,{receive:!1,emit:!0}),o=({children:e,configuration:t})=>(0,n.jsx)(i.Provider,{value:t,children:e}),s=()=>(0,t.useContext)(i);exports.ConfigurationProvider=o,exports.useConfiguration=s,exports.useConfigurationState=a;
|
|
26
2
|
//# sourceMappingURL=ConfigurationContext.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConfigurationContext.cjs","names":["useCrossFrameState","MessageKey"],"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":"
|
|
1
|
+
{"version":3,"file":"ConfigurationContext.cjs","names":["useCrossFrameState","MessageKey"],"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":"4PAYA,MAAM,GAAA,EAAA,EAAA,eACJ,IAAA,GACD,CAEY,MACXA,EAAAA,mBACEC,EAAAA,WAAW,uBACX,IAAA,GACA,CACE,QAAS,GACT,KAAM,GACP,CACF,CAMU,GAER,CAAE,WAAU,oBACf,EAAA,EAAA,KAAC,EAA2B,SAAA,CAAS,MAAO,EACzC,YACmC,CAG3B,OAAA,EAAA,EAAA,YAAoC,EAA2B"}
|
|
@@ -1,49 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
-
const require_useCrossFrameState = require('./useCrossFrameState.cjs');
|
|
6
|
-
let react = require("react");
|
|
7
|
-
let react_jsx_runtime = require("react/jsx-runtime");
|
|
8
|
-
let _intlayer_editor = require("@intlayer/editor");
|
|
9
|
-
|
|
10
|
-
//#region src/DictionariesRecordContext.tsx
|
|
11
|
-
const DictionariesRecordStatesContext = (0, react.createContext)(void 0);
|
|
12
|
-
const DictionariesRecordActionsContext = (0, react.createContext)(void 0);
|
|
13
|
-
const DictionariesRecordProvider = ({ children }) => {
|
|
14
|
-
const [localeDictionaries, setLocaleDictionaries] = require_useCrossFrameState.useCrossFrameState(_intlayer_editor.MessageKey.INTLAYER_LOCALE_DICTIONARIES_CHANGED, void 0);
|
|
15
|
-
const stateValue = (0, react.useMemo)(() => ({ localeDictionaries: localeDictionaries ?? {} }), [localeDictionaries]);
|
|
16
|
-
const actionValue = (0, react.useMemo)(() => ({
|
|
17
|
-
setLocaleDictionaries,
|
|
18
|
-
setLocaleDictionary: (dictionary) => {
|
|
19
|
-
if (!dictionary.localId) return;
|
|
20
|
-
setLocaleDictionaries((dictionaries) => ({
|
|
21
|
-
...dictionaries,
|
|
22
|
-
[dictionary.localId]: dictionary
|
|
23
|
-
}));
|
|
24
|
-
}
|
|
25
|
-
}), [setLocaleDictionaries]);
|
|
26
|
-
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DictionariesRecordStatesContext.Provider, {
|
|
27
|
-
value: stateValue,
|
|
28
|
-
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DictionariesRecordActionsContext.Provider, {
|
|
29
|
-
value: actionValue,
|
|
30
|
-
children
|
|
31
|
-
})
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
const useDictionariesRecordActions = () => (0, react.useContext)(DictionariesRecordActionsContext);
|
|
35
|
-
const useDictionariesRecord = () => {
|
|
36
|
-
const actionsContext = useDictionariesRecordActions();
|
|
37
|
-
const statesContext = (0, react.useContext)(DictionariesRecordStatesContext);
|
|
38
|
-
if (!statesContext) throw new Error("useDictionariesRecordStates must be used within a DictionariesRecordProvider");
|
|
39
|
-
return {
|
|
40
|
-
...statesContext,
|
|
41
|
-
...actionsContext
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
//#endregion
|
|
46
|
-
exports.DictionariesRecordProvider = DictionariesRecordProvider;
|
|
47
|
-
exports.useDictionariesRecord = useDictionariesRecord;
|
|
48
|
-
exports.useDictionariesRecordActions = useDictionariesRecordActions;
|
|
1
|
+
"use client";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`./_virtual/_rolldown/runtime.cjs`);const e=require(`./useCrossFrameState.cjs`);let t=require(`react`),n=require(`react/jsx-runtime`),r=require(`@intlayer/editor`);const i=(0,t.createContext)(void 0),a=(0,t.createContext)(void 0),o=({children:o})=>{let[s,c]=e.useCrossFrameState(r.MessageKey.INTLAYER_LOCALE_DICTIONARIES_CHANGED,void 0),l=(0,t.useMemo)(()=>({localeDictionaries:s??{}}),[s]),u=(0,t.useMemo)(()=>({setLocaleDictionaries:c,setLocaleDictionary:e=>{e.localId&&c(t=>({...t,[e.localId]:e}))}}),[c]);return(0,n.jsx)(i.Provider,{value:l,children:(0,n.jsx)(a.Provider,{value:u,children:o})})},s=()=>(0,t.useContext)(a),c=()=>{let e=s(),n=(0,t.useContext)(i);if(!n)throw Error(`useDictionariesRecordStates must be used within a DictionariesRecordProvider`);return{...n,...e}};exports.DictionariesRecordProvider=o,exports.useDictionariesRecord=c,exports.useDictionariesRecordActions=s;
|
|
49
2
|
//# sourceMappingURL=DictionariesRecordContext.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DictionariesRecordContext.cjs","names":["useCrossFrameState","MessageKey"],"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":"
|
|
1
|
+
{"version":3,"file":"DictionariesRecordContext.cjs","names":["useCrossFrameState","MessageKey"],"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":"4PAyBA,MAAM,GAAA,EAAA,EAAA,eAEJ,IAAA,GAAU,CACN,GAAA,EAAA,EAAA,eAEJ,IAAA,GAAU,CAEC,GAAqD,CAChE,cACI,CACJ,GAAM,CAAC,EAAoB,GACzBA,EAAAA,mBACEC,EAAAA,WAAW,qCACX,IAAA,GACD,CAEG,GAAA,EAAA,EAAA,cACG,CACL,mBAAoB,GAAsB,EAAE,CAC7C,EACD,CAAC,EAAmB,CACrB,CAEK,GAAA,EAAA,EAAA,cACG,CACL,wBACA,oBAAsB,GAA2B,CAC1C,EAAW,SAEhB,EAAuB,IAAkB,CACvC,GAAG,GACF,EAAW,SAA+B,EAC5C,EAAE,EAEN,EACD,CAAC,EAAsB,CACxB,CAED,OACE,EAAA,EAAA,KAAC,EAAgC,SAAA,CAAS,MAAO,YAC/C,EAAA,EAAA,KAAC,EAAiC,SAAA,CAAS,MAAO,EAC/C,YACyC,EACH,EAIlC,OAAA,EAAA,EAAA,YACA,EAAiC,CAEjC,MAA8B,CACzC,IAAM,EAAiB,GAA8B,CAC/C,GAAA,EAAA,EAAA,YAA2B,EAAgC,CAEjE,GAAI,CAAC,EACH,MAAU,MACR,+EACD,CAGH,MAAO,CAAE,GAAG,EAAe,GAAG,EAAgB"}
|
|
@@ -1,170 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
-
const require_useCrossFrameMessageListener = require('./useCrossFrameMessageListener.cjs');
|
|
6
|
-
const require_useCrossFrameState = require('./useCrossFrameState.cjs');
|
|
7
|
-
const require_DictionariesRecordContext = require('./DictionariesRecordContext.cjs');
|
|
8
|
-
const require_useEditorLocale = require('./useEditorLocale.cjs');
|
|
9
|
-
let react = require("react");
|
|
10
|
-
let react_jsx_runtime = require("react/jsx-runtime");
|
|
11
|
-
let _intlayer_editor = require("@intlayer/editor");
|
|
12
|
-
let _intlayer_core = require("@intlayer/core");
|
|
13
|
-
let _intlayer_types = require("@intlayer/types");
|
|
14
|
-
|
|
15
|
-
//#region src/EditedContentContext.tsx
|
|
16
|
-
const EditedContentStateContext = (0, react.createContext)(void 0);
|
|
17
|
-
const usePostEditedContentState = (onEventTriggered) => require_useCrossFrameMessageListener.useCrossFrameMessageListener(`${_intlayer_editor.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/post`, onEventTriggered);
|
|
18
|
-
const useGetEditedContentState = (onEventTriggered) => require_useCrossFrameMessageListener.useCrossFrameMessageListener(`${_intlayer_editor.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/get`, onEventTriggered);
|
|
19
|
-
const EditedContentActionsContext = (0, react.createContext)(void 0);
|
|
20
|
-
const resolveState = (state, prevState) => typeof state === "function" ? state(prevState) : state;
|
|
21
|
-
const EditedContentProvider = ({ children }) => {
|
|
22
|
-
const { localeDictionaries } = require_DictionariesRecordContext.useDictionariesRecord();
|
|
23
|
-
const currentLocale = require_useEditorLocale.useEditorLocale();
|
|
24
|
-
const [editedContent, setEditedContentState] = require_useCrossFrameState.useCrossFrameState(_intlayer_editor.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED);
|
|
25
|
-
const setEditedDictionary = (newValue) => {
|
|
26
|
-
let updatedDictionaries = resolveState(newValue);
|
|
27
|
-
setEditedContentState((prev) => {
|
|
28
|
-
if (!updatedDictionaries.localId) {
|
|
29
|
-
console.error("no localId", updatedDictionaries);
|
|
30
|
-
return prev;
|
|
31
|
-
}
|
|
32
|
-
updatedDictionaries = resolveState(newValue, prev?.[updatedDictionaries.localId]);
|
|
33
|
-
return {
|
|
34
|
-
...prev,
|
|
35
|
-
[updatedDictionaries.localId]: updatedDictionaries
|
|
36
|
-
};
|
|
37
|
-
});
|
|
38
|
-
return updatedDictionaries;
|
|
39
|
-
};
|
|
40
|
-
const setEditedContent = (localDictionaryId, newValue) => {
|
|
41
|
-
setEditedContentState((prev) => ({
|
|
42
|
-
...prev,
|
|
43
|
-
[localDictionaryId]: {
|
|
44
|
-
...prev?.[localDictionaryId],
|
|
45
|
-
content: newValue
|
|
46
|
-
}
|
|
47
|
-
}));
|
|
48
|
-
};
|
|
49
|
-
const addEditedContent = (localDictionaryId, newValue, keyPath = [], overwrite = true) => {
|
|
50
|
-
setEditedContentState((prev) => {
|
|
51
|
-
const originalContent = localeDictionaries[localDictionaryId]?.content;
|
|
52
|
-
const currentContent = structuredClone(prev?.[localDictionaryId]?.content ?? originalContent);
|
|
53
|
-
let newKeyPath = keyPath;
|
|
54
|
-
if (!overwrite) {
|
|
55
|
-
let index = 0;
|
|
56
|
-
const otherKeyPath = keyPath.slice(0, -1);
|
|
57
|
-
const lastKeyPath = keyPath[keyPath.length - 1];
|
|
58
|
-
let finalKey = lastKeyPath.key;
|
|
59
|
-
while (typeof (0, _intlayer_core.getContentNodeByKeyPath)(currentContent, newKeyPath) !== "undefined") {
|
|
60
|
-
index++;
|
|
61
|
-
finalKey = index === 0 ? lastKeyPath.key : `${lastKeyPath.key} (${index})`;
|
|
62
|
-
newKeyPath = [...otherKeyPath, {
|
|
63
|
-
...lastKeyPath,
|
|
64
|
-
key: finalKey
|
|
65
|
-
}];
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
const updatedContent = (0, _intlayer_core.editDictionaryByKeyPath)(currentContent, newKeyPath, newValue);
|
|
69
|
-
return {
|
|
70
|
-
...prev,
|
|
71
|
-
[localDictionaryId]: {
|
|
72
|
-
...prev?.[localDictionaryId],
|
|
73
|
-
content: updatedContent
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
});
|
|
77
|
-
};
|
|
78
|
-
const renameEditedContent = (localDictionaryId, newKey, keyPath = []) => {
|
|
79
|
-
setEditedContentState((prev) => {
|
|
80
|
-
const originalContent = localeDictionaries[localDictionaryId]?.content;
|
|
81
|
-
const contentWithNewField = (0, _intlayer_core.renameContentNodeByKeyPath)(structuredClone(prev?.[localDictionaryId]?.content ?? originalContent), newKey, keyPath);
|
|
82
|
-
return {
|
|
83
|
-
...prev,
|
|
84
|
-
[localDictionaryId]: {
|
|
85
|
-
...prev?.[localDictionaryId],
|
|
86
|
-
content: contentWithNewField
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
});
|
|
90
|
-
};
|
|
91
|
-
const removeEditedContent = (localDictionaryId, keyPath) => {
|
|
92
|
-
setEditedContentState((prev) => {
|
|
93
|
-
const originalContent = localeDictionaries[localDictionaryId]?.content;
|
|
94
|
-
const restoredContent = (0, _intlayer_core.editDictionaryByKeyPath)(structuredClone(prev?.[localDictionaryId]?.content ?? originalContent), keyPath, (0, _intlayer_core.getContentNodeByKeyPath)(originalContent, keyPath));
|
|
95
|
-
return {
|
|
96
|
-
...prev,
|
|
97
|
-
[localDictionaryId]: {
|
|
98
|
-
...prev?.[localDictionaryId],
|
|
99
|
-
content: restoredContent
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
});
|
|
103
|
-
};
|
|
104
|
-
const restoreEditedContent = (localDictionaryId) => {
|
|
105
|
-
setEditedContentState((prev) => {
|
|
106
|
-
const updated = { ...prev };
|
|
107
|
-
delete updated[localDictionaryId];
|
|
108
|
-
return updated;
|
|
109
|
-
});
|
|
110
|
-
};
|
|
111
|
-
const clearEditedDictionaryContent = (localDictionaryId) => {
|
|
112
|
-
setEditedContentState((prev) => {
|
|
113
|
-
return Object.entries(prev).reduce((acc, [key, value]) => {
|
|
114
|
-
if (key === localDictionaryId) return acc;
|
|
115
|
-
return {
|
|
116
|
-
...acc,
|
|
117
|
-
[key]: value
|
|
118
|
-
};
|
|
119
|
-
}, {});
|
|
120
|
-
});
|
|
121
|
-
};
|
|
122
|
-
const clearEditedContent = () => {
|
|
123
|
-
setEditedContentState({});
|
|
124
|
-
};
|
|
125
|
-
const getEditedContentValue = (localDictionaryIdOrKey, keyPath) => {
|
|
126
|
-
if (!editedContent) return void 0;
|
|
127
|
-
const filteredKeyPath = keyPath.filter((key) => key.type !== _intlayer_types.NodeType.Translation);
|
|
128
|
-
if (localDictionaryIdOrKey.includes(":local:") || localDictionaryIdOrKey.includes(":remote:")) return (0, _intlayer_core.getContentNodeByKeyPath)(editedContent?.[localDictionaryIdOrKey]?.content ?? {}, filteredKeyPath, currentLocale);
|
|
129
|
-
const filteredDictionariesLocalId = Object.keys(editedContent).filter((key) => key.startsWith(`${localDictionaryIdOrKey}:`));
|
|
130
|
-
for (const localDictionaryId of filteredDictionariesLocalId) {
|
|
131
|
-
const contentNode = (0, _intlayer_core.getContentNodeByKeyPath)(editedContent?.[localDictionaryId]?.content ?? {}, filteredKeyPath, currentLocale);
|
|
132
|
-
if (contentNode) return contentNode;
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditedContentStateContext.Provider, {
|
|
136
|
-
value: { editedContent },
|
|
137
|
-
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditedContentActionsContext.Provider, {
|
|
138
|
-
value: {
|
|
139
|
-
setEditedContentState,
|
|
140
|
-
setEditedDictionary,
|
|
141
|
-
setEditedContent,
|
|
142
|
-
addEditedContent,
|
|
143
|
-
renameEditedContent,
|
|
144
|
-
removeEditedContent,
|
|
145
|
-
restoreEditedContent,
|
|
146
|
-
clearEditedDictionaryContent,
|
|
147
|
-
clearEditedContent,
|
|
148
|
-
getEditedContentValue
|
|
149
|
-
},
|
|
150
|
-
children
|
|
151
|
-
})
|
|
152
|
-
});
|
|
153
|
-
};
|
|
154
|
-
const useEditedContentActions = () => (0, react.useContext)(EditedContentActionsContext);
|
|
155
|
-
const useEditedContent = () => {
|
|
156
|
-
const stateContext = (0, react.useContext)(EditedContentStateContext);
|
|
157
|
-
const actionContext = useEditedContentActions();
|
|
158
|
-
return {
|
|
159
|
-
...stateContext,
|
|
160
|
-
...actionContext
|
|
161
|
-
};
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
//#endregion
|
|
165
|
-
exports.EditedContentProvider = EditedContentProvider;
|
|
166
|
-
exports.useEditedContent = useEditedContent;
|
|
167
|
-
exports.useEditedContentActions = useEditedContentActions;
|
|
168
|
-
exports.useGetEditedContentState = useGetEditedContentState;
|
|
169
|
-
exports.usePostEditedContentState = usePostEditedContentState;
|
|
1
|
+
"use client";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`./_virtual/_rolldown/runtime.cjs`);const e=require(`./useCrossFrameMessageListener.cjs`),t=require(`./useCrossFrameState.cjs`),n=require(`./DictionariesRecordContext.cjs`),r=require(`./useEditorLocale.cjs`);let i=require(`react`),a=require(`react/jsx-runtime`),o=require(`@intlayer/editor`),s=require(`@intlayer/core/dictionaryManipulator`),c=require(`@intlayer/types`);const l=(0,i.createContext)(void 0),u=t=>e.useCrossFrameMessageListener(`${o.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/post`,t),d=t=>e.useCrossFrameMessageListener(`${o.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/get`,t),f=(0,i.createContext)(void 0),p=(e,t)=>typeof e==`function`?e(t):e,m=({children:e})=>{let{localeDictionaries:i}=n.useDictionariesRecord(),u=r.useEditorLocale(),[d,m]=t.useCrossFrameState(o.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED);return(0,a.jsx)(l.Provider,{value:{editedContent:d},children:(0,a.jsx)(f.Provider,{value:{setEditedContentState:m,setEditedDictionary:e=>{let t=p(e);return m(n=>t.localId?(t=p(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,c=structuredClone(a?.[e]?.content??o),l=n;if(!r){let e=0,t=n.slice(0,-1),r=n[n.length-1],i=r.key;for(;(0,s.getContentNodeByKeyPath)(c,l)!==void 0;)e++,i=e===0?r.key:`${r.key} (${e})`,l=[...t,{...r,key:i}]}let u=(0,s.editDictionaryByKeyPath)(c,l,t);return{...a,[e]:{...a?.[e],content:u}}})},renameEditedContent:(e,t,n=[])=>{m(r=>{let a=i[e]?.content,o=(0,s.renameContentNodeByKeyPath)(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=(0,s.editDictionaryByKeyPath)(structuredClone(n?.[e]?.content??r),t,(0,s.getContentNodeByKeyPath)(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(!d)return;let n=t.filter(e=>e.type!==c.NodeType.Translation);if(e.includes(`:local:`)||e.includes(`:remote:`))return(0,s.getContentNodeByKeyPath)(d?.[e]?.content??{},n,u);let r=Object.keys(d).filter(t=>t.startsWith(`${e}:`));for(let e of r){let t=(0,s.getContentNodeByKeyPath)(d?.[e]?.content??{},n,u);if(t)return t}}},children:e})})},h=()=>(0,i.useContext)(f),g=()=>{let e=(0,i.useContext)(l),t=h();return{...e,...t}};exports.EditedContentProvider=m,exports.useEditedContent=g,exports.useEditedContentActions=h,exports.useGetEditedContentState=d,exports.usePostEditedContentState=u;
|
|
170
2
|
//# sourceMappingURL=EditedContentContext.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EditedContentContext.cjs","names":["useCrossFrameMessageListener","MessageKey","useDictionariesRecord","useEditorLocale","useCrossFrameState","NodeType"],"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.cjs","names":["useCrossFrameMessageListener","MessageKey","useDictionariesRecord","useEditorLocale","useCrossFrameState","NodeType"],"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,34 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
-
const require_useCrossFrameMessageListener = require('./useCrossFrameMessageListener.cjs');
|
|
6
|
-
const require_useCrossFrameState = require('./useCrossFrameState.cjs');
|
|
7
|
-
let react = require("react");
|
|
8
|
-
let react_jsx_runtime = require("react/jsx-runtime");
|
|
9
|
-
let _intlayer_editor = require("@intlayer/editor");
|
|
10
|
-
|
|
11
|
-
//#region src/EditorEnabledContext.tsx
|
|
12
|
-
const EditorEnabledContext = (0, react.createContext)({ enabled: false });
|
|
13
|
-
const useEditorEnabledState = (options) => require_useCrossFrameState.useCrossFrameState(_intlayer_editor.MessageKey.INTLAYER_EDITOR_ENABLED, false, options);
|
|
14
|
-
const usePostEditorEnabledState = (onEventTriggered) => require_useCrossFrameMessageListener.useCrossFrameMessageListener(`${_intlayer_editor.MessageKey.INTLAYER_EDITOR_ENABLED}/post`, onEventTriggered);
|
|
15
|
-
const useGetEditorEnabledState = (onEventTriggered) => require_useCrossFrameMessageListener.useCrossFrameMessageListener(`${_intlayer_editor.MessageKey.INTLAYER_EDITOR_ENABLED}/get`, onEventTriggered);
|
|
16
|
-
const EditorEnabledProvider = ({ children }) => {
|
|
17
|
-
const [isEnabled] = useEditorEnabledState({
|
|
18
|
-
emit: false,
|
|
19
|
-
receive: true
|
|
20
|
-
});
|
|
21
|
-
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditorEnabledContext.Provider, {
|
|
22
|
-
value: { enabled: isEnabled },
|
|
23
|
-
children
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
const useEditorEnabled = () => (0, react.useContext)(EditorEnabledContext);
|
|
27
|
-
|
|
28
|
-
//#endregion
|
|
29
|
-
exports.EditorEnabledProvider = EditorEnabledProvider;
|
|
30
|
-
exports.useEditorEnabled = useEditorEnabled;
|
|
31
|
-
exports.useEditorEnabledState = useEditorEnabledState;
|
|
32
|
-
exports.useGetEditorEnabledState = useGetEditorEnabledState;
|
|
33
|
-
exports.usePostEditorEnabledState = usePostEditorEnabledState;
|
|
1
|
+
"use client";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`./_virtual/_rolldown/runtime.cjs`);const e=require(`./useCrossFrameMessageListener.cjs`),t=require(`./useCrossFrameState.cjs`);let n=require(`react`),r=require(`react/jsx-runtime`),i=require(`@intlayer/editor`);const a=(0,n.createContext)({enabled:!1}),o=e=>t.useCrossFrameState(i.MessageKey.INTLAYER_EDITOR_ENABLED,!1,e),s=t=>e.useCrossFrameMessageListener(`${i.MessageKey.INTLAYER_EDITOR_ENABLED}/post`,t),c=t=>e.useCrossFrameMessageListener(`${i.MessageKey.INTLAYER_EDITOR_ENABLED}/get`,t),l=({children:e})=>{let[t]=o({emit:!1,receive:!0});return(0,r.jsx)(a.Provider,{value:{enabled:t},children:e})},u=()=>(0,n.useContext)(a);exports.EditorEnabledProvider=l,exports.useEditorEnabled=u,exports.useEditorEnabledState=o,exports.useGetEditorEnabledState=c,exports.usePostEditorEnabledState=s;
|
|
34
2
|
//# sourceMappingURL=EditorEnabledContext.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EditorEnabledContext.cjs","names":["useCrossFrameState","MessageKey","useCrossFrameMessageListener"],"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":"
|
|
1
|
+
{"version":3,"file":"EditorEnabledContext.cjs","names":["useCrossFrameState","MessageKey","useCrossFrameMessageListener"],"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":"4SAmBA,MAAM,GAAA,EAAA,EAAA,eAA8D,CAClE,QAAS,GACV,CAAC,CAEW,EAAyB,GACpCA,EAAAA,mBAAmBC,EAAAA,WAAW,wBAAyB,GAAO,EAAQ,CAE3D,EACX,GAEAC,EAAAA,6BACE,GAAGD,EAAAA,WAAW,wBAAwB,OACtC,EACD,CAEU,EACX,GAEAC,EAAAA,6BACE,GAAGD,EAAAA,WAAW,wBAAwB,MACtC,EACD,CAEU,GAAgD,CAAE,cAAe,CAC5E,GAAM,CAAC,GAAa,EAAsB,CACxC,KAAM,GACN,QAAS,GACV,CAAC,CAEF,OACE,EAAA,EAAA,KAAC,EAAqB,SAAA,CAAS,MAAO,CAAE,QAAS,EAAW,CACzD,YAC6B,EAIvB,OAAA,EAAA,EAAA,YAAoC,EAAqB"}
|
|
@@ -1,68 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
-
const require_CommunicatorContext = require('./CommunicatorContext.cjs');
|
|
6
|
-
const require_ConfigurationContext = require('./ConfigurationContext.cjs');
|
|
7
|
-
const require_DictionariesRecordContext = require('./DictionariesRecordContext.cjs');
|
|
8
|
-
const require_EditedContentContext = require('./EditedContentContext.cjs');
|
|
9
|
-
const require_EditorEnabledContext = require('./EditorEnabledContext.cjs');
|
|
10
|
-
const require_FocusDictionaryContext = require('./FocusDictionaryContext.cjs');
|
|
11
|
-
let react = require("react");
|
|
12
|
-
let react_jsx_runtime = require("react/jsx-runtime");
|
|
13
|
-
|
|
14
|
-
//#region src/EditorProvider.tsx
|
|
15
|
-
/**
|
|
16
|
-
* This component add all the providers needed by the editor.
|
|
17
|
-
* It is used to wrap the application, or the editor to work together.
|
|
18
|
-
*/
|
|
19
|
-
const EditorProvidersWrapper = ({ children }) => {
|
|
20
|
-
const getEditedContentState = require_EditedContentContext.useGetEditedContentState();
|
|
21
|
-
(0, react.useEffect)(() => {
|
|
22
|
-
getEditedContentState();
|
|
23
|
-
}, []);
|
|
24
|
-
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_DictionariesRecordContext.DictionariesRecordProvider, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_EditedContentContext.EditedContentProvider, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_FocusDictionaryContext.FocusDictionaryProvider, { children }) }) });
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* This component check if the editor is enabled to render the editor providers.
|
|
28
|
-
*/
|
|
29
|
-
const EditorEnabledCheckRenderer = ({ children, fallback }) => {
|
|
30
|
-
const getEditorEnabled = require_EditorEnabledContext.useGetEditorEnabledState();
|
|
31
|
-
const { enabled } = require_EditorEnabledContext.useEditorEnabled();
|
|
32
|
-
(0, react.useEffect)(() => {
|
|
33
|
-
if (enabled) return;
|
|
34
|
-
getEditorEnabled();
|
|
35
|
-
}, [enabled]);
|
|
36
|
-
return enabled ? children : fallback;
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* This component is used to check if the editor is wrapping the application.
|
|
40
|
-
* It avoid to send window.postMessage to the application if the editor is not wrapping the application.
|
|
41
|
-
*/
|
|
42
|
-
const IframeCheckRenderer = ({ children, fallback }) => {
|
|
43
|
-
const [isInIframe, setIsInIframe] = (0, react.useState)(false);
|
|
44
|
-
(0, react.useEffect)(() => {
|
|
45
|
-
setIsInIframe(window.self !== window.top);
|
|
46
|
-
}, []);
|
|
47
|
-
return isInIframe ? children : fallback;
|
|
48
|
-
};
|
|
49
|
-
const EditorProvider = ({ children, configuration, ...props }) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_EditorEnabledContext.EditorEnabledProvider, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_ConfigurationContext.ConfigurationProvider, {
|
|
50
|
-
configuration,
|
|
51
|
-
children: props.mode === "editor" ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_CommunicatorContext.CommunicatorProvider, {
|
|
52
|
-
...props,
|
|
53
|
-
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditorProvidersWrapper, { children })
|
|
54
|
-
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(IframeCheckRenderer, {
|
|
55
|
-
fallback: children,
|
|
56
|
-
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_CommunicatorContext.CommunicatorProvider, {
|
|
57
|
-
...props,
|
|
58
|
-
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditorEnabledCheckRenderer, {
|
|
59
|
-
fallback: children,
|
|
60
|
-
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditorProvidersWrapper, { children })
|
|
61
|
-
})
|
|
62
|
-
})
|
|
63
|
-
})
|
|
64
|
-
}) });
|
|
65
|
-
|
|
66
|
-
//#endregion
|
|
67
|
-
exports.EditorProvider = EditorProvider;
|
|
1
|
+
"use client";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`./_virtual/_rolldown/runtime.cjs`);const e=require(`./CommunicatorContext.cjs`),t=require(`./ConfigurationContext.cjs`),n=require(`./DictionariesRecordContext.cjs`),r=require(`./EditedContentContext.cjs`),i=require(`./EditorEnabledContext.cjs`),a=require(`./FocusDictionaryContext.cjs`);let o=require(`react`),s=require(`react/jsx-runtime`);const c=({children:e})=>{let t=r.useGetEditedContentState();return(0,o.useEffect)(()=>{t()},[]),(0,s.jsx)(n.DictionariesRecordProvider,{children:(0,s.jsx)(r.EditedContentProvider,{children:(0,s.jsx)(a.FocusDictionaryProvider,{children:e})})})},l=({children:e,fallback:t})=>{let n=i.useGetEditorEnabledState(),{enabled:r}=i.useEditorEnabled();return(0,o.useEffect)(()=>{r||n()},[r]),r?e:t},u=({children:e,fallback:t})=>{let[n,r]=(0,o.useState)(!1);return(0,o.useEffect)(()=>{r(window.self!==window.top)},[]),n?e:t},d=({children:n,configuration:r,...a})=>(0,s.jsx)(i.EditorEnabledProvider,{children:(0,s.jsx)(t.ConfigurationProvider,{configuration:r,children:a.mode===`editor`?(0,s.jsx)(e.CommunicatorProvider,{...a,children:(0,s.jsx)(c,{children:n})}):(0,s.jsx)(u,{fallback:n,children:(0,s.jsx)(e.CommunicatorProvider,{...a,children:(0,s.jsx)(l,{fallback:n,children:(0,s.jsx)(c,{children:n})})})})})});exports.EditorProvider=d;
|
|
68
2
|
//# sourceMappingURL=EditorProvider.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EditorProvider.cjs","names":["useGetEditedContentState","DictionariesRecordProvider","EditedContentProvider","FocusDictionaryProvider","useGetEditorEnabledState","useEditorEnabled","EditorEnabledProvider","ConfigurationProvider","CommunicatorProvider"],"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":"
|
|
1
|
+
{"version":3,"file":"EditorProvider.cjs","names":["useGetEditedContentState","DictionariesRecordProvider","EditedContentProvider","FocusDictionaryProvider","useGetEditorEnabledState","useEditorEnabled","EditorEnabledProvider","ConfigurationProvider","CommunicatorProvider"],"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":"8aAiCA,MAAM,GAAiD,CAAE,cAAe,CACtE,IAAM,EAAwBA,EAAAA,0BAA0B,CAMxD,OAJA,EAAA,EAAA,eAAgB,CACd,GAAuB,EACtB,EAAE,CAAC,EAGJ,EAAA,EAAA,KAACC,EAAAA,2BAAAA,CAAAA,UACC,EAAA,EAAA,KAACC,EAAAA,sBAAAA,CAAAA,UACC,EAAA,EAAA,KAACC,EAAAA,wBAAAA,CAAyB,WAAA,CAAmC,CAAA,CACvC,CAAA,CACG,EAW3B,GAAoE,CACxE,WACA,cACI,CACJ,IAAM,EAAmBC,EAAAA,0BAA0B,CAE7C,CAAE,WAAYC,EAAAA,kBAAkB,CAStC,OAPA,EAAA,EAAA,eAAgB,CACV,GAGJ,GAAkB,EACjB,CAAC,EAAQ,CAAC,CAEN,EAAU,EAAW,GAOxB,GAA6D,CACjE,WACA,cACI,CACJ,GAAM,CAAC,EAAY,IAAA,EAAA,EAAA,UAA0B,GAAM,CAMnD,OAJA,EAAA,EAAA,eAAgB,CACd,EAAc,OAAO,OAAS,OAAO,IAAI,EACxC,EAAE,CAAC,CAEC,EAAa,EAAW,GAQpB,GAA8D,CACzE,WACA,gBACA,GAAG,MAEH,EAAA,EAAA,KAACC,EAAAA,sBAAAA,CAAAA,UACC,EAAA,EAAA,KAACC,EAAAA,sBAAAA,CAAqC,yBACnC,EAAM,OAAS,UACd,EAAA,EAAA,KAACC,EAAAA,qBAAAA,CAAqB,GAAI,YACxB,EAAA,EAAA,KAAC,EAAA,CAAwB,WAAA,CAAkC,EACtC,EAEvB,EAAA,EAAA,KAAC,EAAA,CAAoB,SAAU,YAC7B,EAAA,EAAA,KAACA,EAAAA,qBAAAA,CAAqB,GAAI,YACxB,EAAA,EAAA,KAAC,EAAA,CAA2B,SAAU,YACpC,EAAA,EAAA,KAAC,EAAA,CAAwB,WAAA,CAAkC,EAChC,EACR,EACH,EAEF,CAAA,CACF"}
|
|
@@ -1,56 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
5
|
-
const require_useCrossFrameState = require('./useCrossFrameState.cjs');
|
|
6
|
-
let react = require("react");
|
|
7
|
-
let react_jsx_runtime = require("react/jsx-runtime");
|
|
8
|
-
let _intlayer_editor = require("@intlayer/editor");
|
|
9
|
-
let _intlayer_types = require("@intlayer/types");
|
|
10
|
-
|
|
11
|
-
//#region src/FocusDictionaryContext.tsx
|
|
12
|
-
const FocusDictionaryStateContext = (0, react.createContext)(void 0);
|
|
13
|
-
const FocusDictionaryActionsContext = (0, react.createContext)(void 0);
|
|
14
|
-
const FocusDictionaryProvider = ({ children }) => {
|
|
15
|
-
const [focusedContent, setFocusedContent] = require_useCrossFrameState.useCrossFrameState(_intlayer_editor.MessageKey.INTLAYER_FOCUSED_CONTENT_CHANGED, null);
|
|
16
|
-
const setFocusedContentKeyPath = (keyPath) => {
|
|
17
|
-
setFocusedContent((prev) => {
|
|
18
|
-
if (!prev) return prev;
|
|
19
|
-
const filteredKeyPath = keyPath.filter((key) => key.type !== _intlayer_types.NodeType.Translation);
|
|
20
|
-
return {
|
|
21
|
-
...prev,
|
|
22
|
-
keyPath: filteredKeyPath
|
|
23
|
-
};
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FocusDictionaryStateContext.Provider, {
|
|
27
|
-
value: { focusedContent },
|
|
28
|
-
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FocusDictionaryActionsContext.Provider, {
|
|
29
|
-
value: {
|
|
30
|
-
setFocusedContent,
|
|
31
|
-
setFocusedContentKeyPath
|
|
32
|
-
},
|
|
33
|
-
children
|
|
34
|
-
})
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
const useFocusDictionaryActions = () => {
|
|
38
|
-
const context = (0, react.useContext)(FocusDictionaryActionsContext);
|
|
39
|
-
if (context === void 0) throw new Error("useFocusDictionaryActions must be used within a FocusDictionaryProvider");
|
|
40
|
-
return context;
|
|
41
|
-
};
|
|
42
|
-
const useFocusDictionary = () => {
|
|
43
|
-
const actionContext = useFocusDictionaryActions();
|
|
44
|
-
const stateContext = (0, react.useContext)(FocusDictionaryStateContext);
|
|
45
|
-
if (stateContext === void 0) throw new Error("useFocusDictionaryState must be used within a FocusDictionaryProvider");
|
|
46
|
-
return {
|
|
47
|
-
...stateContext,
|
|
48
|
-
...actionContext
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
//#endregion
|
|
53
|
-
exports.FocusDictionaryProvider = FocusDictionaryProvider;
|
|
54
|
-
exports.useFocusDictionary = useFocusDictionary;
|
|
55
|
-
exports.useFocusDictionaryActions = useFocusDictionaryActions;
|
|
1
|
+
"use client";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`./_virtual/_rolldown/runtime.cjs`);const e=require(`./useCrossFrameState.cjs`);let t=require(`react`),n=require(`react/jsx-runtime`),r=require(`@intlayer/editor`),i=require(`@intlayer/types`);const a=(0,t.createContext)(void 0),o=(0,t.createContext)(void 0),s=({children:t})=>{let[s,c]=e.useCrossFrameState(r.MessageKey.INTLAYER_FOCUSED_CONTENT_CHANGED,null);return(0,n.jsx)(a.Provider,{value:{focusedContent:s},children:(0,n.jsx)(o.Provider,{value:{setFocusedContent:c,setFocusedContentKeyPath:e=>{c(t=>{if(!t)return t;let n=e.filter(e=>e.type!==i.NodeType.Translation);return{...t,keyPath:n}})}},children:t})})},c=()=>{let e=(0,t.useContext)(o);if(e===void 0)throw Error(`useFocusDictionaryActions must be used within a FocusDictionaryProvider`);return e},l=()=>{let e=c(),n=(0,t.useContext)(a);if(n===void 0)throw Error(`useFocusDictionaryState must be used within a FocusDictionaryProvider`);return{...n,...e}};exports.FocusDictionaryProvider=s,exports.useFocusDictionary=l,exports.useFocusDictionaryActions=c;
|
|
56
2
|
//# sourceMappingURL=FocusDictionaryContext.cjs.map
|