2019-07-25 17:27:29 +02:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2019-2022 The Matrix.org Foundation C.I.C.
|
2019-07-25 17:27:29 +02:00
|
|
|
|
2024-09-09 15:57:16 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-07-25 17:27:29 +02:00
|
|
|
*/
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
import { Room, MatrixClient, RoomMember } from "matrix-js-sdk/src/matrix";
|
2022-04-06 13:40:13 +02:00
|
|
|
|
2022-12-29 21:52:51 +01:00
|
|
|
import AutocompleteWrapperModel, { UpdateCallback } from "../../src/editor/autocomplete";
|
|
|
|
import { Caret } from "../../src/editor/caret";
|
|
|
|
import { PillPart, Part, PartCreator } from "../../src/editor/parts";
|
2022-05-09 14:39:32 +02:00
|
|
|
import DocumentPosition from "../../src/editor/position";
|
2019-07-25 17:27:29 +02:00
|
|
|
|
2023-04-28 15:31:02 +02:00
|
|
|
export class MockAutoComplete {
|
2022-04-06 13:40:13 +02:00
|
|
|
public _updateCallback;
|
|
|
|
public _partCreator;
|
|
|
|
public _completions;
|
2022-12-29 21:52:51 +01:00
|
|
|
public _part: Part | null;
|
2022-04-06 13:40:13 +02:00
|
|
|
|
2022-12-29 21:52:51 +01:00
|
|
|
constructor(updateCallback: UpdateCallback, partCreator: PartCreator, completions: PillPart[]) {
|
2019-07-25 17:27:29 +02:00
|
|
|
this._updateCallback = updateCallback;
|
|
|
|
this._partCreator = partCreator;
|
|
|
|
this._completions = completions;
|
|
|
|
this._part = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2021-06-29 14:11:58 +02:00
|
|
|
this._updateCallback({ close: true });
|
2019-07-25 17:27:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tryComplete(close = true) {
|
2022-12-12 12:24:14 +01:00
|
|
|
const matches = this._completions.filter((o) => {
|
2022-12-29 21:52:51 +01:00
|
|
|
return this._part && o.resourceId.startsWith(this._part.text);
|
2019-07-25 17:27:29 +02:00
|
|
|
});
|
2022-12-29 21:52:51 +01:00
|
|
|
if (matches.length === 1 && this._part && this._part.text.length > 1) {
|
2019-07-25 17:27:29 +02:00
|
|
|
const match = matches[0];
|
2023-04-28 15:31:02 +02:00
|
|
|
let pill: PillPart;
|
2019-07-25 17:27:29 +02:00
|
|
|
if (match.resourceId[0] === "@") {
|
2022-12-29 21:52:51 +01:00
|
|
|
pill = this._partCreator.userPill(match.text, match.resourceId);
|
2019-07-25 17:27:29 +02:00
|
|
|
} else {
|
|
|
|
pill = this._partCreator.roomPill(match.resourceId);
|
|
|
|
}
|
2021-06-29 14:11:58 +02:00
|
|
|
this._updateCallback({ replaceParts: [pill], close });
|
2019-07-25 17:27:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// called by EditorModel when typing into pill-candidate part
|
2022-12-29 21:52:51 +01:00
|
|
|
onPartUpdate(part: Part, pos: DocumentPosition) {
|
2019-07-25 17:27:29 +02:00
|
|
|
this._part = part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MockClient & MockRoom are only used for avatars in room and user pills,
|
|
|
|
// which is not tested
|
|
|
|
class MockRoom {
|
2023-02-13 12:39:16 +01:00
|
|
|
getMember(): RoomMember | null {
|
2022-12-12 12:24:14 +01:00
|
|
|
return null;
|
|
|
|
}
|
2019-07-25 17:27:29 +02:00
|
|
|
}
|
|
|
|
|
2022-12-29 21:52:51 +01:00
|
|
|
export function createPartCreator(completions: PillPart[] = []) {
|
|
|
|
const autoCompleteCreator = (partCreator: PartCreator) => {
|
|
|
|
return (updateCallback: UpdateCallback) =>
|
2022-04-06 13:40:13 +02:00
|
|
|
new MockAutoComplete(updateCallback, partCreator, completions) as unknown as AutocompleteWrapperModel;
|
2019-07-25 17:27:29 +02:00
|
|
|
};
|
2022-04-06 13:40:13 +02: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 17:27:29 +02:00
|
|
|
}
|
2020-01-22 12:56:27 +01:00
|
|
|
|
|
|
|
export function createRenderer() {
|
2023-05-16 15:25:43 +02:00
|
|
|
const render = (c?: Caret) => {
|
2020-01-22 12:56:27 +01:00
|
|
|
render.caret = c;
|
|
|
|
render.count += 1;
|
|
|
|
};
|
|
|
|
render.count = 0;
|
2023-05-16 15:25:43 +02:00
|
|
|
render.caret = null as unknown as Caret | undefined;
|
2020-01-22 12:56:27 +01:00
|
|
|
return render;
|
|
|
|
}
|
2022-12-29 21:52:51 +01:00
|
|
|
|
|
|
|
// in many tests we need to narrow the caret type
|
|
|
|
export function isDocumentPosition(caret: Caret): caret is DocumentPosition {
|
|
|
|
return caret instanceof DocumentPosition;
|
|
|
|
}
|