sanity-plugin-studio-smartling 1.1.6 → 1.2.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.
@@ -0,0 +1,5 @@
1
+ import { SanityDocument } from '@sanity/types';
2
+ export declare const findLatestDraft: (documentId: string, ignoreI18n?: boolean) => any;
3
+ export declare const findDocumentAtRevision: (documentId: string, rev: string) => Promise<any>;
4
+ export declare const documentLevelPatch: (documentId: string, translatedFields: SanityDocument, localeId: string) => Promise<void>;
5
+ export declare const fieldLevelPatch: (documentId: string, translatedFields: SanityDocument, localeId: string) => Promise<void>;
package/dist/index.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import { TranslationsTab } from 'sanity-translations-tab';
2
- import { BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentPatcher, defaultStopTypes, customSerializers } from 'sanity-naive-html-serializer';
2
+ import { BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentMerger, defaultStopTypes, customSerializers } from 'sanity-naive-html-serializer';
3
3
  import { SmartlingAdapter } from './adapter';
4
4
  declare const defaultDocumentLevelConfig: {
5
5
  exportForTranslation: (id: string) => Promise<import("sanity-naive-html-serializer/dist/types").SerializedDocument>;
6
- importTranslation: (id: string, localeId: string, document: string) => any;
6
+ importTranslation: (id: string, localeId: string, document: string) => void;
7
7
  adapter: import("sanity-translations-tab").Adapter;
8
8
  };
9
9
  declare const defaultFieldLevelConfig: {
10
10
  exportForTranslation: (id: string) => Promise<import("sanity-naive-html-serializer/dist/types").SerializedDocument>;
11
- importTranslation: (id: string, localeId: string, document: string) => any;
11
+ importTranslation: (id: string, localeId: string, document: string) => void;
12
12
  adapter: import("sanity-translations-tab").Adapter;
13
13
  };
14
- export { TranslationsTab, BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentPatcher, defaultStopTypes, customSerializers, SmartlingAdapter, defaultDocumentLevelConfig, defaultFieldLevelConfig, };
14
+ export { TranslationsTab, BaseDocumentDeserializer, BaseDocumentSerializer, BaseDocumentMerger, defaultStopTypes, customSerializers, SmartlingAdapter, defaultDocumentLevelConfig, defaultFieldLevelConfig, };
@@ -1,7 +1,10 @@
1
1
  'use strict';
2
2
 
3
+ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
+
3
5
  var sanityTranslationsTab = require('sanity-translations-tab');
4
6
  var sanityNaiveHtmlSerializer = require('sanity-naive-html-serializer');
7
+ var sanityClient = _interopDefault(require('part:@sanity/base/client'));
5
8
 
