@uniformdev/tms-sdk 19.154.1-alpha.17 → 19.154.1-alpha.21

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.js CHANGED
@@ -33,20 +33,207 @@ module.exports = __toCommonJS(src_exports);
33
33
 
34
34
  // src/collectTranslationPayload.ts
35
35
  var import_canvas = require("@uniformdev/canvas");
36
+ var import_richtext2 = require("@uniformdev/richtext");
36
37
 
37
38
  // src/constants.ts
38
39
  var TRANSLATION_PAYLOAD_SOURCE_KEY = "source";
39
40
  var TRANSLATION_PAYLOAD_TARGET_KEY = "target";
40
41
  var TRANSLATION_PAYLOAD_ORIGINAL_TARGET_KEY = "originalTarget";
42
+ var SUPPORTED_PARAM_TYPES = {
43
+ text: "text",
44
+ richText: "richText"
45
+ };
41
46
 
42
- // src/collectTranslationPayload.ts
47
+ // src/translationHelpers.ts
48
+ var import_richtext = require("@uniformdev/richtext");
49
+ var import_lite = require("dequal/lite");
43
50
  var MERGE_TRANSLATION_ERRORS = {
44
51
  unknown: "Unknown error",
45
52
  "invalid-args": "Invalid arguments",
46
- "entity-source-locale-missing": "Entity does not include specified Uniform source locale"
53
+ "project-id-mismatch": "Project ID mismatch",
54
+ "entity-id-mismatch": "Enity ID mismatch",
55
+ "entity-locales-mismatch": "Entity does not include required locales"
47
56
  };
