-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from anc95/right-menu
feature: add context menu trigger
- Loading branch information
Showing
11 changed files
with
126 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
}, | ||
"permissions": [ | ||
"storage", | ||
"clipboardRead" | ||
"clipboardRead", | ||
"contextMenus" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,55 @@ | ||
import type { MessagePayload } from "@/common/types" | ||
import { EventName, launch_writely } from '@/common/event-name'; | ||
import { getSetting } from '@/common/store/settings'; | ||
import type { MessagePayload } from '@/common/types'; | ||
|
||
chrome.runtime.onMessage.addListener(async (message: MessagePayload) => { | ||
if (message.type === 'open-options-page') { | ||
chrome.runtime.openOptionsPage() | ||
chrome.runtime.onMessage.addListener( | ||
async (message: MessagePayload<EventName.openOptionsPage>) => { | ||
if (message.type === 'open-options-page') { | ||
chrome.runtime.openOptionsPage(); | ||
} | ||
} | ||
}) | ||
); | ||
|
||
chrome.contextMenus.create({ | ||
title: 'Launch writely', | ||
id: 'writely', | ||
contexts: ['selection'], | ||
}); | ||
|
||
chrome.contextMenus.create({ | ||
title: 'Writely instructions', | ||
id: 'writely-instructions', | ||
contexts: ['selection'], | ||
}); | ||
|
||
const createSubMenu = async () => { | ||
const settings = await getSetting(); | ||
|
||
settings.customInstructions?.map((instruction) => { | ||
chrome.contextMenus.create({ | ||
title: instruction, | ||
id: instruction, | ||
contexts: ['selection'], | ||
parentId: 'writely-instructions', | ||
}); | ||
}); | ||
}; | ||
|
||
createSubMenu(); | ||
|
||
chrome.contextMenus.onClicked.addListener((info, tab) => { | ||
if (info.menuItemId === 'writely' && tab.id) { | ||
chrome.tabs.sendMessage(tab.id, { | ||
type: EventName.launchWritely, | ||
}); | ||
} | ||
|
||
if (info.parentMenuItemId === 'writely-instructions') { | ||
chrome.tabs.sendMessage(tab.id, { | ||
type: EventName.launchWritelyResultPanel, | ||
data: { | ||
instruction: info.menuItemId, | ||
}, | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const launch_writely = 'launch-writely'; | ||
|
||
export enum EventName { | ||
launchWritely = 'launch-writely', | ||
launchWritelyResultPanel = 'launchWritelyResultPanel', | ||
openOptionsPage = 'open-options-page', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export type MessagePayload = { | ||
type: 'open-options-page' | ||
} | ||
import { EventName } from './event-name'; | ||
|
||
export type MessagePayload<T extends EventName, D extends any = any> = { | ||
type: T; | ||
data?: D; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useState } from 'react'; | ||
import { createContainer } from 'unstated-next'; | ||
|
||
export const { useContainer: useInstruction, Provider: InstructionProvider } = | ||
createContainer(() => { | ||
const [instruction, setInstruction] = useState<string>(''); | ||
|
||
return { | ||
instruction, | ||
setInstruction, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters