-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
ScrollView.ts
121 lines (105 loc) · 2.62 KB
/
ScrollView.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
import {
TFile,
ItemView,
WorkspaceLeaf, type ViewStateResult
} from "obsidian";
import ScrollViewComponent from "./ScrollViewComponent.svelte";
import {
type ScrollViewState,
type ScrollViewFile,
VIEW_TYPE_SCROLL
} from "types";
import { writable, type Writable } from "svelte/store";
import TagFolderPlugin from "./main";
import { doEvents } from "./util";
import { mount, unmount } from "svelte";
// Show notes as like scroll.
export class ScrollView extends ItemView {
component?: ReturnType<typeof mount>;
plugin: TagFolderPlugin;
icon = "sheets-in-box";
store: Writable<ScrollViewState>;
state: ScrollViewState = { files: [], title: "", tagPath: "" };
title: string = "";
navigation = true;
getIcon(): string {
return "sheets-in-box";
}
constructor(leaf: WorkspaceLeaf, plugin: TagFolderPlugin) {
super(leaf);
this.plugin = plugin;
this.store = writable<ScrollViewState>({ files: [], title: "", tagPath: "" });
}
getViewType() {
return VIEW_TYPE_SCROLL;
}
getDisplayText() {
return this.state.tagPath || "Tags scroll";
}
async setFile(filenames: ScrollViewFile[]) {
this.state = { ...this.state, files: filenames };
await this.updateView();
}
async setState(state: ScrollViewState, result: ViewStateResult): Promise<void> {
this.state = { ...state };
this.title = state.title;
await this.updateView();
result = {
history: false
};
return;
}
getState() {
return this.state;
}
isFileOpened(path: string) {
return this.state.files.some(e => e.path == path);
}
getScrollViewState(): ScrollViewState {
return this.state;
}
async updateView() {
//Load file content
const items = [] as ScrollViewFile[];
for (const item of this.state.files) {
if (item.content) {
items.push(item);
} else {
const f = this.app.vault.getAbstractFileByPath(item.path);
if (f == null || !(f instanceof TFile)) {
console.log(`File not found:${item.path}`);
items.push(item);
continue;
}
const title = this.plugin.getFileTitle(f);
const w = await this.app.vault.read(f);
await doEvents();
item.content = w;
item.title = title;
items.push(item);
}
}
this.state = { ...this.state, files: [...items] };
this.store.set(this.state);
}
async onOpen() {
const app = mount(ScrollViewComponent,
{
target: this.contentEl,
props: {
store: this.store,
openfile: this.plugin.focusFile,
plugin: this.plugin
},
});
this.component = app;
return await Promise.resolve();
}
async onClose() {
if (this.component) {
unmount(this.component);
this.component = undefined;
}
return await Promise.resolve();
}
}