48
57
  var createErrorResult = (errorKind) => {
49
- return { errorKind, errorText: MERGE_TRANSLATION_ERRORS[errorKind] };
58
+ return { updated: false, errorKind, errorText: MERGE_TRANSLATION_ERRORS[errorKind] };
59
+ };
60
+ var processComponentTranslation = ({
61
+ uniformSourceLocale,
62
+ uniformTargetLocale,
63
+ overrideModifiedTargetLocale,
64
+ originalNode,
65
+ translatedComponent
66
+ }) => {
67
+ var _a, _b;
68
+ let updated = false;
69
+ if (!originalNode.type || !originalNode._id) {
70
+ return { updated };
71
+ }
72
+ const componentParameters = "parameters" in originalNode ? originalNode.parameters : void 0;
73
+ const entryFields = "fields" in originalNode ? originalNode.fields : void 0;
74
+ const originalParametersOrFields = componentParameters != null ? componentParameters : entryFields;
75
+ Object.entries((_a = translatedComponent.parameters) != null ? _a : {}).forEach(([paramKey, parameter]) => {
76
+ const originalParameter = originalParametersOrFields == null ? void 0 : originalParametersOrFields[paramKey];
77
+ if (!originalParameter) {
78
+ return;
79
+ }
80
+ const result = processParameterTranslation({
81
+ uniformSourceLocale,
82
+ uniformTargetLocale,
83
+ overrideModifiedTargetLocale,
84
+ originalParameter,
85
+ parameter
86
+ });
87
+ if (result.updated) {
88
+ updated = true;
89
+ }
90
+ });
91
+ Object.entries((_b = translatedComponent._overrides) != null ? _b : {}).forEach(([overrideKey, override]) => {
92
+ var _a2;
93
+ const originalOverride = (_a2 = originalNode._overrides) == null ? void 0 : _a2[overrideKey];
94
+ if (!originalOverride) {
95
+ return;
96
+ }
97
+ const result = processOverrideTranslation({
98
+ uniformSourceLocale,
99
+ uniformTargetLocale,
100
+ overrideModifiedTargetLocale,
101
+ originalOverride,
102
+ override
103
+ });
104
+ if (result.updated) {
105
+ updated = true;
106
+ }
107
+ });
108
+ return { updated };
109
+ };
110
+ var processParameterTranslation = ({
111
+ uniformSourceLocale,
112
+ uniformTargetLocale,
113
+ overrideModifiedTargetLocale,
114
+ originalParameter,
115
+ parameter
116
+ }) => {
117
+ var _a, _b, _c;
118
+ if (!originalParameter.locales) {
119
+ return { updated: false };
120
+ }
121
+ if (originalParameter.type !== parameter.type) {
122
+ return { updated: false };
123
+ }
124
+ const sourceValue = (_a = parameter.locales) == null ? void 0 : _a[TRANSLATION_PAYLOAD_SOURCE_KEY];
125
+ const targetValue = (_b = parameter.locales) == null ? void 0 : _b[TRANSLATION_PAYLOAD_TARGET_KEY];
126
+ const originalTargetValue = (_c = parameter.locales) == null ? void 0 : _c[TRANSLATION_PAYLOAD_ORIGINAL_TARGET_KEY];
127
+ const isSourceValueUntouched = isSameParameterValue({
128
+ parameterType: originalParameter.type,
129
+ left: originalParameter.locales[uniformSourceLocale],
130
+ right: sourceValue
131
+ });
132
+ const isTargetValueUntouched = overrideModifiedTargetLocale || isSameParameterValue({
133
+ parameterType: originalParameter.type,
134
+ left: originalParameter.locales[uniformTargetLocale],
135
+ right: originalTargetValue
136
+ });
137
+ if (targetValue && isSourceValueUntouched && isTargetValueUntouched) {
138
+ originalParameter.locales[uniformTargetLocale] = targetValue;
139
+ return { updated: true };
140
+ }
141
+ return { updated: false };
142
+ };
143
+ var processOverrideTranslation = ({
144
+ uniformSourceLocale,
145
+ uniformTargetLocale,
146
+ overrideModifiedTargetLocale,
147
+ originalOverride,
148
+ override
149
+ }) => {
150
+ var _a, _b;
151
+ let updated = false;
152
+ Object.entries((_a = override.parameters) != null ? _a : {}).forEach(([paramKey, parameter]) => {
153
+ var _a2, _b2, _c, _d;
154
+ const originalParameter = (_a2 = originalOverride.parameters) == null ? void 0 : _a2[paramKey];
155
+ if (!originalParameter || !originalParameter.locales) {
156
+ return;
157
+ }
158
+ if (originalParameter.type !== parameter.type) {
159
+ return;
160
+ }
161
+ const sourceValue = (_b2 = parameter.locales) == null ? void 0 : _b2[TRANSLATION_PAYLOAD_SOURCE_KEY];
162
+ const targetValue = (_c = parameter.locales) == null ? void 0 : _c[TRANSLATION_PAYLOAD_TARGET_KEY];
163
+ const originalTargetValue = (_d = parameter.locales) == null ? void 0 : _d[TRANSLATION_PAYLOAD_ORIGINAL_TARGET_KEY];
164
+ const isSourceValueUntouched = isSameParameterValue({
165
+ parameterType: originalParameter.type,
166
+ left: originalParameter.locales[uniformSourceLocale],
167
+ right: sourceValue
168
+ });
169
+ const isTargetValueUntouched = overrideModifiedTargetLocale || isSameParameterValue({
170
+ parameterType: originalParameter.type,
171
+ left: originalParameter.locales[uniformTargetLocale],
172
+ right: originalTargetValue
173
+ });
174
+ if (targetValue && isSourceValueUntouched && isTargetValueUntouched) {
175
+ originalParameter.locales[uniformTargetLocale] = targetValue;
176
+ updated = true;
177
+ }
178
+ });
179
+ Object.entries((_b = override.slots) != null ? _b : {}).forEach(([slotKey, slotComponents]) => {
180
+ var _a2;
181
+ const originalSlotComponents = (_a2 = originalOverride.slots) == null ? void 0 : _a2[slotKey];
182
+ if (!(originalSlotComponents == null ? void 0 : originalSlotComponents.length)) {
183
+ return;
184
+ }
185
+ slotComponents.forEach((slotComponent) => {
186
+ if (!slotComponent.type || !slotComponent._id) {
187
+ return;
188
+ }
189
+ const originalComponent = originalSlotComponents.find(
190
+ (x) => x._id === slotComponent._id && x.type === slotComponent.type
191
+ );
192
+ if (!originalComponent) {
193
+ return;
194
+ }
195
+ const { updated: isComponentUpdated } = processComponentTranslation({
196
+ uniformSourceLocale,
197
+ uniformTargetLocale,
198
+ overrideModifiedTargetLocale,
199
+ originalNode: originalComponent,
200
+ translatedComponent: slotComponent
201
+ });
202
+ if (isComponentUpdated) {
203
+ updated = true;
204
+ }
205
+ });
206
+ });
207
+ return { updated };
208
+ };
209
+ var isSameParameterValue = ({
210
+ parameterType,
211
+ left,
212
+ right
213
+ }) => {
214
+ switch (parameterType) {
215
+ case SUPPORTED_PARAM_TYPES.text:
216
+ return left === right || !left && !right;
217
+ break;
218
+ case SUPPORTED_PARAM_TYPES.richText:
219
+ if ((0, import_richtext.isRichTextValueConsideredEmpty)(left) && (0, import_richtext.isRichTextValueConsideredEmpty)(right)) {
220
+ return true;
221
+ }
222
+ return (0, import_lite.dequal)(left, right);
223
+ break;
224
+ default:
225
+ return left === right;
226
+ }
227
+ };
228
+
229
+ // src/collectTranslationPayload.ts
230
+ var MERGE_TRANSLATION_ERRORS2 = {
231
+ unknown: "Unknown error",
232
+ "invalid-args": "Invalid arguments",
233
+ "entity-source-locale-missing": "Entity does not include specified Uniform source locale"
234
+ };
235
+ var createErrorResult2 = (errorKind) => {
236
+ return { errorKind, errorText: MERGE_TRANSLATION_ERRORS2[errorKind] };
50
237
  };
