2026/3/21

This commit is contained in:
项阳 2026-03-21 19:45:06 +08:00
parent 3e94753e55
commit ba946eab9e
4 changed files with 53 additions and 24 deletions

View file

@ -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);
});
});
}

View file

@ -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)}"`);
}
}
}
// 去除双链
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("已清除选中区域的所有双链");
}

View file

@ -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}`);
}
// 重置单词属性

View file

@ -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) + "...";
}