You've already forked matrix-react-sdk
mirror of
https://github.com/matrix-org/matrix-react-sdk.git
synced 2025-07-31 13:44:28 +03:00
Apply prettier formatting
This commit is contained in:
@ -24,54 +24,48 @@ import {
|
||||
toggleInlineFormat,
|
||||
} from "../../src/editor/operations";
|
||||
import { Formatting } from "../../src/components/views/rooms/MessageComposerFormatBar";
|
||||
import { longestBacktickSequence } from '../../src/editor/deserialize';
|
||||
import { longestBacktickSequence } from "../../src/editor/deserialize";
|
||||
|
||||
const SERIALIZED_NEWLINE = { "text": "\n", "type": "newline" };
|
||||
const SERIALIZED_NEWLINE = { text: "\n", type: "newline" };
|
||||
|
||||
describe("editor/operations: formatting operations", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
|
||||
describe("formatRange", () => {
|
||||
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);
|
||||
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);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(6, false),
|
||||
model.positionForOffset(11, false)); // around "world"
|
||||
const range = model.startRange(model.positionForOffset(6, false), model.positionForOffset(11, false)); // around "world"
|
||||
|
||||
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" }]);
|
||||
});
|
||||
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" }]);
|
||||
},
|
||||
);
|
||||
|
||||
it("should apply to word range is within if length 0", () => {
|
||||
const model = new EditorModel([
|
||||
pc.plain("hello world!"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel([pc.plain("hello world!")], pc, renderer);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(6, false));
|
||||
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hello world!", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hello world!", type: "plain" }]);
|
||||
formatRange(range, Formatting.Bold);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hello **world!**", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hello **world!**", type: "plain" }]);
|
||||
});
|
||||
|
||||
it("should do nothing for a range with length 0 at initialisation", () => {
|
||||
const model = new EditorModel([
|
||||
pc.plain("hello world!"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel([pc.plain("hello world!")], pc, renderer);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(6, false));
|
||||
range.setWasEmpty(false);
|
||||
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hello world!", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hello world!", type: "plain" }]);
|
||||
formatRange(range, Formatting.Bold);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hello world!", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hello world!", type: "plain" }]);
|
||||
});
|
||||
});
|
||||
|
||||
@ -83,12 +77,12 @@ describe("editor/operations: formatting operations", () => {
|
||||
["[testing]()", "testing|", ""],
|
||||
["[testing](foobar)", "testing|", ""],
|
||||
])("converts %s -> %s", (input: string, expectation: string, text: string) => {
|
||||
const model = new EditorModel([
|
||||
pc.plain(`foo ${input} bar`),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel([pc.plain(`foo ${input} bar`)], pc, renderer);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(4, false),
|
||||
model.positionForOffset(4 + input.length, false)); // around input
|
||||
const range = model.startRange(
|
||||
model.positionForOffset(4, false),
|
||||
model.positionForOffset(4 + input.length, false),
|
||||
); // around input
|
||||
|
||||
expect(range.parts[0].text).toBe(input);
|
||||
formatRangeAsLink(range, text);
|
||||
@ -99,192 +93,173 @@ describe("editor/operations: formatting operations", () => {
|
||||
|
||||
describe("toggleInlineFormat", () => {
|
||||
it("works for words", () => {
|
||||
const model = new EditorModel([
|
||||
pc.plain("hello world!"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel([pc.plain("hello world!")], pc, renderer);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(6, false),
|
||||
model.positionForOffset(11, false)); // around "world"
|
||||
const range = model.startRange(model.positionForOffset(6, false), model.positionForOffset(11, false)); // around "world"
|
||||
|
||||
expect(range.parts[0].text).toBe("world");
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hello world!", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hello world!", type: "plain" }]);
|
||||
formatRange(range, Formatting.Italics);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hello _world_!", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hello _world_!", type: "plain" }]);
|
||||
});
|
||||
|
||||
describe('escape backticks', () => {
|
||||
it('works for escaping backticks in between texts', () => {
|
||||
describe("escape backticks", () => {
|
||||
it("works for escaping backticks in between texts", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.plain("hello ` world!"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel([pc.plain("hello ` world!")], pc, renderer);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(0, false),
|
||||
model.positionForOffset(13, false)); // hello ` world
|
||||
const range = model.startRange(model.positionForOffset(0, false), model.positionForOffset(13, false)); // hello ` world
|
||||
|
||||
expect(range.parts[0].text.trim().includes("`")).toBeTruthy();
|
||||
expect(longestBacktickSequence(range.parts[0].text.trim())).toBe(1);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hello ` world!", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hello ` world!", type: "plain" }]);
|
||||
formatRangeAsCode(range);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "``hello ` world``!", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "``hello ` world``!", type: "plain" }]);
|
||||
});
|
||||
|
||||
it('escapes longer backticks in between text', () => {
|
||||
it("escapes longer backticks in between text", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.plain("hello```world"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel([pc.plain("hello```world")], pc, renderer);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(0, false),
|
||||
model.getPositionAtEnd()); // hello```world
|
||||
const range = model.startRange(model.positionForOffset(0, false), model.getPositionAtEnd()); // hello```world
|
||||
|
||||
expect(range.parts[0].text.includes("`")).toBeTruthy();
|
||||
expect(longestBacktickSequence(range.parts[0].text)).toBe(3);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hello```world", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hello```world", type: "plain" }]);
|
||||
formatRangeAsCode(range);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "````hello```world````", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "````hello```world````", type: "plain" }]);
|
||||
});
|
||||
|
||||
it('escapes non-consecutive with varying length backticks in between text', () => {
|
||||
it("escapes non-consecutive with varying length backticks in between text", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.plain("hell```o`w`o``rld"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel([pc.plain("hell```o`w`o``rld")], pc, renderer);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(0, false),
|
||||
model.getPositionAtEnd()); // hell```o`w`o``rld
|
||||
const range = model.startRange(model.positionForOffset(0, false), model.getPositionAtEnd()); // hell```o`w`o``rld
|
||||
expect(range.parts[0].text.includes("`")).toBeTruthy();
|
||||
expect(longestBacktickSequence(range.parts[0].text)).toBe(3);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hell```o`w`o``rld", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hell```o`w`o``rld", type: "plain" }]);
|
||||
formatRangeAsCode(range);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "````hell```o`w`o``rld````", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "````hell```o`w`o``rld````", type: "plain" }]);
|
||||
});
|
||||
|
||||
it('untoggles correctly if its already formatted', () => {
|
||||
it("untoggles correctly if its already formatted", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.plain("```hello``world```"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel([pc.plain("```hello``world```")], pc, renderer);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(0, false),
|
||||
model.getPositionAtEnd()); // hello``world
|
||||
const range = model.startRange(model.positionForOffset(0, false), model.getPositionAtEnd()); // hello``world
|
||||
expect(range.parts[0].text.includes("`")).toBeTruthy();
|
||||
expect(longestBacktickSequence(range.parts[0].text)).toBe(3);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "```hello``world```", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "```hello``world```", type: "plain" }]);
|
||||
formatRangeAsCode(range);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hello``world", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hello``world", type: "plain" }]);
|
||||
});
|
||||
it('untoggles correctly it contains varying length of backticks between text', () => {
|
||||
it("untoggles correctly it contains varying length of backticks between text", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.plain("````hell```o`w`o``rld````"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel([pc.plain("````hell```o`w`o``rld````")], pc, renderer);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(0, false),
|
||||
model.getPositionAtEnd()); // hell```o`w`o``rld
|
||||
const range = model.startRange(model.positionForOffset(0, false), model.getPositionAtEnd()); // hell```o`w`o``rld
|
||||
expect(range.parts[0].text.includes("`")).toBeTruthy();
|
||||
expect(longestBacktickSequence(range.parts[0].text)).toBe(4);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "````hell```o`w`o``rld````", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "````hell```o`w`o``rld````", type: "plain" }]);
|
||||
formatRangeAsCode(range);
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hell```o`w`o``rld", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hell```o`w`o``rld", type: "plain" }]);
|
||||
});
|
||||
});
|
||||
|
||||
it('works for parts of words', () => {
|
||||
it("works for parts of words", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.plain("hello world!"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel([pc.plain("hello world!")], pc, renderer);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(7, false),
|
||||
model.positionForOffset(10, false)); // around "orl"
|
||||
const range = model.startRange(model.positionForOffset(7, false), model.positionForOffset(10, false)); // around "orl"
|
||||
|
||||
expect(range.parts[0].text).toBe("orl");
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hello world!", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hello world!", type: "plain" }]);
|
||||
toggleInlineFormat(range, "*");
|
||||
expect(model.serializeParts()).toEqual([{ "text": "hello w*orl*d!", "type": "plain" }]);
|
||||
expect(model.serializeParts()).toEqual([{ text: "hello w*orl*d!", type: "plain" }]);
|
||||
});
|
||||
|
||||
it('works for around pills', () => {
|
||||
it("works for around pills", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.plain("hello there "),
|
||||
pc.atRoomPill("@room"),
|
||||
pc.plain(", how are you doing?"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel(
|
||||
[pc.plain("hello there "), pc.atRoomPill("@room"), pc.plain(", how are you doing?")],
|
||||
pc,
|
||||
renderer,
|
||||
);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(6, false),
|
||||
model.positionForOffset(30, false)); // around "there @room, how are you"
|
||||
const range = model.startRange(model.positionForOffset(6, false), model.positionForOffset(30, false)); // around "there @room, how are you"
|
||||
|
||||
expect(range.parts.map(p => p.text).join("")).toBe("there @room, how are you");
|
||||
expect(range.parts.map((p) => p.text).join("")).toBe("there @room, how are you");
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "hello there ", "type": "plain" },
|
||||
{ "text": "@room", "type": "at-room-pill" },
|
||||
{ "text": ", how are you doing?", "type": "plain" },
|
||||
{ text: "hello there ", type: "plain" },
|
||||
{ text: "@room", type: "at-room-pill" },
|
||||
{ text: ", how are you doing?", type: "plain" },
|
||||
]);
|
||||
formatRange(range, Formatting.Italics);
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "hello _there ", "type": "plain" },
|
||||
{ "text": "@room", "type": "at-room-pill" },
|
||||
{ "text": ", how are you_ doing?", "type": "plain" },
|
||||
{ text: "hello _there ", type: "plain" },
|
||||
{ text: "@room", type: "at-room-pill" },
|
||||
{ text: ", how are you_ doing?", type: "plain" },
|
||||
]);
|
||||
});
|
||||
|
||||
it('works for a paragraph', () => {
|
||||
it("works for a paragraph", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.plain("hello world,"),
|
||||
pc.newline(),
|
||||
pc.plain("how are you doing?"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel(
|
||||
[pc.plain("hello world,"), pc.newline(), pc.plain("how are you doing?")],
|
||||
pc,
|
||||
renderer,
|
||||
);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(6, false),
|
||||
model.positionForOffset(16, false)); // around "world,\nhow"
|
||||
const range = model.startRange(model.positionForOffset(6, false), model.positionForOffset(16, false)); // around "world,\nhow"
|
||||
|
||||
expect(range.parts.map(p => p.text).join("")).toBe("world,\nhow");
|
||||
expect(range.parts.map((p) => p.text).join("")).toBe("world,\nhow");
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "hello world,", "type": "plain" },
|
||||
{ text: "hello world,", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "how are you doing?", "type": "plain" },
|
||||
{ text: "how are you doing?", type: "plain" },
|
||||
]);
|
||||
formatRange(range, Formatting.Bold);
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "hello **world,", "type": "plain" },
|
||||
{ text: "hello **world,", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "how** are you doing?", "type": "plain" },
|
||||
{ text: "how** are you doing?", type: "plain" },
|
||||
]);
|
||||
});
|
||||
|
||||
it('works for a paragraph with spurious breaks around it in selected range', () => {
|
||||
it("works for a paragraph with spurious breaks around it in selected range", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
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 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
|
||||
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");
|
||||
expect(range.parts.map((p) => p.text).join("")).toBe("\n\nhello world,\nhow are you doing?\n\n");
|
||||
expect(model.serializeParts()).toEqual([
|
||||
SERIALIZED_NEWLINE,
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "hello world,", "type": "plain" },
|
||||
{ text: "hello world,", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "how are you doing?", "type": "plain" },
|
||||
{ text: "how are you doing?", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
SERIALIZED_NEWLINE,
|
||||
]);
|
||||
@ -292,64 +267,65 @@ describe("editor/operations: formatting operations", () => {
|
||||
expect(model.serializeParts()).toEqual([
|
||||
SERIALIZED_NEWLINE,
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "**hello world,", "type": "plain" },
|
||||
{ text: "**hello world,", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "how are you doing?**", "type": "plain" },
|
||||
{ text: "how are you doing?**", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
SERIALIZED_NEWLINE,
|
||||
]);
|
||||
});
|
||||
|
||||
it('works for multiple paragraph', () => {
|
||||
it("works for multiple paragraph", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
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);
|
||||
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,
|
||||
);
|
||||
|
||||
let range = model.startRange(model.positionForOffset(0, true), model.getPositionAtEnd()); // select-all
|
||||
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "hello world,", "type": "plain" },
|
||||
{ text: "hello world,", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "how are you doing?", "type": "plain" },
|
||||
{ text: "how are you doing?", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "new paragraph", "type": "plain" },
|
||||
{ text: "new paragraph", type: "plain" },
|
||||
]);
|
||||
toggleInlineFormat(range, "__");
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "__hello world,", "type": "plain" },
|
||||
{ text: "__hello world,", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "how are you doing?__", "type": "plain" },
|
||||
{ text: "how are you doing?__", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "__new paragraph__", "type": "plain" },
|
||||
{ text: "__new paragraph__", type: "plain" },
|
||||
]);
|
||||
range = model.startRange(model.positionForOffset(0, true), model.getPositionAtEnd()); // select-all
|
||||
toggleInlineFormat(range, "__");
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "hello world,", "type": "plain" },
|
||||
{ text: "hello world,", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "how are you doing?", "type": "plain" },
|
||||
{ text: "how are you doing?", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "new paragraph", "type": "plain" },
|
||||
{ text: "new paragraph", type: "plain" },
|
||||
]);
|
||||
});
|
||||
|
||||
it('format word at caret position at beginning of new line without previous selection', () => {
|
||||
it("format word at caret position at beginning of new line without previous selection", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.newline(),
|
||||
pc.plain("hello!"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel([pc.newline(), pc.plain("hello!")], pc, renderer);
|
||||
|
||||
let range = model.startRange(model.positionForOffset(1, false));
|
||||
|
||||
@ -359,17 +335,11 @@ describe("editor/operations: formatting operations", () => {
|
||||
|
||||
formatRange(range, Formatting.Bold); // Toggle
|
||||
|
||||
expect(model.serializeParts()).toEqual([
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "**hello!**", "type": "plain" },
|
||||
]);
|
||||
expect(model.serializeParts()).toEqual([SERIALIZED_NEWLINE, { text: "**hello!**", type: "plain" }]);
|
||||
|
||||
formatRange(range, Formatting.Bold); // Untoggle
|
||||
|
||||
expect(model.serializeParts()).toEqual([
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "hello!", "type": "plain" },
|
||||
]);
|
||||
expect(model.serializeParts()).toEqual([SERIALIZED_NEWLINE, { text: "hello!", type: "plain" }]);
|
||||
|
||||
// Check if it also works for code as it uses toggleInlineFormatting only indirectly
|
||||
range = model.startRange(model.positionForOffset(1, false));
|
||||
@ -377,31 +347,25 @@ describe("editor/operations: formatting operations", () => {
|
||||
|
||||
formatRange(range, Formatting.Code); // Toggle
|
||||
|
||||
expect(model.serializeParts()).toEqual([
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "`hello!`", "type": "plain" },
|
||||
]);
|
||||
expect(model.serializeParts()).toEqual([SERIALIZED_NEWLINE, { text: "`hello!`", type: "plain" }]);
|
||||
|
||||
formatRange(range, Formatting.Code); // Untoggle
|
||||
expect(model.serializeParts()).toEqual([
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "hello!", "type": "plain" },
|
||||
]);
|
||||
expect(model.serializeParts()).toEqual([SERIALIZED_NEWLINE, { text: "hello!", type: "plain" }]);
|
||||
});
|
||||
|
||||
it('caret resets correctly to current line when untoggling formatting while caret at line end', () => {
|
||||
it("caret resets correctly to current line when untoggling formatting while caret at line end", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.plain("hello **hello!**"),
|
||||
pc.newline(),
|
||||
pc.plain("world"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel(
|
||||
[pc.plain("hello **hello!**"), pc.newline(), pc.plain("world")],
|
||||
pc,
|
||||
renderer,
|
||||
);
|
||||
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "hello **hello!**", "type": "plain" },
|
||||
{ text: "hello **hello!**", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "world", "type": "plain" },
|
||||
{ text: "world", type: "plain" },
|
||||
]);
|
||||
|
||||
const endOfFirstLine = 16;
|
||||
@ -412,121 +376,118 @@ describe("editor/operations: formatting operations", () => {
|
||||
|
||||
// We expect formatting to still happen in the first line as the caret should not jump down
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "hello _hello!_", "type": "plain" },
|
||||
{ text: "hello _hello!_", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "world", "type": "plain" },
|
||||
{ text: "world", type: "plain" },
|
||||
]);
|
||||
});
|
||||
|
||||
it('format link in front of new line part', () => {
|
||||
it("format link in front of new line part", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.plain("hello!"),
|
||||
pc.newline(),
|
||||
pc.plain("world!"),
|
||||
pc.newline(),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel(
|
||||
[pc.plain("hello!"), pc.newline(), pc.plain("world!"), pc.newline()],
|
||||
pc,
|
||||
renderer,
|
||||
);
|
||||
|
||||
let range = model.startRange(model.getPositionAtEnd().asOffset(model).add(-1).asPosition(model)); // select-all
|
||||
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "hello!", "type": "plain" },
|
||||
{ text: "hello!", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "world!", "type": "plain" },
|
||||
{ text: "world!", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
]);
|
||||
|
||||
formatRange(range, Formatting.InsertLink); // Toggle
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "hello!", "type": "plain" },
|
||||
{ text: "hello!", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "[world!]()", "type": "plain" },
|
||||
{ text: "[world!]()", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
]);
|
||||
|
||||
range = model.startRange(model.getPositionAtEnd().asOffset(model).add(-1).asPosition(model)); // select-all
|
||||
formatRange(range, Formatting.InsertLink); // Untoggle
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "hello!", "type": "plain" },
|
||||
{ text: "hello!", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "world!", "type": "plain" },
|
||||
{ text: "world!", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
]);
|
||||
});
|
||||
|
||||
it('format multi line code', () => {
|
||||
it("format multi line code", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.plain("int x = 1;"),
|
||||
pc.newline(),
|
||||
pc.newline(),
|
||||
pc.plain("int y = 42;"),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel(
|
||||
[pc.plain("int x = 1;"), pc.newline(), pc.newline(), pc.plain("int y = 42;")],
|
||||
pc,
|
||||
renderer,
|
||||
);
|
||||
|
||||
let range = model.startRange(model.positionForOffset(0), model.getPositionAtEnd()); // select-all
|
||||
|
||||
expect(range.parts.map(p => p.text).join("")).toBe("int x = 1;\n\nint y = 42;");
|
||||
expect(range.parts.map((p) => p.text).join("")).toBe("int x = 1;\n\nint y = 42;");
|
||||
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "int x = 1;", "type": "plain" },
|
||||
{ text: "int x = 1;", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "int y = 42;", "type": "plain" },
|
||||
{ text: "int y = 42;", type: "plain" },
|
||||
]);
|
||||
|
||||
formatRange(range, Formatting.Code); // Toggle
|
||||
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "```", "type": "plain" },
|
||||
{ text: "```", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "int x = 1;", "type": "plain" },
|
||||
{ text: "int x = 1;", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "int y = 42;", "type": "plain" },
|
||||
{ text: "int y = 42;", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "```", "type": "plain" },
|
||||
{ text: "```", type: "plain" },
|
||||
]);
|
||||
|
||||
range = model.startRange(model.positionForOffset(0, false), model.getPositionAtEnd()); // select-all
|
||||
formatRange(range, Formatting.Code); // Untoggle
|
||||
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": "int x = 1;", "type": "plain" },
|
||||
{ text: "int x = 1;", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": "int y = 42;", "type": "plain" },
|
||||
{ text: "int y = 42;", type: "plain" },
|
||||
]);
|
||||
});
|
||||
|
||||
it('does not format pure white space', () => {
|
||||
it("does not format pure white space", () => {
|
||||
const renderer = createRenderer();
|
||||
const pc = createPartCreator();
|
||||
const model = new EditorModel([
|
||||
pc.plain(" "),
|
||||
pc.newline(),
|
||||
pc.newline(),
|
||||
pc.plain(" "),
|
||||
], pc, renderer);
|
||||
const model = new EditorModel(
|
||||
[pc.plain(" "), pc.newline(), pc.newline(), pc.plain(" ")],
|
||||
pc,
|
||||
renderer,
|
||||
);
|
||||
|
||||
const range = model.startRange(model.positionForOffset(0), model.getPositionAtEnd()); // select-all
|
||||
expect(range.parts.map(p => p.text).join("")).toBe(" \n\n ");
|
||||
expect(range.parts.map((p) => p.text).join("")).toBe(" \n\n ");
|
||||
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": " ", "type": "plain" },
|
||||
{ text: " ", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": " ", "type": "plain" },
|
||||
{ text: " ", type: "plain" },
|
||||
]);
|
||||
|
||||
formatRange(range, Formatting.Bold);
|
||||
|
||||
expect(model.serializeParts()).toEqual([
|
||||
{ "text": " ", "type": "plain" },
|
||||
{ text: " ", type: "plain" },
|
||||
SERIALIZED_NEWLINE,
|
||||
SERIALIZED_NEWLINE,
|
||||
{ "text": " ", "type": "plain" },
|
||||
{ text: " ", type: "plain" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user