maction.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2018-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 Implements the CHTMLmaction wrapper for the MmlMaction object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {CHTMLWrapper, CHTMLConstructor} from '../Wrapper.js';
  23. import {CommonMactionMixin} from '../../common/Wrappers/maction.js';
  24. import {ActionDef} from '../../common/Wrappers/maction.js';
  25. import {EventHandler, TooltipData} from '../../common/Wrappers/maction.js';
  26. import {MmlMaction} from '../../../core/MmlTree/MmlNodes/maction.js';
  27. import {TextNode} from '../../../core/MmlTree/MmlNode.js';
  28. import {StyleList} from '../../../util/StyleList.js';
  29. /*****************************************************************/
  30. /**
  31. * The CHTMLmaction wrapper for the MmlMaction object
  32. *
  33. * @template N The HTMLElement node class
  34. * @template T The Text node class
  35. * @template D The Document class
  36. */
  37. // @ts-ignore
  38. export class CHTMLmaction<N, T, D> extends
  39. CommonMactionMixin<CHTMLWrapper<any, any, any>, CHTMLConstructor<any, any, any>>(CHTMLWrapper) {
  40. /**
  41. * The maction wrapper
  42. */
  43. public static kind = MmlMaction.prototype.kind;
  44. /**
  45. * @override
  46. */
  47. public static styles: StyleList = {
  48. 'mjx-maction': {
  49. position: 'relative'
  50. },
  51. 'mjx-maction > mjx-tool': {
  52. display: 'none',
  53. position: 'absolute',
  54. bottom: 0, right: 0,
  55. width: 0, height: 0,
  56. 'z-index': 500
  57. },
  58. 'mjx-tool > mjx-tip': {
  59. display: 'inline-block',
  60. padding: '.2em',
  61. border: '1px solid #888',
  62. 'font-size': '70%',
  63. 'background-color': '#F8F8F8',
  64. color: 'black',
  65. 'box-shadow': '2px 2px 5px #AAAAAA'
  66. },
  67. 'mjx-maction[toggle]': {
  68. cursor: 'pointer'
  69. },
  70. 'mjx-status': {
  71. display: 'block',
  72. position: 'fixed',
  73. left: '1em',
  74. bottom: '1em',
  75. 'min-width': '25%',
  76. padding: '.2em .4em',
  77. border: '1px solid #888',
  78. 'font-size': '90%',
  79. 'background-color': '#F8F8F8',
  80. color: 'black'
  81. }
  82. };
  83. /**
  84. * The valid action types and their handlers
  85. */
  86. public static actions = new Map([
  87. ['toggle', [(node, _data) => {
  88. //
  89. // Mark which child is selected
  90. //
  91. node.adaptor.setAttribute(node.chtml, 'toggle', node.node.attributes.get('selection') as string);
  92. //
  93. // Cache the data needed to select another node
  94. //
  95. const math = node.factory.jax.math;
  96. const document = node.factory.jax.document;
  97. const mml = node.node as MmlMaction;
  98. //
  99. // Add a click handler that changes the selection and rerenders the expression
  100. //
  101. node.setEventHandler('click', (event: Event) => {
  102. if (!math.end.node) {
  103. //
  104. // If the MathItem was created by hand, it might not have a node
  105. // telling it where to replace the existing math, so set it.
  106. //
  107. math.start.node = math.end.node = math.typesetRoot;
  108. math.start.n = math.end.n = 0;
  109. }
  110. mml.nextToggleSelection();
  111. math.rerender(document);
  112. event.stopPropagation();
  113. });
  114. }, {}]],
  115. ['tooltip', [(node, data) => {
  116. const tip = node.childNodes[1];
  117. if (!tip) return;
  118. if (tip.node.isKind('mtext')) {
  119. //
  120. // Text tooltips are handled through title attributes
  121. //
  122. const text = (tip.node as TextNode).getText();
  123. node.adaptor.setAttribute(node.chtml, 'title', text);
  124. } else {
  125. //
  126. // Math tooltips are handled through hidden nodes and event handlers
  127. //
  128. const adaptor = node.adaptor;
  129. const tool = adaptor.append(node.chtml, node.html('mjx-tool', {
  130. style: {bottom: node.em(-node.dy), right: node.em(-node.dx)}
  131. }, [node.html('mjx-tip')]));
  132. tip.toCHTML(adaptor.firstChild(tool));
  133. //
  134. // Set up the event handlers to display and remove the tooltip
  135. //
  136. node.setEventHandler('mouseover', (event: Event) => {
  137. data.stopTimers(node, data);
  138. const timeout = setTimeout(() => adaptor.setStyle(tool, 'display', 'block'), data.postDelay);
  139. data.hoverTimer.set(node, timeout);
  140. event.stopPropagation();
  141. });
  142. node.setEventHandler('mouseout', (event: Event) => {
  143. data.stopTimers(node, data);
  144. const timeout = setTimeout(() => adaptor.setStyle(tool, 'display', ''), data.clearDelay);
  145. data.clearTimer.set(node, timeout);
  146. event.stopPropagation();
  147. });
  148. }
  149. }, TooltipData]],
  150. ['statusline', [(node, data) => {
  151. const tip = node.childNodes[1];
  152. if (!tip) return;
  153. if (tip.node.isKind('mtext')) {
  154. const adaptor = node.adaptor;
  155. const text = (tip.node as TextNode).getText();
  156. adaptor.setAttribute(node.chtml, 'statusline', text);
  157. //
  158. // Set up event handlers to change the status window
  159. //
  160. node.setEventHandler('mouseover', (event: Event) => {
  161. if (data.status === null) {
  162. const body = adaptor.body(adaptor.document);
  163. data.status = adaptor.append(body, node.html('mjx-status', {}, [node.text(text)]));
  164. }
  165. event.stopPropagation();
  166. });
  167. node.setEventHandler('mouseout', (event: Event) => {
  168. if (data.status) {
  169. adaptor.remove(data.status);
  170. data.status = null;
  171. }
  172. event.stopPropagation();
  173. });
  174. }
  175. }, {
  176. status: null // cached status line
  177. }]]
  178. ] as ActionDef<CHTMLmaction<any, any, any>>[]);
  179. /*************************************************************/
  180. /**
  181. * @override
  182. */
  183. public toCHTML(parent: N) {
  184. const chtml = this.standardCHTMLnode(parent);
  185. const child = this.selected;
  186. child.toCHTML(chtml);
  187. this.action(this, this.data);
  188. }
  189. /**
  190. * Add an event handler to the output for this maction
  191. */
  192. public setEventHandler(type: string, handler: EventHandler) {
  193. (this.chtml as any).addEventListener(type, handler);
  194. }
  195. }