Не подтверждена Коммит 7c76c25a создал по автору EYHN's avatar EYHN
Просмотр файлов

refactor(core): new back&forward button base on workbench (#6012)

# feature:

## In Browser:
- hidden back&forward button in sidebar.
- back and forward is equal with `window.history.back()` `window.history.forward()`

## In Desktop:
- Back and forward can be controlled through the sidebar, cmdk, and shortcut keys.
- back and forward act on the currently **active** view.
- buttons change disable&enable style based on current active view history

# Refactor:

Move app-sidebar and app-container from @affine/component to @affine/core
владелец b06aeb22
...@@ -182,7 +182,7 @@ export class LiveData<T = unknown> implements InteropObservable<T> { ...@@ -182,7 +182,7 @@ export class LiveData<T = unknown> implements InteropObservable<T> {
} }
subscribe( subscribe(
observer: Partial<Observer<T>> | ((value: T) => void) | undefined observer?: Partial<Observer<T>> | ((value: T) => void) | undefined
): Subscription { ): Subscription {
this.ops.next('watch'); this.ops.next('watch');
const subscription = this.raw.subscribe(observer); const subscription = this.raw.subscribe(observer);
......
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { ArrowLeftSmallIcon, ArrowRightSmallIcon } from '@blocksuite/icons';
import { useAtomValue } from 'jotai';
import { useMemo } from 'react';
import { IconButton } from '../../../ui/button';
import { Tooltip } from '../../../ui/tooltip';
import type { History } from '..';
import {
navHeaderButton,
navHeaderNavigationButtons,
navHeaderStyle,
} from '../index.css';
import { appSidebarOpenAtom } from '../index.jotai';
import { SidebarSwitch } from './sidebar-switch';
export type SidebarHeaderProps = {
router?: {
back: () => unknown;
forward: () => unknown;
history: History;
};
generalShortcutsInfo?: {
shortcuts: {
[title: string]: string[];
};
};
};
export const SidebarHeader = (props: SidebarHeaderProps) => {
const open = useAtomValue(appSidebarOpenAtom);
const t = useAFFiNEI18N();
const shortcuts = props.generalShortcutsInfo?.shortcuts;
const shortcutsObject = useMemo(() => {
const goBack = t['com.affine.keyboardShortcuts.goBack']();
const goBackShortcut = shortcuts?.[goBack];
const goForward = t['com.affine.keyboardShortcuts.goForward']();
const goForwardShortcut = shortcuts?.[goForward];
return {
goBack,
goBackShortcut,
goForward,
goForwardShortcut,
};
}, [shortcuts, t]);
return (
<div
className={navHeaderStyle}
data-open={open}
data-is-macos-electron={environment.isDesktop && environment.isMacOs}
>
<SidebarSwitch show={open} />
<div className={navHeaderNavigationButtons}>
<Tooltip
content={`${shortcutsObject.goBack} ${shortcutsObject.goBackShortcut}`}
side="bottom"
>
<IconButton
className={navHeaderButton}
data-testid="app-sidebar-arrow-button-back"
disabled={props.router?.history.current === 0}
onClick={() => {
props.router?.back();
}}
>
<ArrowLeftSmallIcon />
</IconButton>
</Tooltip>
<Tooltip
content={`${shortcutsObject.goForward} ${shortcutsObject.goForwardShortcut}`}
side="bottom"
>
<IconButton
className={navHeaderButton}
data-testid="app-sidebar-arrow-button-forward"
disabled={
props.router
? (props.router.history.stack.length > 0 &&
props.router.history.current ===
props.router.history.stack.length - 1) ||
props.router.history.stack.length === 0
: true
}
onClick={() => {
props.router?.forward();
}}
>
<ArrowRightSmallIcon />
</IconButton>
</Tooltip>
</div>
</div>
);
};
export * from './sidebar-switch';
export * from './scrollable'; export * from './scrollable';
export * from './scrollbar'; export * from './scrollbar';
export * from './use-has-scroll-top';
...@@ -2,8 +2,8 @@ import * as ScrollArea from '@radix-ui/react-scroll-area'; ...@@ -2,8 +2,8 @@ import * as ScrollArea from '@radix-ui/react-scroll-area';
import clsx from 'clsx'; import clsx from 'clsx';
import { type PropsWithChildren, useRef } from 'react'; import { type PropsWithChildren, useRef } from 'react';
import { useHasScrollTop } from '../../components/app-sidebar/sidebar-containers/use-has-scroll-top';
import * as styles from './index.css'; import * as styles from './index.css';
import { useHasScrollTop } from './use-has-scroll-top';
export type ScrollableContainerProps = { export type ScrollableContainerProps = {
showScrollTopBorder?: boolean; showScrollTopBorder?: boolean;
......
...@@ -4,7 +4,6 @@ import '@affine/component/theme/theme.css'; ...@@ -4,7 +4,6 @@ import '@affine/component/theme/theme.css';
import { AffineContext } from '@affine/component/context'; import { AffineContext } from '@affine/component/context';
import { GlobalLoading } from '@affine/component/global-loading'; import { GlobalLoading } from '@affine/component/global-loading';
import { NotificationCenter } from '@affine/component/notification-center'; import { NotificationCenter } from '@affine/component/notification-center';
import { WorkspaceFallback } from '@affine/component/workspace';
import { createI18n, setUpLanguage } from '@affine/i18n'; import { createI18n, setUpLanguage } from '@affine/i18n';
import { CacheProvider } from '@emotion/react'; import { CacheProvider } from '@emotion/react';
import { getCurrentStore } from '@toeverything/infra/atom'; import { getCurrentStore } from '@toeverything/infra/atom';
...@@ -13,6 +12,7 @@ import type { PropsWithChildren, ReactElement } from 'react'; ...@@ -13,6 +12,7 @@ import type { PropsWithChildren, ReactElement } from 'react';
import { lazy, memo, Suspense } from 'react'; import { lazy, memo, Suspense } from 'react';
import { RouterProvider } from 'react-router-dom'; import { RouterProvider } from 'react-router-dom';
import { WorkspaceFallback } from './components/workspace';
import { GlobalScopeProvider } from './modules/infra-web/global-scope'; import { GlobalScopeProvider } from './modules/infra-web/global-scope';
import { CloudSessionProvider } from './providers/session-provider'; import { CloudSessionProvider } from './providers/session-provider';
import { router } from './router'; import { router } from './router';
......
// these atoms cannot be moved to @affine/jotai since they use atoms from @affine/component // these atoms cannot be moved to @affine/jotai since they use atoms from @affine/component
import { appSidebarOpenAtom } from '@affine/component/app-sidebar';
import { atom } from 'jotai'; import { atom } from 'jotai';
import { atomWithStorage } from 'jotai/utils'; import { atomWithStorage } from 'jotai/utils';
import { appSidebarOpenAtom } from '../components/app-sidebar';
export type Guide = { export type Guide = {
// should show quick search tips // should show quick search tips
quickSearchTips: boolean; quickSearchTips: boolean;
......
import { useAtom } from 'jotai';
import { atomWithStorage, createJSONStorage } from 'jotai/utils';
import { useCallback } from 'react';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { useNavigate } from 'react-router-dom';
import { router } from '../router';
export type History = {
stack: string[];
current: number;
skip: boolean;
};
export const MAX_HISTORY = 50;
const historyBaseAtom = atomWithStorage<History>(
'router-history',
{
stack: [],
current: 0,
skip: false,
},
createJSONStorage(() => sessionStorage)
);
historyBaseAtom.onMount = set => {
const unsubscribe = router.subscribe(state => {
set(prev => {
const url = state.location.pathname;
// if stack top is the same as current, skip
if (prev.stack[prev.current] === url) {
return prev;
}
if (prev.skip) {
return {
stack: [...prev.stack],
current: prev.current,
skip: false,
};
} else {
if (prev.current < prev.stack.length - 1) {
const newStack = prev.stack.slice(0, prev.current);
newStack.push(url);
if (newStack.length > MAX_HISTORY) {
newStack.shift();
}
return {
stack: newStack,
current: newStack.length - 1,
skip: false,
};
} else {
const newStack = [...prev.stack, url];
if (newStack.length > MAX_HISTORY) {
newStack.shift();
}
return {
stack: newStack,
current: newStack.length - 1,
skip: false,
};
}
}
});
});
return () => {
unsubscribe();
};
};
export function useHistoryAtom() {
const navigate = useNavigate();
const [base, setBase] = useAtom(historyBaseAtom);
return [
base,
useCallback(
(forward: boolean) => {
setBase(prev => {
if (forward) {
const target = Math.min(prev.stack.length - 1, prev.current + 1);
const url = prev.stack[target];
navigate(url);
return {
...prev,
current: target,
skip: true,
};
} else {
const target = Math.max(0, prev.current - 1);
const url = prev.stack[target];
navigate(url);
return {
...prev,
current: target,
skip: true,
};
}
});
},
[setBase, navigate]
),
] as const;
}
import { appSidebarOpenAtom } from '@affine/component/app-sidebar';
import type { useAFFiNEI18N } from '@affine/i18n/hooks'; import type { useAFFiNEI18N } from '@affine/i18n/hooks';
import { SidebarIcon } from '@blocksuite/icons'; import { SidebarIcon } from '@blocksuite/icons';
import { registerAffineCommand } from '@toeverything/infra/command'; import { registerAffineCommand } from '@toeverything/infra/command';
import type { createStore } from 'jotai'; import type { createStore } from 'jotai';
import { appSidebarOpenAtom } from '../components/app-sidebar';
export function registerAffineLayoutCommands({ export function registerAffineLayoutCommands({
t, t,
store, store,
......
import { useAppSettingHelper } from '../../hooks/affine/use-app-setting-helper';
import { import {
AppContainer as AppContainerWithoutSettings, AppContainer as AppContainerWithoutSettings,
type WorkspaceRootProps, type WorkspaceRootProps,
} from '@affine/component/workspace'; } from '../workspace';
import { useAppSettingHelper } from '../../hooks/affine/use-app-setting-helper';
export const AppContainer = (props: WorkspaceRootProps) => { export const AppContainer = (props: WorkspaceRootProps) => {
const { appSettings } = useAppSettingHelper(); const { appSettings } = useAppSettingHelper();
......
import { ToolContainer } from '@affine/component/workspace';
import { HelpIsland } from '../../pure/help-island'; import { HelpIsland } from '../../pure/help-island';
import { ToolContainer } from '../../workspace';
export const HubIsland = () => { export const HubIsland = () => {
return ( return (
......
import { Tooltip } from '@affine/component';
import { Unreachable } from '@affine/env/constant'; import { Unreachable } from '@affine/env/constant';
import { useAFFiNEI18N } from '@affine/i18n/hooks'; import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { CloseIcon, NewIcon, ResetIcon } from '@blocksuite/icons'; import { CloseIcon, NewIcon, ResetIcon } from '@blocksuite/icons';
import clsx from 'clsx'; import clsx from 'clsx';
import { useCallback, useMemo } from 'react'; import { useCallback, useMemo } from 'react';
import { Tooltip } from '../../../ui/tooltip';
import * as styles from './index.css'; import * as styles from './index.css';
export interface AddPageButtonProps { export interface AddPageButtonProps {
......
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать