mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
fix(component): toast too many times when switch page mode (#2296)
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import { useAFFiNEI18N } from '@affine/i18n/hooks';
|
||||||
import { assertExists } from '@blocksuite/store';
|
import { assertExists } from '@blocksuite/store';
|
||||||
import { useBlockSuitePageMeta } from '@toeverything/hooks/use-block-suite-page-meta';
|
import { useBlockSuitePageMeta } from '@toeverything/hooks/use-block-suite-page-meta';
|
||||||
import { useAtomValue, useSetAtom } from 'jotai';
|
import { useAtomValue, useSetAtom } from 'jotai';
|
||||||
@@ -27,6 +28,7 @@ export const EditorModeSwitch = ({
|
|||||||
const pageMeta = useBlockSuitePageMeta(blockSuiteWorkspace).find(
|
const pageMeta = useBlockSuitePageMeta(blockSuiteWorkspace).find(
|
||||||
meta => meta.id === pageId
|
meta => meta.id === pageId
|
||||||
);
|
);
|
||||||
|
const t = useAFFiNEI18N();
|
||||||
assertExists(pageMeta);
|
assertExists(pageMeta);
|
||||||
const { trash } = pageMeta;
|
const { trash } = pageMeta;
|
||||||
|
|
||||||
@@ -41,8 +43,12 @@ export const EditorModeSwitch = ({
|
|||||||
active={currentMode === 'page'}
|
active={currentMode === 'page'}
|
||||||
hide={trash && currentMode !== 'page'}
|
hide={trash && currentMode !== 'page'}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setMode(mode => ({ ...mode, [pageMeta.id]: 'page' }));
|
setMode(mode => {
|
||||||
toast('Page mode');
|
if (mode[pageMeta.id] !== 'page') {
|
||||||
|
toast(t['com.affine.pageMode']());
|
||||||
|
}
|
||||||
|
return { ...mode, [pageMeta.id]: 'page' };
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<EdgelessSwitchItem
|
<EdgelessSwitchItem
|
||||||
@@ -50,8 +56,12 @@ export const EditorModeSwitch = ({
|
|||||||
active={currentMode === 'edgeless'}
|
active={currentMode === 'edgeless'}
|
||||||
hide={trash && currentMode !== 'edgeless'}
|
hide={trash && currentMode !== 'edgeless'}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setMode(mode => ({ ...mode, [pageMeta.id]: 'edgeless' }));
|
setMode(mode => {
|
||||||
toast('Edgeless mode');
|
if (mode[pageMeta.id] !== 'edgeless') {
|
||||||
|
toast(t['com.affine.edgelessMode']());
|
||||||
|
}
|
||||||
|
return { ...mode, [pageMeta.id]: 'edgeless' };
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</StyledEditorModeSwitch>
|
</StyledEditorModeSwitch>
|
||||||
|
|||||||
@@ -47,3 +47,15 @@ export function useOnTransformWorkspace() {
|
|||||||
[setUser, transformWorkspace]
|
[setUser, transformWorkspace]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// global Events
|
||||||
|
interface WindowEventMap {
|
||||||
|
'affine-workspace:transform': CustomEvent<{
|
||||||
|
from: WorkspaceFlavour;
|
||||||
|
to: WorkspaceFlavour;
|
||||||
|
oldId: string;
|
||||||
|
newId: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,8 +9,20 @@ export const toast = (message: string, options?: ToastOptions) => {
|
|||||||
'.main-container'
|
'.main-container'
|
||||||
) as HTMLElement;
|
) as HTMLElement;
|
||||||
logger.debug(`toast with message: "${message}"`, options);
|
logger.debug(`toast with message: "${message}"`, options);
|
||||||
|
window.dispatchEvent(
|
||||||
|
new CustomEvent('affine-toast:emit', { detail: message })
|
||||||
|
);
|
||||||
return basicToast(message, {
|
return basicToast(message, {
|
||||||
portal: mainContainer || document.body,
|
portal: mainContainer || document.body,
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// global Events
|
||||||
|
interface WindowEventMap {
|
||||||
|
'affine-toast:emit': CustomEvent<{
|
||||||
|
message: string;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -265,5 +265,7 @@
|
|||||||
"Default db location hint": "By default will be saved to {{location}}",
|
"Default db location hint": "By default will be saved to {{location}}",
|
||||||
"light": "light",
|
"light": "light",
|
||||||
"dark": "dark",
|
"dark": "dark",
|
||||||
"system": "system"
|
"system": "system",
|
||||||
|
"com.affine.pageMode": "Page Mode",
|
||||||
|
"com.affine.edgelessMode": "Edgeless Mode"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,26 @@ import { openHomePage } from '../libs/load-page';
|
|||||||
import { clickPageMoreActions, waitMarkdownImported } from '../libs/page-logic';
|
import { clickPageMoreActions, waitMarkdownImported } from '../libs/page-logic';
|
||||||
|
|
||||||
test('Switch to edgeless by switch edgeless item', async ({ page }) => {
|
test('Switch to edgeless by switch edgeless item', async ({ page }) => {
|
||||||
|
async function getCount(): Promise<number> {
|
||||||
|
return page.evaluate(() => {
|
||||||
|
return globalThis.__toastCount;
|
||||||
|
});
|
||||||
|
}
|
||||||
await openHomePage(page);
|
await openHomePage(page);
|
||||||
await waitMarkdownImported(page);
|
await waitMarkdownImported(page);
|
||||||
const btn = await page.getByTestId('switch-edgeless-mode-button');
|
const btn = await page.getByTestId('switch-edgeless-mode-button');
|
||||||
|
await page.evaluate(() => {
|
||||||
|
globalThis.__toastCount = 0;
|
||||||
|
window.addEventListener('affine-toast:emit', () => {
|
||||||
|
globalThis.__toastCount++;
|
||||||
|
});
|
||||||
|
});
|
||||||
await btn.click();
|
await btn.click();
|
||||||
await page.waitForTimeout(100);
|
await page.waitForTimeout(100);
|
||||||
|
{
|
||||||
|
const count = await getCount();
|
||||||
|
expect(count).toBe(1);
|
||||||
|
}
|
||||||
const edgeless = page.locator('affine-edgeless-page');
|
const edgeless = page.locator('affine-edgeless-page');
|
||||||
expect(await edgeless.isVisible()).toBe(true);
|
expect(await edgeless.isVisible()).toBe(true);
|
||||||
|
|
||||||
@@ -19,6 +34,18 @@ test('Switch to edgeless by switch edgeless item', async ({ page }) => {
|
|||||||
return window.getComputedStyle(element).getPropertyValue('padding');
|
return window.getComputedStyle(element).getPropertyValue('padding');
|
||||||
});
|
});
|
||||||
expect(editorWrapperPadding).toBe('0px');
|
expect(editorWrapperPadding).toBe('0px');
|
||||||
|
{
|
||||||
|
const count = await getCount();
|
||||||
|
expect(count).toBe(1);
|
||||||
|
}
|
||||||
|
await btn.click();
|
||||||
|
await btn.click();
|
||||||
|
await btn.click();
|
||||||
|
await page.waitForTimeout(100);
|
||||||
|
{
|
||||||
|
const count = await getCount();
|
||||||
|
expect(count).toBe(1);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Convert to edgeless by editor header items', async ({ page }) => {
|
test('Convert to edgeless by editor header items', async ({ page }) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user