@intlayer/editor-react 6.1.6-canary.0 → 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.
Files changed (75) hide show
  1. package/dist/cjs/CommunicatorContext.cjs +43 -65
  2. package/dist/cjs/CommunicatorContext.cjs.map +1 -1
  3. package/dist/cjs/ConfigurationContext.cjs +26 -47
  4. package/dist/cjs/ConfigurationContext.cjs.map +1 -1
  5. package/dist/cjs/DictionariesRecordContext.cjs +48 -74
  6. package/dist/cjs/DictionariesRecordContext.cjs.map +1 -1
  7. package/dist/cjs/EditedContentContext.cjs +164 -225
  8. package/dist/cjs/EditedContentContext.cjs.map +1 -1
  9. package/dist/cjs/EditorEnabledContext.cjs +34 -59
  10. package/dist/cjs/EditorEnabledContext.cjs.map +1 -1
  11. package/dist/cjs/EditorProvider.cjs +65 -66
  12. package/dist/cjs/EditorProvider.cjs.map +1 -1
  13. package/dist/cjs/FocusDictionaryContext.cjs +51 -75
  14. package/dist/cjs/FocusDictionaryContext.cjs.map +1 -1
  15. package/dist/cjs/_virtual/rolldown_runtime.cjs +25 -0
  16. package/dist/cjs/index.cjs +46 -44
  17. package/dist/cjs/useCrossFrameMessageListener.cjs +73 -50
  18. package/dist/cjs/useCrossFrameMessageListener.cjs.map +1 -1
  19. package/dist/cjs/useCrossFrameState.cjs +106 -73
  20. package/dist/cjs/useCrossFrameState.cjs.map +1 -1
  21. package/dist/cjs/useCrossURLPathState.cjs +67 -66
  22. package/dist/cjs/useCrossURLPathState.cjs.map +1 -1
  23. package/dist/cjs/useIframeClickInterceptor.cjs +24 -47
  24. package/dist/cjs/useIframeClickInterceptor.cjs.map +1 -1
  25. package/dist/esm/CommunicatorContext.mjs +34 -32
  26. package/dist/esm/CommunicatorContext.mjs.map +1 -1
  27. package/dist/esm/ConfigurationContext.mjs +19 -23
  28. package/dist/esm/ConfigurationContext.mjs.map +1 -1
  29. package/dist/esm/DictionariesRecordContext.mjs +37 -47
  30. package/dist/esm/DictionariesRecordContext.mjs.map +1 -1
  31. package/dist/esm/EditedContentContext.mjs +150 -201
  32. package/dist/esm/EditedContentContext.mjs.map +1 -1
  33. package/dist/esm/EditorEnabledContext.mjs +22 -32
  34. package/dist/esm/EditorEnabledContext.mjs.map +1 -1
  35. package/dist/esm/EditorProvider.mjs +60 -54
  36. package/dist/esm/EditorProvider.mjs.map +1 -1
  37. package/dist/esm/FocusDictionaryContext.mjs +41 -48
  38. package/dist/esm/FocusDictionaryContext.mjs.map +1 -1
  39. package/dist/esm/index.mjs +15 -13
  40. package/dist/esm/useCrossFrameMessageListener.mjs +69 -25
  41. package/dist/esm/useCrossFrameMessageListener.mjs.map +1 -1
  42. package/dist/esm/useCrossFrameState.mjs +102 -47
  43. package/dist/esm/useCrossFrameState.mjs.map +1 -1
  44. package/dist/esm/useCrossURLPathState.mjs +61 -41
  45. package/dist/esm/useCrossURLPathState.mjs.map +1 -1
  46. package/dist/esm/useIframeClickInterceptor.mjs +19 -21
  47. package/dist/esm/useIframeClickInterceptor.mjs.map +1 -1
  48. package/dist/types/CommunicatorContext.d.ts +12 -8
  49. package/dist/types/CommunicatorContext.d.ts.map +1 -1
  50. package/dist/types/ConfigurationContext.d.ts +12 -7
  51. package/dist/types/ConfigurationContext.d.ts.map +1 -1
  52. package/dist/types/DictionariesRecordContext.d.ts +15 -12
  53. package/dist/types/DictionariesRecordContext.d.ts.map +1 -1
  54. package/dist/types/EditedContentContext.d.ts +33 -30
  55. package/dist/types/EditedContentContext.d.ts.map +1 -1
  56. package/dist/types/EditorEnabledContext.d.ts +14 -9
  57. package/dist/types/EditorEnabledContext.d.ts.map +1 -1
  58. package/dist/types/EditorProvider.d.ts +10 -6
  59. package/dist/types/EditorProvider.d.ts.map +1 -1
  60. package/dist/types/FocusDictionaryContext.d.ts +18 -15
  61. package/dist/types/FocusDictionaryContext.d.ts.map +1 -1
  62. package/dist/types/index.d.ts +13 -13
  63. package/dist/types/useCrossFrameMessageListener.d.ts +7 -2
  64. package/dist/types/useCrossFrameMessageListener.d.ts.map +1 -1
  65. package/dist/types/useCrossFrameState.d.ts +10 -6
  66. package/dist/types/useCrossFrameState.d.ts.map +1 -1
  67. package/dist/types/useCrossURLPathState.d.ts +8 -3
  68. package/dist/types/useCrossURLPathState.d.ts.map +1 -1
  69. package/dist/types/useIframeClickInterceptor.d.ts +5 -2
  70. package/dist/types/useIframeClickInterceptor.d.ts.map +1 -1
  71. package/package.json +34 -40
  72. package/LICENSE +0 -202
  73. package/dist/cjs/index.cjs.map +0 -1
  74. package/dist/esm/index.mjs.map +0 -1
  75. package/dist/types/index.d.ts.map +0 -1
