Skip to content

Commit 42ae1fe

Browse files
committed
Fix support for diffing deltas with blanks
1 parent b9cebf8 commit 42ae1fe

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/SIL.XForge.Scripture/ClientApp/src/app/translate/editor/editor-history/editor-history.service.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ describe('EditorHistoryService', () => {
3434
expect(obj.subObj).toBeNull();
3535
});
3636

37+
it('should remove blanks', () => {
38+
const delta = new Delta().insert('Hello ').insert({ blank: true }).insert({ blank: false }).insert('World');
39+
const result = service.removeBlanks(delta);
40+
expect(result).toEqual(new Delta().insert('Hello World'));
41+
});
42+
3743
describe('formatTimestamp', () => {
3844
it('should return "Invalid Date" if timestamp is null or empty', () => {
3945
expect(service.formatTimestamp(null)).toBe('Invalid Date');

src/SIL.XForge.Scripture/ClientApp/src/app/translate/editor/editor-history/editor-history.service.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Injectable } from '@angular/core';
2+
import { cloneDeep } from 'lodash-es';
23
import { Delta } from 'quill';
4+
import { DeltaOperation } from 'rich-text';
35
import { I18nService } from 'xforge-common/i18n.service';
46

57
const MILLISECONDS_IN_A_DAY = 24 * 60 * 60 * 1000;
@@ -42,6 +44,10 @@ export class EditorHistoryService {
4244
deltaA.forEach(obj => this.removeCid(obj));
4345
deltaB.forEach(obj => this.removeCid(obj));
4446

47+
// Remove the blanks from the deltas
48+
deltaA = this.removeBlanks(deltaA);
49+
deltaB = this.removeBlanks(deltaB);
50+
4551
const diff: Delta = deltaA.diff(deltaB);
4652

4753
// Process each op in the diff
@@ -70,4 +76,19 @@ export class EditorHistoryService {
7076
if (typeof obj[subObj] === 'object' && obj[subObj] != null) this.removeCid(obj[subObj]);
7177
}
7278
}
79+
80+
removeBlanks(modelDelta: Delta): Delta {
81+
if (modelDelta.ops == null || modelDelta.ops.length < 1) {
82+
return new Delta();
83+
}
84+
const adjustedDelta = new Delta();
85+
for (const op of modelDelta.ops) {
86+
const cloneOp: DeltaOperation | undefined = cloneDeep(op);
87+
if (!(cloneOp.insert != null && cloneOp.insert['blank'] != null)) {
88+
(adjustedDelta as any).push(cloneOp);
89+
}
90+
}
91+
92+
return adjustedDelta;
93+
}
7394
}

0 commit comments

Comments
 (0)