LazyHandler.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 __assign = (this && this.__assign) || function () {
  18. __assign = Object.assign || function(t) {
  19. for (var s, i = 1, n = arguments.length; i < n; i++) {
  20. s = arguments[i];
  21. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  22. t[p] = s[p];
  23. }
  24. return t;
  25. };
  26. return __assign.apply(this, arguments);
  27. };
  28. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  29. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  30. return new (P || (P = Promise))(function (resolve, reject) {
  31. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  32. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  33. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  34. step((generator = generator.apply(thisArg, _arguments || [])).next());
  35. });
  36. };
  37. var __generator = (this && this.__generator) || function (thisArg, body) {
  38. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  39. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  40. function verb(n) { return function (v) { return step([n, v]); }; }
  41. function step(op) {
  42. if (f) throw new TypeError("Generator is already executing.");
  43. while (_) try {
  44. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  45. if (y = 0, t) op = [op[0] & 2, t.value];
  46. switch (op[0]) {
  47. case 0: case 1: t = op; break;
  48. case 4: _.label++; return { value: op[1], done: false };
  49. case 5: _.label++; y = op[1]; op = [0]; continue;
  50. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  51. default:
  52. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  53. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  54. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  55. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  56. if (t[2]) _.ops.pop();
  57. _.trys.pop(); continue;
  58. }
  59. op = body.call(thisArg, _);
  60. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  61. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  62. }
  63. };
  64. var __read = (this && this.__read) || function (o, n) {
  65. var m = typeof Symbol === "function" && o[Symbol.iterator];
  66. if (!m) return o;
  67. var i = m.call(o), r, ar = [], e;
  68. try {
  69. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  70. }
  71. catch (error) { e = { error: error }; }
  72. finally {
  73. try {
  74. if (r && !r.done && (m = i["return"])) m.call(i);
  75. }
  76. finally { if (e) throw e.error; }
  77. }
  78. return ar;
  79. };
  80. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  81. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  82. if (ar || !(i in from)) {
  83. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  84. ar[i] = from[i];
  85. }
  86. }
  87. return to.concat(ar || Array.prototype.slice.call(from));
  88. };
  89. var __values = (this && this.__values) || function(o) {
  90. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  91. if (m) return m.call(o);
  92. if (o && typeof o.length === "number") return {
  93. next: function () {
  94. if (o && i >= o.length) o = void 0;
  95. return { value: o && o[i++], done: !o };
  96. }
  97. };
  98. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  99. };
  100. Object.defineProperty(exports, "__esModule", { value: true });
  101. exports.LazyHandler = exports.LazyMathDocumentMixin = exports.LazyMathItemMixin = exports.LAZYID = exports.LazyList = void 0;
  102. var MathItem_js_1 = require("../../core/MathItem.js");
  103. var Retries_js_1 = require("../../util/Retries.js");
  104. var LazyList = (function () {
  105. function LazyList() {
  106. this.id = 0;
  107. this.items = new Map();
  108. }
  109. LazyList.prototype.add = function (math) {
  110. var id = String(this.id++);
  111. this.items.set(id, math);
  112. return id;
  113. };
  114. LazyList.prototype.get = function (id) {
  115. return this.items.get(id);
  116. };
  117. LazyList.prototype.delete = function (id) {
  118. return this.items.delete(id);
  119. };
  120. return LazyList;
  121. }());
  122. exports.LazyList = LazyList;
  123. (0, MathItem_js_1.newState)('LAZYALWAYS', MathItem_js_1.STATE.FINDMATH + 3);
  124. exports.LAZYID = 'data-mjx-lazy';
  125. function LazyMathItemMixin(BaseMathItem) {
  126. return (function (_super) {
  127. __extends(class_1, _super);
  128. function class_1() {
  129. var args = [];
  130. for (var _i = 0; _i < arguments.length; _i++) {
  131. args[_i] = arguments[_i];
  132. }
  133. var _this = _super.apply(this, __spreadArray([], __read(args), false)) || this;
  134. _this.lazyCompile = true;
  135. _this.lazyTypeset = true;
  136. _this.lazyTex = false;
  137. if (!_this.end.node) {
  138. _this.lazyCompile = _this.lazyTypeset = false;
  139. }
  140. return _this;
  141. }
  142. class_1.prototype.compile = function (document) {
  143. if (!this.lazyCompile) {
  144. _super.prototype.compile.call(this, document);
  145. return;
  146. }
  147. if (this.state() < MathItem_js_1.STATE.COMPILED) {
  148. this.lazyTex = (this.inputJax.name === 'TeX');
  149. this.root = document.mmlFactory.create('math');
  150. this.state(MathItem_js_1.STATE.COMPILED);
  151. }
  152. };
  153. class_1.prototype.state = function (state, restore) {
  154. if (state === void 0) { state = null; }
  155. if (restore === void 0) { restore = false; }
  156. if (restore !== null)
  157. _super.prototype.state.call(this, state, restore);
  158. return _super.prototype.state.call(this);
  159. };
  160. class_1.prototype.typeset = function (document) {
  161. var _a;
  162. if (!this.lazyTypeset) {
  163. _super.prototype.typeset.call(this, document);
  164. return;
  165. }
  166. if (this.state() < MathItem_js_1.STATE.TYPESET) {
  167. var adaptor = document.adaptor;
  168. if (!this.lazyMarker) {
  169. var id = document.lazyList.add(this);
  170. this.lazyMarker = adaptor.node('mjx-lazy', (_a = {}, _a[exports.LAZYID] = id, _a));
  171. this.typesetRoot = adaptor.node('mjx-container', {}, [this.lazyMarker]);
  172. }
  173. this.state(MathItem_js_1.STATE.TYPESET);
  174. }
  175. };
  176. class_1.prototype.updateDocument = function (document) {
  177. _super.prototype.updateDocument.call(this, document);
  178. if (this.lazyTypeset) {
  179. document.lazyObserver.observe(this.lazyMarker);
  180. }
  181. };
  182. return class_1;
  183. }(BaseMathItem));
  184. }
  185. exports.LazyMathItemMixin = LazyMathItemMixin;
  186. function LazyMathDocumentMixin(BaseDocument) {
  187. var _a;
  188. return _a = (function (_super) {
  189. __extends(BaseClass, _super);
  190. function BaseClass() {
  191. var args = [];
  192. for (var _i = 0; _i < arguments.length; _i++) {
  193. args[_i] = arguments[_i];
  194. }
  195. var _this = _super.apply(this, __spreadArray([], __read(args), false)) || this;
  196. _this.lazyAlwaysContainers = null;
  197. _this.lazyAlwaysIndex = 0;
  198. _this.lazyPromise = Promise.resolve();
  199. _this.lazyIdle = false;
  200. _this.lazySet = new Set();
  201. _this.options.MathItem =
  202. LazyMathItemMixin(_this.options.MathItem);
  203. var ProcessBits = _this.constructor.ProcessBits;
  204. !ProcessBits.has('lazyAlways') && ProcessBits.allocate('lazyAlways');
  205. _this.lazyObserver = new IntersectionObserver(_this.lazyObserve.bind(_this), { rootMargin: _this.options.lazyMargin });
  206. _this.lazyList = new LazyList();
  207. var callback = _this.lazyHandleSet.bind(_this);
  208. _this.lazyProcessSet = (window && window.requestIdleCallback ?
  209. function () { return window.requestIdleCallback(callback); } :
  210. function () { return setTimeout(callback, 10); });
  211. if (window) {
  212. var done_1 = false;
  213. var handler = function () {
  214. !done_1 && _this.lazyTypesetAll();
  215. done_1 = true;
  216. };
  217. window.matchMedia('print').addListener(handler);
  218. window.addEventListener('beforeprint', handler);
  219. }
  220. return _this;
  221. }
  222. BaseClass.prototype.lazyAlways = function () {
  223. var e_1, _a;
  224. if (!this.lazyAlwaysContainers || this.processed.isSet('lazyAlways'))
  225. return;
  226. try {
  227. for (var _b = __values(this.math), _c = _b.next(); !_c.done; _c = _b.next()) {
  228. var item = _c.value;
  229. var math = item;
  230. if (math.lazyTypeset && this.lazyIsAlways(math)) {
  231. math.lazyCompile = math.lazyTypeset = false;
  232. }
  233. }
  234. }
  235. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  236. finally {
  237. try {
  238. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  239. }
  240. finally { if (e_1) throw e_1.error; }
  241. }
  242. this.processed.set('lazyAlways');
  243. };
  244. BaseClass.prototype.lazyIsAlways = function (math) {
  245. if (math.state() < MathItem_js_1.STATE.LAZYALWAYS) {
  246. math.state(MathItem_js_1.STATE.LAZYALWAYS);
  247. var node = math.start.node;
  248. var adaptor = this.adaptor;
  249. var start = this.lazyAlwaysIndex;
  250. var end = this.lazyAlwaysContainers.length;
  251. do {
  252. var container = this.lazyAlwaysContainers[this.lazyAlwaysIndex];
  253. if (adaptor.contains(container, node))
  254. return true;
  255. if (++this.lazyAlwaysIndex >= end) {
  256. this.lazyAlwaysIndex = 0;
  257. }
  258. } while (this.lazyAlwaysIndex !== start);
  259. }
  260. return false;
  261. };
  262. BaseClass.prototype.state = function (state, restore) {
  263. if (restore === void 0) { restore = false; }
  264. _super.prototype.state.call(this, state, restore);
  265. if (state < MathItem_js_1.STATE.LAZYALWAYS) {
  266. this.processed.clear('lazyAlways');
  267. }
  268. return this;
  269. };
  270. BaseClass.prototype.lazyTypesetAll = function () {
  271. return __awaiter(this, void 0, void 0, function () {
  272. var state, _a, _b, item, math, fontCache;
  273. var e_2, _c;
  274. var _this = this;
  275. return __generator(this, function (_d) {
  276. state = MathItem_js_1.STATE.LAST;
  277. try {
  278. for (_a = __values(this.math), _b = _a.next(); !_b.done; _b = _a.next()) {
  279. item = _b.value;
  280. math = item;
  281. if (!math.lazyCompile && !math.lazyTypeset)
  282. continue;
  283. if (math.lazyCompile) {
  284. math.state(MathItem_js_1.STATE.COMPILED - 1);
  285. state = MathItem_js_1.STATE.COMPILED;
  286. }
  287. else {
  288. math.state(MathItem_js_1.STATE.TYPESET - 1);
  289. if (MathItem_js_1.STATE.TYPESET < state)
  290. state = MathItem_js_1.STATE.TYPESET;
  291. }
  292. math.lazyCompile = math.lazyTypeset = false;
  293. math.lazyMarker && this.lazyObserver.unobserve(math.lazyMarker);
  294. }
  295. }
  296. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  297. finally {
  298. try {
  299. if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
  300. }
  301. finally { if (e_2) throw e_2.error; }
  302. }
  303. if (state === MathItem_js_1.STATE.LAST)
  304. return [2, Promise.resolve()];
  305. this.state(state - 1, null);
  306. fontCache = this.outputJax.options.fontCache;
  307. if (fontCache)
  308. this.outputJax.options.fontCache = 'none';
  309. this.reset();
  310. return [2, (0, Retries_js_1.handleRetriesFor)(function () { return _this.render(); }).then(function () {
  311. if (fontCache)
  312. _this.outputJax.options.fontCache = fontCache;
  313. })];
  314. });
  315. });
  316. };
  317. BaseClass.prototype.lazyObserve = function (entries) {
  318. var e_3, _a;
  319. try {
  320. for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
  321. var entry = entries_1_1.value;
  322. var id = this.adaptor.getAttribute(entry.target, exports.LAZYID);
  323. var math = this.lazyList.get(id);
  324. if (!math)
  325. continue;
  326. if (!entry.isIntersecting) {
  327. this.lazySet.delete(id);
  328. continue;
  329. }
  330. this.lazySet.add(id);
  331. if (!this.lazyIdle) {
  332. this.lazyIdle = true;
  333. this.lazyProcessSet();
  334. }
  335. }
  336. }
  337. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  338. finally {
  339. try {
  340. if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1);
  341. }
  342. finally { if (e_3) throw e_3.error; }
  343. }
  344. };
  345. BaseClass.prototype.lazyHandleSet = function () {
  346. var _this = this;
  347. var set = this.lazySet;
  348. this.lazySet = new Set();
  349. this.lazyPromise = this.lazyPromise.then(function () {
  350. var state = _this.compileEarlierItems(set) ? MathItem_js_1.STATE.COMPILED : MathItem_js_1.STATE.TYPESET;
  351. state = _this.resetStates(set, state);
  352. _this.state(state - 1, null);
  353. return (0, Retries_js_1.handleRetriesFor)(function () {
  354. _this.render();
  355. _this.lazyIdle = false;
  356. });
  357. });
  358. };
  359. BaseClass.prototype.resetStates = function (set, state) {
  360. var e_4, _a;
  361. try {
  362. for (var _b = __values(set.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
  363. var id = _c.value;
  364. var math = this.lazyList.get(id);
  365. if (math.lazyCompile) {
  366. math.state(MathItem_js_1.STATE.COMPILED - 1);
  367. state = MathItem_js_1.STATE.COMPILED;
  368. }
  369. else {
  370. math.state(MathItem_js_1.STATE.TYPESET - 1);
  371. }
  372. math.lazyCompile = math.lazyTypeset = false;
  373. math.lazyMarker && this.lazyObserver.unobserve(math.lazyMarker);
  374. }
  375. }
  376. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  377. finally {
  378. try {
  379. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  380. }
  381. finally { if (e_4) throw e_4.error; }
  382. }
  383. return state;
  384. };
  385. BaseClass.prototype.compileEarlierItems = function (set) {
  386. var e_5, _a;
  387. var math = this.earliestTex(set);
  388. if (!math)
  389. return false;
  390. var compile = false;
  391. try {
  392. for (var _b = __values(this.math), _c = _b.next(); !_c.done; _c = _b.next()) {
  393. var item = _c.value;
  394. var earlier = item;
  395. if (earlier === math || !(earlier === null || earlier === void 0 ? void 0 : earlier.lazyCompile) || !earlier.lazyTex) {
  396. break;
  397. }
  398. earlier.lazyCompile = false;
  399. earlier.lazyMarker && this.lazyObserver.unobserve(earlier.lazyMarker);
  400. earlier.state(MathItem_js_1.STATE.COMPILED - 1);
  401. compile = true;
  402. }
  403. }
  404. catch (e_5_1) { e_5 = { error: e_5_1 }; }
  405. finally {
  406. try {
  407. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  408. }
  409. finally { if (e_5) throw e_5.error; }
  410. }
  411. return compile;
  412. };
  413. BaseClass.prototype.earliestTex = function (set) {
  414. var e_6, _a;
  415. var min = null;
  416. var minMath = null;
  417. try {
  418. for (var _b = __values(set.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
  419. var id = _c.value;
  420. var math = this.lazyList.get(id);
  421. if (!math.lazyTex)
  422. continue;
  423. if (min === null || parseInt(id) < min) {
  424. min = parseInt(id);
  425. minMath = math;
  426. }
  427. }
  428. }
  429. catch (e_6_1) { e_6 = { error: e_6_1 }; }
  430. finally {
  431. try {
  432. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  433. }
  434. finally { if (e_6) throw e_6.error; }
  435. }
  436. return minMath;
  437. };
  438. BaseClass.prototype.clearMathItemsWithin = function (containers) {
  439. var e_7, _a;
  440. var items = _super.prototype.clearMathItemsWithin.call(this, containers);
  441. try {
  442. for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
  443. var math = items_1_1.value;
  444. var marker = math.lazyMarker;
  445. if (marker) {
  446. this.lazyObserver.unobserve(marker);
  447. this.lazyList.delete(this.adaptor.getAttribute(marker, exports.LAZYID));
  448. }
  449. }
  450. }
  451. catch (e_7_1) { e_7 = { error: e_7_1 }; }
  452. finally {
  453. try {
  454. if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
  455. }
  456. finally { if (e_7) throw e_7.error; }
  457. }
  458. return items;
  459. };
  460. BaseClass.prototype.render = function () {
  461. var always = this.options.lazyAlwaysTypeset;
  462. this.lazyAlwaysContainers = !always ? null :
  463. this.adaptor.getElements(Array.isArray(always) ? always : [always], this.document);
  464. this.lazyAlwaysIndex = 0;
  465. _super.prototype.render.call(this);
  466. return this;
  467. };
  468. return BaseClass;
  469. }(BaseDocument)),
  470. _a.OPTIONS = __assign(__assign({}, BaseDocument.OPTIONS), { lazyMargin: '200px', lazyAlwaysTypeset: null, renderActions: __assign(__assign({}, BaseDocument.OPTIONS.renderActions), { lazyAlways: [MathItem_js_1.STATE.LAZYALWAYS, 'lazyAlways', '', false] }) }),
  471. _a;
  472. }
  473. exports.LazyMathDocumentMixin = LazyMathDocumentMixin;
  474. function LazyHandler(handler) {
  475. if (typeof IntersectionObserver !== 'undefined') {
  476. handler.documentClass =
  477. LazyMathDocumentMixin(handler.documentClass);
  478. }
  479. return handler;
  480. }
  481. exports.LazyHandler = LazyHandler;
  482. //# sourceMappingURL=LazyHandler.js.map