base.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. app.views.BasePage = class BasePage extends app.View {
  2. constructor(el, entry) {
  3. super(el);
  4. this.entry = entry;
  5. }
  6. deactivate() {
  7. if (super.deactivate(...arguments)) {
  8. return (this.highlightNodes = []);
  9. }
  10. }
  11. render(content, fromCache) {
  12. if (fromCache == null) {
  13. fromCache = false;
  14. }
  15. this.highlightNodes = [];
  16. this.previousTiming = null;
  17. if (!this.constructor.className) {
  18. this.addClass(`_${this.entry.doc.type}`);
  19. }
  20. this.html(content);
  21. if (!fromCache) {
  22. this.highlightCode();
  23. }
  24. this.activate();
  25. if (this.afterRender) {
  26. this.delay(this.afterRender);
  27. }
  28. if (this.highlightNodes.length > 0) {
  29. requestAnimationFrame(() => this.paintCode());
  30. }
  31. }
  32. highlightCode() {
  33. for (var el of this.findAll("pre[data-language]")) {
  34. var language = el.getAttribute("data-language");
  35. el.classList.add(`language-${language}`);
  36. this.highlightNodes.push(el);
  37. }
  38. }
  39. paintCode(timing) {
  40. if (this.previousTiming) {
  41. if (Math.round(1000 / (timing - this.previousTiming)) > 50) {
  42. // fps
  43. this.nodesPerFrame = Math.round(
  44. Math.min(this.nodesPerFrame * 1.25, 50),
  45. );
  46. } else {
  47. this.nodesPerFrame = Math.round(Math.max(this.nodesPerFrame * 0.8, 10));
  48. }
  49. } else {
  50. this.nodesPerFrame = 10;
  51. }
  52. for (var el of this.highlightNodes.splice(0, this.nodesPerFrame)) {
  53. var clipEl;
  54. if ((clipEl = el.lastElementChild)) {
  55. $.remove(clipEl);
  56. }
  57. Prism.highlightElement(el);
  58. if (clipEl) {
  59. $.append(el, clipEl);
  60. }
  61. }
  62. if (this.highlightNodes.length > 0) {
  63. requestAnimationFrame(() => this.paintCode());
  64. }
  65. this.previousTiming = timing;
  66. }
  67. };