换行分割:硬回车
typescript
const copyToClipboardWithStyles = async (html: string) => {
const modifiedHtml = html.replace(/\n/g, '<br>').replace(/ /g, ' ')
const item = new ClipboardItem({
'text/html': new Blob([modifiedHtml], { type: 'text/html' }),
'text/plain': new Blob([html], { type: 'text/plain' }),
})
if (navigator.clipboard) {
try {
await navigator.clipboard.write([item])
} catch (error) {
console.error('Failed to copy to clipboard:', error)
}
} else {
console.warn('Your browser does not support writing to the clipboard.')
}
}
换行分割:段落
typescript
const copyToClipboardWithStyles = async (html: string) => {
const paragraphs = html.split('\n').map(line => `<p>${line.replace(/ /g, ' ')}</p>`)
const modifiedHtml = paragraphs.join('')
const item = new ClipboardItem({
'text/html': new Blob([modifiedHtml], { type: 'text/html' }),
'text/plain': new Blob([html], { type: 'text/plain' }),
})
if (navigator.clipboard) {
try {
await navigator.clipboard.write([item])
} catch (error) {
console.error('Failed to copy to clipboard:', error)
}
} else {
console.warn('Your browser does not support writing to the clipboard.')
}
}