text_nodes.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Create DOM-compatible DOM Rectangles from a simple array of strings
  2. export default class TextNodes {
  3. constructor() {
  4. this.offset = 0.0;
  5. this.char_width = global.dimensions.char.width;
  6. this.char_height = global.dimensions.char.height;
  7. this.total_width = global.mock_DOM_template[0].length * this.char_width;
  8. this.total_height = global.mock_DOM_template.length * this.char_height;
  9. this.dom_rects = [];
  10. }
  11. build() {
  12. for (let line of global.mock_DOM_text) {
  13. this.addDomRect(line);
  14. }
  15. return [
  16. {
  17. textContent: global.mock_DOM_text.join(""),
  18. parentElement: {
  19. style: {},
  20. },
  21. bounding_box: this.boundingBox(),
  22. dom_rects: this.dom_rects,
  23. },
  24. ];
  25. }
  26. boundingBox() {
  27. return {
  28. top: this.offset,
  29. bottom: this.total_height + this.offset,
  30. left: this.offset,
  31. right: this.total_width + this.offset,
  32. width: this.total_width,
  33. height: this.total_height,
  34. };
  35. }
  36. addDomRect(line) {
  37. const width = line.length * this.char_width;
  38. const height = this.char_height;
  39. const top = this.dom_rects.length * this.char_height + this.offset;
  40. this.dom_rects.push({
  41. top: top,
  42. bottom: top + height,
  43. left: this.offset,
  44. right: width + this.offset,
  45. width: width,
  46. height: height,
  47. });
  48. }
  49. }