maction.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 SVGmaction wrapper for the MmlMaction object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {SVGWrapper, SVGConstructor} 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, AbstractMmlNode} from '../../../core/MmlTree/MmlNode.js';
  28. import {StyleList} from '../../../util/StyleList.js';
  29. /*****************************************************************/
  30. /**
  31. * The SVGmaction 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 SVGmaction<N, T, D> extends
  39. CommonMactionMixin<SVGWrapper<any, any, any>, SVGConstructor<any, any, any>>(SVGWrapper) {
  40. /**
  41. * The maction wrapper
  42. */
  43. public static kind = MmlMaction.prototype.kind;
  44. /**
  45. * @override
  46. */
  47. public static styles: StyleList = {
  48. '[jax="SVG"] mjx-tool': {
  49. display: 'inline-block',
  50. position: 'relative',
  51. width: 0, height: 0
  52. },
  53. '[jax="SVG"] mjx-tool > mjx-tip': {
  54. position: 'absolute',
  55. top: 0, left: 0
  56. },
  57. 'mjx-tool > mjx-tip': {
  58. display: 'inline-block',
  59. padding: '.2em',
  60. border: '1px solid #888',
  61. 'font-size': '70%',
  62. 'background-color': '#F8F8F8',
  63. color: 'black',
  64. 'box-shadow': '2px 2px 5px #AAAAAA'
  65. },
  66. 'g[data-mml-node="maction"][data-toggle]': {
  67. cursor: 'pointer'
  68. },
  69. 'mjx-status': {
  70. display: 'block',
  71. position: 'fixed',
  72. left: '1em',
  73. bottom: '1em',
  74. 'min-width': '25%',
  75. padding: '.2em .4em',
  76. border: '1px solid #888',
  77. 'font-size': '90%',
  78. 'background-color': '#F8F8F8',
  79. color: 'black'
  80. }
  81. };
  82. /**
  83. * The valid action types and their handlers
  84. */
  85. public static actions = new Map([
  86. ['toggle', [(node, _data) => {
  87. //
  88. // Mark which child is selected
  89. //
  90. node.adaptor.setAttribute(node.element, 'data-toggle', node.node.attributes.get('selection') as string);
  91. //
  92. // Cache the data needed to select another node
  93. //
  94. const math = node.factory.jax.math;
  95. const document = node.factory.jax.document;
  96. const mml = node.node as MmlMaction;
  97. //
  98. // Add a click handler that changes the selection and rerenders the expression
  99. //
  100. node.setEventHandler('click', (event: Event) => {
  101. if (!math.end.node) {
  102. //
  103. // If the MathItem was created by hand, it might not have a node
  104. // telling it where to replace the existing math, so set it.
  105. //
  106. math.start.node = math.end.node = math.typesetRoot;
  107. math.start.n = math.end.n = 0;
  108. }
  109. mml.nextToggleSelection();
  110. math.rerender(document);
  111. event.stopPropagation();
  112. });
  113. }, {}]],
  114. ['tooltip', [(node, data) => {
  115. const tip = node.childNodes[1];
  116. if (!tip) return;
  117. const rect = node.firstChild();
  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.insert(node.svg('title', {}, [node.text(text)]), rect);
  124. } else {
  125. //
  126. // Math tooltips are handled through hidden nodes and event handlers
  127. //
  128. const adaptor = node.adaptor;
  129. const container = node.jax.container;
  130. const math = (node.node as AbstractMmlNode).factory.create('math', {}, [node.childNodes[1].node]);
  131. const tool = node.html('mjx-tool', {}, [node.html('mjx-tip')]);
  132. const hidden = adaptor.append(rect, node.svg('foreignObject', {style: {display: 'none'}}, [tool]));
  133. node.jax.processMath(math, adaptor.firstChild(tool));
  134. node.childNodes[1].node.parent = node.node;
  135. //
  136. // Set up the event handlers to display and remove the tooltip
  137. //
  138. node.setEventHandler('mouseover', (event: Event) => {
  139. data.stopTimers(node, data);
  140. data.hoverTimer.set(node, setTimeout(() => {
  141. adaptor.setStyle(tool, 'left', '0');
  142. adaptor.setStyle(tool, 'top', '0');
  143. adaptor.append(container, tool);
  144. const tbox = adaptor.nodeBBox(tool);
  145. const nbox = adaptor.nodeBBox(node.element);
  146. const dx = (nbox.right - tbox.left) / node.metrics.em + node.dx;
  147. const dy = (nbox.bottom - tbox.bottom) / node.metrics.em + node.dy;
  148. adaptor.setStyle(tool, 'left', node.px(dx));
  149. adaptor.setStyle(tool, 'top', node.px(dy));
  150. }, data.postDelay));
  151. event.stopPropagation();
  152. });
  153. node.setEventHandler('mouseout', (event: Event) => {
  154. data.stopTimers(node, data);
  155. const timer = setTimeout(() => adaptor.append(hidden, tool), data.clearDelay);
  156. data.clearTimer.set(node, timer);
  157. event.stopPropagation();
  158. });
  159. }
  160. }, TooltipData]],
  161. ['statusline', [(node, data) => {
  162. const tip = node.childNodes[1];
  163. if (!tip) return;
  164. if (tip.node.isKind('mtext')) {
  165. const adaptor = node.adaptor;
  166. const text = (tip.node as TextNode).getText();
  167. adaptor.setAttribute(node.element, 'data-statusline', text);
  168. //
  169. // Set up event handlers to change the status window
  170. //
  171. node.setEventHandler('mouseover', (event: Event) => {
  172. if (data.status === null) {
  173. const body = adaptor.body(adaptor.document);
  174. data.status = adaptor.append(body, node.html('mjx-status', {}, [node.text(text)]));
  175. }
  176. event.stopPropagation();
  177. });
  178. node.setEventHandler('mouseout', (event: Event) => {
  179. if (data.status) {
  180. adaptor.remove(data.status);
  181. data.status = null;
  182. }
  183. event.stopPropagation();
  184. });
  185. }
  186. }, {
  187. status: null // cached status line
  188. }]]
  189. ] as ActionDef<SVGmaction<any, any, any>>[]);
  190. /*************************************************************/
  191. /**
  192. * @override
  193. */
  194. public toSVG(parent: N) {
  195. const svg = this.standardSVGnode(parent);
  196. const child = this.selected;
  197. const {h, d, w} = child.getOuterBBox();
  198. this.adaptor.append(this.element, this.svg('rect', {
  199. width: this.fixed(w), height: this.fixed(h + d), y: this.fixed(-d),
  200. fill: 'none', 'pointer-events': 'all'
  201. }));
  202. child.toSVG(svg);
  203. const bbox = child.getOuterBBox();
  204. if (child.element) {
  205. child.place(bbox.L * bbox.rscale, 0);
  206. }
  207. this.action(this, this.data);
  208. }
  209. /**
  210. * Add an event handler to the output for this maction
  211. */
  212. public setEventHandler(type: string, handler: EventHandler) {
  213. (this.element as any).addEventListener(type, handler);
  214. }
  215. }