Skip to content

Commit

Permalink
add duplicate register contextmenu protection
Browse files Browse the repository at this point in the history
  • Loading branch information
yeweiasia committed Oct 20, 2022
1 parent a3c898d commit 72c9201
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"codearts",
"plugin"
],
"version": "0.4.2",
"version": "0.4.3",
"license": "SEE LICENSE IN LICENSE",
"description": "core plugin api of cloudide frontend and backend",
"repository": {
Expand Down
18 changes: 13 additions & 5 deletions src/browser/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class PluginPage {
private registeredEventHandlers: Map<string, ((eventType: string, event: any) => void)[]> = new Map();
private extensionPath?: string;
private frontends: Map<IFrontendConstructor<AbstractFrontend>, AbstractFrontend> = new Map();
private registeredContextMenu: Map<Document | HTMLElement, (e: any) => void> = new Map();
private constructor(pluginPageContext: PluginPageContext, frontends: IFrontendConstructor<AbstractFrontend>[]) {
this.pluginPageContext = pluginPageContext;
this.cloudidePluginApi = cloudidePluginApi;
Expand Down Expand Up @@ -359,15 +360,22 @@ export class PluginPage {
* @param menu menu items
* @param concat concatenate all menu items registered
*/
public registerContextMenu(target: HTMLElement | Document, menu?: MenuItem[], concat?: boolean) {
target.addEventListener('contextmenu', (e) => {
public registerContextMenu(target: Document | HTMLElement, menu?: MenuItem[], concat?: boolean) {
const existTargetCallBack = this.registeredContextMenu.get(target);
if (existTargetCallBack) {
target.removeEventListener('contextmenu', existTargetCallBack);
this.registeredContextMenu.delete(target);
}
const callback = (e: any) => {
const curMenu: MenuItem[] | undefined = (e as any)['menu'];
if (concat) {
(e as any)['menu'] = curMenu || menu ? [...(curMenu || []), ...(menu || [])] : curMenu;
e['menu'] = curMenu || menu ? [...(curMenu || []), ...(menu || [])] : curMenu;
} else {
(e as any)['menu'] = menu;
e['menu'] = menu;
}
});
};
this.registeredContextMenu.set(target, callback);
target.addEventListener('contextmenu', callback);
}
}

Expand Down

0 comments on commit 72c9201

Please sign in to comment.