Skip to content

Commit 163d3aa

Browse files
committed
chore: adding response JSON text format
Signed-off-by: Pawel Psztyc <[email protected]>
1 parent e826c50 commit 163d3aa

File tree

6 files changed

+105
-31
lines changed

6 files changed

+105
-31
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"Renderable",
44
"advancedrestclient",
55
"appresources",
6+
"arccontextmenu",
67
"autoupdate",
78
"camelcase",
89
"codemirror",

package-lock.json

Lines changed: 29 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"@types/pouchdb": "^6.4.0",
6868
"api-console": "^6.4.0",
6969
"babel-eslint": "^10.1.0",
70-
"chai": "^4.2.0",
70+
"chai": "^4.3.0",
7171
"chance": "^1.1.7",
7272
"dotenv": "^8.2.0",
7373
"electron": "^11.2.2",
@@ -91,7 +91,7 @@
9191
"typescript-lit-html-plugin": "^0.9.0"
9292
},
9393
"dependencies": {
94-
"@advanced-rest-client/arc-events": "^0.2.13",
94+
"@advanced-rest-client/arc-events": "^0.2.14",
9595
"@advanced-rest-client/arc-types": "^0.2.49",
9696
"@advanced-rest-client/electron-amf-service": "^5.0.0",
9797
"@advanced-rest-client/electron-oauth2": "^5.0.1",

src/app/scripts/arc/ArcContextMenuCommands.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ const commands = /** @type ContextMenuCommand[] */ ([
6060
const itemTarget = /** @type HTMLElement */ (target.closest('.request-list-item'));
6161
ArcModelEvents.Request.delete(root, type, itemTarget.dataset.id);
6262
},
63+
},
64+
65+
// response view manipulation
66+
{
67+
selector: 'response-highlighter',
68+
group: "main",
69+
label: "Format",
70+
title: 'Format the response',
71+
icon: 'code',
72+
execute: (args) => {
73+
const { target } = args;
74+
// @ts-ignore
75+
target.format();
76+
}
6377
}
6478
]);
6579

src/app/scripts/context-menu/ContextMenu.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import '../../../../web_modules/@anypoint-web-components/anypoint-listbox/anypoint-listbox.js';
22
import '../../../../web_modules/@anypoint-web-components/anypoint-item/anypoint-icon-item.js';
33
import '../../../../web_modules/@advanced-rest-client/arc-icons/arc-icon.js';
4+
import { UiEventTypes } from '../../../../web_modules/@advanced-rest-client/arc-events/index.js';
45
import { ContextMenuStore } from './ContextMenuStore.js';
56

67
/** @typedef {import('./interfaces').RegisteredCommand} RegisteredCommand */
@@ -65,6 +66,11 @@ export class ContextMenu {
6566
*/
6667
currentMenu = undefined;
6768

69+
/**
70+
* @type {any}
71+
*/
72+
customArgs = undefined;
73+
6874
/**
6975
* @type {ClickVector}
7076
*/
@@ -85,6 +91,7 @@ export class ContextMenu {
8591
this.menuClickHandler = this.menuClickHandler.bind(this);
8692
this.clickHandler = this.clickHandler.bind(this);
8793
this.keydownHandler = this.keydownHandler.bind(this);
94+
this.customHandler = this.customHandler.bind(this);
8895
}
8996

9097
/**
@@ -104,6 +111,7 @@ export class ContextMenu {
104111
this.workspace.addEventListener('contextmenu', this.contextHandler);
105112
window.addEventListener('click', this.clickHandler);
106113
window.addEventListener('keydown', this.keydownHandler);
114+
window.addEventListener(UiEventTypes.contextMenu, this.customHandler);
107115
this.connectedValue = true;
108116
}
109117

@@ -115,6 +123,7 @@ export class ContextMenu {
115123
this.workspace.removeEventListener('contextmenu', this.contextHandler);
116124
window.removeEventListener('click', this.clickHandler);
117125
window.removeEventListener('keydown', this.keydownHandler);
126+
window.removeEventListener(UiEventTypes.contextMenu, this.customHandler);
118127
this.connectedValue = false;
119128
}
120129

@@ -141,6 +150,34 @@ export class ContextMenu {
141150
this.build(e, target, clickVector, targetVector);
142151
}
143152

153+
/**
154+
* A handler for the `arccontextmenu` event that triggers the menu for given target passed in the
155+
* event's detail object.
156+
* @param {CustomEvent} e
157+
*/
158+
customHandler(e) {
159+
const { target, mouseEvent, selector, args } = e.detail;
160+
const commands = this.listCustomCommands(selector);
161+
if (!commands.length) {
162+
return;
163+
}
164+
mouseEvent.preventDefault();
165+
mouseEvent.stopPropagation();
166+
const clickVector = {
167+
x: mouseEvent.clientX,
168+
y: mouseEvent.clientY,
169+
}
170+
const targetVector = this.readClickPosition(mouseEvent);
171+
172+
this.targetVector = targetVector;
173+
this.customArgs = args;
174+
const groups = this.groupCommands(commands);
175+
this.currentCommands = commands;
176+
this.currentTarget = target;
177+
this.render(target, clickVector, groups);
178+
target.setAttribute('active', '');
179+
}
180+
144181
/**
145182
* Reads {x,y} vector of the click from the pointer event.
146183
* @param {MouseEvent} e
@@ -292,6 +329,23 @@ export class ContextMenu {
292329
return result;
293330
}
294331

332+
/**
333+
* Lists all custom commands where the command's selector matches passed `selector`.
334+
*
335+
* @param {string} selector The build target
336+
* @returns {RegisteredCommand[]}
337+
*/
338+
listCustomCommands(selector) {
339+
const { commands } = this;
340+
const result =/** @type RegisteredCommand[] */ ([]);
341+
commands.forEach((cmd) => {
342+
if (cmd.selector === '*' || cmd.selector === selector) {
343+
result.push(cmd);
344+
}
345+
});
346+
return result;
347+
}
348+
295349
/**
296350
* Creates an ordered grouped list of commands
297351
*
@@ -422,6 +476,9 @@ export class ContextMenu {
422476
vector: targetVector,
423477
command: command.command,
424478
};
479+
if (this.customArgs) {
480+
opts.customArgs = this.customArgs;
481+
}
425482
if (this.#callback && command.command) {
426483
this.#callback(opts);
427484
} else {

src/app/scripts/context-menu/interfaces.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export declare interface EnabledOptions {
1818
* The element with which this context menu was initialized with.
1919
*/
2020
root: HTMLElement;
21+
customArgs?: any;
2122
}
2223

2324
export declare interface ExecuteOptions {
@@ -41,6 +42,7 @@ export declare interface ExecuteOptions {
4142
* The value of the `command` set on the command.
4243
*/
4344
command?: string;
45+
customArgs?: any;
4446
}
4547

4648
export declare interface ContextMenuCommand {

0 commit comments

Comments
 (0)