Skip to content

Commit

Permalink
🔥 rm: 删掉所有关于 Reservation 对象的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
frostime committed Sep 26, 2024
1 parent c0e98ae commit 18f0ee7
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 184 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "siyuan-dailynote-today",
"version": "1.6.8",
"version": "1.6.9",
"description": "",
"main": ".src/index.js",
"keywords": [],
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"en_US": "Daily Note Today"
},
"url": "https://github.com/frostime/siyuan-dailynote-today",
"version": "1.6.8",
"version": "1.6.9",
"minAppVersion": "3.0.0",
"description": {
"zh_CN": "自动创建日记 + 快速打开日记 + 日程 TODO 管理 + 移动块功能",
Expand Down
9 changes: 8 additions & 1 deletion src/func/reserve/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Author : frostime
* @Date : 2024-09-09 22:18:54
* @FilePath : /src/func/reserve/index.ts
* @LastEditTime : 2024-09-13 11:55:40
* @LastEditTime : 2024-09-25 22:59:24
* @Description :
*/
import { confirm } from 'siyuan';
Expand All @@ -15,6 +15,13 @@ import { Retrieve, retrieveResvFromBlocks, RetvFactory } from './retrieve';
export * from './retrieve';
export * from './reserve';

export function reservationAttrVal(date: Date) {
//确保日期格式为 YYYYMMDD
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
return `${year}${month < 10 ? '0' + month : month}${day < 10 ? '0' + day : day}`;
}

/**
* 给定笔记本,将今日的预约块插入笔记本的 daily note 中
Expand Down
14 changes: 4 additions & 10 deletions src/func/reserve/reserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { i18n, lute } from "@/utils";
import { showMessage, confirm } from "siyuan";
import * as serverApi from "@/serverApi";
import { confirmDialog } from "@/components/libs/dialogs";
import { reservation, settings } from "@/global-status";
import { settings } from "@/global-status";
import { parse, ParsedResult } from 'chrono-node';
import { reservationAttrVal } from ".";


const Zh1to9 = '一二三四五六七八九';
Expand Down Expand Up @@ -207,22 +208,15 @@ export async function reserveBlock(blockId) {
* @param blockId 块ID
*/
export async function dereserveBlock(blockId: BlockId) {
let date = reservation.findReserved(blockId);
if (date) {
reservation.removeReservation(date, blockId);
}
reservation.save();
serverApi.setBlockAttrs(blockId, {
'custom-reservation': null, 'memo': null
});
showMessage(i18n.DeReserveMenu.Success, 3000, 'info');
}

function doReserveBlock(blockId, date: Date) {
function doReserveBlock(blockId: BlockId, date: Date) {
console.debug('Do reservation', blockId, date);
reservation.doReserve(date, blockId);
reservation.save();
let dateStr = reservation.dateTemplate(date);
let dateStr = reservationAttrVal(date);
serverApi.setBlockAttrs(blockId, {
'custom-reservation': dateStr, 'memo': `${i18n.ReserveMenu.name} ${dateStr}`
});
Expand Down
169 changes: 1 addition & 168 deletions src/global-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
* @Author : frostime
* @Date : 2024-05-21 14:14:08
* @FilePath : /src/global-status.ts
* @LastEditTime : 2024-09-13 11:59:51
* @LastEditTime : 2024-09-26 16:21:06
* @Description :
*/
import { eventBus } from './event-bus';
import { filterExistsBlocks } from './func';
import { retrieveResvFromBlocks } from '@/func/reserve';
import notebooks from './global-notebooks';

import type DailyNoteTodayPlugin from '@/index';
Expand Down Expand Up @@ -128,168 +126,3 @@ class SettingManager {

export const settings: SettingManager = new SettingManager();

const ReserveFile = 'Reservation.json';

class ReservationManger {
plugin: DailyNoteTodayPlugin;
reserved: Map<string, string>;
//@deprecated 后期把内置的缓存删掉,完全基于块属性来
reservations: { "OnDate": { [date: string]: string[] } } = { "OnDate": {} };

constructor() {
this.reserved = new Map<string, string>();
}

/**
* 更新预约信息, 更新预约的块对应的日期的缓存
*/
private updateReserved() {
for (let date in this.reservations.OnDate) {
for (let blockId of this.reservations.OnDate[date]) {
this.reserved.set(blockId, date);
}
}
}

/**
* 检查块是否已经被预约, 以避免出现重复预约的情况
* @param blockId 预约块的 ID
* @returns 返回预约的日期,如果没有预约,则返回 undefined
*/
findReserved(blockId: string): string | undefined {
return this.reserved.get(blockId);
}

/**
* 删除某个已经被预约的块
* @param date 预约块预约到的日期
* @param blockId 预约块的 ID
*/
removeReservation(date: string, blockId: string) {
let index = this.reservations.OnDate[date].indexOf(blockId);
if (index >= 0) {
this.reservations.OnDate[date].splice(index, 1);
}
}

dateTemplate(date: Date) {
//确保日期格式为 YYYYMMDD
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
return `${year}${month < 10 ? '0' + month : month}${day < 10 ? '0' + day : day}`;
}

setPlugin(plugin: DailyNoteTodayPlugin) {
this.plugin = plugin;
}

async load() {
let loaded = await this.plugin.loadData(ReserveFile);
if (loaded == null || loaded == undefined || loaded == '') {
//如果没有配置文件,则使用默认配置,并保存
console.debug(`没有预约文件,使用默认配置`)
} else {
//如果有配置文件,则使用配置文件
console.log(`读入预约文件: ${ReserveFile}`)
console.log(loaded);
if (typeof loaded === 'string') {
loaded = JSON.parse(loaded);
}
try {
for (let key in loaded) {
this.reservations[key] = loaded[key];
}
} catch (error_msg) {
console.error(`Setting load error: ${error_msg}`);
}
}
await this.syncWithBlock();
this.doPurgeExpired();
this.updateReserved();
this.save();
}

async syncWithBlock() {
let reservations: Reservation[] = await retrieveResvFromBlocks('future');
for (let reservation of reservations) {
let blockId = reservation.id;
let date = reservation.date;
if (!(date in this.reservations.OnDate)) {
this.reservations.OnDate[date] = [];
}
if (this.reservations.OnDate[date].indexOf(blockId) < 0) {
this.reservations.OnDate[date].push(blockId);
}
this.reserved.set(blockId, date);
}
}

save() {
let json = JSON.stringify(this.reservations);
console.debug(`写入预约文件: ${json}`);
this.plugin.saveData(ReserveFile, json);
}

//添加预约
doReserve(date: Date, blockId: string) {
// YYYYMMDD
console.debug(`预约: ${blockId}${date}`);

let reserved = this.findReserved(blockId);
if (reserved) {
this.removeReservation(reserved, blockId);
console.debug(`已经预约到 ${reserved}${blockId} , 现在删除原来的预约`);
}
let date_str = this.dateTemplate(date);
//如果没有这个日期的预约,则创建
if (!(date_str in this.reservations.OnDate)) {
this.reservations.OnDate[date_str] = [];
}
if (this.reservations.OnDate[date_str].indexOf(blockId) < 0) {
this.reservations.OnDate[date_str].push(blockId);
}
this.reserved.set(blockId, date_str);
}

//清理已经过期的预约
doPurgeExpired() {
let date = new Date();
let date_str = this.dateTemplate(date);
let datenum = parseInt(date_str);
for (let key in this.reservations.OnDate) {
if (parseInt(key) + 2 < datenum) {
delete this.reservations.OnDate[key];
}
if (this.reservations.OnDate[key]?.length === 0) {
delete this.reservations.OnDate[key];
}
}
}

//删除所有空掉的块
async doPrune() {
let allBlockId: Set<string> = new Set();
for (const date of Object.keys(this.reservations.OnDate)) {
for (const blockId of this.reservations.OnDate[date]) {
allBlockId.add(blockId);
}
}
if (allBlockId.size == 0) {
return;
}
let allBlockIdArray = Array.from(allBlockId);
let existsBlockIds: Set<string> = await filterExistsBlocks(allBlockIdArray);
// console.log(allBlockIdArray, existsBlockIds);
const OnDate = this.reservations.OnDate;
for (const date of Object.keys(OnDate)) {
const before = OnDate[date];
OnDate[date] = OnDate[date].filter(blockId => existsBlockIds.has(blockId));
console.debug(`Filter ${date}: [${before}] --> [${OnDate[date]}]`)
}
this.save();
}
}

export const reservation: ReservationManger = new ReservationManger();

5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { updateTodayReservation, reserveBlock, dereserveBlock } from './func/res
import { updateStyleSheet, removeStyleSheet, toggleGeneralDailynoteKeymap, openDefaultDailyNote } from './func';

import { setApp, setI18n, setIsMobile, setPlugin, getFocusedBlock } from './utils';
import { settings, reservation } from './global-status';
import { settings } from './global-status';
import notebooks from './global-notebooks';

import { eventBus } from './event-bus';
Expand Down Expand Up @@ -49,13 +49,12 @@ export default class DailyNoteTodayPlugin extends Plugin {
setPlugin(this); //设置全局 plugin

settings.setPlugin(this);
reservation.setPlugin(this);

//初始化 UI
this.initPluginUI();

//初始化数据
await Promise.all([reservation.load(), settings.load(), notebooks.init()]);
await Promise.all([settings.load(), notebooks.init()]);

this.toggleDnHotkey(settings.get('ReplaceAlt5Hotkey'));

Expand Down

0 comments on commit 18f0ee7

Please sign in to comment.