比较 2 个物体并寻找差异

问题描述 投票:0回答:1

我有 2 个对象(使用 Syncfusion 编辑器 - Word 创建),我需要比较文档(文本)内的某些内容是否已更改,并且它至少可以工作 50%,但我不知道如何修复它,因为它没有检测到所有改变

Demo Image 1

Demo Image 2

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);
}
javascript recursion javascript-objects syncfusion
1个回答
0
投票

我们的SFDT具有复杂的结构格式,因此在尝试基于SFDT进行比较时无法正常工作。例如,下面所附的文档包含相同的格式内容,因此我没有发现 UI 级别有任何差异,但该文档与 SFDT 级别不匹配。

用户界面级别:

测试文档1

enter image description here

测试文档2

enter image description here

SFDT级别:

测试文档1 enter image description here

测试文档2 enter image description here

我们已经制定了 Word 文档比较的功能计划。我们计划在 9 月 15 日发布的第 3 卷版本中包含此功能,一旦包含此功能,我们将通知您。

WinForms 中支持 Word 文档比较 |反馈门户(syncfusion.com)

© www.soinside.com 2019 - 2024. All rights reserved.