feat: handle list boundary
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { IEditorOption, IElement, RowFlex } from '..'
|
||||
import { IEditorOption, IElement, ListStyle, ListType, RowFlex } from '..'
|
||||
import { ZERO } from '../dataset/constant/Common'
|
||||
import { INLINE_NODE_NAME, TEXTLIKE_ELEMENT_TYPE } from '../dataset/constant/Element'
|
||||
import { listStyleCSSMapping, listTypeElementMapping } from '../dataset/constant/List'
|
||||
import { titleNodeNameMapping, titleOrderNumberMapping } from '../dataset/constant/Title'
|
||||
import { ControlComponent } from '../dataset/enum/Control'
|
||||
import { ElementType } from '../dataset/enum/Element'
|
||||
@@ -53,9 +54,7 @@ export function convertElementToDom(element: IElement, options: DeepRequired<IEd
|
||||
if (element.italic) {
|
||||
dom.style.fontStyle = 'italic'
|
||||
}
|
||||
if (element.size) {
|
||||
dom.style.fontSize = `${element.size}px`
|
||||
}
|
||||
dom.style.fontSize = `${element.size || options.defaultSize}px`
|
||||
if (element.highlight) {
|
||||
dom.style.backgroundColor = element.highlight
|
||||
}
|
||||
@@ -100,6 +99,38 @@ export function writeElementList(elementList: IElement[], options: DeepRequired<
|
||||
const childDom = buildDomFromElementList(zipElementList(element.valueList!))
|
||||
h.innerHTML = childDom.innerHTML
|
||||
clipboardDom.append(h)
|
||||
} else if (element.type === ElementType.LIST) {
|
||||
const list = document.createElement(listTypeElementMapping[element.listType!])
|
||||
if (element.listStyle) {
|
||||
list.style.listStyleType = listStyleCSSMapping[element.listStyle]
|
||||
}
|
||||
// 按照换行符拆分
|
||||
let curListIndex = 0
|
||||
const listElementListMap: Map<number, IElement[]> = new Map()
|
||||
const zipList = zipElementList(element.valueList!)
|
||||
for (let z = 0; z < zipList.length; z++) {
|
||||
const zipElement = zipList[z]
|
||||
const zipValueList = zipElement.value.split('\n')
|
||||
for (let c = 0; c < zipValueList.length; c++) {
|
||||
if (c > 0) {
|
||||
curListIndex += 1
|
||||
}
|
||||
const value = zipValueList[c]
|
||||
const listElementList = listElementListMap.get(curListIndex) || []
|
||||
listElementList.push({
|
||||
...zipElement,
|
||||
value,
|
||||
})
|
||||
listElementListMap.set(curListIndex, listElementList)
|
||||
}
|
||||
}
|
||||
listElementListMap.forEach(listElementList => {
|
||||
const li = document.createElement('li')
|
||||
const childDom = buildDomFromElementList(listElementList)
|
||||
li.innerHTML = childDom.innerHTML
|
||||
list.append(li)
|
||||
})
|
||||
clipboardDom.append(list)
|
||||
} else if (element.type === ElementType.IMAGE) {
|
||||
const img = document.createElement('img')
|
||||
if (element.value) {
|
||||
@@ -218,6 +249,27 @@ export function getElementListByHTML(htmlText: string, options: IGetElementListB
|
||||
value: '\n'
|
||||
})
|
||||
}
|
||||
} else if (node.nodeName === 'UL' || node.nodeName === 'OL') {
|
||||
const listNode = node as HTMLOListElement | HTMLUListElement
|
||||
const listElement: IElement = {
|
||||
value: '',
|
||||
type: ElementType.LIST,
|
||||
valueList: []
|
||||
}
|
||||
if (node.nodeName === 'OL') {
|
||||
listElement.listType = ListType.OL
|
||||
} else {
|
||||
listElement.listType = ListType.UL
|
||||
listElement.listStyle = <ListStyle><unknown>listNode.style.listStyleType
|
||||
}
|
||||
listNode.querySelectorAll('li').forEach(li => {
|
||||
const liValueList = getElementListByHTML(li.innerHTML, options)
|
||||
liValueList.unshift({
|
||||
value: '\n'
|
||||
})
|
||||
listElement.valueList!.push(...liValueList)
|
||||
})
|
||||
elementList.push(listElement)
|
||||
} else if (node.nodeName === 'HR') {
|
||||
elementList.push({
|
||||
value: '\n',
|
||||
|
||||
@@ -4,7 +4,7 @@ import { LaTexParticle } from '../core/draw/particle/latex/LaTexParticle'
|
||||
import { defaultCheckboxOption } from '../dataset/constant/Checkbox'
|
||||
import { ZERO } from '../dataset/constant/Common'
|
||||
import { defaultControlOption } from '../dataset/constant/Control'
|
||||
import { EDITOR_ELEMENT_ZIP_ATTR, TEXTLIKE_ELEMENT_TYPE } from '../dataset/constant/Element'
|
||||
import { EDITOR_ELEMENT_CONTEXT_ATTR, EDITOR_ELEMENT_ZIP_ATTR, TEXTLIKE_ELEMENT_TYPE } from '../dataset/constant/Element'
|
||||
import { titleSizeMapping } from '../dataset/constant/Title'
|
||||
import { ControlComponent, ControlType } from '../dataset/enum/Control'
|
||||
import { ITd } from '../interface/table/Td'
|
||||
@@ -31,7 +31,8 @@ export function formatElementList(elementList: IElement[], options: IFormatEleme
|
||||
isHandleFirstElement: true,
|
||||
...options
|
||||
}
|
||||
if (isHandleFirstElement && elementList[0]?.value !== ZERO) {
|
||||
const startElement = elementList[0]
|
||||
if (isHandleFirstElement && startElement?.value !== ZERO && startElement?.value !== '\n') {
|
||||
elementList.unshift({
|
||||
value: ZERO
|
||||
})
|
||||
@@ -539,4 +540,25 @@ export function getElementRowFlex(node: HTMLElement) {
|
||||
|
||||
export function isTextLikeElement(element: IElement): boolean {
|
||||
return !element.type || TEXTLIKE_ELEMENT_TYPE.includes(element.type)
|
||||
}
|
||||
|
||||
export function formatElementContext(sourceElementList: IElement[], formatElementList: IElement[], anchorIndex: number) {
|
||||
const anchorElement = sourceElementList[anchorIndex]
|
||||
const anchorNextElement = sourceElementList[anchorIndex + 1]
|
||||
const copyElement = anchorElement?.value === ZERO && anchorNextElement && anchorNextElement.value !== ZERO
|
||||
? anchorNextElement
|
||||
: anchorElement
|
||||
if (!copyElement) return
|
||||
for (let e = 0; e < formatElementList.length; e++) {
|
||||
const targetElement = formatElementList[e]
|
||||
for (let i = 0; i < EDITOR_ELEMENT_CONTEXT_ATTR.length; i++) {
|
||||
const attr = EDITOR_ELEMENT_CONTEXT_ATTR[i]
|
||||
const value = copyElement[attr] as never
|
||||
if (value !== undefined) {
|
||||
targetElement[attr] = value
|
||||
} else {
|
||||
delete targetElement[attr]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user