From 03cd85f423d207eea7f88c4267552f4af6945030 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Fri, 7 Apr 2023 17:35:29 +0800 Subject: [PATCH] improve: copy title and table element --- src/editor/dataset/constant/Element.ts | 5 +++++ src/editor/utils/clipboard.ts | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/editor/dataset/constant/Element.ts b/src/editor/dataset/constant/Element.ts index caca247..7de7bf7 100644 --- a/src/editor/dataset/constant/Element.ts +++ b/src/editor/dataset/constant/Element.ts @@ -72,4 +72,9 @@ export const INLINE_ELEMENT_TYPE: ElementType[] = [ ElementType.PAGE_BREAK, ElementType.SEPARATOR, ElementType.TABLE +] + +export const INLINE_NODE_NAME: string[] = [ + 'HR', + 'TABLE' ] \ No newline at end of file diff --git a/src/editor/utils/clipboard.ts b/src/editor/utils/clipboard.ts index 4d87d25..f51c8f6 100644 --- a/src/editor/utils/clipboard.ts +++ b/src/editor/utils/clipboard.ts @@ -1,6 +1,6 @@ import { IEditorOption, IElement, RowFlex } from '..' import { ZERO } from '../dataset/constant/Common' -import { TEXTLIKE_ELEMENT_TYPE } from '../dataset/constant/Element' +import { INLINE_NODE_NAME, TEXTLIKE_ELEMENT_TYPE } from '../dataset/constant/Element' import { titleNodeNameMapping, titleOrderNumberMapping } from '../dataset/constant/Title' import { ControlComponent } from '../dataset/enum/Control' import { ElementType } from '../dataset/enum/Element' @@ -81,7 +81,8 @@ export function writeElementList(elementList: IElement[], options: DeepRequired< const td = tr.tdList[d] tdDom.colSpan = td.colspan tdDom.rowSpan = td.rowspan - tdDom.innerText = td.value[0]?.value || '' + const childDom = buildDomFromElementList(zipElementList(td.value!)) + tdDom.innerHTML = childDom.innerHTML trDom.append(tdDom) } tableDom.append(trDom) @@ -212,9 +213,11 @@ export function getElementListByHTML(htmlText: string, options: IGetElementListB level: titleNodeNameMapping[node.nodeName], valueList }) - elementList.push({ - value: '\n' - }) + if (node.nextSibling && !INLINE_NODE_NAME.includes(node.nextSibling.nodeName)) { + elementList.push({ + value: '\n' + }) + } } else if (node.nodeName === 'HR') { elementList.push({ value: '\n', @@ -247,12 +250,11 @@ export function getElementListByHTML(htmlText: string, options: IGetElementListB } trElement.querySelectorAll('th,td').forEach(tdElement => { const tableCell = tdElement + const valueList = getElementListByHTML(tableCell.innerHTML, options) const td: ITd = { colspan: tableCell.colSpan, rowspan: tableCell.rowSpan, - value: [{ - value: tableCell.innerText - }] + value: valueList } tr.tdList.push(td) })