-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.ts
More file actions
241 lines (213 loc) · 6.13 KB
/
main.ts
File metadata and controls
241 lines (213 loc) · 6.13 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import {
type App,
addIcon,
Notice,
Platform,
Plugin,
type PluginManifest,
PluginSettingTab,
} from "obsidian";
import { PublishStatusModal } from "src/components/PublishStatusModal";
import { UpdateModal } from "src/components/UpdateModal";
import { flowershowIcon } from "src/constants";
import Publisher from "src/Publisher";
import SettingView from "src/SettingView";
import { DEFAULT_SETTINGS, type IFlowershowSettings } from "src/settings";
import { createSiteNotice, FlowershowError } from "src/utils";
export default class Flowershow extends Plugin {
private startupAnalytics: string[] = [];
private lastLogTimestamp: number;
private loadTimestamp: number;
private publishStatusModal: PublishStatusModal;
private statusBarItem: HTMLElement;
public settings: IFlowershowSettings;
public publisher: Publisher;
constructor(app: App, manifest: PluginManifest) {
super(app, manifest);
this.loadTimestamp = Date.now();
this.lastLogTimestamp = this.loadTimestamp;
this.startupAnalytics = [];
}
async onload() {
this.logStartupEvent("Plugin Constructor ready, starting onload()");
await this.loadSettings();
// Show update modal for users who haven't seen v4.0 changes
// this.showUpdateModalIfNeeded();
if (Platform.isDesktop) {
this.statusBarItem = this.addStatusBarItem();
this.statusBarItem.addClass("mod-clickable");
this.statusBarItem.createEl("span", { text: "💐" });
this.statusBarItem.addEventListener("click", () => {
this.openPublishStatusModal();
});
}
this.publisher = new Publisher(this.app, this.settings);
this.addSettingTab(new FlowershowSettingTab(this.app, this));
await this.addCommands();
addIcon("flowershow-icon", flowershowIcon);
this.addRibbonIcon(
"flowershow-icon",
"Publish with Flowershow",
async () => {
this.openPublishStatusModal();
}
);
}
onunload() {
if (this.statusBarItem) {
this.statusBarItem.remove();
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings(): Promise<void> {
await this.saveData(this.settings);
// Recreate publisher with updated settings
this.publisher = new Publisher(this.app, this.settings);
// Clear cached modal so it picks up new publisher
this.publishStatusModal = null!;
}
async addCommands() {
this.addCommand({
id: "publish-single-note",
name: "Publish single note (with embeds)",
checkCallback: (checking) => {
if (checking) {
const currentFile = this.app.workspace.getActiveFile();
return !!currentFile && currentFile.extension === "md";
}
this.publishSingleNote();
},
});
this.addCommand({
id: "publish-all-files",
name: "Publish all",
checkCallback: (checking: boolean) => {
if (checking) {
return true;
}
this.publishAllFiles();
},
});
}
/** Publish single note and its embeds */
// TODO make sure that embeds in frontmatter are published too!
async publishSingleNote() {
try {
const currentFile = this.app.workspace.getActiveFile();
if (!currentFile) {
new Notice("No file is open. Open a note and try again.");
return;
}
if (currentFile.extension !== "md") {
new Notice(
"This isn't a Markdown file. Open a .md note and try again."
);
return;
}
new Notice("⌛ Publishing note...");
const result = await this.publisher.publishSingleNoteWithEmbeds(
currentFile
);
const frag = createSiteNotice(
`Published ${result.filesPublished} file(s).`,
result.siteUrl
);
new Notice(frag, 8000);
} catch (e: any) {
console.error(e);
if (e instanceof FlowershowError) {
new Notice(`❌ Can't publish note: ${e.message}`);
} else {
new Notice(`❌ Can't publish note.`);
}
throw e;
}
}
// Publish new or changed files, and unpublish deleted files
async publishAllFiles() {
try {
const { changedFiles, deletedFiles, newFiles } =
await this.publisher.getPublishStatus();
// console.log({ changedFiles, deletedFiles, newFiles })
const filesToDelete = deletedFiles;
const filesToPublish = changedFiles.concat(newFiles);
if (!filesToDelete.length && !filesToPublish.length) {
new Notice("❌ Nothing new to publish or delete.");
return;
}
const result = await this.publisher.publishBatch({
filesToPublish,
filesToDelete,
});
const frag = createSiteNotice(
`Published ${result.filesPublished} file(s).`,
result.siteUrl
);
new Notice(frag, 8000);
} catch (e: any) {
console.error(e);
if (e instanceof FlowershowError) {
new Notice(`❌ Can't publish notes: ${e.message}`);
} else {
new Notice(
"❌ Can't publish notes. Check console errors for more info."
);
}
}
}
openPublishStatusModal() {
if (!this.publishStatusModal) {
this.publishStatusModal = new PublishStatusModal({
app: this.app,
publisher: this.publisher,
settings: this.settings,
});
}
this.publishStatusModal.open();
}
public logStartupEvent(message: string) {
const timestamp = Date.now();
this.startupAnalytics.push(
`${message}\nTotal: ${timestamp - this.loadTimestamp}ms Delta: ${
timestamp - this.lastLogTimestamp
}ms\n`
);
this.lastLogTimestamp = timestamp;
}
// private showUpdateModalIfNeeded() {
// // Show modal if user hasn't seen any version yet (new user or upgrading from pre-4.0)
// if (
// !this.settings.lastSeenVersion ||
// this.settings.lastSeenVersion < "4.0.0"
// ) {
// const modal = new UpdateModal(this.app, async () => {
// // Save current version after modal is closed
// this.settings.lastSeenVersion = this.manifest.version;
// await this.saveSettings();
// });
// modal.open();
// }
// }
}
class FlowershowSettingTab extends PluginSettingTab {
plugin: Flowershow;
constructor(app: App, plugin: Flowershow) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
const settingView = new SettingView(
containerEl,
this.plugin.publisher,
this.plugin.settings,
async () => {
await this.plugin.saveSettings();
}
);
settingView.initialize();
}
}