import React, { FC, ReactElement, useCallback, useState, useEffect, useMemo } from 'react' import { NavBarContainer } from './style' import { BoldOutlined, ItalicOutlined, StrikethroughOutlined, FontSizeOutlined, UnorderedListOutlined, OrderedListOutlined, LinkOutlined, PictureOutlined, TableOutlined, CodeOutlined, EllipsisOutlined, BulbOutlined, ExpandOutlined, CompressOutlined } from '@ant-design/icons' import { message, Tooltip, Menu, Dropdown } from 'antd' import { handleText, addTitle, addList, addLink, addPhoto, addTable, addCode } from './utils' import { INavProps } from './type' const { Item, ItemGroup } = Menu const codeThemeList = ['default','hybrid', 'dark', 'github', 'atelier-lakeside-dark', 'color-brewer', 'docco', 'mono-blue', 'paraiso-dark'] // const markdownThemeList = ['maize', 'guthub'] let style: any; const NavBar: FC = ({ setValue, editorElement, setLoading, preview, setPreview }): ReactElement => { const [codeTheme, setCodeTheme] = useState('hybrid') useEffect(() => { let head = document.head let oldLink = head.getElementsByClassName('markdownTheme-style-link') if(oldLink.length) head.removeChild(oldLink[0]) let newLink = document.createElement('link') newLink.setAttribute('rel', 'stylesheet') newLink.setAttribute('type', 'text/css') newLink.setAttribute('class', 'markdownTheme-style-link') newLink.setAttribute('href', `http://cpyfiles.dabanheiji.com/github-markdown.css`) newLink.onload = () => setLoading(false); newLink.onerror = () => { setLoading(false); message.error('主题获取失败,请稍后重试或尝试其它主题') } head.appendChild(newLink) }, []) useEffect(()=>{ setLoading(true) let head = document.head; let oldLink = head.getElementsByClassName('highlightjs-style-link') if(oldLink.length) head.removeChild(oldLink[0]) let newLink = document.createElement('link') newLink.setAttribute('rel', 'stylesheet') newLink.setAttribute('type', 'text/css') newLink.setAttribute('class', 'highlightjs-style-link') newLink.setAttribute('href', `https://cdn.bootcss.com/highlight.js/9.6.0/styles/${codeTheme}.min.css`) newLink.onload = () => setLoading(false); newLink.onerror = () => { setLoading(false); message.error('主题获取失败,请稍后重试或尝试其它主题') } head.appendChild(newLink) style && head.removeChild(style); let color: string = `#1d1f21` if( codeTheme === 'default' || codeTheme === 'github' || codeTheme === 'color-brewer' || codeTheme === 'docco' || codeTheme === 'mono-blue' ) { color = `#f6f8fa` }; style = document.createElement('style') style.textContent = ` .markdown-body .highlight pre, .markdown-body pre{ background-color: ${color} !important; } .spining .ant-spin-container{ height: 100%; } .code-highlight-theme-menu .active, .markdown-theme-menu .active { background-color: rgb(225, 248, 222); border-radius: 3px; } ` head.appendChild(style) }, [codeTheme]) const TitleMenu = ( addTitle(editorElement, a.key, a.domEvent.target.innerText, setValue)}> 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 ) const CodeMenu = ( setCodeTheme(e.key)}> { codeThemeList.map((item, index) => { return ( {item} ) }) } ) return ( handleText(editorElement, '**', '加粗文本', setValue)} /> handleText(editorElement, '*', '斜体文本', setValue)} /> handleText(editorElement, '~~', '中划线', setValue)} /> addList(editorElement, '- ', setValue)} /> addList(editorElement, '1. ', setValue)} /> {/* */} addLink(editorElement, setValue)} /> addPhoto(editorElement, setValue)} /> addTable(editorElement, setValue)} /> addCode(editorElement, setValue)} /> {false && }
{ preview ? setPreview(false)} /> : setPreview(true)} /> }
) } export default NavBar