sanity-plugin-studio-smartling 1.1.6 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  export { TranslationsTab } from 'sanity-translations-tab';
2
- import { BaseDocumentSerializer, defaultStopTypes, customSerializers, BaseDocumentDeserializer, BaseDocumentPatcher } from 'sanity-naive-html-serializer';
3
- export { BaseDocumentDeserializer, BaseDocumentPatcher, BaseDocumentSerializer, customSerializers, defaultStopTypes } from 'sanity-naive-html-serializer';
2
+ import { BaseDocumentMerger, BaseDocumentDeserializer, BaseDocumentSerializer } from 'sanity-naive-html-serializer';
3
+ export { BaseDocumentDeserializer, BaseDocumentMerger, BaseDocumentSerializer, customSerializers, defaultStopTypes } from 'sanity-naive-html-serializer';
4
+ import sanityClient from 'part:@sanity/base/client';
4
5
 
5
6
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
6
7
  try {
@@ -975,7 +976,7 @@ var getTranslationTask = /*#__PURE__*/function () {
975
976
  locales = smartlingTask.contentProgressReport.map(function (item) {
976
977
  return {
977
978
  localeId: item.targetLocaleId,
978
- progress: item.progress.percentComplete
979
+ progress: item.progress ? item.progress.percentComplete : 0
979
980
  };
980
981
  });
981
982
  }
@@ -1043,30 +1044,41 @@ var createJob = /*#__PURE__*/function () {
1043
1044
 
1044
1045
 
1045
1046
  var createJobBatch = /*#__PURE__*/function () {
1046
- var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(jobId, projectId, documentName, accessToken) {
1047
- var url;
1047
+ var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(jobId, projectId, documentName, accessToken, localeIds, workflowUid) {
1048
+ var url, reqBody;
1048
1049
  return runtime_1.wrap(function _callee2$(_context2) {
1049
1050
  while (1) {
1050
1051
  switch (_context2.prev = _context2.next) {
1051
1052
  case 0:
1052
1053
  url = "https://api.smartling.com/job-batches-api/v2/projects/" + projectId + "/batches";
1054
+ reqBody = {
1055
+ authorize: true,
1056
+ translationJobUid: jobId,
1057
+ fileUris: [documentName]
1058
+ };
1059
+
1060
+ if (workflowUid) {
1061
+ reqBody.localeWorkflows = localeIds.map(function (l) {
1062
+ return {
1063
+ targetLocaleId: l,
1064
+ workflowUid: workflowUid
1065
+ };
1066
+ });
1067
+ }
1068
+
1053
1069
  return _context2.abrupt("return", fetch(smartlingProxy, {
1054
1070
  method: 'POST',
1055
1071
  headers: _extends({}, getHeaders(url, accessToken), {
1056
1072
  'content-type': 'application/json'
1057
1073
  }),
1058
- body: JSON.stringify({
1059
- authorize: true,
1060
- translationJobUid: jobId,
1061
- fileUris: [documentName]
1062
- })
1074
+ body: JSON.stringify(reqBody)
1063
1075
  }).then(function (res) {
1064
1076
  return res.json();
1065
1077
  }).then(function (res) {
1066
1078
  return res.response.data.batchUid;
1067
1079
  }));
1068
1080
 
1069
- case 2:
1081
+ case 4:
1070
1082
  case "end":
1071
1083
  return _context2.stop();
1072
1084
  }
@@ -1074,7 +1086,7 @@ var createJobBatch = /*#__PURE__*/function () {
1074
1086
  }, _callee2);
1075
1087
  }));
1076
1088
 
1077
- return function createJobBatch(_x5, _x6, _x7, _x8) {
1089
+ return function createJobBatch(_x5, _x6, _x7, _x8, _x9, _x10) {
1078
1090
  return _ref2.apply(this, arguments);
1079
1091
  };
1080
1092
  }();
@@ -1111,13 +1123,13 @@ var uploadFileToBatch = /*#__PURE__*/function () {
1111
1123
  }, _callee3);
1112
1124
  }));
1113
1125
 
1114
- return function uploadFileToBatch(_x9, _x10, _x11, _x12, _x13) {
1126
+ return function uploadFileToBatch(_x11, _x12, _x13, _x14, _x15) {
1115
1127
  return _ref3.apply(this, arguments);
1116
1128
  };
1117
1129
  }();
