Skip to content

Commit

Permalink
chore: move usage of chrome functions to browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Rue-pro committed Apr 9, 2024
1 parent 9ece31d commit ededbf6
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/app/background/features/addNewWord/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { TLanguageCode, getLanguageCodeByPageUrl } from '@entities/language'
import { noteStore } from '@entities/note'

import { browser } from '@shared/browser'
import { TOnClickContextMenuInfoProps, browser } from '@shared/browser'

export const addNewWord = (parentId: string) => {
chrome.contextMenus.create({
browser.contextMenus.create({
title: 'CONTEXT_MENU_ADD_NEW_WORD',
id: 'new_word',
parentId: parentId,
contexts: ['selection'],
})

const handleClick = async (info: chrome.contextMenus.OnClickData) => {
const handleClick = async (info: TOnClickContextMenuInfoProps) => {
if (info.menuItemId === 'new_word') {
let finalLanguageCode: TLanguageCode = 'other'
const languageCode = getLanguageCodeByPageUrl(info.pageUrl)
Expand All @@ -27,5 +27,5 @@ export const addNewWord = (parentId: string) => {
}
}

chrome.contextMenus.onClicked.addListener(handleClick)
browser.contextMenus.onClicked.addListener(handleClick)
}
15 changes: 10 additions & 5 deletions src/app/background/features/autoAddNewNote/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
import { errorStore } from '@entities/error'
import { noteStore } from '@entities/note'

import { PortEmitter } from '@shared/browser'
import {
PortEmitter,
TOnClickContextMenuInfoProps,
TOnClickContextMenuTabProps,
browser,
} from '@shared/browser'

import {
getLanguageFromPage,
Expand All @@ -23,7 +28,7 @@ import {
const $dictionaryPorts = map<Record<string, PortEmitter>>()

export const autoAddNewNote = (parentId: string) => {
chrome.contextMenus.create({
browser.contextMenus.create({
title: 'CONTEXT_MENU_ADD_NOTE_AUTOMATICALLY',
id: 'auto_new_note',
parentId: parentId,
Expand All @@ -32,8 +37,8 @@ export const autoAddNewNote = (parentId: string) => {
})

const handleClick = async (
info: chrome.contextMenus.OnClickData,
tab?: chrome.tabs.Tab,
info: TOnClickContextMenuInfoProps,
tab?: TOnClickContextMenuTabProps,
) => {
if (info.menuItemId === 'auto_new_note') {
if (!tab?.id) return
Expand Down Expand Up @@ -87,7 +92,7 @@ export const autoAddNewNote = (parentId: string) => {
}
}

chrome.contextMenus.onClicked.addListener(handleClick)
browser.contextMenus.onClicked.addListener(handleClick)
}

function createDictionaryPort(
Expand Down
6 changes: 4 additions & 2 deletions src/app/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { $notes } from '@entities/note/model/store'

import { browser } from '@shared/browser'

import { addNewWord } from './features/addNewWord'
import { autoAddNewNote } from './features/autoAddNewNote'

const PARENT_ID = 'CONTEXT_MENU_ROOT'

chrome.contextMenus.removeAll(() => {
chrome.contextMenus.create({
browser.contextMenus.removeAll(() => {
browser.contextMenus.create({
title: 'CONTEXT_MENU_ROOT',
id: PARENT_ID,
contexts: ['page', 'selection'],
Expand Down
10 changes: 10 additions & 0 deletions src/shared/browser/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,14 @@ export const chromeBrowser: IBrowser = {
})
},
},

contextMenus: {
create: chrome.contextMenus.create,

Check failure on line 144 in src/shared/browser/chrome.ts

View workflow job for this annotation

GitHub Actions / test

src/shared/browser/__test__/chrome.test.ts

ReferenceError: chrome is not defined ❯ src/shared/browser/chrome.ts:144:13 ❯ src/shared/browser/index.ts:1:31

Check failure on line 144 in src/shared/browser/chrome.ts

View workflow job for this annotation

GitHub Actions / test

src/entities/dictionary/model/__tests__/store.test.ts

ReferenceError: chrome is not defined ❯ src/shared/browser/chrome.ts:144:13 ❯ src/shared/browser/index.ts:1:31

Check failure on line 144 in src/shared/browser/chrome.ts

View workflow job for this annotation

GitHub Actions / test

src/entities/language/model/__tests__/store.test.ts

ReferenceError: chrome is not defined ❯ src/shared/browser/chrome.ts:144:13 ❯ src/shared/browser/index.ts:1:31

Check failure on line 144 in src/shared/browser/chrome.ts

View workflow job for this annotation

GitHub Actions / test

src/entities/note/model/__tests__/store.test.ts

ReferenceError: chrome is not defined ❯ src/shared/browser/chrome.ts:144:13 ❯ src/shared/browser/index.ts:1:31

Check failure on line 144 in src/shared/browser/chrome.ts

View workflow job for this annotation

GitHub Actions / test

src/features/language/SelectLanguages/model/__tests__/store.test.ts

ReferenceError: chrome is not defined ❯ src/shared/browser/chrome.ts:144:13 ❯ src/shared/browser/index.ts:1:31

Check failure on line 144 in src/shared/browser/chrome.ts

View workflow job for this annotation

GitHub Actions / test

src/features/language/SelectLanguages/ui/__tests__/SelectLanguages.test.tsx

ReferenceError: chrome is not defined ❯ src/shared/browser/chrome.ts:144:13 ❯ src/shared/browser/index.ts:1:31

onClicked: {
addListener: chrome.contextMenus.onClicked.addListener,
},

removeAll: chrome.contextMenus.removeAll,
},
}
7 changes: 6 additions & 1 deletion src/shared/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ import type { IBrowser } from './types'

export const browser: IBrowser = chromeBrowser

export type { TTab, TOnChangeListenerProps } from './types'
export type {
TTab,
TOnChangeListenerProps,
TOnClickContextMenuInfoProps,
TOnClickContextMenuTabProps,
} from './types'
export * from './port'
45 changes: 45 additions & 0 deletions src/shared/browser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,53 @@ export interface IBrowser {
tabs: {
getActiveTab: () => Promise<TResult<TTab>>
}

contextMenus: {
create: (menuConfig: IMenuConfig) => string | number

onClicked: {
addListener: (
callback: (
info: TOnClickContextMenuInfoProps,
tab?: Partial<TTab>,
) => Promise<void> | void,
) => void
}

removeAll: (callback: () => void) => void
}
}

type IMenuContextType =
| 'all'
| 'page'
| 'frame'
| 'selection'
| 'link'
| 'editable'
| 'image'
| 'video'
| 'audio'
| 'launcher'
| 'browser_action'
| 'page_action'
| 'action'

type IMenuContextItemType = 'normal' | 'checkbox' | 'radio' | 'separator'
export interface IMenuConfig {
title: string
id: string
contexts?: IMenuContextType[]
parentId?: string
type?: IMenuContextItemType
documentUrlPatterns?: string[]
}
export type TOnClickContextMenuInfoProps = {
menuItemId: string | number
selectionText?: string
pageUrl: string
}
export type TOnClickContextMenuTabProps = Partial<TTab>
export type TTab = { id: number; url: string }
export type TActiveTabInfo = { tabId: number }
export type TOnChangeListenerProps<Value = unknown> = {
Expand Down

0 comments on commit ededbf6

Please sign in to comment.