Skip to content

Commit

Permalink
✨ feat: 提供设置过去日记的属性的按钮
Browse files Browse the repository at this point in the history
  • Loading branch information
frostime committed Dec 3, 2023
1 parent 9605139 commit 78d3d47
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 30 deletions.
41 changes: 41 additions & 0 deletions src/components/set-past-dn-attr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Dialog } from "siyuan";
import {searchAndSearchAllDNAttr} from '@/func';
import notebooks from "@/global-notebooks";

/**
* 帮用户设置过去的日记的属性
*/
export const setDNAttrDialog = async () => {
const dialog = new Dialog({
title: 'Running',
content: `
<div class="b3-dialog__content">
<div id="body" style="padding: 6px 12px;">
<ul id="notebooks">
</ul>
</div>
</div>`,
width: "20em",
height: "20em",
});
let div: HTMLDivElement = dialog.element.querySelector(".b3-dialog__container");
div.style.maxHeight = "50%";
let ul: HTMLUListElement = dialog.element.querySelector("#body > ul#notebooks");
for (let notebook of notebooks) {
if (notebook.dailynoteSprig === undefined || notebook.dailynoteSprig === '') {
continue;
}
let li = document.createElement('li');
li.innerHTML = `<b>${notebook.name}...</b>`;
ul.appendChild(li);
let ans = await searchAndSearchAllDNAttr(notebook);
li.innerHTML = `<b>${notebook.name}</b>: <b>${ans?.length ?? 0}</b> daily notes.`;
}
let body = dialog.element.querySelector("#body");
let hint = document.createElement('div');
hint.innerText = 'All Done!';
hint.style.color = 'var(--b3-theme-primary)';
hint.style.fontWeight = 'bold';
body.appendChild(hint);
};

38 changes: 24 additions & 14 deletions src/components/setting-gui.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import SettingPanels from "./settings/setting-panels.svelte";
import Blacklist from "./blacklist.svelte";
import { setDNAttrDialog } from "./set-past-dn-attr";
let contents = i18n.Setting;
let groups = {
Expand Down Expand Up @@ -50,6 +52,10 @@
{
name: "DisableAutoCreateOnMobile",
type: "checkbox"
},
{
name: "SetPastDailyNoteAttr",
type: "button"
}
//1.3.0 版本再加上
// {
Expand Down Expand Up @@ -105,22 +111,26 @@
function onClick({ detail }) {
console.log(detail);
let dialog = new Dialog({
title: i18n.Blacklist.name,
content: `<div id="blacklist" style="height: 100%;"></div>`,
height: "25rem",
width: "30rem",
});
new Blacklist({
target: dialog.element.querySelector("#blacklist"),
props: {
close: () => {
dialog.destroy();
let key = detail.key;
if (key === 'NotebookBlacklist') {
let dialog = new Dialog({
title: i18n.Blacklist.name,
content: `<div id="blacklist" style="height: 100%;"></div>`,
height: "25rem",
width: "30rem",
});
new Blacklist({
target: dialog.element.querySelector("#blacklist"),
props: {
close: () => {
dialog.destroy();
},
},
},
});
});
} else if (key === 'SetPastDailyNoteAttr') {
setDNAttrDialog();
}
}
</script>

<!-- <SettingPanel dataname="global" {settingItems} />
Expand Down
11 changes: 2 additions & 9 deletions src/components/toolbar-menu.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IMenuItemOption, Menu, Plugin, showMessage, IEventBusMap } from "siyuan";
import { currentDiaryStatus, openDiary, updateTodayReservation, searchPastDailyNotes } from "../func";
import { IMenuItemOption, Menu, Plugin, showMessage } from "siyuan";
import { currentDiaryStatus, openDiary, updateTodayReservation } from "../func";
import notebooks from "../global-notebooks";
import { reservation, settings } from "../global-status";
import { i18n, isMobile, debug } from "../utils";
Expand Down Expand Up @@ -103,13 +103,6 @@ export class ToolbarMenuItem {
icon: 'iconRefresh',
click: () => {eventBus.publish(eventBus.EventUpdateAll, '');}
});
menu.addItem({
label: '设置所有过去日期',
icon: 'iconEmoji',
click: async () => {
let dailynotes = await searchPastDailyNotes(notebooks.notebooks[0], new Date('2023-03-01'));
}
});

