Skip to content

Commit d23dea6

Browse files
committed
feat(context-menu): refactor context menu items and update translation menu on tab activation
1 parent 875db70 commit d23dea6

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

entrypoints/background.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { browser } from 'wxt/browser'
55
import { defineBackground } from 'wxt/utils/define-background'
66

77
import { INVALID_URLS } from '@/utils/constants'
8-
import { CONTEXT_MENU } from '@/utils/context-menu'
8+
import { CONTEXT_MENU, CONTEXT_MENU_ITEM_TRANSLATE_PAGE } from '@/utils/context-menu'
99
import logger from '@/utils/logger'
1010
import { bgBroadcastRpc } from '@/utils/rpc'
1111
import { isTabValid } from '@/utils/tab'
@@ -50,6 +50,11 @@ export default defineBackground(() => {
5050
}
5151

5252
browser.tabs.onActivated.addListener(async ({ tabId }) => {
53+
// reset the translate context menu to default
54+
await browser.contextMenus.update(CONTEXT_MENU_ITEM_TRANSLATE_PAGE.id, {
55+
title: CONTEXT_MENU_ITEM_TRANSLATE_PAGE.title,
56+
contexts: CONTEXT_MENU_ITEM_TRANSLATE_PAGE.contexts,
57+
})
5358
const tab = await browser.tabs.get(tabId)
5459
const url = tab.url
5560
url && (await setPopupStatusBasedOnUrl(tabId, url))

entrypoints/content/composables/useTranslator.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import { showSettings } from '../utils/settings'
1616
async function _useTranslator() {
1717
// useToast must be called before the first await
1818
const toast = useToast()
19+
onMounted(() => {
20+
setTranslationMenuTargetLanguage(enabled.value, targetLocale.value)
21+
})
1922
const userConfig = await getUserConfig()
2023
const targetLocale = userConfig.translation.targetLocale.toRef()
2124
const ollamaStatusStore = useOllamaStatusStore()
@@ -51,10 +54,6 @@ async function _useTranslator() {
5154
})
5255
}
5356

54-
onMounted(() => {
55-
setTranslationMenuTargetLanguage(enabled.value, targetLocale.value)
56-
})
57-
5857
useEventListener(document, 'visibilitychange', async () => {
5958
if (document.visibilityState === 'visible') {
6059
setTranslationMenuTargetLanguage(enabled.value, targetLocale.value)

utils/context-menu.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,28 @@ type ContextMenuItem = {
1010
contexts: ContextTypeList
1111
}
1212

13+
export const CONTEXT_MENU_ITEM_TRANSLATE_SELECTED_TEXT: ContextMenuItem = {
14+
id: 'native-mind-selection-translate',
15+
title: 'NativeMind: Translate selected text',
16+
contexts: [ContextType.SELECTION],
17+
}
18+
19+
export const CONTEXT_MENU_ITEM_TRANSLATE_PAGE: ContextMenuItem = {
20+
id: 'native-mind-page-translate',
21+
title: 'NativeMind: Translate this page',
22+
contexts: [ContextType.PAGE],
23+
}
24+
25+
export const CONTEXT_MENU_ITEM_SETTINGS: ContextMenuItem = {
26+
id: 'native-mind-settings',
27+
title: 'Settings',
28+
contexts: [ContextType.ACTION],
29+
}
30+
1331
export const CONTEXT_MENU: ContextMenuItem[] = [
14-
{
15-
id: 'native-mind-page-translate',
16-
title: 'NativeMind: Translate this page',
17-
contexts: [ContextType.PAGE],
18-
},
19-
{
20-
id: 'native-mind-selection-translate',
21-
title: 'NativeMind: Translate selected text',
22-
contexts: [ContextType.SELECTION],
23-
},
24-
{
25-
id: 'native-mind-settings',
26-
title: 'Settings',
27-
contexts: [ContextType.ACTION],
28-
},
29-
// {
30-
// id: 'native-mind-selection-translate',
31-
// title: 'NativeMind: Writing Tools',
32-
// contexts: [ContextType.EDITABLE],
33-
// },
32+
CONTEXT_MENU_ITEM_TRANSLATE_PAGE,
33+
CONTEXT_MENU_ITEM_TRANSLATE_SELECTED_TEXT,
34+
CONTEXT_MENU_ITEM_SETTINGS,
3435
]
3536

3637
export type ContextMenu = typeof CONTEXT_MENU

0 commit comments

Comments
 (0)