2022-10-12 18:44:11 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-11-03 19:00:17 +08:00
|
|
|
import {
|
|
|
|
LogoIcon,
|
|
|
|
MoreIcon,
|
|
|
|
ExportIcon,
|
|
|
|
Export2Markdown,
|
|
|
|
Export2HTML,
|
|
|
|
RightArrow,
|
|
|
|
} from './icons';
|
2022-10-13 18:04:06 +08:00
|
|
|
import {
|
|
|
|
StyledHeader,
|
|
|
|
StyledTitle,
|
|
|
|
StyledTitleWrapper,
|
|
|
|
StyledLogo,
|
|
|
|
StyledHeaderRightSide,
|
2022-10-18 00:28:39 +08:00
|
|
|
IconButton,
|
2022-10-31 11:52:21 +08:00
|
|
|
StyledHeaderContainer,
|
|
|
|
StyledBrowserWarning,
|
|
|
|
StyledCloseButton,
|
2022-11-03 19:00:17 +08:00
|
|
|
StyledMenuItemWrapper,
|
2022-10-13 18:04:06 +08:00
|
|
|
} from './styles';
|
|
|
|
import { useEditor } from '@/components/editor-provider';
|
2022-10-18 00:28:39 +08:00
|
|
|
import EditorModeSwitch from '@/components/editor-mode-switch';
|
2022-10-18 00:43:04 +08:00
|
|
|
import { EdgelessIcon, PaperIcon } from '../editor-mode-switch/icons';
|
2022-10-18 11:55:10 +08:00
|
|
|
import ThemeModeSwitch from '@/components/theme-mode-switch';
|
2022-10-20 14:43:14 +08:00
|
|
|
import { useModal } from '@/components/global-modal-provider';
|
2022-10-31 11:52:21 +08:00
|
|
|
import CloseIcon from '@mui/icons-material/Close';
|
|
|
|
import { getWarningMessage, shouldShowWarning } from './utils';
|
2022-11-03 19:00:17 +08:00
|
|
|
import { Menu, MenuItem } from '@/ui/menu';
|
|
|
|
|
2022-10-13 18:04:06 +08:00
|
|
|
const PopoverContent = () => {
|
2022-10-18 00:43:04 +08:00
|
|
|
const { editor, mode, setMode } = useEditor();
|
2022-10-12 18:44:11 +08:00
|
|
|
return (
|
2022-10-18 00:43:04 +08:00
|
|
|
<>
|
2022-11-03 19:00:17 +08:00
|
|
|
<MenuItem
|
2022-10-18 00:43:04 +08:00
|
|
|
onClick={() => {
|
|
|
|
setMode(mode === 'page' ? 'edgeless' : 'page');
|
|
|
|
}}
|
|
|
|
>
|
2022-11-03 19:00:17 +08:00
|
|
|
<StyledMenuItemWrapper>
|
|
|
|
{mode === 'page' ? <EdgelessIcon /> : <PaperIcon />}
|
|
|
|
Convert to {mode === 'page' ? 'Edgeless' : 'Page'}
|
|
|
|
</StyledMenuItemWrapper>
|
|
|
|
</MenuItem>
|
|
|
|
<Menu
|
|
|
|
placement="left-start"
|
|
|
|
content={
|
|
|
|
<>
|
|
|
|
<MenuItem
|
|
|
|
onClick={() => {
|
|
|
|
editor && editor.contentParser.onExportHtml();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<StyledMenuItemWrapper>
|
|
|
|
<Export2HTML />
|
|
|
|
Export to HTML
|
|
|
|
</StyledMenuItemWrapper>
|
|
|
|
</MenuItem>
|
|
|
|
<MenuItem
|
|
|
|
onClick={() => {
|
|
|
|
editor && editor.contentParser.onExportMarkdown();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<StyledMenuItemWrapper>
|
|
|
|
<Export2Markdown />
|
|
|
|
Export to Markdown
|
|
|
|
</StyledMenuItemWrapper>
|
|
|
|
</MenuItem>
|
|
|
|
</>
|
|
|
|
}
|
2022-10-13 18:04:06 +08:00
|
|
|
>
|
2022-11-03 19:00:17 +08:00
|
|
|
<MenuItem>
|
|
|
|
<StyledMenuItemWrapper>
|
|
|
|
<ExportIcon />
|
|
|
|
Export
|
|
|
|
<RightArrow />
|
|
|
|
</StyledMenuItemWrapper>
|
|
|
|
</MenuItem>
|
|
|
|
</Menu>
|
2022-10-18 00:43:04 +08:00
|
|
|
</>
|
2022-10-12 18:44:11 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-31 11:52:21 +08:00
|
|
|
const BrowserWarning = ({ onClose }: { onClose: () => void }) => {
|
|
|
|
return (
|
|
|
|
<StyledBrowserWarning>
|
|
|
|
{getWarningMessage()}
|
|
|
|
<StyledCloseButton onClick={onClose}>
|
|
|
|
<CloseIcon />
|
|
|
|
</StyledCloseButton>
|
|
|
|
</StyledBrowserWarning>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
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-17 12:55:34 +08:00
|
|
|
const [isHover, setIsHover] = useState(false);
|
2022-10-31 15:18:22 +08:00
|
|
|
const [showWarning, setShowWarning] = useState(shouldShowWarning());
|
|
|
|
|
2022-10-20 14:43:14 +08:00
|
|
|
const { contactModalHandler } = useModal();
|
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 (
|
2022-12-19 16:43:53 +08:00
|
|
|
<StyledHeaderContainer hasWarning={showWarning} data-tauri-drag-region>
|
2022-10-31 11:52:21 +08:00
|
|
|
<BrowserWarning
|
|
|
|
onClose={() => {
|
|
|
|
setShowWarning(false);
|
|
|
|
}}
|
|
|
|
/>
|
2022-12-19 23:56:26 +08:00
|
|
|
<StyledHeader hasWarning={showWarning} data-tauri-drag-region>
|
2022-10-21 15:19:39 +08:00
|
|
|
<StyledLogo
|
2022-11-28 13:17:34 +00:00
|
|
|
data-testid="left-top-corner-logo"
|
2022-10-21 15:19:39 +08:00
|
|
|
onClick={() => {
|
|
|
|
contactModalHandler(true);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<LogoIcon />
|
2022-10-19 11:51:01 +08:00
|
|
|
</StyledLogo>
|
2022-10-21 17:46:45 +08:00
|
|
|
{title ? (
|
|
|
|
<StyledTitle
|
2022-12-19 23:56:26 +08:00
|
|
|
data-tauri-drag-region
|
2022-10-21 17:46:45 +08:00
|
|
|
onMouseEnter={() => {
|
|
|
|
setIsHover(true);
|
2022-10-19 11:51:01 +08:00
|
|
|
}}
|
2022-10-21 17:46:45 +08:00
|
|
|
onMouseLeave={() => {
|
|
|
|
setIsHover(false);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<EditorModeSwitch
|
|
|
|
isHover={isHover}
|
|
|
|
style={{
|
|
|
|
marginRight: '12px',
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<StyledTitleWrapper>{title}</StyledTitleWrapper>
|
|
|
|
</StyledTitle>
|
|
|
|
) : null}
|
2022-10-19 11:51:01 +08:00
|
|
|
|
|
|
|
<StyledHeaderRightSide>
|
|
|
|
<ThemeModeSwitch />
|
2022-11-03 19:00:17 +08:00
|
|
|
<Menu content={<PopoverContent />} placement="bottom-end">
|
2022-10-19 11:51:01 +08:00
|
|
|
<IconButton>
|
|
|
|
<MoreIcon />
|
|
|
|
</IconButton>
|
2022-11-03 19:00:17 +08:00
|
|
|
</Menu>
|
2022-10-19 11:51:01 +08:00
|
|
|
</StyledHeaderRightSide>
|
|
|
|
</StyledHeader>
|
2022-10-31 11:52:21 +08:00
|
|
|
</StyledHeaderContainer>
|
2022-10-12 18:44:11 +08:00
|
|
|
);
|
2022-10-12 10:14:58 +08:00
|
|
|
};
|