From dfcd869666a29e43c5bbd3d05081a2a1c84678c4 Mon Sep 17 00:00:00 2001 From: naskya Date: Thu, 31 Aug 2023 00:17:05 +0900 Subject: [PATCH] fix: post translate button not showing --- packages/client/src/components/MkNote.vue | 36 +++++++++++--------- packages/client/src/components/MkNoteSub.vue | 36 +++++++++++--------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/packages/client/src/components/MkNote.vue b/packages/client/src/components/MkNote.vue index d27b3d14e..1f3e939d8 100644 --- a/packages/client/src/components/MkNote.vue +++ b/packages/client/src/components/MkNote.vue @@ -267,7 +267,7 @@ import { computed, inject, onMounted, ref } from "vue"; import * as mfm from "mfm-js"; import type { Ref } from "vue"; import type * as misskey from "firefish-js"; -import { detect as detectLanguage } from "tinyld"; +import { detect as detectLanguage_ } from "tinyld"; import MkNoteSub from "@/components/MkNoteSub.vue"; import MkSubNoteContent from "./MkSubNoteContent.vue"; import XNoteHeader from "@/components/MkNoteHeader.vue"; @@ -356,30 +356,32 @@ const translating = ref(false); const enableEmojiReactions = defaultStore.state.enableEmojiReactions; const expandOnNoteClick = defaultStore.state.expandOnNoteClick; -const purifyMFM = (src) => { +const detectLanguage = (src: string): string => { const nodes = mfm.parse(src); const filtered = mfm.extract(nodes, (node) => { - node.type === "text" || node.type === "quote"; + return node.type === "text" || node.type === "quote"; }); - return mfm.toString(filtered); + const purified = mfm.toString(filtered).trim(); + return detectLanguage_(purified); }; -const isForeignLanguage = (() => { - if (!defaultStore.state.detectPostLanguage || !appearNote.text) - return false; - const text = purifyMFM(appearNote.text).trim(); - if (!text) return false; - const uiLanguage = ( - localStorage.getItem("translateLang") || - localStorage.getItem("lang") || - navigator.language - ).slice(0, 2); - return detectLanguage(text) !== uiLanguage; -})(); + +const isForeignLanguage: boolean = + defaultStore.state.detectPostLanguage && + (() => { + const targetLanguage = ( + localStorage.getItem("translateLang") || + localStorage.getItem("lang") || + navigator.language + )?.slice(0, 2); + const postLanguage = detectLanguage(appearNote.value.text); + return postLanguage !== "" && postLanguage !== targetLanguage; + })(); + const translate = async () => { if (translation.value != null) return; translating.value = true; translation.value = await os.api("notes/translate", { - noteId: appearNote.id, + noteId: appearNote.value.id, targetLang: localStorage.getItem("translateLang") || localStorage.getItem("lang") || diff --git a/packages/client/src/components/MkNoteSub.vue b/packages/client/src/components/MkNoteSub.vue index 11f60e7f3..e3eb5202a 100644 --- a/packages/client/src/components/MkNoteSub.vue +++ b/packages/client/src/components/MkNoteSub.vue @@ -189,7 +189,7 @@ import { inject, ref, computed } from "vue"; import type { Ref } from "vue"; import * as misskey from "firefish-js"; import * as mfm from "mfm-js"; -import { detect as detectLanguage } from "tinyld"; +import { detect as detectLanguage_ } from "tinyld"; import XNoteHeader from "@/components/MkNoteHeader.vue"; import MkSubNoteContent from "@/components/MkSubNoteContent.vue"; import XReactionsViewer from "@/components/MkReactionsViewer.vue"; @@ -277,30 +277,32 @@ const replies: misskey.entities.Note[] = const enableEmojiReactions = defaultStore.state.enableEmojiReactions; const expandOnNoteClick = defaultStore.state.expandOnNoteClick; -const purifyMFM = (src) => { +const detectLanguage = (src: string): string => { const nodes = mfm.parse(src); const filtered = mfm.extract(nodes, (node) => { - node.type === "text" || node.type === "quote"; + return node.type === "text" || node.type === "quote"; }); - return mfm.toString(filtered); + const purified = mfm.toString(filtered).trim(); + return detectLanguage_(purified); }; -const isForeignLanguage = (() => { - if (!defaultStore.state.detectPostLanguage || !appearNote.text) - return false; - const text = purifyMFM(appearNote.text).trim(); - if (!text) return false; - const uiLanguage = ( - localStorage.getItem("translateLang") || - localStorage.getItem("lang") || - navigator.language - ).slice(0, 2); - return detectLanguage(text) !== uiLanguage; -})(); + +const isForeignLanguage: boolean = + defaultStore.state.detectPostLanguage && + (() => { + const targetLanguage = ( + localStorage.getItem("translateLang") || + localStorage.getItem("lang") || + navigator.language + )?.slice(0, 2); + const postLanguage = detectLanguage(appearNote.value.text); + return postLanguage !== "" && postLanguage !== targetLanguage; + })(); + const translate = async () => { if (translation.value != null) return; translating.value = true; translation.value = await os.api("notes/translate", { - noteId: appearNote.id, + noteId: appearNote.value.id, targetLang: localStorage.getItem("translateLang") || localStorage.getItem("lang") ||