fix:compatible with browsers that do not support ClipboardItem #108

pr675
Hufe921 3 years ago
parent a369adbe32
commit 196f638318

@ -18,7 +18,8 @@
"trlist", "trlist",
"vite", "vite",
"Yahei", "Yahei",
"vitepress" "vitepress",
"contenteditable"
], ],
"cSpell.ignorePaths": [ "cSpell.ignorePaths": [
".github", ".github",

@ -9,12 +9,27 @@ export function writeClipboardItem(text: string, html: string) {
if (!text || !html) return if (!text || !html) return
const plainText = new Blob([text], { type: 'text/plain' }) const plainText = new Blob([text], { type: 'text/plain' })
const htmlText = new Blob([html], { type: 'text/html' }) const htmlText = new Blob([html], { type: 'text/html' })
// @ts-ignore if (window.ClipboardItem) {
const item = new ClipboardItem({ // @ts-ignore
[plainText.type]: plainText, const item = new ClipboardItem({
[htmlText.type]: htmlText [plainText.type]: plainText,
}) [htmlText.type]: htmlText
window.navigator.clipboard.write([item]) })
window.navigator.clipboard.write([item])
} else {
const fakeElement = document.createElement('div')
fakeElement.setAttribute('contenteditable', 'true')
fakeElement.innerHTML = html
document.body.append(fakeElement)
// add new range
const selection = window.getSelection()
const range = document.createRange()
range.selectNodeContents(fakeElement)
selection?.removeAllRanges()
selection?.addRange(range)
document.execCommand('copy')
fakeElement.remove()
}
} }
export function writeElementList(elementList: IElement[], options: DeepRequired<IEditorOption>) { export function writeElementList(elementList: IElement[], options: DeepRequired<IEditorOption>) {

Loading…
Cancel
Save