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',
+67 -20
View File
@@ -5,9 +5,22 @@ import { defaultCheckboxOption } from '../dataset/constant/Checkbox'
import { ZERO } from '../dataset/constant/Common'
import { defaultControlOption } from '../dataset/constant/Control'
import { EDITOR_ELEMENT_ZIP_ATTR } from '../dataset/constant/Element'
import { titleSizeMapping } from '../dataset/constant/Title'
import { ControlComponent, ControlType } from '../dataset/enum/Control'
import { ITd } from '../interface/table/Td'
export function unzipElementList(elementList: IElement[]): IElement[] {
const result: IElement[] = []
for (let v = 0; v < elementList.length; v++) {
const valueItem = elementList[v]
const textList = splitText(valueItem.value)
for (let d = 0; d < textList.length; d++) {
result.push({ ...valueItem, value: textList[d] })
}
}
return result
}
interface IFormatElementListOption {
isHandleFirstElement?: boolean;
editorOptions: Required<IEditorOption>;
@@ -58,19 +71,12 @@ export function formatElementList(elementList: IElement[], options: IFormatEleme
}
}
} else if (el.type === ElementType.HYPERLINK) {
const valueList = el.valueList || []
// 移除父节点
elementList.splice(i, 1)
// 追加字节点
// 元素展开
const valueList = unzipElementList(el.valueList || [])
// 追加节点
if (valueList.length) {
// 元素展开
if (valueList[0].value.length > 1) {
const deleteValue = valueList.splice(0, 1)[0]
const deleteTextList = splitText(deleteValue.value)
for (let d = 0; d < deleteTextList.length; d++) {
valueList.splice(d, 0, { ...deleteValue, value: deleteTextList[d] })
}
}
const hyperlinkId = getUUID()
for (let v = 0; v < valueList.length; v++) {
const value = valueList[v]
@@ -83,19 +89,12 @@ export function formatElementList(elementList: IElement[], options: IFormatEleme
}
i--
} else if (el.type === ElementType.DATE) {
const valueList = el.valueList || []
// 移除父节点
elementList.splice(i, 1)
// 追加字节点
// 元素展开
const valueList = unzipElementList(el.valueList || [])
// 追加节点
if (valueList.length) {
// 元素展开
if (valueList[0].value.length > 1) {
const deleteValue = valueList.splice(0, 1)[0]
const deleteTextList = splitText(deleteValue.value)
for (let d = 0; d < deleteTextList.length; d++) {
valueList.splice(d, 0, { ...deleteValue, value: deleteTextList[d] })
}
}
const dateId = getUUID()
for (let v = 0; v < valueList.length; v++) {
const value = valueList[v]
@@ -107,6 +106,31 @@ export function formatElementList(elementList: IElement[], options: IFormatEleme
}
}
i--
} else if (el.type === ElementType.TITLE) {
// 移除父节点
elementList.splice(i, 1)
// 元素展开
const valueList = unzipElementList(el.valueList || [])
// 追加节点
if (valueList.length) {
const titleId = getUUID()
const titleOptions = editorOptions.title
for (let v = 0; v < valueList.length; v++) {
const value = valueList[v]
value.titleId = titleId
value.type = el.type
value.level = el.level
if (!value.size) {
value.size = titleOptions[titleSizeMapping[value.level!]]
}
if (value.bold === undefined) {
value.bold = true
}
elementList.splice(i, 0, value)
i++
}
}
i--
} else if (el.type === ElementType.CONTROL) {
const { prefix, postfix, value, placeholder, code, type, valueSets } = el.control!
const controlId = getUUID()
@@ -370,6 +394,29 @@ export function zipElementList(payload: IElement[]): IElement[] {
}
dateElement.valueList = zipElementList(valueList)
element = dateElement
} else if (element.type === ElementType.TITLE) {
// 标题处理
const titleId = element.titleId
const level = element.level
const titleElement: IElement = {
type: ElementType.TITLE,
value: '',
level
}
const valueList: IElement[] = []
while (e < elementList.length) {
const titleE = elementList[e]
if (titleId !== titleE.titleId) {
e--
break
}
delete titleE.type
delete titleE.level
valueList.push(titleE)
e++
}
titleElement.valueList = zipElementList(valueList)
element = titleElement
} else if (element.type === ElementType.CONTROL) {
// 控件处理
const controlId = element.controlId