...
 
Commits (5)
    https://gitcode.net/frostime/siyuan-plugin-daily-note/-/commit/4634d35dced0c9a2b7764407c077dafca3542a09 fix #37, 原因是 setTimeout 里 this 已经失效了 2023-05-18T21:19:31+08:00 frostime frostime@foxmail.com https://gitcode.net/frostime/siyuan-plugin-daily-note/-/commit/dcc64845f3bbe30a8fb3c583e9e21c9a6049e2e9 减少输出的日志 close #38 2023-05-18T21:28:26+08:00 frostime frostime@foxmail.com https://gitcode.net/frostime/siyuan-plugin-daily-note/-/commit/473385037e3b1f04d54a8286bf89a38249f23217 log 2023-05-18T21:34:59+08:00 frostime frostime@foxmail.com https://gitcode.net/frostime/siyuan-plugin-daily-note/-/commit/dced655c5dd37599b2dc7b7267a2c919151e2a32 fix #35 2023-05-18T21:51:42+08:00 frostime frostime@foxmail.com 原因是 bindMenuOnCurrentTabs 和 MutationObserver 中增加的监听器不是一个对象 导致在后者中添加的监听器对象在结束的时候没有被正确销毁。 https://gitcode.net/frostime/siyuan-plugin-daily-note/-/commit/9311627924d00a79b2bbdec063946147852aa16e Merge pull request #39 from frostime/v1.0.3-dev 2023-05-18T21:57:41+08:00 Frostime frostime@foxmail.com V1.0.3 dev 稳定性修复
