feat: add title element

This commit is contained in:
Hufe921
2023-04-06 21:13:07 +08:00
committed by Hufe
parent 189ca73c32
commit 9701b2153e
20 changed files with 392 additions and 90 deletions
+54 -23
View File
@@ -1,6 +1,7 @@
import { IEditorOption, IElement, RowFlex } from '..'
import { ZERO } from '../dataset/constant/Common'
import { 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'
import { DeepRequired } from '../interface/Common'
@@ -35,6 +36,33 @@ export function writeClipboardItem(text: string, html: string) {
}
}
export function convertElementToDom(element: IElement, options: DeepRequired<IEditorOption>): HTMLElement {
const isBlock = element.rowFlex === RowFlex.CENTER || element.rowFlex === RowFlex.RIGHT
const dom = document.createElement(isBlock ? 'p' : 'span')
dom.style.fontFamily = element.font || options.defaultFont
if (element.rowFlex) {
const isAlignment = element.rowFlex === RowFlex.ALIGNMENT
dom.style.textAlign = isAlignment ? 'justify' : element.rowFlex
}
if (element.color) {
dom.style.color = element.color
}
if (element.bold) {
dom.style.fontWeight = '600'
}
if (element.italic) {
dom.style.fontStyle = 'italic'
}
if (element.size) {
dom.style.fontSize = `${element.size}px`
}
if (element.highlight) {
dom.style.backgroundColor = element.highlight
}
dom.innerText = element.value.replace(new RegExp(`${ZERO}`, 'g'), '\n')
return dom
}
export function writeElementList(elementList: IElement[], options: DeepRequired<IEditorOption>) {
const clipboardDom: HTMLDivElement = document.createElement('div')
function buildDomFromElementList(payload: IElement[]) {
@@ -61,11 +89,18 @@ export function writeElementList(elementList: IElement[], options: DeepRequired<
clipboardDom.append(tableDom)
} else if (element.type === ElementType.HYPERLINK) {
const a = document.createElement('a')
a.innerText = element.valueList![0].value
a.innerText = element.valueList!.map(v => v.value).join('')
if (element.url) {
a.href = element.url
}
clipboardDom.append(a)
} else if (element.type === ElementType.TITLE) {
const h = document.createElement(`h${titleOrderNumberMapping[element.level!]}`)
element.valueList!.forEach(el => {
const dom = convertElementToDom(el, options)
h.append(dom)
})
clipboardDom.append(h)
} else if (element.type === ElementType.IMAGE) {
const img = document.createElement('img')
if (element.value) {
@@ -98,29 +133,12 @@ export function writeElementList(elementList: IElement[], options: DeepRequired<
text = element.value
}
if (!text) continue
const isBlock = element.rowFlex === RowFlex.CENTER || element.rowFlex === RowFlex.RIGHT
const dom = document.createElement(isBlock ? 'p' : 'span')
// 前一个元素是标题,移除首行换行符
if (payload[e - 1]?.type === ElementType.TITLE) {
text = text.replace(/^\n/, '')
}
const dom = convertElementToDom(element, options)
dom.innerText = text.replace(new RegExp(`${ZERO}`, 'g'), '\n')
dom.style.fontFamily = element.font || options.defaultFont
if (element.rowFlex) {
const isAlignment = element.rowFlex === RowFlex.ALIGNMENT
dom.style.textAlign = isAlignment ? 'justify' : element.rowFlex
}
if (element.color) {
dom.style.color = element.color
}
if (element.bold) {
dom.style.fontWeight = '600'
}
if (element.italic) {
dom.style.fontStyle = 'italic'
}
if (element.size) {
dom.style.fontSize = `${element.size}px`
}
if (element.highlight) {
dom.style.backgroundColor = element.highlight
}
clipboardDom.append(dom)
}
}
@@ -186,6 +204,19 @@ export function getElementListByHTML(htmlText: string, options: IGetElementListB
url: aElement.href
})
}
} else if (/H[1-6]/.test(node.nodeName)) {
const hElement = node as HTMLTitleElement
const valueList = getElementListByHTML(hElement.innerHTML, options)
.filter(el => !el.type || el.type === ElementType.TEXT)
elementList.push({
value: '',
type: ElementType.TITLE,
level: titleNodeNameMapping[node.nodeName],
valueList
})
elementList.push({
value: '\n'
})
} else if (node.nodeName === 'HR') {
elementList.push({
value: '\n',