@@ -1,231 +1,170 @@
1
- "use strict";
2
- "use client";
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
- var EditedContentContext_exports = {};
21
- __export(EditedContentContext_exports, {
22
- EditedContentProvider: () => EditedContentProvider,
23
- useEditedContent: () => useEditedContent,
24
- useEditedContentActions: () => useEditedContentActions,
25
- useGetEditedContentState: () => useGetEditedContentState,
26
- usePostEditedContentState: () => usePostEditedContentState
27
- });
28
- module.exports = __toCommonJS(EditedContentContext_exports);
29
- var import_jsx_runtime = require("react/jsx-runtime");
30
- var import_core = require("@intlayer/core");
31
- var import_editor = require("@intlayer/editor");
32
- var import_react = require("react");
33
- var import_DictionariesRecordContext = require('./DictionariesRecordContext.cjs');
34
- var import_useCrossFrameMessageListener = require('./useCrossFrameMessageListener.cjs');
35
- var import_useCrossFrameState = require('./useCrossFrameState.cjs');
36
- const EditedContentStateContext = (0, import_react.createContext)(void 0);
37
- const usePostEditedContentState = (onEventTriggered) => (0, import_useCrossFrameMessageListener.useCrossFrameMessageListener)(
38
- `${import_editor.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/post`,
39
- onEventTriggered
40
- );
41
- const useGetEditedContentState = (onEventTriggered) => (0, import_useCrossFrameMessageListener.useCrossFrameMessageListener)(
42
- `${import_editor.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/get`,
43
- onEventTriggered
44
- );
45
- const EditedContentActionsContext = (0, import_react.createContext)(void 0);
1
+ 'use client';
2
+
3
+
4
+ const require_rolldown_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
+ let react = require("react");
9
+ react = require_rolldown_runtime.__toESM(react);
10
+ let react_jsx_runtime = require("react/jsx-runtime");
11
+ react_jsx_runtime = require_rolldown_runtime.__toESM(react_jsx_runtime);
12
+ let __intlayer_editor = require("@intlayer/editor");
13
+ __intlayer_editor = require_rolldown_runtime.__toESM(__intlayer_editor);
14
+ let __intlayer_core = require("@intlayer/core");
15
+ __intlayer_core = require_rolldown_runtime.__toESM(__intlayer_core);
16
+
17
+ //#region src/EditedContentContext.tsx
18
+ const EditedContentStateContext = (0, react.createContext)(void 0);
19
+ const usePostEditedContentState = (onEventTriggered) => require_useCrossFrameMessageListener.useCrossFrameMessageListener(`${__intlayer_editor.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/post`, onEventTriggered);
20
+ const useGetEditedContentState = (onEventTriggered) => require_useCrossFrameMessageListener.useCrossFrameMessageListener(`${__intlayer_editor.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/get`, onEventTriggered);
21
+ const EditedContentActionsContext = (0, react.createContext)(void 0);
46
22
  const resolveState = (state, prevState) => typeof state === "function" ? state(prevState) : state;
