From 86f358ba9ff68008e52b966dc57d1c580c52e032 Mon Sep 17 00:00:00 2001 From: Ng Guoyou Date: Thu, 26 Jul 2018 22:26:12 +0800 Subject: [PATCH] Add variables list view to options page --- .eslintrc | 4 +++- CHANGELOG.md | 1 + _locales/en/messages.json | 8 ++++++++ manifest.json | 2 +- package.json | 2 +- src/index.js | 3 ++- src/messaging.js | 25 ++++++++++++++++++++----- src/options/options.html | 15 ++++++++++++++- src/options/options.js | 38 ++++++++++++++++++++++++++++++++++++++ src/options/style.css | 2 ++ 10 files changed, 90 insertions(+), 10 deletions(-) diff --git a/.eslintrc b/.eslintrc index d5b4b4c..88b867d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -39,6 +39,8 @@ "CLICK_TYPES": false, "BROWSERS": false, - "CURRENT_BROWSER": false + "CURRENT_BROWSER": false, + + "addClickToCopy": false } } diff --git a/CHANGELOG.md b/CHANGELOG.md index b35fd0c..4b15a05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 3.0.0 * Fix Chrome rules matching against `_`, now matches against special characters instead +* Add variables view to last download in options * Fix routing failing in some cases (regression) (#80) * Fix last used access key not working at all (Chrome) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 3ee7c74..a619d6c 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -304,6 +304,14 @@ "message": "Capture groups" }, + "o_lRoutingVariables": { + "message": "Variables" + }, + + "o_lRoutingVariablesClickToSee": { + "message": "Click to reveal" + }, + "o_lRoutingLastDownloadHelp": { "message": "Test your current rules, applied to the last downloaded file" }, diff --git a/manifest.json b/manifest.json index e456fbd..00338b5 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "__MSG_extensionName__", "description": "__MSG_extensionDescription__", - "version": "2.7.4", + "version": "3.0.0", "default_locale": "en", "applications": { diff --git a/package.json b/package.json index b1307ce..7b1a62d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "save-in", - "version": "2.7.4", + "version": "3.0.0", "license": "MIT", "scripts": { "build": "env -u WEB_EXT_API_KEY -u WEB_EXT_API_SECRET web-ext build --overwrite-dest -i test docs yarn.lock yarn-error.log", diff --git a/src/index.js b/src/index.js index ec744fc..c38fa2d 100644 --- a/src/index.js +++ b/src/index.js @@ -64,7 +64,8 @@ window.init = () => { } if (options.enableLastLocation) { - const lastUsedTitle = lastUsedPath || browser.i18n.getMessage("contextMenuLastUsed"); + const lastUsedTitle = + lastUsedPath || browser.i18n.getMessage("contextMenuLastUsed"); const lastUsedMenuOptions = { id: `save-in-_-_-last-used`, title: setAccesskey(lastUsedTitle, options.keyLastUsed), diff --git a/src/messaging.js b/src/messaging.js index 17a412d..8cd8146 100644 --- a/src/messaging.js +++ b/src/messaging.js @@ -27,15 +27,30 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => { }); break; case MESSAGE_TYPES.CHECK_ROUTES: + const lastState = + (request.body && request.body.state) || + (window.lastDownloadState != null && window.lastDownloadState); + + const interpolatedVariables = lastState + ? Object.keys(Variable.transformers).reduce( + (acc, val) => + Object.assign(acc, { + [val]: Variable.applyVariables( + new Path.Path(val), + lastState.info + ).finalize() + }), + {} + ) + : null; + sendResponse({ type: MESSAGE_TYPES.CHECK_ROUTES_RESPONSE, body: { optionErrors: window.optionErrors, - routeInfo: OptionsManagement.checkRoutes( - (request.body && request.body.state) || - (window.lastDownloadState != null && window.lastDownloadState) - ), - lastDownload: window.lastDownloadState + routeInfo: OptionsManagement.checkRoutes(lastState), + lastDownload: window.lastDownloadState, + interpolatedVariables } }); break; diff --git a/src/options/options.html b/src/options/options.html index 3fb588f..98b5947 100644 --- a/src/options/options.html +++ b/src/options/options.html @@ -285,7 +285,7 @@

Moving downloads into different directories

__MSG_o_lRoutingLastDownloadEmpty__ - + __MSG_o_lRoutingRenamedTo__ __MSG_o_lRoutingNoMatches__ @@ -293,6 +293,19 @@

Moving downloads into different directories

__MSG_o_lRoutingCaptureGroups__ + + + __MSG_o_lRoutingVariables__ + + +
__MSG_o_lRoutingVariablesClickToSee__
+ + + +
+ + + diff --git a/src/options/options.js b/src/options/options.js index ee2f5d8..371fbed 100644 --- a/src/options/options.js +++ b/src/options/options.js @@ -63,6 +63,42 @@ const updateErrors = () => { lastDlMatch.textContent = body.routeInfo.path; } + // Variables + if (hasLastDownload) { + document + .querySelector("#variables-table-row") + .classList.toggle("hide", !hasLastDownload); + } + document + .querySelector("#see-variables-btn") + .addEventListener("click", () => { + if (body.interpolatedVariables) { + const tableBody = document.querySelector("#variables-body"); + tableBody.classList.toggle("hide"); + tableBody.innerHTML = ""; + + Object.keys(body.interpolatedVariables).forEach(key => { + const val = body.interpolatedVariables[key]; + + const variableRow = document.createElement("tr"); + + const nameEl = document.createElement("td"); + nameEl.textContent = key; + nameEl.classList.add("click-to-copy"); + nameEl.classList.add("code"); + addClickToCopy(nameEl); + + const interpolatedEl = document.createElement("td"); + interpolatedEl.style.fontFamily = "monospace"; + interpolatedEl.textContent = val; + + variableRow.appendChild(nameEl); + variableRow.appendChild(interpolatedEl); + tableBody.appendChild(variableRow); + }); + } + }); + // Capture groups const hasCaptureMatches = body.routeInfo && Array.isArray(body.routeInfo.captures); @@ -83,6 +119,8 @@ const updateErrors = () => { const code = document.createElement("code"); code.innerText = `:$${i + 1}:`; + code.classList.add("click-to-copy"); + addClickToCopy(code); div.appendChild(code); const value = document.createElement("div"); diff --git a/src/options/style.css b/src/options/style.css index 987cd63..687b554 100644 --- a/src/options/style.css +++ b/src/options/style.css @@ -58,6 +58,7 @@ html { min-height: 1%; /* Firefox options page height hack */ } +.code, code, pre { font-family: monospace; @@ -70,6 +71,7 @@ pre { overflow-x: auto; } +.code, code { padding: 4px; }