Skip to content

Commit

Permalink
Format JS with Prettier
Browse files Browse the repository at this point in the history
Having a .prettierrc means anybody who uses format-on-save will automatically follow project conventions.
  • Loading branch information
irskep committed Jul 14, 2024
1 parent 3da495e commit f94e37a
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 184 deletions.
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 2,
"useTabs": false
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
157 changes: 80 additions & 77 deletions silicon/static/js/edit.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,95 @@
// object for editor state
var editor = {
changed: false,
submit_clicked: false,
changed: false,
submit_clicked: false,
};

if (silicon_editor === 'codemirror') {
var cm_instance;
if (silicon_editor === "codemirror") {
var cm_instance;
}

function usurp_unload(e) {
e.preventDefault();
e.returnValue = '';
e.preventDefault();
e.returnValue = "";
}

window.addEventListener("load", function() {
// load CodeMirror instance
if (silicon_editor === 'codemirror') {
require.config({
baseUrl: js_modules_root
});

require([
"lib/codemirror",
"mode/markdown/markdown",
"mode/clike/clike",
"mode/css/css",
"mode/diff/diff",
"mode/dockerfile/dockerfile",
"mode/gfm/gfm",
"mode/htmlmixed/htmlmixed",
"mode/jinja2/jinja2",
"mode/python/python",
"mode/shell/shell",
"mode/sql/sql",
"mode/yaml/yaml",
"addon/display/fullscreen",
"addon/display/panel",
].map(x => `codemirror/${x}`), function(CodeMirror) {
cm_instance = CodeMirror.fromTextArea(document.querySelector('#body-text'), {
mode: {
name: 'gfm',
gitHubSpice: false,
},
lineSeparator: '\n',
lineWrapping: true,
extraKeys: {
// home/end shouldn't go to the beginning/end of paragraphs
Home: 'goLineLeft',
End: 'goLineRight',
// do not redefine the browser history navigation keys
'Alt-Left': false,
'Alt-Right': false
},
autofocus: true,
// appears to do nothing
spellcheck: true,
viewportMargin: Infinity,
});
});
}
window.addEventListener("load", function () {
// load CodeMirror instance
if (silicon_editor === "codemirror") {
require.config({
baseUrl: js_modules_root,
});

// mark the editor as changed if there is an alert shown
// (implies there was an error saving)
if (document.querySelector('#alerts') !== null) {
editor.changed = true;
};
require([
"lib/codemirror",
"mode/markdown/markdown",
"mode/clike/clike",
"mode/css/css",
"mode/diff/diff",
"mode/dockerfile/dockerfile",
"mode/gfm/gfm",
"mode/htmlmixed/htmlmixed",
"mode/jinja2/jinja2",
"mode/python/python",
"mode/shell/shell",
"mode/sql/sql",
"mode/yaml/yaml",
"addon/display/fullscreen",
"addon/display/panel",
].map((x) => `codemirror/${x}`), function (CodeMirror) {
cm_instance = CodeMirror.fromTextArea(
document.querySelector("#body-text"),
{
mode: {
name: "gfm",
gitHubSpice: false,
},
lineSeparator: "\n",
lineWrapping: true,
extraKeys: {
// home/end shouldn't go to the beginning/end of paragraphs
Home: "goLineLeft",
End: "goLineRight",
// do not redefine the browser history navigation keys
"Alt-Left": false,
"Alt-Right": false,
},
autofocus: true,
// appears to do nothing
spellcheck: true,
viewportMargin: Infinity,
}
);
});
}

// mark the editor as changed if the textarea has changed
document.querySelector('#body-text')
.addEventListener('input', (event) => editor.changed = true);
// mark the editor as changed if there is an alert shown
// (implies there was an error saving)
if (document.querySelector("#alerts") !== null) {
editor.changed = true;
}

// don't nag if the Submit button was clicked
document.querySelector('#page-form').onsubmit = function() {
editor.submit_clicked = true;
};
// mark the editor as changed if the textarea has changed
document
.querySelector("#body-text")
.addEventListener("input", (event) => (editor.changed = true));

window.addEventListener('beforeunload', function (e) {
if (silicon_editor === 'codemirror') {
// alert on changed codemirror
if ((! cm_instance.isClean() && ! editor.submit_clicked)) {
usurp_unload(e);
}
} else {
// alert on changed textarea
if ((editor.changed && ! editor.submit_clicked)) {
usurp_unload(e);
};
}
});
// don't nag if the Submit button was clicked
document.querySelector("#page-form").onsubmit = function () {
editor.submit_clicked = true;
};

window.addEventListener("beforeunload", function (e) {
if (silicon_editor === "codemirror") {
// alert on changed codemirror
if (!cm_instance.isClean() && !editor.submit_clicked) {
usurp_unload(e);
}
} else {
// alert on changed textarea
if (editor.changed && !editor.submit_clicked) {
usurp_unload(e);
}
}
});
});
19 changes: 10 additions & 9 deletions silicon/static/js/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
window.addEventListener("load", function() {
/* Toggle nav sidebar in "mobile" mode */
document.querySelector("[data-toggle='nav-top']")
.addEventListener('click', (event) => {
const elements = document.querySelectorAll("[data-toggle='nav-shift']");
window.addEventListener("load", function () {
/* Toggle nav sidebar in "mobile" mode */
document
.querySelector("[data-toggle='nav-top']")
.addEventListener("click", (event) => {
const elements = document.querySelectorAll("[data-toggle='nav-shift']");

elements.forEach(function(element) {
element.classList.toggle('shift');
})
})
elements.forEach(function (element) {
element.classList.toggle("shift");
});
});
});
Loading

0 comments on commit f94e37a

Please sign in to comment.