2020-01-21 16:32:32 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 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";
|
2022-12-29 21:52:51 +01:00
|
|
|
import { createPartCreator, createRenderer, isDocumentPosition } from "./mock";
|
2022-03-16 10:46:07 +01:00
|
|
|
import {
|
|
|
|
formatRange,
|
2022-04-19 11:20:56 +02:00
|
|
|
formatRangeAsCode,
|
2022-05-09 14:39:32 +02:00
|
|
|
formatRangeAsLink,
|
|
|
|
selectRangeOfWordAtCaret,
|
|
|
|
toggleInlineFormat,
|
2022-03-16 10:46:07 +01:00
|
|
|
} from "../../src/editor/operations";
|
|
|
|
import { Formatting } from "../../src/components/views/rooms/MessageComposerFormatBar";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { longestBacktickSequence } from "../../src/editor/deserialize";
|
2020-01-21 16:32:32 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const SERIALIZED_NEWLINE = { text: "\n", type: "newline" };
|
2020-01-22 14:37:27 +01:00
|
|
|
|
2022-05-09 14:39:32 +02:00
|
|
|
describe("editor/operations: formatting operations", () => {
|
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
|
|
|
|
|
|
|
describe("formatRange", () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
it.each([[Formatting.Bold, "hello **world**!"]])(
|
|
|
|
"should correctly wrap format %s",
|
|
|
|
(formatting: Formatting, expected: string) => {
|
|
|
|
const model = new EditorModel([pc.plain("hello world!")], pc, renderer);
|
2022-05-09 14:39:32 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const range = model.startRange(model.positionForOffset(6, false), model.positionForOffset(11, false)); // around "world"
|
2022-05-09 14:39:32 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(range.parts[0].text).toBe("world");
|
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hello world!", type: "plain" }]);
|
|
|
|
formatRange(range, formatting);
|
|
|
|
expect(model.serializeParts()).toEqual([{ text: expected, type: "plain" }]);
|
|
|
|
},
|
|
|
|
);
|
2022-05-09 14:39:32 +02:00
|
|
|
|
|
|
|
it("should apply to word range is within if length 0", () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.plain("hello world!")], pc, renderer);
|
2022-05-09 14:39:32 +02:00
|
|
|
|
|
|
|
const range = model.startRange(model.positionForOffset(6, false));
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hello world!", type: "plain" }]);
|
2022-05-09 14:39:32 +02:00
|
|
|
formatRange(range, Formatting.Bold);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hello **world!**", type: "plain" }]);
|
2022-05-09 14:39:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should do nothing for a range with length 0 at initialisation", () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.plain("hello world!")], pc, renderer);
|
2022-05-09 14:39:32 +02:00
|
|
|
|
|
|
|
const range = model.startRange(model.positionForOffset(6, false));
|
|
|
|
range.setWasEmpty(false);
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hello world!", type: "plain" }]);
|
2022-05-09 14:39:32 +02:00
|
|
|
formatRange(range, Formatting.Bold);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hello world!", type: "plain" }]);
|
2022-05-09 14:39:32 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("formatRangeAsLink", () => {
|
|
|
|
it.each([
|
|
|
|
// Caret is denoted by | in the expectation string
|
|
|
|
["testing", "[testing](|)", ""],
|
|
|
|
["testing", "[testing](foobar|)", "foobar"],
|
|
|
|
["[testing]()", "testing|", ""],
|
|
|
|
["[testing](foobar)", "testing|", ""],
|
|
|
|
])("converts %s -> %s", (input: string, expectation: string, text: string) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.plain(`foo ${input} bar`)], pc, renderer);
|
2022-05-09 14:39:32 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const range = model.startRange(
|
|
|
|
model.positionForOffset(4, false),
|
|
|
|
model.positionForOffset(4 + input.length, false),
|
|
|
|
); // around input
|
2022-05-09 14:39:32 +02:00
|
|
|
|
|
|
|
expect(range.parts[0].text).toBe(input);
|
|
|
|
formatRangeAsLink(range, text);
|
2022-12-29 21:52:51 +01:00
|
|
|
if (isDocumentPosition(renderer.caret)) {
|
|
|
|
expect(renderer.caret.offset).toBe(4 + expectation.indexOf("|"));
|
|
|
|
}
|
2022-05-09 14:39:32 +02:00
|
|
|
expect(model.parts[0].text).toBe("foo " + expectation.replace("|", "") + " bar");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("toggleInlineFormat", () => {
|
|
|
|
it("works for words", () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.plain("hello world!")], pc, renderer);
|
2020-01-21 16:32:32 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const range = model.startRange(model.positionForOffset(6, false), model.positionForOffset(11, false)); // around "world"
|
2020-01-21 16:32:32 +01:00
|
|
|
|
|
|
|
expect(range.parts[0].text).toBe("world");
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hello world!", type: "plain" }]);
|
2022-03-16 10:46:07 +01:00
|
|
|
formatRange(range, Formatting.Italics);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hello _world_!", type: "plain" }]);
|
2020-01-21 16:32:32 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("escape backticks", () => {
|
|
|
|
it("works for escaping backticks in between texts", () => {
|
2022-04-19 11:20:56 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.plain("hello ` world!")], pc, renderer);
|
2022-04-19 11:20:56 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const range = model.startRange(model.positionForOffset(0, false), model.positionForOffset(13, false)); // hello ` world
|
2022-04-19 11:20:56 +02:00
|
|
|
|
|
|
|
expect(range.parts[0].text.trim().includes("`")).toBeTruthy();
|
|
|
|
expect(longestBacktickSequence(range.parts[0].text.trim())).toBe(1);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hello ` world!", type: "plain" }]);
|
2022-04-19 11:20:56 +02:00
|
|
|
formatRangeAsCode(range);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "``hello ` world``!", type: "plain" }]);
|
2022-04-19 11:20:56 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("escapes longer backticks in between text", () => {
|
2022-04-19 11:20:56 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.plain("hello```world")], pc, renderer);
|
2022-04-19 11:20:56 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const range = model.startRange(model.positionForOffset(0, false), model.getPositionAtEnd()); // hello```world
|
2022-04-19 11:20:56 +02:00
|
|
|
|
|
|
|
expect(range.parts[0].text.includes("`")).toBeTruthy();
|
|
|
|
expect(longestBacktickSequence(range.parts[0].text)).toBe(3);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hello```world", type: "plain" }]);
|
2022-04-19 11:20:56 +02:00
|
|
|
formatRangeAsCode(range);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "````hello```world````", type: "plain" }]);
|
2022-04-19 11:20:56 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("escapes non-consecutive with varying length backticks in between text", () => {
|
2022-04-19 11:20:56 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.plain("hell```o`w`o``rld")], pc, renderer);
|
2022-04-19 11:20:56 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const range = model.startRange(model.positionForOffset(0, false), model.getPositionAtEnd()); // hell```o`w`o``rld
|
2022-04-19 11:20:56 +02:00
|
|
|
expect(range.parts[0].text.includes("`")).toBeTruthy();
|
|
|
|
expect(longestBacktickSequence(range.parts[0].text)).toBe(3);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hell```o`w`o``rld", type: "plain" }]);
|
2022-04-19 11:20:56 +02:00
|
|
|
formatRangeAsCode(range);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "````hell```o`w`o``rld````", type: "plain" }]);
|
2022-04-19 11:20:56 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("untoggles correctly if its already formatted", () => {
|
2022-04-19 11:20:56 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.plain("```hello``world```")], pc, renderer);
|
2022-04-19 11:20:56 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const range = model.startRange(model.positionForOffset(0, false), model.getPositionAtEnd()); // hello``world
|
2022-04-19 11:20:56 +02:00
|
|
|
expect(range.parts[0].text.includes("`")).toBeTruthy();
|
|
|
|
expect(longestBacktickSequence(range.parts[0].text)).toBe(3);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "```hello``world```", type: "plain" }]);
|
2022-04-19 11:20:56 +02:00
|
|
|
formatRangeAsCode(range);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hello``world", type: "plain" }]);
|
2022-04-19 11:20:56 +02:00
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
it("untoggles correctly it contains varying length of backticks between text", () => {
|
2022-04-19 11:20:56 +02:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.plain("````hell```o`w`o``rld````")], pc, renderer);
|
2022-04-19 11:20:56 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const range = model.startRange(model.positionForOffset(0, false), model.getPositionAtEnd()); // hell```o`w`o``rld
|
2022-04-19 11:20:56 +02:00
|
|
|
expect(range.parts[0].text.includes("`")).toBeTruthy();
|
|
|
|
expect(longestBacktickSequence(range.parts[0].text)).toBe(4);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "````hell```o`w`o``rld````", type: "plain" }]);
|
2022-04-19 11:20:56 +02:00
|
|
|
formatRangeAsCode(range);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hell```o`w`o``rld", type: "plain" }]);
|
2022-04-19 11:20:56 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("works for parts of words", () => {
|
2020-01-21 16:32:32 +01:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.plain("hello world!")], pc, renderer);
|
2020-01-21 16:32:32 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const range = model.startRange(model.positionForOffset(7, false), model.positionForOffset(10, false)); // around "orl"
|
2020-01-21 16:32:32 +01:00
|
|
|
|
|
|
|
expect(range.parts[0].text).toBe("orl");
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hello world!", type: "plain" }]);
|
2020-01-21 16:32:32 +01:00
|
|
|
toggleInlineFormat(range, "*");
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([{ text: "hello w*orl*d!", type: "plain" }]);
|
2020-01-21 16:32:32 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("works for around pills", () => {
|
2020-01-21 16:32:32 +01:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel(
|
|
|
|
[pc.plain("hello there "), pc.atRoomPill("@room"), pc.plain(", how are you doing?")],
|
|
|
|
pc,
|
|
|
|
renderer,
|
|
|
|
);
|
2020-01-21 16:32:32 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const range = model.startRange(model.positionForOffset(6, false), model.positionForOffset(30, false)); // around "there @room, how are you"
|
2020-01-21 16:32:32 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(range.parts.map((p) => p.text).join("")).toBe("there @room, how are you");
|
2020-01-21 16:32:32 +01:00
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "hello there ", type: "plain" },
|
|
|
|
{ text: "@room", type: "at-room-pill" },
|
|
|
|
{ text: ", how are you doing?", type: "plain" },
|
2020-01-21 16:32:32 +01:00
|
|
|
]);
|
2022-03-16 10:46:07 +01:00
|
|
|
formatRange(range, Formatting.Italics);
|
2020-01-21 16:32:32 +01:00
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "hello _there ", type: "plain" },
|
|
|
|
{ text: "@room", type: "at-room-pill" },
|
|
|
|
{ text: ", how are you_ doing?", type: "plain" },
|
2020-01-21 16:32:32 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("works for a paragraph", () => {
|
2020-01-21 16:32:32 +01:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel(
|
|
|
|
[pc.plain("hello world,"), pc.newline(), pc.plain("how are you doing?")],
|
|
|
|
pc,
|
|
|
|
renderer,
|
|
|
|
);
|
2020-01-21 16:32:32 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const range = model.startRange(model.positionForOffset(6, false), model.positionForOffset(16, false)); // around "world,\nhow"
|
2020-01-21 16:32:32 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(range.parts.map((p) => p.text).join("")).toBe("world,\nhow");
|
2020-01-21 16:32:32 +01:00
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "hello world,", type: "plain" },
|
2020-01-22 14:37:27 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "how are you doing?", type: "plain" },
|
2020-01-21 16:32:32 +01:00
|
|
|
]);
|
2022-03-16 10:46:07 +01:00
|
|
|
formatRange(range, Formatting.Bold);
|
2020-01-21 16:32:32 +01:00
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "hello **world,", type: "plain" },
|
2020-01-22 14:37:27 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "how** are you doing?", type: "plain" },
|
2020-01-21 16:32:32 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("works for a paragraph with spurious breaks around it in selected range", () => {
|
2020-01-22 14:37:27 +01:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel(
|
|
|
|
[
|
|
|
|
pc.newline(),
|
|
|
|
pc.newline(),
|
|
|
|
pc.plain("hello world,"),
|
|
|
|
pc.newline(),
|
|
|
|
pc.plain("how are you doing?"),
|
|
|
|
pc.newline(),
|
|
|
|
pc.newline(),
|
|
|
|
],
|
|
|
|
pc,
|
|
|
|
renderer,
|
|
|
|
);
|
|
|
|
|
|
|
|
const range = model.startRange(model.positionForOffset(0, false), model.getPositionAtEnd()); // select-all
|
|
|
|
|
|
|
|
expect(range.parts.map((p) => p.text).join("")).toBe("\n\nhello world,\nhow are you doing?\n\n");
|
2020-01-22 14:37:27 +01:00
|
|
|
expect(model.serializeParts()).toEqual([
|
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "hello world,", type: "plain" },
|
2020-01-22 14:37:27 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "how are you doing?", type: "plain" },
|
2020-01-22 14:37:27 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
]);
|
2022-03-16 10:46:07 +01:00
|
|
|
formatRange(range, Formatting.Bold);
|
2020-01-22 14:37:27 +01:00
|
|
|
expect(model.serializeParts()).toEqual([
|
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "**hello world,", type: "plain" },
|
2020-01-22 14:37:27 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "how are you doing?**", type: "plain" },
|
2020-01-22 14:37:27 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("works for multiple paragraph", () => {
|
2020-01-21 16:32:32 +01:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel(
|
|
|
|
[
|
|
|
|
pc.plain("hello world,"),
|
|
|
|
pc.newline(),
|
|
|
|
pc.plain("how are you doing?"),
|
|
|
|
pc.newline(),
|
|
|
|
pc.newline(),
|
|
|
|
pc.plain("new paragraph"),
|
|
|
|
],
|
|
|
|
pc,
|
|
|
|
renderer,
|
|
|
|
);
|
2020-01-21 16:32:32 +01:00
|
|
|
|
2020-01-22 14:37:27 +01:00
|
|
|
let range = model.startRange(model.positionForOffset(0, true), model.getPositionAtEnd()); // select-all
|
2020-01-21 16:32:32 +01:00
|
|
|
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "hello world,", type: "plain" },
|
2020-01-22 14:37:27 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "how are you doing?", type: "plain" },
|
2020-01-22 14:37:27 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "new paragraph", type: "plain" },
|
2020-01-21 16:32:32 +01:00
|
|
|
]);
|
|
|
|
toggleInlineFormat(range, "__");
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "__hello world,", type: "plain" },
|
2020-01-22 14:37:27 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "how are you doing?__", type: "plain" },
|
2020-01-22 14:37:27 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "__new paragraph__", type: "plain" },
|
2020-01-21 16:32:32 +01:00
|
|
|
]);
|
2020-01-22 14:37:27 +01:00
|
|
|
range = model.startRange(model.positionForOffset(0, true), model.getPositionAtEnd()); // select-all
|
2020-01-21 16:32:32 +01:00
|
|
|
toggleInlineFormat(range, "__");
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "hello world,", type: "plain" },
|
2020-01-22 14:37:27 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "how are you doing?", type: "plain" },
|
2020-01-22 14:37:27 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "new paragraph", type: "plain" },
|
2020-01-21 16:32:32 +01:00
|
|
|
]);
|
|
|
|
});
|
2022-03-16 10:46:07 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("format word at caret position at beginning of new line without previous selection", () => {
|
2022-03-16 10:46:07 +01:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel([pc.newline(), pc.plain("hello!")], pc, renderer);
|
2022-03-16 10:46:07 +01:00
|
|
|
|
|
|
|
let range = model.startRange(model.positionForOffset(1, false));
|
|
|
|
|
|
|
|
// Initial position should equal start and end since we did not select anything
|
|
|
|
expect(range.getLastStartingPosition()).toBe(range.start);
|
|
|
|
expect(range.getLastStartingPosition()).toBe(range.end);
|
|
|
|
|
|
|
|
formatRange(range, Formatting.Bold); // Toggle
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([SERIALIZED_NEWLINE, { text: "**hello!**", type: "plain" }]);
|
2022-03-16 10:46:07 +01:00
|
|
|
|
|
|
|
formatRange(range, Formatting.Bold); // Untoggle
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([SERIALIZED_NEWLINE, { text: "hello!", type: "plain" }]);
|
2022-03-16 10:46:07 +01:00
|
|
|
|
|
|
|
// Check if it also works for code as it uses toggleInlineFormatting only indirectly
|
|
|
|
range = model.startRange(model.positionForOffset(1, false));
|
|
|
|
selectRangeOfWordAtCaret(range);
|
|
|
|
|
|
|
|
formatRange(range, Formatting.Code); // Toggle
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([SERIALIZED_NEWLINE, { text: "`hello!`", type: "plain" }]);
|
2022-03-16 10:46:07 +01:00
|
|
|
|
|
|
|
formatRange(range, Formatting.Code); // Untoggle
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(model.serializeParts()).toEqual([SERIALIZED_NEWLINE, { text: "hello!", type: "plain" }]);
|
2022-03-16 10:46:07 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("caret resets correctly to current line when untoggling formatting while caret at line end", () => {
|
2022-03-16 10:46:07 +01:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel(
|
|
|
|
[pc.plain("hello **hello!**"), pc.newline(), pc.plain("world")],
|
|
|
|
pc,
|
|
|
|
renderer,
|
|
|
|
);
|
2022-03-16 10:46:07 +01:00
|
|
|
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "hello **hello!**", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "world", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
const endOfFirstLine = 16;
|
|
|
|
const range = model.startRange(model.positionForOffset(endOfFirstLine, true));
|
|
|
|
|
|
|
|
formatRange(range, Formatting.Bold); // Untoggle
|
|
|
|
formatRange(range, Formatting.Italics); // Toggle
|
|
|
|
|
|
|
|
// We expect formatting to still happen in the first line as the caret should not jump down
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "hello _hello!_", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "world", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("format link in front of new line part", () => {
|
2022-03-16 10:46:07 +01: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.plain("world!"), pc.newline()],
|
|
|
|
pc,
|
|
|
|
renderer,
|
|
|
|
);
|
2022-03-16 10:46:07 +01:00
|
|
|
|
|
|
|
let range = model.startRange(model.getPositionAtEnd().asOffset(model).add(-1).asPosition(model)); // select-all
|
|
|
|
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "hello!", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "world!", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
]);
|
|
|
|
|
|
|
|
formatRange(range, Formatting.InsertLink); // Toggle
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "hello!", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "[world!]()", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
]);
|
|
|
|
|
|
|
|
range = model.startRange(model.getPositionAtEnd().asOffset(model).add(-1).asPosition(model)); // select-all
|
|
|
|
formatRange(range, Formatting.InsertLink); // Untoggle
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "hello!", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "world!", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("format multi line code", () => {
|
2022-03-16 10:46:07 +01:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel(
|
|
|
|
[pc.plain("int x = 1;"), pc.newline(), pc.newline(), pc.plain("int y = 42;")],
|
|
|
|
pc,
|
|
|
|
renderer,
|
|
|
|
);
|
2022-03-16 10:46:07 +01:00
|
|
|
|
|
|
|
let range = model.startRange(model.positionForOffset(0), model.getPositionAtEnd()); // select-all
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(range.parts.map((p) => p.text).join("")).toBe("int x = 1;\n\nint y = 42;");
|
2022-03-16 10:46:07 +01:00
|
|
|
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "int x = 1;", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "int y = 42;", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
formatRange(range, Formatting.Code); // Toggle
|
|
|
|
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "```", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "int x = 1;", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "int y = 42;", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "```", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
range = model.startRange(model.positionForOffset(0, false), model.getPositionAtEnd()); // select-all
|
|
|
|
formatRange(range, Formatting.Code); // Untoggle
|
|
|
|
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "int x = 1;", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: "int y = 42;", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("does not format pure white space", () => {
|
2022-03-16 10:46:07 +01:00
|
|
|
const renderer = createRenderer();
|
|
|
|
const pc = createPartCreator();
|
2022-12-12 12:24:14 +01:00
|
|
|
const model = new EditorModel(
|
|
|
|
[pc.plain(" "), pc.newline(), pc.newline(), pc.plain(" ")],
|
|
|
|
pc,
|
|
|
|
renderer,
|
|
|
|
);
|
2022-03-16 10:46:07 +01:00
|
|
|
|
|
|
|
const range = model.startRange(model.positionForOffset(0), model.getPositionAtEnd()); // select-all
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(range.parts.map((p) => p.text).join("")).toBe(" \n\n ");
|
2022-03-16 10:46:07 +01:00
|
|
|
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: " ", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: " ", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
formatRange(range, Formatting.Bold);
|
|
|
|
|
|
|
|
expect(model.serializeParts()).toEqual([
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: " ", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
SERIALIZED_NEWLINE,
|
|
|
|
SERIALIZED_NEWLINE,
|
2022-12-12 12:24:14 +01:00
|
|
|
{ text: " ", type: "plain" },
|
2022-03-16 10:46:07 +01:00
|
|
|
]);
|
|
|
|
});
|
2020-01-21 16:32:32 +01:00
|
|
|
});
|
|
|
|
});
|