2022-10-12 18:44:11 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-10-13 18:04:06 +08:00
|
|
|
import {
|
|
|
|
LogoIcon,
|
|
|
|
PaperIcon,
|
|
|
|
EdgelessIcon,
|
|
|
|
SunIcon,
|
|
|
|
MoonIcon,
|
|
|
|
MoreIcon,
|
|
|
|
ExportIcon,
|
|
|
|
} from './icons';
|
|
|
|
import {
|
|
|
|
StyledHeader,
|
|
|
|
StyledTitle,
|
|
|
|
StyledTitleWrapper,
|
|
|
|
StyledLogo,
|
|
|
|
StyledModeSwitch,
|
|
|
|
StyledHeaderRightSide,
|
|
|
|
StyledMoreMenuItem,
|
|
|
|
} from './styles';
|
|
|
|
import { Popover } from '@/components/popover';
|
|
|
|
import { useTheme } from '@/styles';
|
|
|
|
import { useEditor } from '@/components/editor-provider';
|
2022-10-12 10:14:58 +08:00
|
|
|
|
2022-10-13 18:04:06 +08:00
|
|
|
const EditorModeSwitch = () => {
|
2022-10-12 18:44:11 +08:00
|
|
|
const [mode, setMode] = useState<'page' | 'edgeless'>('page');
|
|
|
|
|
|
|
|
const handleModeSwitch = (mode: 'page' | 'edgeless') => {
|
|
|
|
const event = new CustomEvent('affine.switch-mode', { detail: mode });
|
|
|
|
window.dispatchEvent(event);
|
|
|
|
|
|
|
|
setMode(mode);
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<StyledModeSwitch>
|
|
|
|
<PaperIcon
|
|
|
|
color={mode === 'page' ? '#6880FF' : '#a6abb7'}
|
|
|
|
onClick={() => {
|
|
|
|
handleModeSwitch('page');
|
|
|
|
}}
|
|
|
|
style={{ cursor: 'pointer' }}
|
|
|
|
></PaperIcon>
|
|
|
|
<EdgelessIcon
|
|
|
|
color={mode === 'edgeless' ? '#6880FF' : '#a6abb7'}
|
|
|
|
onClick={() => {
|
|
|
|
handleModeSwitch('edgeless');
|
|
|
|
}}
|
|
|
|
style={{ cursor: 'pointer' }}
|
|
|
|
></EdgelessIcon>
|
|
|
|
</StyledModeSwitch>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const DarkModeSwitch = () => {
|
2022-10-13 18:04:06 +08:00
|
|
|
const { changeMode, mode } = useTheme();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{mode === 'dark' ? (
|
|
|
|
<SunIcon
|
|
|
|
color="#9096A5"
|
|
|
|
style={{ cursor: 'pointer' }}
|
|
|
|
onClick={() => {
|
|
|
|
changeMode('light');
|
|
|
|
}}
|
|
|
|
></SunIcon>
|
|
|
|
) : (
|
|
|
|
<MoonIcon
|
|
|
|
color="#9096A5"
|
|
|
|
style={{ cursor: 'pointer' }}
|
|
|
|
onClick={() => {
|
|
|
|
changeMode('dark');
|
|
|
|
}}
|
|
|
|
></MoonIcon>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const PopoverContent = () => {
|
|
|
|
const { editor } = useEditor();
|
2022-10-12 18:44:11 +08:00
|
|
|
return (
|
|
|
|
<div>
|
2022-10-13 18:04:06 +08:00
|
|
|
<StyledMoreMenuItem
|
|
|
|
onClick={() => {
|
|
|
|
editor && editor.contentParser.onExportHtml();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ExportIcon />
|
|
|
|
Export to HTML
|
|
|
|
</StyledMoreMenuItem>
|
|
|
|
<StyledMoreMenuItem
|
|
|
|
onClick={() => {
|
|
|
|
editor && editor.contentParser.onExportMarkdown();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ExportIcon />
|
|
|
|
Export to markdown
|
|
|
|
</StyledMoreMenuItem>
|
2022-10-12 18:44:11 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-12 10:14:58 +08:00
|
|
|
export const Header = () => {
|
2022-10-12 18:44:11 +08:00
|
|
|
const [title, setTitle] = useState('');
|
2022-10-13 18:04:06 +08:00
|
|
|
const { editor } = useEditor();
|
2022-10-12 18:44:11 +08:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-10-13 18:04:06 +08:00
|
|
|
if (editor) {
|
2022-10-13 14:50:04 +08:00
|
|
|
setTitle(editor.model.title || '');
|
2022-10-12 18:44:11 +08:00
|
|
|
editor.model.propsUpdated.on(() => {
|
|
|
|
setTitle(editor.model.title);
|
|
|
|
});
|
2022-10-13 18:04:06 +08:00
|
|
|
}
|
|
|
|
}, [editor]);
|
2022-10-12 18:44:11 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<StyledHeader>
|
|
|
|
<StyledLogo>
|
|
|
|
<LogoIcon color={'#6880FF'} onClick={() => {}} />
|
|
|
|
</StyledLogo>
|
|
|
|
<StyledTitle>
|
2022-10-13 18:04:06 +08:00
|
|
|
<EditorModeSwitch />
|
2022-10-12 18:44:11 +08:00
|
|
|
<StyledTitleWrapper>{title}</StyledTitleWrapper>
|
|
|
|
</StyledTitle>
|
2022-10-13 18:04:06 +08:00
|
|
|
|
|
|
|
<StyledHeaderRightSide>
|
|
|
|
<DarkModeSwitch />
|
|
|
|
<Popover popoverContent={<PopoverContent />}>
|
|
|
|
<MoreIcon color="#9096A5" style={{ marginLeft: '20px' }} />
|
|
|
|
</Popover>
|
|
|
|
</StyledHeaderRightSide>
|
2022-10-12 18:44:11 +08:00
|
|
|
</StyledHeader>
|
|
|
|
);
|
2022-10-12 10:14:58 +08:00
|
|
|
};
|