浏览代码

[GINR] Web worker with highlight.js

cgars 7 年之前
父节点
当前提交
448725a337
共有 2 个文件被更改,包括 20 次插入3 次删除
  1. 15 3
      public/js/gogs.js
  2. 5 0
      public/plugins/highlighter.js

+ 15 - 3
public/js/gogs.js

@@ -1201,9 +1201,21 @@ $(document).ready(function () {
     });
 
     // Highlight JS
-    if (typeof hljs != 'undefined') {
-        hljs.initHighlightingOnLoad();
-    }
+	if (typeof hljs != 'undefined') {
+		if (typeof(Worker) !== "undefined") {
+			addEventListener('load', function () {
+				var code = document.querySelector('code');
+				var worker = new Worker('/plugins/highlighter.js');
+				worker.onmessage = function (event) {
+					code.innerHTML = event.data;
+				}
+				worker.postMessage(code.textContent);
+			})
+		} else {
+			hljs.initHighlightingOnLoad()
+		}
+
+	}
 
     // Dropzone
     var $dropzone = $('#dropzone');

+ 5 - 0
public/plugins/highlighter.js

@@ -0,0 +1,5 @@
+onmessage = function (event) {
+	importScripts('/plugins/highlight-9.6.0/highlight.pack.js');
+	var result = self.hljs.highlightAuto(event.data);
+	postMessage(result.value);
+}