From 6bf5333b1464d9eb1a890e4e22a7f344147b6a90 Mon Sep 17 00:00:00 2001 From: ksamuel Date: Fri, 14 Aug 2020 11:18:59 +0200 Subject: [PATCH] Reader mode param in URL --- zerobin/paste.py | 11 +++++++---- zerobin/static/js/behavior.js | 30 +++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/zerobin/paste.py b/zerobin/paste.py index bb39ef8..33aecd9 100644 --- a/zerobin/paste.py +++ b/zerobin/paste.py @@ -101,9 +101,9 @@ class Paste(object): expiration = datetime.strptime(expiration, "%Y-%m-%d %H:%M:%S.%f") except StopIteration: - raise TypeError(to_ascii("File %s is malformed" % path)) + raise TypeError("File %s is malformed" % path) except (IOError, OSError): - raise ValueError(to_ascii("Can not open paste from file %s" % path)) + raise ValueError("Can not open paste from file %s" % path) return Paste(uuid=uuid, expiration=expiration, content=content) @@ -230,10 +230,13 @@ class Paste(object): os.remove(self.path) @classmethod - def iter_all(cls): + def iter_all(cls, on_error=lambda e: e): for p in settings.PASTE_FILES_ROOT.rglob("*"): if p.is_file() and "counter" not in str(p): - yield Paste.load_from_file(p) + try: + yield Paste.load_from_file(p) + except (TypeError, ValueError) as e: + on_error(e) @property def has_expired(self): diff --git a/zerobin/static/js/behavior.js b/zerobin/static/js/behavior.js index fcad76b..19559a2 100644 --- a/zerobin/static/js/behavior.js +++ b/zerobin/static/js/behavior.js @@ -45,6 +45,8 @@ const app = new Vue({ clipboard: !!(isSecureContext && navigator.clipboard && navigator.clipboard.writeText), + URLSearchParams: !!window.URLSearchParams, + localStorage: (function () { var val = !!(localStorage); document.querySelector('html').classList.add((val ? '' : 'no-') + 'local-storage'); @@ -68,13 +70,21 @@ const app = new Vue({ }, methods: { - // handleDownload: function () { - // this.$refs.downloadLink.dispatchEvent(new Event("click")); - // }, - toggleReaderMode: function () { + debugger; if (!this.readerMode) { this.messages = []; + if (this.support.URLSearchParams) { + var searchParams = new URLSearchParams(window.location.search) + searchParams.set('readerMode', 1); + window.location.search = searchParams.toString(); + } + } else { + if (this.support.URLSearchParams) { + var searchParams = new URLSearchParams(window.location.search); + searchParams.delete('readerMode'); + window.location.search = searchParams.toString(); + } } this.readerMode = !this.readerMode; @@ -391,6 +401,7 @@ window.zerobin = { doneCallback(content); } } catch (err) { + debugger; errorCallback(err); } @@ -738,6 +749,8 @@ if (content && key) { /* When done */ function (content) { + let readerMode = false; + if (content.indexOf('data:image') == 0) { // Display Image @@ -773,6 +786,10 @@ if (content && key) { url: "data:text/html;charset=UTF-8," + content } + if (app.support.URLSearchParams) { + readerMode = (new URLSearchParams(window.location.search)).get('readerMode'); + } + } bar.set('Code coloration...', '95%'); @@ -781,7 +798,7 @@ if (content && key) { /** Syntaxic coloration */ - if (zerobin.isCode(content) > 100) { + if (zerobin.isCode(content) > 100 && !readerMode) { document.getElementById('paste-content').classList.add('linenums'); prettyPrint(); } else { @@ -805,6 +822,9 @@ if (content && key) { form.forEach((node) => node.disabled = false); content = ''; + if (readerMode) { + app.toggleReaderMode() + } }, 100);