feat: support bases

This commit is contained in:
liufree 2025-10-14 01:11:49 +08:00
parent cd48a9a5b1
commit 79baca43cf
2 changed files with 49 additions and 36 deletions

View file

@ -1,7 +1,7 @@
import {BasesView, MarkdownRenderer, QueryController, TFile} from 'obsidian';
import React, {useEffect, useRef, useState} from 'react';
import {createRoot} from 'react-dom/client';
import {Button, Space, Card,Modal} from 'antd';
import {Button, Space, Card, Modal, notification} from 'antd';
import dayjs from 'dayjs';
export const MemoryCardViewType = 'dash-memory-card-view';
@ -56,9 +56,9 @@ const updateSRFrontmatter = async (
rating: 1 | 3 | 5,
refresh: () => void
) => {
if (!filePath) return;
if (!filePath) return false;
const file = app.vault.getAbstractFileByPath(filePath);
if (!(file instanceof TFile)) return;
if (!(file instanceof TFile)) return false;
let repetitions = Number(frontmatter?.['sr-repetitions']) || 0;
let interval = Number(frontmatter?.['sr-interval']) || 1;
let ease = Number(frontmatter?.['sr-ease']) || 2.5;
@ -84,8 +84,7 @@ const updateSRFrontmatter = async (
const due = dayjs().add(interval, 'day');
const now = dayjs();
if (due.diff(now, 'day') >= 365) {
Modal.success('恭喜已经掌握该卡片');
return;
return true;
}
await app.fileManager.processFrontMatter(file, (fm: Record<string, any>) => {
fm['sr-repetitions'] = repetitions;
@ -95,6 +94,7 @@ const updateSRFrontmatter = async (
fm['sr-lapses'] = lapses;
});
refresh();
return false;
};
const MemoryCard: React.FC<MemoryCardProps> = ({markdown, app, filePath, title, frontmatter}) => {
@ -102,6 +102,7 @@ const MemoryCard: React.FC<MemoryCardProps> = ({markdown, app, filePath, title,
const [rendered, setRendered] = useState(false);
const [fm, setFM] = useState(frontmatter);
const [md, setMD] = useState(markdown);
const [showModal, setShowModal] = useState(false);
useEffect(() => {
setFM(frontmatter);
@ -137,7 +138,8 @@ const MemoryCard: React.FC<MemoryCardProps> = ({markdown, app, filePath, title,
if (action === 'hard') rating = 1;
else if (action === 'medium') rating = 3;
else if (action === 'easy') rating = 5;
await updateSRFrontmatter(app, filePath, fm, rating, refresh);
const isDueOverYear = await updateSRFrontmatter(app, filePath, fm, rating, refresh);
if (isDueOverYear) setShowModal(true);
};
return (
@ -173,11 +175,22 @@ const MemoryCard: React.FC<MemoryCardProps> = ({markdown, app, filePath, title,
justifyContent: 'center',
}}>
<Space>
<Button type="primary" danger onClick={() => handleDifficulty('hard')}></Button>
<Button type="default" onClick={() => handleDifficulty('medium')}></Button>
<Button type="primary" onClick={() => handleDifficulty('easy')}></Button>
<Button type="primary" danger onClick={() => handleDifficulty('hard')}>hard</Button>
<Button type="default" onClick={() => handleDifficulty('medium')}>medium</Button>
<Button type="primary" onClick={() => handleDifficulty('easy')}>easy</Button>
</Space>
</div>
<Modal
open={showModal}
onCancel={() => setShowModal(false)}
footer={null}
centered
>
<div style={{textAlign: 'center', fontSize: 18, padding: '24px 0'}}>
Congratulations! You have memorized this note.
</div>
</Modal>
</>
);
};

View file

@ -118,34 +118,34 @@ const KanbanView: React.FC<ViewProps> = ({app, source}) => {
useEffect(() => {
const onModify = (file: TFile) => {
setTimeout(() => {
setRefresh((prev) => !prev); // 触发刷新
}, 300); // 延迟 500 毫秒,可根据实际情况调整
console.log("文件已修改:", file.path);
};
// const onModify = (file: TFile) => {
// setTimeout(() => {
// setRefresh((prev) => !prev); // 触发刷新
// }, 300); // 延迟 500 毫秒,可根据实际情况调整
// console.log("文件已修改:", file.path);
// };
//
// const onCreate = (file: TFile) => {
// console.log("文件已创建:", file.path);
// setRefresh(!refresh); // 触发刷新
// };
//
// const onDelete = (file: TFile) => {
// console.log("文件已删除:", file.path);
// setRefresh(!refresh); // 触发刷新
// };
const onCreate = (file: TFile) => {
console.log("文件已创建:", file.path);
setRefresh(!refresh); // 触发刷新
};
const onDelete = (file: TFile) => {
console.log("文件已删除:", file.path);
setRefresh(!refresh); // 触发刷新
};
// 监听文件事件
app.vault.on("modify", onModify);
app.vault.on("create", onCreate);
app.vault.on("delete", onDelete);
// 清理事件监听器
return () => {
app.vault.off("modify", onModify);
app.vault.off("create", onCreate);
app.vault.off("delete", onDelete);
};
// // 监听文件事件
// app.vault.on("modify", onModify);
// app.vault.on("create", onCreate);
// app.vault.on("delete", onDelete);
//
// // 清理事件监听器
// return () => {
// app.vault.off("modify", onModify);
// app.vault.off("create", onCreate);
// app.vault.off("delete", onDelete);
// };
}, [app]);
const cardDetails = (dataList: any) => {