From 3988773341a18f4d6373cf3146f74287a24943ab Mon Sep 17 00:00:00 2001 From: SunWuyuan Date: Sat, 22 Mar 2025 18:36:43 +0800 Subject: [PATCH] 1 --- src/components/settings/AboutCard.vue | 2 +- .../settings/cards/EchoChamberCard.vue | 150 ++++++------------ 2 files changed, 53 insertions(+), 99 deletions(-) diff --git a/src/components/settings/AboutCard.vue b/src/components/settings/AboutCard.vue index 1bb6c9a..15d6c4d 100644 --- a/src/components/settings/AboutCard.vue +++ b/src/components/settings/AboutCard.vue @@ -13,7 +13,7 @@
diff --git a/src/components/settings/cards/EchoChamberCard.vue b/src/components/settings/cards/EchoChamberCard.vue index c46e2a9..4112de3 100644 --- a/src/components/settings/cards/EchoChamberCard.vue +++ b/src/components/settings/cards/EchoChamberCard.vue @@ -7,21 +7,16 @@ >
-
+
-
- -
- - - {{ currentQuote.contributor }} - -
-
-
+ + + + + + {{ currentQuote.contributor }} + + @@ -30,129 +25,88 @@ import Typewriter from "typewriter-effect/dist/core"; import quotes from "@/data/echoChamber.json"; import SettingsCard from "@/components/SettingsCard.vue"; +const INITIAL_STATE = { + text: "点击此处可以查看 Classworks 用户群里沙雕群友们的发言", + author: "点击后会复制当前句子到剪贴板中" +}; + +const TYPEWRITER_CONFIG = { + main: { delay: 50, deleteSpeed: 100 }, + source: { delay: 10, deleteSpeed: 10, cursor: "" } +}; + export default { name: "EchoChamberCard", components: { SettingsCard }, - data() { - return { - typewriter: null, - sourceWriter: null, - currentQuote: { - text: this.INITIAL_TEXT, - author: this.INITIAL_SOURCE, - }, - showCopySuccess: false, - hasClicked: false, - INITIAL_TEXT: "点击此处可以查看 Classworks 用户群里沙雕群友们的发言", - INITIAL_SOURCE: "点击后会复制当前句子到剪贴板中", - }; - }, + data: () => ({ + typewriter: null, + sourceWriter: null, + currentQuote: INITIAL_STATE, + hasClicked: false + }), + mounted() { this.initTypewriters(); }, + methods: { - getRandomQuote() { - const index = Math.floor(Math.random() * quotes.quotes.length); - return quotes.quotes[index]; - }, initTypewriters() { - // 主文本打字机 - this.typewriter = new Typewriter(this.$refs.typewriter, { - delay: 50, - deleteSpeed: 100, - loop: false, - }); - - // 来源文本打字机 - this.sourceWriter = new Typewriter(this.$refs.sourceWriter, { - delay: 10, - deleteSpeed: 10, - loop: false, - cursor: "", - }); - - // 显示初始提示 - this.typewriter.typeString(this.INITIAL_TEXT).start(); - this.sourceWriter.typeString(this.INITIAL_SOURCE).start(); + this.typewriter = new Typewriter(this.$refs.typewriter, TYPEWRITER_CONFIG.main); + this.sourceWriter = new Typewriter(this.$refs.sourceWriter, TYPEWRITER_CONFIG.source); + this.typeQuote(INITIAL_STATE); }, typeQuote(quote) { this.typewriter.deleteAll(30).typeString(quote.text).start(); - if (quote.author) { - this.sourceWriter.deleteAll(20).typeString(`${quote.author}`).start(); + this.sourceWriter.deleteAll(20).typeString(quote.author).start(); } }, - refreshQuote() { - this.currentQuote = this.getRandomQuote(); - this.typeQuote(this.currentQuote); - }, - async handleClick() { if (!this.hasClicked) { this.hasClicked = true; - this.currentQuote = this.getRandomQuote(); } await this.copyToClipboard(); - this.refreshQuote(); + this.currentQuote = this.getRandomQuote(); + this.typeQuote(this.currentQuote); + }, + + getRandomQuote() { + return quotes.quotes[Math.floor(Math.random() * quotes.quotes.length)]; }, async copyToClipboard() { if (!this.currentQuote) return; - const quote = this.currentQuote; - const text = `${quote.text}\n${ - quote.author ? `作者:${quote.author}` : "" - }\n${quote.contributor ? `贡献者:${quote.contributor}` : ""}${ - quote.link - ? `\n来源:${quote.link}` - : quote.contributor - ? "\n来源:https://github.com/" + quote.contributor - : "" - }`; + const { text, author, contributor, link } = this.currentQuote; + const parts = [ + text, + author && `作者:${author}`, + contributor && `贡献者:${contributor}`, + (link || contributor) && `来源:${link || `https://github.com/${contributor}`}` + ].filter(Boolean); try { - await navigator.clipboard.writeText(text); - this.showCopySuccess = true; + await navigator.clipboard.writeText(parts.join('\n')); } catch (err) { console.error("复制失败:", err); } - }, + } }, + beforeUnmount() { - if (this.typewriter) { - this.typewriter.stop(); - } - if (this.sourceWriter) { - this.sourceWriter.stop(); - } - }, + [this.typewriter, this.sourceWriter].forEach(writer => writer?.stop()); + } };