parseSource.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. var isRef = function (src) {
  15. var source = src;
  16. return source ? typeof source._ref === 'string' : false;
  17. };
  18. var isAsset = function (src) {
  19. var source = src;
  20. return source ? typeof source._id === 'string' : false;
  21. };
  22. var isAssetStub = function (src) {
  23. var source = src;
  24. return source && source.asset ? typeof source.asset.url === 'string' : false;
  25. };
  26. // Convert an asset-id, asset or image to an image record suitable for processing
  27. // eslint-disable-next-line complexity
  28. function parseSource(source) {
  29. if (!source) {
  30. return null;
  31. }
  32. var image;
  33. if (typeof source === 'string' && isUrl(source)) {
  34. // Someone passed an existing image url?
  35. image = {
  36. asset: { _ref: urlToId(source) },
  37. };
  38. }
  39. else if (typeof source === 'string') {
  40. // Just an asset id
  41. image = {
  42. asset: { _ref: source },
  43. };
  44. }
  45. else if (isRef(source)) {
  46. // We just got passed an asset directly
  47. image = {
  48. asset: source,
  49. };
  50. }
  51. else if (isAsset(source)) {
  52. // If we were passed an image asset document
  53. image = {
  54. asset: {
  55. _ref: source._id || '',
  56. },
  57. };
  58. }
  59. else if (isAssetStub(source)) {
  60. // If we were passed a partial asset (`url`, but no `_id`)
  61. image = {
  62. asset: {
  63. _ref: urlToId(source.asset.url),
  64. },
  65. };
  66. }
  67. else if (typeof source.asset === 'object') {
  68. // Probably an actual image with materialized asset
  69. image = __assign({}, source);
  70. }
  71. else {
  72. // We got something that does not look like an image, or it is an image
  73. // that currently isn't sporting an asset.
  74. return null;
  75. }
  76. var img = source;
  77. if (img.crop) {
  78. image.crop = img.crop;
  79. }
  80. if (img.hotspot) {
  81. image.hotspot = img.hotspot;
  82. }
  83. return applyDefaults(image);
  84. }
  85. exports.default = parseSource;
  86. function isUrl(url) {
  87. return /^https?:\/\//.test("".concat(url));
  88. }
  89. function urlToId(url) {
  90. var parts = url.split('/').slice(-1);
  91. return "image-".concat(parts[0]).replace(/\.([a-z]+)$/, '-$1');
  92. }
  93. // Mock crop and hotspot if image lacks it
  94. function applyDefaults(image) {
  95. if (image.crop && image.hotspot) {
  96. return image;
  97. }
  98. // We need to pad in default values for crop or hotspot
  99. var result = __assign({}, image);
  100. if (!result.crop) {
  101. result.crop = {
  102. left: 0,
  103. top: 0,
  104. bottom: 0,
  105. right: 0,
  106. };
  107. }
  108. if (!result.hotspot) {
  109. result.hotspot = {
  110. x: 0.5,
  111. y: 0.5,
  112. height: 1.0,
  113. width: 1.0,
  114. };
  115. }
  116. return result;
  117. }
  118. //# sourceMappingURL=parseSource.js.map