1118
1130
 
1119
1131
  var createTask = /*#__PURE__*/function () {
1120
- var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(documentId, document, localeIds, secrets) {
1132
+ var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(documentId, document, localeIds, secrets, workflowUid) {
1121
1133
  var accessToken, taskId, batchUid, uploadFileRes;
1122
1134
  return runtime_1.wrap(function _callee4$(_context4) {
1123
1135
  while (1) {
@@ -1147,7 +1159,7 @@ var createTask = /*#__PURE__*/function () {
1147
1159
 
1148
1160
  case 10:
1149
1161
  _context4.next = 12;
1150
- return createJobBatch(taskId, secrets.project, document.name, accessToken);
1162
+ return createJobBatch(taskId, secrets.project, document.name, accessToken, localeIds, workflowUid);
1151
1163
 
1152
1164
  case 12:
1153
1165
  batchUid = _context4.sent;
@@ -1167,7 +1179,7 @@ var createTask = /*#__PURE__*/function () {
1167
1179
  }, _callee4);
1168
1180
  }));
1169
1181
 
1170
- return function createTask(_x14, _x15, _x16, _x17) {
1182
+ return function createTask(_x16, _x17, _x18, _x19, _x20) {
1171
1183
  return _ref4.apply(this, arguments);
1172
1184
  };
1173
1185
  }();
@@ -1219,25 +1231,279 @@ var SmartlingAdapter = {
1219
1231
  getTranslation: getTranslation
1220
1232
  };
1221
1233
 
1234
+ var client = /*#__PURE__*/sanityClient.withConfig({
1235
+ apiVersion: '2021-03-25'
1236
+ }); //document fetch
1237
+
1238
+ var findLatestDraft = function findLatestDraft(documentId, ignoreI18n) {
1239
+ if (ignoreI18n === void 0) {
1240
+ ignoreI18n = true;
1241
+ }
1242
+
1243
+ //eliminates i18n versions
1244
+ var query = "*[_id match $id " + (ignoreI18n ? ' && (_id in path("drafts.*") || _id in path("*"))' : '') + "]";
1245
+ var params = {
1246
+ id: "*" + documentId
1247
+ };
1248
+ return client.fetch(query, params).then(function (docs) {
1249
+ var _docs$find;
1250
+
1251
+ return (_docs$find = docs.find(function (doc) {
1252
+ return doc._id.includes('draft');
1253
+ })) != null ? _docs$find : docs[0];
1254
+ });
1255
+ }; //revision fetch
1256
+
1257
+ var findDocumentAtRevision = /*#__PURE__*/function () {
1258
+ var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(documentId, rev) {
1259
+ var dataset, baseUrl, url, revisionDoc;
1260
+ return runtime_1.wrap(function _callee$(_context) {
1261
+ while (1) {
1262
+ switch (_context.prev = _context.next) {
1263
+ case 0:
1264
+ dataset = client.config().dataset;
1265
+ baseUrl = "/data/history/" + dataset + "/documents/" + documentId + "?revision=" + rev;
1266
+ url = client.getUrl(baseUrl);
1267
+ _context.next = 5;
1268
+ return fetch(url, {
1269
+ credentials: 'include'
1270
+ }).then(function (req) {
1271
+ return req.json();
1272
+ }).then(function (req) {
1273
+ return req.documents && req.documents[0];
1274
+ });
1275
+
1276
+ case 5:
1277
+ revisionDoc = _context.sent;
1278
+
1279
+ if (!(!revisionDoc || revisionDoc._rev !== rev)) {
1280
+ _context.next = 12;
1281
+ break;
1282
+ }
1283
+
1284
+ baseUrl = "/data/history/" + dataset + "/documents/drafts." + documentId + "?revision=" + rev;
1285
+ url = client.getUrl(baseUrl);
1286
+ _context.next = 11;
1287
+ return fetch(url, {
1288
+ credentials: 'include'
1289
+ }).then(function (req) {
1290
+ return req.json();
1291
+ }).then(function (req) {
1292
+ return req.documents[0];
1293
+ });
1294
+
1295
+ case 11:
1296
+ revisionDoc = _context.sent;
1297
+
1298
+ case 12:
1299
+ return _context.abrupt("return", revisionDoc);
1300
+
1301
+ case 13:
1302
+ case "end":
1303
+ return _context.stop();
1304
+ }
1305
+ }
1306
+ }, _callee);
1307
+ }));
1308
+
1309
+ return function findDocumentAtRevision(_x, _x2) {
1310
+ return _ref.apply(this, arguments);
1311
+ };
1312
+ }(); //document-level patch
1313
+
1314
+ var documentLevelPatch = /*#__PURE__*/function () {
1315
+ var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(documentId, translatedFields, localeId) {
1316
+ var baseDoc, merged, targetId, i18nDoc, cleanedMerge;
1317
+ return runtime_1.wrap(function _callee2$(_context2) {
1318
+ while (1) {
1319
+ switch (_context2.prev = _context2.next) {
1320
+ case 0:
1321
+ if (!translatedFields._rev) {
1322
+ _context2.next = 6;
1323
+ break;
1324
+ }
1325
+
1326
+ _context2.next = 3;
1327
+ return findDocumentAtRevision(documentId, translatedFields._rev);
1328
+
1329
+ case 3:
1330
+ baseDoc = _context2.sent;
1331
+ _context2.next = 9;
1332
+ break;
1333
+
1334
+ case 6:
1335
+ _context2.next = 8;
1336
+ return findLatestDraft(documentId);
1337
+
1338
+ case 8:
1339
+ baseDoc = _context2.sent;
1340
+
1341
+ case 9:
1342
+ merged = BaseDocumentMerger.documentLevelMerge(translatedFields, baseDoc);
1343
+ targetId = "i18n." + documentId + "." + localeId;
1344
+ _context2.next = 13;
1345
+ return findLatestDraft(targetId, false);
1346
+
1347
+ case 13:
1348
+ i18nDoc = _context2.sent;
1349
+
1350
+ if (i18nDoc) {
1351
+ cleanedMerge = {}; //don't overwrite any existing values on the i18n doc
1352
+
1353
+ Object.entries(merged).forEach(function (_ref3) {
1354
+ var key = _ref3[0],
1355
+ value = _ref3[1];
1356
+
1357
+ if (Object.keys(translatedFields).includes(key) && !['_id', '_rev', '_updatedAt'].includes(key)) {
1358
+ cleanedMerge[key] = value;
1359
+ }
1360
+ });
1361
+ client.transaction() //@ts-ignore
1362
+ .patch(i18nDoc._id, function (p) {
1363
+ return p.set(cleanedMerge);
1364
+ }).commit();
1365
+ } else {
1366
+ merged._id = "drafts." + targetId; //account for legacy implementations of i18n plugin lang
1367
+
1368
+ if (baseDoc._lang) {
1369
+ merged._lang = localeId;
1370
+ } else if (baseDoc.__i18n_lang) {
1371
+ merged.__i18n_lang = localeId;
1372
+ }
1373
+
1374
+ client.create(merged);
1375
+ }
1376
+
1377
+ case 15:
1378
+ case "end":
1379
+ return _context2.stop();
1380
+ }
1381
+ }
1382
+ }, _callee2);
1383
+ }));
1384
+
1385
+ return function documentLevelPatch(_x3, _x4, _x5) {
1386
+ return _ref2.apply(this, arguments);
1387
+ };
1388
+ }(); //field level patch
1389
+
1390
+ var fieldLevelPatch = /*#__PURE__*/function () {
1391
+ var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(documentId, translatedFields, localeId) {
1392
+ var baseDoc, merged;
1393
+ return runtime_1.wrap(function _callee3$(_context3) {
1394
+ while (1) {
1395
+ switch (_context3.prev = _context3.next) {
1396
+ case 0:
1397
+ if (!translatedFields._rev) {
1398
+ _context3.next = 6;
1399
+ break;
1400
+ }
1401
+
1402
+ _context3.next = 3;
1403
+ return findDocumentAtRevision(documentId, translatedFields._rev);
1404
+
1405
+ case 3:
1406
+ baseDoc = _context3.sent;
1407
+ _context3.next = 9;
1408
+ break;
1409
+
1410
+ case 6:
1411
+ _context3.next = 8;
1412
+ return findLatestDraft(documentId);
1413
+
1414
+ case 8:
1415
+ baseDoc = _context3.sent;
1416
+
1417
+ case 9:
1418
+ merged = BaseDocumentMerger.fieldLevelMerge(translatedFields, baseDoc, localeId, 'en');
1419
+ client.patch(baseDoc._id).set(merged).commit();
1420
+
1421
+ case 11:
1422
+ case "end":
1423
+ return _context3.stop();
1424
+ }
1425
+ }
1426
+ }, _callee3);
1427
+ }));
1428
+
1429
+ return function fieldLevelPatch(_x6, _x7, _x8) {
1430
+ return _ref4.apply(this, arguments);
1431
+ };
1432
+ }();
1433
+
1222
1434
  var defaultDocumentLevelConfig = {
1223
- exportForTranslation: function exportForTranslation(id) {
1224
- return BaseDocumentSerializer.serializeDocument(id, 'document', 'en', defaultStopTypes, customSerializers);
1225
- },
1435
+ exportForTranslation: /*#__PURE__*/function () {
1436
+ var _exportForTranslation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(id) {
1437
+ var doc, serialized;
1438
+ return runtime_1.wrap(function _callee$(_context) {
1439
+ while (1) {
1440
+ switch (_context.prev = _context.next) {
1441
+ case 0:
1442
+ _context.next = 2;
1443
+ return findLatestDraft(id);
1444
+
1445
+ case 2:
1446
+ doc = _context.sent;
1447
+ serialized = BaseDocumentSerializer.serializeDocument(doc, 'document'); //needed for lookup by translation tab
1448
+
1449
+ serialized.name = id;
1450
+ return _context.abrupt("return", serialized);
1451
+
1452
+ case 6:
1453
+ case "end":
1454
+ return _context.stop();
1455
+ }
1456
+ }
1457
+ }, _callee);
1458
+ }));
1459
+
1460
+ function exportForTranslation(_x) {
1461
+ return _exportForTranslation.apply(this, arguments);
1462
+ }
1463
+
1464
+ return exportForTranslation;
1465
+ }(),
1226
1466
  importTranslation: function importTranslation(id, localeId, document) {
1227
- return BaseDocumentDeserializer.deserializeDocument(id, document).then(function (deserialized) {
1228
- return BaseDocumentPatcher.documentLevelPatch(deserialized, id, localeId);
1229
- });
1467
+ var deserialized = BaseDocumentDeserializer.deserializeDocument(document);
1468
+ documentLevelPatch(id, deserialized, localeId);
1230
1469
  },
