Skip to content

Commit a6c1bb0

Browse files
committed
Format JS with Prettier
Having a .prettierrc means anybody who uses format-on-save will automatically follow project conventions.
1 parent 3da495e commit a6c1bb0

6 files changed

Lines changed: 136 additions & 113 deletions

File tree

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ insert_final_newline = true
99
[*.{js,py,md,css,j2,html,yaml,sh,toml}]
1010
charset = utf-8
1111

12-
[*.{html,js,yaml}]
12+
[*.{html,yaml}]
1313
indent_size = 2

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"tabWidth": 4,
3+
"useTabs": false
4+
}

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"editor.defaultFormatter": "esbenp.prettier-vscode"
4+
}

silicon/static/js/edit.js

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,89 +4,92 @@ var editor = {
44
submit_clicked: false,
55
};
66

7-
if (silicon_editor === 'codemirror') {
7+
if (silicon_editor === "codemirror") {
88
var cm_instance;
99
}
1010

1111
function usurp_unload(e) {
1212
e.preventDefault();
13-
e.returnValue = '';
13+
e.returnValue = "";
1414
}
1515

16-
window.addEventListener("load", function() {
16+
window.addEventListener("load", function () {
1717
// load CodeMirror instance
18-
if (silicon_editor === 'codemirror') {
18+
if (silicon_editor === "codemirror") {
1919
require.config({
20-
baseUrl: js_modules_root
20+
baseUrl: js_modules_root,
2121
});
2222

2323
require([
24-
"lib/codemirror",
25-
"mode/markdown/markdown",
26-
"mode/clike/clike",
27-
"mode/css/css",
28-
"mode/diff/diff",
29-
"mode/dockerfile/dockerfile",
30-
"mode/gfm/gfm",
31-
"mode/htmlmixed/htmlmixed",
32-
"mode/jinja2/jinja2",
33-
"mode/python/python",
34-
"mode/shell/shell",
35-
"mode/sql/sql",
36-
"mode/yaml/yaml",
37-
"addon/display/fullscreen",
38-
"addon/display/panel",
39-
].map(x => `codemirror/${x}`), function(CodeMirror) {
40-
cm_instance = CodeMirror.fromTextArea(document.querySelector('#body-text'), {
41-
mode: {
42-
name: 'gfm',
43-
gitHubSpice: false,
44-
},
45-
lineSeparator: '\n',
46-
lineWrapping: true,
47-
extraKeys: {
48-
// home/end shouldn't go to the beginning/end of paragraphs
49-
Home: 'goLineLeft',
50-
End: 'goLineRight',
51-
// do not redefine the browser history navigation keys
52-
'Alt-Left': false,
53-
'Alt-Right': false
54-
},
55-
autofocus: true,
56-
// appears to do nothing
57-
spellcheck: true,
58-
viewportMargin: Infinity,
59-
});
24+
"lib/codemirror",
25+
"mode/markdown/markdown",
26+
"mode/clike/clike",
27+
"mode/css/css",
28+
"mode/diff/diff",
29+
"mode/dockerfile/dockerfile",
30+
"mode/gfm/gfm",
31+
"mode/htmlmixed/htmlmixed",
32+
"mode/jinja2/jinja2",
33+
"mode/python/python",
34+
"mode/shell/shell",
35+
"mode/sql/sql",
36+
"mode/yaml/yaml",
37+
"addon/display/fullscreen",
38+
"addon/display/panel",
39+
].map((x) => `codemirror/${x}`), function (CodeMirror) {
40+
cm_instance = CodeMirror.fromTextArea(
41+
document.querySelector("#body-text"),
42+
{
43+
mode: {
44+
name: "gfm",
45+
gitHubSpice: false,
46+
},
47+
lineSeparator: "\n",
48+
lineWrapping: true,
49+
extraKeys: {
50+
// home/end shouldn't go to the beginning/end of paragraphs
51+
Home: "goLineLeft",
52+
End: "goLineRight",
53+
// do not redefine the browser history navigation keys
54+
"Alt-Left": false,
55+
"Alt-Right": false,
56+
},
57+
autofocus: true,
58+
// appears to do nothing
59+
spellcheck: true,
60+
viewportMargin: Infinity,
61+
}
62+
);
6063
});
6164
}
6265

6366
// mark the editor as changed if there is an alert shown
6467
// (implies there was an error saving)
65-
if (document.querySelector('#alerts') !== null) {
68+
if (document.querySelector("#alerts") !== null) {
6669
editor.changed = true;
67-
};
70+
}
6871

6972
// mark the editor as changed if the textarea has changed
70-
document.querySelector('#body-text')
71-
.addEventListener('input', (event) => editor.changed = true);
73+
document
74+
.querySelector("#body-text")
75+
.addEventListener("input", (event) => (editor.changed = true));
7276

7377
// don't nag if the Submit button was clicked
74-
document.querySelector('#page-form').onsubmit = function() {
78+
document.querySelector("#page-form").onsubmit = function () {
7579
editor.submit_clicked = true;
7680
};
7781

78-
window.addEventListener('beforeunload', function (e) {
79-
if (silicon_editor === 'codemirror') {
82+
window.addEventListener("beforeunload", function (e) {
83+
if (silicon_editor === "codemirror") {
8084
// alert on changed codemirror
81-
if ((! cm_instance.isClean() && ! editor.submit_clicked)) {
85+
if (!cm_instance.isClean() && !editor.submit_clicked) {
8286
usurp_unload(e);
8387
}
8488
} else {
8589
// alert on changed textarea
86-
if ((editor.changed && ! editor.submit_clicked)) {
90+
if (editor.changed && !editor.submit_clicked) {
8791
usurp_unload(e);
88-
};
92+
}
8993
}
9094
});
91-
9295
});

silicon/static/js/main.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
window.addEventListener("load", function() {
1+
window.addEventListener("load", function () {
22
/* Toggle nav sidebar in "mobile" mode */
3-
document.querySelector("[data-toggle='nav-top']")
4-
.addEventListener('click', (event) => {
5-
const elements = document.querySelectorAll("[data-toggle='nav-shift']");
3+
document
4+
.querySelector("[data-toggle='nav-top']")
5+
.addEventListener("click", (event) => {
6+
const elements = document.querySelectorAll(
7+
"[data-toggle='nav-shift']"
8+
);
69

7-
elements.forEach(function(element) {
8-
element.classList.toggle('shift');
9-
})
10-
})
10+
elements.forEach(function (element) {
11+
element.classList.toggle("shift");
12+
});
13+
});
1114
});

silicon/static/js/widgets.js

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ function get_widget(target) {
1313

1414
return {
1515
element: widget_element,
16-
url: widget_element.getAttribute("data-widget-url")
16+
url: widget_element.getAttribute("data-widget-url"),
1717
};
1818
}
1919

2020
/*
2121
* Set up relation add button event.
2222
*/
2323
function relation_add_button() {
24-
document.querySelector("#add-relation-btn")
24+
document
25+
.querySelector("#add-relation-btn")
2526
.addEventListener("click", (event) => {
2627
const relative = window.prompt("Related page title");
2728

@@ -33,53 +34,59 @@ function relation_add_button() {
3334
const widget = get_widget("#related-links");
3435
fetch(widget.url, {
3536
method: "POST",
36-
body: new URLSearchParams({relative: relative}),
37+
body: new URLSearchParams({ relative: relative }),
3738
headers: new Headers({
38-
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
39-
})
40-
})
41-
.then(response => {
42-
if (response.ok) {
43-
return response.text();
44-
}
45-
throw new Error(`Error fetching ${response.url}, got ${response.status}`);
46-
})
47-
.then(html => {
48-
widget.element.innerHTML = html;
49-
relation_delete_buttons();
39+
"Content-type":
40+
"application/x-www-form-urlencoded; charset=UTF-8",
41+
}),
5042
})
51-
.catch(err => {
52-
console.error(err);
53-
});
43+
.then((response) => {
44+
if (response.ok) {
45+
return response.text();
46+
}
47+
throw new Error(
48+
`Error fetching ${response.url}, got ${response.status}`
49+
);
50+
})
51+
.then((html) => {
52+
widget.element.innerHTML = html;
53+
relation_delete_buttons();
54+
})
55+
.catch((err) => {
56+
console.error(err);
57+
});
5458
});
5559
}
5660

5761
/*
5862
* Set up relation delete button events.
5963
*/
6064
function relation_delete_buttons() {
61-
const del_btns = document.querySelectorAll(".del-relation-btn")
65+
const del_btns = document.querySelectorAll(".del-relation-btn");
6266
const widget = get_widget("#related-links");
6367

64-
del_btns.forEach(function(btn) {
68+
del_btns.forEach(function (btn) {
6569
const relative = btn.getAttribute("data-del-relative");
6670
btn.addEventListener("click", (event) => {
67-
const answer = window.confirm(`Delete this page's relationship to ${relative}?`);
71+
const answer = window.confirm(
72+
`Delete this page's relationship to ${relative}?`
73+
);
6874
if (answer === true) {
6975
fetch(`${widget.url}/${relative}`, {
7076
method: "DELETE",
71-
}).then(response => {
72-
return response.text();
73-
}).then(html => {
74-
widget.element.innerHTML = html;
75-
relation_delete_buttons();
7677
})
78+
.then((response) => {
79+
return response.text();
80+
})
81+
.then((html) => {
82+
widget.element.innerHTML = html;
83+
relation_delete_buttons();
84+
});
7785
}
7886
});
7987
});
8088
}
8189

82-
8390
/*
8491
* Set up the table of contents update button.
8592
*/
@@ -89,49 +96,51 @@ function toc_update_button() {
8996
return;
9097
}
9198

92-
document.querySelector("#update-toc").style.visibility = 'visible';
99+
document.querySelector("#update-toc").style.visibility = "visible";
93100

94-
document.querySelector("#update-toc-btn")
95-
.addEventListener("click", (event => {
101+
document
102+
.querySelector("#update-toc-btn")
103+
.addEventListener("click", (event) => {
96104
const widget = get_widget("#toc");
97105
// get the contents of CodeMirror, or the textarea
98106
let body_text;
99107
if (document.querySelector(".CodeMirror")) {
100-
body_text = document.querySelector(".CodeMirror").CodeMirror.getValue();
108+
body_text = document
109+
.querySelector(".CodeMirror")
110+
.CodeMirror.getValue();
101111
} else {
102112
body_text = document.querySelector("#body-text").value;
103113
}
104114

105115
fetch(widget.url, {
106116
method: "POST",
107-
body: new URLSearchParams(
108-
{body: body_text}
109-
),
117+
body: new URLSearchParams({ body: body_text }),
110118
headers: new Headers({
111-
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
112-
})
113-
})
114-
.then(response => {
115-
if (response.ok) {
116-
return response.text();
117-
}
118-
throw new Error(`Error fetching ${response.url}, got ${response.status}`);
119+
"Content-type":
120+
"application/x-www-form-urlencoded; charset=UTF-8",
121+
}),
119122
})
120-
.then(html => {
121-
widget.element.innerHTML = html;
122-
})
123-
.catch(err => {
124-
console.error(err);
125-
});
126-
127-
}));
123+
.then((response) => {
124+
if (response.ok) {
125+
return response.text();
126+
}
127+
throw new Error(
128+
`Error fetching ${response.url}, got ${response.status}`
129+
);
130+
})
131+
.then((html) => {
132+
widget.element.innerHTML = html;
133+
})
134+
.catch((err) => {
135+
console.error(err);
136+
});
137+
});
128138
}
129139

130-
131140
/*
132141
* Set up all the page events.
133142
*/
134-
window.addEventListener("load", function() {
143+
window.addEventListener("load", function () {
135144
relation_add_button();
136145
relation_delete_buttons();
137146
toc_update_button();

0 commit comments

Comments
 (0)