maction.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 CommonMaction wrapper mixin for the MmlMaction object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {AnyWrapper, WrapperConstructor, Constructor, AnyWrapperClass} from '../Wrapper.js';
  23. import {MmlMaction} from '../../../core/MmlTree/MmlNodes/maction.js';
  24. import {BBox} from '../../../util/BBox.js';
  25. import {split} from '../../../util/string.js';
  26. /*****************************************************************/
  27. /**
  28. * The types needed to define the actiontypes
  29. *
  30. * @template W The maction wrapper type
  31. */
  32. export type ActionData = {[name: string]: any};
  33. export type ActionHandler<W extends AnyWrapper> = (node: W, data?: ActionData) => void;
  34. export type ActionPair<W extends AnyWrapper> = [ActionHandler<W>, ActionData];
  35. export type ActionMap<W extends AnyWrapper> = Map<string, ActionPair<W>>;
  36. export type ActionDef<W extends AnyWrapper> = [string, [ActionHandler<W>, ActionData]];
  37. export type EventHandler = (event: Event) => void;
  38. /**
  39. * Data used for tooltip actions
  40. */
  41. export const TooltipData = {
  42. dx: '.2em', // x-offset of tooltip from right side of maction bbox
  43. dy: '.1em', // y-offset of tooltip from bottom of maction bbox
  44. postDelay: 600, // milliseconds before tooltip posts
  45. clearDelay: 100, // milliseconds before tooltip is removed
  46. hoverTimer: new Map<any, number>(), // timers for posting tooltips
  47. clearTimer: new Map<any, number>(), // timers for removing tooltips
  48. /*
  49. * clear the timers if any are active
  50. */
  51. stopTimers: (node: any, data: ActionData) => {
  52. if (data.clearTimer.has(node)) {
  53. clearTimeout(data.clearTimer.get(node));
  54. data.clearTimer.delete(node);
  55. }
  56. if (data.hoverTimer.has(node)) {
  57. clearTimeout(data.hoverTimer.get(node));
  58. data.hoverTimer.delete(node);
  59. }
  60. }
  61. };
  62. /*****************************************************************/
  63. /**
  64. * The CommonMaction interface
  65. *
  66. * @template W The maction wrapper type
  67. */
  68. export interface CommonMaction<W extends AnyWrapper> extends AnyWrapper {
  69. /**
  70. * The handler for the specified actiontype
  71. */
  72. action: ActionHandler<W>;
  73. data: ActionData;
  74. /**
  75. * Tooltip offsets
  76. */
  77. dx: number;
  78. dy: number;
  79. /**
  80. * The selected child wrapper
  81. */
  82. readonly selected: W;
  83. }
  84. /**
  85. * The CommonMaction class interface
  86. *
  87. * @template W The maction wrapper type
  88. */
  89. export interface CommonMactionClass<W extends AnyWrapper> extends AnyWrapperClass {
  90. /**
  91. * The valid action types and their handlers
  92. */
  93. actions: ActionMap<W>;
  94. }
  95. /**
  96. * Shorthand for the CommonMaction constructor
  97. *
  98. * @template W The maction wrapper type
  99. */
  100. export type MactionConstructor<W extends AnyWrapper> = Constructor<CommonMaction<W>>;
  101. /*****************************************************************/
  102. /**
  103. * The CommonMaction wrapper mixin for the MmlMaction object
  104. *
  105. * @template W The maction wrapper type
  106. * @template T The Wrapper class constructor type
  107. */
  108. export function CommonMactionMixin<
  109. W extends AnyWrapper,
  110. T extends WrapperConstructor
  111. >(Base: T): MactionConstructor<W> & T {
  112. return class extends Base {
  113. /**
  114. * The handler for the specified actiontype
  115. */
  116. public action: ActionHandler<W>;
  117. /**
  118. * The data for the specified actiontype
  119. */
  120. public data: ActionData;
  121. /**
  122. * The x-offset for tooltips
  123. */
  124. public dx: number;
  125. /**
  126. * The y-offset for tooltips
  127. */
  128. public dy: number;
  129. /**
  130. * @return {W} The selected child wrapper
  131. */
  132. public get selected(): W {
  133. const selection = this.node.attributes.get('selection') as number;
  134. const i = Math.max(1, Math.min(this.childNodes.length, selection)) - 1;
  135. return this.childNodes[i] || this.wrap((this.node as MmlMaction).selected);
  136. }
  137. /*************************************************************/
  138. /**
  139. * @override
  140. */
  141. constructor(...args: any[]) {
  142. super(...args);
  143. const actions = (this.constructor as CommonMactionClass<W>).actions;
  144. const action = this.node.attributes.get('actiontype') as string;
  145. const [handler, data] = actions.get(action) || [((_node, _data) => {}) as ActionHandler<W>, {}];
  146. this.action = handler;
  147. this.data = data;
  148. this.getParameters();
  149. }
  150. /**
  151. * Look up attribute parameters
  152. */
  153. public getParameters() {
  154. const offsets = this.node.attributes.get('data-offsets') as string;
  155. let [dx, dy] = split(offsets || '');
  156. this.dx = this.length2em(dx || TooltipData.dx);
  157. this.dy = this.length2em(dy || TooltipData.dy);
  158. }
  159. /**
  160. * @override
  161. */
  162. public computeBBox(bbox: BBox, recompute: boolean = false) {
  163. bbox.updateFrom(this.selected.getOuterBBox());
  164. this.selected.setChildPWidths(recompute);
  165. }
  166. };
  167. }