Skip to content

Commit 05db94f

Browse files
committedNov 14, 2023
fix: inline preview with loading state
1 parent f21dfe8 commit 05db94f

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed
 

‎src/autocomplete.js

+26-13
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,17 @@ class Autocomplete {
9191
var initialPosition = this.completionProvider && this.completionProvider.initialPosition;
9292
if (this.autoShown || (this.popup && this.popup.isOpen) || !initialPosition) return;
9393

94-
var completionsForEmpty = [{
95-
caption: config.nls("Loading..."),
96-
value: ""
97-
}];
98-
this.completions = new FilteredList(completionsForEmpty);
94+
this.completions = new FilteredList(Autocomplete.completionsForLoading);
9995
this.openPopup(this.editor, initialPosition.prefix, false);
10096
this.popup.renderer.setStyle("ace_loading", true);
10197
}.bind(this), this.stickySelectionDelay);
10298
}
10399

100+
static completionsForLoading = [{
101+
caption: config.nls("Loading..."),
102+
value: ""
103+
}];
104+
104105
$init() {
105106
this.popup = new AcePopup(this.parentNode || document.body || document.documentElement);
106107
this.popup.on("click", function(e) {
@@ -238,7 +239,8 @@ class Autocomplete {
238239
this.popup.autoSelect = this.autoSelect;
239240
this.popup.setSelectOnHover(this.setSelectOnHover);
240241

241-
var previousSelectedItem = this.popup.data[this.popup.getRow()];
242+
var oldRow = this.popup.getRow();
243+
var previousSelectedItem = this.popup.data[oldRow];
242244

243245
this.popup.setData(this.completions.filtered, this.completions.filterText);
244246
if (this.editor.textInput.setAriaOptions) {
@@ -250,12 +252,17 @@ class Autocomplete {
250252

251253
editor.keyBinding.addKeyboardHandler(this.keyboardHandler);
252254

253-
var newRow = this.popup.data.indexOf(previousSelectedItem);
254-
255-
if (newRow && this.stickySelection)
256-
this.popup.setRow(this.autoSelect ? newRow : -1);
257-
else
258-
this.popup.setRow(this.autoSelect ? 0 : -1);
255+
var newRow;
256+
if (this.stickySelection)
257+
newRow = this.popup.data.indexOf(previousSelectedItem);
258+
if (!newRow || newRow === -1)
259+
newRow = 0;
260+
261+
this.popup.setRow(this.autoSelect ? newRow : -1);
262+
263+
// If we stay on the same row, but the content is different, we want to update the popup.
264+
if (newRow === oldRow && previousSelectedItem !== this.completions.filtered[newRow])
265+
this.$onPopupChange();
259266

260267
if (!keepPopupPosition) {
261268
this.popup.setTheme(editor.getTheme());
@@ -458,6 +465,7 @@ class Autocomplete {
458465
}];
459466
this.completions = new FilteredList(completionsForEmpty);
460467
this.openPopup(this.editor, prefix, keepPopupPosition);
468+
this.popup.renderer.setStyle("ace_loading", false);
461469
return;
462470
}
463471
return this.detach();
@@ -471,7 +479,12 @@ class Autocomplete {
471479
if (this.autoInsert && !this.autoShown && filtered.length == 1)
472480
return this.insertMatch(filtered[0]);
473481
}
474-
this.completions = completions;
482+
this.completions = finished ?
483+
completions :
484+
new FilteredList(
485+
Autocomplete.completionsForLoading.concat(filtered), completions.filterText
486+
);
487+
475488
this.openPopup(this.editor, prefix, keepPopupPosition);
476489

477490
this.popup.renderer.setStyle("ace_loading", !finished);

‎src/autocomplete_test.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,9 @@ module.exports = {
714714
completer.stickySelectionDelay = 100;
715715
user.type("Ctrl-Space");
716716
assert.equal(completer.popup.isOpen, true);
717-
assert.equal(completer.popup.data.length, 2);
718-
assert.equal(completer.popup.getRow(), 0);
717+
assert.equal(completer.popup.data.length, 3);
718+
sendKey("Down");
719+
assert.equal(completer.popup.getRow(), 1);
719720

720721
setTimeout(() => {
721722
completer.popup.renderer.$loop._flush();
@@ -770,7 +771,7 @@ module.exports = {
770771
completer.stickySelectionDelay = -1;
771772
user.type("Ctrl-Space");
772773
assert.equal(completer.popup.isOpen, true);
773-
assert.equal(completer.popup.data.length, 2);
774+
assert.equal(completer.popup.data.length, 3);
774775
assert.equal(completer.popup.getRow(), 0);
775776

776777
setTimeout(() => {
@@ -1005,7 +1006,7 @@ module.exports = {
10051006

10061007
editor.completers = [fastCompleter, slowCompleter];
10071008
user.type("Ctrl-Space");
1008-
assert.equal(completer.popup.data.length, 3);
1009+
assert.equal(completer.popup.data.length, 4);
10091010
assert.ok(isLoading());
10101011
setTimeout(() => {
10111012
completer.popup.renderer.$loop._flush();

0 commit comments

Comments
 (0)
Please sign in to comment.