Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions blocksuite/affine/blocks/code/src/code-toolbar/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ export const toggleGroup: MenuItemGroup<CodeBlockToolbarContext> = {
<span class="label">${label}</span>
<toggle-switch
style="margin-left: auto;"
.on="${wrapped}"
.on=${wrapped}
.onChange=${(on: boolean) => blockComponent.setWrap(on)}
></toggle-switch>
</editor-menu-action>
`;
Expand Down Expand Up @@ -249,7 +250,12 @@ export const toggleGroup: MenuItemGroup<CodeBlockToolbarContext> = {
<span class="label">${label}</span>
<toggle-switch
style="margin-left: auto;"
.on="${lineNumber}"
.on=${lineNumber}
.onChange=${(on: boolean) => {
blockComponent.store.updateBlock(blockComponent.model, {
lineNumber: on,
});
}}
></toggle-switch>
</editor-menu-action>
`;
Expand Down
11 changes: 7 additions & 4 deletions blocksuite/affine/components/src/toggle-switch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,23 @@ const styles = css`
export class ToggleSwitch extends LitElement {
static override styles = styles;

private _toggleSwitch() {
this.on = !this.on;
private _toggleSwitch(event: Event) {
this.on = (event.target as HTMLInputElement).checked;
if (this.onChange) {
this.onChange(this.on);
}
}

override render() {
return html`
<label class=${this.on ? 'on' : ''}>
<label
class=${this.on ? 'on' : ''}
@click=${(event: MouseEvent) => event.stopPropagation()}
>
<input
type="checkbox"
class="switch"
?checked=${this.on}
.checked=${this.on}
@change=${this._toggleSwitch}
/>
</label>
Expand Down
78 changes: 62 additions & 16 deletions tests/blocksuite/e2e/code/crud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,16 +374,38 @@ test('toggle code block wrap can work in the same more menu', async ({
await codeBlockController.codeBlock.hover();
const moreMenu = await codeBlockController.openMore();

await moreMenu.wrapButton.click();

await expect(moreMenu.menu).toBeVisible();
await expect(codeBlockContainer).toHaveClass(/wrap/);
await expect(moreMenu.cancelWrapButton).toBeVisible();
for (const selector of [
'.label',
'toggle-switch label',
'toggle-switch input',
':scope',
]) {
const wrap = moreMenu.wrapButton.locator(selector);
if (selector.endsWith('input')) {
await wrap.evaluate((input: HTMLInputElement) => input.click());
} else {
await wrap.click(
selector === ':scope' ? { position: { x: 1, y: 1 } } : {}
);
}

await moreMenu.cancelWrapButton.click();
await expect(moreMenu.menu).toBeVisible();
await expect(codeBlockContainer).toHaveClass(/wrap/);
await expect(moreMenu.cancelWrapButton.locator('input')).toBeChecked();

const cancelWrap = moreMenu.cancelWrapButton.locator(selector);
if (selector.endsWith('input')) {
await cancelWrap.evaluate((input: HTMLInputElement) => input.click());
} else {
await cancelWrap.click(
selector === ':scope' ? { position: { x: 1, y: 1 } } : {}
);
}

await expect(moreMenu.menu).toBeVisible();
await expect(codeBlockContainer).not.toHaveClass(/wrap/);
await expect(moreMenu.menu).toBeVisible();
await expect(codeBlockContainer).not.toHaveClass(/wrap/);
await expect(moreMenu.wrapButton.locator('input')).not.toBeChecked();
}
});

test('add caption works', async ({ page }, testInfo) => {
Expand Down Expand Up @@ -471,16 +493,40 @@ test('toggle code block line number can work in the same more menu', async ({
await codeBlockController.codeBlock.hover();
const moreMenu = await codeBlockController.openMore();

await moreMenu.cancelLineNumberButton.click();

await expect(moreMenu.menu).toBeVisible();
await expect(lineNumber).toBeHidden();
await expect(moreMenu.lineNumberButton).toBeVisible();
for (const selector of [
'.label',
'toggle-switch label',
'toggle-switch input',
':scope',
]) {
const hide = moreMenu.cancelLineNumberButton.locator(selector);
if (selector.endsWith('input')) {
await hide.evaluate((input: HTMLInputElement) => input.click());
} else {
await hide.click(
selector === ':scope' ? { position: { x: 1, y: 1 } } : {}
);
}

await moreMenu.lineNumberButton.click();
await expect(moreMenu.menu).toBeVisible();
await expect(lineNumber).toBeHidden();
await expect(moreMenu.lineNumberButton.locator('input')).not.toBeChecked();

const show = moreMenu.lineNumberButton.locator(selector);
if (selector.endsWith('input')) {
await show.evaluate((input: HTMLInputElement) => input.click());
} else {
await show.click(
selector === ':scope' ? { position: { x: 1, y: 1 } } : {}
);
}

await expect(moreMenu.menu).toBeVisible();
await expect(lineNumber).toBeVisible();
await expect(moreMenu.menu).toBeVisible();
await expect(lineNumber).toBeVisible();
await expect(
moreMenu.cancelLineNumberButton.locator('input')
).toBeChecked();
}
});

test('code block toolbar widget can appear and disappear during mousemove', async ({
Expand Down