@intlayer/editor-react 6.1.6 → 7.0.0-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/CommunicatorContext.cjs +43 -65
- package/dist/cjs/CommunicatorContext.cjs.map +1 -1
- package/dist/cjs/ConfigurationContext.cjs +26 -47
- package/dist/cjs/ConfigurationContext.cjs.map +1 -1
- package/dist/cjs/DictionariesRecordContext.cjs +48 -74
- package/dist/cjs/DictionariesRecordContext.cjs.map +1 -1
- package/dist/cjs/EditedContentContext.cjs +164 -225
- package/dist/cjs/EditedContentContext.cjs.map +1 -1
- package/dist/cjs/EditorEnabledContext.cjs +34 -59
- package/dist/cjs/EditorEnabledContext.cjs.map +1 -1
- package/dist/cjs/EditorProvider.cjs +65 -66
- package/dist/cjs/EditorProvider.cjs.map +1 -1
- package/dist/cjs/FocusDictionaryContext.cjs +51 -75
- package/dist/cjs/FocusDictionaryContext.cjs.map +1 -1
- package/dist/cjs/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/cjs/index.cjs +46 -44
- package/dist/cjs/useCrossFrameMessageListener.cjs +73 -50
- package/dist/cjs/useCrossFrameMessageListener.cjs.map +1 -1
- package/dist/cjs/useCrossFrameState.cjs +106 -73
- package/dist/cjs/useCrossFrameState.cjs.map +1 -1
- package/dist/cjs/useCrossURLPathState.cjs +67 -66
- package/dist/cjs/useCrossURLPathState.cjs.map +1 -1
- package/dist/cjs/useIframeClickInterceptor.cjs +24 -47
- package/dist/cjs/useIframeClickInterceptor.cjs.map +1 -1
- package/dist/esm/CommunicatorContext.mjs +34 -32
- package/dist/esm/CommunicatorContext.mjs.map +1 -1
- package/dist/esm/ConfigurationContext.mjs +19 -23
- package/dist/esm/ConfigurationContext.mjs.map +1 -1
- package/dist/esm/DictionariesRecordContext.mjs +37 -47
- package/dist/esm/DictionariesRecordContext.mjs.map +1 -1
- package/dist/esm/EditedContentContext.mjs +150 -201
- package/dist/esm/EditedContentContext.mjs.map +1 -1
- package/dist/esm/EditorEnabledContext.mjs +22 -32
- package/dist/esm/EditorEnabledContext.mjs.map +1 -1
- package/dist/esm/EditorProvider.mjs +60 -54
- package/dist/esm/EditorProvider.mjs.map +1 -1
- package/dist/esm/FocusDictionaryContext.mjs +41 -48
- package/dist/esm/FocusDictionaryContext.mjs.map +1 -1
- package/dist/esm/index.mjs +15 -13
- package/dist/esm/useCrossFrameMessageListener.mjs +69 -25
- package/dist/esm/useCrossFrameMessageListener.mjs.map +1 -1
- package/dist/esm/useCrossFrameState.mjs +102 -47
- package/dist/esm/useCrossFrameState.mjs.map +1 -1
- package/dist/esm/useCrossURLPathState.mjs +61 -41
- package/dist/esm/useCrossURLPathState.mjs.map +1 -1
- package/dist/esm/useIframeClickInterceptor.mjs +19 -21
- package/dist/esm/useIframeClickInterceptor.mjs.map +1 -1
- package/dist/types/CommunicatorContext.d.ts +12 -8
- package/dist/types/CommunicatorContext.d.ts.map +1 -1
- package/dist/types/ConfigurationContext.d.ts +12 -7
- package/dist/types/ConfigurationContext.d.ts.map +1 -1
- package/dist/types/DictionariesRecordContext.d.ts +15 -12
- package/dist/types/DictionariesRecordContext.d.ts.map +1 -1
- package/dist/types/EditedContentContext.d.ts +33 -30
- package/dist/types/EditedContentContext.d.ts.map +1 -1
- package/dist/types/EditorEnabledContext.d.ts +14 -9
- package/dist/types/EditorEnabledContext.d.ts.map +1 -1
- package/dist/types/EditorProvider.d.ts +10 -6
- package/dist/types/EditorProvider.d.ts.map +1 -1
- package/dist/types/FocusDictionaryContext.d.ts +18 -15
- package/dist/types/FocusDictionaryContext.d.ts.map +1 -1
- package/dist/types/index.d.ts +13 -13
- package/dist/types/useCrossFrameMessageListener.d.ts +7 -2
- package/dist/types/useCrossFrameMessageListener.d.ts.map +1 -1
- package/dist/types/useCrossFrameState.d.ts +10 -6
- package/dist/types/useCrossFrameState.d.ts.map +1 -1
- package/dist/types/useCrossURLPathState.d.ts +8 -3
- package/dist/types/useCrossURLPathState.d.ts.map +1 -1
- package/dist/types/useIframeClickInterceptor.d.ts +5 -2
- package/dist/types/useIframeClickInterceptor.d.ts.map +1 -1
- package/package.json +34 -40
- package/LICENSE +0 -202
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
|
@@ -1,56 +1,46 @@
|
|
|
1
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
import { useCrossFrameState } from "./useCrossFrameState.mjs";
|
|
5
|
+
import { createContext, useContext, useMemo } from "react";
|
|
2
6
|
import { jsx } from "react/jsx-runtime";
|
|
3
7
|
import { MessageKey } from "@intlayer/editor";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
useContext,
|
|
7
|
-
useMemo
|
|
8
|
-
} from "react";
|
|
9
|
-
import { useCrossFrameState } from "./useCrossFrameState.mjs";
|
|
8
|
+
|
|
9
|
+
//#region src/DictionariesRecordContext.tsx
|
|
10
10
|
const DictionariesRecordStatesContext = createContext(void 0);
|
|
11
11
|
const DictionariesRecordActionsContext = createContext(void 0);
|
|
12
|
-
const DictionariesRecordProvider = ({
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
[dictionary.localId]: dictionary
|
|
33
|
-
}));
|
|
34
|
-
}
|
|
35
|
-
}),
|
|
36
|
-
[setLocaleDictionaries]
|
|
37
|
-
);
|
|
38
|
-
return /* @__PURE__ */ jsx(DictionariesRecordStatesContext.Provider, { value: stateValue, children: /* @__PURE__ */ jsx(DictionariesRecordActionsContext.Provider, { value: actionValue, children }) });
|
|
12
|
+
const DictionariesRecordProvider = ({ children }) => {
|
|
13
|
+
const [localeDictionaries, setLocaleDictionaries] = useCrossFrameState(MessageKey.INTLAYER_LOCALE_DICTIONARIES_CHANGED, void 0);
|
|
14
|
+
const stateValue = useMemo(() => ({ localeDictionaries: localeDictionaries ?? {} }), [localeDictionaries]);
|
|
15
|
+
const actionValue = useMemo(() => ({
|
|
16
|
+
setLocaleDictionaries,
|
|
17
|
+
setLocaleDictionary: (dictionary) => {
|
|
18
|
+
if (!dictionary.localId) return;
|
|
19
|
+
setLocaleDictionaries((dictionaries) => ({
|
|
20
|
+
...dictionaries,
|
|
21
|
+
[dictionary.localId]: dictionary
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
}), [setLocaleDictionaries]);
|
|
25
|
+
return /* @__PURE__ */ jsx(DictionariesRecordStatesContext.Provider, {
|
|
26
|
+
value: stateValue,
|
|
27
|
+
children: /* @__PURE__ */ jsx(DictionariesRecordActionsContext.Provider, {
|
|
28
|
+
value: actionValue,
|
|
29
|
+
children
|
|
30
|
+
})
|
|
31
|
+
});
|
|
39
32
|
};
|
|
40
33
|
const useDictionariesRecordActions = () => useContext(DictionariesRecordActionsContext);
|
|
41
34
|
const useDictionariesRecord = () => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return { ...statesContext, ...actionsContext };
|
|
50
|
-
};
|
|
51
|
-
export {
|
|
52
|
-
DictionariesRecordProvider,
|
|
53
|
-
useDictionariesRecord,
|
|
54
|
-
useDictionariesRecordActions
|
|
35
|
+
const actionsContext = useDictionariesRecordActions();
|
|
36
|
+
const statesContext = useContext(DictionariesRecordStatesContext);
|
|
37
|
+
if (!statesContext) throw new Error("useDictionariesRecordStates must be used within a DictionariesRecordProvider");
|
|
38
|
+
return {
|
|
39
|
+
...statesContext,
|
|
40
|
+
...actionsContext
|
|
41
|
+
};
|
|
55
42
|
};
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
export { DictionariesRecordProvider, useDictionariesRecord, useDictionariesRecordActions };
|
|
56
46
|
//# sourceMappingURL=DictionariesRecordContext.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/DictionariesRecordContext.tsx"],"sourcesContent":["'use client';\n\nimport
|
|
1
|
+
{"version":3,"file":"DictionariesRecordContext.mjs","names":["DictionariesRecordProvider: FC<PropsWithChildren>"],"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,MAAaA,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,212 +1,161 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
editDictionaryByKeyPath,
|
|
5
|
-
getContentNodeByKeyPath,
|
|
6
|
-
renameContentNodeByKeyPath
|
|
7
|
-
} from "@intlayer/core";
|
|
8
|
-
import { MessageKey } from "@intlayer/editor";
|
|
9
|
-
import {
|
|
10
|
-
createContext,
|
|
11
|
-
useContext
|
|
12
|
-
} from "react";
|
|
13
|
-
import {
|
|
14
|
-
useDictionariesRecord
|
|
15
|
-
} from "./DictionariesRecordContext.mjs";
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
|
|
16
4
|
import { useCrossFrameMessageListener } from "./useCrossFrameMessageListener.mjs";
|
|
17
5
|
import { useCrossFrameState } from "./useCrossFrameState.mjs";
|
|
6
|
+
import { useDictionariesRecord } from "./DictionariesRecordContext.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
|
+
|
|
12
|
+
//#region src/EditedContentContext.tsx
|
|
18
13
|
const EditedContentStateContext = createContext(void 0);
|
|
19
|
-
const usePostEditedContentState = (onEventTriggered) => useCrossFrameMessageListener(
|
|
20
|
-
|
|
21
|
-
onEventTriggered
|
|
22
|
-
);
|
|
23
|
-
const useGetEditedContentState = (onEventTriggered) => useCrossFrameMessageListener(
|
|
24
|
-
`${MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/get`,
|
|
25
|
-
onEventTriggered
|
|
26
|
-
);
|
|
14
|
+
const usePostEditedContentState = (onEventTriggered) => useCrossFrameMessageListener(`${MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/post`, onEventTriggered);
|
|
15
|
+
const useGetEditedContentState = (onEventTriggered) => useCrossFrameMessageListener(`${MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/get`, onEventTriggered);
|
|
27
16
|
const EditedContentActionsContext = createContext(void 0);
|
|
28
17
|
const resolveState = (state, prevState) => typeof state === "function" ? state(prevState) : state;
|
|
29
18
|
const EditedContentProvider = ({ children }) => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const contentNode = getContentNodeByKeyPath(currentContent, keyPath);
|
|
160
|
-
return contentNode;
|
|
161
|
-
}
|
|
162
|
-
const filteredDictionariesLocalId = Object.keys(editedContent).filter(
|
|
163
|
-
(key) => key.startsWith(`${localDictionaryIdOrKey}:`)
|
|
164
|
-
);
|
|
165
|
-
for (const localDictionaryId of filteredDictionariesLocalId) {
|
|
166
|
-
const currentContent = editedContent?.[localDictionaryId]?.content ?? {};
|
|
167
|
-
const contentNode = getContentNodeByKeyPath(currentContent, keyPath);
|
|
168
|
-
if (contentNode) return contentNode;
|
|
169
|
-
}
|
|
170
|
-
return void 0;
|
|
171
|
-
};
|
|
172
|
-
return /* @__PURE__ */ jsx(
|
|
173
|
-
EditedContentStateContext.Provider,
|
|
174
|
-
{
|
|
175
|
-
value: {
|
|
176
|
-
editedContent
|
|
177
|
-
},
|
|
178
|
-
children: /* @__PURE__ */ jsx(
|
|
179
|
-
EditedContentActionsContext.Provider,
|
|
180
|
-
{
|
|
181
|
-
value: {
|
|
182
|
-
setEditedContentState,
|
|
183
|
-
setEditedDictionary,
|
|
184
|
-
setEditedContent,
|
|
185
|
-
addEditedContent,
|
|
186
|
-
renameEditedContent,
|
|
187
|
-
removeEditedContent,
|
|
188
|
-
restoreEditedContent,
|
|
189
|
-
clearEditedDictionaryContent,
|
|
190
|
-
clearEditedContent,
|
|
191
|
-
getEditedContentValue
|
|
192
|
-
},
|
|
193
|
-
children
|
|
194
|
-
}
|
|
195
|
-
)
|
|
196
|
-
}
|
|
197
|
-
);
|
|
19
|
+
const { localeDictionaries } = useDictionariesRecord();
|
|
20
|
+
const [editedContent, setEditedContentState] = useCrossFrameState(MessageKey.INTLAYER_EDITED_CONTENT_CHANGED);
|
|
21
|
+
const setEditedDictionary = (newValue) => {
|
|
22
|
+
let updatedDictionaries = resolveState(newValue);
|
|
23
|
+
setEditedContentState((prev) => {
|
|
24
|
+
if (!updatedDictionaries.localId) {
|
|
25
|
+
console.error("no localId", updatedDictionaries);
|
|
26
|
+
return prev;
|
|
27
|
+
}
|
|
28
|
+
updatedDictionaries = resolveState(newValue, prev?.[updatedDictionaries.localId]);
|
|
29
|
+
return {
|
|
30
|
+
...prev,
|
|
31
|
+
[updatedDictionaries.localId]: updatedDictionaries
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
return updatedDictionaries;
|
|
35
|
+
};
|
|
36
|
+
const setEditedContent = (localDictionaryId, newValue) => {
|
|
37
|
+
setEditedContentState((prev) => ({
|
|
38
|
+
...prev,
|
|
39
|
+
[localDictionaryId]: {
|
|
40
|
+
...prev?.[localDictionaryId],
|
|
41
|
+
content: newValue
|
|
42
|
+
}
|
|
43
|
+
}));
|
|
44
|
+
};
|
|
45
|
+
const addEditedContent = (localDictionaryId, newValue, keyPath = [], overwrite = true) => {
|
|
46
|
+
setEditedContentState((prev) => {
|
|
47
|
+
const originalContent = localeDictionaries[localDictionaryId]?.content;
|
|
48
|
+
const currentContent = structuredClone(prev?.[localDictionaryId]?.content ?? originalContent);
|
|
49
|
+
let newKeyPath = keyPath;
|
|
50
|
+
if (!overwrite) {
|
|
51
|
+
let index = 0;
|
|
52
|
+
const otherKeyPath = keyPath.slice(0, -1);
|
|
53
|
+
const lastKeyPath = keyPath[keyPath.length - 1];
|
|
54
|
+
let finalKey = lastKeyPath.key;
|
|
55
|
+
while (typeof getContentNodeByKeyPath(currentContent, newKeyPath) !== "undefined") {
|
|
56
|
+
index++;
|
|
57
|
+
finalKey = index === 0 ? lastKeyPath.key : `${lastKeyPath.key} (${index})`;
|
|
58
|
+
newKeyPath = [...otherKeyPath, {
|
|
59
|
+
...lastKeyPath,
|
|
60
|
+
key: finalKey
|
|
61
|
+
}];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const updatedContent = editDictionaryByKeyPath(currentContent, newKeyPath, newValue);
|
|
65
|
+
return {
|
|
66
|
+
...prev,
|
|
67
|
+
[localDictionaryId]: {
|
|
68
|
+
...prev?.[localDictionaryId],
|
|
69
|
+
content: updatedContent
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
const renameEditedContent = (localDictionaryId, newKey, keyPath = []) => {
|
|
75
|
+
setEditedContentState((prev) => {
|
|
76
|
+
const originalContent = localeDictionaries[localDictionaryId]?.content;
|
|
77
|
+
const contentWithNewField = renameContentNodeByKeyPath(structuredClone(prev?.[localDictionaryId]?.content ?? originalContent), newKey, keyPath);
|
|
78
|
+
return {
|
|
79
|
+
...prev,
|
|
80
|
+
[localDictionaryId]: {
|
|
81
|
+
...prev?.[localDictionaryId],
|
|
82
|
+
content: contentWithNewField
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
const removeEditedContent = (localDictionaryId, keyPath) => {
|
|
88
|
+
setEditedContentState((prev) => {
|
|
89
|
+
const originalContent = localeDictionaries[localDictionaryId]?.content;
|
|
90
|
+
const restoredContent = editDictionaryByKeyPath(structuredClone(prev?.[localDictionaryId]?.content ?? originalContent), keyPath, getContentNodeByKeyPath(originalContent, keyPath));
|
|
91
|
+
return {
|
|
92
|
+
...prev,
|
|
93
|
+
[localDictionaryId]: {
|
|
94
|
+
...prev?.[localDictionaryId],
|
|
95
|
+
content: restoredContent
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
const restoreEditedContent = (localDictionaryId) => {
|
|
101
|
+
setEditedContentState((prev) => {
|
|
102
|
+
const updated = { ...prev };
|
|
103
|
+
delete updated[localDictionaryId];
|
|
104
|
+
return updated;
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
const clearEditedDictionaryContent = (localDictionaryId) => {
|
|
108
|
+
setEditedContentState((prev) => {
|
|
109
|
+
return Object.entries(prev).reduce((acc, [key, value]) => {
|
|
110
|
+
if (key === localDictionaryId) return acc;
|
|
111
|
+
return {
|
|
112
|
+
...acc,
|
|
113
|
+
[key]: value
|
|
114
|
+
};
|
|
115
|
+
}, {});
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
const clearEditedContent = () => {
|
|
119
|
+
setEditedContentState({});
|
|
120
|
+
};
|
|
121
|
+
const getEditedContentValue = (localDictionaryIdOrKey, keyPath) => {
|
|
122
|
+
if (!editedContent) return void 0;
|
|
123
|
+
if (localDictionaryIdOrKey.includes(":local:") || localDictionaryIdOrKey.includes(":remote:")) return getContentNodeByKeyPath(editedContent?.[localDictionaryIdOrKey]?.content ?? {}, keyPath);
|
|
124
|
+
const filteredDictionariesLocalId = Object.keys(editedContent).filter((key) => key.startsWith(`${localDictionaryIdOrKey}:`));
|
|
125
|
+
for (const localDictionaryId of filteredDictionariesLocalId) {
|
|
126
|
+
const contentNode = getContentNodeByKeyPath(editedContent?.[localDictionaryId]?.content ?? {}, keyPath);
|
|
127
|
+
if (contentNode) return contentNode;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
return /* @__PURE__ */ jsx(EditedContentStateContext.Provider, {
|
|
131
|
+
value: { editedContent },
|
|
132
|
+
children: /* @__PURE__ */ jsx(EditedContentActionsContext.Provider, {
|
|
133
|
+
value: {
|
|
134
|
+
setEditedContentState,
|
|
135
|
+
setEditedDictionary,
|
|
136
|
+
setEditedContent,
|
|
137
|
+
addEditedContent,
|
|
138
|
+
renameEditedContent,
|
|
139
|
+
removeEditedContent,
|
|
140
|
+
restoreEditedContent,
|
|
141
|
+
clearEditedDictionaryContent,
|
|
142
|
+
clearEditedContent,
|
|
143
|
+
getEditedContentValue
|
|
144
|
+
},
|
|
145
|
+
children
|
|
146
|
+
})
|
|
147
|
+
});
|
|
198
148
|
};
|
|
199
149
|
const useEditedContentActions = () => useContext(EditedContentActionsContext);
|
|
200
150
|
const useEditedContent = () => {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
useEditedContent,
|
|
208
|
-
useEditedContentActions,
|
|
209
|
-
useGetEditedContentState,
|
|
210
|
-
usePostEditedContentState
|
|
151
|
+
const stateContext = useContext(EditedContentStateContext);
|
|
152
|
+
const actionContext = useEditedContentActions();
|
|
153
|
+
return {
|
|
154
|
+
...stateContext,
|
|
155
|
+
...actionContext
|
|
156
|
+
};
|
|
211
157
|
};
|
|
158
|
+
|
|
159
|
+
//#endregion
|
|
160
|
+
export { EditedContentProvider, useEditedContent, useEditedContentActions, useGetEditedContentState, usePostEditedContentState };
|
|
212
161
|
//# sourceMappingURL=EditedContentContext.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/EditedContentContext.tsx"],"sourcesContent":["'use client';\n\nimport {\n editDictionaryByKeyPath,\n getContentNodeByKeyPath,\n renameContentNodeByKeyPath,\n type ContentNode,\n type Dictionary,\n type KeyPath,\n type LocalDictionaryId,\n} from '@intlayer/core';\nimport { MessageKey } from '@intlayer/editor';\nimport {\n createContext,\n useContext,\n type Dispatch,\n type FC,\n type PropsWithChildren,\n type SetStateAction,\n} from 'react';\nimport {\n useDictionariesRecord,\n type DictionaryContent,\n} from './DictionariesRecordContext';\nimport { useCrossFrameMessageListener } from './useCrossFrameMessageListener';\nimport { useCrossFrameState } from './useCrossFrameState';\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 dictionaryKey: string,\n keyPath: KeyPath[],\n localDictionaryId?: LocalDictionaryId\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\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 updatedDictionaries = resolveState(\n newValue,\n prev?.[updatedDictionaries.key]\n );\n\n return {\n ...prev,\n [updatedDictionaries.key]: 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 const otherKeyPath = keyPath.slice(0, -1);\n const lastKeyPath: KeyPath = keyPath[keyPath.length - 1];\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 | string,\n keyPath: KeyPath[]\n ): ContentNode | undefined => {\n if (!editedContent) return undefined;\n\n const isDictionaryId =\n localDictionaryIdOrKey.includes(':local:') ||\n localDictionaryIdOrKey.includes(':remote:');\n\n if (isDictionaryId) {\n const currentContent =\n editedContent?.[localDictionaryIdOrKey]?.content ?? {};\n\n const contentNode = getContentNodeByKeyPath(currentContent, keyPath);\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 = editedContent?.[localDictionaryId]?.content ?? {};\n const contentNode = getContentNodeByKeyPath(currentContent, keyPath);\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":";AAqTM;AAnTN;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAKK;AACP,SAAS,kBAAkB;AAC3B;AAAA,EACE;AAAA,EACA;AAAA,OAKK;AACP;AAAA,EACE;AAAA,OAEK;AACP,SAAS,oCAAoC;AAC7C,SAAS,0BAA0B;AAMnC,MAAM,4BAA4B,cAEhC,MAAS;AAEJ,MAAM,4BAA4B,CACvC,qBAEA;AAAA,EACE,GAAG,WAAW,+BAA+B;AAAA,EAC7C;AACF;AAEK,MAAM,2BAA2B,CACtC,qBAEA;AAAA,EACE,GAAG,WAAW,+BAA+B;AAAA,EAC7C;AACF;AAkCF,MAAM,8BAA8B,cAElC,MAAS;AAEX,MAAM,eAAe,CAAK,OAA2B,cACnD,OAAO,UAAU,aACZ,MAA+B,SAAS,IACxC;AAEA,MAAM,wBAA+C,CAAC,EAAE,SAAS,MAAM;AAC5E,QAAM,EAAE,mBAAmB,IAA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C;AAAA;AAAA,MACH;AAAA;AAAA,EACF;AAEJ;AAEO,MAAM,0BAA0B,MACrC,WAAW,2BAA2B;AAEjC,MAAM,mBAAmB,MAAM;AACpC,QAAM,eAAe,WAAW,yBAAyB;AACzD,QAAM,gBAAgB,wBAAwB;AAE9C,SAAO,EAAE,GAAG,cAAc,GAAG,cAAc;AAC7C;","names":[]}
|
|
1
|
+
{"version":3,"file":"EditedContentContext.mjs","names":["EditedContentProvider: FC<PropsWithChildren>","setEditedDictionary: Dispatch<SetStateAction<Dictionary>>","updatedDictionaries: Dictionary","lastKeyPath: KeyPath"],"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 type {\n ContentNode,\n Dictionary,\n KeyPath,\n LocalDictionaryId,\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';\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\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 const otherKeyPath = keyPath.slice(0, -1);\n const lastKeyPath: KeyPath = keyPath[keyPath.length - 1];\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 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(currentContent, keyPath);\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(currentContent, keyPath);\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":";;;;;;;;;;;;AAiCA,MAAM,4BAA4B,cAEhC,OAAU;AAEZ,MAAa,6BACX,qBAEA,6BACE,GAAG,WAAW,gCAAgC,QAC9C,iBACD;AAEH,MAAa,4BACX,qBAEA,6BACE,GAAG,WAAW,gCAAgC,OAC9C,iBACD;AAiCH,MAAM,8BAA8B,cAElC,OAAU;AAEZ,MAAM,gBAAoB,OAA2B,cACnD,OAAO,UAAU,aACZ,MAA+B,UAAU,GACzC;AAEP,MAAaA,yBAAgD,EAAE,eAAe;CAC5E,MAAM,EAAE,uBAAuB,uBAAuB;CAEtD,MAAM,CAAC,eAAe,yBACpB,mBACE,WAAW,gCACZ;CAEH,MAAMC,uBACJ,aACG;EACH,IAAIC,sBAAkC,aAAa,SAAS;AAE5D,yBAAuB,SAAS;AAC9B,OAAI,CAAC,oBAAoB,SAAS;AAChC,YAAQ,MAAM,cAAc,oBAAoB;AAEhD,WAAO;;AAGT,yBAAsB,aACpB,UACA,OAAO,oBAAoB,SAC5B;AAED,UAAO;IACL,GAAG;KACF,oBAAoB,UAA+B;IACrD;IACD;AAEF,SAAO;;CAGT,MAAM,oBACJ,mBACA,aACG;AACH,yBAAuB,UAAU;GAC/B,GAAG;IACF,oBAAoB;IACnB,GAAG,OAAO;IACV,SAAS;IACV;GACF,E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essageListener.mjs";
|
|
5
|
+
import { useCrossFrameState } from "./useCrossFrameState.mjs";
|
|
6
|
+
import { createContext, useContext } from "react";
|
|
2
7
|
import { jsx } from "react/jsx-runtime";
|
|
3
8
|
import { MessageKey } from "@intlayer/editor";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from "react";
|
|
8
|
-
import { useCrossFrameMessageListener } from "./useCrossFrameMessageListener.mjs";
|
|
9
|
-
import {
|
|
10
|
-
useCrossFrameState
|
|
11
|
-
} from "./useCrossFrameState.mjs";
|
|
12
|
-
const EditorEnabledContext = createContext({
|
|
13
|
-
enabled: false
|
|
14
|
-
});
|
|
9
|
+
|
|
10
|
+
//#region src/EditorEnabledContext.tsx
|
|
11
|
+
const EditorEnabledContext = createContext({ enabled: false });
|
|
15
12
|
const useEditorEnabledState = (options) => useCrossFrameState(MessageKey.INTLAYER_EDITOR_ENABLED, false, options);
|
|
16
|
-
const usePostEditorEnabledState = (onEventTriggered) => useCrossFrameMessageListener(
|
|
17
|
-
|
|
18
|
-
onEventTriggered
|
|
19
|
-
);
|
|
20
|
-
const useGetEditorEnabledState = (onEventTriggered) => useCrossFrameMessageListener(
|
|
21
|
-
`${MessageKey.INTLAYER_EDITOR_ENABLED}/get`,
|
|
22
|
-
onEventTriggered
|
|
23
|
-
);
|
|
13
|
+
const usePostEditorEnabledState = (onEventTriggered) => useCrossFrameMessageListener(`${MessageKey.INTLAYER_EDITOR_ENABLED}/post`, onEventTriggered);
|
|
14
|
+
const useGetEditorEnabledState = (onEventTriggered) => useCrossFrameMessageListener(`${MessageKey.INTLAYER_EDITOR_ENABLED}/get`, onEventTriggered);
|
|
24
15
|
const EditorEnabledProvider = ({ children }) => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
16
|
+
const [isEnabled] = useEditorEnabledState({
|
|
17
|
+
emit: false,
|
|
18
|
+
receive: true
|
|
19
|
+
});
|
|
20
|
+
return /* @__PURE__ */ jsx(EditorEnabledContext.Provider, {
|
|
21
|
+
value: { enabled: isEnabled },
|
|
22
|
+
children
|
|
23
|
+
});
|
|
30
24
|
};
|
|
31
25
|
const useEditorEnabled = () => useContext(EditorEnabledContext);
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
useEditorEnabledState,
|
|
36
|
-
useGetEditorEnabledState,
|
|
37
|
-
usePostEditorEnabledState
|
|
38
|
-
};
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
export { EditorEnabledProvider, useEditorEnabled, useEditorEnabledState, useGetEditorEnabledState, usePostEditorEnabledState };
|
|
39
29
|
//# sourceMappingURL=EditorEnabledContext.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/EditorEnabledContext.tsx"],"sourcesContent":["'use client';\n\nimport { MessageKey } from '@intlayer/editor';\nimport {\n createContext,\n
|
|
1
|
+
{"version":3,"file":"EditorEnabledContext.mjs","names":["EditorEnabledProvider: FC<PropsWithChildren>"],"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,MAAaA,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"}
|