Added _wrapInDiv() method

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
pull/21833/head
Šimon Brandner 2021-01-19 18:21:59 +01:00
parent e6ab47ff76
commit 95939f3d6c
No known key found for this signature in database
GPG Key ID: 9760693FDD98A790
1 changed files with 11 additions and 6 deletions

View File

@ -94,8 +94,11 @@ export default class TextualBody extends React.Component {
const blocks = ReactDOM.findDOMNode(this).getElementsByTagName("pre"); const blocks = ReactDOM.findDOMNode(this).getElementsByTagName("pre");
if (blocks.length > 0) { if (blocks.length > 0) {
for (let i = 0; i < blocks.length; i++) { for (let i = 0; i < blocks.length; i++) {
this._handleCodeBlockExpansion(blocks[i]); // Wrap a div around <pre> so that the copy button can be correctly positioned
this._addCodeCopyButton(blocks[i]); // when the <pre> overflows and is scrolled horizontally.
const div = this._wrapInDiv(blocks[i]);
this._handleCodeBlockExpansion(div);
this._addCodeCopyButton(div);
} }
// 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.
@ -125,17 +128,19 @@ export default class TextualBody extends React.Component {
button.onmouseleave = close; button.onmouseleave = close;
}; };
// Wrap a div around <pre> so that the copy button can be correctly positioned codeBlock.appendChild(button);
// when the <pre> overflows and is scrolled horizontally. }
_wrapInDiv(codeBlock) {
const div = document.createElement("div"); const div = document.createElement("div");
div.className = "mx_EventTile_pre_container"; div.className = "mx_EventTile_pre_container";
// Insert containing div in place of <pre> block // Insert containing div in place of <pre> block
codeBlock.parentNode.replaceChild(div, codeBlock); codeBlock.parentNode.replaceChild(div, codeBlock);
// Append <pre> block and copy button to container // Append <pre> block and copy button to container
div.appendChild(codeBlock); div.appendChild(codeBlock);
div.appendChild(button);
return div;
} }
_handleCodeBlockExpansion(codeBlock) { _handleCodeBlockExpansion(codeBlock) {