Update the note context object generation process to ensure consistency (#1792)

This commit is contained in:
toasted-nutbread 2021-07-03 20:09:22 -04:00 committed by GitHub
parent ea47cb8248
commit 468c923277
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 11 deletions

View File

@ -33,6 +33,7 @@ class DisplayAnki {
this._updateAdderButtonsPromise = Promise.resolve(); this._updateAdderButtonsPromise = Promise.resolve();
this._updateAdderButtonsToken = null; this._updateAdderButtonsToken = null;
this._eventListeners = new EventListenerCollection(); this._eventListeners = new EventListenerCollection();
this._noteContext = null;
this._checkForDuplicates = false; this._checkForDuplicates = false;
this._suspendNewCards = false; this._suspendNewCards = false;
this._compactTags = false; this._compactTags = false;
@ -50,6 +51,7 @@ class DisplayAnki {
} }
prepare() { prepare() {
this._noteContext = this._getNoteContext();
this._display.hotkeyHandler.registerActions([ this._display.hotkeyHandler.registerActions([
['addNoteKanji', () => { this._tryAddAnkiNoteForSelectedEntry('kanji'); }], ['addNoteKanji', () => { this._tryAddAnkiNoteForSelectedEntry('kanji'); }],
['addNoteTermKanji', () => { this._tryAddAnkiNoteForSelectedEntry('term-kanji'); }], ['addNoteTermKanji', () => { this._tryAddAnkiNoteForSelectedEntry('term-kanji'); }],
@ -64,6 +66,10 @@ class DisplayAnki {
this._hideAnkiNoteErrors(false); this._hideAnkiNoteErrors(false);
} }
setupEntriesBegin() {
this._noteContext = this._getNoteContext();
}
setupEntry(entry) { setupEntry(entry) {
this._addMultipleEventListeners(entry, '.action-view-tags', 'click', this._onShowTagsBind); this._addMultipleEventListeners(entry, '.action-view-tags', 'click', this._onShowTagsBind);
this._addMultipleEventListeners(entry, '.action-add-note', 'click', this._onNoteAddBind); this._addMultipleEventListeners(entry, '.action-add-note', 'click', this._onNoteAddBind);
@ -81,11 +87,10 @@ class DisplayAnki {
let ankiNoteData; let ankiNoteData;
let ankiNoteDataException; let ankiNoteDataException;
try { try {
const context = this._getNoteContext();
ankiNoteData = await this._ankiNoteBuilder.getRenderingData({ ankiNoteData = await this._ankiNoteBuilder.getRenderingData({
dictionaryEntry, dictionaryEntry,
mode: 'test', mode: 'test',
context, context: this._noteContext,
resultOutputMode: this.resultOutputMode, resultOutputMode: this.resultOutputMode,
glossaryLayoutMode: this._glossaryLayoutMode, glossaryLayoutMode: this._glossaryLayoutMode,
compactTags: this._compactTags, compactTags: this._compactTags,
@ -107,8 +112,7 @@ class DisplayAnki {
let note; let note;
let errors; let errors;
try { try {
const noteContext = this._getNoteContext(); ({note: note, errors} = await this._createNote(dictionaryEntry, mode, false));
({note: note, errors} = await this._createNote(dictionaryEntry, mode, noteContext, false));
} catch (e) { } catch (e) {
errors = [e]; errors = [e];
} }
@ -263,11 +267,9 @@ class DisplayAnki {
const modes = this._getModes(isTerms); const modes = this._getModes(isTerms);
let states; let states;
try { try {
const noteContext = this._getNoteContext();
states = await this._areDictionaryEntriesAddable( states = await this._areDictionaryEntriesAddable(
dictionaryEntries, dictionaryEntries,
modes, modes,
noteContext,
this._checkForDuplicates ? null : true, this._checkForDuplicates ? null : true,
this._displayTags !== 'never' this._displayTags !== 'never'
); );
@ -376,8 +378,7 @@ class DisplayAnki {
const progressIndicatorVisible = this._display.progressIndicatorVisible; const progressIndicatorVisible = this._display.progressIndicatorVisible;
const overrideToken = progressIndicatorVisible.setOverride(true); const overrideToken = progressIndicatorVisible.setOverride(true);
try { try {
const noteContext = this._getNoteContext(); const {note, errors} = await this._createNote(dictionaryEntry, mode, true);
const {note, errors} = await this._createNote(dictionaryEntry, mode, noteContext, true);
allErrors.push(...errors); allErrors.push(...errors);
let noteId = null; let noteId = null;
@ -462,12 +463,12 @@ class DisplayAnki {
return templates; return templates;
} }
async _areDictionaryEntriesAddable(dictionaryEntries, modes, context, forceCanAddValue, fetchAdditionalInfo) { async _areDictionaryEntriesAddable(dictionaryEntries, modes, forceCanAddValue, fetchAdditionalInfo) {
const modeCount = modes.length; const modeCount = modes.length;
const notePromises = []; const notePromises = [];
for (const dictionaryEntry of dictionaryEntries) { for (const dictionaryEntry of dictionaryEntries) {
for (const mode of modes) { for (const mode of modes) {
const notePromise = this._createNote(dictionaryEntry, mode, context, false); const notePromise = this._createNote(dictionaryEntry, mode, false);
notePromises.push(notePromise); notePromises.push(notePromise);
} }
} }
@ -499,7 +500,8 @@ class DisplayAnki {
return results; return results;
} }
async _createNote(dictionaryEntry, mode, context, injectMedia) { async _createNote(dictionaryEntry, mode, injectMedia) {
const context = this._noteContext;
const modeOptions = this._modeOptions.get(mode); const modeOptions = this._modeOptions.get(mode);
if (typeof modeOptions === 'undefined') { throw new Error(`Unsupported note type: ${mode}`); } if (typeof modeOptions === 'undefined') { throw new Error(`Unsupported note type: ${mode}`); }
const template = this._ankiFieldTemplates; const template = this._ankiFieldTemplates;

View File

@ -946,6 +946,8 @@ class Display extends EventDispatcher {
const container = this._container; const container = this._container;
container.textContent = ''; container.textContent = '';
this._displayAnki.setupEntriesBegin();
for (let i = 0, ii = dictionaryEntries.length; i < ii; ++i) { for (let i = 0, ii = dictionaryEntries.length; i < ii; ++i) {
if (i > 0) { if (i > 0) {
await promiseTimeout(1); await promiseTimeout(1);