let rect = this.ele.getBoundingClientRect();
const iconIsRight = settings.get('IconPosition') === 'right';
Expand Down
21 changes: 14 additions & 7 deletions src/func/dailynote/past-dn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ export async function getPastDNHPath(notebook: NotebookId | Notebook, date: Date
notebook = notebooks.find(notebook);
}
if (notebook === null) {
throw new Error('DailyNoteToday: 请先设置日记本');
// throw new Error('DailyNoteToday: 请先设置日记本');
return null;
}

let dnSprig = notebook?.dailynoteSprig;
if (dnSprig === undefined) {
throw new Error('DailyNoteToday: 请先设置日记本');
// throw new Error('DailyNoteToday: 请先设置日记本');
return null;
}

let dateStr = formatDate(date, '-');
Expand Down Expand Up @@ -47,6 +49,9 @@ interface IDailyNote {
*/
export async function searchDailyNote(notebook: Notebook, date: Date): Promise<IDailyNote> {
let hpath = await getPastDNHPath(notebook, date);
if (hpath === null) {
return null;
}
let docs: Block[] = await getDocsByHpath(hpath, notebook);
let ans = {
date: date,
Expand All @@ -63,7 +68,8 @@ export async function searchDailyNotesBetween(notebook: Notebook, begin: Date, e
let dnSprig = notebook?.dailynoteSprig;

if (dnSprig === undefined) {
throw new Error('DailyNoteToday: 请先设置日记本');
// throw new Error('DailyNoteToday: 请先设置日记本');
return [];
}

end = end === undefined ? new Date() : end;
Expand All @@ -78,7 +84,7 @@ export async function searchDailyNotesBetween(notebook: Notebook, begin: Date, e
}
let allDnDocs = allDates.map(async (date: Date) => {
let ans = await searchDailyNote(notebook, date);
if (callback !== undefined) {
if (callback !== undefined && callback !== null) {
callback(ans);
}
return ans;
Expand All @@ -98,8 +104,8 @@ export async function searchDailyNotesBetween(notebook: Notebook, begin: Date, e
export async function searchAndSearchAllDNAttr(notebook: Notebook) {

let dailynoteSprig = notebook?.dailynoteSprig;
if (dailynoteSprig === undefined) {
throw new Error('DailyNoteToday: 请先设置日记本');
if (dailynoteSprig === undefined || dailynoteSprig === null || dailynoteSprig === '') {
return;
}
if (dailynoteSprig.startsWith('/')) {
dailynoteSprig = dailynoteSprig.substring(1);
Expand All @@ -122,7 +128,8 @@ export async function searchAndSearchAllDNAttr(notebook: Notebook) {
order by created asc limit 1;`;
let docs = await api.sql(sql);
if (docs.length === 0) {
throw `DailyNoteToday: 未找到日记本 ${notebook.name} 的第一篇日记`;
console.warn(`未找到日记本 ${notebook.name} 的日记`);
return;
}
let firstDoc = docs[0];
let created: string = firstDoc.created;
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
"link": "Link",
"ref": "Ref"
}
},
"SetPastDailyNoteAttr": {
"title": "Addl document attribute for past Daily Notes",
"text": "✨ Due to the limitations of SiYuan's implementation, it used to be difficult to find specific daily note at any time, adding this doc attribute is a prerequisite for the subsequent optimization of daily note's functionality.<br/>If you don't understand what this button is for, it is recommended that you press it, and it won't bring you any extra trouble!<br/> Note: Clicking this button once is enough."
}
},
"SettingGroups": {
Expand Down
5 changes: 5 additions & 0 deletions src/i18n/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
"link": "链接块",
"ref": "引用块"
}
},
"SetPastDailyNoteAttr": {
"title": "为过去的 Daily Note 补充文档属性 (实验性)",
"text": "✨ 受限于思源的实现, 过去难以查找任意时间的日记, 添加这个属性是后续优化 daily note 的功能的前提。<br/>你不必理解具体的细节, 点一下这个按钮, 不会给你带来任何额外的麻烦。<br/>注: 这个按钮点一次就够了",
"button": "开始"
}
},
"SettingGroups": {
Expand Down

0 comments on commit 78d3d47

Please sign in to comment.