text.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const { createReadStream } = require('fs');
  2. const ReadLine = require('readline');
  3. const { extname } = require('path');
  4. const MAX_LINES = 400;
  5. const MAX_LENGTH = 20000;
  6. exports.template = /* html */`
  7. <section class="asset-text">
  8. <ui-code language="xml"></ui-code>
  9. <ui-markdown></ui-markdown>
  10. </section>`;
  11. exports.$ = {
  12. container: '.asset-text',
  13. code: 'ui-code',
  14. markdown: 'ui-markdown',
  15. };
  16. exports.style = /* css */`
  17. .asset-text {
  18. flex: 1;
  19. display: flex;
  20. flex-direction: column;
  21. /* it is necessary */
  22. height: 0px;
  23. }
  24. .asset-text > ui-code {
  25. flex: 1;
  26. }
  27. `;
  28. exports.methods = {
  29. renderContent(content, type = '.txt') {
  30. const panel = this;
  31. const domMap = {
  32. '.txt': panel.$.code,
  33. '.html': panel.$.code,
  34. '.htm': panel.$.code,
  35. '.xml': panel.$.code,
  36. '.css': panel.$.code,
  37. '.less': panel.$.code,
  38. '.scss': panel.$.code,
  39. '.stylus': panel.$.code,
  40. '.yaml': panel.$.code,
  41. '.ini': panel.$.code,
  42. '.csv': panel.$.code,
  43. '.proto': panel.$.code,
  44. '.ts': panel.$.code,
  45. '.tsx': panel.$.code,
  46. '.md': panel.$.markdown,
  47. '.markdown': panel.$.markdown,
  48. };
  49. Object.values(domMap).forEach(dom => {
  50. if (dom) {
  51. dom.style.display = 'none';
  52. dom.textContent = '';
  53. }
  54. });
  55. if (domMap[type]) {
  56. domMap[type].textContent = content;
  57. domMap[type].style.display = 'block';
  58. }
  59. },
  60. };
  61. exports.update = function(assetList, metaList) {
  62. this.assetList = assetList;
  63. this.metaList = metaList;
  64. this.meta = metaList[0];
  65. this.asset = assetList[0];
  66. let display = 'none';
  67. if (assetList.length === 1) {
  68. display = 'flex';
  69. }
  70. this.$.container.style.display = display;
  71. if (display === 'none') {
  72. return;
  73. }
  74. // Displays 400 lines or 20,000 characters
  75. let remainLines = MAX_LINES;
  76. let remainLength = MAX_LENGTH;
  77. let text = '';
  78. const readStream = createReadStream(this.asset.file, {
  79. encoding: 'utf-8',
  80. });
  81. const readLineStream = ReadLine.createInterface({
  82. input: readStream,
  83. setEncoding: 'utf-8',
  84. });
  85. readLineStream.on('line', (line) => {
  86. const lineLength = line.length;
  87. if (lineLength > remainLength) {
  88. line = line.substring(0, remainLength);
  89. remainLength = 0;
  90. } else {
  91. remainLength -= lineLength;
  92. }
  93. remainLines--;
  94. text += `${line}\n`;
  95. if (remainLines <= 0 || remainLength <= 0) {
  96. text += '...\n';
  97. readLineStream.close();
  98. readStream.close();
  99. }
  100. });
  101. readLineStream.on('close', (err) => {
  102. if (err) {
  103. throw err;
  104. }
  105. this.renderContent(text, extname(this.asset.name));
  106. });
  107. };