feat:data persistence

This commit is contained in:
黄云飞
2021-12-29 20:42:34 +08:00
parent e4002c3415
commit 8a4688cfd1
8 changed files with 119 additions and 5 deletions
+69 -2
View File
@@ -1,6 +1,7 @@
import { getUUID } from "."
import { deepClone, getUUID } from "."
import { ElementType, IElement } from ".."
import { ZERO } from "../dataset/constant/Common"
import { EDITOR_ELEMENT_ZIP_ATTR } from "../dataset/constant/Element"
export function formatElementList(elementList: IElement[]) {
if (elementList[0]?.value !== ZERO) {
@@ -59,4 +60,70 @@ export function formatElementList(elementList: IElement[]) {
}
i++
}
}
}
export function zipElementList(payload: IElement[]): IElement[] {
const elementList = deepClone(payload)
const zipElementListData: IElement[] = []
let e = 0
while (e < elementList.length) {
let element = elementList[e]
// 筛选所需项
if (e === 0 && element.value === ZERO) {
e++
continue
}
// 表格处理
if (element.type === ElementType.TABLE) {
if (element.trList) {
for (let t = 0; t < element.trList.length; t++) {
const tr = element.trList[t]
delete tr.id
for (let d = 0; d < tr.tdList.length; d++) {
const td = tr.tdList[d]
tr.tdList[d] = {
colspan: td.colspan,
rowspan: td.rowspan,
value: zipElementList(td.value)
}
}
}
}
} else if (element.type === ElementType.HYPERLINK) {
// 超链接处理
const hyperlinkId = element.hyperlinkId
const hyperlinkElement: IElement = {
type: ElementType.HYPERLINK,
value: '',
url: element.url
}
const valueList: IElement[] = []
while (e < elementList.length) {
const hyperlinkE = elementList[e]
if (hyperlinkId !== hyperlinkE.hyperlinkId) {
e--
break
}
delete hyperlinkE.type
delete hyperlinkE.url
valueList.push(hyperlinkE)
e++
}
hyperlinkElement.valueList = zipElementList(valueList)
element = hyperlinkElement
}
// 压缩保留字段
const newElement: IElement = {
value: element.value === ZERO ? `\n` : element.value,
}
EDITOR_ELEMENT_ZIP_ATTR.forEach(attr => {
const value = element[attr] as never
if (value !== undefined) {
newElement[attr] = value
}
})
zipElementListData.push(newElement)
e++
}
return zipElementListData
}
+2 -1
View File
@@ -11,7 +11,7 @@ export function debounce(func: Function, delay: number) {
}
}
export function deepClone(obj: any) {
export function deepClone<T>(obj: T): T {
if (!obj || typeof obj !== 'object') {
return obj
}
@@ -20,6 +20,7 @@ export function deepClone(obj: any) {
newObj = obj.map(item => deepClone(item))
} else {
Object.keys(obj).forEach((key) => {
// @ts-ignore
return newObj[key] = deepClone(obj[key])
})
}