Skip to content

Commit

Permalink
Adds more keyboard shortcuts, push to 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Ober committed Jul 19, 2020
1 parent e5b3539 commit 44c966a
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 19 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,17 @@ Does not send any data anywhere, as far as I know (except to pinboard, of course
* To [marcinsmialek](https://github.com/marcinsmialek) for fixing a bug with the search in the omnibar and adding a keyboard shortcut to open the popup (#48)

### Changelog (incomplete)
#### v1.4.0
- Added prettier dark mode and option to use browser/system setting for the dark mode, see [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme).
- (#51) Adds keybind Alt+Shift+P to open the create bookmark dialog within the popup ([Firefox only](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/browserAction/openPopup))
- (#51) Adds keybind Alt+Shift+Left to open the sidebar (Firefox only)
- (#50) Fixes search prefixing and makes search the same across popup, sidebar and browser address bar
- (#49) Improves tag suggestions when adding a new bookmark

#### v1.3.7
* Merged pull request #48, thanks @marcinsmialek! This adds the keyboard shortcut Alt-P (by default) to open the pinboard popup. Also, it fixes the behaviour when searching for bookmarks in the omnibar when no results are found.
* On development side: updated dependencies. Did not replace tslint by eslint yet because it seems to be quite the task...

#### v1.3.1 - 1.3.6:
* Fixed some bugs
* Started adding keyboard controls to the popup
Expand Down
17 changes: 15 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description" : "Browser extension for pinboard (including omnibar search, sidebar and actionbar button popup)",
"homepage_url": "https://github.com/seeba8/yet-another-pinboard-extension",
"author": "seeba8",
"version": "1.3.7",
"version": "1.4.0",
"background": {
"scripts": [
"vendor/webextension-polyfill/browser-polyfill.min.js",
Expand All @@ -25,7 +25,20 @@
"_execute_browser_action": {
"suggested_key": {
"default": "Alt+P"
}
},
"description": "Opens the pinboard popup"
},
"execute_sidebar_action": {
"suggested_key": {
"default": "Alt+Shift+S"
},
"description": "Opens the pinboard sidebar (Firefox only)"
},
"create_bookmark": {
"suggested_key": {
"default": "Alt+Shift+P"
},
"description": "Opens the pinboard popup in the bookmark creation dialog (Firefox only)"
}
},
"icons": {
Expand Down
16 changes: 16 additions & 0 deletions src/ts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
declare let chrome: any;
let pins: Pins;
let options: Options;
let waitForOpenedPopup = false;

// (browser as any).contextMenus is necessary since Firefox renamed their api to browser.menus...
browser.runtime.onInstalled.addListener(handleAddonInstalled);
browser.runtime.onStartup.addListener(handleStartup);
Expand All @@ -28,6 +30,7 @@ async function onWakeUp() {
browser.storage.onChanged.addListener(handleStorageChanged);
browser.tabs.onUpdated.addListener(handleTabUpdated);
browser.bookmarks.onCreated.addListener(handleBookmarkCreated);
browser.commands.onCommand.addListener(handleCommand);
(browser as any).contextMenus.onClicked.addListener(handleContextMenuClick);
pins = await Pins.updateList();
}
Expand All @@ -51,6 +54,16 @@ async function handleStartup() {

}

async function handleCommand(command: string) {
if(command === "create_bookmark") {
await browser.browserAction.openPopup();
waitForOpenedPopup = true;
} else if (command === "execute_sidebar_action") {
// toggle gives an error with the current type definition, but it works.
(browser.sidebarAction as any).toggle();
}
}

async function onCheckUpdate(alarm: browser.alarms.Alarm) {
if (alarm.name === "checkUpdate") {
pins = await Pins.updateList();
Expand Down Expand Up @@ -159,5 +172,8 @@ async function handleMessage(request: any, sender: browser.runtime.MessageSender
SharedFunctions.showErrorBadge(request.error);
} else if (request.callFunction === "hideErrorBadge") {
SharedFunctions.hideErrorBadge();
} else if (request.callFunction === "popupOpened" && waitForOpenedPopup) {
browser.runtime.sendMessage({"callFunction": "createBookmark"});
waitForOpenedPopup = false;
}
}
6 changes: 3 additions & 3 deletions src/ts/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"use strict";
namespace Connector {
type QueueElement = {
params: any,
params: any,
reject: (message: any) => void,
resolve: (message: any) => void,
resolve: (message: any) => void,
type: string,
}

Expand All @@ -14,7 +14,7 @@ namespace Connector {
getAllPins: "https://api.pinboard.in/v1/posts/all",
// getAuthToken: "https://api.pinboard.in/v1/user/auth_token",
getLastUpdate: "https://api.pinboard.in/v1/posts/update",
//suggestTags: "https://api.pinboard.in/v1/posts/suggest",
// suggestTags: "https://api.pinboard.in/v1/posts/suggest",
});

const MIN_INTERVAL = 3 * 1000;
Expand Down
4 changes: 2 additions & 2 deletions src/ts/options-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async function onLoad() {
if (options.styleType === StyleType.dark) {
(document.getElementById("dark") as HTMLInputElement).checked = true;
} else if (options.styleType === StyleType.light) {
(document.getElementById("light") as HTMLInputElement).checked = true;
(document.getElementById("light") as HTMLInputElement).checked = true;
} else if (options.styleType === StyleType.browser) {
(document.getElementById("browser") as HTMLInputElement).checked = true;
} else {
Expand All @@ -83,7 +83,7 @@ async function onLoad() {
(document.getElementById(k) as HTMLInputElement).value = v;
}

const tabs = await browser.tabs.query({});//.then((tabs: browser.tabs.Tab[]) => {
const tabs = await browser.tabs.query({});// .then((tabs: browser.tabs.Tab[]) => {
for (const tab of tabs) {
const li = document.createElement("li") as HTMLLIElement;
li.textContent = tab.title;
Expand Down
4 changes: 2 additions & 2 deletions src/ts/pins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Pins extends Map<string, Pin> {

let c = -1;
for (const pin of this.forEachReversed()) {
if (options.toRead && pin.toread !== "yes") continue;
if (options.toRead && pin.toread !== "yes") { continue; }
if (text === "" || searchFields.some(field => { return pin[field].toLowerCase().includes(text) })) {
c++;
if (options.offset > c) {
Expand All @@ -157,7 +157,7 @@ class Pins extends Map<string, Pin> {
}

public *filterWithOptions(text: string, options: Options, additional:{count?: number, toRead?: boolean, offset?: number} = {}) {
if(text === undefined) text = "";
if(text === undefined) { text = ""; }
text = text.toLowerCase();
let searchArea = [];
let hasPrefix = false;
Expand Down
32 changes: 22 additions & 10 deletions src/ts/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,27 @@ Array.from(prevNext.div.children).forEach((element) => {
element.addEventListener("click", handlePrevNextClick);
});
document.body.addEventListener("keydown", onKeyDown);
browser.runtime.onMessage.addListener(onMessage);


handleStartup();

async function handleStartup() {
await Promise.all([loadOptions(), reloadPins()/*, getDPI()*/]);
filterTextbox.focus();
collectTags();
browser.runtime.sendMessage({"callFunction": "popupOpened"});
}

function onMessage(data: any) {
if(data.callFunction === "createBookmark") {
handleBookmarkCurrent(undefined);
}
}

function collectTags() {
const tagsMap = new Map<string, number>();
//console.time("collectTags");
// console.time("collectTags");
for(const pin of pins.forEachReversed()) {
if(pin.tags !== "") {
for(const tag of pin.tags.split(" ")) {
Expand All @@ -91,16 +100,16 @@ function collectTags() {
}
}
}
//console.timeEnd("collectTags");
//console.time("sortTags");
// console.timeEnd("collectTags");
// console.time("sortTags");
// sort map accpording to https://stackoverflow.com/a/48324540
tagsMap[Symbol.iterator] = function* () {
yield* [...this.entries()].sort((a, b) => b[1] - a[1]);
}
for(const [key, val] of tagsMap) {
tags.push(key);
}
//console.timeEnd("sortTags");
// console.timeEnd("sortTags");
}

async function loadOptions() {
Expand Down Expand Up @@ -153,14 +162,17 @@ async function handleReadLaterCurrent(e: MouseEvent) {
}

async function handleBookmarkCurrent(e: MouseEvent) {
e.preventDefault();
if(e !== undefined) {
e.preventDefault();
}
document.getElementById("editwrapper").classList.toggle("hidden");
document.getElementById("greyout").classList.toggle("hidden");
const tab = (await browser.tabs.query({ currentWindow: true, active: true }))[0];
editBox.description.value = SharedFunctions.executeTitleRegex(tab.title, options.titleRegex);
editBox.URL.value = tab.url;
editBox.toReadCheckbox.checked = false;
editBox.tags.value = "";
editBox.description.focus();
}

function preparePrevNext(numberPins: number) {
Expand Down Expand Up @@ -469,9 +481,9 @@ function getCurrentToken() {
}

function onTagTextKeyDown(e: KeyboardEvent) {

const selectedSuggestion = document.getElementsByClassName("selected").item(0);
if(selectedSuggestion === null) return;
if(selectedSuggestion === null) { return; }
switch(e.code) {
case "ArrowDown":
e.stopPropagation();
Expand Down Expand Up @@ -501,20 +513,20 @@ function onTagTextKeyDown(e: KeyboardEvent) {
function onTagTextInput(e: InputEvent) {
clearSuggestionList();
const currentToken = getCurrentToken();
if (currentToken === "") return;
if (currentToken === "") { return; }
fillSuggestionList(currentToken);

}

function onSuggestionMouseover(e: MouseEvent) {
if((e.target as HTMLElement).id === "suggestions") return;
if((e.target as HTMLElement).id === "suggestions") { return; }
const selectedSuggestion = document.getElementsByClassName("selected").item(0);
selectedSuggestion.classList.toggle("selected");
(e.target as HTMLElement).classList.toggle("selected");
}

function onSuggestionClick(e: MouseEvent) {
if((e.target as HTMLElement).id === "suggestions") return;
if((e.target as HTMLElement).id === "suggestions") { return; }
acceptSuggestion((e.target as HTMLElement).textContent);
}
}

0 comments on commit 44c966a

Please sign in to comment.