mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: whitespaces when copying code blocks (#12730)
This ensures the copied text always has proper white space characters.
This commit is contained in:
parent
c43a7bb23b
commit
2aed82e646
@ -7,8 +7,8 @@ import { withPluginApi } from "discourse/lib/plugin-api";
|
||||
|
||||
// http://github.com/feross/clipboard-copy
|
||||
function clipboardCopy(text) {
|
||||
// Use the Async Clipboard API when available. Requires a secure browsing
|
||||
// context (i.e. HTTPS)
|
||||
// Use the Async Clipboard API when available.
|
||||
// Requires a secure browsing context (i.e. HTTPS)
|
||||
if (navigator.clipboard) {
|
||||
return navigator.clipboard.writeText(text).catch(function (err) {
|
||||
throw err !== undefined
|
||||
@ -88,7 +88,15 @@ export default {
|
||||
const code = button.nextSibling;
|
||||
|
||||
if (code) {
|
||||
clipboardCopy(code.innerText.trim()).then(() => {
|
||||
// replace any weird whitespace characters with a proper '\u20' whitespace
|
||||
const text = code.innerText
|
||||
.replace(
|
||||
/[\f\v\u00a0\u1680\u2000-\u200a\u202f\u205f\u3000\ufeff]/g,
|
||||
" "
|
||||
)
|
||||
.trim();
|
||||
|
||||
clipboardCopy(text).then(() => {
|
||||
button.classList.add("copied");
|
||||
const state = button.innerHTML;
|
||||
button.innerHTML = I18n.t("copy_codeblock.copied");
|
||||
|
Loading…
Reference in New Issue
Block a user