generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
115 lines (96 loc) · 2.45 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
import { App, Plugin, PluginSettingTab, Setting, TAbstractFile, View } from 'obsidian'
declare module "obsidian" {
interface Vault {
fileMap: {
[name: string]: File
}
}
interface WorkspaceLeaf {
rebuildView: () => void
}
interface View {
files?: any
fileItems?: any
}
}
type FileExplorerView = View & {
files: any
fileItems: any
onModify: () => void
}
type Settings = {
ignoredFiles: string
}
const DEFAULTS: Settings = {
ignoredFiles: ''
}
function isFileExplorerView(view: View): view is FileExplorerView {
return view.files && view.fileItems
}
export default class ObsidianIgnore extends Plugin {
settings: Settings
filesToIgnore: string[]
async onload() {
await this.loadSettings()
this.addSettingTab(new SettingTab(this.app, this))
this.app.vault.on("create", this.processFile.bind(this))
}
onunload() {
// TODO: Restore ignored files on unload
}
processFile(file: TAbstractFile) {
if (this.filesToIgnore.contains(file.name)) {
this.removeIgnoredFile(file.name)
}
}
removeIgnoredFile(name: string) {
console.log(`Ignoring file: ${name}`)
if (!this.app.vault.fileMap[name]) {
console.warn(`Could not find file "${name}" to ignore`)
} else {
delete this.app.vault.fileMap[name]
this.filesToIgnore.remove(name)
this.reloadFileExplorer()
}
}
reloadFileExplorer() {
this.app.workspace.iterateAllLeaves(l => {
const view = l.view
if (isFileExplorerView(view)) {
console.log('Reloading file explorer')
l.rebuildView()
}
})
}
async loadSettings() {
console.log('Loading settings...')
this.settings = Object.assign({}, DEFAULTS, await this.loadData())
this.filesToIgnore = this.settings.ignoredFiles.split(',')
console.log('Files to ignore: ' + JSON.stringify(this.filesToIgnore))
}
async saveSettings() {
await this.saveData(this.settings)
}
}
class SettingTab extends PluginSettingTab {
plugin: ObsidianIgnore
constructor(app: App, plugin: ObsidianIgnore) {
super(app, plugin)
this.plugin = plugin
}
display(): void {
this.containerEl.empty()
this.containerEl.createEl('h2', { text: 'Ignored File Settings' })
new Setting(this.containerEl)
.setName('Ignored files')
.setDesc('File/folder names to ignore')
.addText(text => text
.setPlaceholder('someFolder,someFile.txt')
.setValue(this.plugin.settings.ignoredFiles)
.onChange(async (value) => {
this.plugin.settings.ignoredFiles = value
await this.plugin.saveSettings()
})
)
}
}