diff --git a/src/main.ts b/src/main.ts index 0a34aa9..a7c9ae8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -80,13 +80,17 @@ export default class OpenWords extends Plugin { // 激活视图 async activateView() { const leaves = this.app.workspace.getLeavesOfType(MAIN_VIEW); + if (leaves.length === 0) { await this.app.workspace.getLeaf(true).setViewState({ type: MAIN_VIEW, active: true, }); } else { - await this.app.workspace.revealLeaf(leaves[0]); + const leaf = leaves[0]; + if (leaf) { + await this.app.workspace.revealLeaf(leaf); + } } } @@ -286,7 +290,7 @@ export default class OpenWords extends Plugin { const innerWord = doubleBracketMatch[1]; // 获取双链中的名称 const alias = doubleBracketMatch[3]; // 获取双链中的别名 // 如果双链中的名称不在单词表中,去除双链,保留别名或名称 - if (!unmasteredWords.has(innerWord)) { + if (innerWord && !unmasteredWords.has(innerWord)) { return alias; // 如果有别名,保留别名;否则保留名称 } // 如果双链中的名称在单词表中,保持原样 diff --git a/src/views/LearningPage.ts b/src/views/LearningPage.ts index dfdecd6..ab6f934 100644 --- a/src/views/LearningPage.ts +++ b/src/views/LearningPage.ts @@ -69,11 +69,11 @@ export function pickNextLCard(this: MainView) { } const randomMode = Math.random() < this.plugin.settings.randomRatio; if (randomMode) { - return pool[Math.floor(Math.random() * pool.length)]; + return pool[Math.floor(Math.random() * pool.length)] ?? null; } else { const topN = Math.max(1, Math.ceil(pool.length / 100)); const topPool = pool.slice(0, topN); - return topPool[Math.floor(Math.random() * topPool.length)]; + return topPool[Math.floor(Math.random() * topPool.length)] ?? null; } } @@ -105,7 +105,7 @@ export function renderCard(this: MainView, cardContainer: HTMLDivElement) { } // 评分按钮渲染 -export function renderSettings(cardContainer: HTMLDivElement, settingsContainer: HTMLElement) { +export function renderSettings(this: MainView, cardContainer: HTMLDivElement, settingsContainer: HTMLElement) { const grades: { grade: SuperMemoGrade, label: string }[] = [ { grade: 0, label: '评分 1: 回答错误, 完全不会' }, { grade: 1, label: '评分 2: 回答错误, 看到正确答案后感觉很熟悉' }, diff --git a/src/views/MainView.ts b/src/views/MainView.ts index 1fc8aa0..4050e11 100644 --- a/src/views/MainView.ts +++ b/src/views/MainView.ts @@ -51,7 +51,7 @@ export class MainView extends ItemView { async onOpen() { this.component.load(); - const container = this.containerEl.children[1]; + const container = this.containerEl.querySelector('.view-content') as HTMLElement; this.viewContainer = container.createDiv({ cls: 'openwords-view-container' }); this.statusBarEl = container.createDiv({ cls: 'openwords-view-statusbar' }); await this.render(); diff --git a/src/views/SpellingPage.ts b/src/views/SpellingPage.ts index 3e96309..fdfb42b 100644 --- a/src/views/SpellingPage.ts +++ b/src/views/SpellingPage.ts @@ -59,7 +59,7 @@ export function pickNextSCard(this: MainView) { const sorted = cards.slice().sort((a, b) => a.efactor - b.efactor); const half = Math.ceil(sorted.length / 2); const pool = sorted.slice(0, half); - return pool[Math.floor(Math.random() * pool.length)]; + return pool[Math.floor(Math.random() * pool.length)] ?? null; } export async function renderInput( @@ -106,19 +106,19 @@ export async function renderInput( inputField.addEventListener('input', () => { if (inputField.value.length === 1 && i < word.length - 1) { - inputFields[i + 1].focus(); + inputFields[i + 1]?.focus(); } this.checkSpelling(inputFields, word, feedbackContainer, wordMeaningContainer, inputContainer); }); inputField.addEventListener('keydown', (event) => { if (event.key === 'Backspace' && inputField.value === '' && i > 0) { - inputFields[i - 1].focus(); + inputFields[i - 1]?.focus(); } }); } - inputFields[0].focus(); + inputFields[0]?.focus(); } } @@ -176,7 +176,7 @@ export async function checkSpelling( feedbackContainer.setText('错误,请重试!'); this.errorCount += 1; inputFields.forEach((field) => (field.value = '')); - inputFields[0].focus(); + inputFields[0]?.focus(); } } }