log.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. export default {
  2. template: "<textarea disabled></textarea>",
  3. data() {
  4. return {
  5. num_lines: 0,
  6. total_count: 0,
  7. };
  8. },
  9. mounted() {
  10. const text = decodeURIComponent(this.lines);
  11. this.$el.innerHTML = text;
  12. this.num_lines = text ? text.split("\n").length : 0;
  13. this.total_count = this.num_lines;
  14. },
  15. methods: {
  16. push(line, total_count) {
  17. if (total_count === this.total_count) return;
  18. this.total_count = total_count;
  19. const decoded = decodeURIComponent(line);
  20. const textarea = this.$el;
  21. textarea.innerHTML += (this.num_lines ? "\n" : "") + decoded;
  22. textarea.scrollTop = textarea.scrollHeight;
  23. this.num_lines += decoded.split("\n").length;
  24. while (this.max_lines && this.num_lines > this.max_lines) {
  25. const index = textarea.innerHTML.indexOf("\n");
  26. if (index == -1) break;
  27. textarea.innerHTML = textarea.innerHTML.slice(index + 1);
  28. this.num_lines -= 1;
  29. }
  30. },
  31. clear() {
  32. const textarea = this.$el;
  33. textarea.innerHTML = "";
  34. this.num_lines = 0;
  35. },
  36. },
  37. props: {
  38. max_lines: Number,
  39. lines: String,
  40. },
  41. };