我有 2 个对象(使用 Syncfusion 编辑器 - Word 创建),我需要比较文档(文本)内的某些内容是否已更改,并且它至少可以工作 50%,但我不知道如何修复它,因为它没有检测到所有改变
sfdtNewest = https://gist.github.com/danielediazmx/1003399a82cd1b082c1de517f6836ba8 sfdtOlder = https://gist.github.com/danielediazmx/ff625d79867eea11dd29b28cc5b703de
const diff = require('deep-diff');
export function DocumentDiffer(sfdtNewest: string, sfdtOlder: string): string {
if (!sfdtNewest || !sfdtOlder) return JSON.stringify(sfdtNewest);
const newest = JSON.parse(sfdtNewest);
const older = JSON.parse(sfdtOlder);
let newestSections = newest.sections;
let olderSections = older.sections;
newestSections.map((newestSection: any, indexSection: number) => {
newestSection.blocks.map((block: any, indexBlock: number) => {
let addedIndexes: number = 0;
block.inlines.map((inline: any, indexInline: number) => {
let finalIndex = indexInline + addedIndexes;
const lineToCompare = newestSections[indexSection].blocks[indexBlock].inlines[finalIndex];
// TODO: compare in older inlines
const oldSectionExist: boolean = olderSections.length >= indexSection;
const oldBlockExist: boolean = olderSections[indexSection].blocks.length >= indexBlock;
const oldInlineExist: boolean = olderSections[indexSection].blocks[indexBlock].inlines.length >= finalIndex;
const oldInlines: boolean = olderSections[indexSection].blocks[indexBlock].inlines[finalIndex] != undefined;
if (oldSectionExist && oldBlockExist && oldInlineExist && oldInlines && lineToCompare) {
const oldLine = olderSections[indexSection].blocks[indexBlock].inlines[finalIndex];
const nextLine = newestSections[indexSection].blocks[indexBlock].inlines.length > finalIndex ? newestSections[indexSection].blocks[indexBlock].inlines[finalIndex + 1] : null;
const next2ndLine = newestSections[indexSection].blocks[indexBlock].inlines.length > (finalIndex + 1) ? newestSections[indexSection].blocks[indexBlock].inlines[finalIndex + 2] : null;
const nextLineInOld = olderSections[indexSection].blocks[indexBlock].inlines.length > finalIndex ? olderSections[indexSection].blocks[indexBlock].inlines[finalIndex + 1] : null;
const changes = diff(oldLine, lineToCompare);
if (nextLine && !nextLineInOld) {
// text added at the end
nextLine.characterFormat.fontColor = "#45b464"
newestSections[indexSection].blocks[indexBlock].inlines[finalIndex + 1] = nextLine;
} else {
if (changes) {
changes.map((change: any) => {
if (change.kind == 'E') {
if (change.path[0] == 'text') {
if (change.lhs != " " && change.lhs != "") {
// removed
if (nextLine && nextLine.text == oldLine.text || (next2ndLine && next2ndLine.text == oldLine.text)) {
addedIndexes += (nextLine && nextLine.text == oldLine.text ? 1 : 2);
} else {
oldLine.characterFormat.fontColor = "#b44545"
oldLine.characterFormat.strikethrough = "SingleStrike"
const inlines = newestSections[indexSection].blocks[indexBlock].inlines;
const iinlines = [
...inlines.slice(0, finalIndex),
oldLine,
...inlines.slice(finalIndex)
];
newestSections[indexSection].blocks[indexBlock].inlines = iinlines;
}
}
if (change.rhs != " " && change.rhs != "") {
// added
lineToCompare.characterFormat.fontColor = "#00fff6"
}
}
}
})
}
}
} else {}
});
});
});
newest.sections = newestSections;
return JSON.stringify(newest);
}