From 9694b4916af3fae1c4314265439ae6347df7c082 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Thu, 31 Aug 2023 15:02:03 +0200
Subject: [PATCH] run mathjax only on chars and less than once / sec fixes #122

---
 .../scipost/static/scipost/report-preview.js  | 36 ++++++++++++++-----
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/scipost_django/scipost/static/scipost/report-preview.js b/scipost_django/scipost/static/scipost/report-preview.js
index 267448185..3f8bb230a 100644
--- a/scipost_django/scipost/static/scipost/report-preview.js
+++ b/scipost_django/scipost/static/scipost/report-preview.js
@@ -1,10 +1,18 @@
-$(function(){
+$(function () {
+    function typesetMath() {
+        if (typeof MathJax.Hub !== "undefined") {
+            MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
+        }
+    }
+
     function set_preview(el) {
         $('[data-receive$="' + $(el).attr('id').split('id_')[1] + '"]').text($(el).val())
     }
+    
     function set_preview_select(el) {
         $('[data-receive$="' + $(el).attr('id').split('id_')[1] + '"]').text($(el).find('option:selected').text())
     }
+
     function update_identity_preview(show_identity) {
         $('[data-receive="report-identity"] [if-anonymous]').hide();
         if (show_identity) {
@@ -13,13 +21,25 @@ $(function(){
             $('[data-receive="report-identity"] [if-anonymous="true"]').show();
         }
     }
-    $('#id_weaknesses, #id_strengths, #id_report, #id_requested_changes').on('keyup', function(){
-        set_preview(this)
-        if (typeof MathJax.Hub !== "undefined") {
-            // First trigger will fail since MathJax is loaded in the footer.
-            MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
-        }
-    }).trigger('keyup');
+
+    $("#id_weaknesses, #id_strengths, #id_report, #id_requested_changes")
+        .on("keyup", function (e) {
+            // do not fire function on arrow keys, modifiers, etc.
+            if (e.which < 48) {
+                return;
+            }
+            
+            set_preview(this);
+
+            // MathJax is slow, so we only run it every second since the last keyup
+            clearTimeout($(this).data("mathjaxTimer"));
+            const mathjaxTimer = setTimeout(function () {
+                typesetMath();
+            }, 1000);
+            $(this).data("mathjaxTimer", mathjaxTimer);
+        })
+        .trigger("keyup");
+
     $('#id_validity, #id_originality, #id_significance, #id_clarity, #id_formatting, #id_grammar').on('change', function(){
         set_preview_select(this);
     }).trigger('change');
-- 
GitLab