From fdf8cbe0ff50147c3a891244ca0cb67fd9073a00 Mon Sep 17 00:00:00 2001 From: a Date: Fri, 7 Aug 2020 18:17:30 +0200 Subject: [PATCH] Add remove card call --- src/background/background.ts | 13 +++++++------ src/background/database.ts | 13 ++++++++++++- src/communication.ts | 22 +++++++++++++++++++++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/background/background.ts b/src/background/background.ts index 4eb67b0..8a8531e 100644 --- a/src/background/background.ts +++ b/src/background/background.ts @@ -1,12 +1,16 @@ import { browser, Runtime, Tabs } from "webextension-polyfill-ts"; import { GTranslateScraper } from "./gtranslate_scraper"; import { Flashcards } from "./database"; -import { Communicator, command, commandKinds } from "../communication"; +import { Communicator, commandKinds } from "../communication"; + +let com = new Communicator(); const scraper = new GTranslateScraper(); +com.translateCallback = (toTranslate) => scraper.translate(toTranslate); + const db = new Flashcards(); -let com = new Communicator(); -console.log(db); +com.addFlashcardCallback = (card) => db.addFlashcard(card); +com.removeFlashcardCallback = (card) => console.log(db); const getTab = async (s: Runtime.MessageSender): Promise => { if (s.tab) return s.tab; @@ -46,9 +50,6 @@ com.getEnabledCallback = async (s: Runtime.MessageSender): Promise => { return isEnabledSession(tab.id); }; -com.translateCallback = (toTranslate) => scraper.translate(toTranslate); -com.addFlashcardCallback = (card) => db.addTranslation(card); - browser.tabs.onUpdated.addListener(async (tabID, changeInfo) => { if (changeInfo.status == "complete") { if (await isEnabledSession(tabID)) { diff --git a/src/background/database.ts b/src/background/database.ts index 6318f6a..af4328f 100644 --- a/src/background/database.ts +++ b/src/background/database.ts @@ -18,7 +18,7 @@ export class Flashcards { }; } - addTranslation(t: Translation | Flashcard): Promise { + addFlashcard(t: Translation | Flashcard): Promise { let card: Flashcard; if ("dateAdded" in t && "exported" in t) card = t; else { @@ -42,6 +42,17 @@ export class Flashcards { }); } + removeFlashcard(card: Flashcard): Promise { + 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> { let req = this.db .transaction(["flashcards"], "readonly") diff --git a/src/communication.ts b/src/communication.ts index 9cf2cad..643993d 100644 --- a/src/communication.ts +++ b/src/communication.ts @@ -13,6 +13,10 @@ export class Communicator { value: Translation | Flashcard, sender: Runtime.MessageSender ) => Promise; + removeFlashcardCallback: ( + value: Flashcard, + sender: Runtime.MessageSender + ) => Promise; constructor() { browser.runtime.onMessage.addListener( @@ -26,6 +30,8 @@ export class Communicator { return this.translateCallback(c.toTranslate, s); case commandKinds.addFlashcard: 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) => { sendMessage({ commandKind: commandKinds.addFlashcard, card: card }); }; + static removeFlashcard = (card: Flashcard) => { + sendMessage({ commandKind: commandKinds.removeFlashcard, card: card }); + }; } const sendMessage = (m: command) => browser.runtime.sendMessage(m); @@ -52,6 +61,7 @@ export const enum commandKinds { getEnabled = "getEnabled", translate = "translate", addFlashcard = "addFlashcard", + removeFlashcard = "removeFlashcard", } interface setEnabled { commandKind: commandKinds.setEnabled; @@ -72,4 +82,14 @@ interface addFlashcard { 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;