Show the viewer button for anki notes which already exist

This commit is contained in:
toasted-nutbread 2019-10-09 20:31:09 -04:00
parent 97f5b7139f
commit 7ce54864f3
2 changed files with 39 additions and 8 deletions

View File

@ -97,15 +97,33 @@ async function apiDefinitionsAddable(definitions, modes, optionsContext) {
}
}
const results = await utilBackend().anki.canAddNotes(notes);
const cannotAdd = [];
const anki = utilBackend().anki;
const results = await anki.canAddNotes(notes);
for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) {
const state = {};
for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) {
state[modes[modeOffset]] = results[resultBase + modeOffset];
const index = resultBase + modeOffset;
const result = results[index];
const info = {canAdd: result};
state[modes[modeOffset]] = info;
if (!result) {
cannotAdd.push([notes[index], info]);
}
}
states.push(state);
}
if (cannotAdd.length > 0) {
const noteIdsArray = await anki.findNoteIds(cannotAdd.map(e => e[0]));
for (let i = 0, ii = Math.min(cannotAdd.length, noteIdsArray.length); i < ii; ++i) {
const noteIds = noteIdsArray[i];
if (noteIds.length > 0) {
cannotAdd[i][1].noteId = noteIds[0];
}
}
}
} catch (e) {
// NOP
}

View File

@ -286,15 +286,23 @@ class Display {
for (let i = 0; i < states.length; ++i) {
const state = states[i];
let noteId = null;
for (const mode in state) {
const button = this.adderButtonFind(i, mode);
if (button === null) {
continue;
}
button.classList.toggle('disabled', !state[mode]);
const info = state[mode];
if (!info.canAdd && noteId === null && info.noteId) {
noteId = info.noteId;
}
button.classList.toggle('disabled', !info.canAdd);
button.classList.remove('pending');
}
if (noteId !== null) {
this.viewerButtonShow(i, noteId);
}
}
} catch (e) {
this.onError(e);
@ -380,11 +388,7 @@ class Display {
if (adderButton !== null) {
adderButton.classList.add('disabled');
}
const viewerButton = this.viewerButtonFind(index);
if (viewerButton !== null) {
viewerButton.classList.remove('pending', 'disabled');
viewerButton.dataset.noteId = noteId;
}
this.viewerButtonShow(index, noteId);
} else {
throw new Error('Note could not be added');
}
@ -504,6 +508,15 @@ class Display {
return entry !== null ? entry.querySelector('.action-view-note') : null;
}
viewerButtonShow(index, noteId) {
const viewerButton = this.viewerButtonFind(index);
if (viewerButton === null) {
return;
}
viewerButton.classList.remove('pending', 'disabled');
viewerButton.dataset.noteId = noteId;
}
static delay(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}