effect-header.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. const { createReadStream } = require('fs');
  3. const ReadLine = require('readline');
  4. const MAX_LINES = 400;
  5. const MAX_LENGTH = 20000;
  6. exports.template = /* html */`
  7. <section class="asset-effect-header">
  8. <ui-code language="glsl"></ui-code>
  9. <ui-label class="multiple-warn-tip" value="i18n:ENGINE.assets.multipleWarning"></ui-label>
  10. </section>
  11. `;
  12. exports.style = /* css */`
  13. .asset-effect-header {
  14. flex: 1;
  15. display: flex;
  16. flex-direction: column;
  17. margin-bottom: 4px;
  18. /* it is necessary */
  19. height: 0px;
  20. }
  21. .asset-effect-header[multiple-invalid] > *:not(.multiple-warn-tip) {
  22. display: none!important;
  23. }
  24. .asset-effect-header[multiple-invalid] > .multiple-warn-tip {
  25. display: block;
  26. }
  27. .asset-effect-header .multiple-warn-tip {
  28. display: none;
  29. text-align: center;
  30. color: var(--color-focus-contrast-weakest);
  31. margin-top: 8px;
  32. }
  33. .asset-effect-header > ui-code {
  34. flex: 1;
  35. }
  36. `;
  37. exports.$ = {
  38. container: '.asset-effect-header',
  39. code: 'ui-code',
  40. };
  41. exports.ready = function() {
  42. this.code = this.$.code;
  43. };
  44. exports.update = function(assetList, metaList) {
  45. this.assetList = assetList;
  46. this.metaList = metaList;
  47. this.meta = metaList[0];
  48. this.asset = assetList[0];
  49. if (assetList.length > 1) {
  50. this.$.container.setAttribute('multiple-invalid', '');
  51. return;
  52. } else {
  53. this.$.container.removeAttribute('multiple-invalid');
  54. }
  55. // Displays 400 lines or 20,000 characters
  56. const readStream = createReadStream(this.asset.file, {
  57. encoding: 'utf-8',
  58. });
  59. let remainLines = MAX_LINES;
  60. let remainLength = MAX_LENGTH;
  61. let text = '';
  62. const readLineStream = ReadLine.createInterface({
  63. input: readStream,
  64. setEncoding: 'utf-8',
  65. });
  66. readLineStream.on('line', (line) => {
  67. const lineLength = line.length;
  68. if (lineLength > remainLength) {
  69. line = line.substr(0, remainLength);
  70. remainLength = 0;
  71. } else {
  72. remainLength -= lineLength;
  73. }
  74. remainLines--;
  75. text += `${line}\n`;
  76. if (remainLines <= 0 || remainLength <= 0) {
  77. text += '...\n';
  78. readLineStream.close();
  79. readStream.close();
  80. }
  81. });
  82. readLineStream.on('close', (err) => {
  83. if (err) {
  84. throw err;
  85. }
  86. if (this.code) {
  87. this.code.textContent = text;
  88. }
  89. });
  90. };