6
9
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
7
10
  try {
@@ -976,7 +979,7 @@ var getTranslationTask = /*#__PURE__*/function () {
976
979
  locales = smartlingTask.contentProgressReport.map(function (item) {
977
980
  return {
978
981
  localeId: item.targetLocaleId,
979
- progress: item.progress.percentComplete
982
+ progress: item.progress ? item.progress.percentComplete : 0
980
983
  };
981
984
  });
982
985
  }
@@ -1220,25 +1223,279 @@ var SmartlingAdapter = {
1220
1223
  getTranslation: getTranslation
1221
1224
  };
1222
1225
 
1226
+ var client = /*#__PURE__*/sanityClient.withConfig({
1227
+ apiVersion: '2021-03-25'
1228
+ }); //document fetch
1229
+
1230
+ var findLatestDraft = function findLatestDraft(documentId, ignoreI18n) {
1231
+ if (ignoreI18n === void 0) {
1232
+ ignoreI18n = true;
1233
+ }
1234
+
1235
+ //eliminates i18n versions
1236
+ var query = "*[_id match $id " + (ignoreI18n ? ' && (_id in path("drafts.*") || _id in path("*"))' : '') + "]";
1237
+ var params = {
1238
+ id: "*" + documentId
1239
+ };
1240
+ return client.fetch(query, params).then(function (docs) {
1241
+ var _docs$find;
1242
+
1243
+ return (_docs$find = docs.find(function (doc) {
1244
+ return doc._id.includes('draft');
1245
+ })) != null ? _docs$find : docs[0];
1246
+ });
1247
+ }; //revision fetch
1248
+
1249
+ var findDocumentAtRevision = /*#__PURE__*/function () {
1250
+ var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(documentId, rev) {
1251
+ var dataset, baseUrl, url, revisionDoc;
1252
+ return runtime_1.wrap(function _callee$(_context) {
1253
+ while (1) {
1254
+ switch (_context.prev = _context.next) {
1255
+ case 0:
1256
+ dataset = client.config().dataset;
1257
+ baseUrl = "/data/history/" + dataset + "/documents/" + documentId + "?revision=" + rev;
1258
+ url = client.getUrl(baseUrl);
1259
+ _context.next = 5;
1260
+ return fetch(url, {
1261
+ credentials: 'include'
1262
+ }).then(function (req) {
1263
+ return req.json();
1264
+ }).then(function (req) {
1265
+ return req.documents[0];
1266
+ });
1267
+
1268
+ case 5:
1269
+ revisionDoc = _context.sent;
1270
+
1271
+ if (!(!revisionDoc || revisionDoc._rev !== rev)) {
1272
+ _context.next = 12;
1273
+ break;
1274
+ }
1275
+
1276
+ baseUrl = "/data/history/" + dataset + "/documents/drafts." + documentId + "?revision=" + rev;
1277
+ url = client.getUrl(baseUrl);
1278
+ _context.next = 11;
1279
+ return fetch(url, {
1280
+ credentials: 'include'
1281
+ }).then(function (req) {
1282
+ return req.json();
1283
+ }).then(function (req) {
1284
+ return req.documents[0];
1285
+ });
1286
+
1287
+ case 11:
1288
+ revisionDoc = _context.sent;
1289
+
1290
+ case 12:
1291
+ return _context.abrupt("return", revisionDoc);
1292
+
1293
+ case 13:
1294
+ case "end":
1295
+ return _context.stop();
1296
+ }
1297
+ }
1298
+ }, _callee);
1299
+ }));
1300
+
1301
+ return function findDocumentAtRevision(_x, _x2) {
1302
+ return _ref.apply(this, arguments);
1303
+ };
1304
+ }(); //document-level patch
1305
+
1306
+ var documentLevelPatch = /*#__PURE__*/function () {
1307
+ var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(documentId, translatedFields, localeId) {
1308
+ var baseDoc, merged, targetId, i18nDoc, cleanedMerge;
1309
+ return runtime_1.wrap(function _callee2$(_context2) {
1310
+ while (1) {
1311
+ switch (_context2.prev = _context2.next) {
1312
+ case 0:
1313
+ if (!translatedFields._rev) {
1314
+ _context2.next = 6;
1315
+ break;
1316
+ }
1317
+
1318
+ _context2.next = 3;
1319
+ return findDocumentAtRevision(documentId, translatedFields._rev);
1320
+
1321
+ case 3:
1322
+ baseDoc = _context2.sent;
1323
+ _context2.next = 9;
1324
+ break;
1325
+
1326
+ case 6:
1327
+ _context2.next = 8;
1328
+ return findLatestDraft(documentId);
1329
+
1330
+ case 8:
1331
+ baseDoc = _context2.sent;
1332
+
1333
+ case 9:
1334
+ merged = sanityNaiveHtmlSerializer.BaseDocumentMerger.documentLevelMerge(translatedFields, baseDoc);
1335
+ targetId = "i18n." + documentId + "." + localeId;
1336
+ _context2.next = 13;
1337
+ return findLatestDraft(targetId, false);
1338
+
1339
+ case 13:
1340
+ i18nDoc = _context2.sent;
1341
+
1342
+ if (i18nDoc) {
1343
+ cleanedMerge = {}; //don't overwrite any existing values on the i18n doc
1344
+
1345
+ Object.entries(merged).forEach(function (_ref3) {
1346
+ var key = _ref3[0],
1347
+ value = _ref3[1];
1348
+
1349
+ if (Object.keys(translatedFields).includes(key) && !['_id', '_rev', '_updatedAt'].includes(key)) {
1350
+ cleanedMerge[key] = value;
1351
+ }
1352
+ });
1353
+ client.transaction() //@ts-ignore
1354
+ .patch(i18nDoc._id, function (p) {
1355
+ return p.set(cleanedMerge);
1356
+ }).commit();
1357
+ } else {
1358
+ merged._id = "drafts." + targetId; //account for legacy implementations of i18n plugin lang
1359
+
1360
+ if (baseDoc._lang) {
1361
+ merged._lang = localeId;
1362
+ } else if (baseDoc.__i18n_lang) {
1363
+ merged.__i18n_lang = localeId;
1364
+ }
1365
+
1366
+ client.create(merged);
1367
+ }
1368
+
1369
+ case 15:
1370
+ case "end":
1371
+ return _context2.stop();
1372
+ }
1373
+ }
1374
+ }, _callee2);
1375
+ }));
1376
+
1377
+ return function documentLevelPatch(_x3, _x4, _x5) {
1378
+ return _ref2.apply(this, arguments);
1379
+ };
1380
+ }(); //field level patch
1381
+
1382
+ var fieldLevelPatch = /*#__PURE__*/function () {
1383
+ var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(documentId, translatedFields, localeId) {
1384
+ var baseDoc, merged;
1385
+ return runtime_1.wrap(function _callee3$(_context3) {
1386
+ while (1) {
1387
+ switch (_context3.prev = _context3.next) {
1388
+ case 0:
1389
+ if (!translatedFields._rev) {
1390
+ _context3.next = 6;
1391
+ break;
1392
+ }
1393
+
1394
+ _context3.next = 3;
1395
+ return findDocumentAtRevision(documentId, translatedFields._rev);
1396
+
1397
+ case 3:
1398
+ baseDoc = _context3.sent;
1399
+ _context3.next = 9;
1400
+ break;
1401
+
1402
+ case 6:
1403
+ _context3.next = 8;
1404
+ return findLatestDraft(documentId);
1405
+
1406
+ case 8:
1407
+ baseDoc = _context3.sent;
1408
+
1409
+ case 9:
1410
+ merged = sanityNaiveHtmlSerializer.BaseDocumentMerger.fieldLevelMerge(translatedFields, baseDoc, localeId, 'en');
1411
+ client.patch(baseDoc._id).set(merged).commit();
1412
+
1413
+ case 11:
1414
+ case "end":
1415
+ return _context3.stop();
1416
+ }
1417
+ }
1418
+ }, _callee3);
1419
+ }));
1420
+
1421
+ return function fieldLevelPatch(_x6, _x7, _x8) {
1422
+ return _ref4.apply(this, arguments);
1423
+ };
1424
+ }();
1425
+
1223
1426
  var defaultDocumentLevelConfig = {
1224
- exportForTranslation: function exportForTranslation(id) {
1225
- return sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(id, 'document', 'en', sanityNaiveHtmlSerializer.defaultStopTypes, sanityNaiveHtmlSerializer.customSerializers);
1226
- },
1427
+ exportForTranslation: /*#__PURE__*/function () {
1428
+ var _exportForTranslation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(id) {
1429
+ var doc, serialized;
1430
+ return runtime_1.wrap(function _callee$(_context) {
1431
+ while (1) {
1432
+ switch (_context.prev = _context.next) {
1433
+ case 0:
1434
+ _context.next = 2;
1435
+ return findLatestDraft(id);
1436
+
1437
+ case 2:
1438
+ doc = _context.sent;
1439
+ serialized = sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(doc, 'document'); //needed for lookup by translation tab
1440
+
1441
+ serialized.name = id;
1442
+ return _context.abrupt("return", serialized);
1443
+
1444
+ case 6:
1445
+ case "end":
1446
+ return _context.stop();
1447
+ }
1448
+ }
1449
+ }, _callee);
1450
+ }));
1451
+
1452
+ function exportForTranslation(_x) {
1453
+ return _exportForTranslation.apply(this, arguments);
1454
+ }
1455
+
1456
+ return exportForTranslation;
1457
+ }(),
1227
1458
  importTranslation: function importTranslation(id, localeId, document) {
1228
- return sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(id, document).then(function (deserialized) {
1229
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher.documentLevelPatch(deserialized, id, localeId);
1230
- });
1459
+ var deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1460
+ documentLevelPatch(id, deserialized, localeId);
1231
1461
  },
