base.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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) {
  13. this.paintCode = this.paintCode.bind(this);
  14. this.el = el;
  15. this.entry = entry;
  16. super(...arguments);
  17. }
  18. deactivate() {
  19. if (super.deactivate(...arguments)) {
  20. return (this.highlightNodes = []);
  21. }
  22. }
  23. render(content, fromCache) {
  24. if (fromCache == null) {
  25. fromCache = false;
  26. }
  27. this.highlightNodes = [];
  28. this.previousTiming = null;
  29. if (!this.constructor.className) {
  30. this.addClass(`_${this.entry.doc.type}`);
  31. }
  32. this.html(content);
  33. if (!fromCache) {
  34. this.highlightCode();
  35. }
  36. this.activate();
  37. if (this.afterRender) {
  38. this.delay(this.afterRender);
  39. }
  40. if (this.highlightNodes.length > 0) {
  41. $.requestAnimationFrame(() => $.requestAnimationFrame(this.paintCode));
  42. }
  43. }
  44. highlightCode() {
  45. for (var el of Array.from(this.findAll("pre[data-language]"))) {
  46. var language = el.getAttribute("data-language");
  47. el.classList.add(`language-${language}`);
  48. this.highlightNodes.push(el);
  49. }
  50. }
  51. paintCode(timing) {
  52. if (this.previousTiming) {
  53. if (Math.round(1000 / (timing - this.previousTiming)) > 50) {
  54. // fps
  55. this.nodesPerFrame = Math.round(
  56. Math.min(this.nodesPerFrame * 1.25, 50),
  57. );
  58. } else {
  59. this.nodesPerFrame = Math.round(Math.max(this.nodesPerFrame * 0.8, 10));
  60. }
  61. } else {
  62. this.nodesPerFrame = 10;
  63. }
  64. for (var el of Array.from(
  65. this.highlightNodes.splice(0, this.nodesPerFrame),
  66. )) {
  67. var clipEl;
  68. if ((clipEl = el.lastElementChild)) {
  69. $.remove(clipEl);
  70. }
  71. Prism.highlightElement(el);
  72. if (clipEl) {
  73. $.append(el, clipEl);
  74. }
  75. }
  76. if (this.highlightNodes.length > 0) {
  77. $.requestAnimationFrame(this.paintCode);
  78. }
  79. this.previousTiming = timing;
  80. }
  81. };