47
23
  const EditedContentProvider = ({ children }) => {
48
- const { localeDictionaries } = (0, import_DictionariesRecordContext.useDictionariesRecord)();
49
- const [editedContent, setEditedContentState] = (0, import_useCrossFrameState.useCrossFrameState)(
50
- import_editor.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED
51
- );
52
- const setEditedDictionary = (newValue) => {
53
- let updatedDictionaries = resolveState(newValue);
54
- setEditedContentState((prev) => {
55
- updatedDictionaries = resolveState(
56
- newValue,
57
- prev?.[updatedDictionaries.key]
58
- );
59
- return {
60
- ...prev,
61
- [updatedDictionaries.key]: updatedDictionaries
62
- };
63
- });
64
- return updatedDictionaries;
65
- };
66
- const setEditedContent = (localDictionaryId, newValue) => {
67
- setEditedContentState((prev) => ({
68
- ...prev,
69
- [localDictionaryId]: {
70
- ...prev?.[localDictionaryId],
71
- content: newValue
72
- }
73
- }));
74
- };
75
- const addEditedContent = (localDictionaryId, newValue, keyPath = [], overwrite = true) => {
76
- setEditedContentState((prev) => {
77
- const originalContent = localeDictionaries[localDictionaryId]?.content;
78
- const currentContent = structuredClone(
79
- prev?.[localDictionaryId]?.content ?? originalContent
80
- );
81
- let newKeyPath = keyPath;
82
- if (!overwrite) {
83
- let index = 0;
84
- const otherKeyPath = keyPath.slice(0, -1);
85
- const lastKeyPath = keyPath[keyPath.length - 1];
86
- let finalKey = lastKeyPath.key;
87
- while (typeof (0, import_core.getContentNodeByKeyPath)(currentContent, newKeyPath) !== "undefined") {
88
- index++;
89
- finalKey = index === 0 ? lastKeyPath.key : `${lastKeyPath.key} (${index})`;
90
- newKeyPath = [
91
- ...otherKeyPath,
92
- { ...lastKeyPath, key: finalKey }
93
- ];
94
- }
95
- }
96
- const updatedContent = (0, import_core.editDictionaryByKeyPath)(
97
- currentContent,
98
- newKeyPath,
99
- newValue
100
- );
101
- return {
102
- ...prev,
103
- [localDictionaryId]: {
104
- ...prev?.[localDictionaryId],
105
- content: updatedContent
106
- }
107
- };
108
- });
109
- };
110
- const renameEditedContent = (localDictionaryId, newKey, keyPath = []) => {
111
- setEditedContentState((prev) => {
112
- const originalContent = localeDictionaries[localDictionaryId]?.content;
113
- const currentContent = structuredClone(
114
- prev?.[localDictionaryId]?.content ?? originalContent
115
- );
116
- const contentWithNewField = (0, import_core.renameContentNodeByKeyPath)(
117
- currentContent,
118
- newKey,
119
- keyPath
120
- );
121
- return {
122
- ...prev,
123
- [localDictionaryId]: {
124
- ...prev?.[localDictionaryId],
125
- content: contentWithNewField
126
- }
127
- };
128
- });
129
- };
130
- const removeEditedContent = (localDictionaryId, keyPath) => {
131
- setEditedContentState((prev) => {
132
- const originalContent = localeDictionaries[localDictionaryId]?.content;
133
- const currentContent = structuredClone(
134
- prev?.[localDictionaryId]?.content ?? originalContent
135
- );
136
- const initialContent = (0, import_core.getContentNodeByKeyPath)(originalContent, keyPath);
137
- const restoredContent = (0, import_core.editDictionaryByKeyPath)(
138
- currentContent,
139
- keyPath,
140
- initialContent
141
- );
142
- return {
143
- ...prev,
144
- [localDictionaryId]: {
145
- ...prev?.[localDictionaryId],
146
- content: restoredContent
147
- }
148
- };
149
- });
150
- };
151
- const restoreEditedContent = (localDictionaryId) => {
152
- setEditedContentState((prev) => {
153
- const updated = { ...prev };
154
- delete updated[localDictionaryId];
155
- return updated;
156
- });
157
- };
158
- const clearEditedDictionaryContent = (localDictionaryId) => {
159
- setEditedContentState((prev) => {
160
- const filtered = Object.entries(prev).reduce((acc, [key, value]) => {
161
- if (key === localDictionaryId) {
162
- return acc;
163
- }
164
- return { ...acc, [key]: value };
165
- }, {});
166
- return filtered;
167
- });
168
- };
169
- const clearEditedContent = () => {
170
- setEditedContentState({});
171
- };
172
- const getEditedContentValue = (localDictionaryIdOrKey, keyPath) => {
173
- if (!editedContent) return void 0;
174
- const isDictionaryId = localDictionaryIdOrKey.includes(":local:") || localDictionaryIdOrKey.includes(":remote:");
175
- if (isDictionaryId) {
176
- const currentContent = editedContent?.[localDictionaryIdOrKey]?.content ?? {};
177
- const contentNode = (0, import_core.getContentNodeByKeyPath)(currentContent, keyPath);
178
- return contentNode;
179
- }
180
- const filteredDictionariesLocalId = Object.keys(editedContent).filter(
181
- (key) => key.startsWith(`${localDictionaryIdOrKey}:`)
182
- );
183
- for (const localDictionaryId of filteredDictionariesLocalId) {
184
- const currentContent = editedContent?.[localDictionaryId]?.content ?? {};
185
- const contentNode = (0, import_core.getContentNodeByKeyPath)(currentContent, keyPath);
186
- if (contentNode) return contentNode;
187
- }
188
- return void 0;
189
- };
190
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
191
- EditedContentStateContext.Provider,
192
- {
193
- value: {
194
- editedContent
195
- },
196
- children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
197
- EditedContentActionsContext.Provider,
198
- {
199
- value: {
200
- setEditedContentState,
201
- setEditedDictionary,
202
- setEditedContent,
203
- addEditedContent,
204
- renameEditedContent,
205
- removeEditedContent,
206
- restoreEditedContent,
207
- clearEditedDictionaryContent,
208
- clearEditedContent,
209
- getEditedContentValue
210
- },
211
- children
212
- }
213
- )
214
- }
215
- );
24
+ const { localeDictionaries } = require_DictionariesRecordContext.useDictionariesRecord();
25
+ const [editedContent, setEditedContentState] = require_useCrossFrameState.useCrossFrameState(__intlayer_editor.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED);
26
+ const setEditedDictionary = (newValue) => {
27
+ let updatedDictionaries = resolveState(newValue);
28
+ setEditedContentState((prev) => {
29
+ if (!updatedDictionaries.localId) {
30
+ console.error("no localId", updatedDictionaries);
31
+ return prev;
32
+ }
33
+ updatedDictionaries = resolveState(newValue, prev?.[updatedDictionaries.localId]);
34
+ return {
35
+ ...prev,
36
+ [updatedDictionaries.localId]: updatedDictionaries
37
+ };
38
+ });
39
+ return updatedDictionaries;
40
+ };
41
+ const setEditedContent = (localDictionaryId, newValue) => {
42
+ setEditedContentState((prev) => ({
43
+ ...prev,
44
+ [localDictionaryId]: {
45
+ ...prev?.[localDictionaryId],
46
+ content: newValue
47
+ }
48
+ }));
49
+ };
50
+ const addEditedContent = (localDictionaryId, newValue, keyPath = [], overwrite = true) => {
51
+ setEditedContentState((prev) => {
52
+ const originalContent = localeDictionaries[localDictionaryId]?.content;
53
+ const currentContent = structuredClone(prev?.[localDictionaryId]?.content ?? originalContent);
54
+ let newKeyPath = keyPath;
55
+ if (!overwrite) {
56
+ let index = 0;
57
+ const otherKeyPath = keyPath.slice(0, -1);
58
+ const lastKeyPath = keyPath[keyPath.length - 1];
59
+ let finalKey = lastKeyPath.key;
60
+ while (typeof (0, __intlayer_core.getContentNodeByKeyPath)(currentContent, newKeyPath) !== "undefined") {
61
+ index++;
62
+ finalKey = index === 0 ? lastKeyPath.key : `${lastKeyPath.key} (${index})`;
63
+ newKeyPath = [...otherKeyPath, {
64
+ ...lastKeyPath,
65
+ key: finalKey
66
+ }];
67
+ }
68
+ }
69
+ const updatedContent = (0, __intlayer_core.editDictionaryByKeyPath)(currentContent, newKeyPath, newValue);
70
+ return {
71
+ ...prev,
72
+ [localDictionaryId]: {
73
+ ...prev?.[localDictionaryId],
74
+ content: updatedContent
75
+ }
76
+ };
77
+ });
78
+ };
79
+ const renameEditedContent = (localDictionaryId, newKey, keyPath = []) => {
80
+ setEditedContentState((prev) => {
81
+ const originalContent = localeDictionaries[localDictionaryId]?.content;
82
+ const contentWithNewField = (0, __intlayer_core.renameContentNodeByKeyPath)(structuredClone(prev?.[localDictionaryId]?.content ?? originalContent), newKey, keyPath);
83
+ return {
84
+ ...prev,
85
+ [localDictionaryId]: {
86
+ ...prev?.[localDictionaryId],
87
+ content: contentWithNewField
88
+ }
89
+ };
90
+ });
91
+ };
92
+ const removeEditedContent = (localDictionaryId, keyPath) => {
93
+ setEditedContentState((prev) => {
94
+ const originalContent = localeDictionaries[localDictionaryId]?.content;
95
+ const restoredContent = (0, __intlayer_core.editDictionaryByKeyPath)(structuredClone(prev?.[localDictionaryId]?.content ?? originalContent), keyPath, (0, __intlayer_core.getContentNodeByKeyPath)(originalContent, keyPath));
96
+ return {
97
+ ...prev,
98
+ [localDictionaryId]: {
99
+ ...prev?.[localDictionaryId],
100
+ content: restoredContent
101
+ }
102
+ };
103
+ });
104
+ };
105
+ const restoreEditedContent = (localDictionaryId) => {
106
+ setEditedContentState((prev) => {
107
+ const updated = { ...prev };
108
+ delete updated[localDictionaryId];
109
+ return updated;
110
+ });
111
+ };
112
+ const clearEditedDictionaryContent = (localDictionaryId) => {
113
+ setEditedContentState((prev) => {
114
+ return Object.entries(prev).reduce((acc, [key, value]) => {
115
+ if (key === localDictionaryId) return acc;
116
+ return {
117
+ ...acc,
118
+ [key]: value
119
+ };
120
+ }, {});
121
+ });
122
+ };
123
+ const clearEditedContent = () => {
124
+ setEditedContentState({});
125
+ };
126
+ const getEditedContentValue = (localDictionaryIdOrKey, keyPath) => {
127
+ if (!editedContent) return void 0;
128
+ if (localDictionaryIdOrKey.includes(":local:") || localDictionaryIdOrKey.includes(":remote:")) return (0, __intlayer_core.getContentNodeByKeyPath)(editedContent?.[localDictionaryIdOrKey]?.content ?? {}, keyPath);
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 ?? {}, keyPath);
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
+ });
216
153
  };
