@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user