2019-05-09 16:01:47 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
2019-05-22 17:16:32 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-05-09 16:01:47 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default class AutocompleteWrapperModel {
|
2019-06-14 13:16:34 +03:00
|
|
|
constructor(updateCallback, getAutocompleterComponent, updateQuery, partCreator) {
|
2019-05-09 16:01:47 +03:00
|
|
|
this._updateCallback = updateCallback;
|
|
|
|
this._getAutocompleterComponent = getAutocompleterComponent;
|
|
|
|
this._updateQuery = updateQuery;
|
2019-06-14 13:16:34 +03:00
|
|
|
this._partCreator = partCreator;
|
2019-05-09 16:01:47 +03:00
|
|
|
this._query = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
onEscape(e) {
|
|
|
|
this._getAutocompleterComponent().onEscape(e);
|
2019-05-09 16:59:48 +03:00
|
|
|
this._updateCallback({
|
2019-08-29 18:43:18 +03:00
|
|
|
replaceParts: [this._partCreator.plain(this._queryPart.text)],
|
2019-05-09 16:59:48 +03:00
|
|
|
close: true,
|
|
|
|
});
|
2019-05-09 16:01:47 +03:00
|
|
|
}
|
|
|
|
|
2019-08-28 16:53:16 +03:00
|
|
|
close() {
|
|
|
|
this._updateCallback({close: true});
|
|
|
|
}
|
|
|
|
|
2019-08-21 17:22:04 +03:00
|
|
|
hasSelection() {
|
|
|
|
return this._getAutocompleterComponent().hasSelection();
|
|
|
|
}
|
|
|
|
|
2019-09-02 12:25:29 +03:00
|
|
|
hasCompletions() {
|
|
|
|
const ac = this._getAutocompleterComponent();
|
|
|
|
return ac && ac.countCompletions() > 0;
|
|
|
|
}
|
|
|
|
|
2019-05-09 16:01:47 +03:00
|
|
|
onEnter() {
|
2019-05-09 17:07:00 +03:00
|
|
|
this._updateCallback({close: true});
|
2019-05-09 16:01:47 +03:00
|
|
|
}
|
|
|
|
|
2019-05-24 13:38:01 +03:00
|
|
|
async onTab(e) {
|
|
|
|
const acComponent = this._getAutocompleterComponent();
|
|
|
|
|
2019-06-04 14:21:39 +03:00
|
|
|
if (acComponent.countCompletions() === 0) {
|
2019-05-24 13:38:01 +03:00
|
|
|
// Force completions to show for the text currently entered
|
|
|
|
await acComponent.forceComplete();
|
|
|
|
// Select the first item by moving "down"
|
2019-06-04 14:21:39 +03:00
|
|
|
await acComponent.moveSelection(+1);
|
2019-05-24 13:38:01 +03:00
|
|
|
} else {
|
2019-06-04 14:21:39 +03:00
|
|
|
await acComponent.moveSelection(e.shiftKey ? -1 : +1);
|
2019-05-24 13:38:01 +03:00
|
|
|
}
|
2019-05-09 16:01:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
onUpArrow() {
|
2019-06-04 14:57:15 +03:00
|
|
|
this._getAutocompleterComponent().moveSelection(-1);
|
2019-05-09 16:01:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
onDownArrow() {
|
2019-06-04 14:57:15 +03:00
|
|
|
this._getAutocompleterComponent().moveSelection(+1);
|
2019-05-09 16:01:47 +03:00
|
|
|
}
|
|
|
|
|
2019-08-29 18:47:14 +03:00
|
|
|
onPartUpdate(part, pos) {
|
2019-05-09 16:01:47 +03:00
|
|
|
// cache the typed value and caret here
|
|
|
|
// so we can restore it in onComponentSelectionChange when the value is undefined (meaning it should be the typed text)
|
|
|
|
this._queryPart = part;
|
2019-08-29 18:47:14 +03:00
|
|
|
this._partIndex = pos.index;
|
2019-08-28 16:53:16 +03:00
|
|
|
return this._updateQuery(part.text);
|
2019-05-09 16:01:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
onComponentSelectionChange(completion) {
|
|
|
|
if (!completion) {
|
|
|
|
this._updateCallback({
|
2019-08-29 18:43:18 +03:00
|
|
|
replaceParts: [this._queryPart],
|
2019-05-09 16:01:47 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._updateCallback({
|
2019-08-29 18:43:18 +03:00
|
|
|
replaceParts: this._partForCompletion(completion),
|
2019-05-09 16:01:47 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onComponentConfirm(completion) {
|
|
|
|
this._updateCallback({
|
2019-08-29 18:43:18 +03:00
|
|
|
replaceParts: this._partForCompletion(completion),
|
2019-05-09 16:01:47 +03:00
|
|
|
close: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_partForCompletion(completion) {
|
2019-06-14 13:16:34 +03:00
|
|
|
const {completionId} = completion;
|
|
|
|
const text = completion.completion;
|
2019-09-23 15:39:19 +03:00
|
|
|
switch (completion.type) {
|
|
|
|
case "room":
|
2020-02-21 14:50:18 +03:00
|
|
|
return [this._partCreator.roomPill(text, completionId), this._partCreator.plain(completion.suffix)];
|
2019-09-23 15:39:19 +03:00
|
|
|
case "at-room":
|
2019-09-23 15:40:41 +03:00
|
|
|
return [this._partCreator.atRoomPill(completionId), this._partCreator.plain(completion.suffix)];
|
2019-09-23 15:39:19 +03:00
|
|
|
case "user":
|
|
|
|
// not using suffix here, because we also need to calculate
|
|
|
|
// the suffix when clicking a display name to insert a mention,
|
|
|
|
// which happens in createMentionParts
|
|
|
|
return this._partCreator.createMentionParts(this._partIndex, text, completionId);
|
|
|
|
case "command":
|
|
|
|
// command needs special handling for auto complete, but also renders as plain texts
|
|
|
|
return [this._partCreator.command(text)];
|
2019-05-09 16:01:47 +03:00
|
|
|
default:
|
2019-09-23 15:39:19 +03:00
|
|
|
// used for emoji and other plain text completion replacement
|
2019-08-29 18:43:18 +03:00
|
|
|
return [this._partCreator.plain(text)];
|
2019-05-09 16:01:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|