217
- const useEditedContentActions = () => (0, import_react.useContext)(EditedContentActionsContext);
154
+ const useEditedContentActions = () => (0, react.useContext)(EditedContentActionsContext);
218
155
  const useEditedContent = () => {
219
- const stateContext = (0, import_react.useContext)(EditedContentStateContext);
220
- const actionContext = useEditedContentActions();
221
- return { ...stateContext, ...actionContext };
156
+ const stateContext = (0, react.useContext)(EditedContentStateContext);
157
+ const actionContext = useEditedContentActions();
158
+ return {
159
+ ...stateContext,
160
+ ...actionContext
161
+ };
222
162
  };
223
- // Annotate the CommonJS export names for ESM import in node:
224
- 0 && (module.exports = {
225
- EditedContentProvider,
226
- useEditedContent,
227
- useEditedContentActions,
228
- useGetEditedContentState,
229
- usePostEditedContentState
230
- });
163
+
164
+ //#endregion
165
+ exports.EditedContentProvider = EditedContentProvider;
166
+ exports.useEditedContent = useEditedContent;
167
+ exports.useEditedContentActions = useEditedContentActions;
168
+ exports.useGetEditedContentState = useGetEditedContentState;
169
+ exports.usePostEditedContentState = usePostEditedContentState;
231
170
  //# sourceMappingURL=EditedContentContext.cjs.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":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAqTM;AAnTN,kBAQO;AACP,oBAA2B;AAC3B,mBAOO;AACP,uCAGO;AACP,0CAA6C;AAC7C,gCAAmC;AAMnC,MAAM,gCAA4B,4BAEhC,MAAS;AAEJ,MAAM,4BAA4B,CACvC,yBAEA;AAAA,EACE,GAAG,yBAAW,+BAA+B;AAAA,EAC7C;AACF;AAEK,MAAM,2BAA2B,CACtC,yBAEA;AAAA,EACE,GAAG,yBAAW,+BAA+B;AAAA,EAC7C;AACF;AAkCF,MAAM,kCAA8B,4BAElC,MAAS;AAEX,MAAM,eAAe,CAAK,OAA2B,cACnD,OAAO,UAAU,aACZ,MAA+B,SAAS,IACxC;AAEA,MAAM,wBAA+C,CAAC,EA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AA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,UAEC;AAAA;AAAA,MACH;AAAA;AAAA,EACF;AAEJ;AAEO,MAAM,0BAA0B,UACrC,yBAAW,2BAA2B;AAEjC,MAAM,mBAAmB,MAAM;AACpC,QAAM,mBAAe,yBAAW,yBAAyB;AACzD,QAAM,gBAAgB,wBAAwB;AAE9C,SAAO,EAAE,GAAG,cAAc,GAAG,cAAc;AAC7C;","names":[]}
