MJContextMenu.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __values = (this && this.__values) || function(o) {
  18. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  19. if (m) return m.call(o);
  20. if (o && typeof o.length === "number") return {
  21. next: function () {
  22. if (o && i >= o.length) o = void 0;
  23. return { value: o && o[i++], done: !o };
  24. }
  25. };
  26. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  27. };
  28. var __read = (this && this.__read) || function (o, n) {
  29. var m = typeof Symbol === "function" && o[Symbol.iterator];
  30. if (!m) return o;
  31. var i = m.call(o), r, ar = [], e;
  32. try {
  33. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  34. }
  35. catch (error) { e = { error: error }; }
  36. finally {
  37. try {
  38. if (r && !r.done && (m = i["return"])) m.call(i);
  39. }
  40. finally { if (e) throw e.error; }
  41. }
  42. return ar;
  43. };
  44. Object.defineProperty(exports, "__esModule", { value: true });
  45. exports.MJContextMenu = void 0;
  46. var context_menu_js_1 = require("mj-context-menu/js/context_menu.js");
  47. var item_submenu_js_1 = require("mj-context-menu/js/item_submenu.js");
  48. var MJContextMenu = (function (_super) {
  49. __extends(MJContextMenu, _super);
  50. function MJContextMenu() {
  51. var _this = _super !== null && _super.apply(this, arguments) || this;
  52. _this.mathItem = null;
  53. _this.annotation = '';
  54. _this.annotationTypes = {};
  55. return _this;
  56. }
  57. MJContextMenu.prototype.post = function (x, y) {
  58. if (this.mathItem) {
  59. if (y !== undefined) {
  60. var input = this.mathItem.inputJax.name;
  61. var original = this.findID('Show', 'Original');
  62. original.content = (input === 'MathML' ? 'Original MathML' : input + ' Commands');
  63. var clipboard = this.findID('Copy', 'Original');
  64. clipboard.content = original.content;
  65. var semantics = this.findID('Settings', 'semantics');
  66. input === 'MathML' ? semantics.disable() : semantics.enable();
  67. this.getAnnotationMenu();
  68. this.dynamicSubmenus();
  69. }
  70. _super.prototype.post.call(this, x, y);
  71. }
  72. };
  73. MJContextMenu.prototype.unpost = function () {
  74. _super.prototype.unpost.call(this);
  75. this.mathItem = null;
  76. };
  77. MJContextMenu.prototype.findID = function () {
  78. var e_1, _a;
  79. var names = [];
  80. for (var _i = 0; _i < arguments.length; _i++) {
  81. names[_i] = arguments[_i];
  82. }
  83. var menu = this;
  84. var item = null;
  85. try {
  86. for (var names_1 = __values(names), names_1_1 = names_1.next(); !names_1_1.done; names_1_1 = names_1.next()) {
  87. var name_1 = names_1_1.value;
  88. if (menu) {
  89. item = menu.find(name_1);
  90. menu = (item instanceof item_submenu_js_1.Submenu ? item.submenu : null);
  91. }
  92. else {
  93. item = null;
  94. }
  95. }
  96. }
  97. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  98. finally {
  99. try {
  100. if (names_1_1 && !names_1_1.done && (_a = names_1.return)) _a.call(names_1);
  101. }
  102. finally { if (e_1) throw e_1.error; }
  103. }
  104. return item;
  105. };
  106. MJContextMenu.prototype.getAnnotationMenu = function () {
  107. var _this = this;
  108. var annotations = this.getAnnotations(this.getSemanticNode());
  109. this.createAnnotationMenu('Show', annotations, function () { return _this.showAnnotation.post(); });
  110. this.createAnnotationMenu('Copy', annotations, function () { return _this.copyAnnotation(); });
  111. };
  112. MJContextMenu.prototype.getSemanticNode = function () {
  113. var node = this.mathItem.root;
  114. while (node && !node.isKind('semantics')) {
  115. if (node.isToken || node.childNodes.length !== 1)
  116. return null;
  117. node = node.childNodes[0];
  118. }
  119. return node;
  120. };
  121. MJContextMenu.prototype.getAnnotations = function (node) {
  122. var e_2, _a;
  123. var annotations = [];
  124. if (!node)
  125. return annotations;
  126. try {
  127. for (var _b = __values(node.childNodes), _c = _b.next(); !_c.done; _c = _b.next()) {
  128. var child = _c.value;
  129. if (child.isKind('annotation')) {
  130. var match = this.annotationMatch(child);
  131. if (match) {
  132. var value = child.childNodes.reduce(function (text, chars) { return text + chars.toString(); }, '');
  133. annotations.push([match, value]);
  134. }
  135. }
  136. }
  137. }
  138. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  139. finally {
  140. try {
  141. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  142. }
  143. finally { if (e_2) throw e_2.error; }
  144. }
  145. return annotations;
  146. };
  147. MJContextMenu.prototype.annotationMatch = function (child) {
  148. var e_3, _a;
  149. var encoding = child.attributes.get('encoding');
  150. try {
  151. for (var _b = __values(Object.keys(this.annotationTypes)), _c = _b.next(); !_c.done; _c = _b.next()) {
  152. var type = _c.value;
  153. if (this.annotationTypes[type].indexOf(encoding) >= 0) {
  154. return type;
  155. }
  156. }
  157. }
  158. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  159. finally {
  160. try {
  161. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  162. }
  163. finally { if (e_3) throw e_3.error; }
  164. }
  165. return null;
  166. };
  167. MJContextMenu.prototype.createAnnotationMenu = function (id, annotations, action) {
  168. var _this = this;
  169. var menu = this.findID(id, 'Annotation');
  170. menu.submenu = this.factory.get('subMenu')(this.factory, {
  171. items: annotations.map(function (_a) {
  172. var _b = __read(_a, 2), type = _b[0], value = _b[1];
  173. return {
  174. type: 'command',
  175. id: type,
  176. content: type,
  177. action: function () {
  178. _this.annotation = value;
  179. action();
  180. }
  181. };
  182. }),
  183. id: 'annotations'
  184. }, menu);
  185. if (annotations.length) {
  186. menu.enable();
  187. }
  188. else {
  189. menu.disable();
  190. }
  191. };
  192. MJContextMenu.prototype.dynamicSubmenus = function () {
  193. var e_4, _a;
  194. try {
  195. for (var _b = __values(MJContextMenu.DynamicSubmenus), _c = _b.next(); !_c.done; _c = _b.next()) {
  196. var _d = __read(_c.value, 2), id = _d[0], method = _d[1];
  197. var menu = this.find(id);
  198. if (!menu)
  199. continue;
  200. var sub = method(this, menu);
  201. menu.submenu = sub;
  202. if (sub.items.length) {
  203. menu.enable();
  204. }
  205. else {
  206. menu.disable();
  207. }
  208. }
  209. }
  210. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  211. finally {
  212. try {
  213. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  214. }
  215. finally { if (e_4) throw e_4.error; }
  216. }
  217. };
  218. MJContextMenu.DynamicSubmenus = new Map();
  219. return MJContextMenu;
  220. }(context_menu_js_1.ContextMenu));
  221. exports.MJContextMenu = MJContextMenu;
  222. //# sourceMappingURL=MJContextMenu.js.map