feat:copy with style
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { debounce } from '../../utils'
|
import { debounce } from '../../utils'
|
||||||
import { getElementListByClipboardHTML } from '../../utils/clipboard'
|
import { getElementListByHTML } from '../../utils/clipboard'
|
||||||
import { Draw } from '../draw/Draw'
|
import { Draw } from '../draw/Draw'
|
||||||
import { CanvasEvent } from '../event/CanvasEvent'
|
import { CanvasEvent } from '../event/CanvasEvent'
|
||||||
|
|
||||||
@@ -67,7 +67,7 @@ export class CursorAgent {
|
|||||||
}
|
}
|
||||||
if (item.type === 'text/html' && isHTML) {
|
if (item.type === 'text/html' && isHTML) {
|
||||||
item.getAsString(htmlText => {
|
item.getAsString(htmlText => {
|
||||||
const elementList = getElementListByClipboardHTML(htmlText)
|
const elementList = getElementListByHTML(htmlText)
|
||||||
this.draw.insertElementList(elementList)
|
this.draw.insertElementList(elementList)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { MouseEventButton } from '../../dataset/enum/Event'
|
|||||||
import { KeyMap } from '../../dataset/enum/Keymap'
|
import { KeyMap } from '../../dataset/enum/Keymap'
|
||||||
import { IElement } from '../../interface/Element'
|
import { IElement } from '../../interface/Element'
|
||||||
import { ICurrentPosition } from '../../interface/Position'
|
import { ICurrentPosition } from '../../interface/Position'
|
||||||
import { writeTextByElementList } from '../../utils/clipboard'
|
import { writeElementList } from '../../utils/clipboard'
|
||||||
import { Cursor } from '../cursor/Cursor'
|
import { Cursor } from '../cursor/Cursor'
|
||||||
import { Draw } from '../draw/Draw'
|
import { Draw } from '../draw/Draw'
|
||||||
import { HyperlinkParticle } from '../draw/particle/HyperlinkParticle'
|
import { HyperlinkParticle } from '../draw/particle/HyperlinkParticle'
|
||||||
@@ -571,7 +571,7 @@ export class CanvasEvent {
|
|||||||
const { startIndex, endIndex } = this.range.getRange()
|
const { startIndex, endIndex } = this.range.getRange()
|
||||||
const elementList = this.draw.getElementList()
|
const elementList = this.draw.getElementList()
|
||||||
if (startIndex !== endIndex) {
|
if (startIndex !== endIndex) {
|
||||||
writeTextByElementList(elementList.slice(startIndex + 1, endIndex + 1))
|
writeElementList(elementList.slice(startIndex + 1, endIndex + 1))
|
||||||
let curIndex: number
|
let curIndex: number
|
||||||
if (activeControl) {
|
if (activeControl) {
|
||||||
curIndex = this.control.cut()
|
curIndex = this.control.cut()
|
||||||
@@ -588,7 +588,7 @@ export class CanvasEvent {
|
|||||||
const { startIndex, endIndex } = this.range.getRange()
|
const { startIndex, endIndex } = this.range.getRange()
|
||||||
const elementList = this.draw.getElementList()
|
const elementList = this.draw.getElementList()
|
||||||
if (startIndex !== endIndex) {
|
if (startIndex !== endIndex) {
|
||||||
writeTextByElementList(elementList.slice(startIndex + 1, endIndex + 1))
|
writeElementList(elementList.slice(startIndex + 1, endIndex + 1))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,48 +1,84 @@
|
|||||||
import { IElement } from '..'
|
import { IElement } from '..'
|
||||||
import { HORIZON_TAB, WRAP, ZERO } from '../dataset/constant/Common'
|
import { ZERO } from '../dataset/constant/Common'
|
||||||
import { TEXTLIKE_ELEMENT_TYPE } from '../dataset/constant/Element'
|
import { TEXTLIKE_ELEMENT_TYPE } from '../dataset/constant/Element'
|
||||||
import { ElementType } from '../dataset/enum/Element'
|
import { ElementType } from '../dataset/enum/Element'
|
||||||
|
import { zipElementList } from './element'
|
||||||
|
|
||||||
export function writeText(text: string) {
|
export function writeClipboardItem(text: string, html: string) {
|
||||||
if (!text) return
|
if (!text || !html) return
|
||||||
window.navigator.clipboard.writeText(text.replaceAll(ZERO, `\n`))
|
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])
|
||||||
}
|
}
|
||||||
|
|
||||||
export function writeTextByElementList(elementList: IElement[]) {
|
export function writeElementList(elementList: IElement[]) {
|
||||||
let text = ``
|
const clipboardDom: HTMLDivElement = document.createElement('div')
|
||||||
function pickTextFromElement(payload: IElement[]) {
|
function buildDomFromElementList(payload: IElement[]) {
|
||||||
for (let e = 0; e < payload.length; e++) {
|
for (let e = 0; e < payload.length; e++) {
|
||||||
const element = payload[e]
|
const element = payload[e]
|
||||||
|
// 构造表格
|
||||||
if (element.type === ElementType.TABLE) {
|
if (element.type === ElementType.TABLE) {
|
||||||
if (e !== 0) {
|
const tableDom: HTMLTableElement = document.createElement('table')
|
||||||
text += WRAP
|
|
||||||
}
|
|
||||||
const trList = element.trList!
|
const trList = element.trList!
|
||||||
for (let t = 0; t < trList.length; t++) {
|
for (let t = 0; t < trList.length; t++) {
|
||||||
|
const trDom = document.createElement('tr')
|
||||||
const tr = trList[t]
|
const tr = trList[t]
|
||||||
for (let d = 0; d < tr.tdList.length; d++) {
|
for (let d = 0; d < tr.tdList.length; d++) {
|
||||||
|
const tdDom = document.createElement('td')
|
||||||
const td = tr.tdList[d]
|
const td = tr.tdList[d]
|
||||||
// 排除td首个元素
|
tdDom.innerText = td.value[0].value
|
||||||
pickTextFromElement(td.value.slice(1, td.value.length))
|
trDom.append(tdDom)
|
||||||
if (d !== tr.tdList.length - 1) {
|
|
||||||
// td之间加水平制表符
|
|
||||||
text += HORIZON_TAB
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// tr后加换行符
|
tableDom.append(trDom)
|
||||||
text += WRAP
|
|
||||||
}
|
}
|
||||||
|
clipboardDom.append(tableDom)
|
||||||
|
} else if (element.type === ElementType.HYPERLINK) {
|
||||||
|
const a = document.createElement('a')
|
||||||
|
a.innerText = element.valueList![0].value
|
||||||
|
if (element.url) {
|
||||||
|
a.href = element.url
|
||||||
|
}
|
||||||
|
clipboardDom.append(a)
|
||||||
} else if (!element.type || TEXTLIKE_ELEMENT_TYPE.includes(element.type)) {
|
} else if (!element.type || TEXTLIKE_ELEMENT_TYPE.includes(element.type)) {
|
||||||
text += element.value
|
const span = document.createElement('span')
|
||||||
|
let text = ''
|
||||||
|
if (element.type === ElementType.CONTROL) {
|
||||||
|
text = element.control!.value?.[0]?.value || ''
|
||||||
|
} else {
|
||||||
|
text = element.value
|
||||||
|
}
|
||||||
|
if (!text) continue
|
||||||
|
span.innerText = text.replace(new RegExp(`${ZERO}`, 'g'), '\n')
|
||||||
|
if (element.color) {
|
||||||
|
span.style.color = element.color
|
||||||
|
}
|
||||||
|
if (element.bold) {
|
||||||
|
span.style.fontWeight = '600'
|
||||||
|
}
|
||||||
|
if (element.italic) {
|
||||||
|
span.style.fontStyle = 'italic'
|
||||||
|
}
|
||||||
|
if (element.size) {
|
||||||
|
span.style.fontSize = `${element.size}px`
|
||||||
|
}
|
||||||
|
clipboardDom.append(span)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pickTextFromElement(elementList)
|
buildDomFromElementList(zipElementList(elementList))
|
||||||
if (!text) return
|
// 写入剪贴板
|
||||||
writeText(text.replace(new RegExp(`^${ZERO}`), ''))
|
const text = clipboardDom.innerText
|
||||||
|
const html = clipboardDom.innerHTML
|
||||||
|
if (!text || !html) return
|
||||||
|
writeClipboardItem(text, html)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getElementListByClipboardHTML(htmlText: string): IElement[] {
|
export function getElementListByHTML(htmlText: string): IElement[] {
|
||||||
const elementList: IElement[] = []
|
const elementList: IElement[] = []
|
||||||
function findTextNode(dom: Element | Node) {
|
function findTextNode(dom: Element | Node) {
|
||||||
if (dom.nodeType === 3) {
|
if (dom.nodeType === 3) {
|
||||||
@@ -61,14 +97,20 @@ export function getElementListByClipboardHTML(htmlText: string): IElement[] {
|
|||||||
const childNodes = dom.childNodes
|
const childNodes = dom.childNodes
|
||||||
for (let n = 0; n < childNodes.length; n++) {
|
for (let n = 0; n < childNodes.length; n++) {
|
||||||
const node = childNodes[n]
|
const node = childNodes[n]
|
||||||
findTextNode(node)
|
// br元素与display:block元素需换行
|
||||||
// block
|
if (node.nodeName === 'BR') {
|
||||||
if (node.nodeType === 1 && n !== childNodes.length - 1) {
|
elementList.push({
|
||||||
const display = window.getComputedStyle(node as Element).display
|
value: '\n'
|
||||||
if (display === 'block') {
|
})
|
||||||
elementList.push({
|
} else {
|
||||||
value: `\n`
|
findTextNode(node)
|
||||||
})
|
if (node.nodeType === 1 && n !== childNodes.length - 1) {
|
||||||
|
const display = window.getComputedStyle(node as Element).display
|
||||||
|
if (display === 'block') {
|
||||||
|
elementList.push({
|
||||||
|
value: '\n'
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user