-
Notifications
You must be signed in to change notification settings - Fork 5
/
view.ts
109 lines (96 loc) · 3.06 KB
/
view.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
import { ItemView, TAbstractFile, TFile, WorkspaceLeaf } from "obsidian";
import type { CardsViewSettings } from "./settings";
import Root from "./components/Root.svelte";
import store, { Sort } from "./components/store";
import { get } from "svelte/store";
export const VIEW_TYPE = "cards-view";
export class CardsViewPluginView extends ItemView {
private settings: CardsViewSettings;
private svelteRoot?: Root;
constructor(settings: CardsViewSettings, leaf: WorkspaceLeaf) {
super(leaf);
this.settings = settings;
}
getViewType() {
return VIEW_TYPE;
}
getDisplayText() {
return "Cards View";
}
async onOpen() {
const viewContent = this.containerEl.children[1];
store.app.set(this.app);
store.view.set(this);
store.settings.set(this.settings);
store.appCache.set(this.app.metadataCache);
this.registerEvent(
this.app.metadataCache.on("resolved", async () =>
store.appCache.update(() => this.app.metadataCache),
),
);
store.files.set(this.app.vault.getMarkdownFiles());
this.registerEvent(
this.app.vault.on("create", async (file: TAbstractFile) => {
if (file instanceof TFile && file.extension === "md") {
store.files.update((files) => files?.concat(file));
}
}),
);
this.registerEvent(
this.app.vault.on("delete", async (file: TAbstractFile) => {
if (file instanceof TFile && file.extension === "md") {
store.files.update((files) =>
files?.filter((f) => f.path !== file.path),
);
}
}),
);
this.registerEvent(
this.app.vault.on("modify", async (file: TAbstractFile) => {
if (file instanceof TFile && file.extension === "md") {
store.files.update((files) =>
files?.map((f) => (f.path === file.path ? file : f)),
);
}
}),
);
this.registerEvent(
this.app.vault.on(
"rename",
async (file: TAbstractFile, oldPath: string) => {
if (file instanceof TFile && file.extension === "md") {
store.files.update((files) =>
files?.map((f) => (f.path === oldPath ? file : f)),
);
}
},
),
);
this.svelteRoot = new Root({
target: viewContent,
});
// On scroll 80% of viewContent, load more cards
viewContent.addEventListener("scroll", async () => {
if (
viewContent.scrollTop + viewContent.clientHeight >
viewContent.scrollHeight - 500
) {
store.skipNextTransition.set(true);
store.displayedCount.set(get(store.displayedFiles).length + 50);
}
});
this.app.workspace.on("active-leaf-change", () => {
// check our leaf is visible
const rootLeaf = this.app.workspace.getMostRecentLeaf(
this.app.workspace.rootSplit,
);
store.viewIsVisible.set(rootLeaf?.view?.getViewType() === VIEW_TYPE);
});
}
async onClose() {
store.viewIsVisible.set(false);
store.searchQuery.set("");
store.displayedCount.set(50);
store.sort.set(Sort.Modified);
}
}