image-preview.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict';
  2. exports.template = /* html */`
  3. <ui-section header="i18n:ENGINE.inspector.preview.header" class="preview-section config no-padding" expand>
  4. <div class="image-preview">
  5. <div class="image-content">
  6. <ui-asset-image class="image" size="origin"></ui-asset-image>
  7. </div>
  8. <div class="label">
  9. <span class="size"></span>
  10. </div>
  11. </div>
  12. </ui-section>
  13. `;
  14. exports.style = /* css */`
  15. .preview-section {
  16. margin-top: 0px;
  17. }
  18. .image-preview {
  19. position: relative;
  20. display: flex;
  21. }
  22. .image-preview > .image-content {
  23. height: var(--inspector-footer-preview-height, 200px);
  24. background: var(--color-normal-fill-emphasis);
  25. box-sizing: border-box;
  26. }
  27. .image-preview > .image {
  28. width: 100%;
  29. height: 100%;
  30. }
  31. .image-preview > .label {
  32. position: absolute;
  33. width: 100%;
  34. left: 0;
  35. bottom: 0;
  36. text-align: center;
  37. }
  38. .image-preview > .label > .size {
  39. font-size: 10px;
  40. padding: 2px 8px;
  41. background-color: var(--color-primary-fill);
  42. color: var(--color-primary-contrast-weakest);
  43. border-radius: calc(var(--size-normal-radius) * 1px);
  44. }
  45. .image-preview > .label > .size:empty {
  46. display: none;
  47. }
  48. `;
  49. exports.$ = {
  50. container: '.image-preview',
  51. image: '.image',
  52. size: '.size',
  53. };
  54. /**
  55. * attribute corresponds to the edit element
  56. */
  57. const Elements = {
  58. image: {
  59. ready() {
  60. const $uiImg = this.$.image.$uiImage;
  61. $uiImg.setAttribute('show-alpha', true);
  62. const $img = $uiImg.$img;
  63. $img.addEventListener('load', () => {
  64. this.$.size.innerHTML = `${$img.naturalWidth} x ${$img.naturalHeight}`;
  65. });
  66. },
  67. update() {
  68. const panel = this;
  69. panel.$.image.value = panel.asset.uuid;
  70. this.$.size.innerHTML = '';
  71. },
  72. },
  73. };
  74. exports.ready = function() {
  75. for (const prop in Elements) {
  76. const element = Elements[prop];
  77. if (element.ready) {
  78. element.ready.call(this);
  79. }
  80. }
  81. };
  82. exports.update = function(assetList, metaList) {
  83. this.assetList = assetList;
  84. this.metaList = metaList;
  85. this.asset = assetList[0];
  86. this.meta = metaList[0];
  87. // 如何多选就隐藏预览
  88. if (assetList.length > 1) {
  89. this.$.container.style.display = 'none';
  90. } else {
  91. this.$.container.style.display = 'block';
  92. }
  93. for (const prop in Elements) {
  94. const element = Elements[prop];
  95. if (element.update) {
  96. element.update.call(this);
  97. }
  98. }
  99. };