generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
158 lines (127 loc) · 4.67 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
import { TFile, Plugin } from 'obsidian';
import folgezettelUtilities from "src/folgezettelUtilities"
import FolgezettelView from 'src/folgezettelView';
import indentClasses from 'src/folgezettelIndentClasses';
import colorClasses from 'src/folgezettelColorClasses';
// Renaming a file to be a child of itself breaks the plugin
export default class FolgeRenamingPlugin extends Plugin {
async unload() {
this.app.workspace.detachLeavesOfType(FolgezettelView.folgezettelViewType);
}
async activateView() {
this.app.workspace.detachLeavesOfType(FolgezettelView.folgezettelViewType);
await this.app.workspace.getRightLeaf(false).setViewState({
type: FolgezettelView.folgezettelViewType,
active: true,
});
this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(FolgezettelView.folgezettelViewType)[0]
);
}
async fixFileFolge(file:Element) {
}
async fixNumbering() {
const fileExplorer = document.querySelector('.nav-folder-children');
if (fileExplorer == null) {
return
}
const files = fileExplorer.querySelectorAll('.nav-file');
files.forEach(this.fixFileFolge);
}
updateFoglezettelsView() {
this.app.workspace.getLeavesOfType(FolgezettelView.folgezettelViewType).forEach((leaf) => {
if (!(leaf.view instanceof FolgezettelView)) {
return
}
leaf.view.recalc()
});
}
attemptIndentFile(file: Element) {
var titleContentDiv = file.querySelector('.nav-file-title')
if (titleContentDiv == null) {
return
}
var titleDiv = titleContentDiv.querySelector('.nav-file-title-content')
if (titleDiv == null) {
return
}
var fileFolgeIdentifier = folgezettelUtilities.getFolgeIdentifier(titleDiv.innerHTML)
if (!folgezettelUtilities.isFolgeFile(fileFolgeIdentifier)) {
return
}
var folgeTokens = folgezettelUtilities.getFolgeTokens(fileFolgeIdentifier)
const classIndex = indentClasses[folgeTokens.length] != undefined ? folgeTokens.length : folgeTokens.length - 1
titleDiv.removeClasses(colorClasses)
titleDiv.addClass(colorClasses[classIndex])
file.removeClasses(indentClasses)
file.addClass(indentClasses[classIndex])
}
checkFoldersForIndent(element: Element) {
const files = element.querySelectorAll('.nav-file');
files.forEach(this.attemptIndentFile);
const folders = element.querySelectorAll('.nav-folder');
folders.forEach((folderElement) => { this.checkFoldersForIndent(folderElement) })
}
indentFiles() {
const fileExplorer = document.querySelector('.nav-folder-children');
if (fileExplorer == null) {
return
}
this.checkFoldersForIndent(fileExplorer)
}
attemptUpdateFolgeFilesForRename(newFile: TFile, oldPath: string) {
const oldFileName = oldPath.substring(oldPath.lastIndexOf('/')+1, oldPath.length);
var oldFolgeIdentifier = folgezettelUtilities.getFolgeIdentifier(oldFileName)
if (!folgezettelUtilities.isFolgeFile(oldFolgeIdentifier)) {
return
}
var newFolgeIdentifier = folgezettelUtilities.getFolgeIdentifier(newFile.basename)
if (!folgezettelUtilities.isFolgeFile(newFolgeIdentifier)) {
return
}
if (oldFolgeIdentifier == newFolgeIdentifier) {
// We do not both with moves via folders, move everything if you must
return
}
var files = this.app.vault.getMarkdownFiles();
files.forEach(file => {
this.updateFile(file, oldFolgeIdentifier, newFolgeIdentifier)
});
}
updateFile(file: TFile, oldParentFolgeIdentifier: string, newParentFolgeIdentifier: string){
var fileFolgeIdentifier = folgezettelUtilities.getFolgeIdentifier(file.basename)
if (!folgezettelUtilities.isFolgeFile(fileFolgeIdentifier)) {
return
}
var oldParentFolgeTokens = folgezettelUtilities.getFolgeTokens(oldParentFolgeIdentifier)
var fileFolgeTokens = folgezettelUtilities.getFolgeTokens(fileFolgeIdentifier)
if (!folgezettelUtilities.isDirectParent(fileFolgeTokens, oldParentFolgeTokens)) {
// This isn't a parent of mine
return
}
const newPath = folgezettelUtilities.getNewPathForFolgezettelWithMovedParent(oldParentFolgeIdentifier, newParentFolgeIdentifier, file)
this.app.fileManager.renameFile(file, newPath)
}
async onload() {
console.log("Loading Folgezettel Renaming Plugin")
this.registerView(
FolgezettelView.folgezettelViewType,
(leaf) => new FolgezettelView(leaf)
);
this.addRibbonIcon("dice", "Activate view", () => {
this.activateView();
});
this.addRibbonIcon("dice", "Fix Folge", () => {
this.fixNumbering();
});
this.registerEvent(
this.app.workspace.on('file-open', (file) => {
this.updateFoglezettelsView()
this.indentFiles()
})
);
this.registerEvent(this.app.vault.on('rename', async (newFile: any, oldPath: string) => {
this.attemptUpdateFolgeFilesForRename(newFile, oldPath)
}));
}
}