1
+ {"version":3,"file":"EditedContentContext.cjs","names":["useCrossFrameMessageListener","MessageKey","EditedContentProvider: FC<PropsWithChildren>","useDictionariesRecord","useCrossFrameState","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,qDAEJ,OAAU;AAEZ,MAAa,6BACX,qBAEAA,kEACE,GAAGC,6BAAW,gCAAgC,QAC9C,iBACD;AAEH,MAAa,4BACX,qBAEAD,kEACE,GAAGC,6BAAW,gCAAgC,OAC9C,iBACD;AAiCH,MAAM,uDAEJ,OAAU;AAEZ,MAAM,gBAAoB,OAA2B,cACnD,OAAO,UAAU,aACZ,MAA+B,UAAU,GACzC;AAEP,MAAaC,yBAAgD,EAAE,eAAe;CAC5E,MAAM,EAAE,uBAAuBC,yDAAuB;CAEtD,MAAM,CAAC,eAAe,yBACpBC,8CACEH,6BAAW,gCACZ;CAEH,MAAMI,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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wnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
- var EditorEnabledContext_exports = {};
21
- __export(EditorEnabledContext_exports, {
22
- EditorEnabledProvider: () => EditorEnabledProvider,
23
- useEditorEnabled: () => useEditorEnabled,
24
- useEditorEnabledState: () => useEditorEnabledState,
25
- useGetEditorEnabledState: () => useGetEditorEnabledState,
26
- usePostEditorEnabledState: () => usePostEditorEnabledState
27
- });
28
- module.exports = __toCommonJS(EditorEnabledContext_exports);
29
- var import_jsx_runtime = require("react/jsx-runtime");
30
- var import_editor = require("@intlayer/editor");
31
- var import_react = require("react");
32
- var import_useCrossFrameMessageListener = require('./useCrossFrameMessageListener.cjs');
33
- var import_useCrossFrameState = require('./useCrossFrameState.cjs');
34
- const EditorEnabledContext = (0, import_react.createContext)({
35
- enabled: false
36
- });
37
- const useEditorEnabledState = (options) => (0, import_useCrossFrameState.useCrossFrameState)(import_editor.MessageKey.INTLAYER_EDITOR_ENABLED, false, options);
38
- const usePostEditorEnabledState = (onEventTriggered) => (0, import_useCrossFrameMessageListener.useCrossFrameMessageListener)(
39
- `${import_editor.MessageKey.INTLAYER_EDITOR_ENABLED}/post`,
40
- onEventTriggered
41
- );
42
- const useGetEditorEnabledState = (onEventTriggered) => (0, import_useCrossFrameMessageListener.useCrossFrameMessageListener)(
43
- `${import_editor.MessageKey.INTLAYER_EDITOR_ENABLED}/get`,
44
- onEventTriggered
45
- );
1
+ 'use client';
2
+
3
+
4
+ const require_rolldown_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
+ react = require_rolldown_runtime.__toESM(react);
9
+ let react_jsx_runtime = require("react/jsx-runtime");
10
+ react_jsx_runtime = require_rolldown_runtime.__toESM(react_jsx_runtime);
11
+ let __intlayer_editor = require("@intlayer/editor");
12
+ __intlayer_editor = require_rolldown_runtime.__toESM(__intlayer_editor);
13
+
14
+ //#region src/EditorEnabledContext.tsx
15
+ const EditorEnabledContext = (0, react.createContext)({ enabled: false });
16
+ const useEditorEnabledState = (options) => require_useCrossFrameState.useCrossFrameState(__intlayer_editor.MessageKey.INTLAYER_EDITOR_ENABLED, false, options);
17
+ const usePostEditorEnabledState = (onEventTriggered) => require_useCrossFrameMessageListener.useCrossFrameMessageListener(`${__intlayer_editor.MessageKey.INTLAYER_EDITOR_ENABLED}/post`, onEventTriggered);
18
+ const useGetEditorEnabledState = (onEventTriggered) => require_useCrossFrameMessageListener.useCrossFrameMessageListener(`${__intlayer_editor.MessageKey.INTLAYER_EDITOR_ENABLED}/get`, onEventTriggered);
46
19
  const EditorEnabledProvider = ({ children }) => {
47
- const [isEnabled] = useEditorEnabledState({
48
- emit: false,
49
- receive: true
50
- });
51
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(EditorEnabledContext.Provider, { value: { enabled: isEnabled }, children });
20
+ const [isEnabled] = useEditorEnabledState({
21
+ emit: false,
22
+ receive: true
23
+ });
24
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditorEnabledContext.Provider, {
25
+ value: { enabled: isEnabled },
26
+ children
27
+ });
52
28
  };
53
- const useEditorEnabled = () => (0, import_react.useContext)(EditorEnabledContext);
54
- // Annotate the CommonJS export names for ESM import in node:
55
- 0 && (module.exports = {
56
- EditorEnabledProvider,
57
- useEditorEnabled,
58
- useEditorEnabledState,
59
- useGetEditorEnabledState,
60
- usePostEditorEnabledState
61
- });
29
+ const useEditorEnabled = () => (0, react.useContext)(EditorEnabledContext);
30
+
31
+ //#endregion
32
+ exports.EditorEnabledProvider = EditorEnabledProvider;
33
+ exports.useEditorEnabled = useEditorEnabled;
34
+ exports.useEditorEnabledState = useEditorEnabledState;
35
+ exports.useGetEditorEnabledState = useGetEditorEnabledState;
36
+ exports.usePostEditorEnabledState = usePostEditorEnabledState;
62
37
  //# sourceMappingURL=EditorEnabledContext.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/EditorEnabledContext.tsx"],"sourcesContent":["'use client';\n\nimport { MessageKey } from '@intlayer/editor';\nimport {\n createContext,\n useContext,\n type FC,\n type PropsWithChildren,\n} from 'react';\nimport { useCrossFrameMessageListener } from './useCrossFrameMessageListener';\nimport {\n useCrossFrameState,\n type CrossFrameStateOptions,\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":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiDI;AA/CJ,oBAA2B;AAC3B,mBAKO;AACP,0CAA6C;AAC7C,gCAGO;AAMP,MAAM,2BAAuB,4BAAuC;AAAA,EAClE,SAAS;AACX,CAAC;AAEM,MAAM,wBAAwB,CAAC,gBACpC,8CAAmB,yBAAW,yBAAyB,OAAO,OAAO;AAEhE,MAAM,4BAA4B,CACvC,yBAEA;AAAA,EACE,GAAG,yBAAW,uBAAuB;AAAA,EACrC;AACF;AAEK,MAAM,2BAA2B,CACtC,yBAEA;AAAA,EACE,GAAG,yBAAW,uBAAuB;AAAA,EACrC;AACF;AAEK,MAAM,wBAA+C,CAAC,EAAE,SAAS,MAAM;AAC5E,QAAM,CAAC,SAAS,IAAI,sBAAsB;AAAA,IACxC,MAAM;AAAA,IACN,SAAS;AAAA,EACX,CAAC;AAED,SACE,4CAAC,qBAAqB,UAArB,EAA8B,OAAO,EAAE,SAAS,UAAU,GACxD,UACH;AAEJ;AAEO,MAAM,mBAAmB,UAAM,yBAAW,oBAAoB;","names":[]}
1
+ {"version":3,"file":"EditorEnabledContext.cjs","names":["useCrossFrameState","MessageKey","useCrossFrameMessageListener","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,gDAA8D,EAClE,SAAS,OACV,CAAC;AAEF,MAAa,yBAAyB,YACpCA,8CAAmBC,6BAAW,yBAAyB,OAAO,QAAQ;AAExE,MAAa,6BACX,qBAEAC,kEACE,GAAGD,6BAAW,wBAAwB,QACtC,iBACD;AAEH,MAAa,4BACX,qBAEAC,kEACE,GAAGD,6BAAW,wBAAwB,OACtC,iBACD;AAEH,MAAaE,yBAAgD,EAAE,eAAe;CAC5E,MAAM,CAAC,aAAa,sBAAsB;EACxC,MAAM;EACN,SAAS;EACV,CAAC;AAEF,QACE,2CAAC,qBAAqB;EAAS,OAAO,EAAE,SAAS,WAAW;EACzD;GAC6B;;AAIpC,MAAa,+CAAoC,qBAAqB"}