base.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS002: Fix invalid constructor
  6. * DS101: Remove unnecessary use of Array.from
  7. * DS102: Remove unnecessary code created because of implicit returns
  8. * DS207: Consider shorter variations of null checks
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  10. */
  11. app.views.BasePage = class BasePage extends app.View {
  12. constructor(el, entry) { this.paintCode = this.paintCode.bind(this); this.el = el; this.entry = entry; super(...arguments); }
  13. deactivate() {
  14. if (super.deactivate(...arguments)) {
  15. return this.highlightNodes = [];
  16. }
  17. }
  18. render(content, fromCache) {
  19. if (fromCache == null) { fromCache = false; }
  20. this.highlightNodes = [];
  21. this.previousTiming = null;
  22. if (!this.constructor.className) { this.addClass(`_${this.entry.doc.type}`); }
  23. this.html(content);
  24. if (!fromCache) { this.highlightCode(); }
  25. this.activate();
  26. if (this.afterRender) { this.delay(this.afterRender); }
  27. if (this.highlightNodes.length > 0) {
  28. $.requestAnimationFrame(() => $.requestAnimationFrame(this.paintCode));
  29. }
  30. }
  31. highlightCode() {
  32. for (var el of Array.from(this.findAll('pre[data-language]'))) {
  33. var language = el.getAttribute('data-language');
  34. el.classList.add(`language-${language}`);
  35. this.highlightNodes.push(el);
  36. }
  37. }
  38. paintCode(timing) {
  39. if (this.previousTiming) {
  40. if (Math.round(1000 / (timing - this.previousTiming)) > 50) { // fps
  41. this.nodesPerFrame = Math.round(Math.min(this.nodesPerFrame * 1.25, 50));
  42. } else {
  43. this.nodesPerFrame = Math.round(Math.max(this.nodesPerFrame * .8, 10));
  44. }
  45. } else {
  46. this.nodesPerFrame = 10;
  47. }
  48. for (var el of Array.from(this.highlightNodes.splice(0, this.nodesPerFrame))) {
  49. var clipEl;
  50. if (clipEl = el.lastElementChild) { $.remove(clipEl); }
  51. Prism.highlightElement(el);
  52. if (clipEl) { $.append(el, clipEl); }
  53. }
  54. if (this.highlightNodes.length > 0) { $.requestAnimationFrame(this.paintCode); }
  55. this.previousTiming = timing;
  56. }
  57. };