diff options
author | silverwind <me@silverwind.io> | 2020-04-13 15:02:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-13 16:02:31 +0300 |
commit | 27e3cddfbef533f783898800ac4abdd5a453b436 (patch) | |
tree | 1ba9f63c9fac2f06549a28e043c3669042cf9ced /web_src | |
parent | cc4da79fb6302f35dfe9e2d5af7cda384083b0af (diff) | |
download | gitea-27e3cddfbef533f783898800ac4abdd5a453b436.tar.gz gitea-27e3cddfbef533f783898800ac4abdd5a453b436.zip |
Move syntax highlighting to web worker (#11017)
This should eliminate page freezes when loading big files/diff.
`highlightBlock` is needed to preserve existing nodes when highlighting
and for that, highlight.js needs access to the DOM API so I added a DOM
implementation to make it work, which adds around 300kB to the output
file size of the lazy-loaded `highlight.js`.
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'web_src')
-rw-r--r-- | web_src/js/features/highlight.js | 23 | ||||
-rw-r--r-- | web_src/js/features/highlight.worker.js | 12 | ||||
-rw-r--r-- | web_src/js/index.js | 15 |
3 files changed, 34 insertions, 16 deletions
diff --git a/web_src/js/features/highlight.js b/web_src/js/features/highlight.js index dcd8a8d21e..d3f6ba71b8 100644 --- a/web_src/js/features/highlight.js +++ b/web_src/js/features/highlight.js @@ -1,12 +1,19 @@ -export default async function initHighlight() { - if (!window.config || !window.config.HighlightJS) return; +export default async function highlight(elementOrNodeList) { + if (!window.config || !window.config.HighlightJS || !elementOrNodeList) return; + const nodes = 'length' in elementOrNodeList ? elementOrNodeList : [elementOrNodeList]; + if (!nodes.length) return; - const hljs = await import(/* webpackChunkName: "highlight" */'highlight.js'); + const {default: Worker} = await import(/* webpackChunkName: "highlight" */'./highlight.worker.js'); + const worker = new Worker(); - const nodes = [].slice.call(document.querySelectorAll('pre code') || []); - for (let i = 0; i < nodes.length; i++) { - hljs.highlightBlock(nodes[i]); - } + worker.addEventListener('message', ({data}) => { + const {index, html} = data; + nodes[index].outerHTML = html; + }); - return hljs; + for (let index = 0; index < nodes.length; index++) { + const node = nodes[index]; + if (!node) continue; + worker.postMessage({index, html: node.outerHTML}); + } } diff --git a/web_src/js/features/highlight.worker.js b/web_src/js/features/highlight.worker.js new file mode 100644 index 0000000000..7d6cc4e438 --- /dev/null +++ b/web_src/js/features/highlight.worker.js @@ -0,0 +1,12 @@ +import {highlightBlock} from 'highlight.js'; +import {createWindow} from 'domino'; + +self.onmessage = function ({data}) { + const window = createWindow(); + self.document = window.document; + + const {index, html} = data; + document.body.innerHTML = html; + highlightBlock(document.body.firstChild); + self.postMessage({index, html: document.body.innerHTML}); +}; diff --git a/web_src/js/index.js b/web_src/js/index.js index 6476b2cfbf..63a5134bbd 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -11,12 +11,12 @@ import './vendor/semanticdropdown.js'; import {svg} from './utils.js'; import initContextPopups from './features/contextpopup.js'; -import initHighlight from './features/highlight.js'; import initGitGraph from './features/gitgraph.js'; import initClipboard from './features/clipboard.js'; import initUserHeatmap from './features/userheatmap.js'; import initDateTimePicker from './features/datetimepicker.js'; import createDropzone from './features/dropzone.js'; +import highlight from './features/highlight.js'; import ActivityTopAuthors from './components/ActivityTopAuthors.vue'; const {AppSubUrl, StaticUrlPrefix, csrf} = window.config; @@ -29,7 +29,6 @@ let previewFileModes; let simpleMDEditor; const commentMDEditors = {}; let codeMirrorEditor; -let hljs; // Silence fomantic's error logging when tabs are used without a target content element $.fn.tab.settings.silent = true; @@ -49,7 +48,7 @@ function initCommentPreviewTab($form) { $previewPanel.html(data); emojify.run($previewPanel[0]); $('pre code', $previewPanel[0]).each(function () { - hljs.highlightBlock(this); + highlight(this); }); }); }); @@ -75,7 +74,7 @@ function initEditPreviewTab($form) { $previewPanel.html(data); emojify.run($previewPanel[0]); $('pre code', $previewPanel[0]).each(function () { - hljs.highlightBlock(this); + highlight(this); }); }); }); @@ -1011,7 +1010,7 @@ async function initRepository() { $renderContent.html(data.content); emojify.run($renderContent[0]); $('pre code', $renderContent[0]).each(function () { - hljs.highlightBlock(this); + highlight(this); }); } const $content = $segment.parent(); @@ -1337,7 +1336,7 @@ function initWikiForm() { preview.innerHTML = `<div class="markdown ui segment">${data}</div>`; emojify.run($('.editor-preview')[0]); $(preview).find('pre code').each((_, e) => { - hljs.highlightBlock(e); + highlight(e); }); }); }; @@ -2633,8 +2632,8 @@ $(document).ready(async () => { }); // parallel init of lazy-loaded features - [hljs] = await Promise.all([ - initHighlight(), + await Promise.all([ + highlight(document.querySelectorAll('pre code')), initGitGraph(), initClipboard(), initUserHeatmap(), |