generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
135 lines (120 loc) · 3.41 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
import { Notice, Plugin } from "obsidian";
import { SearchModal } from "Views/search_modal";
import { ItemsSuggestModal } from "Views/suggest_modal";
import { KinopoiskSuggestItem } from "Models/kinopoisk_response";
import { MoviewShow } from "Models/MovieShow.model";
import {
ObsidianKinopoiskPluginSettings,
DEFAULT_SETTINGS,
ObsidianKinopoiskSettingTab,
} from "Settings/settings";
import {
makeFileName,
getTemplateContents,
replaceVariableSyntax,
} from "Utils/utils";
import { CursorJumper } from "Utils/cursor_jumper";
export default class ObsidianKinopoiskPlugin extends Plugin {
settings: ObsidianKinopoiskPluginSettings;
async onload() {
await this.loadSettings();
this.addRibbonIcon("film", "Search in Kinopoisk", () => {
this.createNewNote();
});
this.addCommand({
id: "open-search-kinopoisk-modal",
name: "Search",
callback: () => {
this.createNewNote();
},
});
this.addSettingTab(new ObsidianKinopoiskSettingTab(this.app, this));
}
showNotice(error: Error) {
try {
new Notice(error.message);
} catch {
// eslint-disable
}
}
async createNewNote(): Promise<void> {
try {
const movieShow = await this.searchMovieShow();
const {
movieFileNameFormat,
movieFolder,
seriesFileNameFormat,
seriesFolder,
} = this.settings;
const renderedContents = await this.getRenderedContents(movieShow);
const fileNameFormat = movieShow.isSeries
? seriesFileNameFormat
: movieFileNameFormat;
const folderPath = movieShow.isSeries ? seriesFolder : movieFolder;
const fileName = makeFileName(movieShow, fileNameFormat);
const filePath = `${folderPath}/${fileName}`;
const targetFile = await this.app.vault.create(
filePath,
renderedContents
);
const newLeaf = this.app.workspace.getLeaf(true);
if (!newLeaf) {
console.warn("No new leaf");
return;
}
await newLeaf.openFile(targetFile, { state: { mode: "source" } });
newLeaf.setEphemeralState({ rename: "all" });
// cursor focus
await new CursorJumper(this.app).jumpToNextCursorLocation();
} catch (err) {
console.warn(err);
this.showNotice(err);
}
}
async searchMovieShow(): Promise<MoviewShow> {
const searchedItems = await this.openSearchModal();
return await this.openSuggestModal(searchedItems);
}
async openSearchModal(): Promise<KinopoiskSuggestItem[]> {
return new Promise((resolve, reject) => {
return new SearchModal(this, (error, results) => {
return error ? reject(error) : resolve(results ?? []);
}).open();
});
}
async openSuggestModal(items: KinopoiskSuggestItem[]): Promise<MoviewShow> {
return new Promise((resolve, reject) => {
return new ItemsSuggestModal(this, items, (error, selectedItem) => {
return error ? reject(error) : resolve(selectedItem!);
}).open();
});
}
async getRenderedContents(movieShow: MoviewShow) {
const { movieTemplateFile, seriesTemplateFile } = this.settings;
const templateFile = movieShow.isSeries
? seriesTemplateFile
: movieTemplateFile;
if (templateFile) {
const templateContents = await getTemplateContents(
this.app,
templateFile
);
const replacedVariable = replaceVariableSyntax(
movieShow,
templateContents
);
return replacedVariable;
}
return "";
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}