mirror of
https://github.com/element-hq/element-web
synced 2024-11-28 04:21:57 +03:00
Added button that copies code to clipboard
Signed-off-by: Oliver Hunt <oliver@hunt.bz>
This commit is contained in:
parent
b8b6a87423
commit
a640e943f7
2 changed files with 40 additions and 0 deletions
|
@ -345,6 +345,7 @@ export function bodyToHtml(content, highlights, opts) {
|
||||||
}
|
}
|
||||||
safeBody = sanitizeHtml(body, sanitizeHtmlParams);
|
safeBody = sanitizeHtml(body, sanitizeHtmlParams);
|
||||||
safeBody = unicodeToImage(safeBody);
|
safeBody = unicodeToImage(safeBody);
|
||||||
|
safeBody = addCodeCopyButton(safeBody);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
delete sanitizeHtmlParams.textFilter;
|
delete sanitizeHtmlParams.textFilter;
|
||||||
|
@ -363,6 +364,18 @@ export function bodyToHtml(content, highlights, opts) {
|
||||||
return <span className={className} dangerouslySetInnerHTML={{ __html: safeBody }} />;
|
return <span className={className} dangerouslySetInnerHTML={{ __html: safeBody }} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function addCodeCopyButton(safeBody) {
|
||||||
|
var el = document.createElement("div");
|
||||||
|
el.innerHTML = safeBody;
|
||||||
|
var codeBlocks = Array.from(el.getElementsByTagName("pre"));
|
||||||
|
codeBlocks.forEach(p => {
|
||||||
|
var button = document.createElement("span");
|
||||||
|
button.className = "mx_EventTile_copyButton";
|
||||||
|
p.appendChild(button);
|
||||||
|
});
|
||||||
|
return el.innerHTML;
|
||||||
|
}
|
||||||
|
|
||||||
export function emojifyText(text) {
|
export function emojifyText(text) {
|
||||||
return {
|
return {
|
||||||
__html: unicodeToImage(escape(text)),
|
__html: unicodeToImage(escape(text)),
|
||||||
|
|
|
@ -62,6 +62,19 @@ module.exports = React.createClass({
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
copyToClipboard: function(text) {
|
||||||
|
const textArea = document.createElement("textarea");
|
||||||
|
textArea.value = text;
|
||||||
|
document.body.appendChild(textArea);
|
||||||
|
textArea.select();
|
||||||
|
try {
|
||||||
|
const successful = document.execCommand('copy');
|
||||||
|
} catch (err) {
|
||||||
|
console.log('Unable to copy');
|
||||||
|
}
|
||||||
|
document.body.removeChild(textArea);
|
||||||
|
},
|
||||||
|
|
||||||
componentDidMount: function() {
|
componentDidMount: function() {
|
||||||
this._unmounted = false;
|
this._unmounted = false;
|
||||||
|
|
||||||
|
@ -80,6 +93,20 @@ module.exports = React.createClass({
|
||||||
}
|
}
|
||||||
}, 10);
|
}, 10);
|
||||||
}
|
}
|
||||||
|
const buttons = ReactDOM.findDOMNode(this).getElementsByClassName("mx_EventTile_copyButton");
|
||||||
|
if (buttons.length > 0) {
|
||||||
|
// Do this asynchronously: parsing code takes time and we don't
|
||||||
|
// need to block the DOM update on it.
|
||||||
|
setTimeout(() => {
|
||||||
|
if (this._unmounted) return;
|
||||||
|
for (let i = 0; i < buttons.length; i++) {
|
||||||
|
buttons[i].onclick = (e) => {
|
||||||
|
const copyCode = buttons[i].parentNode.getElementsByTagName("code")[0];
|
||||||
|
this.copyToClipboard(copyCode.textContent);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}, 10);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue