generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathmain.ts
182 lines (158 loc) · 4.75 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Menu, Notice, Platform, Plugin, WorkspaceLeaf } from 'obsidian';
import FileManager from './src/fileManager';
import SyncNotebooks from './src/syncNotebooks';
import ApiManager from './src/api';
import { settingsStore } from './src/settings';
import { WereadSettingsTab } from './src/settingTab';
import { WEREAD_BROWSER_VIEW_ID, WereadReadingView } from './src/components/wereadReading';
import './style.css';
export default class WereadPlugin extends Plugin {
private syncNotebooks: SyncNotebooks;
private syncing = false;
async onload() {
console.log('load weread plugin');
settingsStore.initialise(this);
const fileManager = new FileManager(this.app.vault, this.app.metadataCache);
const apiManager = new ApiManager();
this.syncNotebooks = new SyncNotebooks(fileManager, apiManager);
const ribbonEl = this.addRibbonIcon('book-open', '同步微信读书笔记', (event) => {
if (event.button === 0) {
this.startSync();
}
});
ribbonEl.addEventListener('contextmenu', (event: MouseEvent) => {
event.preventDefault();
event.stopPropagation(); // 阻止事件传播
const preventDefaultMouseDown = (mouseDownEvent: MouseEvent) => {
mouseDownEvent.preventDefault();
};
// 额外阻止mousedown事件的默认行为
window.addEventListener('mousedown', preventDefaultMouseDown);
const menu = new Menu();
menu.addItem((item) =>
item
.setTitle('同步微信读书笔记')
.setIcon('refresh-ccw')
.onClick(() => {
this.startSync();
})
);
menu.addItem((item) =>
item
.setTitle('强制同步微信读书笔记')
.setIcon('refresh-ccw-dot')
.onClick(() => {
this.startSync(true);
})
);
menu.addItem((item) =>
item
.setTitle('在新标签页打开微信读书')
.setIcon('book-open-text')
.onClick(() => {
this.activateReadingView('TAB');
})
);
menu.addItem((item) =>
item
.setTitle('在窗口打开微信读书')
.setIcon('app-window')
.onClick(() => {
this.activateReadingView('WINDOW');
})
);
menu.showAtMouseEvent(event);
menu.onHide(() => {
window.removeEventListener('mousedown', preventDefaultMouseDown);
});
});
this.addCommand({
id: 'sync-weread-notes-command',
name: '同步微信读书笔记',
callback: () => {
this.startSync();
}
});
this.addCommand({
id: 'Force-sync-weread-notes-command',
name: '强制同步微信读书笔记',
callback: () => {
this.startSync(true);
}
});
this.registerView(WEREAD_BROWSER_VIEW_ID, (leaf) => new WereadReadingView(leaf));
this.addCommand({
id: 'open-weread-reading-view',
name: '在新标签页打开微信读书',
callback: () => {
this.activateReadingView('TAB');
}
});
this.addCommand({
id: 'open-weread-reading-view',
name: '在新窗口打开微信读书',
callback: () => {
this.activateReadingView('WINDOW');
}
});
this.registerEvent(
this.app.workspace.on('editor-menu', (menu, editor, view) => {
const noteFile = fileManager.getWereadNoteAnnotationFile(view.file);
if (noteFile == null) {
return;
}
menu.addSeparator();
menu.addItem((item) =>
item
.setIcon('refresh-ccw')
.setTitle('同步当前读书笔记')
.onClick(() => {
this.syncNotebooks.syncNotebook(noteFile);
})
);
})
);
this.addSettingTab(new WereadSettingsTab(this.app, this));
}
async startSync(force = false) {
if (this.syncing) {
new Notice('正在同步微信读书笔记,请勿重复点击');
return;
}
this.syncing = true;
try {
await this.syncNotebooks.syncNotebooks(force, window.moment().format('YYYY-MM-DD'));
console.log('syncing Weread note finish');
} catch (e) {
if (Platform.isDesktopApp) {
new Notice('同步微信读书笔记异常,请打开控制台查看详情');
} else {
new Notice('同步微信读书笔记异常,请使用电脑端打开控制台查看详情' + e);
}
console.error('同步微信读书笔记异常', e);
} finally {
this.syncing = false;
}
}
async activateReadingView(type: string) {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null;
const leaves = workspace.getLeavesOfType(WEREAD_BROWSER_VIEW_ID);
if (leaves.length > 0) {
// A leaf with our view already exists, use that
leaf = leaves[0];
} else {
if (type === 'TAB') {
leaf = workspace.getLeaf('split', 'vertical');
} else if (type === 'WINDOW') {
leaf = workspace.openPopoutLeaf();
}
await leaf.setViewState({ type: WEREAD_BROWSER_VIEW_ID, active: true });
}
// "Reveal" the leaf in case it is in a collapsed sidebar
workspace.revealLeaf(leaf);
}
onunload() {
console.log('unloading weread plugin', new Date().toLocaleString());
}
}