2019-07-25 13:34:21 +02:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import EditorModel from "../../src/editor/model";
|
2021-06-29 14:11:58 +02:00
|
|
|
import { createPartCreator, createRenderer } from "./mock";
|
2022-04-06 13:40:13 +02:00
|
|
|
import DocumentOffset from "../../src/editor/offset";
|
2019-07-25 13:34:21 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("editor/model", function () {
|
|
|
|
describe("plain text manipulation", function () {
|
|
|
|
it("insert text into empty document", function () {
|
2019-07-25 13:34:21 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const model = new EditorModel([], createPartCreator(), renderer);
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("hello", "insertText", new DocumentOffset(5, true));
|
2019-07-25 13:34:21 +02:00
|
|
|
expect(renderer.count).toBe(1);
|
|
|
|
expect(renderer.caret.index).toBe(0);
|
|
|
|
expect(renderer.caret.offset).toBe(5);
|
|
|
|
expect(model.parts.length).toBe(1);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("hello");
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
it("append text to existing document", function () {
|
2019-07-25 13:34:21 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("hello")], pc, renderer);
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("hello world", "insertText", new DocumentOffset(11, true));
|
2019-07-25 13:34:21 +02:00
|
|
|
expect(renderer.count).toBe(1);
|
|
|
|
expect(renderer.caret.index).toBe(0);
|
|
|
|
expect(renderer.caret.offset).toBe(11);
|
|
|
|
expect(model.parts.length).toBe(1);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("hello world");
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
it("prepend text to existing document", function () {
|
2019-07-25 13:34:21 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("world")], pc, renderer);
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("hello world", "insertText", new DocumentOffset(6, false));
|
2019-07-25 13:34:21 +02:00
|
|
|
expect(renderer.count).toBe(1);
|
|
|
|
expect(renderer.caret.index).toBe(0);
|
|
|
|
expect(renderer.caret.offset).toBe(6);
|
|
|
|
expect(model.parts.length).toBe(1);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("hello world");
|
|
|
|
});
|
2019-07-25 16:06:43 +02:00
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("handling line breaks", function () {
|
|
|
|
it("insert new line into existing document", function () {
|
2019-07-25 13:34:21 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("hello")], pc, renderer);
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("hello\n", "insertText", new DocumentOffset(6, true));
|
2019-07-25 13:34:21 +02:00
|
|
|
expect(renderer.count).toBe(1);
|
|
|
|
expect(renderer.caret.index).toBe(1);
|
|
|
|
expect(renderer.caret.offset).toBe(1);
|
|
|
|
expect(model.parts.length).toBe(2);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("hello");
|
|
|
|
expect(model.parts[1].type).toBe("newline");
|
|
|
|
expect(model.parts[1].text).toBe("\n");
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
it("insert multiple new lines into existing document", function () {
|
2019-07-25 16:06:43 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("hello")], pc, renderer);
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("hello\n\n\nworld!", "insertText", new DocumentOffset(14, true));
|
2019-07-25 16:06:43 +02:00
|
|
|
expect(renderer.count).toBe(1);
|
|
|
|
expect(renderer.caret.index).toBe(4);
|
|
|
|
expect(renderer.caret.offset).toBe(6);
|
|
|
|
expect(model.parts.length).toBe(5);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("hello");
|
|
|
|
expect(model.parts[1].type).toBe("newline");
|
|
|
|
expect(model.parts[1].text).toBe("\n");
|
|
|
|
expect(model.parts[2].type).toBe("newline");
|
|
|
|
expect(model.parts[2].text).toBe("\n");
|
|
|
|
expect(model.parts[3].type).toBe("newline");
|
|
|
|
expect(model.parts[3].text).toBe("\n");
|
|
|
|
expect(model.parts[4].type).toBe("plain");
|
|
|
|
expect(model.parts[4].text).toBe("world!");
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
it("type in empty line", function () {
|
2019-07-25 16:06:43 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel(
|
|
|
|
[pc.plain("hello"), pc.newline(), pc.newline(), pc.plain("world")],
|
|
|
|
pc,
|
|
|
|
renderer,
|
|
|
|
);
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("hello\nwarm\nworld", "insertText", new DocumentOffset(10, true));
|
2019-07-25 16:06:43 +02:00
|
|
|
expect(renderer.count).toBe(1);
|
|
|
|
expect(renderer.caret.index).toBe(2);
|
|
|
|
expect(renderer.caret.offset).toBe(4);
|
|
|
|
expect(model.parts.length).toBe(5);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("hello");
|
|
|
|
expect(model.parts[1].type).toBe("newline");
|
|
|
|
expect(model.parts[1].text).toBe("\n");
|
|
|
|
expect(model.parts[2].type).toBe("plain");
|
|
|
|
expect(model.parts[2].text).toBe("warm");
|
|
|
|
expect(model.parts[3].type).toBe("newline");
|
|
|
|
expect(model.parts[3].text).toBe("\n");
|
|
|
|
expect(model.parts[4].type).toBe("plain");
|
|
|
|
expect(model.parts[4].text).toBe("world");
|
|
|
|
});
|
2019-07-25 13:34:21 +02:00
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("non-editable part manipulation", function () {
|
|
|
|
it("typing at start of non-editable part prepends", function () {
|
2019-07-25 14:48:53 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.plain("try "), pc.roomPill("#someroom")], pc, renderer);
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("try foo#someroom", "insertText", new DocumentOffset(7, false));
|
2019-07-25 14:48:53 +02:00
|
|
|
expect(renderer.caret.index).toBe(0);
|
|
|
|
expect(renderer.caret.offset).toBe(7);
|
|
|
|
expect(model.parts.length).toBe(2);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("try foo");
|
|
|
|
expect(model.parts[1].type).toBe("room-pill");
|
|
|
|
expect(model.parts[1].text).toBe("#someroom");
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
it("typing in middle of non-editable part appends", function () {
|
2019-07-25 15:06:32 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.plain("try "), pc.roomPill("#someroom"), pc.plain("?")], pc, renderer);
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("try #some perhapsroom?", "insertText", new DocumentOffset(17, false));
|
2019-07-25 15:06:32 +02:00
|
|
|
expect(renderer.caret.index).toBe(2);
|
|
|
|
expect(renderer.caret.offset).toBe(8);
|
|
|
|
expect(model.parts.length).toBe(3);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("try ");
|
|
|
|
expect(model.parts[1].type).toBe("room-pill");
|
|
|
|
expect(model.parts[1].text).toBe("#someroom");
|
|
|
|
expect(model.parts[2].type).toBe("plain");
|
|
|
|
expect(model.parts[2].text).toBe(" perhaps?");
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
it("remove non-editable part with backspace", function () {
|
2019-07-25 14:48:53 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.roomPill("#someroom")], pc, renderer);
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("#someroo", "deleteContentBackward", new DocumentOffset(8, true));
|
2019-07-25 14:48:53 +02:00
|
|
|
expect(renderer.count).toBe(1);
|
|
|
|
expect(renderer.caret.index).toBe(-1);
|
|
|
|
expect(renderer.caret.offset).toBe(0);
|
|
|
|
expect(model.parts.length).toBe(0);
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
it("remove non-editable part with delete", function () {
|
2019-07-25 14:48:53 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.roomPill("#someroom")], pc, renderer);
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("someroom", "deleteContentForward", new DocumentOffset(0, false));
|
2019-07-25 14:48:53 +02:00
|
|
|
expect(renderer.count).toBe(1);
|
|
|
|
expect(renderer.caret.index).toBe(-1);
|
|
|
|
expect(renderer.caret.offset).toBe(0);
|
|
|
|
expect(model.parts.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("auto-complete", function () {
|
|
|
|
it("insert user pill", function () {
|
2019-07-25 13:34:21 +02:00
|
|
|
const renderer = createRenderer();
|
2021-06-29 14:11:58 +02:00
|
|
|
const pc = createPartCreator([{ resourceId: "@alice", label: "Alice" }]);
|
2019-07-25 13:34:21 +02:00
|
|
|
const model = new EditorModel([pc.plain("hello ")], pc, renderer);
|
|
|
|
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("hello @a", "insertText", new DocumentOffset(8, true));
|
2019-07-25 13:34:21 +02:00
|
|
|
|
|
|
|
expect(renderer.count).toBe(1);
|
|
|
|
expect(renderer.caret.index).toBe(1);
|
|
|
|
expect(renderer.caret.offset).toBe(2);
|
|
|
|
expect(model.parts.length).toBe(2);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("hello ");
|
|
|
|
expect(model.parts[1].type).toBe("pill-candidate");
|
|
|
|
expect(model.parts[1].text).toBe("@a");
|
|
|
|
|
2022-04-06 13:40:13 +02:00
|
|
|
// this is a hacky mock function
|
|
|
|
// @ts-ignore
|
2019-07-25 14:49:19 +02:00
|
|
|
model.autoComplete.tryComplete(); // see MockAutoComplete
|
2019-07-25 13:34:21 +02:00
|
|
|
|
|
|
|
expect(renderer.count).toBe(2);
|
|
|
|
expect(renderer.caret.index).toBe(1);
|
|
|
|
expect(renderer.caret.offset).toBe(5);
|
|
|
|
expect(model.parts.length).toBe(2);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("hello ");
|
|
|
|
expect(model.parts[1].type).toBe("user-pill");
|
|
|
|
expect(model.parts[1].text).toBe("Alice");
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("insert room pill", function () {
|
2019-07-25 13:34:21 +02:00
|
|
|
const renderer = createRenderer();
|
2021-06-29 14:11:58 +02:00
|
|
|
const pc = createPartCreator([{ resourceId: "#riot-dev" }]);
|
2019-07-25 13:34:21 +02:00
|
|
|
const model = new EditorModel([pc.plain("hello ")], pc, renderer);
|
|
|
|
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("hello #r", "insertText", new DocumentOffset(8, true));
|
2019-07-25 13:34:21 +02:00
|
|
|
|
|
|
|
expect(renderer.count).toBe(1);
|
|
|
|
expect(renderer.caret.index).toBe(1);
|
|
|
|
expect(renderer.caret.offset).toBe(2);
|
|
|
|
expect(model.parts.length).toBe(2);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("hello ");
|
|
|
|
expect(model.parts[1].type).toBe("pill-candidate");
|
|
|
|
expect(model.parts[1].text).toBe("#r");
|
|
|
|
|
2022-04-06 13:40:13 +02:00
|
|
|
// this is a hacky mock function
|
|
|
|
// @ts-ignore
|
2019-07-25 14:49:19 +02:00
|
|
|
model.autoComplete.tryComplete(); // see MockAutoComplete
|
2019-07-25 13:34:21 +02:00
|
|
|
|
|
|
|
expect(renderer.count).toBe(2);
|
|
|
|
expect(renderer.caret.index).toBe(1);
|
|
|
|
expect(renderer.caret.offset).toBe(9);
|
|
|
|
expect(model.parts.length).toBe(2);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("hello ");
|
|
|
|
expect(model.parts[1].type).toBe("room-pill");
|
|
|
|
expect(model.parts[1].text).toBe("#riot-dev");
|
|
|
|
});
|
2019-07-25 14:49:19 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("type after inserting pill", function () {
|
2019-07-25 14:49:19 +02:00
|
|
|
const renderer = createRenderer();
|
2021-06-29 14:11:58 +02:00
|
|
|
const pc = createPartCreator([{ resourceId: "#riot-dev" }]);
|
2019-07-25 14:49:19 +02:00
|
|
|
const model = new EditorModel([pc.plain("hello ")], pc, renderer);
|
|
|
|
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("hello #r", "insertText", new DocumentOffset(8, true));
|
|
|
|
// this is a hacky mock function
|
|
|
|
// @ts-ignore
|
2019-07-25 14:49:19 +02:00
|
|
|
model.autoComplete.tryComplete(); // see MockAutoComplete
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("hello #riot-dev!!", "insertText", new DocumentOffset(17, true));
|
2019-07-25 14:49:19 +02:00
|
|
|
|
|
|
|
expect(renderer.count).toBe(3);
|
|
|
|
expect(renderer.caret.index).toBe(2);
|
|
|
|
expect(renderer.caret.offset).toBe(2);
|
|
|
|
expect(model.parts.length).toBe(3);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("hello ");
|
|
|
|
expect(model.parts[1].type).toBe("room-pill");
|
|
|
|
expect(model.parts[1].text).toBe("#riot-dev");
|
|
|
|
expect(model.parts[2].type).toBe("plain");
|
|
|
|
expect(model.parts[2].text).toBe("!!");
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("pasting text does not trigger auto-complete", function () {
|
2019-07-25 14:49:19 +02:00
|
|
|
const renderer = createRenderer();
|
2021-06-29 14:11:58 +02:00
|
|
|
const pc = createPartCreator([{ resourceId: "#define-room" }]);
|
2019-07-25 14:49:19 +02:00
|
|
|
const model = new EditorModel([pc.plain("try ")], pc, renderer);
|
|
|
|
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("try #define", "insertFromPaste", new DocumentOffset(11, true));
|
2019-07-25 14:49:19 +02:00
|
|
|
|
|
|
|
expect(model.autoComplete).toBeFalsy();
|
|
|
|
expect(renderer.caret.index).toBe(0);
|
|
|
|
expect(renderer.caret.offset).toBe(11);
|
|
|
|
expect(model.parts.length).toBe(1);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("try #define");
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("dropping text does not trigger auto-complete", function () {
|
2019-07-25 14:49:19 +02:00
|
|
|
const renderer = createRenderer();
|
2021-06-29 14:11:58 +02:00
|
|
|
const pc = createPartCreator([{ resourceId: "#define-room" }]);
|
2019-07-25 14:49:19 +02:00
|
|
|
const model = new EditorModel([pc.plain("try ")], pc, renderer);
|
|
|
|
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("try #define", "insertFromDrop", new DocumentOffset(11, true));
|
2019-07-25 14:49:19 +02:00
|
|
|
|
|
|
|
expect(model.autoComplete).toBeFalsy();
|
|
|
|
expect(renderer.caret.index).toBe(0);
|
|
|
|
expect(renderer.caret.offset).toBe(11);
|
|
|
|
expect(model.parts.length).toBe(1);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("try #define");
|
|
|
|
});
|
2020-07-21 18:40:34 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("insert room pill without splitting at the colon", () => {
|
2020-07-21 18:40:34 +02:00
|
|
|
const renderer = createRenderer();
|
2021-06-29 14:11:58 +02:00
|
|
|
const pc = createPartCreator([{ resourceId: "#room:server" }]);
|
2020-07-21 18:40:34 +02:00
|
|
|
const model = new EditorModel([], pc, renderer);
|
|
|
|
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("#roo", "insertText", new DocumentOffset(4, true));
|
2020-07-21 18:40:34 +02:00
|
|
|
|
|
|
|
expect(renderer.count).toBe(1);
|
|
|
|
expect(model.parts.length).toBe(1);
|
|
|
|
expect(model.parts[0].type).toBe("pill-candidate");
|
|
|
|
expect(model.parts[0].text).toBe("#roo");
|
|
|
|
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("#room:s", "insertText", new DocumentOffset(7, true));
|
2020-07-21 18:40:34 +02:00
|
|
|
|
|
|
|
expect(renderer.count).toBe(2);
|
|
|
|
expect(model.parts.length).toBe(1);
|
|
|
|
expect(model.parts[0].type).toBe("pill-candidate");
|
|
|
|
expect(model.parts[0].text).toBe("#room:s");
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("allow typing e-mail addresses without splitting at the @", () => {
|
2020-07-21 18:40:34 +02:00
|
|
|
const renderer = createRenderer();
|
2021-06-29 14:11:58 +02:00
|
|
|
const pc = createPartCreator([{ resourceId: "@alice", label: "Alice" }]);
|
2020-07-21 18:40:34 +02:00
|
|
|
const model = new EditorModel([], pc, renderer);
|
|
|
|
|
2022-04-06 13:40:13 +02:00
|
|
|
model.update("foo@a", "insertText", new DocumentOffset(5, true));
|
2020-07-21 18:40:34 +02:00
|
|
|
|
|
|
|
expect(renderer.count).toBe(1);
|
|
|
|
expect(model.parts.length).toBe(1);
|
|
|
|
expect(model.parts[0].type).toBe("plain");
|
|
|
|
expect(model.parts[0].text).toBe("foo@a");
|
|
|
|
});
|
2019-07-25 13:34:21 +02:00
|
|
|
});
|
|
|
|
});
|