Commit 23f3b31b by qlintonger xeno

feat: 额外设置内容

parent 67fbd5eb
......@@ -245,8 +245,8 @@ export class Processing {
}
}
}
const treeOld = reconstructTree(constructNodeA)
const treeNew = reconstructTree(constructNodeB)
const treeOld = reconstructTree(constructNodeA, contentHoldNode)
const treeNew = reconstructTree(constructNodeB, contentHoldNode)
console.log('the resp', {constructNodeA, constructNodeB})
return {
treeOld, treeNew
......
import { TreeReconstructed } from '@/lib/XMLProcessor/src/typing'
function createNodeFormDataItem(current: any, d: Document, contentHoldNode: string[]) {
let rootNode = d.createElement(current.label);
rootNode.setAttribute('data-key', current.key);
rootNode.setAttribute('data-indent-level', current.chained.length.toString());
rootNode.setAttribute('data-w-e-type', current.label);
if (current.type) {
rootNode.setAttribute('data-modify-type', current.type);
}
if (contentHoldNode.includes(current.label)) {
rootNode.textContent = current.textContent;
}
return rootNode
}
export function reconstructTree(data: TreeReconstructed[]): string {
const doc = new Document()
let root: any
export function reconstructTree(data: any[], contentHoldNode: string[]) {
const d = new Document();
let rootNode: any = null;
const nodeMap = new Map<string, any>();
for (let i = 0; i < data.length; i++) {
if (i===0) {
root = doc.createElement(data[i].label)
root.setAttribute('data-key', data[i].key)
root.setAttribute('data-indent-level', data[i].chained.length.toString())
root.setAttribute('data-w-e-type', data[i].label)
doc.appendChild(root)
const current = data[i];
if (!rootNode) {
rootNode = createNodeFormDataItem(current, d, contentHoldNode)
nodeMap.set(current.key, rootNode)
d.appendChild(rootNode);
} else {
const nv = doc.createElement(data[i].label)
nv.setAttribute('data-key', data[i].key)
nv.setAttribute('data-indent-level', data[i].chained.length.toString())
nv.setAttribute('data-w-e-type', data[i].label)
if (data[i].type) {
nv.setAttribute('data-modify-type', data[i].type)
}
if (data[i].label === 'PARA' || data[i].label === 'TITLE') {
nv.textContent = data[i].textContent
console.log('indent-here', data[i].chained.length)
const previousNode = data[i - 1];
// 截取至当前节点的结合
const nodeSetUntilNow = data.slice(0, i + 1);
if (previousNode.chained.length < current.chained.length) {
// 把上一个节点视为parent即可
const parentNode = nodeMap.get(previousNode.key)!;
const currentNode = createNodeFormDataItem(current, d, contentHoldNode);
parentNode.appendChild(currentNode);
nodeMap.set(current.key, currentNode);
} else {
nv.textContent = ' '
nv.setAttribute('data-modify-type', 'null-blank')
// 找到离自己最近的上层节点
const parentItem = nodeSetUntilNow.findLast((node) => {
return node.chained.length === current.chained.length - 1;
})!;
const parentNode = nodeMap.get(parentItem.key)!;
const currentNode = createNodeFormDataItem(current, d, contentHoldNode);
parentNode.appendChild(currentNode);
nodeMap.set(current.key, currentNode);
}
root.appendChild(nv)
}
}
// Serialize to XML string
const serializer = new XMLSerializer()
return serializer.serializeToString(doc)
return new XMLSerializer().serializeToString(d)
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment