From 18f0ee70d4a53f7f2fa4be7dfcde51ae307fc614 Mon Sep 17 00:00:00 2001 From: frostime Date: Thu, 26 Sep 2024 16:25:14 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20rm:=20=E5=88=A0=E6=8E=89?= =?UTF-8?q?=E6=89=80=E6=9C=89=E5=85=B3=E4=BA=8E=20Reservation=20=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- plugin.json | 2 +- src/func/reserve/index.ts | 9 +- src/func/reserve/reserve.ts | 14 +-- src/global-status.ts | 169 +----------------------------------- src/index.ts | 5 +- 6 files changed, 17 insertions(+), 184 deletions(-) diff --git a/package.json b/package.json index bb834fe..1400af5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "siyuan-dailynote-today", - "version": "1.6.8", + "version": "1.6.9", "description": "", "main": ".src/index.js", "keywords": [], diff --git a/plugin.json b/plugin.json index dafa19f..2ceb97c 100644 --- a/plugin.json +++ b/plugin.json @@ -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 管理 + 移动块功能", diff --git a/src/func/reserve/index.ts b/src/func/reserve/index.ts index 222c2f0..6dba63e 100644 --- a/src/func/reserve/index.ts +++ b/src/func/reserve/index.ts @@ -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'; @@ -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 中 diff --git a/src/func/reserve/reserve.ts b/src/func/reserve/reserve.ts index f0512dc..0736ef4 100644 --- a/src/func/reserve/reserve.ts +++ b/src/func/reserve/reserve.ts @@ -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 = '一二三四五六七八九'; @@ -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}` }); diff --git a/src/global-status.ts b/src/global-status.ts index 0368994..fdfa071 100644 --- a/src/global-status.ts +++ b/src/global-status.ts @@ -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'; @@ -128,168 +126,3 @@ class SettingManager { export const settings: SettingManager = new SettingManager(); -const ReserveFile = 'Reservation.json'; - -class ReservationManger { - plugin: DailyNoteTodayPlugin; - reserved: Map; - //@deprecated 后期把内置的缓存删掉,完全基于块属性来 - reservations: { "OnDate": { [date: string]: string[] } } = { "OnDate": {} }; - - constructor() { - this.reserved = new Map(); - } - - /** - * 更新预约信息, 更新预约的块对应的日期的缓存 - */ - 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 = 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 = 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(); - diff --git a/src/index.ts b/src/index.ts index 220d173..0750743 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -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'));