Front end of the Slack clone application.

CodeNode.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const getNumberOfLines = require("./helpers").getNumberOfLines;
  7. const getUnfinishedLine = require("./helpers").getUnfinishedLine;
  8. class CodeNode {
  9. constructor(generatedCode) {
  10. this.generatedCode = generatedCode;
  11. }
  12. clone() {
  13. return new CodeNode(this.generatedCode);
  14. }
  15. getGeneratedCode() {
  16. return this.generatedCode;
  17. }
  18. getMappings(mappingsContext) {
  19. const lines = getNumberOfLines(this.generatedCode);
  20. const mapping = Array(lines+1).join(";");
  21. if(lines > 0) {
  22. mappingsContext.unfinishedGeneratedLine = getUnfinishedLine(this.generatedCode);
  23. if(mappingsContext.unfinishedGeneratedLine > 0) {
  24. return mapping + "A";
  25. } else {
  26. return mapping;
  27. }
  28. } else {
  29. const prevUnfinished = mappingsContext.unfinishedGeneratedLine;
  30. mappingsContext.unfinishedGeneratedLine += getUnfinishedLine(this.generatedCode);
  31. if(prevUnfinished === 0 && mappingsContext.unfinishedGeneratedLine > 0) {
  32. return "A";
  33. } else {
  34. return "";
  35. }
  36. }
  37. }
  38. addGeneratedCode(generatedCode) {
  39. this.generatedCode += generatedCode;
  40. }
  41. mapGeneratedCode(fn) {
  42. const generatedCode = fn(this.generatedCode);
  43. return new CodeNode(generatedCode);
  44. }
  45. getNormalizedNodes() {
  46. return [this];
  47. }
  48. merge(otherNode) {
  49. if(otherNode instanceof CodeNode) {
  50. this.generatedCode += otherNode.generatedCode;
  51. return this;
  52. }
  53. return false;
  54. }
  55. }
  56. module.exports = CodeNode;