......@@ -9,14 +9,14 @@ import { eventBus } from "../event-bus";
import * as serverApi from '../serverApi';
//右键菜单的监听器
let gutterContextMenuEvent: EventListenerOrEventListenerObject;
let gutterContextMenuEventObj: EventListenerOrEventListenerObject;
export class ContextMenu {
private observer: MutationObserver | null = null;
constructor() {
gutterContextMenuEvent = (event: MouseEvent) => { this.gutterContextMenuEvent(event) }
gutterContextMenuEventObj = (event: MouseEvent) => { this.gutterContextMenuEvent(event) }
}
bindMenuOnCurrentTabs() {
......@@ -24,8 +24,8 @@ export class ContextMenu {
let gutters: NodeListOf<Element> = document.querySelectorAll('div.protyle-gutters');
info(`监听当前的 ${gutters.length} 个 Tab 标签上的 gutter`);
for (let g of gutters) {
info(`监听 Tab ${g.parentElement?.getAttribute('data-id')}`);
g.addEventListener('contextmenu', gutterContextMenuEvent);
// info(`监听 Tab ${g.parentElement?.getAttribute('data-id')}`);
g.addEventListener('contextmenu', gutterContextMenuEventObj);
}
}
......@@ -33,18 +33,16 @@ export class ContextMenu {
let gutters: NodeListOf<Element> = document.querySelectorAll('div.protyle-gutters');
info(`解除监听当前的 ${gutters.length} 个 Tab 标签上的 gutter`);
for (let g of gutters) {
info(`解除对 ${g.parentElement?.getAttribute('data-id')} 的监听`);
g.removeEventListener('contextmenu', gutterContextMenuEvent);
// info(`解除对 ${g.parentElement?.getAttribute('data-id')} 的监听`);
g.removeEventListener('contextmenu', gutterContextMenuEventObj);
}
}
addEditorTabObserver() {
info(`开始对 Tab 标签变化的监听`);
// info(`开始对 Tab 标签变化的监听`);
let centerLayout = document.querySelector('#layouts div.layout__center div.layout-tab-container') as HTMLElement;
let gutterContextMenuEvent = (event: MouseEvent) => { this.gutterContextMenuEvent(event) };
this.observer = new MutationObserver(function (mutationsList) {
info(`监听到标签页发生变化:`);
console.log(mutationsList);
for (var mutation of mutationsList) {
if (mutation.type == 'childList' && mutation.addedNodes.length) {
......@@ -53,9 +51,9 @@ export class ContextMenu {
let gutter: HTMLDivElement | null = protyle.querySelector(
'div.protyle-gutters'
);
gutter?.addEventListener('contextmenu', gutterContextMenuEvent);
gutter?.addEventListener('contextmenu', gutterContextMenuEventObj);
let data_id = protyle.getAttribute('data-id');
info(`Add Listener: protyle-${data_id}`);
info(`标签页发生变化, Add Listener to protyle-${data_id}`);
}
}
if (mutation.type == 'childList' && mutation.removedNodes.length) {
......@@ -65,9 +63,9 @@ export class ContextMenu {
let gutter: HTMLDivElement | null = protyle.querySelector(
'div.protyle-gutters'
);
gutter?.removeEventListener('contextmenu', gutterContextMenuEvent);
gutter?.removeEventListener('contextmenu', gutterContextMenuEventObj);
let data_id = protyle.getAttribute('data-id');
info(`Del Listener: protyle-${data_id}`);
info(`标签页发生变化 Del Listener of protyle-${data_id}`);
}
}
}
......@@ -83,8 +81,8 @@ export class ContextMenu {
}
removeEditorTabObserver() {
info(`停止对 Tab 标签变化的监听`);
if (this.observer) {
info(`停止对 Tab 标签变化的监听`);
this.observer.disconnect();
}
}
......@@ -107,7 +105,7 @@ export class ContextMenu {
let data_id = src_ele.getAttribute('data-node-id');
if (data_id && event.altKey) {
info(`Contextemnu on: ${data_id}`);
// info(`Contextemnu on: ${data_id}`);
let menu = new Menu('MoveMenu');
menu.addItem({
label: i18n.MoveMenu.Move,
......
......@@ -50,7 +50,6 @@ export class ToolbarMenuItem {
}
showMenu() {
info('点击了今日日记按钮');
// await this.updateDailyNoteStatus();
let menu = new Menu("dntoday-menu");
let menuItems = this.createMenuItems();
......@@ -84,7 +83,7 @@ export class ToolbarMenuItem {
* 初始化的时候,加载所有的笔记本
*/
autoOpenDailyNote() {
info('Auto open daily note');
info('自动开启日记');
if (notebooks.notebooks.length > 0) {
if (settings.settings.OpenOnStart === true) {
let notebookId: string = settings.get('DefaultNotebook');
......@@ -105,7 +104,6 @@ export class ToolbarMenuItem {
}
async updateDailyNoteStatus() {
info('Update daily note status');
let diaryStatus: Map<string, boolean> = await currentDiaryStatus();
notebooks.notebooks.forEach((notebook) => {
let status = diaryStatus.get(notebook.id);
......
......@@ -104,8 +104,6 @@ export async function queryNotebooks(): Promise<Array<Notebook> | null> {
if (notebook.icon == "") {
notebook.icon = "1f5c3";
}
info(`${notebook.name}: ${notebook.dailynoteSprig} - ${notebook.dailynotePath}`)
}
info(`Read all notebooks: ${all_notebook_names}`);
......@@ -125,7 +123,6 @@ export async function queryNotebooks(): Promise<Array<Notebook> | null> {
* 2. 对每种 hpath,调用 `await getDocsByHpath(todayDNHpath)`,查询是否存在对应的文件
*/
export async function currentDiaryStatus() {
info('updateDiaryStatus');
// let todayDiary = getTodayDiaryPath();
//所有 hpath 的配置方案
let hpath_set: Set<string> = new Set();
......@@ -144,10 +141,9 @@ export async function currentDiaryStatus() {
diaryStatus.set(notebookId, true);
});
count_diary += notebook_with_diary.length;
info(`${todayDNHpath}${notebook_with_diary.length} 篇`)
}
}
info(`当前日记共 ${count_diary} 篇`);
info(`更新日记状态: 当前日记共 ${count_diary} 篇`);
return diaryStatus;
}
......@@ -191,9 +187,8 @@ export async function getDocsByHpath(hpath: string, notebook: Notebook | null =
export async function createDiary(notebook: Notebook, todayDiaryHpath: string) {
info(`Try to create: ${notebook.name} ${todayDiaryHpath}`);
let doc_id = await serverApi.createDocWithMd(notebook.id, todayDiaryHpath, "");
info(`Create new diary ${doc_id}`);
info(`创建日记: ${notebook.name} ${todayDiaryHpath}`);
return doc_id;
}
......@@ -204,7 +199,7 @@ export async function createDiary(notebook: Notebook, todayDiaryHpath: string) {
*/
export async function openDiary(notebook: Notebook) {
let todayDiaryPath = notebook.dailynotePath;
info(`Open ${notebook.name}/${todayDiaryPath}`);
info(`打开日记 ${notebook.name}${todayDiaryPath}`);
//queryNotebooks() 保证了 todayDiaryPath 不为 null
let docs = await getDocsByHpath(todayDiaryPath!, notebook);
......
......@@ -40,7 +40,6 @@ class Notebooks {
* @calledby: this.onload()
*/
async init(MAX_RETRY: number = 5, RETRY_INTERVAL: number = 1000) {
info('Notebooks init');
let retry = 0;
while (retry < MAX_RETRY) {
let result = await queryNotebooks();
......
......@@ -56,26 +56,22 @@ class SettingManager {
* 导入的时候,需要先加载设置;如果没有设置,则使用默认设置
*/
async load() {
info(`Read storage: `);
let loaded = await this.plugin.loadData(ConfigFile);
info(`Read storage done: `);
console.log(loaded);
if (loaded == null || loaded == undefined || loaded == '') {
//如果没有配置文件,则使用默认配置,并保存
info(`Setting not found, use default setting`)
info(`没有配置文件,使用默认配置`)
this.save();
} else {
//如果有配置文件,则使用配置文件
info(`读入配置文件: DailyNoteToday.json`)
loaded = JSON.parse(loaded);
info(`读入配置文件: ${ConfigFile}`)
console.log(loaded);
loaded = JSON.parse(loaded);
try {
for (let key in loaded) {
this.set(key, loaded[key]);
}
} catch (error_msg) {
error(`Setting load error: ${error_msg}`);
console.log(error_msg);
}
this.save();
}
......@@ -83,7 +79,7 @@ class SettingManager {
async save() {
let json = JSON.stringify(this.settings);
info(`Write storage: ${json}`);
info(`写入配置文件: ${json}`);
this.plugin.saveData(ConfigFile, json);
}
}
......
......@@ -25,11 +25,10 @@ export default class DailyNoteTodayPlugin extends Plugin {
upToDate: any = null;
async onload() {
info('plugin load');
console.log(this);
info('Plugin load');
setI18n(this.i18n); //设置全局 i18n
info(`Start: ${new Date()}`);
settings.setPlugin(this);
let start = performance.now();
......@@ -53,12 +52,14 @@ export default class DailyNoteTodayPlugin extends Plugin {
// 如果有笔记本,且设置中允许启动时打开,则打开第一个笔记本
this.toolbar_item.autoOpenDailyNote();
// 等日记创建,完成了状态更新后再读取新的状态
setTimeout(this.toolbar_item.updateDailyNoteStatus.bind(this), 2000);
setTimeout(
() => this.toolbar_item.updateDailyNoteStatus(), 2000
);
eventBus.subscribe('UpdateAll', () => {this.updateAll()});
let end = performance.now();
info(`Onload, 耗时: ${end - start} ms`);
info(`启动耗时: ${end - start} ms`);
}
/**
......@@ -74,7 +75,6 @@ export default class DailyNoteTodayPlugin extends Plugin {
}
private initSetting() {
info('initSetting');
let div_setting: HTMLDivElement = document.createElement('div');
this.component_setting = new Setting({
target: div_setting,
......@@ -96,14 +96,12 @@ export default class DailyNoteTodayPlugin extends Plugin {
}
private async initContextMenu() {
info('initContextMenu');
this.menu = new ContextMenu();
this.menu.bindMenuOnCurrentTabs();
this.menu.addEditorTabObserver();
}
private initToolbarItem() {
info('initToolbarItem');
this.toolbar_item = new ToolbarMenuItem(this);
this.toolbar_item.updateDailyNoteStatus();
}
......@@ -173,11 +171,12 @@ export default class DailyNoteTodayPlugin extends Plugin {
}
onunload() {
info('plugin unload')
info('Plugin unload')
this.menu.releaseMenuOnCurrentTabs();
this.menu.removeEditorTabObserver();
settings.save();
if (this.upToDate) {
info(`清理定时器 ${this.upToDate}`);
clearTimeout(this.upToDate);
this.upToDate = null;
}
......