From 744c4ae45abbee81e4b6e3c7d59a33079c4e872e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BA=91=E9=A3=9E?= Date: Tue, 22 Feb 2022 18:00:11 +0800 Subject: [PATCH] feat:copy table elements --- src/editor/dataset/constant/Common.ts | 3 ++- src/editor/utils/clipboard.ts | 33 +++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/editor/dataset/constant/Common.ts b/src/editor/dataset/constant/Common.ts index 6d915ad..fdfb73f 100644 --- a/src/editor/dataset/constant/Common.ts +++ b/src/editor/dataset/constant/Common.ts @@ -1,2 +1,3 @@ export const ZERO = '\u200B' -export const WRAP = '\n' \ No newline at end of file +export const WRAP = '\n' +export const HORIZON_TAB = '\t' \ No newline at end of file diff --git a/src/editor/utils/clipboard.ts b/src/editor/utils/clipboard.ts index ba0e335..ea012b1 100644 --- a/src/editor/utils/clipboard.ts +++ b/src/editor/utils/clipboard.ts @@ -1,6 +1,7 @@ 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 { ElementType } from "../dataset/enum/Element" export function writeText(text: string) { if (!text) return @@ -8,8 +9,32 @@ export function writeText(text: string) { } export function writeTextByElementList(elementList: IElement[]) { - const text = elementList - .map(p => !p.type || TEXTLIKE_ELEMENT_TYPE.includes(p.type) ? p.value : '') - .join('') + let text: string = `` + function pickTextFromElement(payload: IElement[]) { + 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) } \ No newline at end of file