diff --git a/components/Fireworks.js b/components/Fireworks.js index b6f1860f..c3d48429 100644 --- a/components/Fireworks.js +++ b/components/Fireworks.js @@ -17,7 +17,6 @@ const Fireworks = () => { useEffect(() => { // 异步加载 async function loadFireworks() { - console.log('加载烟花') loadExternalResource( 'https://cdn.bootcdn.net/ajax/libs/animejs/3.2.1/anime.min.js', 'js' diff --git a/components/Giscus.js b/components/Giscus.js index ed1564eb..a5bb2c50 100644 --- a/components/Giscus.js +++ b/components/Giscus.js @@ -1,6 +1,8 @@ import { siteConfig } from '@/lib/config' import { useGlobal } from '@/lib/global' -import Giscus from '@giscus/react' +import { loadExternalResource } from '@/lib/utils' +import { useEffect } from 'react' +// import Giscus from '@giscus/react' /** * Giscus评论 @see https://giscus.app/zh-CN @@ -12,21 +14,34 @@ import Giscus from '@giscus/react' const GiscusComponent = () => { const { isDarkMode } = useGlobal() const theme = isDarkMode ? 'dark' : 'light' + useEffect(() => { + loadExternalResource('/js/giscus.js', 'js').then(() => { + if (window.Giscus) { + window.Giscus.init('#giscus') + } + }) + return () => { + window.Giscus.destroy() + } + }, [isDarkMode]) return ( - +
) } diff --git a/package.json b/package.json index 38c2e9cb..76826951 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,6 @@ "bundle-report": "cross-env ANALYZE=true yarn build" }, "dependencies": { - "@giscus/react": "^2.2.6", "@headlessui/react": "^1.7.15", "@next/bundle-analyzer": "^12.1.1", "@vercel/analytics": "^1.0.0", @@ -29,7 +28,6 @@ "aos": "^2.3.4", "feed": "^4.2.2", "js-md5": "^0.7.3", - "localStorage": "^1.0.4", "lodash.throttle": "^4.1.1", "memory-cache": "^0.2.0", "next": "13.3.1", diff --git a/public/js/giscus.js b/public/js/giscus.js new file mode 100644 index 00000000..373454c4 --- /dev/null +++ b/public/js/giscus.js @@ -0,0 +1,209 @@ +/* eslint-disable */ +;(function () { + var baseUrl = 'https://giscus.app' + var giscusIframe = null + + // 错误日志 + function handleError(a) { + return '[giscus] An error occurred. Error message: "'.concat(a, '".') + } + // 站点元信息 + function getMetaContent(name, includeProperty) { + void 0 === includeProperty && (includeProperty = !1) + includeProperty = includeProperty + ? "meta[property='og:".concat(name, "'],") + : '' + return (name = document.querySelector( + includeProperty + "meta[name='".concat(name, "']") + )) + ? name.content + : '' + } + + // 渲染 + function render(querySelector) { + // const giscusContainer = document.currentScript + const giscusContainer = document.querySelector(querySelector) + // var k = new URL(m.src).origin + let dataset = new URL(location.href) + let paramsSession = dataset.searchParams.get('giscus') || '' + const localStorageSession = localStorage.getItem('giscus-session') + dataset.searchParams.delete('giscus') + dataset.hash = '' + let url = dataset.toString() + if (paramsSession) + localStorage.setItem('giscus-session', JSON.stringify(paramsSession)), + history.replaceState(void 0, document.title, url) + else if (localStorageSession) { + try { + paramsSession = JSON.parse(localStorageSession) + } catch (a) { + localStorage.removeItem('giscus-session'), + console.warn( + ''.concat( + handleError(a === null || void 0 === a ? void 0 : a.message), + ' Session has been cleared.' + ) + ) + } + } + + dataset = giscusContainer.dataset + var params = {} + params.origin = url + params.session = paramsSession + params.theme = dataset.theme + params.reactionsEnabled = dataset.reactionsEnabled || '1' + params.emitMetadata = dataset.emitMetadata || '0' + params.inputPosition = dataset.inputPosition || 'bottom' + params.repo = dataset.repo + params.repoId = dataset.repoId + params.category = dataset.category || '' + params.categoryId = dataset.categoryId + params.strict = dataset.strict || '0' + params.description = getMetaContent('description', !0) + params.backLink = getMetaContent('giscus:backlink') || url + switch (dataset.mapping) { + case 'url': + params.term = url + break + case 'title': + params.term = document.title + break + case 'og:title': + params.term = getMetaContent('title', !0) + break + case 'specific': + params.term = dataset.term + break + case 'number': + params.number = dataset.term + break + default: + params.term = + location.pathname.length < 2 + ? 'index' + : location.pathname.substring(1).replace(/\.\w+$/, '') + } + const q = + (paramsSession = document.querySelector('.giscus')) && paramsSession.id + q && (params.origin = ''.concat(url, '#').concat(q)) + url = dataset.lang ? '/'.concat(dataset.lang) : '' + url = '' + .concat(baseUrl) + .concat(url, '/widget?') + .concat(new URLSearchParams(params)) + dataset = dataset.loading === 'lazy' ? 'lazy' : void 0 + + // 创建iframe + giscusIframe = document.createElement('iframe') + Object.entries({ + class: 'giscus-frame giscus-frame--loading', + title: 'Comments', + scrolling: 'no', + allow: 'clipboard-write', + src: url, + loading: dataset + }).forEach(function (a) { + const g = a[0] + return (a = a[1]) && giscusIframe.setAttribute(g, a) + }) + giscusIframe.style.opacity = '0' + giscusIframe.addEventListener('load', function () { + giscusIframe.style.removeProperty('opacity') + giscusIframe.classList.remove('giscus-frame--loading') + }) + dataset = + document.getElementById('giscus-css') || document.createElement('link') + dataset.id = 'giscus-css' + dataset.rel = 'stylesheet' + dataset.href = ''.concat(baseUrl, '/default.css') + document.head.prepend(dataset) + if (paramsSession) { + for (; paramsSession.firstChild; ) paramsSession.firstChild.remove() + paramsSession.appendChild(giscusIframe) + } else + (paramsSession = document.createElement('div')), + paramsSession.setAttribute('class', 'giscus'), + paramsSession.appendChild(giscusIframe), + giscusContainer.insertAdjacentElement('afterend', paramsSession) + } + + // 处理接收消息 + function handdleMessage(event) { + if (!giscusIframe) { + return + } + event.origin === baseUrl && + ((event = event.data), + typeof event === 'object' && + event.giscus && + (event.giscus.resizeHeight && + (giscusIframe.style.height = ''.concat( + event.giscus.resizeHeight, + 'px' + )), + event.giscus.signOut + ? (localStorage.removeItem('giscus-session'), + console.log( + '[giscus] User has logged out. Session has been cleared.' + ), + p()) + : event.giscus.error && + ((event = event.giscus.error), + event.includes('Bad credentials') || + event.includes('Invalid state value') || + event.includes('State has expired') + ? localStorage.getItem('giscus-session') !== null + ? (localStorage.removeItem('giscus-session'), + console.warn( + ''.concat(handleError(event), ' Session has been cleared.') + ), + p()) + : localStorageSession || + console.error( + '' + .concat( + handleError(event), + ' No session is stored initially. ' + ) + .concat( + 'Please consider reporting this error at https://github.com/giscus/giscus/issues/new.' + ) + ) + : event.includes('Discussion not found') + ? console.warn( + '[giscus] '.concat( + event, + '. A new discussion will be created if a comment/reaction is submitted.' + ) + ) + : event.includes('API rate limit exceeded') + ? console.warn(handleError(event)) + : console.error( + '' + .concat(handleError(event), ' ') + .concat( + 'Please consider reporting this error at https://github.com/giscus/giscus/issues/new.' + ) + )))) + } + + // 初始化 + function initializeGiscus(querySelector) { + render(querySelector) + window.addEventListener('message', handdleMessage) + } + + // 销毁 + function destroyGiscus() { + giscusIframe?.remove() + giscusIframe = null + } + + // 暴露接口 + window.Giscus = { + init: initializeGiscus, + destroy: destroyGiscus + } +})() diff --git a/yarn.lock b/yarn.lock index 1a6cc2a8..fd917710 100644 --- a/yarn.lock +++ b/yarn.lock @@ -179,13 +179,6 @@ resolved "https://registry.yarnpkg.com/@fisch0920/medium-zoom/-/medium-zoom-1.0.7.tgz#46b09b40a1be796f7ff13ea594973e1f1d3c66f6" integrity sha512-hPUrgVM/QvsZdZzDTPyL1C1mOtEw03RqTLmK7ZlJ8S/64u4O4O5BvPvjB/9kyLtE6iVaS9UDRAMSwmM9uh2JIw== -"@giscus/react@^2.2.6": - version "2.4.0" - resolved "https://registry.yarnpkg.com/@giscus/react/-/react-2.4.0.tgz#43fa5fbdcca9d44f532e6c61c46878da3476909c" - integrity sha512-y8d8qiZ2sBuaXRcgn/ZWfMlRs9bx26p62BU/HEKQQ+IfHo3B/kglgPjX/IqudwlX+DOlHUl1NvtFo9C8Eqo0eQ== - dependencies: - giscus "^1.4.0" - "@headlessui/react@^1.7.15": version "1.7.18" resolved "https://registry.yarnpkg.com/@headlessui/react/-/react-1.7.18.tgz#30af4634d2215b2ca1aa29d07f33d02bea82d9d7" @@ -252,18 +245,6 @@ "@jridgewell/resolve-uri" "^3.1.0" "@jridgewell/sourcemap-codec" "^1.4.14" -"@lit-labs/ssr-dom-shim@^1.2.0": - version "1.2.0" - resolved "https://registry.yarnpkg.com/@lit-labs/ssr-dom-shim/-/ssr-dom-shim-1.2.0.tgz#353ce4a76c83fadec272ea5674ede767650762fd" - integrity sha512-yWJKmpGE6lUURKAaIltoPIE/wrbY3TEkqQt+X0m+7fQNnAv0keydnYvbiJFP1PnMhizmIWRWOG5KLhYyc/xl+g== - -"@lit/reactive-element@^2.0.4": - version "2.0.4" - resolved "https://registry.yarnpkg.com/@lit/reactive-element/-/reactive-element-2.0.4.tgz#8f2ed950a848016383894a26180ff06c56ae001b" - integrity sha512-GFn91inaUa2oHLak8awSIigYz0cU0Payr1rcFsrkf5OJ5eSPxElyZfKh0f2p9FsTiZWXQdWGJeXZICEfXXYSXQ== - dependencies: - "@lit-labs/ssr-dom-shim" "^1.2.0" - "@matejmazur/react-katex@^3.1.3": version "3.1.3" resolved "https://registry.yarnpkg.com/@matejmazur/react-katex/-/react-katex-3.1.3.tgz#f07404c848b93bfef9ed9653a4bb080dc8bf2bf0" @@ -456,11 +437,6 @@ dependencies: "@types/node" "*" -"@types/trusted-types@^2.0.2": - version "2.0.7" - resolved "https://registry.yarnpkg.com/@types/trusted-types/-/trusted-types-2.0.7.tgz#baccb07a970b91707df3a3e8ba6896c57ead2d11" - integrity sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw== - "@types/web-bluetooth@^0.0.20": version "0.0.20" resolved "https://registry.yarnpkg.com/@types/web-bluetooth/-/web-bluetooth-0.0.20.tgz#f066abfcd1cbe66267cdbbf0de010d8a41b41597" @@ -1965,13 +1941,6 @@ get-tsconfig@^4.5.0: dependencies: resolve-pkg-maps "^1.0.0" -giscus@^1.4.0: - version "1.4.0" - resolved "https://registry.yarnpkg.com/giscus/-/giscus-1.4.0.tgz#89a9c445776e91b59ab47249ae6acdfa01bb6941" - integrity sha512-Pll+pcclTx47NcFDw8nuka2Ja85Gc4XWpzSgL0rszOQaMQRQIV8UMR+zP4a+/N3tV2TXc1SZ537kWlsN6EsAaw== - dependencies: - lit "^3.1.0" - glob-parent@^5.1.2, glob-parent@~5.1.2: version "5.1.2" resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4" @@ -2559,31 +2528,6 @@ lines-and-columns@^1.1.6: resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.2.4.tgz#eca284f75d2965079309dc0ad9255abb2ebc1632" integrity sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg== -lit-element@^4.0.4: - version "4.0.4" - resolved "https://registry.yarnpkg.com/lit-element/-/lit-element-4.0.4.tgz#e0b37ebbe2394bcb9578d611a409f49475dff361" - integrity sha512-98CvgulX6eCPs6TyAIQoJZBCQPo80rgXR+dVBs61cstJXqtI+USQZAbA4gFHh6L/mxBx9MrgPLHLsUgDUHAcCQ== - dependencies: - "@lit-labs/ssr-dom-shim" "^1.2.0" - "@lit/reactive-element" "^2.0.4" - lit-html "^3.1.2" - -lit-html@^3.1.2: - version "3.1.2" - resolved "https://registry.yarnpkg.com/lit-html/-/lit-html-3.1.2.tgz#6655ce82367472de7680c62b1bcb0beb0e426fa1" - integrity sha512-3OBZSUrPnAHoKJ9AMjRL/m01YJxQMf+TMHanNtTHG68ubjnZxK0RFl102DPzsw4mWnHibfZIBJm3LWCZ/LmMvg== - dependencies: - "@types/trusted-types" "^2.0.2" - -lit@^3.1.0: - version "3.1.2" - resolved "https://registry.yarnpkg.com/lit/-/lit-3.1.2.tgz#f276258e8a56593f1d834a5fd00a7eb5e891ae73" - integrity sha512-VZx5iAyMtX7CV4K8iTLdCkMaYZ7ipjJZ0JcSdJ0zIdGxxyurjIn7yuuSxNBD7QmjvcNJwr0JS4cAdAtsy7gZ6w== - dependencies: - "@lit/reactive-element" "^2.0.4" - lit-element "^4.0.4" - lit-html "^3.1.2" - loader-utils@^2.0.0: version "2.0.4" resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-2.0.4.tgz#8b5cb38b5c34a9a018ee1fc0e6a066d1dfcc528c" @@ -2593,11 +2537,6 @@ loader-utils@^2.0.0: emojis-list "^3.0.0" json5 "^2.1.2" -localStorage@^1.0.4: - version "1.0.4" - resolved "https://registry.yarnpkg.com/localStorage/-/localStorage-1.0.4.tgz#57dfa28084385f81431accb8ae24b196398223f7" - integrity sha512-r35zrihcDiX+dqWlJSeIwS9nrF95OQTgqMFm3FB2D/+XgdmZtcutZOb7t0xXkhOEM8a9kpuu7cc28g1g36I5DQ== - lodash.debounce@^4.0.6: version "4.0.8" resolved "https://r2.cnpmjs.org/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"