svg_v3_highlighter.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SvgV3Highlighter = void 0;
  4. const DomUtil = require("../common/dom_util.js");
  5. const XpathUtil = require("../common/xpath_util.js");
  6. const color_picker_js_1 = require("./color_picker.js");
  7. const svg_highlighter_js_1 = require("./svg_highlighter.js");
  8. class SvgV3Highlighter extends svg_highlighter_js_1.SvgHighlighter {
  9. constructor() {
  10. super();
  11. this.mactionName = 'maction';
  12. }
  13. highlightNode(node) {
  14. let info;
  15. if (this.isHighlighted(node)) {
  16. info = {
  17. node: node,
  18. background: this.colorString().background,
  19. foreground: this.colorString().foreground
  20. };
  21. return info;
  22. }
  23. if (node.tagName === 'svg' || node.tagName === 'MJX-CONTAINER') {
  24. info = {
  25. node: node,
  26. background: node.style.backgroundColor,
  27. foreground: node.style.color
  28. };
  29. node.style.backgroundColor = this.colorString().background;
  30. node.style.color = this.colorString().foreground;
  31. return info;
  32. }
  33. const rect = DomUtil.createElementNS('http://www.w3.org/2000/svg', 'rect');
  34. rect.setAttribute('sre-highlighter-added', 'true');
  35. const padding = 40;
  36. const bbox = node.getBBox();
  37. rect.setAttribute('x', (bbox.x - padding).toString());
  38. rect.setAttribute('y', (bbox.y - padding).toString());
  39. rect.setAttribute('width', (bbox.width + 2 * padding).toString());
  40. rect.setAttribute('height', (bbox.height + 2 * padding).toString());
  41. const transform = node.getAttribute('transform');
  42. if (transform) {
  43. rect.setAttribute('transform', transform);
  44. }
  45. rect.setAttribute('fill', this.colorString().background);
  46. node.setAttribute(this.ATTR, 'true');
  47. node.parentNode.insertBefore(rect, node);
  48. info = { node: node, foreground: node.getAttribute('fill') };
  49. if (node.nodeName === 'rect') {
  50. const picker = new color_picker_js_1.ColorPicker({ alpha: 0, color: 'black' });
  51. node.setAttribute('fill', picker.rgba().foreground);
  52. }
  53. else {
  54. node.setAttribute('fill', this.colorString().foreground);
  55. }
  56. return info;
  57. }
  58. unhighlightNode(info) {
  59. const previous = info.node.previousSibling;
  60. if (previous && previous.hasAttribute('sre-highlighter-added')) {
  61. info.foreground
  62. ? info.node.setAttribute('fill', info.foreground)
  63. : info.node.removeAttribute('fill');
  64. info.node.parentNode.removeChild(previous);
  65. return;
  66. }
  67. info.node.style.backgroundColor = info.background;
  68. info.node.style.color = info.foreground;
  69. }
  70. isMactionNode(node) {
  71. return node.getAttribute('data-mml-node') === this.mactionName;
  72. }
  73. getMactionNodes(node) {
  74. return Array.from(XpathUtil.evalXPath(`.//*[@data-mml-node="${this.mactionName}"]`, node));
  75. }
  76. }
  77. exports.SvgV3Highlighter = SvgV3Highlighter;