Merge pull request #1768 from RylanBot/patch

fix: 评论区插件补丁+优化
This commit is contained in:
tangly1024
2024-01-17 16:02:09 +08:00
committed by GitHub
3 changed files with 76 additions and 72 deletions

View File

@@ -1,4 +1,4 @@
import React, { useState } from 'react' import { useState } from 'react';
/** /**
* Tabs切换标签 * Tabs切换标签
@@ -6,59 +6,37 @@ import React, { useState } from 'react'
* @returns * @returns
*/ */
const Tabs = ({ className, children }) => { const Tabs = ({ className, children }) => {
const [currentTab, setCurrentTab] = useState(0) const [currentTab, setCurrentTab] = useState(0);
if (!children) { const validChildren = children.filter(c => c);
return <></>
if (validChildren.length === 0) {
return <></>;
} }
children = children.filter(c => c && c !== '') return (
<div className={`mb-5 duration-200 ${className}`}>
let count = 0 <ul className="flex justify-center space-x-5 pb-4 dark:text-gray-400 text-gray-600 overflow-auto">
children.forEach(e => { {validChildren.map((item, index) => (
if (e) { <li key={index}
count++ className={`${currentTab === index ? 'font-black border-b-2 border-red-600 text-red-600 animate__animated animate__jello' : 'font-extralight cursor-pointer'} text-sm font-sans`}
} onClick={() => setCurrentTab(index)}>
}) {item.key}
</li>
if (count === 0) { ))}
return <></> </ul>
} {/* 标签切换的时候不销毁 DOM 元素,使用 CSS 样式进行隐藏 */}
<div>
if (count === 1) { {validChildren.map((item, index) => (
return <section className={'duration-200 ' + className}> <section
{children} key={index}
</section> className={`${currentTab === index ? 'opacity-100 static h-auto' : 'opacity-0 absolute h-0'}`}>
} {item}
</section>
function tabClickHandle(i) { ))}
setCurrentTab(i) </div>
}
return <div className={'mb-5 duration-200 ' + className}>
<ul className='flex justify-center space-x-5 pb-4 dark:text-gray-400 text-gray-600 overflow-auto'>
{children.map((item, index) => {
return <li key={index}
className={(currentTab === index ? 'font-black border-b-2 border-red-600 text-red-600 animate__animated animate__jello ' : 'font-extralight cursor-pointer') + ' text-sm font-sans '}
onClick={() => {
tabClickHandle(index)
}}>
{item?.key}
</li>
})}
</ul>
<div>
{children.map((item, index) => {
return <section key={index}
data-aos="fade-up"
data-aos-duration="300"
data-aos-once="true"
data-aos-anchor-placement="top-bottom">
{currentTab === index && item}
</section>
})}
</div>
</div> </div>
} );
};
export default Tabs export default Tabs;

View File

@@ -1,5 +1,6 @@
import { useEffect, useState } from 'react'
import { siteConfig } from '@/lib/config' import { siteConfig } from '@/lib/config'
import { useEffect } from 'react' import { useGlobal } from '@/lib/global'
/** /**
* 评论插件 * 评论插件
@@ -9,28 +10,48 @@ import { useEffect } from 'react'
* @constructor * @constructor
*/ */
const Utterances = ({ issueTerm, layout }) => { const Utterances = ({ issueTerm, layout }) => {
const { isDarkMode } = useGlobal()
const [isLoading, setLoading] = useState(true);
useEffect(() => { useEffect(() => {
const theme = const script = document.createElement('script');
siteConfig('APPEARANCE') === 'auto' const anchor = document.getElementById('comments');
? 'preferred-color-scheme' script.onload = () => setLoading(false);
: siteConfig('APPEARANCE') === 'light' script.setAttribute('src', 'https://utteranc.es/client.js');
? 'github-light' script.setAttribute('crossorigin', 'anonymous');
: 'github-dark' script.setAttribute('async', true);
const script = document.createElement('script') script.setAttribute('repo', siteConfig('COMMENT_UTTERRANCES_REPO'));
const anchor = document.getElementById('comments') script.setAttribute('issue-term', 'title');
script.setAttribute('src', 'https://utteranc.es/client.js') // 初始主题
script.setAttribute('crossorigin', 'anonymous') script.setAttribute('theme', isDarkMode ? 'github-dark' : 'github-light');
script.setAttribute('async', true) anchor.appendChild(script);
script.setAttribute('repo', siteConfig('COMMENT_UTTERRANCES_REPO'))
script.setAttribute('issue-term', 'title')
script.setAttribute('theme', theme)
anchor.appendChild(script)
return () => { return () => {
anchor.innerHTML = '' // anchor.innerHTML = ''
};
}, []);
useEffect(() => {
// 直接设置 iframe 的类来改变主题,不重新加载脚本
const iframe = document.querySelector('iframe.utterances-frame');
if (iframe) {
iframe.contentWindow.postMessage({
type: 'set-theme',
theme: isDarkMode ? 'github-dark' : 'github-light'
}, 'https://utteranc.es');
} }
}) }, [isDarkMode]);
return <div id="comments" className='utterances' >
</div> return (
<div id="comments" className='utterances'>
{isLoading && (
<div className="flex justify-center items-center m-8">
<div className="animate-spin rounded-full h-8 w-8 border-2 border-indigo-400 border-t-transparent"></div>
</div>
)}
</div>
);
} }
export default Utterances export default Utterances

View File

@@ -160,6 +160,11 @@ nav {
@apply text-blue-700 @apply text-blue-700
} }
/* twikoo 内置的 element-ui 加载样式 */
.el-loading-spinner {
@apply flex justify-center items-center;
}
/* Webmention style */ /* Webmention style */
.webmention-block { .webmention-block {
background: rgba(0, 116, 222, .2); background: rgba(0, 116, 222, .2);