Commit 23f3b31b by qlintonger xeno

feat: 额外设置内容

parent 67fbd5eb
...@@ -245,8 +245,8 @@ export class Processing { ...@@ -245,8 +245,8 @@ export class Processing {
} }
} }
} }
const treeOld = reconstructTree(constructNodeA) const treeOld = reconstructTree(constructNodeA, contentHoldNode)
const treeNew = reconstructTree(constructNodeB) const treeNew = reconstructTree(constructNodeB, contentHoldNode)
console.log('the resp', {constructNodeA, constructNodeB}) console.log('the resp', {constructNodeA, constructNodeB})
return { return {
treeOld, treeNew 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 { export function reconstructTree(data: any[], contentHoldNode: string[]) {
const doc = new Document() const d = new Document();
let root: any let rootNode: any = null;
const nodeMap = new Map<string, any>();
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
if (i===0) { const current = data[i];
root = doc.createElement(data[i].label) if (!rootNode) {
root.setAttribute('data-key', data[i].key) rootNode = createNodeFormDataItem(current, d, contentHoldNode)
root.setAttribute('data-indent-level', data[i].chained.length.toString()) nodeMap.set(current.key, rootNode)
root.setAttribute('data-w-e-type', data[i].label) d.appendChild(rootNode);
doc.appendChild(root)
} else { } else {
const nv = doc.createElement(data[i].label) const previousNode = data[i - 1];
nv.setAttribute('data-key', data[i].key) // 截取至当前节点的结合
nv.setAttribute('data-indent-level', data[i].chained.length.toString()) const nodeSetUntilNow = data.slice(0, i + 1);
nv.setAttribute('data-w-e-type', data[i].label) if (previousNode.chained.length < current.chained.length) {
if (data[i].type) { // 把上一个节点视为parent即可
nv.setAttribute('data-modify-type', data[i].type) const parentNode = nodeMap.get(previousNode.key)!;
} const currentNode = createNodeFormDataItem(current, d, contentHoldNode);
if (data[i].label === 'PARA' || data[i].label === 'TITLE') { parentNode.appendChild(currentNode);
nv.textContent = data[i].textContent nodeMap.set(current.key, currentNode);
console.log('indent-here', data[i].chained.length)
} else { } 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)
} }
} }
return new XMLSerializer().serializeToString(d)
// Serialize to XML string
const serializer = new XMLSerializer()
return serializer.serializeToString(doc)
} }
\ 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