Skip to content

Commit 9df3562

Browse files
authored
Merge pull request #921 from fcollonval/ft/api-diff-widget
Refactor diff widget api
2 parents 8828c77 + 384b89c commit 9df3562

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

src/components/diff/NotebookDiff.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,13 @@ export class NotebookDiff extends Panel implements Git.Diff.IDiffWidget {
164164

165165
try {
166166
// ENH request content only if it changed
167-
const previousContent = await this._model.reference.content();
168-
const currentContent = await this._model.challenger.content();
167+
const referenceContent = await this._model.reference.content();
168+
const challengerContent = await this._model.challenger.content();
169169

170-
const data = await requestAPI<INbdimeDiff>('diffnotebook', 'POST', {
171-
currentContent,
172-
previousContent
173-
});
174-
175-
const nbModel = new NotebookDiffModel(data.base, data.diff);
176-
177-
const nbdWidget = this.createDiffView(nbModel, this._renderMime);
170+
const nbdWidget = await this.createDiffView(
171+
challengerContent,
172+
referenceContent
173+
);
178174

179175
while (this._scroller.widgets.length > 0) {
180176
this._scroller.widgets[0].dispose();
@@ -196,11 +192,18 @@ export class NotebookDiff extends Panel implements Git.Diff.IDiffWidget {
196192
}
197193
}
198194

199-
protected createDiffView(
200-
model: NotebookDiffModel,
201-
renderMime: IRenderMimeRegistry
202-
): NotebookDiffWidget {
203-
return new NotebookDiffWidget(model, renderMime);
195+
protected async createDiffView(
196+
challengerContent: string,
197+
referenceContent: string
198+
): Promise<NotebookDiffWidget> {
199+
const data = await requestAPI<INbdimeDiff>('diffnotebook', 'POST', {
200+
currentContent: challengerContent,
201+
previousContent: referenceContent
202+
});
203+
204+
const model = new NotebookDiffModel(data.base, data.diff);
205+
206+
return new NotebookDiffWidget(model, this._renderMime);
204207
}
205208

206209
/**

src/components/diff/PlainTextDiff.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ export class PlainTextDiff extends Widget implements Git.Diff.IDiffWidget {
6767
* is attached so CodeMirror get proper size.
6868
*/
6969
onAfterAttach(): void {
70-
this.createDiffView().catch(reason => {
71-
this.showError(reason);
72-
});
70+
this.ready
71+
.then(() => {
72+
if (this._challenger !== null && this._reference !== null) {
73+
this.createDiffView(this._challenger, this._reference);
74+
}
75+
})
76+
.catch(reason => {
77+
this.showError(reason);
78+
});
7379
}
7480

7581
/**
@@ -90,8 +96,18 @@ export class PlainTextDiff extends Widget implements Git.Diff.IDiffWidget {
9096
// Clear all
9197
this._container.innerHTML = '';
9298
this._mergeView = null;
99+
93100
// ENH request content only if it changed
94-
this.createDiffView();
101+
if (this._reference !== null) {
102+
this._reference = await this._model.reference.content();
103+
}
104+
if (this._challenger !== null) {
105+
this._challenger = await this._model.challenger.content();
106+
}
107+
108+
this.createDiffView(this._challenger, this._reference);
109+
this._challenger = null;
110+
this._reference = null;
95111
} catch (reason) {
96112
this.showError(reason);
97113
}
@@ -119,31 +135,24 @@ export class PlainTextDiff extends Widget implements Git.Diff.IDiffWidget {
119135
/**
120136
* Create the Plain Text Diff view
121137
*/
122-
protected async createDiffView(): Promise<void> {
138+
protected async createDiffView(
139+
challengerContent: string,
140+
referenceContent: string
141+
): Promise<void> {
123142
if (!this._mergeView) {
124143
const mode =
125144
Mode.findByFileName(this._model.filename) ||
126145
Mode.findBest(this._model.filename);
127146

128-
await this.ready;
129-
130-
if (!this._reference) {
131-
this._reference = await this._model.reference.content();
132-
}
133-
if (!this._challenger) {
134-
this._challenger = await this._model.challenger.content();
135-
}
136-
137147
this._mergeView = mergeView(this._container, {
138-
value: this._challenger,
139-
orig: this._reference,
148+
value: challengerContent,
149+
orig: referenceContent,
140150
mode: mode.mime,
141-
...PlainTextDiff.getDefaultOptions()
151+
...this.getDefaultOptions()
142152
}) as MergeView.MergeViewEditor;
143-
144-
this._reference = null;
145-
this._challenger = null;
146153
}
154+
155+
return Promise.resolve();
147156
}
148157

149158
/**
@@ -164,7 +173,7 @@ export class PlainTextDiff extends Widget implements Git.Diff.IDiffWidget {
164173
</p>`;
165174
}
166175

167-
protected static getDefaultOptions(): Partial<MergeView.MergeViewEditorConfiguration> {
176+
protected getDefaultOptions(): Partial<MergeView.MergeViewEditorConfiguration> {
168177
// FIXME add options from settings and connect settings to update options
169178
return {
170179
lineNumbers: true,

0 commit comments

Comments
 (0)