abstract_highlighter.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AbstractHighlighter = void 0;
  4. const XpathUtil = require("../common/xpath_util.js");
  5. const enrich_attr_js_1 = require("../enrich_mathml/enrich_attr.js");
  6. let counter = 0;
  7. class AbstractHighlighter {
  8. constructor() {
  9. this.counter = counter++;
  10. this.ATTR = 'sre-highlight-' + this.counter.toString();
  11. this.color = null;
  12. this.mactionName = '';
  13. this.currentHighlights = [];
  14. }
  15. highlight(nodes) {
  16. this.currentHighlights.push(nodes.map((node) => {
  17. const info = this.highlightNode(node);
  18. this.setHighlighted(node);
  19. return info;
  20. }));
  21. }
  22. highlightAll(node) {
  23. const mactions = this.getMactionNodes(node);
  24. for (let i = 0, maction; (maction = mactions[i]); i++) {
  25. this.highlight([maction]);
  26. }
  27. }
  28. unhighlight() {
  29. const nodes = this.currentHighlights.pop();
  30. if (!nodes) {
  31. return;
  32. }
  33. nodes.forEach((highlight) => {
  34. if (this.isHighlighted(highlight.node)) {
  35. this.unhighlightNode(highlight);
  36. this.unsetHighlighted(highlight.node);
  37. }
  38. });
  39. }
  40. unhighlightAll() {
  41. while (this.currentHighlights.length > 0) {
  42. this.unhighlight();
  43. }
  44. }
  45. setColor(color) {
  46. this.color = color;
  47. }
  48. colorString() {
  49. return this.color.rgba();
  50. }
  51. addEvents(node, events) {
  52. const mactions = this.getMactionNodes(node);
  53. for (let i = 0, maction; (maction = mactions[i]); i++) {
  54. for (const [key, event] of Object.entries(events)) {
  55. maction.addEventListener(key, event);
  56. }
  57. }
  58. }
  59. getMactionNodes(node) {
  60. return Array.from(node.getElementsByClassName(this.mactionName));
  61. }
  62. isMactionNode(node) {
  63. const className = node.className || node.getAttribute('class');
  64. return className ? !!className.match(new RegExp(this.mactionName)) : false;
  65. }
  66. isHighlighted(node) {
  67. return node.hasAttribute(this.ATTR);
  68. }
  69. setHighlighted(node) {
  70. node.setAttribute(this.ATTR, 'true');
  71. }
  72. unsetHighlighted(node) {
  73. node.removeAttribute(this.ATTR);
  74. }
  75. colorizeAll(node) {
  76. XpathUtil.updateEvaluator(node);
  77. const allNodes = XpathUtil.evalXPath(`.//*[@${enrich_attr_js_1.Attribute.ID}]`, node);
  78. allNodes.forEach((x) => this.colorize(x));
  79. }
  80. uncolorizeAll(node) {
  81. const allNodes = XpathUtil.evalXPath(`.//*[@${enrich_attr_js_1.Attribute.ID}]`, node);
  82. allNodes.forEach((x) => this.uncolorize(x));
  83. }
  84. colorize(node) {
  85. const fore = (0, enrich_attr_js_1.addPrefix)('foreground');
  86. if (node.hasAttribute(fore)) {
  87. node.setAttribute(fore + '-old', node.style.color);
  88. node.style.color = node.getAttribute(fore);
  89. }
  90. }
  91. uncolorize(node) {
  92. const fore = (0, enrich_attr_js_1.addPrefix)('foreground') + '-old';
  93. if (node.hasAttribute(fore)) {
  94. node.style.color = node.getAttribute(fore);
  95. }
  96. }
  97. }
  98. exports.AbstractHighlighter = AbstractHighlighter;