Skip to content

Commit

Permalink
feat: add editing latest note fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Rue-pro committed Apr 24, 2024
1 parent f04eea6 commit 199c91c
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 2 deletions.
15 changes: 15 additions & 0 deletions public/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@
"CONTEXT_MENU_ADD_NOTE_AUTOMATICALLY": {
"message": "Add note"
},
"CONTEXT_MENU_EDIT_LATEST_NOTE_TRANSLATION": {
"message": "Edit latest note translation"
},
"CONTEXT_MENU_EDIT_LATEST_NOTE_CONTEXT": {
"message": "Edit latest note context"
},
"CONTEXT_MENU_EDIT_LATEST_NOTE_TRANSCRIPTION": {
"message": "Edit latest note transcription"
},
"ERROR_CAN_NOT_IDENTIFY_DICTIONARY_ADD_NOTE_AUTOMATICALLY": {
"message": "Adding note automatically failed, can't identify dictionary"
},
Expand Down Expand Up @@ -180,5 +189,11 @@
},
"CLEAR": {
"message": "Clear"
},
"ERROR_CAN_NOT_GET_ACTIVE_TAB": {
"message": "Can't get active tab"
},
"ERROR_CAN_NOT_GET_ACTIVE_TAB_ON_ACTIVATED": {
"message": "Can't get active tab while activating"
}
}
3 changes: 3 additions & 0 deletions src/background/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { addNewWord } from '@background/features/addNewWord'
import { autoAddNewNote } from '@background/features/autoAddNewNote'
import { editLatestNote } from '@background/features/editLatestNote'

import { browser } from '@background/shared/browser'

Expand All @@ -18,6 +19,8 @@ browser.contextMenus.removeAll(() => {
addNewWord(PARENT_ID)

autoAddNewNote(PARENT_ID)

editLatestNote(PARENT_ID)
})

$notes.listen(() => {})
1 change: 1 addition & 0 deletions src/background/features/editLatestNote/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ui'
57 changes: 57 additions & 0 deletions src/background/features/editLatestNote/ui.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { browser } from '@background/shared/browser'
import {
TOnClickContextMenuInfoProps,
TOnClickContextMenuTabProps,
} from '@background/shared/browser/contextMenus'
import { currentPageLanguageStore } from '@background/shared/entities/language'

import { INoteSelectors } from '@shared/entities/note'
import { editNote } from '@shared/entities/note/model/store'

export const editLatestNote = (parentId: string) => {
currentPageLanguageStore.$currentPageLanguage.listen(() => {})

const fields: Array<keyof Omit<INoteSelectors, 'text'>> = [
'translation',
'context',
'transcription',
]
console.log('fields', fields)

fields.forEach((field) => {
const handleClick = async (
info: TOnClickContextMenuInfoProps,
tab?: TOnClickContextMenuTabProps,
) => {
if (info.menuItemId === `edit_latest_note_${field}`) {
if (!tab?.id) return

editNote(currentPageLanguageStore.$currentPageLanguage.get(), {
...currentPageLanguageStore.$latestNote.get(),
translation: info.selectionText,
})
}
}

currentPageLanguageStore.$latestNote.listen((latestNote) => {
if (latestNote) {
browser.contextMenus.update(`edit_latest_note_${field}`, {
title: `${browser.i18n.getMessage(`CONTEXT_MENU_EDIT_LATEST_NOTE_${field.toUpperCase()}`)} ${latestNote.text}`,
})
} else {
browser.contextMenus.remove(`edit_latest_note_${field}`)
}
})

browser.contextMenus.create({
title: browser.i18n.getMessage(
`CONTEXT_MENU_EDIT_LATEST_NOTE_${field.toUpperCase()}`,
),
id: `edit_latest_note_${field}`,
parentId: parentId,
contexts: ['selection'],
})

browser.contextMenus.onClicked.addListener(handleClick)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { IContextMenus } from './types'
export const chromeContextMenus: IContextMenus = {
create: chrome.contextMenus.create,

update: chrome.contextMenus.update,

onClicked: chrome.contextMenus.onClicked,

removeAll: chrome.contextMenus.removeAll,

remove: chrome.contextMenus.remove,
}
7 changes: 7 additions & 0 deletions src/background/shared/browser/contextMenus/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import { TTab } from '@shared/shared/browser/tabs'
export interface IContextMenus {
create: (menuConfig: IMenuConfig) => string | number

update: (
menuItemId: string | number,
menuConfig: Partial<IMenuConfig>,
) => void

remove: (menuItemId: string | number) => void

onClicked: {
addListener: (
callback: (
Expand Down
4 changes: 2 additions & 2 deletions src/content/dictionary/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { $languages } from '@shared/entities/language/model/store'

import { getNotesFields } from './helpers/getNotesFields'

$languages.listen(() => {})

browser.runtime.onConnect.addListener(async function (port) {
$languages.listen(() => {})

if (port.name === 'CurrentPage') {
getLanguageFromPageHandler(port, () => {
let pageLanguage: TLanguageCode = 'other'
Expand Down

0 comments on commit 199c91c

Please sign in to comment.