domOutput.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { Output } from './output';
  2. export class DOMOutput extends Output {
  3. constructor (selector) {
  4. super();
  5. let id = selector;
  6. if (selector[0] == '#') {
  7. id = selector.substring(1);
  8. }
  9. this.el = document.getElementById(id);
  10. this.currentLine = null;
  11. }
  12. write (text, newLine = false) {
  13. if (this.currentLine == null) {
  14. const span = document.createElement('span');
  15. span.classList.add('ivprog-io-output-text');
  16. this.el.append(span);
  17. this.currentLine = span;
  18. }
  19. this.currentLine.innerHTML += text;
  20. if (newLine) {
  21. this.currentLine = null;
  22. }
  23. }
  24. sendOutput (text) {
  25. let output = '' + text;
  26. if (output.indexOf('\n') !== -1) {
  27. const outputList = output.split('\n');
  28. let last = outputList.pop();
  29. outputList.forEach( t => {
  30. t = t.replace(/\t/g,'  ');
  31. t = t.replace(/\s/g," ");
  32. if (t.length == 0)
  33. t = " ";
  34. this.write(t, true);
  35. });
  36. last = last.replace(/\t/g, '  ');
  37. if (last.length != 0)
  38. this.write(last);
  39. } else {
  40. output = output.replace(/\t/g, '  ');
  41. this.write(output);
  42. }
  43. }
  44. clear () {
  45. this.currentLine = null;
  46. while(this.el.childNodes.length > 0) {
  47. this.el.removeChild(this.el.firstChild);
  48. }
  49. }
  50. }