feat: copy and paste original elements #397 (#426)

This commit is contained in:
baseWalker
2024-01-26 15:13:17 +08:00
committed by GitHub
parent 3cf911c501
commit 2fc16de4e1
3 changed files with 67 additions and 19 deletions
+36 -5
View File
@@ -1,9 +1,38 @@
import { IEditorOption, IElement } from '..'
import { EDITOR_CLIPBOARD } from '../dataset/constant/Editor'
import { DeepRequired } from '../interface/Common'
import { createDomFromElementList } from './element'
import { createDomFromElementList, zipElementList } from './element'
export function writeClipboardItem(text: string, html: string) {
if (!text || !html) return
export interface IClipboardData {
text: string
elementList: IElement[]
}
export function setClipboardData(data: IClipboardData) {
localStorage.setItem(
EDITOR_CLIPBOARD,
JSON.stringify({
text: data.text,
elementList: data.elementList
})
)
}
export function getClipboardData(): IClipboardData | null {
const clipboardText = localStorage.getItem(EDITOR_CLIPBOARD)
return clipboardText ? JSON.parse(clipboardText) : null
}
export function removeClipboardData() {
localStorage.removeItem(EDITOR_CLIPBOARD)
}
export function writeClipboardItem(
text: string,
html: string,
elementList: IElement[]
) {
if (!text && !html && !elementList.length) return
const plainText = new Blob([text], { type: 'text/plain' })
const htmlText = new Blob([html], { type: 'text/html' })
if (window.ClipboardItem) {
@@ -27,6 +56,8 @@ export function writeClipboardItem(text: string, html: string) {
document.execCommand('copy')
fakeElement.remove()
}
// 编辑器结构化数据
setClipboardData({ text, elementList })
}
export function writeElementList(
@@ -40,6 +71,6 @@ export function writeElementList(
// 先追加后移除,否则innerText无法解析换行符
clipboardDom.remove()
const html = clipboardDom.innerHTML
if (!text || !html) return
writeClipboardItem(text, html)
if (!text && !html && !elementList.length) return
writeClipboardItem(text, html, zipElementList(elementList))
}