Merge pull request #5633 from SimonBrandner/fix-duplication

Fix duplication of codeblock elements
This commit is contained in:
J. Ryan Stinnett 2021-02-10 13:43:33 +00:00 committed by GitHub
commit 2fa9880aea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -96,6 +96,9 @@ export default class TextualBody extends React.Component {
const pres = ReactDOM.findDOMNode(this).getElementsByTagName("pre"); const pres = ReactDOM.findDOMNode(this).getElementsByTagName("pre");
if (pres.length > 0) { if (pres.length > 0) {
for (let i = 0; i < pres.length; i++) { for (let i = 0; i < pres.length; i++) {
// If there already is a div wrapping the codeblock we want to skip this.
// This happens after the codeblock was edited.
if (pres[i].parentNode.className == "mx_EventTile_pre_container") continue;
// Wrap a div around <pre> so that the copy button can be correctly positioned // Wrap a div around <pre> so that the copy button can be correctly positioned
// when the <pre> overflows and is scrolled horizontally. // when the <pre> overflows and is scrolled horizontally.
const div = this._wrapInDiv(pres[i]); const div = this._wrapInDiv(pres[i]);
@ -110,16 +113,17 @@ export default class TextualBody extends React.Component {
// Highlight code // Highlight code
const codes = ReactDOM.findDOMNode(this).getElementsByTagName("code"); const codes = ReactDOM.findDOMNode(this).getElementsByTagName("code");
if (codes.length > 0) { if (codes.length > 0) {
for (let i = 0; i < codes.length; i++) { // Do this asynchronously: parsing code takes time and we don't
// Do this asynchronously: parsing code takes time and we don't // need to block the DOM update on it.
// need to block the DOM update on it. setTimeout(() => {
setTimeout(() => { if (this._unmounted) return;
if (this._unmounted) return; for (let i = 0; i < codes.length; i++) {
for (let i = 0; i < pres.length; i++) { // If the code already has the hljs class we want to skip this.
this._highlightCode(codes[i]); // This happens after the codeblock was edited.
} if (codes[i].className.includes("hljs")) continue;
}, 10); this._highlightCode(codes[i]);
} }
}, 10);
} }
} }
} }