Skip to content

Commit

Permalink
feat(anki): add support for v3 scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
Coobaha committed Feb 2, 2022
1 parent bfe958e commit a3bb593
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
28 changes: 15 additions & 13 deletions alfred/anki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ const goTo = async (href: string) => {
}

const APP_BASE_URL = isTranslate
? 'https://translate.google.com/?text='
? 'https://translate.google.com/?sl=fi&tl=en&text='
: `${process.env.FINNISH_URL || 'https://cooba.me/suomea'}/`.replace(
/\/+$/,
'/',
);

const tabUrl = isTranslate ? `https://translate.google.com` : APP_BASE_URL;
return run<true>(
(browserName, browserId, href, APP_BASE_URL) => {
(browserName, browserId, href, APP_BASE_URL, tabUrl) => {
let browser: ReturnType<typeof Application> & GoogleChrome;
try {
browser = Application<GoogleChrome>(browserName);
Expand All @@ -81,7 +81,7 @@ const goTo = async (href: string) => {
.some((window: any, winIdx: number) => {
const tabs = window.tabs();
const tabIndex = tabs.findIndex((tab: GoogleChrome.Tab) =>
tab.url().includes(APP_BASE_URL),
tab.url().includes(tabUrl),
);

if (tabIndex > -1) {
Expand All @@ -103,6 +103,7 @@ const goTo = async (href: string) => {
browserId,
href,
APP_BASE_URL,
tabUrl,
);
};

Expand Down Expand Up @@ -189,12 +190,7 @@ const search = async (term: string) => {
.then(handleNewData);
}

await Promise.all([
wk(),
sk('ru'),
sk('fi'),
sk('en')
]);
await Promise.all([wk(), sk('ru'), sk('fi'), sk('en')]);

const titles = new Set<string>();
all.map((x) => titles.add(x.title));
Expand Down Expand Up @@ -225,9 +221,15 @@ const search = async (term: string) => {
if (results.length === 0) {
alfy.output([
{
title: term,
subtitle: `No results found - click to open in browser`,
arg: term,
title: `open in Google Translate`,
subtitle: term,
arg: `translate:${term}`,
autocomplete: term,
},
{
title: `open in Browser`,
subtitle: term,
arg: `${term}`,
autocomplete: term,
},
]);
Expand Down
2 changes: 1 addition & 1 deletion alfred/anki_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ async function findExistingNotesId(term: string): Promise<number[]> {
.fetch(ankiConnectURI, {
method: 'POST',
body: {
action: 'findNotes',
action: 'find_notes',
version: 6,
params: {
query: search,
Expand Down
2 changes: 1 addition & 1 deletion alfred/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@
<key>note</key>
<string>open anki tab</string>
<key>xpos</key>
<integer>595</integer>
<integer>770</integer>
<key>ypos</key>
<integer>350</integer>
</dict>
Expand Down
17 changes: 14 additions & 3 deletions anki/plugin/suomea.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

from aqt import mw, gui_hooks
from aqt.addons import AddonsDialog
from aqt.browser.previewer import Previewer
from aqt.clayout import CardLayout
from aqt.hooks_gen import addons_dialog_will_show
from aqt.previewer import Previewer
from anki.cards import Card
from aqt.reviewer import Reviewer
from aqt.webview import WebContent

from .settings import settings, SettingsModal, anki_connect_setup


def prepare(html, card, context: str):
if settings.enabled is False:
return html
Expand All @@ -30,7 +30,18 @@ def prepare(html, card, context: str):
except Exception:
pass

next_card = sched.getCard()
next_card = None

if sched.version == 3:
cards = sched.get_queued_cards(fetch_limit=2)
queued_card = cards.cards[1]

if queued_card is not None:
next_card = Card(sched.col)
next_card._load_from_backend_card(queued_card.card)
next_card.load()
else:
next_card = sched.getCard()

if next_card is not None and settings.question_field is not None:
try:
Expand Down
2 changes: 1 addition & 1 deletion server/src/routes/suomea/suomea_schema.gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -428,5 +428,5 @@
}
}
},
"$hash": "41ad553a726cfb888e68f1e3b677a0d25a60c978498808059ca6b49bf25d0fd4__v0.3.1"
"$hash": "4f5a54af8ada80636e3f014a65bc34790c8745c78bde16f4657429af7e656de5__v0.3.1"
}
2 changes: 1 addition & 1 deletion web/src/anki_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export async function findExistingNotesId(
.signal(controller)
.options({ forceCache: true, cacheKey: `findNotes_${search}` })
.post({
action: 'findNotes',
action: 'find_notes',
version: 6,
params: {
query: search,
Expand Down

0 comments on commit a3bb593

Please sign in to comment.