summaryrefslogtreecommitdiff
path: root/pastepy/static/js
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2019-03-15 23:32:06 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2019-03-15 23:32:06 +0100
commit7a0cdd41297431fcb515efea98c6ba2bc9a993c8 (patch)
treebc1328d6d13223444baa436e094a8baac6fa209d /pastepy/static/js
parentdcf0e725b2c709a8c5ac6e6ba1311003bfe7e672 (diff)
Redesign everything with bootstrap 4 and bootswatch themes
Diffstat (limited to 'pastepy/static/js')
-rw-r--r--pastepy/static/js/edit.js83
-rw-r--r--pastepy/static/js/theme.js41
-rw-r--r--pastepy/static/js/view.js30
3 files changed, 154 insertions, 0 deletions
diff --git a/pastepy/static/js/edit.js b/pastepy/static/js/edit.js
new file mode 100644
index 0000000..8e754d8
--- /dev/null
+++ b/pastepy/static/js/edit.js
@@ -0,0 +1,83 @@
+function tab_add(t, ti, ss, se) {
+ t.value = t.value.substr(0, ti) + "\t" + t.value.substr(ti);
+ t.selectionStart = ss + 1;
+ t.selectionEnd = se + 1;
+}
+
+function tab_remove(t, ti, ss, se) {
+ t.value = t.value.substr(0, ti-1) + t.value.substr(ti);
+ t.selectionStart = ss - 1;
+ t.selectionEnd = se - 1;
+}
+
+function textarea_handle_tab(t, e) {
+ if(navigator.userAgent.match("Gecko")) {
+ keyCode = e.which;
+ } else {
+ keyCode = e.keyCode;
+ }
+ if(keyCode != 9)
+ return;
+ e.preventDefault();
+ var ss = t.selectionStart;
+ var se = t.selectionEnd;
+ if(ss == se) {
+ if(e.shiftKey) {
+ if(t.value[ss-1] == "\t") {
+ tab_remove(t, ss, ss, se);
+ }
+ } else {
+ tab_add(t, ss, ss, se);
+ }
+ } else {
+ var s;
+ if(ss < se)
+ s = t.value.substr(ss, se - ss);
+ else
+ s = t.value.substr(se, ss - se);
+ if(s.indexOf("\n") > -1) {
+ var nl = s.lastIndexOf("\n");
+ if(e.shiftKey) {
+ while(nl > -1) {
+ if(s[nl+1] == "\t") {
+ tab_remove(t, ss + nl + 2, ss, se);
+ se--;
+ }
+ nl = s.lastIndexOf("\n", nl - 2);
+ }
+ if(t.value[(ss < se ? ss : se)-1] == "\n" && e.shiftKey)
+ tab_remove(t, ss+1, ss, se);
+ if((ss == 0 || se == 0) && t.value[0] == "\t")
+ tab_remove(t, 1, ss, se);
+ } else {
+ while(nl > -1) {
+ tab_add(t, ss + nl + 1, ss, se);
+ se++;
+ if(nl == 0)
+ break;
+ nl = s.lastIndexOf("\n", nl - 2);
+ }
+ if(ss == 0 || se == 0 || t.value[(ss < se ? ss : se)-1] == "\n")
+ tab_add(t, ss, ss-1, se);
+ }
+
+ } else {
+ var nl = t.value.lastIndexOf("\n", (ss < se ? se : ss) - 1) + 1;
+ if(e.shiftKey) {
+ if(t.value[nl] == "\t") {
+ tab_remove(t, nl + 1, ss, se);
+ }
+ } else {
+ tab_add(t, nl, ss, se);
+ }
+ }
+ }
+ setTimeout("document.getElementById('" + t.id + "').focus();", 0);
+}
+
+window.onload = function() {
+ text = document.getElementById("text");
+ text.onkeydown = function(event) {
+ return textarea_handle_tab(text, event);
+ }
+}
diff --git a/pastepy/static/js/theme.js b/pastepy/static/js/theme.js
new file mode 100644
index 0000000..d13c120
--- /dev/null
+++ b/pastepy/static/js/theme.js
@@ -0,0 +1,41 @@
+function set_theme(name) {
+ var themes = {
+ default: {
+ bootstrap: '/static/css/bootstrap.flatly.min.css',
+ pygments: '/highlight_stylesheet',
+ pastepy: '/static/css/paste.css',
+ },
+ dark: {
+ bootstrap: '/static/css/bootstrap.darkly.min.css',
+ pygments: '/highlight_stylesheet/paraiso-dark',
+ pastepy: '/static/css/paste.dark.css',
+ }
+ };
+ var bootstrap_css = document.getElementById('bootstrap-css');
+ var pygments_css = document.getElementById('pygments-css');
+ var pastepy_css = document.getElementById('pastepy-css');
+ var theme = themes[name];
+ bootstrap_css.href = theme.bootstrap;
+ if (pygments_css) {
+ pygments_css.href = theme.pygments;
+ }
+ if (pastepy_css) {
+ pastepy_css.href = theme.pastepy;
+ }
+ var theme = localStorage.setItem('theme', name);
+}
+function switch_theme(event) {
+ if (event && event.type !== 'load') {
+ event.preventDefault();
+ }
+ var bootstrap_css = document.getElementById('bootstrap-css');
+ var pygments_css = document.getElementById('pygments-css');
+ var theme = localStorage.getItem('theme');
+ set_theme(theme === 'dark' ? 'default' : 'dark');
+}
+window.addEventListener('load', function() {
+ var theme = localStorage.getItem('theme');
+ if (theme) {
+ set_theme(theme);
+ }
+});
diff --git a/pastepy/static/js/view.js b/pastepy/static/js/view.js
new file mode 100644
index 0000000..0242aa3
--- /dev/null
+++ b/pastepy/static/js/view.js
@@ -0,0 +1,30 @@
+function set_highlight(line) {
+ var line = 'codeline-' + line
+ var codeline = document.getElementById(line);
+ if (codeline) {
+ codeline.className = 'selected';
+ }
+}
+function cleanup() {
+ var elements = document.getElementsByClassName('selected');
+ for (var i = 0; i < elements.length; i++) {
+ elements[i].className = '';
+ }
+}
+window.addEventListener('load', function(event) {
+ var line = document.location.hash.substr(6);
+ if (line) {
+ set_highlight(line);
+ }
+ var linenodivs = document.getElementsByClassName('linenodiv');
+ if (linenodivs.length === 0) {
+ return;
+ }
+ var as = linenodivs[0].getElementsByTagName('a');
+ for (var i = 0; i < as.length; i++) {
+ as[i].onclick = function(event) {
+ cleanup();
+ set_highlight(event.target.text.trim());
+ }
+ }
+});