@uniformdev/tms-sdk 19.154.1-alpha.27 → 19.154.1-alpha.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.esm.js +248 -28
- package/dist/index.js +250 -30
- package/dist/index.mjs +248 -28
- package/package.json +6 -5
package/dist/index.esm.js
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
bindVariables,
|
|
4
4
|
walkNodeTree as walkNodeTree2
|
|
5
5
|
} from "@uniformdev/canvas";
|
|
6
|
-
import { isRichTextValue, isRichTextValueConsideredEmpty as isRichTextValueConsideredEmpty2 } from "@uniformdev/richtext";
|
|
6
|
+
import { isRichTextValue as isRichTextValue2, isRichTextValueConsideredEmpty as isRichTextValueConsideredEmpty2 } from "@uniformdev/richtext";
|
|
7
7
|
|
|
8
8
|
// src/constants.ts
|
|
9
9
|
var TRANSLATION_PAYLOAD_SOURCE_KEY = "source";
|
|
@@ -14,12 +14,219 @@ var SUPPORTED_PARAM_TYPES = {
|
|
|
14
14
|
richText: "richText"
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
+
// src/richText/renderAsTranslatableXml.ts
|
|
18
|
+
import { getRichTextTagsFromTextFormat, isRichTextNodeType } from "@uniformdev/richtext";
|
|
19
|
+
var REF_ATTRIBUTE_NAME = "ref";
|
|
20
|
+
function renderAsTranslatableXml(root) {
|
|
21
|
+
if (!root) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
const result = {
|
|
25
|
+
attributes: [],
|
|
26
|
+
xmlParts: []
|
|
27
|
+
};
|
|
28
|
+
renderXmlNode(result, root);
|
|
29
|
+
const metadata = result.attributes.reduce((result2, current, index) => {
|
|
30
|
+
result2[String(index)] = {
|
|
31
|
+
attrs: current
|
|
32
|
+
};
|
|
33
|
+
return result2;
|
|
34
|
+
}, {});
|
|
35
|
+
return {
|
|
36
|
+
metadata,
|
|
37
|
+
xml: result.xmlParts.join("")
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
var IGNORE_ATTRS = ["type", "children", "text"];
|
|
41
|
+
function renderXmlNode(context, node) {
|
|
42
|
+
if (!node) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const base64Props = getPropsAsBase64(node, IGNORE_ATTRS);
|
|
46
|
+
let refKey = context.attributes.findIndex((x) => x === base64Props);
|
|
47
|
+
if (refKey == -1) {
|
|
48
|
+
context.attributes.push(base64Props);
|
|
49
|
+
refKey = context.attributes.length - 1;
|
|
50
|
+
}
|
|
51
|
+
context.xmlParts.push(`<${node.type} ${REF_ATTRIBUTE_NAME}="${refKey}"`);
|
|
52
|
+
const metaAttrsString = getNodeMetaAttributesString(node);
|
|
53
|
+
if (metaAttrsString) {
|
|
54
|
+
context.xmlParts.push(" " + metaAttrsString);
|
|
55
|
+
}
|
|
56
|
+
context.xmlParts.push(">");
|
|
57
|
+
if (isRichTextNodeType(node, "text")) {
|
|
58
|
+
context.xmlParts.push(node.text);
|
|
59
|
+
} else if (Array.isArray(node.children) && node.children.length) {
|
|
60
|
+
node.children.forEach((child) => {
|
|
61
|
+
renderXmlNode(context, child);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
context.xmlParts.push(`</${node.type}>`);
|
|
65
|
+
}
|
|
66
|
+
var getPropsAsBase64 = (node, ignoreProps) => {
|
|
67
|
+
const props = {};
|
|
68
|
+
Object.keys(node).forEach((key) => {
|
|
69
|
+
if (!ignoreProps.includes(key)) {
|
|
70
|
+
props[key] = node[key];
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
return btoa(JSON.stringify(props));
|
|
74
|
+
};
|
|
75
|
+
var getNodeMetaAttributesString = (node) => {
|
|
76
|
+
const metadata = {};
|
|
77
|
+
if (isRichTextNodeType(node, "text")) {
|
|
78
|
+
const formatTags = getRichTextTagsFromTextFormat(node.format);
|
|
79
|
+
if (formatTags.length) {
|
|
80
|
+
metadata["format"] = formatTags.join(" ");
|
|
81
|
+
}
|
|
82
|
+
} else if (isRichTextNodeType(node, "heading")) {
|
|
83
|
+
if (node.tag) {
|
|
84
|
+
metadata["tag"] = node.tag;
|
|
85
|
+
}
|
|
86
|
+
} else if (isRichTextNodeType(node, "paragraph")) {
|
|
87
|
+
if (node.format) {
|
|
88
|
+
metadata["format"] = node.format;
|
|
89
|
+
}
|
|
90
|
+
} else if (isRichTextNodeType(node, "list")) {
|
|
91
|
+
if (node.tag) {
|
|
92
|
+
metadata["tag"] = node.tag;
|
|
93
|
+
}
|
|
94
|
+
} else if (isRichTextNodeType(node, "listitem")) {
|
|
95
|
+
metadata["value"] = String(node.value);
|
|
96
|
+
}
|
|
97
|
+
if (!Object.keys(metadata).length) {
|
|
98
|
+
return "";
|
|
99
|
+
}
|
|
100
|
+
const result = [];
|
|
101
|
+
Object.entries(metadata).forEach(([key, value]) => {
|
|
102
|
+
result.push(`${key}="${value}"`);
|
|
103
|
+
});
|
|
104
|
+
return result.join(" ");
|
|
105
|
+
};
|
|
106
|
+
|
|
17
107
|
// src/translationHelpers.ts
|
|
18
108
|
import {
|
|
19
109
|
walkNodeTree
|
|
20
110
|
} from "@uniformdev/canvas";
|
|
21
111
|
import { isRichTextValueConsideredEmpty } from "@uniformdev/richtext";
|
|
22
112
|
import { dequal } from "dequal/lite";
|
|
113
|
+
|
|
114
|
+
// src/richText/parseTranslatableXml.ts
|
|
115
|
+
import { isRichTextValue } from "@uniformdev/richtext";
|
|
116
|
+
import { XMLParser } from "fast-xml-parser";
|
|
117
|
+
var ATTR_NAME_PREFIX = "@_";
|
|
118
|
+
var ATTRIBUTES_SUBNODE_NAME = ":@";
|
|
119
|
+
var IGNORE_PROPS_PREFIX = [ATTR_NAME_PREFIX, ATTRIBUTES_SUBNODE_NAME, "#"];
|
|
120
|
+
var parseTranslatableXml = ({ xml, metadata }) => {
|
|
121
|
+
const parser = new XMLParser({
|
|
122
|
+
ignoreDeclaration: true,
|
|
123
|
+
ignorePiTags: true,
|
|
124
|
+
ignoreAttributes: false,
|
|
125
|
+
preserveOrder: true,
|
|
126
|
+
trimValues: false,
|
|
127
|
+
attributeNamePrefix: ATTR_NAME_PREFIX
|
|
128
|
+
});
|
|
129
|
+
const rawObj = parser.parse(prepareXml(xml));
|
|
130
|
+
const result = processParseObject(rawObj, {
|
|
131
|
+
metadata,
|
|
132
|
+
nodeTypes: []
|
|
133
|
+
});
|
|
134
|
+
return result;
|
|
135
|
+
};
|
|
136
|
+
var prepareXml = (xml) => {
|
|
137
|
+
const result = xml.replaceAll("</text>", "</text>");
|
|
138
|
+
return result;
|
|
139
|
+
};
|
|
140
|
+
var processParseObject = (rawObj, context) => {
|
|
141
|
+
const result = visitAndTransformNode(rawObj, context);
|
|
142
|
+
const root = Array.isArray(result) ? result.at(0) : result;
|
|
143
|
+
if (root && root.type === "root") {
|
|
144
|
+
const value = { root };
|
|
145
|
+
if (isRichTextValue(value)) {
|
|
146
|
+
return value;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return null;
|
|
150
|
+
};
|
|
151
|
+
var visitAndTransformNode = (node, context) => {
|
|
152
|
+
if (Array.isArray(node)) {
|
|
153
|
+
return node.map((x) => visitAndTransformNode(x, context)).filter((node2) => !!node2);
|
|
154
|
+
} else if (typeof node === "object" && node !== null) {
|
|
155
|
+
const type = Object.keys(node).find(
|
|
156
|
+
(key) => !IGNORE_PROPS_PREFIX.some((prefix) => key.startsWith(prefix))
|
|
157
|
+
);
|
|
158
|
+
if (!type) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
const transformed = {};
|
|
162
|
+
transformed["type"] = type;
|
|
163
|
+
const propsFromRef = restorePropsFromRef(node, context);
|
|
164
|
+
Object.entries(propsFromRef).forEach(([key, value]) => {
|
|
165
|
+
transformed[key] = value;
|
|
166
|
+
});
|
|
167
|
+
if (type === "text") {
|
|
168
|
+
const nestedNode = node[type];
|
|
169
|
+
const nestedNodeAsArray = Array.isArray(nestedNode) ? nestedNode : [nestedNode];
|
|
170
|
+
const firstChild = nestedNodeAsArray.at(0);
|
|
171
|
+
const text = firstChild == null ? void 0 : firstChild["#text"];
|
|
172
|
+
transformed["text"] = typeof text === "string" && text ? text : "";
|
|
173
|
+
} else {
|
|
174
|
+
const children = [];
|
|
175
|
+
const nestedNode = node[type];
|
|
176
|
+
if (typeof nestedNode === "object" && nestedNode) {
|
|
177
|
+
const nestedNodeAsArray = Array.isArray(nestedNode) ? nestedNode : [nestedNode];
|
|
178
|
+
nestedNodeAsArray.forEach((currNode) => {
|
|
179
|
+
const transformed2 = visitAndTransformNode(currNode, context);
|
|
180
|
+
if (transformed2) {
|
|
181
|
+
children.push(transformed2);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
if (children.length) {
|
|
186
|
+
transformed["children"] = children;
|
|
187
|
+
} else if (type === "paragraph" && Array.isArray(nestedNode) && !nestedNode.length) {
|
|
188
|
+
transformed["children"] = [];
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return transformed;
|
|
192
|
+
} else {
|
|
193
|
+
return node;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
var restorePropsFromRef = (node, context) => {
|
|
197
|
+
const result = {};
|
|
198
|
+
let uniformRef = node[ATTR_NAME_PREFIX + REF_ATTRIBUTE_NAME];
|
|
199
|
+
if (!uniformRef) {
|
|
200
|
+
const attrsSubNode = node[ATTRIBUTES_SUBNODE_NAME];
|
|
201
|
+
uniformRef = attrsSubNode == null ? void 0 : attrsSubNode[ATTR_NAME_PREFIX + REF_ATTRIBUTE_NAME];
|
|
202
|
+
}
|
|
203
|
+
if (typeof uniformRef === "string" && uniformRef && context.metadata[uniformRef]) {
|
|
204
|
+
const metadata = context.metadata[uniformRef];
|
|
205
|
+
if (metadata.attrs) {
|
|
206
|
+
const decodedProps = JSON.parse(atob(metadata.attrs));
|
|
207
|
+
Object.keys(decodedProps).forEach((key) => {
|
|
208
|
+
result[key] = decodedProps[key];
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// src/richText/types.ts
|
|
216
|
+
var isTranslatableRichTextValue = (value) => {
|
|
217
|
+
if (typeof value !== "object" || !value) {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
if (!("xml" in value) || typeof value.xml !== "string") {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
if (!("metadata" in value) || typeof value.metadata !== "object" || !value.metadata) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
return true;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
// src/translationHelpers.ts
|
|
23
230
|
var MERGE_TRANSLATION_ERRORS = {
|
|
24
231
|
unknown: "Unknown error",
|
|
25
232
|
"invalid-args": "Invalid arguments",
|
|
@@ -103,9 +310,19 @@ var processParameterTranslation = ({
|
|
|
103
310
|
left: originalParameter.locales[uniformTargetLocale],
|
|
104
311
|
right: originalTargetValue
|
|
105
312
|
});
|
|
106
|
-
if (targetValue && isSourceValueUntouched && isTargetValueUntouched) {
|
|
107
|
-
|
|
108
|
-
|
|
313
|
+
if (targetValue !== void 0 && isSourceValueUntouched && isTargetValueUntouched) {
|
|
314
|
+
if (parameter.type == SUPPORTED_PARAM_TYPES.richText && isTranslatableRichTextValue(targetValue)) {
|
|
315
|
+
try {
|
|
316
|
+
const richTextValue = parseTranslatableXml(targetValue);
|
|
317
|
+
originalParameter.locales[uniformTargetLocale] = richTextValue;
|
|
318
|
+
return { updated: true };
|
|
319
|
+
} catch (e) {
|
|
320
|
+
return { updated: false };
|
|
321
|
+
}
|
|
322
|
+
} else if (parameter.type == SUPPORTED_PARAM_TYPES.text) {
|
|
323
|
+
originalParameter.locales[uniformTargetLocale] = targetValue;
|
|
324
|
+
return { updated: true };
|
|
325
|
+
}
|
|
109
326
|
}
|
|
110
327
|
return { updated: false };
|
|
111
328
|
};
|
|
@@ -118,29 +335,18 @@ var processOverrideTranslation = ({
|
|
|
118
335
|
var _a, _b;
|
|
119
336
|
let updated = false;
|
|
120
337
|
Object.entries((_a = override.parameters) != null ? _a : {}).forEach(([paramKey, parameter]) => {
|
|
121
|
-
var _a2
|
|
338
|
+
var _a2;
|
|
122
339
|
const originalParameter = (_a2 = originalOverride.parameters) == null ? void 0 : _a2[paramKey];
|
|
123
|
-
if (!originalParameter
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
if (originalParameter.type !== parameter.type) {
|
|
340
|
+
if (!originalParameter) {
|
|
127
341
|
return;
|
|
128
342
|
}
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
left: originalParameter.locales[uniformSourceLocale],
|
|
135
|
-
right: sourceValue
|
|
136
|
-
});
|
|
137
|
-
const isTargetValueUntouched = isSameParameterValue({
|
|
138
|
-
parameterType: originalParameter.type,
|
|
139
|
-
left: originalParameter.locales[uniformTargetLocale],
|
|
140
|
-
right: originalTargetValue
|
|
343
|
+
const result = processParameterTranslation({
|
|
344
|
+
uniformSourceLocale,
|
|
345
|
+
uniformTargetLocale,
|
|
346
|
+
originalParameter,
|
|
347
|
+
parameter
|
|
141
348
|
});
|
|
142
|
-
if (
|
|
143
|
-
originalParameter.locales[uniformTargetLocale] = targetValue;
|
|
349
|
+
if (result.updated) {
|
|
144
350
|
updated = true;
|
|
145
351
|
}
|
|
146
352
|
});
|
|
@@ -194,8 +400,12 @@ var isSameParameterValue = ({
|
|
|
194
400
|
return left === right || !left && !right;
|
|
195
401
|
break;
|
|
196
402
|
case SUPPORTED_PARAM_TYPES.richText:
|
|
197
|
-
|
|
198
|
-
|
|
403
|
+
try {
|
|
404
|
+
if (isRichTextValueConsideredEmpty(left) && isRichTextValueConsideredEmpty(right)) {
|
|
405
|
+
return true;
|
|
406
|
+
}
|
|
407
|
+
} catch (e) {
|
|
408
|
+
return false;
|
|
199
409
|
}
|
|
200
410
|
return dequal(left, right);
|
|
201
411
|
break;
|
|
@@ -302,7 +512,7 @@ var collectFromNode = ({
|
|
|
302
512
|
type: param.type,
|
|
303
513
|
locales: {
|
|
304
514
|
[TRANSLATION_PAYLOAD_SOURCE_KEY]: sourceLocaleValue,
|
|
305
|
-
[TRANSLATION_PAYLOAD_TARGET_KEY]: sourceLocaleValue,
|
|
515
|
+
[TRANSLATION_PAYLOAD_TARGET_KEY]: preprocessTargetValue(param, sourceLocaleValue),
|
|
306
516
|
[TRANSLATION_PAYLOAD_ORIGINAL_TARGET_KEY]: targetLocaleValue
|
|
307
517
|
}
|
|
308
518
|
};
|
|
@@ -346,7 +556,7 @@ var collectFromNode = ({
|
|
|
346
556
|
type: param.type,
|
|
347
557
|
locales: {
|
|
348
558
|
[TRANSLATION_PAYLOAD_SOURCE_KEY]: sourceLocaleValue,
|
|
349
|
-
[TRANSLATION_PAYLOAD_TARGET_KEY]: sourceLocaleValue,
|
|
559
|
+
[TRANSLATION_PAYLOAD_TARGET_KEY]: preprocessTargetValue(param, sourceLocaleValue),
|
|
350
560
|
[TRANSLATION_PAYLOAD_ORIGINAL_TARGET_KEY]: targetLocaleValue
|
|
351
561
|
}
|
|
352
562
|
};
|
|
@@ -405,7 +615,7 @@ var canTranslateParameterValue = (parameter, value) => {
|
|
|
405
615
|
}
|
|
406
616
|
return true;
|
|
407
617
|
} else if (parameter.type === SUPPORTED_PARAM_TYPES.richText) {
|
|
408
|
-
return
|
|
618
|
+
return isRichTextValue2(value) && !isRichTextValueConsideredEmpty2(value);
|
|
409
619
|
}
|
|
410
620
|
return false;
|
|
411
621
|
};
|
|
@@ -425,6 +635,16 @@ var isTargetLocaleUntouched = (parameterType, sourceLocaleValue, targetLocaleVal
|
|
|
425
635
|
right: targetLocaleValue
|
|
426
636
|
});
|
|
427
637
|
};
|
|
638
|
+
var preprocessTargetValue = (parameter, value) => {
|
|
639
|
+
if (parameter.type === SUPPORTED_PARAM_TYPES.richText) {
|
|
640
|
+
try {
|
|
641
|
+
return isRichTextValue2(value) ? renderAsTranslatableXml(value.root) : value;
|
|
642
|
+
} catch (e) {
|
|
643
|
+
return value;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return value;
|
|
647
|
+
};
|
|
428
648
|
|
|
429
649
|
// src/getCompositionForTranslation.ts
|
|
430
650
|
import { CANVAS_DRAFT_STATE } from "@uniformdev/canvas";
|
package/dist/index.js
CHANGED
|
@@ -33,7 +33,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
33
33
|
|
|
34
34
|
// src/collectTranslationPayload.ts
|
|
35
35
|
var import_canvas2 = require("@uniformdev/canvas");
|
|
36
|
-
var
|
|
36
|
+
var import_richtext4 = require("@uniformdev/richtext");
|
|
37
37
|
|
|
38
38
|
// src/constants.ts
|
|
39
39
|
var TRANSLATION_PAYLOAD_SOURCE_KEY = "source";
|
|
@@ -44,10 +44,217 @@ var SUPPORTED_PARAM_TYPES = {
|
|
|
44
44
|
richText: "richText"
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
+
// src/richText/renderAsTranslatableXml.ts
|
|
48
|
+
var import_richtext = require("@uniformdev/richtext");
|
|
49
|
+
var REF_ATTRIBUTE_NAME = "ref";
|
|
50
|
+
function renderAsTranslatableXml(root) {
|
|
51
|
+
if (!root) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
const result = {
|
|
55
|
+
attributes: [],
|
|
56
|
+
xmlParts: []
|
|
57
|
+
};
|
|
58
|
+
renderXmlNode(result, root);
|
|
59
|
+
const metadata = result.attributes.reduce((result2, current, index) => {
|
|
60
|
+
result2[String(index)] = {
|
|
61
|
+
attrs: current
|
|
62
|
+
};
|
|
63
|
+
return result2;
|
|
64
|
+
}, {});
|
|
65
|
+
return {
|
|
66
|
+
metadata,
|
|
67
|
+
xml: result.xmlParts.join("")
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
var IGNORE_ATTRS = ["type", "children", "text"];
|
|
71
|
+
function renderXmlNode(context, node) {
|
|
72
|
+
if (!node) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const base64Props = getPropsAsBase64(node, IGNORE_ATTRS);
|
|
76
|
+
let refKey = context.attributes.findIndex((x) => x === base64Props);
|
|
77
|
+
if (refKey == -1) {
|
|
78
|
+
context.attributes.push(base64Props);
|
|
79
|
+
refKey = context.attributes.length - 1;
|
|
80
|
+
}
|
|
81
|
+
context.xmlParts.push(`<${node.type} ${REF_ATTRIBUTE_NAME}="${refKey}"`);
|
|
82
|
+
const metaAttrsString = getNodeMetaAttributesString(node);
|
|
83
|
+
if (metaAttrsString) {
|
|
84
|
+
context.xmlParts.push(" " + metaAttrsString);
|
|
85
|
+
}
|
|
86
|
+
context.xmlParts.push(">");
|
|
87
|
+
if ((0, import_richtext.isRichTextNodeType)(node, "text")) {
|
|
88
|
+
context.xmlParts.push(node.text);
|
|
89
|
+
} else if (Array.isArray(node.children) && node.children.length) {
|
|
90
|
+
node.children.forEach((child) => {
|
|
91
|
+
renderXmlNode(context, child);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
context.xmlParts.push(`</${node.type}>`);
|
|
95
|
+
}
|
|
96
|
+
var getPropsAsBase64 = (node, ignoreProps) => {
|
|
97
|
+
const props = {};
|
|
98
|
+
Object.keys(node).forEach((key) => {
|
|
99
|
+
if (!ignoreProps.includes(key)) {
|
|
100
|
+
props[key] = node[key];
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return btoa(JSON.stringify(props));
|
|
104
|
+
};
|
|
105
|
+
var getNodeMetaAttributesString = (node) => {
|
|
106
|
+
const metadata = {};
|
|
107
|
+
if ((0, import_richtext.isRichTextNodeType)(node, "text")) {
|
|
108
|
+
const formatTags = (0, import_richtext.getRichTextTagsFromTextFormat)(node.format);
|
|
109
|
+
if (formatTags.length) {
|
|
110
|
+
metadata["format"] = formatTags.join(" ");
|
|
111
|
+
}
|
|
112
|
+
} else if ((0, import_richtext.isRichTextNodeType)(node, "heading")) {
|
|
113
|
+
if (node.tag) {
|
|
114
|
+
metadata["tag"] = node.tag;
|
|
115
|
+
}
|
|
116
|
+
} else if ((0, import_richtext.isRichTextNodeType)(node, "paragraph")) {
|
|
117
|
+
if (node.format) {
|
|
118
|
+
metadata["format"] = node.format;
|
|
119
|
+
}
|
|
120
|
+
} else if ((0, import_richtext.isRichTextNodeType)(node, "list")) {
|
|
121
|
+
if (node.tag) {
|
|
122
|
+
metadata["tag"] = node.tag;
|
|
123
|
+
}
|
|
124
|
+
} else if ((0, import_richtext.isRichTextNodeType)(node, "listitem")) {
|
|
125
|
+
metadata["value"] = String(node.value);
|
|
126
|
+
}
|
|
127
|
+
if (!Object.keys(metadata).length) {
|
|
128
|
+
return "";
|
|
129
|
+
}
|
|
130
|
+
const result = [];
|
|
131
|
+
Object.entries(metadata).forEach(([key, value]) => {
|
|
132
|
+
result.push(`${key}="${value}"`);
|
|
133
|
+
});
|
|
134
|
+
return result.join(" ");
|
|
135
|
+
};
|
|
136
|
+
|
|
47
137
|
// src/translationHelpers.ts
|
|
48
138
|
var import_canvas = require("@uniformdev/canvas");
|
|
49
|
-
var
|
|
139
|
+
var import_richtext3 = require("@uniformdev/richtext");
|
|
50
140
|
var import_lite = require("dequal/lite");
|
|
141
|
+
|
|
142
|
+
// src/richText/parseTranslatableXml.ts
|
|
143
|
+
var import_richtext2 = require("@uniformdev/richtext");
|
|
144
|
+
var import_fast_xml_parser = require("fast-xml-parser");
|
|
145
|
+
var ATTR_NAME_PREFIX = "@_";
|
|
146
|
+
var ATTRIBUTES_SUBNODE_NAME = ":@";
|
|
147
|
+
var IGNORE_PROPS_PREFIX = [ATTR_NAME_PREFIX, ATTRIBUTES_SUBNODE_NAME, "#"];
|
|
148
|
+
var parseTranslatableXml = ({ xml, metadata }) => {
|
|
149
|
+
const parser = new import_fast_xml_parser.XMLParser({
|
|
150
|
+
ignoreDeclaration: true,
|
|
151
|
+
ignorePiTags: true,
|
|
152
|
+
ignoreAttributes: false,
|
|
153
|
+
preserveOrder: true,
|
|
154
|
+
trimValues: false,
|
|
155
|
+
attributeNamePrefix: ATTR_NAME_PREFIX
|
|
156
|
+
});
|
|
157
|
+
const rawObj = parser.parse(prepareXml(xml));
|
|
158
|
+
const result = processParseObject(rawObj, {
|
|
159
|
+
metadata,
|
|
160
|
+
nodeTypes: []
|
|
161
|
+
});
|
|
162
|
+
return result;
|
|
163
|
+
};
|
|
164
|
+
var prepareXml = (xml) => {
|
|
165
|
+
const result = xml.replaceAll("</text>", "</text>");
|
|
166
|
+
return result;
|
|
167
|
+
};
|
|
168
|
+
var processParseObject = (rawObj, context) => {
|
|
169
|
+
const result = visitAndTransformNode(rawObj, context);
|
|
170
|
+
const root = Array.isArray(result) ? result.at(0) : result;
|
|
171
|
+
if (root && root.type === "root") {
|
|
172
|
+
const value = { root };
|
|
173
|
+
if ((0, import_richtext2.isRichTextValue)(value)) {
|
|
174
|
+
return value;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return null;
|
|
178
|
+
};
|
|
179
|
+
var visitAndTransformNode = (node, context) => {
|
|
180
|
+
if (Array.isArray(node)) {
|
|
181
|
+
return node.map((x) => visitAndTransformNode(x, context)).filter((node2) => !!node2);
|
|
182
|
+
} else if (typeof node === "object" && node !== null) {
|
|
183
|
+
const type = Object.keys(node).find(
|
|
184
|
+
(key) => !IGNORE_PROPS_PREFIX.some((prefix) => key.startsWith(prefix))
|
|
185
|
+
);
|
|
186
|
+
if (!type) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
const transformed = {};
|
|
190
|
+
transformed["type"] = type;
|
|
191
|
+
const propsFromRef = restorePropsFromRef(node, context);
|
|
192
|
+
Object.entries(propsFromRef).forEach(([key, value]) => {
|
|
193
|
+
transformed[key] = value;
|
|
194
|
+
});
|
|
195
|
+
if (type === "text") {
|
|
196
|
+
const nestedNode = node[type];
|
|
197
|
+
const nestedNodeAsArray = Array.isArray(nestedNode) ? nestedNode : [nestedNode];
|
|
198
|
+
const firstChild = nestedNodeAsArray.at(0);
|
|
199
|
+
const text = firstChild == null ? void 0 : firstChild["#text"];
|
|
200
|
+
transformed["text"] = typeof text === "string" && text ? text : "";
|
|
201
|
+
} else {
|
|
202
|
+
const children = [];
|
|
203
|
+
const nestedNode = node[type];
|
|
204
|
+
if (typeof nestedNode === "object" && nestedNode) {
|
|
205
|
+
const nestedNodeAsArray = Array.isArray(nestedNode) ? nestedNode : [nestedNode];
|
|
206
|
+
nestedNodeAsArray.forEach((currNode) => {
|
|
207
|
+
const transformed2 = visitAndTransformNode(currNode, context);
|
|
208
|
+
if (transformed2) {
|
|
209
|
+
children.push(transformed2);
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
if (children.length) {
|
|
214
|
+
transformed["children"] = children;
|
|
215
|
+
} else if (type === "paragraph" && Array.isArray(nestedNode) && !nestedNode.length) {
|
|
216
|
+
transformed["children"] = [];
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return transformed;
|
|
220
|
+
} else {
|
|
221
|
+
return node;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
var restorePropsFromRef = (node, context) => {
|
|
225
|
+
const result = {};
|
|
226
|
+
let uniformRef = node[ATTR_NAME_PREFIX + REF_ATTRIBUTE_NAME];
|
|
227
|
+
if (!uniformRef) {
|
|
228
|
+
const attrsSubNode = node[ATTRIBUTES_SUBNODE_NAME];
|
|
229
|
+
uniformRef = attrsSubNode == null ? void 0 : attrsSubNode[ATTR_NAME_PREFIX + REF_ATTRIBUTE_NAME];
|
|
230
|
+
}
|
|
231
|
+
if (typeof uniformRef === "string" && uniformRef && context.metadata[uniformRef]) {
|
|
232
|
+
const metadata = context.metadata[uniformRef];
|
|
233
|
+
if (metadata.attrs) {
|
|
234
|
+
const decodedProps = JSON.parse(atob(metadata.attrs));
|
|
235
|
+
Object.keys(decodedProps).forEach((key) => {
|
|
236
|
+
result[key] = decodedProps[key];
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return result;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
// src/richText/types.ts
|
|
244
|
+
var isTranslatableRichTextValue = (value) => {
|
|
245
|
+
if (typeof value !== "object" || !value) {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
if (!("xml" in value) || typeof value.xml !== "string") {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
if (!("metadata" in value) || typeof value.metadata !== "object" || !value.metadata) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
return true;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
// src/translationHelpers.ts
|
|
51
258
|
var MERGE_TRANSLATION_ERRORS = {
|
|
52
259
|
unknown: "Unknown error",
|
|
53
260
|
"invalid-args": "Invalid arguments",
|
|
@@ -131,9 +338,19 @@ var processParameterTranslation = ({
|
|
|
131
338
|
left: originalParameter.locales[uniformTargetLocale],
|
|
132
339
|
right: originalTargetValue
|
|
133
340
|
});
|
|
134
|
-
if (targetValue && isSourceValueUntouched && isTargetValueUntouched) {
|
|
135
|
-
|
|
136
|
-
|
|
341
|
+
if (targetValue !== void 0 && isSourceValueUntouched && isTargetValueUntouched) {
|
|
342
|
+
if (parameter.type == SUPPORTED_PARAM_TYPES.richText && isTranslatableRichTextValue(targetValue)) {
|
|
343
|
+
try {
|
|
344
|
+
const richTextValue = parseTranslatableXml(targetValue);
|
|
345
|
+
originalParameter.locales[uniformTargetLocale] = richTextValue;
|
|
346
|
+
return { updated: true };
|
|
347
|
+
} catch (e) {
|
|
348
|
+
return { updated: false };
|
|
349
|
+
}
|
|
350
|
+
} else if (parameter.type == SUPPORTED_PARAM_TYPES.text) {
|
|
351
|
+
originalParameter.locales[uniformTargetLocale] = targetValue;
|
|
352
|
+
return { updated: true };
|
|
353
|
+
}
|
|
137
354
|
}
|
|
138
355
|
return { updated: false };
|
|
139
356
|
};
|
|
@@ -146,29 +363,18 @@ var processOverrideTranslation = ({
|
|
|
146
363
|
var _a, _b;
|
|
147
364
|
let updated = false;
|
|
148
365
|
Object.entries((_a = override.parameters) != null ? _a : {}).forEach(([paramKey, parameter]) => {
|
|
149
|
-
var _a2
|
|
366
|
+
var _a2;
|
|
150
367
|
const originalParameter = (_a2 = originalOverride.parameters) == null ? void 0 : _a2[paramKey];
|
|
151
|
-
if (!originalParameter
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
if (originalParameter.type !== parameter.type) {
|
|
368
|
+
if (!originalParameter) {
|
|
155
369
|
return;
|
|
156
370
|
}
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
left: originalParameter.locales[uniformSourceLocale],
|
|
163
|
-
right: sourceValue
|
|
164
|
-
});
|
|
165
|
-
const isTargetValueUntouched = isSameParameterValue({
|
|
166
|
-
parameterType: originalParameter.type,
|
|
167
|
-
left: originalParameter.locales[uniformTargetLocale],
|
|
168
|
-
right: originalTargetValue
|
|
371
|
+
const result = processParameterTranslation({
|
|
372
|
+
uniformSourceLocale,
|
|
373
|
+
uniformTargetLocale,
|
|
374
|
+
originalParameter,
|
|
375
|
+
parameter
|
|
169
376
|
});
|
|
170
|
-
if (
|
|
171
|
-
originalParameter.locales[uniformTargetLocale] = targetValue;
|
|
377
|
+
if (result.updated) {
|
|
172
378
|
updated = true;
|
|
173
379
|
}
|
|
174
380
|
});
|
|
@@ -222,8 +428,12 @@ var isSameParameterValue = ({
|
|
|
222
428
|
return left === right || !left && !right;
|
|
223
429
|
break;
|
|
224
430
|
case SUPPORTED_PARAM_TYPES.richText:
|
|
225
|
-
|
|
226
|
-
|
|
431
|
+
try {
|
|
432
|
+
if ((0, import_richtext3.isRichTextValueConsideredEmpty)(left) && (0, import_richtext3.isRichTextValueConsideredEmpty)(right)) {
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
} catch (e) {
|
|
436
|
+
return false;
|
|
227
437
|
}
|
|
228
438
|
return (0, import_lite.dequal)(left, right);
|
|
229
439
|
break;
|
|
@@ -330,7 +540,7 @@ var collectFromNode = ({
|
|
|
330
540
|
type: param.type,
|
|
331
541
|
locales: {
|
|
332
542
|
[TRANSLATION_PAYLOAD_SOURCE_KEY]: sourceLocaleValue,
|
|
333
|
-
[TRANSLATION_PAYLOAD_TARGET_KEY]: sourceLocaleValue,
|
|
543
|
+
[TRANSLATION_PAYLOAD_TARGET_KEY]: preprocessTargetValue(param, sourceLocaleValue),
|
|
334
544
|
[TRANSLATION_PAYLOAD_ORIGINAL_TARGET_KEY]: targetLocaleValue
|
|
335
545
|
}
|
|
336
546
|
};
|
|
@@ -374,7 +584,7 @@ var collectFromNode = ({
|
|
|
374
584
|
type: param.type,
|
|
375
585
|
locales: {
|
|
376
586
|
[TRANSLATION_PAYLOAD_SOURCE_KEY]: sourceLocaleValue,
|
|
377
|
-
[TRANSLATION_PAYLOAD_TARGET_KEY]: sourceLocaleValue,
|
|
587
|
+
[TRANSLATION_PAYLOAD_TARGET_KEY]: preprocessTargetValue(param, sourceLocaleValue),
|
|
378
588
|
[TRANSLATION_PAYLOAD_ORIGINAL_TARGET_KEY]: targetLocaleValue
|
|
379
589
|
}
|
|
380
590
|
};
|
|
@@ -433,7 +643,7 @@ var canTranslateParameterValue = (parameter, value) => {
|
|
|
433
643
|
}
|
|
434
644
|
return true;
|
|
435
645
|
} else if (parameter.type === SUPPORTED_PARAM_TYPES.richText) {
|
|
436
|
-
return (0,
|
|
646
|
+
return (0, import_richtext4.isRichTextValue)(value) && !(0, import_richtext4.isRichTextValueConsideredEmpty)(value);
|
|
437
647
|
}
|
|
438
648
|
return false;
|
|
439
649
|
};
|
|
@@ -444,7 +654,7 @@ var isTargetLocaleUntouched = (parameterType, sourceLocaleValue, targetLocaleVal
|
|
|
444
654
|
if (parameterType === SUPPORTED_PARAM_TYPES.text && !targetLocaleValue) {
|
|
445
655
|
return true;
|
|
446
656
|
}
|
|
447
|
-
if (parameterType === SUPPORTED_PARAM_TYPES.richText && (0,
|
|
657
|
+
if (parameterType === SUPPORTED_PARAM_TYPES.richText && (0, import_richtext4.isRichTextValueConsideredEmpty)(targetLocaleValue)) {
|
|
448
658
|
return true;
|
|
449
659
|
}
|
|
450
660
|
return isSameParameterValue({
|
|
@@ -453,6 +663,16 @@ var isTargetLocaleUntouched = (parameterType, sourceLocaleValue, targetLocaleVal
|
|
|
453
663
|
right: targetLocaleValue
|
|
454
664
|
});
|
|
455
665
|
};
|
|
666
|
+
var preprocessTargetValue = (parameter, value) => {
|
|
667
|
+
if (parameter.type === SUPPORTED_PARAM_TYPES.richText) {
|
|
668
|
+
try {
|
|
669
|
+
return (0, import_richtext4.isRichTextValue)(value) ? renderAsTranslatableXml(value.root) : value;
|
|
670
|
+
} catch (e) {
|
|
671
|
+
return value;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
return value;
|
|
675
|
+
};
|
|
456
676
|
|
|
457
677
|
// src/getCompositionForTranslation.ts
|
|
458
678
|
var import_canvas3 = require("@uniformdev/canvas");
|
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
bindVariables,
|
|
4
4
|
walkNodeTree as walkNodeTree2
|
|
5
5
|
} from "@uniformdev/canvas";
|
|
6
|
-
import { isRichTextValue, isRichTextValueConsideredEmpty as isRichTextValueConsideredEmpty2 } from "@uniformdev/richtext";
|
|
6
|
+
import { isRichTextValue as isRichTextValue2, isRichTextValueConsideredEmpty as isRichTextValueConsideredEmpty2 } from "@uniformdev/richtext";
|
|
7
7
|
|
|
8
8
|
// src/constants.ts
|
|
9
9
|
var TRANSLATION_PAYLOAD_SOURCE_KEY = "source";
|
|
@@ -14,12 +14,219 @@ var SUPPORTED_PARAM_TYPES = {
|
|
|
14
14
|
richText: "richText"
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
+
// src/richText/renderAsTranslatableXml.ts
|
|
18
|
+
import { getRichTextTagsFromTextFormat, isRichTextNodeType } from "@uniformdev/richtext";
|
|
19
|
+
var REF_ATTRIBUTE_NAME = "ref";
|
|
20
|
+
function renderAsTranslatableXml(root) {
|
|
21
|
+
if (!root) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
const result = {
|
|
25
|
+
attributes: [],
|
|
26
|
+
xmlParts: []
|
|
27
|
+
};
|
|
28
|
+
renderXmlNode(result, root);
|
|
29
|
+
const metadata = result.attributes.reduce((result2, current, index) => {
|
|
30
|
+
result2[String(index)] = {
|
|
31
|
+
attrs: current
|
|
32
|
+
};
|
|
33
|
+
return result2;
|
|
34
|
+
}, {});
|
|
35
|
+
return {
|
|
36
|
+
metadata,
|
|
37
|
+
xml: result.xmlParts.join("")
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
var IGNORE_ATTRS = ["type", "children", "text"];
|
|
41
|
+
function renderXmlNode(context, node) {
|
|
42
|
+
if (!node) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const base64Props = getPropsAsBase64(node, IGNORE_ATTRS);
|
|
46
|
+
let refKey = context.attributes.findIndex((x) => x === base64Props);
|
|
47
|
+
if (refKey == -1) {
|
|
48
|
+
context.attributes.push(base64Props);
|
|
49
|
+
refKey = context.attributes.length - 1;
|
|
50
|
+
}
|
|
51
|
+
context.xmlParts.push(`<${node.type} ${REF_ATTRIBUTE_NAME}="${refKey}"`);
|
|
52
|
+
const metaAttrsString = getNodeMetaAttributesString(node);
|
|
53
|
+
if (metaAttrsString) {
|
|
54
|
+
context.xmlParts.push(" " + metaAttrsString);
|
|
55
|
+
}
|
|
56
|
+
context.xmlParts.push(">");
|
|
57
|
+
if (isRichTextNodeType(node, "text")) {
|
|
58
|
+
context.xmlParts.push(node.text);
|
|
59
|
+
} else if (Array.isArray(node.children) && node.children.length) {
|
|
60
|
+
node.children.forEach((child) => {
|
|
61
|
+
renderXmlNode(context, child);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
context.xmlParts.push(`</${node.type}>`);
|
|
65
|
+
}
|
|
66
|
+
var getPropsAsBase64 = (node, ignoreProps) => {
|
|
67
|
+
const props = {};
|
|
68
|
+
Object.keys(node).forEach((key) => {
|
|
69
|
+
if (!ignoreProps.includes(key)) {
|
|
70
|
+
props[key] = node[key];
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
return btoa(JSON.stringify(props));
|
|
74
|
+
};
|
|
75
|
+
var getNodeMetaAttributesString = (node) => {
|
|
76
|
+
const metadata = {};
|
|
77
|
+
if (isRichTextNodeType(node, "text")) {
|
|
78
|
+
const formatTags = getRichTextTagsFromTextFormat(node.format);
|
|
79
|
+
if (formatTags.length) {
|
|
80
|
+
metadata["format"] = formatTags.join(" ");
|
|
81
|
+
}
|
|
82
|
+
} else if (isRichTextNodeType(node, "heading")) {
|
|
83
|
+
if (node.tag) {
|
|
84
|
+
metadata["tag"] = node.tag;
|
|
85
|
+
}
|
|
86
|
+
} else if (isRichTextNodeType(node, "paragraph")) {
|
|
87
|
+
if (node.format) {
|
|
88
|
+
metadata["format"] = node.format;
|
|
89
|
+
}
|
|
90
|
+
} else if (isRichTextNodeType(node, "list")) {
|
|
91
|
+
if (node.tag) {
|
|
92
|
+
metadata["tag"] = node.tag;
|
|
93
|
+
}
|
|
94
|
+
} else if (isRichTextNodeType(node, "listitem")) {
|
|
95
|
+
metadata["value"] = String(node.value);
|
|
96
|
+
}
|
|
97
|
+
if (!Object.keys(metadata).length) {
|
|
98
|
+
return "";
|
|
99
|
+
}
|
|
100
|
+
const result = [];
|
|
101
|
+
Object.entries(metadata).forEach(([key, value]) => {
|
|
102
|
+
result.push(`${key}="${value}"`);
|
|
103
|
+
});
|
|
104
|
+
return result.join(" ");
|
|
105
|
+
};
|
|
106
|
+
|
|
17
107
|
// src/translationHelpers.ts
|
|
18
108
|
import {
|
|
19
109
|
walkNodeTree
|
|
20
110
|
} from "@uniformdev/canvas";
|
|
21
111
|
import { isRichTextValueConsideredEmpty } from "@uniformdev/richtext";
|
|
22
112
|
import { dequal } from "dequal/lite";
|
|
113
|
+
|
|
114
|
+
// src/richText/parseTranslatableXml.ts
|
|
115
|
+
import { isRichTextValue } from "@uniformdev/richtext";
|
|
116
|
+
import { XMLParser } from "fast-xml-parser";
|
|
117
|
+
var ATTR_NAME_PREFIX = "@_";
|
|
118
|
+
var ATTRIBUTES_SUBNODE_NAME = ":@";
|
|
119
|
+
var IGNORE_PROPS_PREFIX = [ATTR_NAME_PREFIX, ATTRIBUTES_SUBNODE_NAME, "#"];
|
|
120
|
+
var parseTranslatableXml = ({ xml, metadata }) => {
|
|
121
|
+
const parser = new XMLParser({
|
|
122
|
+
ignoreDeclaration: true,
|
|
123
|
+
ignorePiTags: true,
|
|
124
|
+
ignoreAttributes: false,
|
|
125
|
+
preserveOrder: true,
|
|
126
|
+
trimValues: false,
|
|
127
|
+
attributeNamePrefix: ATTR_NAME_PREFIX
|
|
128
|
+
});
|
|
129
|
+
const rawObj = parser.parse(prepareXml(xml));
|
|
130
|
+
const result = processParseObject(rawObj, {
|
|
131
|
+
metadata,
|
|
132
|
+
nodeTypes: []
|
|
133
|
+
});
|
|
134
|
+
return result;
|
|
135
|
+
};
|
|
136
|
+
var prepareXml = (xml) => {
|
|
137
|
+
const result = xml.replaceAll("</text>", "</text>");
|
|
138
|
+
return result;
|
|
139
|
+
};
|
|
140
|
+
var processParseObject = (rawObj, context) => {
|
|
141
|
+
const result = visitAndTransformNode(rawObj, context);
|
|
142
|
+
const root = Array.isArray(result) ? result.at(0) : result;
|
|
143
|
+
if (root && root.type === "root") {
|
|
144
|
+
const value = { root };
|
|
145
|
+
if (isRichTextValue(value)) {
|
|
146
|
+
return value;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return null;
|
|
150
|
+
};
|
|
151
|
+
var visitAndTransformNode = (node, context) => {
|
|
152
|
+
if (Array.isArray(node)) {
|
|
153
|
+
return node.map((x) => visitAndTransformNode(x, context)).filter((node2) => !!node2);
|
|
154
|
+
} else if (typeof node === "object" && node !== null) {
|
|
155
|
+
const type = Object.keys(node).find(
|
|
156
|
+
(key) => !IGNORE_PROPS_PREFIX.some((prefix) => key.startsWith(prefix))
|
|
157
|
+
);
|
|
158
|
+
if (!type) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
const transformed = {};
|
|
162
|
+
transformed["type"] = type;
|
|
163
|
+
const propsFromRef = restorePropsFromRef(node, context);
|
|
164
|
+
Object.entries(propsFromRef).forEach(([key, value]) => {
|
|
165
|
+
transformed[key] = value;
|
|
166
|
+
});
|
|
167
|
+
if (type === "text") {
|
|
168
|
+
const nestedNode = node[type];
|
|
169
|
+
const nestedNodeAsArray = Array.isArray(nestedNode) ? nestedNode : [nestedNode];
|
|
170
|
+
const firstChild = nestedNodeAsArray.at(0);
|
|
171
|
+
const text = firstChild == null ? void 0 : firstChild["#text"];
|
|
172
|
+
transformed["text"] = typeof text === "string" && text ? text : "";
|
|
173
|
+
} else {
|
|
174
|
+
const children = [];
|
|
175
|
+
const nestedNode = node[type];
|
|
176
|
+
if (typeof nestedNode === "object" && nestedNode) {
|
|
177
|
+
const nestedNodeAsArray = Array.isArray(nestedNode) ? nestedNode : [nestedNode];
|
|
178
|
+
nestedNodeAsArray.forEach((currNode) => {
|
|
179
|
+
const transformed2 = visitAndTransformNode(currNode, context);
|
|
180
|
+
if (transformed2) {
|
|
181
|
+
children.push(transformed2);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
if (children.length) {
|
|
186
|
+
transformed["children"] = children;
|
|
187
|
+
} else if (type === "paragraph" && Array.isArray(nestedNode) && !nestedNode.length) {
|
|
188
|
+
transformed["children"] = [];
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return transformed;
|
|
192
|
+
} else {
|
|
193
|
+
return node;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
var restorePropsFromRef = (node, context) => {
|
|
197
|
+
const result = {};
|
|
198
|
+
let uniformRef = node[ATTR_NAME_PREFIX + REF_ATTRIBUTE_NAME];
|
|
199
|
+
if (!uniformRef) {
|
|
200
|
+
const attrsSubNode = node[ATTRIBUTES_SUBNODE_NAME];
|
|
201
|
+
uniformRef = attrsSubNode == null ? void 0 : attrsSubNode[ATTR_NAME_PREFIX + REF_ATTRIBUTE_NAME];
|
|
202
|
+
}
|
|
203
|
+
if (typeof uniformRef === "string" && uniformRef && context.metadata[uniformRef]) {
|
|
204
|
+
const metadata = context.metadata[uniformRef];
|
|
205
|
+
if (metadata.attrs) {
|
|
206
|
+
const decodedProps = JSON.parse(atob(metadata.attrs));
|
|
207
|
+
Object.keys(decodedProps).forEach((key) => {
|
|
208
|
+
result[key] = decodedProps[key];
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// src/richText/types.ts
|
|
216
|
+
var isTranslatableRichTextValue = (value) => {
|
|
217
|
+
if (typeof value !== "object" || !value) {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
if (!("xml" in value) || typeof value.xml !== "string") {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
if (!("metadata" in value) || typeof value.metadata !== "object" || !value.metadata) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
return true;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
// src/translationHelpers.ts
|
|
23
230
|
var MERGE_TRANSLATION_ERRORS = {
|
|
24
231
|
unknown: "Unknown error",
|
|
25
232
|
"invalid-args": "Invalid arguments",
|
|
@@ -103,9 +310,19 @@ var processParameterTranslation = ({
|
|
|
103
310
|
left: originalParameter.locales[uniformTargetLocale],
|
|
104
311
|
right: originalTargetValue
|
|
105
312
|
});
|
|
106
|
-
if (targetValue && isSourceValueUntouched && isTargetValueUntouched) {
|
|
107
|
-
|
|
108
|
-
|
|
313
|
+
if (targetValue !== void 0 && isSourceValueUntouched && isTargetValueUntouched) {
|
|
314
|
+
if (parameter.type == SUPPORTED_PARAM_TYPES.richText && isTranslatableRichTextValue(targetValue)) {
|
|
315
|
+
try {
|
|
316
|
+
const richTextValue = parseTranslatableXml(targetValue);
|
|
317
|
+
originalParameter.locales[uniformTargetLocale] = richTextValue;
|
|
318
|
+
return { updated: true };
|
|
319
|
+
} catch (e) {
|
|
320
|
+
return { updated: false };
|
|
321
|
+
}
|
|
322
|
+
} else if (parameter.type == SUPPORTED_PARAM_TYPES.text) {
|
|
323
|
+
originalParameter.locales[uniformTargetLocale] = targetValue;
|
|
324
|
+
return { updated: true };
|
|
325
|
+
}
|
|
109
326
|
}
|
|
110
327
|
return { updated: false };
|
|
111
328
|
};
|
|
@@ -118,29 +335,18 @@ var processOverrideTranslation = ({
|
|
|
118
335
|
var _a, _b;
|
|
119
336
|
let updated = false;
|
|
120
337
|
Object.entries((_a = override.parameters) != null ? _a : {}).forEach(([paramKey, parameter]) => {
|
|
121
|
-
var _a2
|
|
338
|
+
var _a2;
|
|
122
339
|
const originalParameter = (_a2 = originalOverride.parameters) == null ? void 0 : _a2[paramKey];
|
|
123
|
-
if (!originalParameter
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
if (originalParameter.type !== parameter.type) {
|
|
340
|
+
if (!originalParameter) {
|
|
127
341
|
return;
|
|
128
342
|
}
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
left: originalParameter.locales[uniformSourceLocale],
|
|
135
|
-
right: sourceValue
|
|
136
|
-
});
|
|
137
|
-
const isTargetValueUntouched = isSameParameterValue({
|
|
138
|
-
parameterType: originalParameter.type,
|
|
139
|
-
left: originalParameter.locales[uniformTargetLocale],
|
|
140
|
-
right: originalTargetValue
|
|
343
|
+
const result = processParameterTranslation({
|
|
344
|
+
uniformSourceLocale,
|
|
345
|
+
uniformTargetLocale,
|
|
346
|
+
originalParameter,
|
|
347
|
+
parameter
|
|
141
348
|
});
|
|
142
|
-
if (
|
|
143
|
-
originalParameter.locales[uniformTargetLocale] = targetValue;
|
|
349
|
+
if (result.updated) {
|
|
144
350
|
updated = true;
|
|
145
351
|
}
|
|
146
352
|
});
|
|
@@ -194,8 +400,12 @@ var isSameParameterValue = ({
|
|
|
194
400
|
return left === right || !left && !right;
|
|
195
401
|
break;
|
|
196
402
|
case SUPPORTED_PARAM_TYPES.richText:
|
|
197
|
-
|
|
198
|
-
|
|
403
|
+
try {
|
|
404
|
+
if (isRichTextValueConsideredEmpty(left) && isRichTextValueConsideredEmpty(right)) {
|
|
405
|
+
return true;
|
|
406
|
+
}
|
|
407
|
+
} catch (e) {
|
|
408
|
+
return false;
|
|
199
409
|
}
|
|
200
410
|
return dequal(left, right);
|
|
201
411
|
break;
|
|
@@ -302,7 +512,7 @@ var collectFromNode = ({
|
|
|
302
512
|
type: param.type,
|
|
303
513
|
locales: {
|
|
304
514
|
[TRANSLATION_PAYLOAD_SOURCE_KEY]: sourceLocaleValue,
|
|
305
|
-
[TRANSLATION_PAYLOAD_TARGET_KEY]: sourceLocaleValue,
|
|
515
|
+
[TRANSLATION_PAYLOAD_TARGET_KEY]: preprocessTargetValue(param, sourceLocaleValue),
|
|
306
516
|
[TRANSLATION_PAYLOAD_ORIGINAL_TARGET_KEY]: targetLocaleValue
|
|
307
517
|
}
|
|
308
518
|
};
|
|
@@ -346,7 +556,7 @@ var collectFromNode = ({
|
|
|
346
556
|
type: param.type,
|
|
347
557
|
locales: {
|
|
348
558
|
[TRANSLATION_PAYLOAD_SOURCE_KEY]: sourceLocaleValue,
|
|
349
|
-
[TRANSLATION_PAYLOAD_TARGET_KEY]: sourceLocaleValue,
|
|
559
|
+
[TRANSLATION_PAYLOAD_TARGET_KEY]: preprocessTargetValue(param, sourceLocaleValue),
|
|
350
560
|
[TRANSLATION_PAYLOAD_ORIGINAL_TARGET_KEY]: targetLocaleValue
|
|
351
561
|
}
|
|
352
562
|
};
|
|
@@ -405,7 +615,7 @@ var canTranslateParameterValue = (parameter, value) => {
|
|
|
405
615
|
}
|
|
406
616
|
return true;
|
|
407
617
|
} else if (parameter.type === SUPPORTED_PARAM_TYPES.richText) {
|
|
408
|
-
return
|
|
618
|
+
return isRichTextValue2(value) && !isRichTextValueConsideredEmpty2(value);
|
|
409
619
|
}
|
|
410
620
|
return false;
|
|
411
621
|
};
|
|
@@ -425,6 +635,16 @@ var isTargetLocaleUntouched = (parameterType, sourceLocaleValue, targetLocaleVal
|
|
|
425
635
|
right: targetLocaleValue
|
|
426
636
|
});
|
|
427
637
|
};
|
|
638
|
+
var preprocessTargetValue = (parameter, value) => {
|
|
639
|
+
if (parameter.type === SUPPORTED_PARAM_TYPES.richText) {
|
|
640
|
+
try {
|
|
641
|
+
return isRichTextValue2(value) ? renderAsTranslatableXml(value.root) : value;
|
|
642
|
+
} catch (e) {
|
|
643
|
+
return value;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return value;
|
|
647
|
+
};
|
|
428
648
|
|
|
429
649
|
// src/getCompositionForTranslation.ts
|
|
430
650
|
import { CANVAS_DRAFT_STATE } from "@uniformdev/canvas";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniformdev/tms-sdk",
|
|
3
|
-
"version": "19.154.1-alpha.
|
|
3
|
+
"version": "19.154.1-alpha.30+156c2c7b00",
|
|
4
4
|
"description": "Uniform Translation Management System SDK",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -33,11 +33,12 @@
|
|
|
33
33
|
"access": "public"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@uniformdev/canvas": "19.154.1-alpha.
|
|
37
|
-
"@uniformdev/mesh-sdk": "19.154.1-alpha.
|
|
38
|
-
"@uniformdev/richtext": "19.154.1-alpha.
|
|
36
|
+
"@uniformdev/canvas": "19.154.1-alpha.30+156c2c7b00",
|
|
37
|
+
"@uniformdev/mesh-sdk": "19.154.1-alpha.30+156c2c7b00",
|
|
38
|
+
"@uniformdev/richtext": "19.154.1-alpha.30+156c2c7b00",
|
|
39
39
|
"dequal": "2.0.3",
|
|
40
|
+
"fast-xml-parser": "4.4.0",
|
|
40
41
|
"immer": "10.0.4"
|
|
41
42
|
},
|
|
42
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "156c2c7b0073414d5c07c790e45eef99e96b10fc"
|
|
43
44
|
}
|