From ba946eab9e47c05d0f7b725d68a0a4f65bab4452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A1=B9=E9=98=B3?= <39241051+insile@users.noreply.github.com> Date: Sat, 21 Mar 2026 19:45:06 +0800 Subject: [PATCH] 2026/3/21 --- src/service/register.ts | 31 ++++++++++++++++--------------- src/service/words-link.ts | 28 +++++++++++++++++++++++++--- src/service/words-manager.ts | 12 ++++++------ src/utils/process.ts | 6 ++++++ 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/src/service/register.ts b/src/service/register.ts index c9df969..695d483 100644 --- a/src/service/register.ts +++ b/src/service/register.ts @@ -1,8 +1,8 @@ import OpenWords from "../main"; -import { activateView, updateStatusBar } from "../utils/process"; +import { activateView, truncateSelection, updateStatusBar } from "../utils/process"; import { OPENWORDS_VIEW } from "../views/main-view"; -import { addDoubleBrackets, autoDoubleLinkWord } from "./words-link"; -import { Notice, TFile } from "obsidian"; +import { addDoubleBrackets, autoDoubleLinkWord, removeDoubleBrackets } from "./words-link"; +import { TFile } from "obsidian"; import { loadWordMetadata } from "./words-manager"; @@ -32,6 +32,14 @@ export function registerCommands(plugin: OpenWords) { await autoDoubleLinkWord(plugin); } }); + // 移除双链命令 + plugin.addCommand({ + id: 'removeDoubleBrackets', + name: '清除双链', + editorCallback: async () => { + await removeDoubleBrackets(plugin); + } + }); } // 注册文件监听器 @@ -87,24 +95,17 @@ export function unregisterFileWatchers(plugin: OpenWords) { export function registerEditorMenu(plugin: OpenWords) { plugin.registerEvent( plugin.app.workspace.on("editor-menu", (menu, editor, view) => { - const truncateSelection = (text: string, maxLength: number = 15): string => { - if (text.length <= maxLength) return text; - // 截断并添加省略号 - return text.substring(0, maxLength) + "..."; - }; + const selection = editor.getSelection().trim(); const hasAnyLink = /\[\[.*?\]\]/.test(selection); if (hasAnyLink) { menu.addItem((item) => { item - .setTitle("清除选中区域的所有双链") - .setIcon("link-off") - .onClick(() => { - // 将所有 [[A|B]] 替换为 B,将 [[A]] 替换为 A - const cleanedText = selection.replace(/\[\[(?:[^|\]]*\|)?([^\]]+)\]\]/g, '$1'); - editor.replaceSelection(cleanedText); - new Notice("已清除选中区域的所有双链"); + .setTitle("清除选中区域的所有链接") + .setIcon("link") + .onClick(async () => { + await removeDoubleBrackets(plugin); }); }); } diff --git a/src/service/words-link.ts b/src/service/words-link.ts index 8427648..9ec5bef 100644 --- a/src/service/words-link.ts +++ b/src/service/words-link.ts @@ -1,3 +1,4 @@ +import { truncateSelection } from "utils/process"; import OpenWords from "../main"; import { MarkdownView, Notice } from "obsidian"; @@ -68,9 +69,30 @@ export async function autoDoubleLinkWord(plugin: OpenWords) { } else { editor.replaceSelection(`[[${selection}]]`); } - new Notice(`已成功链接到: ${selection}`); + new Notice(`已成功链接到: ${truncateSelection(selection)}`); } else { // 4. 如果文件不存在,可以提示或提供创建选项 - new Notice(`单词库中未找到 "${selection}"`); + new Notice(`单词库中未找到 "${truncateSelection(selection)}"`); } -} \ No newline at end of file +} + +// 去除双链 +export async function removeDoubleBrackets(plugin: OpenWords) { + const activeView = plugin.app.workspace.getActiveViewOfType(MarkdownView); + if (!activeView) { + new Notice("请在 Markdown 编辑器中使用此命令"); + return; + } + const editor = activeView.editor; + const selection = editor.getSelection().trim(); + if (!selection) { + new Notice("请先选中一个单词"); + return; + } + + // 将所有 [[A|B]] 替换为 B,将 [[A]] 替换为 A + const cleanedText = selection.replace(/\[\[(?:[^|\]]*\|)?([^\]]+)\]\]/g, '$1'); + editor.replaceSelection(cleanedText); + new Notice("已清除选中区域的所有双链"); + +} diff --git a/src/service/words-manager.ts b/src/service/words-manager.ts index cb5c735..e977c74 100644 --- a/src/service/words-manager.ts +++ b/src/service/words-manager.ts @@ -119,28 +119,28 @@ export async function updateCard(plugin: OpenWords, card: CardInfo, grade: Super frontMatter["重复次数"] = result.repetition; }); - new Notice(`${card.front} \n易记因子: ${result.efactor.toFixed(2)} \n重复次数: ${result.repetition} \n间隔: ${result.interval} \n到期日: ${newDate}`); - // 等待元数据缓存更新(最多等待 1 秒) let attempts = 0; - while (attempts < 20) { + while (attempts < 10) { if (mode === 'new') { if (grade >= 3 && plugin.dueCards.has(card.front)) { break; // 以移出新单词池 - } else if (grade < 3) { + } else if (grade < 3 && (Math.abs(asNumber(plugin.newCards.get(card.front)?.efactor) - result.efactor) < 0.01)) { break; } } else { if (grade < 3 && plugin.newCards.has(card.front)) { break; // 以移出旧单词池 - } else if (grade >= 3 && plugin.dueCards.get(card.front)?.efactor === result.efactor) { + } else if (grade >= 3 && (Math.abs(asNumber(plugin.dueCards.get(card.front)?.efactor) - result.efactor) < 0.01)) { break; } } - await new Promise(resolve => setTimeout(resolve, 50)); + await new Promise(resolve => setTimeout(resolve, 100)); attempts++; } + + new Notice(`${card.front} \n易记因子: ${result.efactor.toFixed(2)} \n重复次数: ${result.repetition} \n间隔: ${result.interval} \n到期日: ${newDate}`); } // 重置单词属性 diff --git a/src/utils/process.ts b/src/utils/process.ts index 55df252..65c3484 100644 --- a/src/utils/process.ts +++ b/src/utils/process.ts @@ -39,3 +39,9 @@ export async function writeFile(app: App, filePath: string, content: string) { } } +// 截断选中文本 +export function truncateSelection(text: string, maxLength: number = 15) { + if (text.length <= maxLength) return text; + // 截断并添加省略号 + return text.substring(0, maxLength) + "..."; +}