Skip to content

Commit c1c4ec7

Browse files
committed
step: can save/load/delete drafts on input/focus/submit
1 parent dbd23d4 commit c1c4ec7

6 files changed

Lines changed: 2906 additions & 0 deletions

File tree

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "github-comment-draft",
3+
"version": "1.0.0",
4+
"description": "Lets you stop whining over the long github comment loss",
5+
"private": true,
6+
"scripts": {
7+
"build": "webpack",
8+
"watch": "webpack --watch"
9+
},
10+
"husky": {
11+
"hooks": {
12+
"pre-commit": "pretty-quick --staged"
13+
}
14+
},
15+
"keywords": [],
16+
"author": "",
17+
"license": "ISC",
18+
"devDependencies": {
19+
"clean-webpack-plugin": "^0.1.19",
20+
"copy-webpack-plugin": "^4.5.2",
21+
"husky": "^1.0.0-rc.13",
22+
"prettier": "^1.14.2",
23+
"pretty-quick": "^1.6.0",
24+
"webpack": "^4.17.1",
25+
"webpack-cli": "^3.1.0"
26+
}
27+
}

src/content-script.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { get, set, remove } from "./storage";
2+
3+
const FOCUS_COLOR = "darkblue";
4+
const FOCUS_STYLES = [
5+
{ key: "border-color", value: `${FOCUS_COLOR}` },
6+
{ key: "box-shadow", value: `0 0 10px ${FOCUS_COLOR}` }
7+
];
8+
9+
function doShit(elem, pathname) {
10+
const elemId = elem.id;
11+
12+
FOCUS_STYLES.forEach(({ key, value }) => {
13+
elem.style.setProperty(key, value, "important");
14+
elem.nextElementSibling.style.setProperty(key, value, "important")
15+
});
16+
const draft = elem.value;
17+
if (!draft) {
18+
elem.value = get(`drafts:${pathname}:${elemId}`);
19+
}
20+
21+
elem.addEventListener("blur", function() {
22+
FOCUS_STYLES.forEach(({ key, value }) => {
23+
elem.style.setProperty(key, "");
24+
elem.nextElementSibling.style.setProperty(key, "")
25+
})
26+
});
27+
28+
elem.addEventListener("input", function(e) {
29+
const draft = e.target.value;
30+
if (!draft) return;
31+
set(`drafts:${pathname}:${elemId}`, draft);
32+
});
33+
34+
elem.closest("form").addEventListener("submit", function() {
35+
remove(`drafts:${pathname}:${elemId}`);
36+
});
37+
}
38+
39+
function onFocusAny(e) {
40+
if (e.target.tagName !== "TEXTAREA") return;
41+
const pathname = location.pathname;
42+
doShit(e.target, pathname);
43+
}
44+
45+
// seems like the easiest certain way to cover all we want
46+
document.addEventListener("focus", onFocusAny, true);

src/manifest.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Github Comment Draft",
3+
"version": "1.0.0",
4+
"description": "Lets you stop whining over the long github comment loss",
5+
"manifest_version": 2,
6+
"permissions": ["activeTab"],
7+
"content_scripts": [
8+
{
9+
"run_at": "document_start",
10+
"matches": ["https://github.com/*"],
11+
"js": ["content-script.js"]
12+
}
13+
]
14+
}

src/storage.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const PREFIX = "github-comment-draft:";
2+
3+
export const set = (key, value) => {
4+
localStorage.setItem(`${PREFIX}${key}`, value);
5+
return value;
6+
};
7+
8+
export const get = key => localStorage.getItem(`${PREFIX}${key}`);
9+
10+
export const remove = key => localStorage.removeItem(`${PREFIX}${key}`);

webpack.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const path = require("path");
2+
const CopyWebpackPlugin = require("copy-webpack-plugin");
3+
const CleanWebpackPlugin = require("clean-webpack-plugin");
4+
5+
module.exports = {
6+
mode: "production",
7+
entry: { "content-script": "./src/content-script.js" },
8+
output: {
9+
path: path.resolve(__dirname, "dist"),
10+
filename: "[name].js"
11+
},
12+
plugins: [
13+
new CleanWebpackPlugin(["dist"]),
14+
new CopyWebpackPlugin(["src/manifest.json"])
15+
]
16+
};

0 commit comments

Comments
 (0)