Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SF-1665 Clear edit history before chapter embeds are duplicated #1461

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ export function getPoetryVerseTextDoc(id: TextDocId): TextData {
return delta;
}

export function getEmptyChapterDoc(id: TextDocId): TextData {
export function getEmptyChapterDoc(id: TextDocId, includeHeading: boolean = true): TextData {
const delta = new Delta();
delta.insert({ blank: true }, { segment: 's_1' });
delta.insert('\n', { para: { style: 's' } });
if (includeHeading) {
delta.insert({ blank: true }, { segment: 's_1' });
delta.insert('\n', { para: { style: 's' } });
}
delta.insert({ chapter: { number: id.chapterNum.toString(), style: 'c' } });
delta.insert({ blank: true }, { segment: 's_2' });
delta.insert('\n', { para: { style: 's' } });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,15 @@ export function registerScripture(): string[] {
if (delta == null) {
return;
}
const sourceDelta: DeltaStatic = delta[source];
// Workaround to cancel the op if an undo gets triggered that includes inserting a chapter embed.
// The delta diff algorithm has a problem if an op occurs before the first text inserts in a doc. Generally,
// this has no effect, but when edits are made in a blank section heading after a chapter embed at
// index 0, duplicate chapter headings appear and the doc gets corrupted.
if (sourceDelta.ops != null && sourceDelta.ops.filter(o => o.insert?.chapter != null).length > 0) {
this.clear();
return;
}
this.stack[dest].push(delta);
this.lastRecorded = 0;
this.ignoreChange = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,28 @@ describe('TextComponent', () => {
expect(rangePostUndo).toBeTruthy();
}));

it('drops history when edit includes chapter embed', fakeAsync(() => {
const env = new TestEnvironment({ includeHeading: false });
env.fixture.detectChanges();
env.id = new TextDocId('project01', 43, 1);
tick();
env.fixture.detectChanges();
const updateContentsSpy: jasmine.Spy<any> = spyOn<any>(env.component.editor!, 'updateContents').and.callThrough();

const range: RangeStatic = env.component.getSegmentRange('s_1')!;
env.component.editor!.setSelection(range.index + 1, 'user');
tick();
env.fixture.detectChanges();
env.insertText(range.index + 1, 'text');
expect(env.component.getSegmentText('s_1')).toEqual('text');
expect(updateContentsSpy.calls.count()).toEqual(2);

// SUT
env.triggerUndo();
expect(env.component.getSegmentText('s_1')).toEqual('text');
expect(updateContentsSpy.calls.count()).toEqual(2);
}));

describe('MultiCursor Presence', () => {
it('should not update presence if something other than the user moves the cursor', fakeAsync(() => {
const env: TestEnvironment = new TestEnvironment();
Expand Down Expand Up @@ -2747,6 +2769,7 @@ interface PerformDropTestArgs {
interface TestEnvCtorArgs {
chapterNum?: number;
textDoc?: RichText.DeltaOperation[];
includeHeading?: boolean;
}

class MockDragEvent extends DragEvent {
Expand Down Expand Up @@ -2817,7 +2840,7 @@ class TestEnvironment {
private _onlineStatus = new BehaviorSubject<boolean>(true);
private isOnline: boolean = true;

constructor({ textDoc, chapterNum }: TestEnvCtorArgs = {}) {
constructor({ textDoc, chapterNum, includeHeading }: TestEnvCtorArgs = {}) {
when(mockedPwaService.onlineStatus).thenReturn(this._onlineStatus.asObservable());
when(mockedPwaService.isOnline).thenCall(() => this.isOnline);
when(mockedTranslocoService.translate<string>(anything())).thenCall(
Expand Down Expand Up @@ -2848,7 +2871,7 @@ class TestEnvironment {
data: getPoetryVerseTextDoc(lukTextDocId),
type: RichText.type.name
},
{ id: jhnTextDocId.toString(), data: getEmptyChapterDoc(jhnTextDocId), type: RichText.type.name }
{ id: jhnTextDocId.toString(), data: getEmptyChapterDoc(jhnTextDocId, includeHeading), type: RichText.type.name }
]);

when(mockedProjectService.getText(anything())).thenCall(id =>
Expand Down