Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Option to format via context menu #290

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ yarn test-only
yarn test
```

### Known Issues

- ContextMenu will be shown on all editable fields, but will work only on textarea
- Elements present inside iframe can't be formatted

## Help

We would love your [help](https://github.com/prettier/prettier-browser-extension/issues) :)
48 changes: 35 additions & 13 deletions src/background/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
import browser from "webextension-polyfill";

browser.commands.onCommand.addListener((command) => {
const menuItemId = "prettier-format";

const menuItem = {
contexts: ["editable"],
id: menuItemId,
title: "Format with Prettier",
};

const format = async (tab) => {
try {
await browser.tabs.sendMessage(tab.id, { action: "runPrettierFormat" });
} catch (err) {
console.error("Error occurred while sending message to tab.", err);
}
};

browser.commands.onCommand.addListener((command, tab) => {
if (command !== "run-prettier-format") {
return;
}

browser.tabs
.query({ active: true, currentWindow: true })
.then((tabs) =>
Promise.all(
tabs.map((tab) =>
browser.tabs.sendMessage(tab.id, { action: "runPrettierFormat" })
)
)
)
.catch((err) => {
console.error("Error occurred while sending message to tab.", err);
});
format(tab);
});

browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId == menuItemId) {
format(tab);
}
});

const createContextMenu = async () => {
try {
await browser.contextMenus.removeAll();
await browser.contextMenus.create(menuItem);
} catch (err) {
console.log("Error while creating context menu", err);
}
};

createContextMenu();
2 changes: 2 additions & 0 deletions src/content/extension.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import browser from "webextension-polyfill";
import prettier from "prettier/standalone";

import Storage from "./storage";
import { PARSERS, PARSERS_LANG_MAP } from "./parsers";

Expand All @@ -11,6 +12,7 @@ const STACKEXCHANGE_SITES = [
"https://stackapps.com",
"https://superuser.com",
];

const STACKEXCHANGE_URL_REGEX = /^https:\/\/([a-z]+).stackexchange.com/;
const STACKEXCHANGE_VALID_PATHNAMES = /(^\/questions|\/posts\/\d+\/edit|^\/review)/u;

Expand Down
3 changes: 3 additions & 0 deletions src/dev-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"description": "Format using Prettier"
}
},
"permissions": ["storage"],
"permissions": ["storage", "contextMenus", "activeTab"],
"content_scripts": [
{
"matches": ["<all_urls>"],
Expand Down
3 changes: 3 additions & 0 deletions src/prod-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"content_security_policy": "script-src 'self'; object-src 'self'"
}
5 changes: 3 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ module.exports = ({ outDir, env }) => {
background: "./src/background/index.js",
content: "./src/content/index.js",
options: isDevMode
? ["react-devtools", "./src/options/index.js"]
: "./src/options/index.js",
? ["react-devtools", "./src/options"]
: "./src/options",
},
mode: env,
module: {
Expand Down Expand Up @@ -85,6 +85,7 @@ module.exports = ({ outDir, env }) => {
files: [
"src/manifest.json",
isFirefox && "src/firefox-manifest.json",
`src/${isDevMode ? "dev" : "prod"}-manifest.json`,
].filter(Boolean),
to: "manifest.json",
transform: (manifest) => ({ version, ...manifest }),
Expand Down