51
238
  var collectTranslationPayload = ({
52
239
  uniformProjectId,
@@ -59,10 +246,10 @@ var collectTranslationPayload = ({
59
246
  overrideModifiedTargetLocale
60
247
  }) => {
61
248
  if (!uniformSourceLocale || !uniformTargetLocale || !targetLang || !entity) {
62
- return createErrorResult("invalid-args");
249
+ return createErrorResult2("invalid-args");
63
250
  }
64
251
  if (!entity._locales || !entity._locales.includes(uniformSourceLocale)) {
65
- return createErrorResult("entity-source-locale-missing");
252
+ return createErrorResult2("entity-source-locale-missing");
66
253
  }
67
254
  const payload = {
68
255
  schemaVersion: 1,
@@ -120,7 +307,7 @@ var collectFromNode = ({
120
307
  if (!canTranslateParameterValue(param, sourceLocaleValue)) {
121
308
  return;
122
309
  }
123
- if (!overrideModifiedTargetLocale && !isTargetLocaleUntouched(sourceLocaleValue, targetLocaleValue)) {
310
+ if (!overrideModifiedTargetLocale && !isTargetLocaleUntouched(param.type, sourceLocaleValue, targetLocaleValue)) {
124
311
  return;
125
312
  }
126
313
  parameters[paramKey] = {
@@ -141,7 +328,7 @@ var collectFromNode = ({
141
328
  if (!canTranslateParameterValue(param, sourceLocaleValue)) {
142
329
  return;
143
330
  }
144
- if (!overrideModifiedTargetLocale && !isTargetLocaleUntouched(sourceLocaleValue, targetLocaleValue)) {
331
+ if (!overrideModifiedTargetLocale && !isTargetLocaleUntouched(param.type, sourceLocaleValue, targetLocaleValue)) {
145
332
  return;
146
333
  }
147
334
  (_c = overrides[overrideKey]) != null ? _c : overrides[overrideKey] = {};
@@ -192,7 +379,7 @@ var collectFromNode = ({
192
379
  };
193
380
  };
194
381
  var canTranslateParameterValue = (parameter, value) => {
195
- if (parameter.type === "text") {
382
+ if (parameter.type === SUPPORTED_PARAM_TYPES.text) {
196
383
  if (typeof value !== "string" || !value) {
197
384
  return false;
198
385
  }
@@ -209,13 +396,26 @@ var canTranslateParameterValue = (parameter, value) => {
209
396
  return false;
210
397
  }
211
398
  return true;
212
- } else if (parameter.type === "richText") {
399
+ } else if (parameter.type === SUPPORTED_PARAM_TYPES.richText) {
213
400
  return typeof value === "object" && !!value;
214
401
  }
215
402
  return false;
216
403
  };
217
- var isTargetLocaleUntouched = (sourceLocaleValue, targetLocaleValue) => {
218
- return targetLocaleValue === void 0 || targetLocaleValue === null || targetLocaleValue === "" || targetLocaleValue === sourceLocaleValue;
404
+ var isTargetLocaleUntouched = (parameterType, sourceLocaleValue, targetLocaleValue) => {
405
+ if (targetLocaleValue === void 0 || targetLocaleValue === null) {
406
+ return true;
407
+ }
408
+ if (parameterType === SUPPORTED_PARAM_TYPES.text && !targetLocaleValue) {
409
+ return true;
410
+ }
411
+ if (parameterType === SUPPORTED_PARAM_TYPES.richText && (0, import_richtext2.isRichTextValueConsideredEmpty)(targetLocaleValue)) {
412
+ return true;
413
+ }
414
+ return isSameParameterValue({
415
+ parameterType,
416
+ left: sourceLocaleValue,
417
+ right: targetLocaleValue
418
+ });
219
419
  };
220
420
 
221
421
  // src/getCompositionForTranslation.ts
@@ -274,180 +474,6 @@ var import_canvas5 = require("@uniformdev/canvas");
274
474
  // src/translateComposition.ts
275
475
  var import_canvas4 = require("@uniformdev/canvas");
276
476
  var import_immer = require("immer");
277
-
278
- // src/translationHelpers.ts
279
- var import_lite = require("dequal/lite");
280
- var MERGE_TRANSLATION_ERRORS2 = {
281
- unknown: "Unknown error",
282
- "invalid-args": "Invalid arguments",
283
- "project-id-mismatch": "Project ID mismatch",
284
- "entity-id-mismatch": "Enity ID mismatch",
285
- "entity-locales-mismatch": "Entity does not include required locales"
286
- };
287
- var createErrorResult2 = (errorKind) => {
288
- return { updated: false, errorKind, errorText: MERGE_TRANSLATION_ERRORS2[errorKind] };
289
- };
290
- var processComponentTranslation = ({
291
- uniformSourceLocale,
292
- uniformTargetLocale,
293
- overrideModifiedTargetLocale,
294
- originalNode,
295
- translatedComponent
296
- }) => {
297
- var _a, _b;
298
- let updated = false;
299
- if (!originalNode.type || !originalNode._id) {
300
- return { updated };
301
- }
302
- const componentParameters = "parameters" in originalNode ? originalNode.parameters : void 0;
303
- const entryFields = "fields" in originalNode ? originalNode.fields : void 0;
304
- const originalParametersOrFields = componentParameters != null ? componentParameters : entryFields;
305
- Object.entries((_a = translatedComponent.parameters) != null ? _a : {}).forEach(([paramKey, parameter]) => {
306
- const originalParameter = originalParametersOrFields == null ? void 0 : originalParametersOrFields[paramKey];
307
- if (!originalParameter) {
308
- return;
309
- }
310
- const result = processParameterTranslation({
311
- uniformSourceLocale,
312
- uniformTargetLocale,
313
- overrideModifiedTargetLocale,
314
- originalParameter,
315
- parameter
316
- });
317
- if (result.updated) {
318
- updated = true;
319
- }
320
- });
321
- Object.entries((_b = translatedComponent._overrides) != null ? _b : {}).forEach(([overrideKey, override]) => {
322
- var _a2;
323
- const originalOverride = (_a2 = originalNode._overrides) == null ? void 0 : _a2[overrideKey];
324
- if (!originalOverride) {
325
- return;
326
- }
327
- const result = processOverrideTranslation({
328
- uniformSourceLocale,
329
- uniformTargetLocale,
330
- overrideModifiedTargetLocale,
331
- originalOverride,
332
- override
333
- });
334
- if (result.updated) {
335
- updated = true;
336
- }
337
- });
338
- return { updated };
339
- };
340
- var processParameterTranslation = ({
341
- uniformSourceLocale,
342
- uniformTargetLocale,
343
- overrideModifiedTargetLocale,
344
- originalParameter,
345
- parameter
346
- }) => {
347
- var _a, _b, _c;
348
- if (!originalParameter.locales) {
349
- return { updated: false };
350
- }
351
- const sourceValue = (_a = parameter.locales) == null ? void 0 : _a[TRANSLATION_PAYLOAD_SOURCE_KEY];
352
- const targetValue = (_b = parameter.locales) == null ? void 0 : _b[TRANSLATION_PAYLOAD_TARGET_KEY];
353
- const originalTargetValue = (_c = parameter.locales) == null ? void 0 : _c[TRANSLATION_PAYLOAD_ORIGINAL_TARGET_KEY];
354
- const isSourceValueUntouched = isSameParameterValue({
355
- parameterType: originalParameter.type,
356
- originalValue: originalParameter.locales[uniformSourceLocale],
357
- value: sourceValue
358
- });
359
- const isTargetValueUntouched = overrideModifiedTargetLocale || isSameParameterValue({
360
- parameterType: originalParameter.type,
361
- originalValue: originalParameter.locales[uniformTargetLocale],
362
- value: originalTargetValue
363
- });
364
- if (targetValue && isSourceValueUntouched && isTargetValueUntouched) {
365
- originalParameter.locales[uniformTargetLocale] = targetValue;
366
- return { updated: true };
367
- }
368
- return { updated: false };
369
- };
370
- var processOverrideTranslation = ({
371
- uniformSourceLocale,
372
- uniformTargetLocale,
373
- overrideModifiedTargetLocale,
374
- originalOverride,
375
- override
376
- }) => {
377
- var _a, _b;
378
- let updated = false;
379
- Object.entries((_a = override.parameters) != null ? _a : {}).forEach(([paramKey, parameter]) => {
380
- var _a2, _b2, _c, _d;
381
- const originalParameter = (_a2 = originalOverride.parameters) == null ? void 0 : _a2[paramKey];
382
- if (!originalParameter || !originalParameter.locales) {
383
- return;
384
- }
385
- const sourceValue = (_b2 = parameter.locales) == null ? void 0 : _b2[TRANSLATION_PAYLOAD_SOURCE_KEY];
386
- const targetValue = (_c = parameter.locales) == null ? void 0 : _c[TRANSLATION_PAYLOAD_TARGET_KEY];
387
- const originalTargetValue = (_d = parameter.locales) == null ? void 0 : _d[TRANSLATION_PAYLOAD_ORIGINAL_TARGET_KEY];
388
- const isSourceValueUntouched = isSameParameterValue({
389
- parameterType: originalParameter.type,
390
- originalValue: originalParameter.locales[uniformSourceLocale],
391
- value: sourceValue
392
- });
393
- const isTargetValueUntouched = overrideModifiedTargetLocale || isSameParameterValue({
394
- parameterType: originalParameter.type,
395
- originalValue: originalParameter.locales[uniformTargetLocale],
396
- value: originalTargetValue
397
- });
398
- if (targetValue && isSourceValueUntouched && isTargetValueUntouched) {
399
- originalParameter.locales[uniformTargetLocale] = targetValue;
400
- updated = true;
401
- }
402
- });
403
- Object.entries((_b = override.slots) != null ? _b : {}).forEach(([slotKey, slotComponents]) => {
404
- var _a2;
405
- const originalSlotComponents = (_a2 = originalOverride.slots) == null ? void 0 : _a2[slotKey];
406
- if (!(originalSlotComponents == null ? void 0 : originalSlotComponents.length)) {
407
- return;
408
- }
409
- slotComponents.forEach((slotComponent) => {
410
- if (!slotComponent.type || !slotComponent._id) {
411
- return;
412
- }
413
- const originalComponent = originalSlotComponents.find(
414
- (x) => x._id === slotComponent._id && x.type === slotComponent.type
415
- );
416
- if (!originalComponent) {
417
- return;
418
- }
419
- const { updated: isComponentUpdated } = processComponentTranslation({
420
- uniformSourceLocale,
421
- uniformTargetLocale,
422
- overrideModifiedTargetLocale,
423
- originalNode: originalComponent,
424
- translatedComponent: slotComponent
425
- });
426
- if (isComponentUpdated) {
427
- updated = true;
428
- }
429
- });
430
- });
431
- return { updated };
432
- };
433
- var isSameParameterValue = ({
434
- parameterType,
435
- originalValue,
436
- value
437
- }) => {
438
- switch (parameterType) {
439
- case "text":
440
- return originalValue === value || !originalValue && !value;
441
- break;
442
- case "richText":
443
- return (0, import_lite.dequal)(originalValue, value);
444
- break;
445
- default:
446
- return originalValue === value;
447
- }
448
- };
449
-
450
- // src/translateComposition.ts
451
477
  var translateComposition = ({
452
478
  composition,
453
479
  translationPayload,
@@ -455,19 +481,19 @@ var translateComposition = ({
455
481
  }) => {
456
482
  var _a;
457
483
  if (!composition || !composition.composition || !translationPayload) {
458
- return createErrorResult2("invalid-args");
484
+ return createErrorResult("invalid-args");
459
485
  }
460
486
  if (composition.projectId !== translationPayload.metadata.uniformProjectId) {
461
- return createErrorResult2("project-id-mismatch");
487
+ return createErrorResult("project-id-mismatch");
462
488
  }
463
489
  if (composition.composition._id !== translationPayload.metadata.entity.id) {
464
- return createErrorResult2("entity-id-mismatch");
490
+ return createErrorResult("entity-id-mismatch");
465
491
  }
466
492
  const compositionLocales = (_a = composition.composition._locales) != null ? _a : [];
467
493
  const uniformSourceLocale = translationPayload.metadata.uniformSourceLocale;
468
494
  const uniformTargetLocale = translationPayload.metadata.uniformTargetLocale;
469
495
  if (!compositionLocales.includes(uniformSourceLocale)) {
470
- return createErrorResult2("entity-locales-mismatch");
496
+ return createErrorResult("entity-locales-mismatch");
471
497
  }
472
498
  let updated = false;
473
499
  const translatedComposition = (0, import_immer.produce)(composition, (draft) => {
@@ -585,19 +611,19 @@ var translateEntry = ({
585
611
  }) => {
586
612
  var _a;
587
613
  if (!entry || !translationPayload) {
588
- return createErrorResult2("invalid-args");
614
+ return createErrorResult("invalid-args");
589
615
  }
590
616
  if (entry.projectId !== translationPayload.metadata.uniformProjectId) {
591
- return createErrorResult2("project-id-mismatch");
617
+ return createErrorResult("project-id-mismatch");
592
618
  }
593
619
  if ((entry == null ? void 0 : entry.entry._id) !== translationPayload.metadata.entity.id) {
594
- return createErrorResult2("entity-id-mismatch");
620
+ return createErrorResult("entity-id-mismatch");
595
621
  }
596
622
  const entryLocales = (_a = entry.entry._locales) != null ? _a : [];
597
623
  const uniformSourceLocale = translationPayload.metadata.uniformSourceLocale;
598
624
  const uniformTargetLocale = translationPayload.metadata.uniformTargetLocale;
599
625
  if (!entryLocales.includes(uniformSourceLocale)) {
600
- return createErrorResult2("entity-locales-mismatch");
626
+ return createErrorResult("entity-locales-mismatch");
601
627
  }
602
628
  let updated = false;
603
629
  const translatedEntry = (0, import_immer2.produce)(entry, (draft) => {