base.js 2.2 KB

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