Files
upstream-affine/tests/blocksuite/e2e/latex/inline.spec.ts
upstream-mirror 702cc698a7
Some checks failed
Build & Test / Analyze (javascript, affine) (push) Failing after 4s
Build & Test / Analyze (typescript, affine) (push) Failing after 3s
Build & Test / Rust test filter (push) Failing after 4s
Build & Test / Check yarn binary (push) Failing after 3s
Build & Test / E2E BlockSuite Test (3) (push) Failing after 3s
Build & Test / Analyze (javascript, blocksuite) (push) Failing after 3s
Build & Test / Analyze (typescript, blocksuite) (push) Failing after 3s
Build & Test / Mobile native build filter (push) Failing after 3s
Build & Test / E2E BlockSuite Test (1) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (2) (push) Failing after 4s
Build & Test / E2E Mobile Test (1) (push) Failing after 3s
Build & Test / Unit Test (1) (push) Has been skipped
Build & Test / Unit Test (2) (push) Has been skipped
Build & Test / Unit Test (3) (push) Has been skipped
Build & Test / Unit Test (4) (push) Has been skipped
Build & Test / Unit Test (5) (push) Has been skipped
Build & Test / Native Unit Test (push) Has been skipped
Build & Test / E2E BlockSuite Test (4) (push) Failing after 3s
Build & Test / E2E BlockSuite Test (5) (push) Failing after 3s
Build & Test / E2E BlockSuite Cross Browser Test (push) Failing after 3s
Build & Test / E2E Mobile Test (2) (push) Failing after 3s
Build & Test / Build AFFiNE native (x86_64-unknown-linux-gnu) (push) Failing after 3s
Build & Test / Build Server native (push) Failing after 3s
Build & Test / Check Git Status (push) Has been skipped
Build & Test / Server Test (0, 4) (push) Has been skipped
Build & Test / Server Test (1, 4) (push) Has been skipped
Build & Test / Server Test (2, 4) (push) Has been skipped
Build & Test / Server Test (3, 4) (push) Has been skipped
Build & Test / Server Test with Elasticsearch (push) Has been skipped
Build & Test / Build @affine/electron renderer (push) Failing after 3s
Build & Test / Copilot test filter (push) Failing after 4s
Build & Test / Lint Rust (push) Has been skipped
Build & Test / Server Test with Embedded Search (push) Has been skipped
Build & Test / Server Test with Manticore Search (push) Has been skipped
Build & Test / Server E2E Test (push) Has been skipped
Build & Test / Cloud Desktop E2E Test (push) Has been skipped
Build & Test / Cloud E2E Test 1/2 (push) Has been skipped
Build & Test / Cloud E2E Test 2/2 (push) Has been skipped
Build & Test / Build Android app (push) Has been skipped
Build & Test / Test Android app (push) Has been skipped
Build & Test / Run native tests (push) Has been skipped
Build & Test / Server Copilot Api Test (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (1, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (2, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (3, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (4, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (5, 5) (push) Has been skipped
Release / Canary Gate (push) Has been skipped
Build & Test / Build AFFiNE native (aarch64-apple-darwin) (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-apple-darwin) (push) Has been cancelled
Build & Test / Desktop Test (ubuntu-latest, linux, x64, x86_64-unknown-linux-gnu, true) (push) Has been cancelled
Build & Test / Desktop Test (windows-latest, windows, x64, x86_64-pc-windows-msvc, true) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, arm64, aarch64-apple-darwin, true) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, x64, x86_64-apple-darwin, false) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled
Build & Test / Build iOS app (push) Has been cancelled
Copilot Test Automatically / Setup Test (push) Failing after 1s
Release / Prepare (push) Failing after 2s
Release / Release Cloud (push) Has been skipped
Release / Release Desktop (push) Has been skipped
Release / Release Docker Image (push) Has been skipped
Release / Release Mobile (push) Has been skipped
snapshot of https://github.com/toeverything/AFFiNE.git (canary, 2026-09-16; blobs > 131072B omitted)
2026-09-16 04:01:20 +05:00

442 lines
14 KiB
TypeScript

import { expect } from '@playwright/test';
import {
dragBetweenIndices,
enterPlaygroundRoom,
focusRichText,
initEmptyParagraphState,
waitNextFrame,
} from '../utils/actions/index.js';
import {
cutByKeyboard,
pasteByKeyboard,
pressArrowLeft,
pressArrowRight,
pressArrowUp,
pressBackspace,
pressBackspaceWithShortKey,
pressEnter,
pressShiftEnter,
redoByKeyboard,
selectAllByKeyboard,
type,
undoByKeyboard,
} from '../utils/actions/keyboard.js';
import {
assertRichTextInlineDeltas,
assertRichTextInlineRange,
} from '../utils/asserts.js';
import { ZERO_WIDTH_FOR_EMPTY_LINE } from '../utils/inline-editor.js';
import { test } from '../utils/playwright.js';
import { getFormatBar } from '../utils/query.js';
test('add inline latex at the start of line', async ({ page }, testInfo) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);
await focusRichText(page);
const latexEditorLine = page.locator('latex-editor-menu v-line div');
const latexElement = page.locator(
'affine-paragraph rich-text affine-latex-node'
);
expect(await latexEditorLine.isVisible()).not.toBeTruthy();
expect(await latexElement.isVisible()).not.toBeTruthy();
await type(page, '$$ ');
expect(await latexEditorLine.isVisible()).toBeTruthy();
expect(await latexElement.isVisible()).toBeTruthy();
expect(await latexElement.locator('.placeholder').innerText()).toBe(
'Equation'
);
await type(page, 'E=mc^2');
expect(await latexEditorLine.innerText()).toBe('E=mc^2');
const innerHTML = await latexElement.locator('.katex').innerHTML();
expect(innerHTML).toMatchSnapshot(`${testInfo.title}.html`);
await pressEnter(page);
expect(await latexEditorLine.isVisible()).not.toBeTruthy();
expect(await latexElement.locator('.katex').innerHTML()).toBe(innerHTML);
});
test('add inline latex in the middle of text', async ({ page }, testInfo) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);
await focusRichText(page);
const latexEditorLine = page.locator('latex-editor-menu v-line div');
const latexElement = page.locator(
'affine-paragraph rich-text affine-latex-node'
);
expect(await latexEditorLine.isVisible()).not.toBeTruthy();
expect(await latexElement.isVisible()).not.toBeTruthy();
await type(page, 'aaaa');
await pressArrowLeft(page, 2);
await type(page, '$$ ');
expect(await latexEditorLine.isVisible()).toBeTruthy();
expect(await latexElement.isVisible()).toBeTruthy();
expect(await latexElement.locator('.placeholder').innerText()).toBe(
'Equation'
);
await type(page, 'E=mc^2');
expect(await latexEditorLine.innerText()).toBe('E=mc^2');
const innerHTML = await latexElement.locator('.katex').innerHTML();
expect(innerHTML).toMatchSnapshot(`${testInfo.title}.html`);
await pressEnter(page);
expect(await latexEditorLine.isVisible()).not.toBeTruthy();
expect(await latexElement.locator('.katex').innerHTML()).toBe(innerHTML);
});
test('update inline latex by clicking the node', async ({ page }, testInfo) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);
await focusRichText(page);
const latexEditorLine = page.locator('latex-editor-menu v-line div');
const latexElement = page.locator(
'affine-paragraph rich-text affine-latex-node'
);
expect(await latexEditorLine.isVisible()).not.toBeTruthy();
await type(page, '$$ ');
expect(await latexEditorLine.isVisible()).toBeTruthy();
await type(page, 'E=mc^2');
await pressEnter(page);
expect(await latexEditorLine.isVisible()).not.toBeTruthy();
await latexElement.click();
expect(await latexEditorLine.isVisible()).toBeTruthy();
await pressBackspace(page, 6);
await type(page, String.raw`\def\arraystretch{1.5}`);
await pressShiftEnter(page);
await type(page, String.raw`\begin{array}{c:c:c}`);
await pressShiftEnter(page);
await type(page, String.raw`a & b & c \\ \\ hline`);
await pressShiftEnter(page);
await type(page, String.raw`d & e & f \\`);
await pressShiftEnter(page);
await type(page, String.raw`\hdashline`);
await pressShiftEnter(page);
await type(page, String.raw`g & h & i`);
await pressShiftEnter(page);
await type(page, String.raw`\end{array}`);
const innerHTML = await latexElement.locator('.katex').innerHTML();
expect(innerHTML).toMatchSnapshot(`${testInfo.title}.html`);
// click outside to hide the editor
await page.click('affine-editor-container');
expect(await latexEditorLine.isVisible()).not.toBeTruthy();
});
test('latex editor', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);
await focusRichText(page);
const latexEditorLine = page.locator('latex-editor-menu v-line div');
const latexElement = page.locator(
'affine-paragraph rich-text affine-latex-node'
);
expect(await latexEditorLine.isVisible()).not.toBeTruthy();
await type(page, '$$ ');
expect(await latexEditorLine.isVisible()).toBeTruthy();
// test cursor movement works as expected
// https://github.com/toeverything/blocksuite/pull/8368
await type(page, 'ababababababababababababababababababababababababab');
expect(await latexEditorLine.innerText()).toBe(
'ababababababababababababababababababababababababab'
);
// click outside to hide the editor
expect(await latexEditorLine.isVisible()).toBeTruthy();
await page.mouse.click(130, 130);
expect(await latexEditorLine.isVisible()).not.toBeTruthy();
await latexElement.click();
expect(await latexEditorLine.isVisible()).toBeTruthy();
expect(await latexEditorLine.innerText()).toBe(
'ababababababababababababababababababababababababab'
);
await pressBackspaceWithShortKey(page, 2);
expect(await latexEditorLine.innerText()).toBe(ZERO_WIDTH_FOR_EMPTY_LINE);
await undoByKeyboard(page);
expect(await latexEditorLine.innerText()).toBe(
'ababababababababababababababababababababababababab'
);
await redoByKeyboard(page);
expect(await latexEditorLine.innerText()).toBe(ZERO_WIDTH_FOR_EMPTY_LINE);
await undoByKeyboard(page);
expect(await latexEditorLine.innerText()).toBe(
'ababababababababababababababababababababababababab'
);
// undo-redo
await pressArrowLeft(page, 5);
await page.keyboard.down('Shift');
await pressArrowUp(page);
await pressArrowRight(page);
await page.keyboard.up('Shift');
/**
* abababababababababab|ababab
* abababababababababa|babab
*/
await cutByKeyboard(page);
expect(await latexEditorLine.innerText()).toBe('ababababababababababababab');
/**
* abababababababababab|babab
*/
await pressArrowRight(page, 2);
/**
* ababababababababababba|bab
*/
await pasteByKeyboard(page);
expect(await latexEditorLine.innerText()).toBe(
'ababababababababababababababababababababababababab'
);
await selectAllByKeyboard(page);
await pressBackspace(page);
expect(await latexEditorLine.innerText()).toBe(ZERO_WIDTH_FOR_EMPTY_LINE);
// highlight
await type(
page,
String.raw`a+\left(\vcenter{\hbox{$\frac{\frac a b}c$}}\right)`
);
expect(
(await latexEditorLine.locator('latex-editor-unit').innerHTML()).replace(
/lit\$\d+\$/g,
'lit$test$'
)
).toBe(
'\x3C!----><span>\x3C!--?lit$test$-->\x3C!----><v-text style="color:#000000;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->a+</span></v-text>\x3C!---->\x3C!----><v-text style="color:#795E26;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->\\left</span></v-text>\x3C!---->\x3C!----><v-text style="color:#000000;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->(</span></v-text>\x3C!---->\x3C!----><v-text style="color:#795E26;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->\\vcenter</span></v-text>\x3C!---->\x3C!----><v-text style="color:#000000;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->{</span></v-text>\x3C!---->\x3C!----><v-text style="color:#795E26;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->\\hbox</span></v-text>\x3C!---->\x3C!----><v-text style="color:#000000;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->{</span></v-text>\x3C!---->\x3C!----><v-text style="color:#267F99;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->$</span></v-text>\x3C!---->\x3C!----><v-text style="color:#267F99;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->\\frac{\\frac a b}c</span></v-text>\x3C!---->\x3C!----><v-text style="color:#267F99;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->$</span></v-text>\x3C!---->\x3C!----><v-text style="color:#000000;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->}}</span></v-text>\x3C!---->\x3C!----><v-text style="color:#795E26;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->\\right</span></v-text>\x3C!---->\x3C!----><v-text style="color:#000000;">\x3C!----><span data-v-text="true" style="word-break:break-word;text-wrap:wrap;white-space-collapse:break-spaces;">\x3C!--?lit$test$-->)</span></v-text>\x3C!----></span>'
);
});
test('add inline latex using slash menu', async ({ page }, testInfo) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);
await focusRichText(page);
const latexEditorLine = page.locator('latex-editor-menu v-line div');
const latexElement = page.locator(
'affine-paragraph rich-text affine-latex-node'
);
expect(await latexEditorLine.isVisible()).not.toBeTruthy();
expect(await latexElement.isVisible()).not.toBeTruthy();
await type(page, '/ieq\n');
expect(await latexEditorLine.isVisible()).toBeTruthy();
expect(await latexElement.isVisible()).toBeTruthy();
expect(await latexElement.locator('.placeholder').innerText()).toBe(
'Equation'
);
await type(page, 'E=mc^2');
expect(await latexEditorLine.innerText()).toBe('E=mc^2');
const innerHTML = await latexElement.locator('.katex').innerHTML();
expect(innerHTML).toMatchSnapshot(`${testInfo.title}.html`);
await pressEnter(page);
expect(await latexEditorLine.isVisible()).not.toBeTruthy();
expect(await latexElement.locator('.katex').innerHTML()).toBe(innerHTML);
});
test('should preserve distinct latex values when converting selections in reverse order', async ({
page,
}) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);
await focusRichText(page);
await type(page, 'a+b test a^2 test');
await dragBetweenIndices(page, [0, 9], [0, 12]);
const { formatBar } = getFormatBar(page);
await expect(formatBar).toBeVisible();
await formatBar.getByRole('button', { name: 'Inline equation' }).click();
await waitNextFrame(page);
await assertRichTextInlineDeltas(page, [
{
insert: 'a+b test ',
},
{
insert: ' ',
attributes: {
latex: 'a^2',
},
},
{
insert: ' test',
},
]);
await dragBetweenIndices(page, [0, 0], [0, 3]);
await expect(formatBar).toBeVisible();
await formatBar.getByRole('button', { name: 'Inline equation' }).click();
await waitNextFrame(page);
await assertRichTextInlineDeltas(page, [
{
insert: ' ',
attributes: {
latex: 'a+b',
},
},
{
insert: ' test ',
},
{
insert: ' ',
attributes: {
latex: 'a^2',
},
},
{
insert: ' test',
},
]);
});
test('add inline latex using markdown shortcut', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);
await focusRichText(page);
// toggle by space or enter
await type(page, 'aa$$bb$$ cc$$dd$$\n');
await assertRichTextInlineDeltas(page, [
{
insert: 'aa',
},
{
insert: ' ',
attributes: {
latex: 'bb',
},
},
{
insert: 'cc',
},
{
insert: ' ',
attributes: {
latex: 'dd',
},
},
]);
await pressArrowUp(page);
await pressArrowRight(page, 3);
await pressBackspace(page);
await assertRichTextInlineDeltas(page, [
{
insert: 'aacc',
},
{
insert: ' ',
attributes: {
latex: 'dd',
},
},
]);
});
test('undo-redo when add inline latex using markdown shortcut', async ({
page,
}) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);
await focusRichText(page);
await type(page, 'aa$$bb$$ ');
await assertRichTextInlineDeltas(page, [
{
insert: 'aa',
},
{
insert: ' ',
attributes: {
latex: 'bb',
},
},
]);
await assertRichTextInlineRange(page, 0, 3, 0);
await undoByKeyboard(page);
await assertRichTextInlineDeltas(page, [
{
insert: 'aa$$bb$$ ',
},
]);
await assertRichTextInlineRange(page, 0, 9, 0);
await redoByKeyboard(page);
await assertRichTextInlineDeltas(page, [
{
insert: 'aa',
},
{
insert: ' ',
attributes: {
latex: 'bb',
},
},
]);
await assertRichTextInlineRange(page, 0, 3, 0);
});
test('auto focus after add inline latex using markdown shortcut', async ({
page,
}) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);
await focusRichText(page);
await type(page, 'aa$$ bbb\ncc');
await assertRichTextInlineDeltas(page, [
{
insert: 'aa',
},
{
insert: ' ',
attributes: {
latex: 'bbb',
},
},
{
insert: 'cc',
},
]);
await undoByKeyboard(page);
await assertRichTextInlineDeltas(page, [
{
insert: 'aa',
},
{
insert: ' ',
attributes: {
latex: 'bbb',
},
},
]);
await redoByKeyboard(page);
await assertRichTextInlineDeltas(page, [
{
insert: 'aa',
},
{
insert: ' ',
attributes: {
latex: 'bbb',
},
},
{
insert: 'cc',
},
]);
});