feat:copy table elements

pr675
黄云飞 4 years ago
parent db337021e9
commit 744c4ae45a

@ -1,2 +1,3 @@
export const ZERO = '\u200B' export const ZERO = '\u200B'
export const WRAP = '\n' export const WRAP = '\n'
export const HORIZON_TAB = '\t'

@ -1,6 +1,7 @@
import { IElement } from ".." import { IElement } from ".."
import { ZERO } from "../dataset/constant/Common" import { HORIZON_TAB, WRAP, 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"
export function writeText(text: string) { export function writeText(text: string) {
if (!text) return if (!text) return
@ -8,8 +9,32 @@ export function writeText(text: string) {
} }
export function writeTextByElementList(elementList: IElement[]) { export function writeTextByElementList(elementList: IElement[]) {
const text = elementList let text: string = ``
.map(p => !p.type || TEXTLIKE_ELEMENT_TYPE.includes(p.type) ? p.value : '') function pickTextFromElement(payload: IElement[]) {
.join('') for (let e = 0; e < payload.length; e++) {
const element = payload[e]
if (element.type === ElementType.TABLE) {
const trList = element.trList!
for (let t = 0; t < trList.length; t++) {
const tr = trList[t]
for (let d = 0; d < tr.tdList.length; d++) {
const td = tr.tdList[d]
// 排除td首个元素
pickTextFromElement(td.value.slice(1, td.value.length - 1))
if (d !== tr.tdList.length - 1) {
// td之间加水平制表符
text += HORIZON_TAB
}
}
// tr后加换行符
text += WRAP
}
} else if (!element.type || TEXTLIKE_ELEMENT_TYPE.includes(element.type)) {
text += element.value
}
}
}
pickTextFromElement(elementList)
if (!text) return
writeText(text) writeText(text)
} }
Loading…
Cancel
Save