From c7668c7c685ba182d9d6d21e5094e17eac3a352f Mon Sep 17 00:00:00 2001 From: Cam Song Date: Fri, 16 Sep 2016 00:17:00 +0800 Subject: [PATCH] release 0.16.0, add ignore spaces feature, close #5 --- README.md | 6 +++++ ignore-spaces.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ manifest.json | 4 ++-- 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 ignore-spaces.js diff --git a/README.md b/README.md index e6eb17c..c35e6e6 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,12 @@ Amazing native-like 'Download' button in file detail page! ![show notifications](https://cloud.githubusercontent.com/assets/948896/14252559/860a154a-faba-11e5-8721-1b69070b3cdb.png) +#### 7. Ignore whitespace feature + +Add 'Ignore Spaces' and 'Show Spaces' button to let you ignore whitespace in code review. The trick behind this is adding `?w=1` to any diff URL will remove any changes only in whitespace, enabling you to see only that code that has changed. + +![Ignore whitespace](https://camo.githubusercontent.com/797184940defadec00393e6559b835358a863eeb/68747470733a2f2f6769746875622d696d616765732e73332e616d617a6f6e6177732e636f6d2f626c6f672f323031312f736563726574732f776869746573706163652e706e67) + ### TODO * Download folder on one click diff --git a/ignore-spaces.js b/ignore-spaces.js new file mode 100644 index 0000000..f0034f2 --- /dev/null +++ b/ignore-spaces.js @@ -0,0 +1,58 @@ +var IgnoreSpace = { + init: function() { + if (document.querySelectorAll('.ignore-spaces-btn')) { + document.querySelectorAll('.ignore-spaces-btn').forEach(function(node) { + node.remove(); + }) + } + if (this.isValid()) { + if (this.isCurrentIgnore()) { + this.renderShowSpacesBtn(); + } else { + this.renderIgnoreSpacesBtn(); + } + } + }, + + // Check if it's code review diff page. like https://github.com/angular/angular.js/pull/10539/files?w=1 + isValid() { + return document.querySelector('.diffbar') && document.querySelector('.pr-review-tools'); + }, + + isCurrentIgnore() { + return location.search.indexOf('w=1') > -1; + }, + + renderShowSpacesBtn() { + // remove w=1 in url + let url = location.href.replace('w=1&', '').replace('&w=1', '').replace('?w=1', ''); + this.addBtn(this.newBtn(url, 'Show Spaces', 'Click to show spaces in the Diff')); + }, + + renderIgnoreSpacesBtn() { + // add w=1 in url + let url = location.href + (location.href.indexOf('?') > -1 ? '&w=1' : '?w=1'); + this.addBtn(this.newBtn(url, 'Ignore Spaces', 'Click to hide spaces in the Diff')); + }, + + newBtn(url, text, hint) { + let btn = document.createElement('a'); + btn.setAttribute('class', 'btn btn-sm btn-primary tooltipped tooltipped-n ignore-spaces-btn'); + btn.setAttribute('style', 'float: left; margin-right: 20px'); + btn.setAttribute('href', url); + btn.setAttribute('aria-label', hint); + btn.textContent = text; + return btn; + }, + + addBtn(btn) { + document.querySelector('.pr-review-tools').prepend(btn); + } +} + +IgnoreSpace.init(); + +// 当使用 pjax 更新页面时执行 +document.addEventListener('_pjax:end', function() { + IgnoreSpace.init(); +}, false); diff --git a/manifest.json b/manifest.json index 30b8723..1277ae4 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "name": "Octo Mate", "description": "Missing mate of GitHub, making single file download effortless and with more features", "default_locale": "en", - "version": "0.15.2", + "version": "0.16.0", "background": { "persistent": false, "page": "background.html" @@ -18,7 +18,7 @@ "matches": ["https://github.com/*"], "run_at": "document_end", "css": ["style.css"], - "js": ["jquery-2.1.3.min.js", "page.js", "config.js", "script.js", "show-size.js", "tab-size.js", "panel.js"] + "js": ["jquery-2.1.3.min.js", "page.js", "config.js", "script.js", "show-size.js", "tab-size.js", "panel.js", "ignore-spaces.js"] } ], "browser_action": {