-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
486 lines (398 loc) · 11.1 KB
/
Copy pathmain.ts
File metadata and controls
486 lines (398 loc) · 11.1 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// 导入库
import { MarkdownDocument } from 'markdown';
import moment from "moment";
import { JsonObjectExpression } from 'typescript';
import {
App,
Modal,
Vault,
TFile,
Notice,
TAbstractFile,
Plugin,
PluginManifest,
PluginSettingTab,
Setting,
ItemView, View,
Workspace,
WorkspaceLeaf,
} from 'obsidian';
const VIEW_TYPE = "task-list";
// 接口
interface TaskManageSettings {
mySetting: string;
takspath: string;
}
// 插件默认设置
const DEFAULT_SETTINGS: TaskManageSettings = {
mySetting: 'default',
takspath: "TaskManage",
}
// 插件主要功能
export default class TaskManagePlugin extends Plugin {
settings: TaskManageSettings;
private viewProxy: TaskManageListItemViewProxy;
private taskmanagelist : TaskManageList;
private taskmanagecontrol: TaskManageController;
constructor(app: App, manifest: PluginManifest) {
super(app, manifest);
// const leaf = this.app.workspace.get
this.taskmanagelist = new TaskManageList();
this.viewProxy = new TaskManageListItemViewProxy(app.workspace);
this.taskmanagecontrol = new TaskManageController(app.vault, this.viewProxy);
}
async onload() { // 插件重载
console.log('loading plugin'); // 打印插件加载信息
await this.loadSettings(); // 导入配置
// 界面---左边的按钮
this.addRibbonIcon('dice', 'Sample Plugin', () => {
new Notice('This is a notice!');
});
// 界面---状态栏
this.addStatusBarItem().setText('Status Bar Text'); // 设置状态栏文本
// 界面---命令列表
this.addCommand({ // 命令列表里面添加一个命令
id: 'open-sample-modal',
name: 'Open Sample Modal', // 命令的名字
callback: () => {
console.log('Simple Callback');
},
checkCallback: (checking: boolean) => {
let leaf = this.app.workspace.activeLeaf;
if (leaf) {
if (!checking) {
new TaskManageModal(this.app).open(); // 命令的实现类
}
return true;
}
return false;
}
});
this.addCommand({ // 命令列表里面添加一个命令
id: 'test-task',
name: 'Test Task', // 命令的名字
checkCallback: (checking: boolean) => {
if (!checking) {
this.showReminderList();
}
console.log(this.app.workspace.getRightLeaf(false))
console.log(this.app.workspace.getLeftLeaf(false))
console.log(this.app.workspace.getUnpinnedLeaf(VIEW_TYPE))
console.log(this.app.workspace.getLeaf(false)) // 文本编辑区
return true;
},
});
// 界面---设置界面
this.addSettingTab(new TaskManageSettingTab(this.app, this));
// 编辑界面
this.registerCodeMirror((cm: CodeMirror.Editor) => {
console.log('codemirror', cm);
});
this.registerDomEvent(document, 'click', (evt: MouseEvent) => { // 注册点击事件
// console.log('click', evt);
});
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000)); // 设置时间间隔 定时任务
// this.registerView("债务列表", (leaf : WorkspaceLeaf) => {return new TaskManageListItemView(leaf);})
this.registerView(VIEW_TYPE, (leaf: WorkspaceLeaf) => {
return this.viewProxy.createView(this.taskmanagelist, leaf);
});
// this.app.workspace.getRightLeaf(false).setViewState({
// type: VIEW_TYPE,
// });
// 界面---打开通知界面
if (this.app.workspace.layoutReady) {
console.log("view", "115")
this.viewProxy.openView();
} else {
(this.app.workspace as any).on("layout-ready", () => {
this.viewProxy.openView();
});
console.log("view", "122")
}
this.watchVault();
}
// 通知---文件更改通知
private watchVault() {
[
this.app.vault.on("modify", async (file) => {
this.taskmanagecontrol.reloadFile(file, true);
}),
this.app.vault.on("delete", (file) => {
this.taskmanagecontrol.removeFile(file.path);
}),
this.app.vault.on("rename", (file, oldPath) => {
this.taskmanagecontrol.removeFile(oldPath);
this.taskmanagecontrol.reloadFile(file);
}),
].forEach(eventRef => {
this.registerEvent(eventRef);
})
}
showReminderList(): void {
if (this.app.workspace.getLeavesOfType(VIEW_TYPE).length) {
console.log("view", "145")
return;
}
console.log("view", "148")
this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_TYPE,
});
}
onunload() { // 卸载插件
console.log('unloading plugin');
}
// 导入配置
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
// 保存配置
async saveSettings() {
await this.saveData(this.settings);
}
}
// 实用的命令
class TaskManageModal extends Modal {
constructor(app: App) {
super(app);
}
// 打开的程序
onOpen() {
let {contentEl} = this;
this.app.workspace.getRightLeaf(true).setViewState({
type: VIEW_TYPE,
});
contentEl.setText('Woah888!'); // 出来的一个窗口
console.log("www--:", this.containerEl)
}
// 关闭的程序
onClose() {
let {contentEl} = this;
contentEl.empty();
}
}
// 插件的设置界面类
class TaskManageSettingTab extends PluginSettingTab {
plugin: TaskManagePlugin;
constructor(app: App, plugin: TaskManagePlugin) {
super(app, plugin);
this.plugin = plugin;
}
// 界面配置
display(): void {
let {containerEl} = this;
containerEl.empty();
// 界面的名称
containerEl.createEl('h2', {text: '插件设置'});
this.plugin.settings.takspath
// 一个配置项
new Setting(containerEl)
.setName('任务目录') // 设置名称
.setDesc('任务文件存放的目录') // 设置说明
.addText(text => text
.setPlaceholder('选择想要设置的目录')
.setValue(this.plugin.settings.takspath)
.onChange(async (value) => {
console.log('Secret: ' + value);
this.plugin.settings.takspath = value; //保存设置到变量
await this.plugin.saveSettings(); //保存设置到文件
}));
}
}
// 控制器模型
class TaskManageController{
taskmanageview: TaskManageListItemViewProxy
constructor(private vault: Vault, taskview: TaskManageListItemViewProxy) {
this.taskmanageview = taskview
}
async reloadFile(file: TAbstractFile, reloadUI: boolean = false) {
console.log("reload file: path=%s");
if (!(file instanceof TFile)) {
console.debug("Cannot read file other than TFile: file=%o", file);
return;
}
if (!this.isMarkdownFile(file)) {
console.debug("Not a markdown file: file=%o", file);
return;
}
const content = new Content(file.path, await this.vault.cachedRead(file));
content.getTaskManage()
// 界面更新
this.taskmanageview.reload()
return content
}
async removeFile(path: string) {
console.log("Remove file: path=%s", path);
}
async openFile(path: string) {
console.log("open file: path=%s", path);
}
private isMarkdownFile(file: TFile) {
return file.extension.toLowerCase() === "md";
}
}
// 开始时间: 2021-09-08
// 结束时间: 2021-10-08
// 重复间隔: 每天 每周一二三
// 完成记录:
// 进度: 3%
// 完成开始时间: 2021-09-08 16:24
// 完成结束时间: 2021-09-08 18:23
// 花费时间:
// 任务备注: 开发ob的任务提醒插件
// {
// "starttime": "2021-9-11 23:50",
// "endtime": "2021-9-11 23:50",
// "reparttime": "everyday",
// "progress": 2,
// "completerecord": [
// {
// "start": "2021-9-11 23:56",
// "end": "2021-9-11 23:57"
// },
// {
// "start": "2021-9-11 23:56",
// "end": "2021-9-11 23:57"
// }
// ],
// "totaltime": "12.5",
// "note": "hello"
// }
class Task {
starttime: string
endtime: string
reparttime: string
progress: number
completerecord: []
totaltime: string // 总计时间
note: string // 备注
}
class TaskParse {
regexp : RegExp
constructor() {
this.regexp = new RegExp("(?<=##).*?(?=##)");
}
public parse(data: string) {
const result = this.regexp.exec(data)
if (result) {
console.log("pause", result.length)
var sjson = JSON.parse(result[0])
console.log(sjson)
}
return result
}
}
class DateTime {
now() {
return moment().format('YYYY-MM-DD HH:mm:ss');
// console.log(now)
}
}
// 读取md文件的内容
class Content {
private doc: MarkdownDocument;
private taskparse: TaskParse;
private tasklist: Array<string>;
constructor(file: string, content: string) {
this.doc = new MarkdownDocument(file, content);
this.taskparse = new TaskParse();
}
public getTaskManage(){
this.doc.getTodos().forEach(todo => {
if (todo.checked) {
return;
}
// 解析任务的字符串 --flag--
// const parsed = parseReminder(this.doc.file, todo.lineIndex, todo.body);
console.log("getTaskManage", todo.lineIndex, todo.checked, todo.body);
var date = this.taskparse.parse(todo.body)
console.log(date)
})
}
}
class TaskManageList {
tasklist : Array<string>
constructor() {
this.tasklist = []
}
setlist() {
}
addlist (task: string) {
this.tasklist.push(task)
}
getlist () {
}
}
// 任务列表显示窗口
class TaskManageListItemView extends ItemView {
private view: TaskManageList;
constructor(
tasklist : TaskManageList,
leaf: WorkspaceLeaf,
) {
super(leaf);
this.view = tasklist;
}
getViewType(): string {
return VIEW_TYPE;
}
getDisplayText(): string {
return VIEW_TYPE;
}
getIcon(): string {
return "clock";
}
async onOpen(): Promise<void> {
console.log("reminder", "67", "listview open")
this.contentEl.appendText("hello")
}
reload() {
console.log("reminder", "67", "listview reload")
// this.contentEl.appendText("hello reload\r\n")
// this.containerEl.appendText("tain e1 \n")
// this.containerEl.appendChild
this.contentEl.setText("<main><div> 4543 </div></main>")
// this.contentEl
}
onClose(): Promise<void> {
console.log("reminder", "67", "listview close")
return
}
setText() {
// this.contentEl.setText("")
this.contentEl.appendText("1111")
this.contentEl.appendText("222")
}
}
class TaskManageListItemViewProxy {
constructor(
private workspace: Workspace
) { }
createView(tasklist:TaskManageList, leaf: WorkspaceLeaf): View {
return new TaskManageListItemView(
tasklist,
leaf
);
}
openView(): void {
if (this.workspace.getLeavesOfType(VIEW_TYPE).length) {
// reminder list view is already in workspace
// console.log(this.workspace.getLeavesOfType(VIEW_TYPE))
return;
}
// Create new view
this.workspace.getRightLeaf(false).setViewState({
type: VIEW_TYPE,
});
}
reload(force: boolean = false) {
const views = this.getViews();
views.forEach(view => view.reload())
console.log("TaskManageListItemViewProxy", views)
// console.log(this.workspace.getRightLeaf(false))
}
private getViews() {
return this.workspace
.getLeavesOfType(VIEW_TYPE)
.map(leaf => leaf.view as TaskManageListItemView);
}
}