mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-21 03:03:15 +08:00
**Problem:** When a language was selected in a code block's language
dropdown, there was no way to reset it back to "Plain Text" (no syntax
highlighting). The dropdown only contained shiki languages, and the null
value representing Plain Text had no corresponding selectable option.
**Solution:** Added a "Plain Text" entry as the first item in the
language selector dropdown. This entry:
- Maps to language: null when selected
- Shows a checkmark when Plain Text is active
- Persists in localStorage alongside recently used languages
- Is searchable via aliases ("plain", "text", "none")
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added Plain Text as a selectable code block language.
* Plain Text is available in newly generated and previously saved
language lists.
* Code blocks correctly show Plain Text when no language is selected.
* Recognizes common names including “plain,” “text,” “plaintext,” “txt,”
and “none.”
* **Tests**
* Added coverage for switching between Rust and Plain Text.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
136 lines
3.9 KiB
TypeScript
136 lines
3.9 KiB
TypeScript
import { test } from '@affine-test/kit/playwright';
|
|
import { openHomePage } from '@affine-test/kit/utils/load-page';
|
|
import { type, waitForEditorLoad } from '@affine-test/kit/utils/page-logic';
|
|
import { expect } from '@playwright/test';
|
|
|
|
import {
|
|
createNewPage,
|
|
gotoContentFromTitle,
|
|
initCodeBlockByOneStep,
|
|
} from './utils';
|
|
|
|
test.describe('Code Block Autocomplete Operations', () => {
|
|
test('angle brackets are not supported', async ({ page }) => {
|
|
// open the home page and insert the code block
|
|
await initCodeBlockByOneStep(page);
|
|
await page.keyboard.type('<');
|
|
const codeUnit = page.locator('affine-code-unit');
|
|
await expect(codeUnit).toHaveText('<');
|
|
});
|
|
});
|
|
|
|
test.describe('Code Block Language Selector', () => {
|
|
test('switch language and back to plain text', async ({ page }) => {
|
|
await initCodeBlockByOneStep(page);
|
|
const code = page.locator('affine-code');
|
|
|
|
await code.hover({
|
|
position: { x: 155, y: 65 },
|
|
});
|
|
await page.getByTestId('lang-button').click();
|
|
await page.getByRole('button', { name: 'Rust' }).click();
|
|
|
|
await expect(page.getByTestId('lang-button')).toHaveText('Rust');
|
|
|
|
await page.getByTestId('lang-button').click();
|
|
await page.getByRole('button', { name: 'Plain Text' }).click();
|
|
|
|
await expect(page.getByTestId('lang-button')).toHaveText('Plain Text');
|
|
});
|
|
});
|
|
|
|
test.describe('Code Block Preview', () => {
|
|
test('enable html preview', async ({ page }) => {
|
|
const code = page.locator('affine-code');
|
|
|
|
await openHomePage(page);
|
|
await createNewPage(page);
|
|
await waitForEditorLoad(page);
|
|
await gotoContentFromTitle(page);
|
|
await type(page, '```html aaa');
|
|
await code.hover({
|
|
position: {
|
|
x: 155,
|
|
y: 65,
|
|
},
|
|
});
|
|
await page.getByText('Preview').click();
|
|
await expect(
|
|
page
|
|
.locator('iframe[title="HTML Preview"]')
|
|
.contentFrame()
|
|
.getByText('aaa')
|
|
).toBeVisible();
|
|
});
|
|
|
|
test('enable mermaid preview', async ({ page }) => {
|
|
const code = page.locator('affine-code');
|
|
const mermaidSvg = page.locator('mermaid-preview .mermaid-preview-svg svg');
|
|
|
|
await openHomePage(page);
|
|
await createNewPage(page);
|
|
await waitForEditorLoad(page);
|
|
await gotoContentFromTitle(page);
|
|
await type(page, '```mermaid graph TD;A-->B');
|
|
await code.hover({
|
|
position: {
|
|
x: 155,
|
|
y: 65,
|
|
},
|
|
});
|
|
await page.getByText('Preview').click();
|
|
await expect(mermaidSvg).toBeVisible();
|
|
});
|
|
|
|
test('enable typst preview', async ({ page }) => {
|
|
const code = page.locator('affine-code');
|
|
const typstPreview = page.locator('typst-preview');
|
|
|
|
await openHomePage(page);
|
|
await createNewPage(page);
|
|
await waitForEditorLoad(page);
|
|
await gotoContentFromTitle(page);
|
|
await type(page, '```typst = Title');
|
|
await code.hover({
|
|
position: {
|
|
x: 155,
|
|
y: 65,
|
|
},
|
|
});
|
|
await page.getByText('Preview').click();
|
|
await expect(typstPreview).toBeVisible();
|
|
});
|
|
|
|
test('change lang without preview', async ({ page }) => {
|
|
const code = page.locator('affine-code');
|
|
const preview = page.locator('affine-code .affine-code-block-preview');
|
|
|
|
await openHomePage(page);
|
|
await createNewPage(page);
|
|
await waitForEditorLoad(page);
|
|
await gotoContentFromTitle(page);
|
|
await type(page, '```html aaa');
|
|
|
|
await code.hover({
|
|
position: {
|
|
x: 155,
|
|
y: 65,
|
|
},
|
|
});
|
|
await page.getByText('Preview').click();
|
|
await expect(preview).toBeVisible();
|
|
|
|
// change to lang without preview support
|
|
await page.getByTestId('lang-button').click();
|
|
await page.getByRole('button', { name: 'ABAP' }).click();
|
|
|
|
await expect(preview).toBeHidden();
|
|
|
|
// change back to html
|
|
await page.getByTestId('lang-button').click();
|
|
await page.getByRole('button', { name: 'HTML', exact: true }).click();
|
|
|
|
await expect(preview).toBeVisible();
|
|
});
|
|
});
|