1232
1462
  adapter: SmartlingAdapter
1233
1463
  };
1234
1464
  var defaultFieldLevelConfig = {
1235
- exportForTranslation: function exportForTranslation(id) {
1236
- return sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(id, 'field', 'en', sanityNaiveHtmlSerializer.defaultStopTypes, sanityNaiveHtmlSerializer.customSerializers);
1237
- },
1465
+ exportForTranslation: /*#__PURE__*/function () {
1466
+ var _exportForTranslation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(id) {
1467
+ var doc, serialized;
1468
+ return runtime_1.wrap(function _callee2$(_context2) {
1469
+ while (1) {
1470
+ switch (_context2.prev = _context2.next) {
1471
+ case 0:
1472
+ _context2.next = 2;
1473
+ return findLatestDraft(id);
1474
+
1475
+ case 2:
1476
+ doc = _context2.sent;
1477
+ serialized = sanityNaiveHtmlSerializer.BaseDocumentSerializer.serializeDocument(doc, 'field'); //needed for lookup by translation tab
1478
+
1479
+ serialized.name = id;
1480
+ return _context2.abrupt("return", serialized);
1481
+
1482
+ case 6:
1483
+ case "end":
1484
+ return _context2.stop();
1485
+ }
1486
+ }
1487
+ }, _callee2);
1488
+ }));
1489
+
1490
+ function exportForTranslation(_x2) {
1491
+ return _exportForTranslation2.apply(this, arguments);
1492
+ }
1493
+
1494
+ return exportForTranslation;
1495
+ }(),
1238
1496
  importTranslation: function importTranslation(id, localeId, document) {
1239
- return sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(id, document).then(function (deserialized) {
1240
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher.fieldLevelPatch(deserialized, id, localeId, 'en');
1241
- });
1497
+ var deserialized = sanityNaiveHtmlSerializer.BaseDocumentDeserializer.deserializeDocument(document);
1498
+ fieldLevelPatch(id, deserialized, localeId);
1242
1499
  },
1243
1500
  adapter: SmartlingAdapter
1244
1501
  };
@@ -1255,10 +1512,10 @@ Object.defineProperty(exports, 'BaseDocumentDeserializer', {
1255
1512
  return sanityNaiveHtmlSerializer.BaseDocumentDeserializer;
1256
1513
  }
1257
1514
  });
1258
- Object.defineProperty(exports, 'BaseDocumentPatcher', {
1515
+ Object.defineProperty(exports, 'BaseDocumentMerger', {
1259
1516
  enumerable: true,
1260
1517
  get: function () {
1261
- return sanityNaiveHtmlSerializer.BaseDocumentPatcher;
1518
+ return sanityNaiveHtmlSerializer.BaseDocumentMerger;
1262
1519
  }
1263
1520
  });
1264
1521
  Object.defineProperty(exports, 'BaseDocumentSerializer', {