|
| 1 | +import Comment from '@/components/Comment' |
| 2 | +import formatDate from '@/lib/formatDate' |
| 3 | +import { useGlobal } from '@/lib/global' |
| 4 | +import { faEye, faFolderOpen } from '@fortawesome/free-solid-svg-icons' |
| 5 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' |
| 6 | +import mediumZoom from 'medium-zoom' |
| 7 | +import Link from 'next/link' |
| 8 | +import 'prismjs' |
| 9 | +import 'prismjs/components/prism-bash' |
| 10 | +import 'prismjs/components/prism-javascript' |
| 11 | +import 'prismjs/components/prism-markup' |
| 12 | +import 'prismjs/components/prism-python' |
| 13 | +import 'prismjs/components/prism-typescript' |
| 14 | +import { useEffect, useRef } from 'react' |
| 15 | +import { Code, Collection, CollectionRow, Equation, NotionRenderer } from 'react-notion-x' |
| 16 | +import ArticleAround from './ArticleArount' |
| 17 | + |
| 18 | +/** |
| 19 | + * |
| 20 | + * @param {*} param0 |
| 21 | + * @returns |
| 22 | + */ |
| 23 | +export default function ArticleDetail ({ post, recommendPosts, prev, next }) { |
| 24 | + const { locale } = useGlobal() |
| 25 | + const date = formatDate(post?.date?.start_date || post.createdTime, locale.LOCALE) |
| 26 | + |
| 27 | + const zoom = typeof window !== 'undefined' && mediumZoom({ |
| 28 | + container: '.notion-viewport', |
| 29 | + background: 'rgba(0, 0, 0, 0.2)', |
| 30 | + margin: getMediumZoomMargin() |
| 31 | + }) |
| 32 | + const zoomRef = useRef(zoom ? zoom.clone() : null) |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + // 将所有container下的所有图片添加medium-zoom |
| 36 | + const container = document.getElementById('container') |
| 37 | + const imgList = container.getElementsByTagName('img') |
| 38 | + if (imgList && zoomRef.current) { |
| 39 | + for (let i = 0; i < imgList.length; i++) { |
| 40 | + (zoomRef.current).attach(imgList[i]) |
| 41 | + } |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + return (<div id="container" className=" max-w-4xl overflow-x-auto flex-grow mx-auto w-screen md:w-full "> |
| 46 | + <article itemScope itemType="https://schema.org/Movie" |
| 47 | + className="subpixel-antialiased py-10 px-5 lg:pt-24 md:px-24 dark:border-gray-700 bg-white dark:bg-gray-800" |
| 48 | + > |
| 49 | + |
| 50 | + <header className='animate__slideInDown animate__animated'> |
| 51 | + {post.type && !post.type.includes('Page') && post?.page_cover && ( |
| 52 | + <div className="w-full relative md:flex-shrink-0 overflow-hidden"> |
| 53 | + <img alt={post.title} src={post?.page_cover} className='object-center w-full' /> |
| 54 | + </div> |
| 55 | + )} |
| 56 | + |
| 57 | + {/* 文章Title */} |
| 58 | + <div className="font-bold text-3xl text-black dark:text-white font-serif pt-10"> |
| 59 | + {post.title} |
| 60 | + </div> |
| 61 | + |
| 62 | + <section className="flex-wrap flex mt-2 text-gray-400 dark:text-gray-400 font-light leading-8"> |
| 63 | + <div> |
| 64 | + <Link href={`/category/${post.category}`} passHref> |
| 65 | + <a className="cursor-pointer text-md mr-2 hover:text-black dark:hover:text-white border-b dark:border-gray-500 border-dashed"> |
| 66 | + <FontAwesomeIcon icon={faFolderOpen} className="mr-1" /> |
| 67 | + {post.category} |
| 68 | + </a> |
| 69 | + </Link> |
| 70 | + <span className='mr-2'>|</span> |
| 71 | + |
| 72 | + {post.type[0] !== 'Page' && (<> |
| 73 | + <Link |
| 74 | + href={`/archive#${post?.date?.start_date?.substr(0, 7)}`} |
| 75 | + passHref |
| 76 | + > |
| 77 | + <a className="pl-1 mr-2 cursor-pointer hover:text-gray-700 dark:hover:text-gray-200 border-b dark:border-gray-500 border-dashed"> |
| 78 | + {date} |
| 79 | + </a> |
| 80 | + </Link> |
| 81 | + <span className='mr-2'>|</span> |
| 82 | + </>)} |
| 83 | + |
| 84 | + <div className="hidden busuanzi_container_page_pv font-light mr-2"> |
| 85 | + <FontAwesomeIcon icon={faEye} className='mr-1'/> |
| 86 | + |
| 87 | + <span className="mr-2 busuanzi_value_page_pv"/> |
| 88 | + <span className='mr-2'>|</span> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + |
| 92 | + </section> |
| 93 | + |
| 94 | + </header> |
| 95 | + |
| 96 | + {/* Notion文章主体 */} |
| 97 | + <section id='notion-article' className='px-1'> |
| 98 | + {post.blockMap && ( |
| 99 | + <NotionRenderer |
| 100 | + recordMap={post.blockMap} |
| 101 | + mapPageUrl={mapPageUrl} |
| 102 | + components={{ |
| 103 | + equation: Equation, |
| 104 | + code: Code, |
| 105 | + collectionRow: CollectionRow, |
| 106 | + collection: Collection |
| 107 | + }} |
| 108 | + /> |
| 109 | + )} |
| 110 | + </section> |
| 111 | + |
| 112 | + <section className="px-1 py-2 my-1 text-sm font-light overflow-auto text-gray-600 dark:text-gray-400"> |
| 113 | + {/* 文章内嵌广告 */} |
| 114 | + <ins className="adsbygoogle" |
| 115 | + style={{ display: 'block', textAlign: 'center' }} |
| 116 | + data-adtest="on" |
| 117 | + data-ad-layout="in-article" |
| 118 | + data-ad-format="fluid" |
| 119 | + data-ad-client="ca-pub-2708419466378217" |
| 120 | + data-ad-slot="3806269138"/> |
| 121 | + </section> |
| 122 | + |
| 123 | + </article> |
| 124 | + |
| 125 | + <ArticleAround prev={prev} next={next}/> |
| 126 | + |
| 127 | + {/* 评论互动 */} |
| 128 | + <div className="duration-200 shadow px-12 w-screen md:w-full overflow-x-auto dark:border-gray-700 bg-white dark:bg-gray-800"> |
| 129 | + <div className='text-2xl mt-8 mx-8'>发表评论</div> |
| 130 | + <Comment frontMatter={post} /> |
| 131 | + </div> |
| 132 | + </div>) |
| 133 | +} |
| 134 | + |
| 135 | +const mapPageUrl = id => { |
| 136 | + return 'https://www.notion.so/' + id.replace(/-/g, '') |
| 137 | +} |
| 138 | + |
| 139 | +function getMediumZoomMargin () { |
| 140 | + const width = window.innerWidth |
| 141 | + |
| 142 | + if (width < 500) { |
| 143 | + return 8 |
| 144 | + } else if (width < 800) { |
| 145 | + return 20 |
| 146 | + } else if (width < 1280) { |
| 147 | + return 30 |
| 148 | + } else if (width < 1600) { |
| 149 | + return 40 |
| 150 | + } else if (width < 1920) { |
| 151 | + return 48 |
| 152 | + } else { |
| 153 | + return 72 |
| 154 | + } |
| 155 | +} |
0 commit comments