Skip to content

Commit

Permalink
support right menu
Browse files Browse the repository at this point in the history
  • Loading branch information
anc95 committed Mar 19, 2023
1 parent 980141b commit ebbf13e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 13 deletions.
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"permissions": [
"storage",
"clipboardRead"
"clipboardRead",
"contextMenus"
]
}
30 changes: 25 additions & 5 deletions src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import type { MessagePayload } from "@/common/types"
import { EventName, launch_writely } from '@/common/event-name';
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: 'Writely',
id: 'writely',
contexts: ['selection'],
// onclick: (info) => {
// chrome.runtime.sendMessage(launch_writely);
// },
});

chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === 'writely' && tab.id) {
chrome.tabs.sendMessage(tab.id, {
type: EventName.launchWritely,
});
}
});
6 changes: 6 additions & 0 deletions src/common/event-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const launch_writely = 'launch-writely';

export enum EventName {
launchWritely = 'launch-writely',
openOptionsPage = 'open-options-page',
}
8 changes: 5 additions & 3 deletions src/common/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type MessagePayload = {
type: 'open-options-page'
}
import { EventName } from './event-name';

export type MessagePayload<T extends EventName> = {
type: T;
};
7 changes: 5 additions & 2 deletions src/content/container/ask-writely/result-panel/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useView } from '../../store/view';
import i18next from 'i18next';
import { useResultPanel } from '../../store/result-panel';
import type { MessagePayload } from '@/common/types';
import { EventName } from '@/common/event-name';

export const Header: React.FC = () => {
const { hide, goToInputPage } = useView();
Expand Down Expand Up @@ -52,8 +53,10 @@ export const Header: React.FC = () => {
/>
<Operation
onClick={() => {
chrome.runtime.sendMessage<MessagePayload>({
type: 'open-options-page',
chrome.runtime.sendMessage<
MessagePayload<EventName.openOptionsPage>
>({
type: EventName.openOptionsPage,
});
}}
icon={<DashiconsAdminGeneric />}
Expand Down
18 changes: 18 additions & 0 deletions src/content/container/store/view.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EventName } from '@/common/event-name';
import { MessagePayload } from '@/common/types';
import { useCallback, useEffect, useRef, useState } from 'react';
import { createContainer } from 'unstated-next';
import { useSelectionManager } from './selection';
Expand Down Expand Up @@ -70,6 +72,22 @@ const { useContainer: useView, Provider: ViewProvider } = createContainer(
};
}, []);

useEffect(() => {
const listener = (message: MessagePayload<EventName.launchWritely>) => {
if (message.type !== EventName.launchWritely) {
return;
}

if (viewStatusRef.current === 'none') {
goToInputPage();
}
};

chrome.runtime.onMessage.addListener(listener);

return () => chrome.runtime.onMessage.removeListener(listener);
}, []);

return {
viewStatus,
goToInputPage,
Expand Down
2 changes: 0 additions & 2 deletions src/content/utils/selection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ export class SelectionManager {
this.savedRange = this.selection.getRangeAt(0).cloneRange();
this.setText();
} else {
// this.restoreRange();
this.setText();
this.textPasted = false;
}
}
Expand Down

0 comments on commit ebbf13e

Please sign in to comment.