2019-07-25 18:27:29 +03:00
|
|
|
/*
|
2022-04-06 14:40:13 +03:00
|
|
|
Copyright 2019, 2022 The Matrix.org Foundation C.I.C.
|
2019-07-25 18:27:29 +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.
|
|
|
|
*/
|
|
|
|
|
2022-04-06 14:40:13 +03:00
|
|
|
import { Room, MatrixClient } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
|
|
|
import AutocompleteWrapperModel from "../../src/editor/autocomplete";
|
2021-06-29 15:11:58 +03:00
|
|
|
import { PartCreator } from "../../src/editor/parts";
|
2022-05-09 15:39:32 +03:00
|
|
|
import DocumentPosition from "../../src/editor/position";
|
2019-07-25 18:27:29 +03:00
|
|
|
|
|
|
|
class MockAutoComplete {
|
2022-04-06 14:40:13 +03:00
|
|
|
public _updateCallback;
|
|
|
|
public _partCreator;
|
|
|
|
public _completions;
|
|
|
|
public _part;
|
|
|
|
|
2019-07-25 18:27:29 +03:00
|
|
|
constructor(updateCallback, partCreator, completions) {
|
|
|
|
this._updateCallback = updateCallback;
|
|
|
|
this._partCreator = partCreator;
|
|
|
|
this._completions = completions;
|
|
|
|
this._part = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2021-06-29 15:11:58 +03:00
|
|
|
this._updateCallback({ close: true });
|
2019-07-25 18:27:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tryComplete(close = true) {
|
|
|
|
const matches = this._completions.filter(o => {
|
|
|
|
return o.resourceId.startsWith(this._part.text);
|
|
|
|
});
|
|
|
|
if (matches.length === 1 && this._part.text.length > 1) {
|
|
|
|
const match = matches[0];
|
|
|
|
let pill;
|
|
|
|
if (match.resourceId[0] === "@") {
|
|
|
|
pill = this._partCreator.userPill(match.label, match.resourceId);
|
|
|
|
} else {
|
|
|
|
pill = this._partCreator.roomPill(match.resourceId);
|
|
|
|
}
|
2021-06-29 15:11:58 +03:00
|
|
|
this._updateCallback({ replaceParts: [pill], close });
|
2019-07-25 18:27:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// called by EditorModel when typing into pill-candidate part
|
2019-08-29 19:00:38 +03:00
|
|
|
onPartUpdate(part, pos) {
|
2019-07-25 18:27:29 +03:00
|
|
|
this._part = part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MockClient & MockRoom are only used for avatars in room and user pills,
|
|
|
|
// which is not tested
|
|
|
|
class MockRoom {
|
|
|
|
getMember() { return null; }
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createPartCreator(completions = []) {
|
|
|
|
const autoCompleteCreator = (partCreator) => {
|
2022-04-06 14:40:13 +03:00
|
|
|
return (updateCallback) =>
|
|
|
|
new MockAutoComplete(updateCallback, partCreator, completions) as unknown as AutocompleteWrapperModel;
|
2019-07-25 18:27:29 +03:00
|
|
|
};
|
2022-04-06 14:40:13 +03:00
|
|
|
const room = new MockRoom() as unknown as Room;
|
|
|
|
const client = {
|
|
|
|
getRooms: jest.fn().mockReturnValue([]),
|
|
|
|
getRoom: jest.fn().mockReturnValue(null),
|
|
|
|
} as unknown as MatrixClient;
|
|
|
|
return new PartCreator(room, client, autoCompleteCreator);
|
2019-07-25 18:27:29 +03:00
|
|
|
}
|
2020-01-22 14:56:27 +03:00
|
|
|
|
|
|
|
export function createRenderer() {
|
2022-05-09 15:39:32 +03:00
|
|
|
const render = (c: DocumentPosition) => {
|
2020-01-22 14:56:27 +03:00
|
|
|
render.caret = c;
|
|
|
|
render.count += 1;
|
|
|
|
};
|
|
|
|
render.count = 0;
|
2022-05-09 15:39:32 +03:00
|
|
|
render.caret = null as DocumentPosition;
|
2020-01-22 14:56:27 +03:00
|
|
|
return render;
|
|
|
|
}
|