Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit a53c195

Browse files
authored
Merge pull request #527 from matrix-org/dbkr/highlight_async
Run highlight.js asynchronously
2 parents 974e4c0 + 8cf273a commit a53c195

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/HtmlUtils.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,6 @@ export function bodyToHtml(content, highlights, opts) {
302302
return <span className={className} dangerouslySetInnerHTML={{ __html: safeBody }} />;
303303
}
304304

305-
export function highlightDom(element) {
306-
var blocks = element.getElementsByTagName("code");
307-
for (var i = 0; i < blocks.length; i++) {
308-
highlight.highlightBlock(blocks[i]);
309-
}
310-
}
311-
312305
export function emojifyText(text) {
313306
return {
314307
__html: unicodeToImage(escape(text)),

src/components/views/messages/TextualBody.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818

1919
var React = require('react');
2020
var ReactDOM = require('react-dom');
21+
var highlight = require('highlight.js');
2122
var HtmlUtils = require('../../../HtmlUtils');
2223
var linkify = require('linkifyjs');
2324
var linkifyElement = require('linkifyjs/element');
@@ -62,17 +63,34 @@ module.exports = React.createClass({
6263
},
6364

6465
componentDidMount: function() {
66+
this._unmounted = false;
67+
6568
linkifyElement(this.refs.content, linkifyMatrix.options);
6669
this.calculateUrlPreview();
6770

68-
if (this.props.mxEvent.getContent().format === "org.matrix.custom.html")
69-
HtmlUtils.highlightDom(ReactDOM.findDOMNode(this));
71+
if (this.props.mxEvent.getContent().format === "org.matrix.custom.html") {
72+
const blocks = ReactDOM.findDOMNode(this).getElementsByTagName("code");
73+
if (blocks.length > 0) {
74+
// Do this asynchronously: parsing code takes time and we don't
75+
// need to block the DOM update on it.
76+
setTimeout(() => {
77+
if (this._unmounted) return;
78+
for (let i = 0; i < blocks.length; i++) {
79+
highlight.highlightBlock(blocks[i]);
80+
}
81+
}, 10);
82+
}
83+
}
7084
},
7185

7286
componentDidUpdate: function() {
7387
this.calculateUrlPreview();
7488
},
7589

90+
componentWillUnmount: function() {
91+
this._unmounted = true;
92+
},
93+
7694
shouldComponentUpdate: function(nextProps, nextState) {
7795
//console.log("shouldComponentUpdate: ShowUrlPreview for %s is %s", this.props.mxEvent.getId(), this.props.showUrlPreview);
7896

0 commit comments

Comments
 (0)