Add remove card call

This commit is contained in:
a 2020-08-07 18:17:30 +02:00
parent dc9a38cc7d
commit fdf8cbe0ff
3 changed files with 40 additions and 8 deletions

View file

@ -1,12 +1,16 @@
import { browser, Runtime, Tabs } from "webextension-polyfill-ts"; import { browser, Runtime, Tabs } from "webextension-polyfill-ts";
import { GTranslateScraper } from "./gtranslate_scraper"; import { GTranslateScraper } from "./gtranslate_scraper";
import { Flashcards } from "./database"; import { Flashcards } from "./database";
import { Communicator, command, commandKinds } from "../communication"; import { Communicator, commandKinds } from "../communication";
let com = new Communicator();
const scraper = new GTranslateScraper(); const scraper = new GTranslateScraper();
com.translateCallback = (toTranslate) => scraper.translate(toTranslate);
const db = new Flashcards(); const db = new Flashcards();
let com = new Communicator(); com.addFlashcardCallback = (card) => db.addFlashcard(card);
console.log(db); com.removeFlashcardCallback = (card) => console.log(db);
const getTab = async (s: Runtime.MessageSender): Promise<Tabs.Tab> => { const getTab = async (s: Runtime.MessageSender): Promise<Tabs.Tab> => {
if (s.tab) return s.tab; if (s.tab) return s.tab;
@ -46,9 +50,6 @@ com.getEnabledCallback = async (s: Runtime.MessageSender): Promise<boolean> => {
return isEnabledSession(tab.id); return isEnabledSession(tab.id);
}; };
com.translateCallback = (toTranslate) => scraper.translate(toTranslate);
com.addFlashcardCallback = (card) => db.addTranslation(card);
browser.tabs.onUpdated.addListener(async (tabID, changeInfo) => { browser.tabs.onUpdated.addListener(async (tabID, changeInfo) => {
if (changeInfo.status == "complete") { if (changeInfo.status == "complete") {
if (await isEnabledSession(tabID)) { if (await isEnabledSession(tabID)) {

View file

@ -18,7 +18,7 @@ export class Flashcards {
}; };
} }
addTranslation(t: Translation | Flashcard): Promise<Flashcard> { addFlashcard(t: Translation | Flashcard): Promise<Flashcard> {
let card: Flashcard; let card: Flashcard;
if ("dateAdded" in t && "exported" in t) card = t; if ("dateAdded" in t && "exported" in t) card = t;
else { else {
@ -42,6 +42,17 @@ export class Flashcards {
}); });
} }
removeFlashcard(card: Flashcard): Promise<void> {
let req = this.db
.transaction(["flashcards"], "readwrite")
.objectStore("flashcards")
.delete(card.id);
return new Promise((resolve, reject) => {
req.onsuccess = () => resolve();
req.onerror = () => reject(req.error);
});
}
async getAllCards(): Promise<Array<Flashcard>> { async getAllCards(): Promise<Array<Flashcard>> {
let req = this.db let req = this.db
.transaction(["flashcards"], "readonly") .transaction(["flashcards"], "readonly")

View file

@ -13,6 +13,10 @@ export class Communicator {
value: Translation | Flashcard, value: Translation | Flashcard,
sender: Runtime.MessageSender sender: Runtime.MessageSender
) => Promise<Flashcard>; ) => Promise<Flashcard>;
removeFlashcardCallback: (
value: Flashcard,
sender: Runtime.MessageSender
) => Promise<Flashcard>;
constructor() { constructor() {
browser.runtime.onMessage.addListener( browser.runtime.onMessage.addListener(
@ -26,6 +30,8 @@ export class Communicator {
return this.translateCallback(c.toTranslate, s); return this.translateCallback(c.toTranslate, s);
case commandKinds.addFlashcard: case commandKinds.addFlashcard:
return this.addFlashcardCallback(c.card, s); return this.addFlashcardCallback(c.card, s);
case commandKinds.removeFlashcard:
return this.removeFlashcardCallback(c.card, s);
} }
} }
); );
@ -43,6 +49,9 @@ export class Communicator {
static addFlashcard = (card: Flashcard | Translation) => { static addFlashcard = (card: Flashcard | Translation) => {
sendMessage({ commandKind: commandKinds.addFlashcard, card: card }); sendMessage({ commandKind: commandKinds.addFlashcard, card: card });
}; };
static removeFlashcard = (card: Flashcard) => {
sendMessage({ commandKind: commandKinds.removeFlashcard, card: card });
};
} }
const sendMessage = (m: command) => browser.runtime.sendMessage(m); const sendMessage = (m: command) => browser.runtime.sendMessage(m);
@ -52,6 +61,7 @@ export const enum commandKinds {
getEnabled = "getEnabled", getEnabled = "getEnabled",
translate = "translate", translate = "translate",
addFlashcard = "addFlashcard", addFlashcard = "addFlashcard",
removeFlashcard = "removeFlashcard",
} }
interface setEnabled { interface setEnabled {
commandKind: commandKinds.setEnabled; commandKind: commandKinds.setEnabled;
@ -72,4 +82,14 @@ interface addFlashcard {
card: Translation | Flashcard; card: Translation | Flashcard;
} }
export type command = setEnabled | getEnabled | translate | addFlashcard; interface removeFlashcard {
commandKind: commandKinds.removeFlashcard;
card: Flashcard;
}
export type command =
| setEnabled
| getEnabled
| translate
| addFlashcard
| removeFlashcard;