1231
1470
  adapter: SmartlingAdapter
1232
1471
  };
1233
1472
  var defaultFieldLevelConfig = {
1234
- exportForTranslation: function exportForTranslation(id) {
1235
- return BaseDocumentSerializer.serializeDocument(id, 'field', 'en', defaultStopTypes, customSerializers);
1236
- },
1473
+ exportForTranslation: /*#__PURE__*/function () {
1474
+ var _exportForTranslation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(id) {
1475
+ var doc, serialized;
1476
+ return runtime_1.wrap(function _callee2$(_context2) {
1477
+ while (1) {
1478
+ switch (_context2.prev = _context2.next) {
1479
+ case 0:
1480
+ _context2.next = 2;
1481
+ return findLatestDraft(id);
1482
+
1483
+ case 2:
1484
+ doc = _context2.sent;
1485
+ serialized = BaseDocumentSerializer.serializeDocument(doc, 'field'); //needed for lookup by translation tab
1486
+
1487
+ serialized.name = id;
1488
+ return _context2.abrupt("return", serialized);
1489
+
1490
+ case 6:
1491
+ case "end":
1492
+ return _context2.stop();
1493
+ }
1494
+ }
1495
+ }, _callee2);
1496
+ }));
1497
+
1498
+ function exportForTranslation(_x2) {
1499
+ return _exportForTranslation2.apply(this, arguments);
1500
+ }
1501
+
1502
+ return exportForTranslation;
1503
+ }(),
1237
1504
  importTranslation: function importTranslation(id, localeId, document) {
1238
- return BaseDocumentDeserializer.deserializeDocument(id, document).then(function (deserialized) {
1239
- return BaseDocumentPatcher.fieldLevelPatch(deserialized, id, localeId, 'en');
1240
- });
1505
+ var deserialized = BaseDocumentDeserializer.deserializeDocument(document);
1506
+ fieldLevelPatch(id, deserialized, localeId);
1241
1507
  },
1242
1508
  adapter: SmartlingAdapter
1243
1509
  };