Commit 8110c56b by qlintonger xeno

feat: 使用diff_match_patch来对比完毕+2345

parent 0a4ed742
......@@ -11,6 +11,7 @@
<TITLEC>发动机QEC拆卸(V2500-A5系列)</TITLEC>
<TITLE>Remove the Engine's QEC(V2500-A5 series)XXXX</TITLE>
<TITLE>Remove the Engine's QEC(V2500-A5 series)XXXX</TITLE>
<TITLE>Remove the Engine's QEC(V2500-A5 series)XXXX</TITLE>
<TOPIC CK-LEVEL="C">
<TITLEC>飞机/发动机基本信息</TITLEC>
<TITLE>AIRCRAFT/ENGINE INFORMATION</TITLE>
......
export function reconstructTree(data: any[]) {
// Step 1: Sort data by chained array to ensure parent-child order
const sortedData = [...data].sort((a, b) => {
const aChained = a.chained;
const bChained = b.chained;
for (let i = 0; i < Math.max(aChained.length, bChained.length); i++) {
const aVal = aChained[i] || 0;
const bVal = bChained[i] || 0;
if (aVal !== bVal) return aVal - bVal;
}
return 0;
});
// Step 2: Build a map for quick lookup and to track children
const nodeMap = new Map();
const result: any[] = [];
sortedData.forEach(item => {
nodeMap.set(item.key, { ...item, children: [] });
});
// Step 3: Assign children to parents based on chained hierarchy
sortedData.forEach(item => {
const node = nodeMap.get(item.key);
const chained = item.chained;
// Find the parent by looking for the closest shorter chained array
let parent = null;
for (let i = chained.length - 1; i > 0; i--) {
const parentChained = chained.slice(0, i);
const parentKey = sortedData.find(d =>
d.type !== 'placeholder' &&
d.chained.length === parentChained.length &&
d.chained.every((val: any, idx: number) => val === parentChained[idx])
)?.key;
if (parentKey) {
parent = nodeMap.get(parentKey);
break;
}
}
if (parent) {
parent.children.push(node);
import { TreeReconstructed } from '@/lib/XMLProcessor/src/typing'
export function reconstructTree(data: TreeReconstructed[]): string {
const doc = new Document()
let root: 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)
} else {
result.push(node);
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)
}
});
// Step 4: Convert to XML
function createXmlNode(node: any, doc: Document) {
const element = doc.createElement(node.label);
// Set data-key attribute
element.setAttribute('data-key', node.key);
// Set data-type attribute if type is not null
if (node.type !== null) {
element.setAttribute('data-modify-type', node.type);
if (data[i].label === 'PARA' || data[i].label === 'TITLE') {
nv.textContent = data[i].textContent
}
element.setAttribute('data-w-e-type', node.label)
element.setAttribute('data-indent-level', node.chained.length.toString())
// Set textContent for TITLE or PARA nodes
if (node.label === 'TITLE' || node.label === 'PARA') {
element.textContent = node.textContent;
root.appendChild(nv)
}
// Recursively add children
node.children.forEach((child: any) => {
element.appendChild(createXmlNode(child, doc));
});
return element;
}
// Create XML document
const doc = new Document();
// Use the first element (JOBCARD) as the root if it exists
if (result.length > 0) {
const rootNode = createXmlNode(result[0], doc)
doc.appendChild(rootNode)
}
// Serialize to XML string
const serializer = new XMLSerializer();
return serializer.serializeToString(doc);
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