diff --git a/.vscode/settings.json b/.vscode/settings.json index 60a2174..2bcc355 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -18,7 +18,8 @@ "trlist", "vite", "Yahei", - "vitepress" + "vitepress", + "contenteditable" ], "cSpell.ignorePaths": [ ".github", diff --git a/src/editor/utils/clipboard.ts b/src/editor/utils/clipboard.ts index 9580815..6bc3daa 100644 --- a/src/editor/utils/clipboard.ts +++ b/src/editor/utils/clipboard.ts @@ -9,12 +9,27 @@ export function writeClipboardItem(text: string, html: string) { if (!text || !html) return const plainText = new Blob([text], { type: 'text/plain' }) const htmlText = new Blob([html], { type: 'text/html' }) - // @ts-ignore - const item = new ClipboardItem({ - [plainText.type]: plainText, - [htmlText.type]: htmlText - }) - window.navigator.clipboard.write([item]) + if (window.ClipboardItem) { + // @ts-ignore + const item = new ClipboardItem({ + [plainText.type]: plainText, + [htmlText.type]: htmlText + }) + 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) {