TreeExplorer.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2009-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Tree Explorers allow to switch on effects on the entire
  19. * expression tree.
  20. *
  21. * @author v.sorge@mathjax.org (Volker Sorge)
  22. */
  23. import {A11yDocument, Region} from './Region.js';
  24. import {Explorer, AbstractExplorer} from './Explorer.js';
  25. import Sre from '../sre.js';
  26. export interface TreeExplorer extends Explorer {
  27. }
  28. export class AbstractTreeExplorer extends AbstractExplorer<void> {
  29. /**
  30. * @override
  31. */
  32. protected constructor(public document: A11yDocument,
  33. protected region: Region<void>,
  34. protected node: HTMLElement,
  35. protected mml: HTMLElement) {
  36. super(document, null, node);
  37. }
  38. /**
  39. * @override
  40. */
  41. public readonly stoppable = false;
  42. /**
  43. * @override
  44. */
  45. public Attach() {
  46. super.Attach();
  47. this.Start();
  48. }
  49. /**
  50. * @override
  51. */
  52. public Detach() {
  53. this.Stop();
  54. super.Detach();
  55. }
  56. }
  57. export class FlameColorer extends AbstractTreeExplorer {
  58. /**
  59. * @override
  60. */
  61. public Start() {
  62. if (this.active) return;
  63. this.active = true;
  64. this.highlighter.highlightAll(this.node);
  65. }
  66. /**
  67. * @override
  68. */
  69. public Stop() {
  70. if (this.active) {
  71. this.highlighter.unhighlightAll();
  72. }
  73. this.active = false;
  74. }
  75. }
  76. export class TreeColorer extends AbstractTreeExplorer {
  77. /**
  78. * @override
  79. */
  80. public Start() {
  81. if (this.active) return;
  82. this.active = true;
  83. let generator = Sre.getSpeechGenerator('Color');
  84. if (!this.node.hasAttribute('hasforegroundcolor')) {
  85. generator.generateSpeech(this.node, this.mml);
  86. this.node.setAttribute('hasforegroundcolor', 'true');
  87. }
  88. // TODO: Make this cleaner in Sre.
  89. (this.highlighter as any).colorizeAll(this.node);
  90. }
  91. /**
  92. * @override
  93. */
  94. public Stop() {
  95. if (this.active) {
  96. (this.highlighter as any).uncolorizeAll(this.node);
  97. }
  98. this.active = false;
  99. }
  100. }