generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
94 lines (79 loc) · 2.18 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
import { App, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile } from 'obsidian';
export default class MobileLogging extends Plugin {
async onload() {
console.log('loading mobile logging plugin');
if (this.app.isMobile) {
monkeyPatchConsole(this)
this.addCommand({
id: 'open-console-log',
name: 'Console Log',
// callback: () => {
// console.log('Simple Callback');
// },
checkCallback: (checking: boolean) => {
let leaf = this.app.workspace.activeLeaf;
if (leaf) {
if (!checking) {
new ConsoleModal(this.app).open();
}
return true;
}
return false;
}
});
}
}
onunload() {
console.log('unloading mobile logging plugin');
}
}
// Call this method inside your plugin's `onLoad` function
async function monkeyPatchConsole(plugin: Plugin) {
const logs: string[] = [];
const logMessages = (prefix: string) => async (...messages: unknown[]) => {
const logTFile = plugin.app.vault.getAbstractFileByPath('Log.md') as TFile;
const logFileContent = await plugin.app.vault.read(logTFile)
logs.push(`\n[${prefix}]`);
for (const message of messages) {
logs.push(String(message));
}
await plugin.app.vault.modify(logTFile, logs.join(" "))
};
console.debug = logMessages("debug");
console.error = logMessages("error");
console.info = logMessages("info");
console.log = logMessages("log");
console.warn = logMessages("warn");
}
class ConsoleModal extends Modal {
constructor(app: App) {
super(app);
}
onOpen() {
let logValue = ""
let {contentEl} = this;
contentEl.setText('Enter Log');
new Setting(contentEl).addTextArea((component) => {
component.onChange(val => {
logValue = val
})
})
new Setting(contentEl).addButton(component => {
component.setButtonText('Submit')
component.onClick(e => {
const func = Function(`return ${logValue}`)
const value = func()
if (typeof value === 'object') {
console.log(JSON.stringify(value, null, 2))
} else {
console.log(value)
}
this.close()
})
})
}
onClose() {
let {contentEl} = this;
contentEl.empty();
}
}