@intlayer/editor 4.0.3 → 4.0.4
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/README.md +22 -0
- package/dist/cjs/dictionaryManipulator/editDictionaryByKeyPath.cjs +85 -0
- package/dist/cjs/dictionaryManipulator/editDictionaryByKeyPath.cjs.map +1 -0
- package/dist/cjs/dictionaryManipulator/getDictionaryValueByKeyPath.cjs +42 -0
- package/dist/cjs/dictionaryManipulator/getDictionaryValueByKeyPath.cjs.map +1 -0
- package/dist/cjs/dictionaryManipulator/getSectionType.cjs +48 -0
- package/dist/cjs/dictionaryManipulator/getSectionType.cjs.map +1 -0
- package/dist/cjs/dictionaryManipulator/index.cjs +31 -0
- package/dist/cjs/dictionaryManipulator/index.cjs.map +1 -0
- package/dist/cjs/dictionaryManipulator/isValidReactElement.cjs +30 -0
- package/dist/cjs/dictionaryManipulator/isValidReactElement.cjs.map +1 -0
- package/dist/cjs/dictionaryManipulator/removeDictionaryValueByKeyPath.cjs +55 -0
- package/dist/cjs/dictionaryManipulator/removeDictionaryValueByKeyPath.cjs.map +1 -0
- package/dist/cjs/dictionaryManipulator/renameDictionaryValueByKeyPath.cjs +64 -0
- package/dist/cjs/dictionaryManipulator/renameDictionaryValueByKeyPath.cjs.map +1 -0
- package/dist/cjs/index.cjs +2 -2
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/dictionaryManipulator/editDictionaryByKeyPath.mjs +61 -0
- package/dist/esm/dictionaryManipulator/editDictionaryByKeyPath.mjs.map +1 -0
- package/dist/esm/dictionaryManipulator/getDictionaryValueByKeyPath.mjs +18 -0
- package/dist/esm/dictionaryManipulator/getDictionaryValueByKeyPath.mjs.map +1 -0
- package/dist/esm/dictionaryManipulator/getSectionType.mjs +26 -0
- package/dist/esm/dictionaryManipulator/getSectionType.mjs.map +1 -0
- package/dist/esm/dictionaryManipulator/index.mjs +6 -0
- package/dist/esm/dictionaryManipulator/index.mjs.map +1 -0
- package/dist/esm/dictionaryManipulator/isValidReactElement.mjs +6 -0
- package/dist/esm/dictionaryManipulator/isValidReactElement.mjs.map +1 -0
- package/dist/esm/dictionaryManipulator/removeDictionaryValueByKeyPath.mjs +31 -0
- package/dist/esm/dictionaryManipulator/removeDictionaryValueByKeyPath.mjs.map +1 -0
- package/dist/esm/dictionaryManipulator/renameDictionaryValueByKeyPath.mjs +40 -0
- package/dist/esm/dictionaryManipulator/renameDictionaryValueByKeyPath.mjs.map +1 -0
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/dictionaryManipulator/editDictionaryByKeyPath.d.ts +3 -0
- package/dist/types/dictionaryManipulator/editDictionaryByKeyPath.d.ts.map +1 -0
- package/dist/types/dictionaryManipulator/getDictionaryValueByKeyPath.d.ts +3 -0
- package/dist/types/dictionaryManipulator/getDictionaryValueByKeyPath.d.ts.map +1 -0
- package/dist/types/dictionaryManipulator/getSectionType.d.ts +3 -0
- package/dist/types/dictionaryManipulator/getSectionType.d.ts.map +1 -0
- package/dist/types/dictionaryManipulator/index.d.ts +6 -0
- package/dist/types/dictionaryManipulator/index.d.ts.map +1 -0
- package/dist/types/dictionaryManipulator/isValidReactElement.d.ts +9 -0
- package/dist/types/dictionaryManipulator/isValidReactElement.d.ts.map +1 -0
- package/dist/types/dictionaryManipulator/removeDictionaryValueByKeyPath.d.ts +3 -0
- package/dist/types/dictionaryManipulator/removeDictionaryValueByKeyPath.d.ts.map +1 -0
- package/dist/types/dictionaryManipulator/renameDictionaryValueByKeyPath.d.ts +3 -0
- package/dist/types/dictionaryManipulator/renameDictionaryValueByKeyPath.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +9 -3
- package/dist/cjs/CrossFrameCommunicator.cjs +0 -89
- package/dist/cjs/CrossFrameCommunicator.cjs.map +0 -1
- package/dist/esm/CrossFrameCommunicator.mjs +0 -65
- package/dist/esm/CrossFrameCommunicator.mjs.map +0 -1
- package/dist/types/CrossFrameCommunicator.d.ts +0 -52
- package/dist/types/CrossFrameCommunicator.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -16,3 +16,25 @@
|
|
|
16
16
|
/>
|
|
17
17
|
</a>
|
|
18
18
|
</div>
|
|
19
|
+
|
|
20
|
+
# @intlayer/editor: Utilities to interface the application with the Intlayer editor and manipulate dictionaries
|
|
21
|
+
|
|
22
|
+
**Intlayer** is a suite of packages designed specifically for JavaScript developers. It is compatible with frameworks like React, Next.js, and Express.js.
|
|
23
|
+
|
|
24
|
+
The **`intlayer-editor`** package provides utilities to interface the application with the Intlayer editor. It includes the API to interact with the editor, and utilities to manipulate dictionaries. This package is cross-platform.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
Install the necessary package using your preferred package manager:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install @intlayer/editor
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
yarn add @intlayer/editor
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pnpm add @intlayer/editor
|
|
40
|
+
```
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var editDictionaryByKeyPath_exports = {};
|
|
20
|
+
__export(editDictionaryByKeyPath_exports, {
|
|
21
|
+
editDictionaryByKeyPath: () => editDictionaryByKeyPath
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(editDictionaryByKeyPath_exports);
|
|
24
|
+
var import_core = require("@intlayer/core");
|
|
25
|
+
const editDictionaryByKeyPath = (dictionaryContent, keyPath, newValue) => {
|
|
26
|
+
let currentValue = dictionaryContent;
|
|
27
|
+
let parentValue = null;
|
|
28
|
+
let lastKeys = [];
|
|
29
|
+
for (const keyObj of keyPath) {
|
|
30
|
+
parentValue = currentValue;
|
|
31
|
+
if (keyObj.type === import_core.NodeType.Object) {
|
|
32
|
+
lastKeys = [keyObj.key];
|
|
33
|
+
if (!currentValue[keyObj.key]) {
|
|
34
|
+
currentValue = {
|
|
35
|
+
...currentValue,
|
|
36
|
+
[keyObj.key]: {}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
currentValue = currentValue[keyObj.key];
|
|
40
|
+
} else if (keyObj.type === import_core.NodeType.Array) {
|
|
41
|
+
lastKeys = [keyObj.key];
|
|
42
|
+
if (!currentValue[keyObj.key]) {
|
|
43
|
+
currentValue[keyObj.key] = {};
|
|
44
|
+
}
|
|
45
|
+
currentValue = currentValue[keyObj.key];
|
|
46
|
+
} else if (keyObj.type === import_core.NodeType.Translation) {
|
|
47
|
+
lastKeys = [import_core.NodeType.Translation, keyObj.key];
|
|
48
|
+
if (!currentValue[import_core.NodeType.Translation]) {
|
|
49
|
+
currentValue[import_core.NodeType.Translation] = {
|
|
50
|
+
...currentValue[import_core.NodeType.Translation],
|
|
51
|
+
[keyObj.key]: newValue
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
currentValue = currentValue[import_core.NodeType.Translation][keyObj.key];
|
|
55
|
+
} else if (keyObj.type === import_core.NodeType.Enumeration) {
|
|
56
|
+
lastKeys = [import_core.NodeType.Enumeration, keyObj.key];
|
|
57
|
+
if (!currentValue[import_core.NodeType.Enumeration]) {
|
|
58
|
+
currentValue[import_core.NodeType.Enumeration] = {
|
|
59
|
+
...currentValue[import_core.NodeType.Enumeration],
|
|
60
|
+
[keyObj.key]: newValue
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
currentValue = currentValue[import_core.NodeType.Enumeration][keyObj.key];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (parentValue && lastKeys.length > 0) {
|
|
67
|
+
for (const key of lastKeys.slice(0, -1)) {
|
|
68
|
+
parentValue = parentValue[key];
|
|
69
|
+
}
|
|
70
|
+
if (
|
|
71
|
+
// Remove the field if the new value is undefined
|
|
72
|
+
typeof newValue === "undefined"
|
|
73
|
+
) {
|
|
74
|
+
delete parentValue[lastKeys[lastKeys.length - 1]];
|
|
75
|
+
} else {
|
|
76
|
+
parentValue[lastKeys[lastKeys.length - 1]] = newValue;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return dictionaryContent;
|
|
80
|
+
};
|
|
81
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
82
|
+
0 && (module.exports = {
|
|
83
|
+
editDictionaryByKeyPath
|
|
84
|
+
});
|
|
85
|
+
//# sourceMappingURL=editDictionaryByKeyPath.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/editDictionaryByKeyPath.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { type DictionaryValue, type KeyPath, NodeType } from '@intlayer/core';\n\ntype LastKeyType = string | number;\n\nexport const editDictionaryByKeyPath = (\n dictionaryContent: DictionaryValue,\n keyPath: KeyPath[],\n newValue: DictionaryValue\n): DictionaryValue => {\n let currentValue: any = dictionaryContent;\n let parentValue: any = null;\n let lastKeys: LastKeyType[] = [];\n\n for (const keyObj of keyPath) {\n parentValue = currentValue;\n\n if (keyObj.type === NodeType.Object) {\n lastKeys = [keyObj.key];\n\n if (!currentValue[keyObj.key]) {\n currentValue = {\n ...currentValue,\n [keyObj.key]: {},\n };\n }\n currentValue = currentValue[keyObj.key];\n } else if (keyObj.type === NodeType.Array) {\n lastKeys = [keyObj.key];\n\n if (!currentValue[keyObj.key]) {\n currentValue[keyObj.key] = {};\n }\n currentValue = currentValue[keyObj.key];\n } else if (keyObj.type === NodeType.Translation) {\n lastKeys = [NodeType.Translation, keyObj.key];\n\n if (!currentValue[NodeType.Translation]) {\n currentValue[NodeType.Translation] = {\n ...currentValue[NodeType.Translation],\n [keyObj.key]: newValue,\n };\n }\n currentValue = currentValue[NodeType.Translation][keyObj.key];\n } else if (keyObj.type === NodeType.Enumeration) {\n lastKeys = [NodeType.Enumeration, keyObj.key];\n\n if (!currentValue[NodeType.Enumeration]) {\n currentValue[NodeType.Enumeration] = {\n ...currentValue[NodeType.Enumeration],\n [keyObj.key]: newValue,\n };\n }\n currentValue = currentValue[NodeType.Enumeration][keyObj.key];\n }\n }\n\n // Assign the new value to the last key of the parent\n if (parentValue && lastKeys.length > 0) {\n for (const key of lastKeys.slice(0, -1)) {\n parentValue = parentValue[key];\n }\n if (\n // Remove the field if the new value is undefined\n typeof newValue === 'undefined'\n ) {\n delete parentValue[lastKeys[lastKeys.length - 1]];\n } else {\n parentValue[lastKeys[lastKeys.length - 1]] = newValue;\n }\n }\n\n return dictionaryContent;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,kBAA6D;AAItD,MAAM,0BAA0B,CACrC,mBACA,SACA,aACoB;AACpB,MAAI,eAAoB;AACxB,MAAI,cAAmB;AACvB,MAAI,WAA0B,CAAC;AAE/B,aAAW,UAAU,SAAS;AAC5B,kBAAc;AAEd,QAAI,OAAO,SAAS,qBAAS,QAAQ;AACnC,iBAAW,CAAC,OAAO,GAAG;AAEtB,UAAI,CAAC,aAAa,OAAO,GAAG,GAAG;AAC7B,uBAAe;AAAA,UACb,GAAG;AAAA,UACH,CAAC,OAAO,GAAG,GAAG,CAAC;AAAA,QACjB;AAAA,MACF;AACA,qBAAe,aAAa,OAAO,GAAG;AAAA,IACxC,WAAW,OAAO,SAAS,qBAAS,OAAO;AACzC,iBAAW,CAAC,OAAO,GAAG;AAEtB,UAAI,CAAC,aAAa,OAAO,GAAG,GAAG;AAC7B,qBAAa,OAAO,GAAG,IAAI,CAAC;AAAA,MAC9B;AACA,qBAAe,aAAa,OAAO,GAAG;AAAA,IACxC,WAAW,OAAO,SAAS,qBAAS,aAAa;AAC/C,iBAAW,CAAC,qBAAS,aAAa,OAAO,GAAG;AAE5C,UAAI,CAAC,aAAa,qBAAS,WAAW,GAAG;AACvC,qBAAa,qBAAS,WAAW,IAAI;AAAA,UACnC,GAAG,aAAa,qBAAS,WAAW;AAAA,UACpC,CAAC,OAAO,GAAG,GAAG;AAAA,QAChB;AAAA,MACF;AACA,qBAAe,aAAa,qBAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAC9D,WAAW,OAAO,SAAS,qBAAS,aAAa;AAC/C,iBAAW,CAAC,qBAAS,aAAa,OAAO,GAAG;AAE5C,UAAI,CAAC,aAAa,qBAAS,WAAW,GAAG;AACvC,qBAAa,qBAAS,WAAW,IAAI;AAAA,UACnC,GAAG,aAAa,qBAAS,WAAW;AAAA,UACpC,CAAC,OAAO,GAAG,GAAG;AAAA,QAChB;AAAA,MACF;AACA,qBAAe,aAAa,qBAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAC9D;AAAA,EACF;AAGA,MAAI,eAAe,SAAS,SAAS,GAAG;AACtC,eAAW,OAAO,SAAS,MAAM,GAAG,EAAE,GAAG;AACvC,oBAAc,YAAY,GAAG;AAAA,IAC/B;AACA;AAAA;AAAA,MAEE,OAAO,aAAa;AAAA,MACpB;AACA,aAAO,YAAY,SAAS,SAAS,SAAS,CAAC,CAAC;AAAA,IAClD,OAAO;AACL,kBAAY,SAAS,SAAS,SAAS,CAAC,CAAC,IAAI;AAAA,IAC/C;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var getDictionaryValueByKeyPath_exports = {};
|
|
20
|
+
__export(getDictionaryValueByKeyPath_exports, {
|
|
21
|
+
getDictionaryValueByKeyPath: () => getDictionaryValueByKeyPath
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(getDictionaryValueByKeyPath_exports);
|
|
24
|
+
var import_core = require("@intlayer/core");
|
|
25
|
+
const getDictionaryValueByKeyPath = (dictionaryContent, keyPath) => {
|
|
26
|
+
let currentValue = JSON.parse(JSON.stringify(dictionaryContent ?? {}));
|
|
27
|
+
for (const keyObj of keyPath) {
|
|
28
|
+
if (keyObj.type === import_core.NodeType.Object || keyObj.type === import_core.NodeType.Array) {
|
|
29
|
+
currentValue = currentValue?.[keyObj.key];
|
|
30
|
+
} else if (keyObj.type === import_core.NodeType.Translation) {
|
|
31
|
+
currentValue = currentValue?.[import_core.NodeType.Translation][keyObj.key];
|
|
32
|
+
} else if (keyObj.type === import_core.NodeType.Enumeration) {
|
|
33
|
+
currentValue = currentValue?.[import_core.NodeType.Enumeration][keyObj.key];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return currentValue;
|
|
37
|
+
};
|
|
38
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
39
|
+
0 && (module.exports = {
|
|
40
|
+
getDictionaryValueByKeyPath
|
|
41
|
+
});
|
|
42
|
+
//# sourceMappingURL=getDictionaryValueByKeyPath.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/getDictionaryValueByKeyPath.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { type DictionaryValue, type KeyPath, NodeType } from '@intlayer/core';\n\nexport const getDictionaryValueByKeyPath = (\n dictionaryContent: DictionaryValue,\n keyPath: KeyPath[]\n): DictionaryValue => {\n let currentValue: any = JSON.parse(JSON.stringify(dictionaryContent ?? {}));\n\n for (const keyObj of keyPath) {\n if (keyObj.type === NodeType.Object || keyObj.type === NodeType.Array) {\n currentValue = currentValue?.[keyObj.key];\n } else if (keyObj.type === NodeType.Translation) {\n currentValue = currentValue?.[NodeType.Translation][keyObj.key];\n } else if (keyObj.type === NodeType.Enumeration) {\n currentValue = currentValue?.[NodeType.Enumeration][keyObj.key];\n }\n }\n\n return currentValue as DictionaryValue;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,kBAA6D;AAEtD,MAAM,8BAA8B,CACzC,mBACA,YACoB;AACpB,MAAI,eAAoB,KAAK,MAAM,KAAK,UAAU,qBAAqB,CAAC,CAAC,CAAC;AAE1E,aAAW,UAAU,SAAS;AAC5B,QAAI,OAAO,SAAS,qBAAS,UAAU,OAAO,SAAS,qBAAS,OAAO;AACrE,qBAAe,eAAe,OAAO,GAAG;AAAA,IAC1C,WAAW,OAAO,SAAS,qBAAS,aAAa;AAC/C,qBAAe,eAAe,qBAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAChE,WAAW,OAAO,SAAS,qBAAS,aAAa;AAC/C,qBAAe,eAAe,qBAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAChE;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var getSectionType_exports = {};
|
|
20
|
+
__export(getSectionType_exports, {
|
|
21
|
+
getSectionType: () => getSectionType
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(getSectionType_exports);
|
|
24
|
+
var import_core = require("@intlayer/core");
|
|
25
|
+
var import_isValidReactElement = require('./isValidReactElement.cjs');
|
|
26
|
+
const getSectionType = (section) => {
|
|
27
|
+
if (typeof section === "string") {
|
|
28
|
+
return import_core.NodeType.Text;
|
|
29
|
+
}
|
|
30
|
+
if (section?.nodeType === import_core.NodeType.Translation) {
|
|
31
|
+
return import_core.NodeType.Translation;
|
|
32
|
+
}
|
|
33
|
+
if (section?.nodeType === import_core.NodeType.Enumeration) {
|
|
34
|
+
return import_core.NodeType.Enumeration;
|
|
35
|
+
}
|
|
36
|
+
if (Array.isArray(section)) {
|
|
37
|
+
return import_core.NodeType.Array;
|
|
38
|
+
}
|
|
39
|
+
if ((0, import_isValidReactElement.isValidElement)(section)) {
|
|
40
|
+
return import_core.NodeType.ReactNode;
|
|
41
|
+
}
|
|
42
|
+
return import_core.NodeType.Object;
|
|
43
|
+
};
|
|
44
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
+
0 && (module.exports = {
|
|
46
|
+
getSectionType
|
|
47
|
+
});
|
|
48
|
+
//# sourceMappingURL=getSectionType.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/getSectionType.ts"],"sourcesContent":["import {\n type TranslationContent,\n type EnumerationContent,\n type DictionaryValue,\n NodeType,\n} from '@intlayer/core';\nimport { isValidElement } from './isValidReactElement';\n\nexport const getSectionType = (section: DictionaryValue): NodeType => {\n if (typeof section === 'string') {\n return NodeType.Text;\n }\n\n if (\n (section as TranslationContent<unknown>)?.nodeType === NodeType.Translation\n ) {\n return NodeType.Translation;\n }\n\n if (\n (section as EnumerationContent<unknown>)?.nodeType === NodeType.Enumeration\n ) {\n return NodeType.Enumeration;\n }\n\n if (Array.isArray(section)) {\n return NodeType.Array;\n }\n\n if (isValidElement(section)) {\n return NodeType.ReactNode;\n }\n\n return NodeType.Object;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAKO;AACP,iCAA+B;AAExB,MAAM,iBAAiB,CAAC,YAAuC;AACpE,MAAI,OAAO,YAAY,UAAU;AAC/B,WAAO,qBAAS;AAAA,EAClB;AAEA,MACG,SAAyC,aAAa,qBAAS,aAChE;AACA,WAAO,qBAAS;AAAA,EAClB;AAEA,MACG,SAAyC,aAAa,qBAAS,aAChE;AACA,WAAO,qBAAS;AAAA,EAClB;AAEA,MAAI,MAAM,QAAQ,OAAO,GAAG;AAC1B,WAAO,qBAAS;AAAA,EAClB;AAEA,UAAI,2CAAe,OAAO,GAAG;AAC3B,WAAO,qBAAS;AAAA,EAClB;AAEA,SAAO,qBAAS;AAClB;","names":[]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var dictionaryManipulator_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(dictionaryManipulator_exports);
|
|
18
|
+
__reExport(dictionaryManipulator_exports, require('./editDictionaryByKeyPath.cjs'), module.exports);
|
|
19
|
+
__reExport(dictionaryManipulator_exports, require('./getDictionaryValueByKeyPath.cjs'), module.exports);
|
|
20
|
+
__reExport(dictionaryManipulator_exports, require('./getSectionType.cjs'), module.exports);
|
|
21
|
+
__reExport(dictionaryManipulator_exports, require('./removeDictionaryValueByKeyPath.cjs'), module.exports);
|
|
22
|
+
__reExport(dictionaryManipulator_exports, require('./renameDictionaryValueByKeyPath.cjs'), module.exports);
|
|
23
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
24
|
+
0 && (module.exports = {
|
|
25
|
+
...require('./editDictionaryByKeyPath.cjs'),
|
|
26
|
+
...require('./getDictionaryValueByKeyPath.cjs'),
|
|
27
|
+
...require('./getSectionType.cjs'),
|
|
28
|
+
...require('./removeDictionaryValueByKeyPath.cjs'),
|
|
29
|
+
...require('./renameDictionaryValueByKeyPath.cjs')
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/index.ts"],"sourcesContent":["export * from './editDictionaryByKeyPath';\nexport * from './getDictionaryValueByKeyPath';\nexport * from './getSectionType';\nexport * from './removeDictionaryValueByKeyPath';\nexport * from './renameDictionaryValueByKeyPath';\n"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,0CAAc,sCAAd;AACA,0CAAc,0CADd;AAEA,0CAAc,6BAFd;AAGA,0CAAc,6CAHd;AAIA,0CAAc,6CAJd;","names":[]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var isValidReactElement_exports = {};
|
|
20
|
+
__export(isValidReactElement_exports, {
|
|
21
|
+
isValidElement: () => isValidElement
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(isValidReactElement_exports);
|
|
24
|
+
const REACT_ELEMENT_TYPE = Symbol.for("react.element");
|
|
25
|
+
const isValidElement = (object) => typeof object === "object" && object !== null && object.$$typeof === REACT_ELEMENT_TYPE;
|
|
26
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
27
|
+
0 && (module.exports = {
|
|
28
|
+
isValidElement
|
|
29
|
+
});
|
|
30
|
+
//# sourceMappingURL=isValidReactElement.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/isValidReactElement.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nconst REACT_ELEMENT_TYPE = Symbol.for('react.element');\n\n/**\n * Verifies the object is a ReactElement.\n * See https://reactjs.org/docs/react-api.html#isvalidelement\n * @param object\n * @return True if `object` is a ReactElement.\n * @final\n */\n\nexport const isValidElement = (object: any): boolean =>\n typeof object === 'object' &&\n object !== null &&\n object.$$typeof === REACT_ELEMENT_TYPE;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,MAAM,qBAAqB,OAAO,IAAI,eAAe;AAU9C,MAAM,iBAAiB,CAAC,WAC7B,OAAO,WAAW,YAClB,WAAW,QACX,OAAO,aAAa;","names":[]}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var removeDictionaryValueByKeyPath_exports = {};
|
|
20
|
+
__export(removeDictionaryValueByKeyPath_exports, {
|
|
21
|
+
removeDictionaryValueByKeyPath: () => removeDictionaryValueByKeyPath
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(removeDictionaryValueByKeyPath_exports);
|
|
24
|
+
var import_core = require("@intlayer/core");
|
|
25
|
+
const removeDictionaryValueByKeyPath = (dictionaryContent, keyPath) => {
|
|
26
|
+
let currentValue = dictionaryContent;
|
|
27
|
+
let parentValue = null;
|
|
28
|
+
let lastKey = null;
|
|
29
|
+
for (const keyObj of keyPath) {
|
|
30
|
+
parentValue = currentValue;
|
|
31
|
+
if (keyObj.type === import_core.NodeType.Object || keyObj.type === import_core.NodeType.Array) {
|
|
32
|
+
lastKey = keyObj.key;
|
|
33
|
+
currentValue = currentValue[keyObj.key];
|
|
34
|
+
} else if (keyObj.type === import_core.NodeType.Translation) {
|
|
35
|
+
lastKey = import_core.NodeType.Translation;
|
|
36
|
+
currentValue = currentValue[import_core.NodeType.Translation][keyObj.key];
|
|
37
|
+
} else if (keyObj.type === import_core.NodeType.Enumeration) {
|
|
38
|
+
lastKey = import_core.NodeType.Enumeration;
|
|
39
|
+
currentValue = currentValue[import_core.NodeType.Enumeration][keyObj.key];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (parentValue && lastKey !== null) {
|
|
43
|
+
if (Array.isArray(parentValue)) {
|
|
44
|
+
parentValue.splice(lastKey, 1);
|
|
45
|
+
} else {
|
|
46
|
+
delete parentValue[lastKey];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return dictionaryContent;
|
|
50
|
+
};
|
|
51
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
52
|
+
0 && (module.exports = {
|
|
53
|
+
removeDictionaryValueByKeyPath
|
|
54
|
+
});
|
|
55
|
+
//# sourceMappingURL=removeDictionaryValueByKeyPath.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/removeDictionaryValueByKeyPath.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport { DictionaryValue, KeyPath, NodeType } from '@intlayer/core';\n\nexport const removeDictionaryValueByKeyPath = (\n dictionaryContent: DictionaryValue,\n keyPath: KeyPath[]\n): DictionaryValue => {\n let currentValue: any = dictionaryContent;\n let parentValue: any = null;\n let lastKey: string | number | null = null;\n\n for (const keyObj of keyPath) {\n parentValue = currentValue;\n\n if (keyObj.type === NodeType.Object || keyObj.type === NodeType.Array) {\n lastKey = keyObj.key;\n currentValue = currentValue[keyObj.key];\n } else if (keyObj.type === NodeType.Translation) {\n lastKey = NodeType.Translation;\n currentValue = currentValue[NodeType.Translation][keyObj.key];\n } else if (keyObj.type === NodeType.Enumeration) {\n lastKey = NodeType.Enumeration;\n currentValue = currentValue[NodeType.Enumeration][keyObj.key];\n }\n }\n\n if (parentValue && lastKey !== null) {\n if (Array.isArray(parentValue)) {\n parentValue.splice(lastKey as unknown as number, 1);\n } else {\n delete parentValue[lastKey];\n }\n }\n\n return dictionaryContent;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,kBAAmD;AAE5C,MAAM,iCAAiC,CAC5C,mBACA,YACoB;AACpB,MAAI,eAAoB;AACxB,MAAI,cAAmB;AACvB,MAAI,UAAkC;AAEtC,aAAW,UAAU,SAAS;AAC5B,kBAAc;AAEd,QAAI,OAAO,SAAS,qBAAS,UAAU,OAAO,SAAS,qBAAS,OAAO;AACrE,gBAAU,OAAO;AACjB,qBAAe,aAAa,OAAO,GAAG;AAAA,IACxC,WAAW,OAAO,SAAS,qBAAS,aAAa;AAC/C,gBAAU,qBAAS;AACnB,qBAAe,aAAa,qBAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAC9D,WAAW,OAAO,SAAS,qBAAS,aAAa;AAC/C,gBAAU,qBAAS;AACnB,qBAAe,aAAa,qBAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAC9D;AAAA,EACF;AAEA,MAAI,eAAe,YAAY,MAAM;AACnC,QAAI,MAAM,QAAQ,WAAW,GAAG;AAC9B,kBAAY,OAAO,SAA8B,CAAC;AAAA,IACpD,OAAO;AACL,aAAO,YAAY,OAAO;AAAA,IAC5B;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var renameDictionaryValueByKeyPath_exports = {};
|
|
20
|
+
__export(renameDictionaryValueByKeyPath_exports, {
|
|
21
|
+
renameDictionaryValueByKeyPath: () => renameDictionaryValueByKeyPath
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(renameDictionaryValueByKeyPath_exports);
|
|
24
|
+
var import_core = require("@intlayer/core");
|
|
25
|
+
const renameDictionaryValueByKeyPath = (dictionaryContent, newKey, keyPath) => {
|
|
26
|
+
let currentValue = dictionaryContent;
|
|
27
|
+
let parentValue = null;
|
|
28
|
+
let lastKey = null;
|
|
29
|
+
for (const keyObj of keyPath) {
|
|
30
|
+
parentValue = currentValue;
|
|
31
|
+
if (keyObj.type === import_core.NodeType.Object || keyObj.type === import_core.NodeType.Array) {
|
|
32
|
+
lastKey = keyObj.key;
|
|
33
|
+
currentValue = currentValue[keyObj.key];
|
|
34
|
+
} else if (keyObj.type === import_core.NodeType.Translation) {
|
|
35
|
+
lastKey = import_core.NodeType.Translation;
|
|
36
|
+
currentValue = currentValue[import_core.NodeType.Translation][keyObj.key];
|
|
37
|
+
} else if (keyObj.type === import_core.NodeType.Enumeration) {
|
|
38
|
+
lastKey = import_core.NodeType.Enumeration;
|
|
39
|
+
currentValue = currentValue[import_core.NodeType.Enumeration][keyObj.key];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (parentValue && lastKey !== null) {
|
|
43
|
+
if (Array.isArray(parentValue)) {
|
|
44
|
+
parentValue[lastKey] = currentValue;
|
|
45
|
+
} else {
|
|
46
|
+
const newParentValue = {};
|
|
47
|
+
for (const key of Object.keys(parentValue)) {
|
|
48
|
+
if (key === lastKey) {
|
|
49
|
+
newParentValue[newKey] = currentValue;
|
|
50
|
+
} else {
|
|
51
|
+
newParentValue[key] = parentValue[key];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
Object.keys(parentValue).forEach((key) => delete parentValue[key]);
|
|
55
|
+
Object.assign(parentValue, newParentValue);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return dictionaryContent;
|
|
59
|
+
};
|
|
60
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
61
|
+
0 && (module.exports = {
|
|
62
|
+
renameDictionaryValueByKeyPath
|
|
63
|
+
});
|
|
64
|
+
//# sourceMappingURL=renameDictionaryValueByKeyPath.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/renameDictionaryValueByKeyPath.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport { DictionaryValue, KeyPath, NodeType } from '@intlayer/core';\n\nexport const renameDictionaryValueByKeyPath = (\n dictionaryContent: DictionaryValue,\n newKey: string | number,\n keyPath: KeyPath[]\n): DictionaryValue => {\n let currentValue: any = dictionaryContent;\n let parentValue: any = null;\n let lastKey: string | number | null = null;\n\n for (const keyObj of keyPath) {\n parentValue = currentValue;\n\n if (keyObj.type === NodeType.Object || keyObj.type === NodeType.Array) {\n lastKey = keyObj.key;\n currentValue = currentValue[keyObj.key];\n } else if (keyObj.type === NodeType.Translation) {\n lastKey = NodeType.Translation;\n currentValue = currentValue[NodeType.Translation][keyObj.key];\n } else if (keyObj.type === NodeType.Enumeration) {\n lastKey = NodeType.Enumeration;\n currentValue = currentValue[NodeType.Enumeration][keyObj.key];\n }\n }\n\n // Assign the new value to the last key of the parent while preserving the order\n if (parentValue && lastKey !== null) {\n if (Array.isArray(parentValue)) {\n parentValue[lastKey as number] = currentValue;\n } else {\n const newParentValue: any = {};\n for (const key of Object.keys(parentValue)) {\n if (key === lastKey) {\n newParentValue[newKey] = currentValue;\n } else {\n newParentValue[key] = parentValue[key];\n }\n }\n // Replace the contents of parentValue with newParentValue\n Object.keys(parentValue).forEach((key) => delete parentValue[key]);\n Object.assign(parentValue, newParentValue);\n }\n }\n\n return dictionaryContent;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,kBAAmD;AAE5C,MAAM,iCAAiC,CAC5C,mBACA,QACA,YACoB;AACpB,MAAI,eAAoB;AACxB,MAAI,cAAmB;AACvB,MAAI,UAAkC;AAEtC,aAAW,UAAU,SAAS;AAC5B,kBAAc;AAEd,QAAI,OAAO,SAAS,qBAAS,UAAU,OAAO,SAAS,qBAAS,OAAO;AACrE,gBAAU,OAAO;AACjB,qBAAe,aAAa,OAAO,GAAG;AAAA,IACxC,WAAW,OAAO,SAAS,qBAAS,aAAa;AAC/C,gBAAU,qBAAS;AACnB,qBAAe,aAAa,qBAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAC9D,WAAW,OAAO,SAAS,qBAAS,aAAa;AAC/C,gBAAU,qBAAS;AACnB,qBAAe,aAAa,qBAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAC9D;AAAA,EACF;AAGA,MAAI,eAAe,YAAY,MAAM;AACnC,QAAI,MAAM,QAAQ,WAAW,GAAG;AAC9B,kBAAY,OAAiB,IAAI;AAAA,IACnC,OAAO;AACL,YAAM,iBAAsB,CAAC;AAC7B,iBAAW,OAAO,OAAO,KAAK,WAAW,GAAG;AAC1C,YAAI,QAAQ,SAAS;AACnB,yBAAe,MAAM,IAAI;AAAA,QAC3B,OAAO;AACL,yBAAe,GAAG,IAAI,YAAY,GAAG;AAAA,QACvC;AAAA,MACF;AAEA,aAAO,KAAK,WAAW,EAAE,QAAQ,CAAC,QAAQ,OAAO,YAAY,GAAG,CAAC;AACjE,aAAO,OAAO,aAAa,cAAc;AAAA,IAC3C;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -15,9 +15,9 @@ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "defau
|
|
|
15
15
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
16
|
var index_exports = {};
|
|
17
17
|
module.exports = __toCommonJS(index_exports);
|
|
18
|
-
__reExport(index_exports, require('./
|
|
18
|
+
__reExport(index_exports, require('./dictionaryManipulator/index.cjs'), module.exports);
|
|
19
19
|
// Annotate the CommonJS export names for ESM import in node:
|
|
20
20
|
0 && (module.exports = {
|
|
21
|
-
...require('./
|
|
21
|
+
...require('./dictionaryManipulator/index.cjs')
|
|
22
22
|
});
|
|
23
23
|
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './dictionaryManipulator/index';\n"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,0BAAc,0CAAd;","names":[]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { NodeType } from "@intlayer/core";
|
|
2
|
+
const editDictionaryByKeyPath = (dictionaryContent, keyPath, newValue) => {
|
|
3
|
+
let currentValue = dictionaryContent;
|
|
4
|
+
let parentValue = null;
|
|
5
|
+
let lastKeys = [];
|
|
6
|
+
for (const keyObj of keyPath) {
|
|
7
|
+
parentValue = currentValue;
|
|
8
|
+
if (keyObj.type === NodeType.Object) {
|
|
9
|
+
lastKeys = [keyObj.key];
|
|
10
|
+
if (!currentValue[keyObj.key]) {
|
|
11
|
+
currentValue = {
|
|
12
|
+
...currentValue,
|
|
13
|
+
[keyObj.key]: {}
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
currentValue = currentValue[keyObj.key];
|
|
17
|
+
} else if (keyObj.type === NodeType.Array) {
|
|
18
|
+
lastKeys = [keyObj.key];
|
|
19
|
+
if (!currentValue[keyObj.key]) {
|
|
20
|
+
currentValue[keyObj.key] = {};
|
|
21
|
+
}
|
|
22
|
+
currentValue = currentValue[keyObj.key];
|
|
23
|
+
} else if (keyObj.type === NodeType.Translation) {
|
|
24
|
+
lastKeys = [NodeType.Translation, keyObj.key];
|
|
25
|
+
if (!currentValue[NodeType.Translation]) {
|
|
26
|
+
currentValue[NodeType.Translation] = {
|
|
27
|
+
...currentValue[NodeType.Translation],
|
|
28
|
+
[keyObj.key]: newValue
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
currentValue = currentValue[NodeType.Translation][keyObj.key];
|
|
32
|
+
} else if (keyObj.type === NodeType.Enumeration) {
|
|
33
|
+
lastKeys = [NodeType.Enumeration, keyObj.key];
|
|
34
|
+
if (!currentValue[NodeType.Enumeration]) {
|
|
35
|
+
currentValue[NodeType.Enumeration] = {
|
|
36
|
+
...currentValue[NodeType.Enumeration],
|
|
37
|
+
[keyObj.key]: newValue
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
currentValue = currentValue[NodeType.Enumeration][keyObj.key];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (parentValue && lastKeys.length > 0) {
|
|
44
|
+
for (const key of lastKeys.slice(0, -1)) {
|
|
45
|
+
parentValue = parentValue[key];
|
|
46
|
+
}
|
|
47
|
+
if (
|
|
48
|
+
// Remove the field if the new value is undefined
|
|
49
|
+
typeof newValue === "undefined"
|
|
50
|
+
) {
|
|
51
|
+
delete parentValue[lastKeys[lastKeys.length - 1]];
|
|
52
|
+
} else {
|
|
53
|
+
parentValue[lastKeys[lastKeys.length - 1]] = newValue;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return dictionaryContent;
|
|
57
|
+
};
|
|
58
|
+
export {
|
|
59
|
+
editDictionaryByKeyPath
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=editDictionaryByKeyPath.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/editDictionaryByKeyPath.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { type DictionaryValue, type KeyPath, NodeType } from '@intlayer/core';\n\ntype LastKeyType = string | number;\n\nexport const editDictionaryByKeyPath = (\n dictionaryContent: DictionaryValue,\n keyPath: KeyPath[],\n newValue: DictionaryValue\n): DictionaryValue => {\n let currentValue: any = dictionaryContent;\n let parentValue: any = null;\n let lastKeys: LastKeyType[] = [];\n\n for (const keyObj of keyPath) {\n parentValue = currentValue;\n\n if (keyObj.type === NodeType.Object) {\n lastKeys = [keyObj.key];\n\n if (!currentValue[keyObj.key]) {\n currentValue = {\n ...currentValue,\n [keyObj.key]: {},\n };\n }\n currentValue = currentValue[keyObj.key];\n } else if (keyObj.type === NodeType.Array) {\n lastKeys = [keyObj.key];\n\n if (!currentValue[keyObj.key]) {\n currentValue[keyObj.key] = {};\n }\n currentValue = currentValue[keyObj.key];\n } else if (keyObj.type === NodeType.Translation) {\n lastKeys = [NodeType.Translation, keyObj.key];\n\n if (!currentValue[NodeType.Translation]) {\n currentValue[NodeType.Translation] = {\n ...currentValue[NodeType.Translation],\n [keyObj.key]: newValue,\n };\n }\n currentValue = currentValue[NodeType.Translation][keyObj.key];\n } else if (keyObj.type === NodeType.Enumeration) {\n lastKeys = [NodeType.Enumeration, keyObj.key];\n\n if (!currentValue[NodeType.Enumeration]) {\n currentValue[NodeType.Enumeration] = {\n ...currentValue[NodeType.Enumeration],\n [keyObj.key]: newValue,\n };\n }\n currentValue = currentValue[NodeType.Enumeration][keyObj.key];\n }\n }\n\n // Assign the new value to the last key of the parent\n if (parentValue && lastKeys.length > 0) {\n for (const key of lastKeys.slice(0, -1)) {\n parentValue = parentValue[key];\n }\n if (\n // Remove the field if the new value is undefined\n typeof newValue === 'undefined'\n ) {\n delete parentValue[lastKeys[lastKeys.length - 1]];\n } else {\n parentValue[lastKeys[lastKeys.length - 1]] = newValue;\n }\n }\n\n return dictionaryContent;\n};\n"],"mappings":"AACA,SAA6C,gBAAgB;AAItD,MAAM,0BAA0B,CACrC,mBACA,SACA,aACoB;AACpB,MAAI,eAAoB;AACxB,MAAI,cAAmB;AACvB,MAAI,WAA0B,CAAC;AAE/B,aAAW,UAAU,SAAS;AAC5B,kBAAc;AAEd,QAAI,OAAO,SAAS,SAAS,QAAQ;AACnC,iBAAW,CAAC,OAAO,GAAG;AAEtB,UAAI,CAAC,aAAa,OAAO,GAAG,GAAG;AAC7B,uBAAe;AAAA,UACb,GAAG;AAAA,UACH,CAAC,OAAO,GAAG,GAAG,CAAC;AAAA,QACjB;AAAA,MACF;AACA,qBAAe,aAAa,OAAO,GAAG;AAAA,IACxC,WAAW,OAAO,SAAS,SAAS,OAAO;AACzC,iBAAW,CAAC,OAAO,GAAG;AAEtB,UAAI,CAAC,aAAa,OAAO,GAAG,GAAG;AAC7B,qBAAa,OAAO,GAAG,IAAI,CAAC;AAAA,MAC9B;AACA,qBAAe,aAAa,OAAO,GAAG;AAAA,IACxC,WAAW,OAAO,SAAS,SAAS,aAAa;AAC/C,iBAAW,CAAC,SAAS,aAAa,OAAO,GAAG;AAE5C,UAAI,CAAC,aAAa,SAAS,WAAW,GAAG;AACvC,qBAAa,SAAS,WAAW,IAAI;AAAA,UACnC,GAAG,aAAa,SAAS,WAAW;AAAA,UACpC,CAAC,OAAO,GAAG,GAAG;AAAA,QAChB;AAAA,MACF;AACA,qBAAe,aAAa,SAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAC9D,WAAW,OAAO,SAAS,SAAS,aAAa;AAC/C,iBAAW,CAAC,SAAS,aAAa,OAAO,GAAG;AAE5C,UAAI,CAAC,aAAa,SAAS,WAAW,GAAG;AACvC,qBAAa,SAAS,WAAW,IAAI;AAAA,UACnC,GAAG,aAAa,SAAS,WAAW;AAAA,UACpC,CAAC,OAAO,GAAG,GAAG;AAAA,QAChB;AAAA,MACF;AACA,qBAAe,aAAa,SAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAC9D;AAAA,EACF;AAGA,MAAI,eAAe,SAAS,SAAS,GAAG;AACtC,eAAW,OAAO,SAAS,MAAM,GAAG,EAAE,GAAG;AACvC,oBAAc,YAAY,GAAG;AAAA,IAC/B;AACA;AAAA;AAAA,MAEE,OAAO,aAAa;AAAA,MACpB;AACA,aAAO,YAAY,SAAS,SAAS,SAAS,CAAC,CAAC;AAAA,IAClD,OAAO;AACL,kBAAY,SAAS,SAAS,SAAS,CAAC,CAAC,IAAI;AAAA,IAC/C;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { NodeType } from "@intlayer/core";
|
|
2
|
+
const getDictionaryValueByKeyPath = (dictionaryContent, keyPath) => {
|
|
3
|
+
let currentValue = JSON.parse(JSON.stringify(dictionaryContent ?? {}));
|
|
4
|
+
for (const keyObj of keyPath) {
|
|
5
|
+
if (keyObj.type === NodeType.Object || keyObj.type === NodeType.Array) {
|
|
6
|
+
currentValue = currentValue?.[keyObj.key];
|
|
7
|
+
} else if (keyObj.type === NodeType.Translation) {
|
|
8
|
+
currentValue = currentValue?.[NodeType.Translation][keyObj.key];
|
|
9
|
+
} else if (keyObj.type === NodeType.Enumeration) {
|
|
10
|
+
currentValue = currentValue?.[NodeType.Enumeration][keyObj.key];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return currentValue;
|
|
14
|
+
};
|
|
15
|
+
export {
|
|
16
|
+
getDictionaryValueByKeyPath
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=getDictionaryValueByKeyPath.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/getDictionaryValueByKeyPath.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { type DictionaryValue, type KeyPath, NodeType } from '@intlayer/core';\n\nexport const getDictionaryValueByKeyPath = (\n dictionaryContent: DictionaryValue,\n keyPath: KeyPath[]\n): DictionaryValue => {\n let currentValue: any = JSON.parse(JSON.stringify(dictionaryContent ?? {}));\n\n for (const keyObj of keyPath) {\n if (keyObj.type === NodeType.Object || keyObj.type === NodeType.Array) {\n currentValue = currentValue?.[keyObj.key];\n } else if (keyObj.type === NodeType.Translation) {\n currentValue = currentValue?.[NodeType.Translation][keyObj.key];\n } else if (keyObj.type === NodeType.Enumeration) {\n currentValue = currentValue?.[NodeType.Enumeration][keyObj.key];\n }\n }\n\n return currentValue as DictionaryValue;\n};\n"],"mappings":"AACA,SAA6C,gBAAgB;AAEtD,MAAM,8BAA8B,CACzC,mBACA,YACoB;AACpB,MAAI,eAAoB,KAAK,MAAM,KAAK,UAAU,qBAAqB,CAAC,CAAC,CAAC;AAE1E,aAAW,UAAU,SAAS;AAC5B,QAAI,OAAO,SAAS,SAAS,UAAU,OAAO,SAAS,SAAS,OAAO;AACrE,qBAAe,eAAe,OAAO,GAAG;AAAA,IAC1C,WAAW,OAAO,SAAS,SAAS,aAAa;AAC/C,qBAAe,eAAe,SAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAChE,WAAW,OAAO,SAAS,SAAS,aAAa;AAC/C,qBAAe,eAAe,SAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAChE;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NodeType
|
|
3
|
+
} from "@intlayer/core";
|
|
4
|
+
import { isValidElement } from './isValidReactElement.mjs';
|
|
5
|
+
const getSectionType = (section) => {
|
|
6
|
+
if (typeof section === "string") {
|
|
7
|
+
return NodeType.Text;
|
|
8
|
+
}
|
|
9
|
+
if (section?.nodeType === NodeType.Translation) {
|
|
10
|
+
return NodeType.Translation;
|
|
11
|
+
}
|
|
12
|
+
if (section?.nodeType === NodeType.Enumeration) {
|
|
13
|
+
return NodeType.Enumeration;
|
|
14
|
+
}
|
|
15
|
+
if (Array.isArray(section)) {
|
|
16
|
+
return NodeType.Array;
|
|
17
|
+
}
|
|
18
|
+
if (isValidElement(section)) {
|
|
19
|
+
return NodeType.ReactNode;
|
|
20
|
+
}
|
|
21
|
+
return NodeType.Object;
|
|
22
|
+
};
|
|
23
|
+
export {
|
|
24
|
+
getSectionType
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=getSectionType.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/getSectionType.ts"],"sourcesContent":["import {\n type TranslationContent,\n type EnumerationContent,\n type DictionaryValue,\n NodeType,\n} from '@intlayer/core';\nimport { isValidElement } from './isValidReactElement';\n\nexport const getSectionType = (section: DictionaryValue): NodeType => {\n if (typeof section === 'string') {\n return NodeType.Text;\n }\n\n if (\n (section as TranslationContent<unknown>)?.nodeType === NodeType.Translation\n ) {\n return NodeType.Translation;\n }\n\n if (\n (section as EnumerationContent<unknown>)?.nodeType === NodeType.Enumeration\n ) {\n return NodeType.Enumeration;\n }\n\n if (Array.isArray(section)) {\n return NodeType.Array;\n }\n\n if (isValidElement(section)) {\n return NodeType.ReactNode;\n }\n\n return NodeType.Object;\n};\n"],"mappings":"AAAA;AAAA,EAIE;AAAA,OACK;AACP,SAAS,sBAAsB;AAExB,MAAM,iBAAiB,CAAC,YAAuC;AACpE,MAAI,OAAO,YAAY,UAAU;AAC/B,WAAO,SAAS;AAAA,EAClB;AAEA,MACG,SAAyC,aAAa,SAAS,aAChE;AACA,WAAO,SAAS;AAAA,EAClB;AAEA,MACG,SAAyC,aAAa,SAAS,aAChE;AACA,WAAO,SAAS;AAAA,EAClB;AAEA,MAAI,MAAM,QAAQ,OAAO,GAAG;AAC1B,WAAO,SAAS;AAAA,EAClB;AAEA,MAAI,eAAe,OAAO,GAAG;AAC3B,WAAO,SAAS;AAAA,EAClB;AAEA,SAAO,SAAS;AAClB;","names":[]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './editDictionaryByKeyPath.mjs';
|
|
2
|
+
export * from './getDictionaryValueByKeyPath.mjs';
|
|
3
|
+
export * from './getSectionType.mjs';
|
|
4
|
+
export * from './removeDictionaryValueByKeyPath.mjs';
|
|
5
|
+
export * from './renameDictionaryValueByKeyPath.mjs';
|
|
6
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/index.ts"],"sourcesContent":["export * from './editDictionaryByKeyPath';\nexport * from './getDictionaryValueByKeyPath';\nexport * from './getSectionType';\nexport * from './removeDictionaryValueByKeyPath';\nexport * from './renameDictionaryValueByKeyPath';\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/isValidReactElement.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nconst REACT_ELEMENT_TYPE = Symbol.for('react.element');\n\n/**\n * Verifies the object is a ReactElement.\n * See https://reactjs.org/docs/react-api.html#isvalidelement\n * @param object\n * @return True if `object` is a ReactElement.\n * @final\n */\n\nexport const isValidElement = (object: any): boolean =>\n typeof object === 'object' &&\n object !== null &&\n object.$$typeof === REACT_ELEMENT_TYPE;\n"],"mappings":"AACA,MAAM,qBAAqB,OAAO,IAAI,eAAe;AAU9C,MAAM,iBAAiB,CAAC,WAC7B,OAAO,WAAW,YAClB,WAAW,QACX,OAAO,aAAa;","names":[]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { NodeType } from "@intlayer/core";
|
|
2
|
+
const removeDictionaryValueByKeyPath = (dictionaryContent, keyPath) => {
|
|
3
|
+
let currentValue = dictionaryContent;
|
|
4
|
+
let parentValue = null;
|
|
5
|
+
let lastKey = null;
|
|
6
|
+
for (const keyObj of keyPath) {
|
|
7
|
+
parentValue = currentValue;
|
|
8
|
+
if (keyObj.type === NodeType.Object || keyObj.type === NodeType.Array) {
|
|
9
|
+
lastKey = keyObj.key;
|
|
10
|
+
currentValue = currentValue[keyObj.key];
|
|
11
|
+
} else if (keyObj.type === NodeType.Translation) {
|
|
12
|
+
lastKey = NodeType.Translation;
|
|
13
|
+
currentValue = currentValue[NodeType.Translation][keyObj.key];
|
|
14
|
+
} else if (keyObj.type === NodeType.Enumeration) {
|
|
15
|
+
lastKey = NodeType.Enumeration;
|
|
16
|
+
currentValue = currentValue[NodeType.Enumeration][keyObj.key];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (parentValue && lastKey !== null) {
|
|
20
|
+
if (Array.isArray(parentValue)) {
|
|
21
|
+
parentValue.splice(lastKey, 1);
|
|
22
|
+
} else {
|
|
23
|
+
delete parentValue[lastKey];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return dictionaryContent;
|
|
27
|
+
};
|
|
28
|
+
export {
|
|
29
|
+
removeDictionaryValueByKeyPath
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=removeDictionaryValueByKeyPath.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/removeDictionaryValueByKeyPath.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport { DictionaryValue, KeyPath, NodeType } from '@intlayer/core';\n\nexport const removeDictionaryValueByKeyPath = (\n dictionaryContent: DictionaryValue,\n keyPath: KeyPath[]\n): DictionaryValue => {\n let currentValue: any = dictionaryContent;\n let parentValue: any = null;\n let lastKey: string | number | null = null;\n\n for (const keyObj of keyPath) {\n parentValue = currentValue;\n\n if (keyObj.type === NodeType.Object || keyObj.type === NodeType.Array) {\n lastKey = keyObj.key;\n currentValue = currentValue[keyObj.key];\n } else if (keyObj.type === NodeType.Translation) {\n lastKey = NodeType.Translation;\n currentValue = currentValue[NodeType.Translation][keyObj.key];\n } else if (keyObj.type === NodeType.Enumeration) {\n lastKey = NodeType.Enumeration;\n currentValue = currentValue[NodeType.Enumeration][keyObj.key];\n }\n }\n\n if (parentValue && lastKey !== null) {\n if (Array.isArray(parentValue)) {\n parentValue.splice(lastKey as unknown as number, 1);\n } else {\n delete parentValue[lastKey];\n }\n }\n\n return dictionaryContent;\n};\n"],"mappings":"AAEA,SAAmC,gBAAgB;AAE5C,MAAM,iCAAiC,CAC5C,mBACA,YACoB;AACpB,MAAI,eAAoB;AACxB,MAAI,cAAmB;AACvB,MAAI,UAAkC;AAEtC,aAAW,UAAU,SAAS;AAC5B,kBAAc;AAEd,QAAI,OAAO,SAAS,SAAS,UAAU,OAAO,SAAS,SAAS,OAAO;AACrE,gBAAU,OAAO;AACjB,qBAAe,aAAa,OAAO,GAAG;AAAA,IACxC,WAAW,OAAO,SAAS,SAAS,aAAa;AAC/C,gBAAU,SAAS;AACnB,qBAAe,aAAa,SAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAC9D,WAAW,OAAO,SAAS,SAAS,aAAa;AAC/C,gBAAU,SAAS;AACnB,qBAAe,aAAa,SAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAC9D;AAAA,EACF;AAEA,MAAI,eAAe,YAAY,MAAM;AACnC,QAAI,MAAM,QAAQ,WAAW,GAAG;AAC9B,kBAAY,OAAO,SAA8B,CAAC;AAAA,IACpD,OAAO;AACL,aAAO,YAAY,OAAO;AAAA,IAC5B;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { NodeType } from "@intlayer/core";
|
|
2
|
+
const renameDictionaryValueByKeyPath = (dictionaryContent, newKey, keyPath) => {
|
|
3
|
+
let currentValue = dictionaryContent;
|
|
4
|
+
let parentValue = null;
|
|
5
|
+
let lastKey = null;
|
|
6
|
+
for (const keyObj of keyPath) {
|
|
7
|
+
parentValue = currentValue;
|
|
8
|
+
if (keyObj.type === NodeType.Object || keyObj.type === NodeType.Array) {
|
|
9
|
+
lastKey = keyObj.key;
|
|
10
|
+
currentValue = currentValue[keyObj.key];
|
|
11
|
+
} else if (keyObj.type === NodeType.Translation) {
|
|
12
|
+
lastKey = NodeType.Translation;
|
|
13
|
+
currentValue = currentValue[NodeType.Translation][keyObj.key];
|
|
14
|
+
} else if (keyObj.type === NodeType.Enumeration) {
|
|
15
|
+
lastKey = NodeType.Enumeration;
|
|
16
|
+
currentValue = currentValue[NodeType.Enumeration][keyObj.key];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (parentValue && lastKey !== null) {
|
|
20
|
+
if (Array.isArray(parentValue)) {
|
|
21
|
+
parentValue[lastKey] = currentValue;
|
|
22
|
+
} else {
|
|
23
|
+
const newParentValue = {};
|
|
24
|
+
for (const key of Object.keys(parentValue)) {
|
|
25
|
+
if (key === lastKey) {
|
|
26
|
+
newParentValue[newKey] = currentValue;
|
|
27
|
+
} else {
|
|
28
|
+
newParentValue[key] = parentValue[key];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
Object.keys(parentValue).forEach((key) => delete parentValue[key]);
|
|
32
|
+
Object.assign(parentValue, newParentValue);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return dictionaryContent;
|
|
36
|
+
};
|
|
37
|
+
export {
|
|
38
|
+
renameDictionaryValueByKeyPath
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=renameDictionaryValueByKeyPath.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/dictionaryManipulator/renameDictionaryValueByKeyPath.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport { DictionaryValue, KeyPath, NodeType } from '@intlayer/core';\n\nexport const renameDictionaryValueByKeyPath = (\n dictionaryContent: DictionaryValue,\n newKey: string | number,\n keyPath: KeyPath[]\n): DictionaryValue => {\n let currentValue: any = dictionaryContent;\n let parentValue: any = null;\n let lastKey: string | number | null = null;\n\n for (const keyObj of keyPath) {\n parentValue = currentValue;\n\n if (keyObj.type === NodeType.Object || keyObj.type === NodeType.Array) {\n lastKey = keyObj.key;\n currentValue = currentValue[keyObj.key];\n } else if (keyObj.type === NodeType.Translation) {\n lastKey = NodeType.Translation;\n currentValue = currentValue[NodeType.Translation][keyObj.key];\n } else if (keyObj.type === NodeType.Enumeration) {\n lastKey = NodeType.Enumeration;\n currentValue = currentValue[NodeType.Enumeration][keyObj.key];\n }\n }\n\n // Assign the new value to the last key of the parent while preserving the order\n if (parentValue && lastKey !== null) {\n if (Array.isArray(parentValue)) {\n parentValue[lastKey as number] = currentValue;\n } else {\n const newParentValue: any = {};\n for (const key of Object.keys(parentValue)) {\n if (key === lastKey) {\n newParentValue[newKey] = currentValue;\n } else {\n newParentValue[key] = parentValue[key];\n }\n }\n // Replace the contents of parentValue with newParentValue\n Object.keys(parentValue).forEach((key) => delete parentValue[key]);\n Object.assign(parentValue, newParentValue);\n }\n }\n\n return dictionaryContent;\n};\n"],"mappings":"AAEA,SAAmC,gBAAgB;AAE5C,MAAM,iCAAiC,CAC5C,mBACA,QACA,YACoB;AACpB,MAAI,eAAoB;AACxB,MAAI,cAAmB;AACvB,MAAI,UAAkC;AAEtC,aAAW,UAAU,SAAS;AAC5B,kBAAc;AAEd,QAAI,OAAO,SAAS,SAAS,UAAU,OAAO,SAAS,SAAS,OAAO;AACrE,gBAAU,OAAO;AACjB,qBAAe,aAAa,OAAO,GAAG;AAAA,IACxC,WAAW,OAAO,SAAS,SAAS,aAAa;AAC/C,gBAAU,SAAS;AACnB,qBAAe,aAAa,SAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAC9D,WAAW,OAAO,SAAS,SAAS,aAAa;AAC/C,gBAAU,SAAS;AACnB,qBAAe,aAAa,SAAS,WAAW,EAAE,OAAO,GAAG;AAAA,IAC9D;AAAA,EACF;AAGA,MAAI,eAAe,YAAY,MAAM;AACnC,QAAI,MAAM,QAAQ,WAAW,GAAG;AAC9B,kBAAY,OAAiB,IAAI;AAAA,IACnC,OAAO;AACL,YAAM,iBAAsB,CAAC;AAC7B,iBAAW,OAAO,OAAO,KAAK,WAAW,GAAG;AAC1C,YAAI,QAAQ,SAAS;AACnB,yBAAe,MAAM,IAAI;AAAA,QAC3B,OAAO;AACL,yBAAe,GAAG,IAAI,YAAY,GAAG;AAAA,QACvC;AAAA,MACF;AAEA,aAAO,KAAK,WAAW,EAAE,QAAQ,CAAC,QAAQ,OAAO,YAAY,GAAG,CAAC;AACjE,aAAO,OAAO,aAAa,cAAc;AAAA,IAC3C;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './dictionaryManipulator/index.mjs';
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './dictionaryManipulator/index';\n"],"mappings":"AAAA,cAAc;","names":[]}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type DictionaryValue, type KeyPath } from '@intlayer/core';
|
|
2
|
+
export declare const editDictionaryByKeyPath: (dictionaryContent: DictionaryValue, keyPath: KeyPath[], newValue: DictionaryValue) => DictionaryValue;
|
|
3
|
+
//# sourceMappingURL=editDictionaryByKeyPath.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"editDictionaryByKeyPath.d.ts","sourceRoot":"","sources":["../../../src/dictionaryManipulator/editDictionaryByKeyPath.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,OAAO,EAAY,MAAM,gBAAgB,CAAC;AAI9E,eAAO,MAAM,uBAAuB,sBACf,eAAe,WACzB,OAAO,EAAE,YACR,eAAe,KACxB,eAgEF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getDictionaryValueByKeyPath.d.ts","sourceRoot":"","sources":["../../../src/dictionaryManipulator/getDictionaryValueByKeyPath.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,OAAO,EAAY,MAAM,gBAAgB,CAAC;AAE9E,eAAO,MAAM,2BAA2B,sBACnB,eAAe,WACzB,OAAO,EAAE,KACjB,eAcF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getSectionType.d.ts","sourceRoot":"","sources":["../../../src/dictionaryManipulator/getSectionType.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,eAAe,EACpB,QAAQ,EACT,MAAM,gBAAgB,CAAC;AAGxB,eAAO,MAAM,cAAc,YAAa,eAAe,KAAG,QA0BzD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/dictionaryManipulator/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kBAAkB,CAAC;AACjC,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verifies the object is a ReactElement.
|
|
3
|
+
* See https://reactjs.org/docs/react-api.html#isvalidelement
|
|
4
|
+
* @param object
|
|
5
|
+
* @return True if `object` is a ReactElement.
|
|
6
|
+
* @final
|
|
7
|
+
*/
|
|
8
|
+
export declare const isValidElement: (object: any) => boolean;
|
|
9
|
+
//# sourceMappingURL=isValidReactElement.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isValidReactElement.d.ts","sourceRoot":"","sources":["../../../src/dictionaryManipulator/isValidReactElement.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AAEH,eAAO,MAAM,cAAc,WAAY,GAAG,KAAG,OAGL,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"removeDictionaryValueByKeyPath.d.ts","sourceRoot":"","sources":["../../../src/dictionaryManipulator/removeDictionaryValueByKeyPath.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,OAAO,EAAY,MAAM,gBAAgB,CAAC;AAEpE,eAAO,MAAM,8BAA8B,sBACtB,eAAe,WACzB,OAAO,EAAE,KACjB,eA6BF,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { DictionaryValue, KeyPath } from '@intlayer/core';
|
|
2
|
+
export declare const renameDictionaryValueByKeyPath: (dictionaryContent: DictionaryValue, newKey: string | number, keyPath: KeyPath[]) => DictionaryValue;
|
|
3
|
+
//# sourceMappingURL=renameDictionaryValueByKeyPath.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renameDictionaryValueByKeyPath.d.ts","sourceRoot":"","sources":["../../../src/dictionaryManipulator/renameDictionaryValueByKeyPath.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,OAAO,EAAY,MAAM,gBAAgB,CAAC;AAEpE,eAAO,MAAM,8BAA8B,sBACtB,eAAe,UAC1B,MAAM,GAAG,MAAM,WACd,OAAO,EAAE,KACjB,eAwCF,CAAC"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './dictionaryManipulator/index';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/editor",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.4",
|
|
4
4
|
"private": false,
|
|
5
|
-
"description": "Provides the
|
|
5
|
+
"description": "Provides the utilities to interface the application with the Intlayer editor and manipulate dictionaries",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"intlayer",
|
|
8
8
|
"editor",
|
|
@@ -50,6 +50,9 @@
|
|
|
50
50
|
"./dist",
|
|
51
51
|
"./package.json"
|
|
52
52
|
],
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@intlayer/core": "4.0.4"
|
|
55
|
+
},
|
|
53
56
|
"devDependencies": {
|
|
54
57
|
"@types/node": "^22.10.6",
|
|
55
58
|
"@typescript-eslint/parser": "^8.20.0",
|
|
@@ -61,10 +64,13 @@
|
|
|
61
64
|
"tsup": "^8.3.5",
|
|
62
65
|
"typescript": "^5.7.3",
|
|
63
66
|
"@utils/ts-config": "1.0.4",
|
|
64
|
-
"@utils/ts-config-types": "1.0.4",
|
|
65
67
|
"@utils/eslint-config": "1.0.4",
|
|
68
|
+
"@utils/ts-config-types": "1.0.4",
|
|
66
69
|
"@utils/tsup-config": "1.0.4"
|
|
67
70
|
},
|
|
71
|
+
"peerDependencies": {
|
|
72
|
+
"@intlayer/core": "4.0.4"
|
|
73
|
+
},
|
|
68
74
|
"engines": {
|
|
69
75
|
"node": ">=14.18"
|
|
70
76
|
},
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var CrossFrameCommunicator_exports = {};
|
|
20
|
-
__export(CrossFrameCommunicator_exports, {
|
|
21
|
-
CrossFrameCommunicator: () => CrossFrameCommunicator
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(CrossFrameCommunicator_exports);
|
|
24
|
-
class CrossFrameCommunicator {
|
|
25
|
-
targetWindow;
|
|
26
|
-
targetOrigin;
|
|
27
|
-
allowedOrigins;
|
|
28
|
-
handlers;
|
|
29
|
-
constructor(options) {
|
|
30
|
-
this.targetWindow = options.targetWindow;
|
|
31
|
-
this.targetOrigin = options.targetOrigin ?? "*";
|
|
32
|
-
this.allowedOrigins = options.allowedOrigins ?? ["*"];
|
|
33
|
-
this.handlers = {};
|
|
34
|
-
window.addEventListener("message", this.handleIncomingMessage);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Clean up the event listener if needed (for single-page apps, unmounting, etc.).
|
|
38
|
-
*/
|
|
39
|
-
destroy() {
|
|
40
|
-
window.removeEventListener("message", this.handleIncomingMessage);
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Register a handler for a specific message type.
|
|
44
|
-
*/
|
|
45
|
-
on(type, handler) {
|
|
46
|
-
if (!this.handlers[type]) {
|
|
47
|
-
this.handlers[type] = [];
|
|
48
|
-
}
|
|
49
|
-
this.handlers[type].push(handler);
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Post a message to the target window.
|
|
53
|
-
* @param type A string identifying the message
|
|
54
|
-
* @param data The payload (any serializable data)
|
|
55
|
-
*/
|
|
56
|
-
sendMessage(type, data) {
|
|
57
|
-
this.targetWindow.postMessage({ type, data }, this.targetOrigin);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Internal method to dispatch incoming messages to registered handlers.
|
|
61
|
-
*/
|
|
62
|
-
handleIncomingMessage = (event) => {
|
|
63
|
-
const { data, origin, source } = event;
|
|
64
|
-
if (!data || typeof data !== "object" || !data.type) {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
if (!this.isAllowedOrigin(origin)) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
if (source !== this.targetWindow) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
const messageType = data.type;
|
|
74
|
-
if (this.handlers[messageType]) {
|
|
75
|
-
this.handlers[messageType].forEach((handler) => {
|
|
76
|
-
handler(data.data, event);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
isAllowedOrigin(origin) {
|
|
81
|
-
if (this.allowedOrigins.includes("*")) return true;
|
|
82
|
-
return this.allowedOrigins.includes(origin);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
-
0 && (module.exports = {
|
|
87
|
-
CrossFrameCommunicator
|
|
88
|
-
});
|
|
89
|
-
//# sourceMappingURL=CrossFrameCommunicator.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/CrossFrameCommunicator.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nexport interface CrossFrameMessage<T = any> {\n type: string;\n data: T;\n}\n\nexport type MessageHandler = (data: any, event: MessageEvent) => void;\n\nexport type CrossFrameCommunicatorOptions = {\n /**\n * The window to which we will post messages.\n * - In an iframe (child) app, this is usually `window.parent`.\n * - In the parent app, this is `iframe.contentWindow`.\n */\n targetWindow: Window;\n\n /**\n * The expected origin when sending messages.\n * - For example, \"https://my-parent-domain.com\" or \"https://my-child-domain.com\".\n * - Use \"*\" only if absolutely necessary, but it is not recommended.\n */\n targetOrigin?: string;\n\n /**\n * An array of allowed origins from which we accept incoming messages.\n * - If empty, no external origin is allowed.\n * - You can also add \"*\" to allow all, but that is not recommended in production.\n */\n allowedOrigins?: string[];\n};\n\nexport class CrossFrameCommunicator {\n private targetWindow: Window;\n private targetOrigin: string;\n private allowedOrigins: string[];\n private handlers: Record<string, MessageHandler[]>;\n\n constructor(options: CrossFrameCommunicatorOptions) {\n this.targetWindow = options.targetWindow;\n this.targetOrigin = options.targetOrigin ?? '*';\n this.allowedOrigins = options.allowedOrigins ?? ['*'];\n this.handlers = {};\n\n // Listen for incoming messages on the current window\n window.addEventListener('message', this.handleIncomingMessage);\n }\n\n /**\n * Clean up the event listener if needed (for single-page apps, unmounting, etc.).\n */\n public destroy() {\n window.removeEventListener('message', this.handleIncomingMessage);\n }\n\n /**\n * Register a handler for a specific message type.\n */\n public on(type: string, handler: MessageHandler) {\n if (!this.handlers[type]) {\n this.handlers[type] = [];\n }\n this.handlers[type].push(handler);\n }\n\n /**\n * Post a message to the target window.\n * @param type A string identifying the message\n * @param data The payload (any serializable data)\n */\n public sendMessage<T>(type: string, data: T) {\n // Use the targetOrigin instead of '*'\n this.targetWindow.postMessage({ type, data }, this.targetOrigin);\n }\n\n /**\n * Internal method to dispatch incoming messages to registered handlers.\n */\n private handleIncomingMessage = (event: MessageEvent) => {\n const { data, origin, source } = event;\n\n // 1) Check if the event has the structure we expect\n if (!data || typeof data !== 'object' || !data.type) {\n return;\n }\n\n // 2) Check if the origin is allowed\n if (!this.isAllowedOrigin(origin)) {\n // Optionally, you could console.warn or log this\n // console.warn(`Message from disallowed origin: ${origin}`);\n return;\n }\n\n // 3) (Optional) Check if the event source is the window we expect\n // This ensures the message is from the same Window object (iframe) we are targeting.\n // If you only care about the domain, you can skip this check.\n if (source !== this.targetWindow) {\n // console.warn('Message received from an unexpected window source');\n return;\n }\n\n // 4) Dispatch to any registered handlers\n const messageType = data.type;\n if (this.handlers[messageType]) {\n this.handlers[messageType].forEach((handler) => {\n handler(data.data, event);\n });\n }\n };\n\n private isAllowedOrigin(origin: string): boolean {\n // If the array includes \"*\", then all origins are allowed\n if (this.allowedOrigins.includes('*')) return true;\n\n // Otherwise, the origin must be in the array\n return this.allowedOrigins.includes(origin);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAgCO,MAAM,uBAAuB;AAAA,EAC1B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAER,YAAY,SAAwC;AAClD,SAAK,eAAe,QAAQ;AAC5B,SAAK,eAAe,QAAQ,gBAAgB;AAC5C,SAAK,iBAAiB,QAAQ,kBAAkB,CAAC,GAAG;AACpD,SAAK,WAAW,CAAC;AAGjB,WAAO,iBAAiB,WAAW,KAAK,qBAAqB;AAAA,EAC/D;AAAA;AAAA;AAAA;AAAA,EAKO,UAAU;AACf,WAAO,oBAAoB,WAAW,KAAK,qBAAqB;AAAA,EAClE;AAAA;AAAA;AAAA;AAAA,EAKO,GAAG,MAAc,SAAyB;AAC/C,QAAI,CAAC,KAAK,SAAS,IAAI,GAAG;AACxB,WAAK,SAAS,IAAI,IAAI,CAAC;AAAA,IACzB;AACA,SAAK,SAAS,IAAI,EAAE,KAAK,OAAO;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,YAAe,MAAc,MAAS;AAE3C,SAAK,aAAa,YAAY,EAAE,MAAM,KAAK,GAAG,KAAK,YAAY;AAAA,EACjE;AAAA;AAAA;AAAA;AAAA,EAKQ,wBAAwB,CAAC,UAAwB;AACvD,UAAM,EAAE,MAAM,QAAQ,OAAO,IAAI;AAGjC,QAAI,CAAC,QAAQ,OAAO,SAAS,YAAY,CAAC,KAAK,MAAM;AACnD;AAAA,IACF;AAGA,QAAI,CAAC,KAAK,gBAAgB,MAAM,GAAG;AAGjC;AAAA,IACF;AAKA,QAAI,WAAW,KAAK,cAAc;AAEhC;AAAA,IACF;AAGA,UAAM,cAAc,KAAK;AACzB,QAAI,KAAK,SAAS,WAAW,GAAG;AAC9B,WAAK,SAAS,WAAW,EAAE,QAAQ,CAAC,YAAY;AAC9C,gBAAQ,KAAK,MAAM,KAAK;AAAA,MAC1B,CAAC;AAAA,IACH;AAAA,EACF;AAAA,EAEQ,gBAAgB,QAAyB;AAE/C,QAAI,KAAK,eAAe,SAAS,GAAG,EAAG,QAAO;AAG9C,WAAO,KAAK,eAAe,SAAS,MAAM;AAAA,EAC5C;AACF;","names":[]}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
class CrossFrameCommunicator {
|
|
2
|
-
targetWindow;
|
|
3
|
-
targetOrigin;
|
|
4
|
-
allowedOrigins;
|
|
5
|
-
handlers;
|
|
6
|
-
constructor(options) {
|
|
7
|
-
this.targetWindow = options.targetWindow;
|
|
8
|
-
this.targetOrigin = options.targetOrigin ?? "*";
|
|
9
|
-
this.allowedOrigins = options.allowedOrigins ?? ["*"];
|
|
10
|
-
this.handlers = {};
|
|
11
|
-
window.addEventListener("message", this.handleIncomingMessage);
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Clean up the event listener if needed (for single-page apps, unmounting, etc.).
|
|
15
|
-
*/
|
|
16
|
-
destroy() {
|
|
17
|
-
window.removeEventListener("message", this.handleIncomingMessage);
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Register a handler for a specific message type.
|
|
21
|
-
*/
|
|
22
|
-
on(type, handler) {
|
|
23
|
-
if (!this.handlers[type]) {
|
|
24
|
-
this.handlers[type] = [];
|
|
25
|
-
}
|
|
26
|
-
this.handlers[type].push(handler);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Post a message to the target window.
|
|
30
|
-
* @param type A string identifying the message
|
|
31
|
-
* @param data The payload (any serializable data)
|
|
32
|
-
*/
|
|
33
|
-
sendMessage(type, data) {
|
|
34
|
-
this.targetWindow.postMessage({ type, data }, this.targetOrigin);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Internal method to dispatch incoming messages to registered handlers.
|
|
38
|
-
*/
|
|
39
|
-
handleIncomingMessage = (event) => {
|
|
40
|
-
const { data, origin, source } = event;
|
|
41
|
-
if (!data || typeof data !== "object" || !data.type) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
if (!this.isAllowedOrigin(origin)) {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
if (source !== this.targetWindow) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
const messageType = data.type;
|
|
51
|
-
if (this.handlers[messageType]) {
|
|
52
|
-
this.handlers[messageType].forEach((handler) => {
|
|
53
|
-
handler(data.data, event);
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
isAllowedOrigin(origin) {
|
|
58
|
-
if (this.allowedOrigins.includes("*")) return true;
|
|
59
|
-
return this.allowedOrigins.includes(origin);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
export {
|
|
63
|
-
CrossFrameCommunicator
|
|
64
|
-
};
|
|
65
|
-
//# sourceMappingURL=CrossFrameCommunicator.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/CrossFrameCommunicator.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nexport interface CrossFrameMessage<T = any> {\n type: string;\n data: T;\n}\n\nexport type MessageHandler = (data: any, event: MessageEvent) => void;\n\nexport type CrossFrameCommunicatorOptions = {\n /**\n * The window to which we will post messages.\n * - In an iframe (child) app, this is usually `window.parent`.\n * - In the parent app, this is `iframe.contentWindow`.\n */\n targetWindow: Window;\n\n /**\n * The expected origin when sending messages.\n * - For example, \"https://my-parent-domain.com\" or \"https://my-child-domain.com\".\n * - Use \"*\" only if absolutely necessary, but it is not recommended.\n */\n targetOrigin?: string;\n\n /**\n * An array of allowed origins from which we accept incoming messages.\n * - If empty, no external origin is allowed.\n * - You can also add \"*\" to allow all, but that is not recommended in production.\n */\n allowedOrigins?: string[];\n};\n\nexport class CrossFrameCommunicator {\n private targetWindow: Window;\n private targetOrigin: string;\n private allowedOrigins: string[];\n private handlers: Record<string, MessageHandler[]>;\n\n constructor(options: CrossFrameCommunicatorOptions) {\n this.targetWindow = options.targetWindow;\n this.targetOrigin = options.targetOrigin ?? '*';\n this.allowedOrigins = options.allowedOrigins ?? ['*'];\n this.handlers = {};\n\n // Listen for incoming messages on the current window\n window.addEventListener('message', this.handleIncomingMessage);\n }\n\n /**\n * Clean up the event listener if needed (for single-page apps, unmounting, etc.).\n */\n public destroy() {\n window.removeEventListener('message', this.handleIncomingMessage);\n }\n\n /**\n * Register a handler for a specific message type.\n */\n public on(type: string, handler: MessageHandler) {\n if (!this.handlers[type]) {\n this.handlers[type] = [];\n }\n this.handlers[type].push(handler);\n }\n\n /**\n * Post a message to the target window.\n * @param type A string identifying the message\n * @param data The payload (any serializable data)\n */\n public sendMessage<T>(type: string, data: T) {\n // Use the targetOrigin instead of '*'\n this.targetWindow.postMessage({ type, data }, this.targetOrigin);\n }\n\n /**\n * Internal method to dispatch incoming messages to registered handlers.\n */\n private handleIncomingMessage = (event: MessageEvent) => {\n const { data, origin, source } = event;\n\n // 1) Check if the event has the structure we expect\n if (!data || typeof data !== 'object' || !data.type) {\n return;\n }\n\n // 2) Check if the origin is allowed\n if (!this.isAllowedOrigin(origin)) {\n // Optionally, you could console.warn or log this\n // console.warn(`Message from disallowed origin: ${origin}`);\n return;\n }\n\n // 3) (Optional) Check if the event source is the window we expect\n // This ensures the message is from the same Window object (iframe) we are targeting.\n // If you only care about the domain, you can skip this check.\n if (source !== this.targetWindow) {\n // console.warn('Message received from an unexpected window source');\n return;\n }\n\n // 4) Dispatch to any registered handlers\n const messageType = data.type;\n if (this.handlers[messageType]) {\n this.handlers[messageType].forEach((handler) => {\n handler(data.data, event);\n });\n }\n };\n\n private isAllowedOrigin(origin: string): boolean {\n // If the array includes \"*\", then all origins are allowed\n if (this.allowedOrigins.includes('*')) return true;\n\n // Otherwise, the origin must be in the array\n return this.allowedOrigins.includes(origin);\n }\n}\n"],"mappings":"AAgCO,MAAM,uBAAuB;AAAA,EAC1B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAER,YAAY,SAAwC;AAClD,SAAK,eAAe,QAAQ;AAC5B,SAAK,eAAe,QAAQ,gBAAgB;AAC5C,SAAK,iBAAiB,QAAQ,kBAAkB,CAAC,GAAG;AACpD,SAAK,WAAW,CAAC;AAGjB,WAAO,iBAAiB,WAAW,KAAK,qBAAqB;AAAA,EAC/D;AAAA;AAAA;AAAA;AAAA,EAKO,UAAU;AACf,WAAO,oBAAoB,WAAW,KAAK,qBAAqB;AAAA,EAClE;AAAA;AAAA;AAAA;AAAA,EAKO,GAAG,MAAc,SAAyB;AAC/C,QAAI,CAAC,KAAK,SAAS,IAAI,GAAG;AACxB,WAAK,SAAS,IAAI,IAAI,CAAC;AAAA,IACzB;AACA,SAAK,SAAS,IAAI,EAAE,KAAK,OAAO;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,YAAe,MAAc,MAAS;AAE3C,SAAK,aAAa,YAAY,EAAE,MAAM,KAAK,GAAG,KAAK,YAAY;AAAA,EACjE;AAAA;AAAA;AAAA;AAAA,EAKQ,wBAAwB,CAAC,UAAwB;AACvD,UAAM,EAAE,MAAM,QAAQ,OAAO,IAAI;AAGjC,QAAI,CAAC,QAAQ,OAAO,SAAS,YAAY,CAAC,KAAK,MAAM;AACnD;AAAA,IACF;AAGA,QAAI,CAAC,KAAK,gBAAgB,MAAM,GAAG;AAGjC;AAAA,IACF;AAKA,QAAI,WAAW,KAAK,cAAc;AAEhC;AAAA,IACF;AAGA,UAAM,cAAc,KAAK;AACzB,QAAI,KAAK,SAAS,WAAW,GAAG;AAC9B,WAAK,SAAS,WAAW,EAAE,QAAQ,CAAC,YAAY;AAC9C,gBAAQ,KAAK,MAAM,KAAK;AAAA,MAC1B,CAAC;AAAA,IACH;AAAA,EACF;AAAA,EAEQ,gBAAgB,QAAyB;AAE/C,QAAI,KAAK,eAAe,SAAS,GAAG,EAAG,QAAO;AAG9C,WAAO,KAAK,eAAe,SAAS,MAAM;AAAA,EAC5C;AACF;","names":[]}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
export interface CrossFrameMessage<T = any> {
|
|
2
|
-
type: string;
|
|
3
|
-
data: T;
|
|
4
|
-
}
|
|
5
|
-
export type MessageHandler = (data: any, event: MessageEvent) => void;
|
|
6
|
-
export type CrossFrameCommunicatorOptions = {
|
|
7
|
-
/**
|
|
8
|
-
* The window to which we will post messages.
|
|
9
|
-
* - In an iframe (child) app, this is usually `window.parent`.
|
|
10
|
-
* - In the parent app, this is `iframe.contentWindow`.
|
|
11
|
-
*/
|
|
12
|
-
targetWindow: Window;
|
|
13
|
-
/**
|
|
14
|
-
* The expected origin when sending messages.
|
|
15
|
-
* - For example, "https://my-parent-domain.com" or "https://my-child-domain.com".
|
|
16
|
-
* - Use "*" only if absolutely necessary, but it is not recommended.
|
|
17
|
-
*/
|
|
18
|
-
targetOrigin?: string;
|
|
19
|
-
/**
|
|
20
|
-
* An array of allowed origins from which we accept incoming messages.
|
|
21
|
-
* - If empty, no external origin is allowed.
|
|
22
|
-
* - You can also add "*" to allow all, but that is not recommended in production.
|
|
23
|
-
*/
|
|
24
|
-
allowedOrigins?: string[];
|
|
25
|
-
};
|
|
26
|
-
export declare class CrossFrameCommunicator {
|
|
27
|
-
private targetWindow;
|
|
28
|
-
private targetOrigin;
|
|
29
|
-
private allowedOrigins;
|
|
30
|
-
private handlers;
|
|
31
|
-
constructor(options: CrossFrameCommunicatorOptions);
|
|
32
|
-
/**
|
|
33
|
-
* Clean up the event listener if needed (for single-page apps, unmounting, etc.).
|
|
34
|
-
*/
|
|
35
|
-
destroy(): void;
|
|
36
|
-
/**
|
|
37
|
-
* Register a handler for a specific message type.
|
|
38
|
-
*/
|
|
39
|
-
on(type: string, handler: MessageHandler): void;
|
|
40
|
-
/**
|
|
41
|
-
* Post a message to the target window.
|
|
42
|
-
* @param type A string identifying the message
|
|
43
|
-
* @param data The payload (any serializable data)
|
|
44
|
-
*/
|
|
45
|
-
sendMessage<T>(type: string, data: T): void;
|
|
46
|
-
/**
|
|
47
|
-
* Internal method to dispatch incoming messages to registered handlers.
|
|
48
|
-
*/
|
|
49
|
-
private handleIncomingMessage;
|
|
50
|
-
private isAllowedOrigin;
|
|
51
|
-
}
|
|
52
|
-
//# sourceMappingURL=CrossFrameCommunicator.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CrossFrameCommunicator.d.ts","sourceRoot":"","sources":["../../src/CrossFrameCommunicator.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,iBAAiB,CAAC,CAAC,GAAG,GAAG;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,CAAC,CAAC;CACT;AAED,MAAM,MAAM,cAAc,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,YAAY,KAAK,IAAI,CAAC;AAEtE,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;;;OAIG;IACH,YAAY,EAAE,MAAM,CAAC;IAErB;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;CAC3B,CAAC;AAEF,qBAAa,sBAAsB;IACjC,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,cAAc,CAAW;IACjC,OAAO,CAAC,QAAQ,CAAmC;gBAEvC,OAAO,EAAE,6BAA6B;IAUlD;;OAEG;IACI,OAAO;IAId;;OAEG;IACI,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc;IAO/C;;;;OAIG;IACI,WAAW,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;IAK3C;;OAEG;IACH,OAAO,CAAC,qBAAqB,CA8B3B;IAEF,OAAO,CAAC,eAAe;CAOxB"}
|