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 { 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<Tabs.Tab> => {
if (s.tab) return s.tab;
@ -46,9 +50,6 @@ com.getEnabledCallback = async (s: Runtime.MessageSender): Promise<boolean> => {
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)) {

View file

@ -18,7 +18,7 @@ export class Flashcards {
};
}
addTranslation(t: Translation | Flashcard): Promise<Flashcard> {
addFlashcard(t: Translation | Flashcard): Promise<Flashcard> {
let card: Flashcard;
if ("dateAdded" in t && "exported" in t) card = t;
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>> {
let req = this.db
.transaction(["flashcards"], "readonly")

View file

@ -13,6 +13,10 @@ export class Communicator {
value: Translation | Flashcard,
sender: Runtime.MessageSender
) => Promise<Flashcard>;
removeFlashcardCallback: (
value: Flashcard,
sender: Runtime.MessageSender
) => Promise<Flashcard>;
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;