-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.ts
203 lines (161 loc) · 5.95 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
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
import { App, Notice, Plugin, PluginSettingTab, addIcon, Modal, TFile } from 'obsidian';
import { FlowershowSettings, DEFAULT_SETTINGS } from 'src/FlowershowSettings';
import Publisher, { IPublisher } from 'src/Publisher';
import PublishStatusBar from 'src/PublishStatusBar';
import PublishStatusManager from 'src/PublishStatusManager';
import PublishStatusModal from 'src/PublishStatusModal';
import SettingView from 'src/SettingView';
import SiteManager, { ISiteManager } from 'src/SiteManager';
import { seedling } from 'src/constants';
export default class Flowershow extends Plugin {
appVersion: string;
settings: FlowershowSettings;
publishStatusModal: PublishStatusModal;
publisher: IPublisher;
siteManager: ISiteManager;
publishStatusManager: PublishStatusManager;
async onload() {
this.appVersion = this.manifest.version;
console.log("Initializing Flowershow plugin v" + this.appVersion);
await this.loadSettings();
this.publisher = new Publisher(this.app.vault, this.app.metadataCache, this.settings);
this.siteManager = new SiteManager(this.app.metadataCache, this.settings);
this.publishStatusManager = new PublishStatusManager(this.siteManager, this.publisher);
this.addSettingTab(new FlowershowSettingTab(this.app, this));
await this.addCommands();
addIcon('flowershow-icon', seedling);
this.addRibbonIcon("flowershow-icon", "Publish with Flowershow", async () => {
this.openPublishStatusModal();
});
}
// onunload() {
// }
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings(): Promise<void> {
await this.saveData(this.settings);
}
async addCommands() {
this.addCommand({
id: 'publish-note',
name: 'Publish single note',
checkCallback: (checking: boolean) => {
if (checking) {
return !!this.app.workspace.getActiveFile();
}
this.publishSingleNote();
}
});
this.addCommand({
id: 'publish-all-notes',
name: 'Publish all notes',
checkCallback: (checking: boolean) => {
if (checking) {
return true
}
this.publishAllNotes();
},
});
}
async publishSingleNote() {
try {
const currentFile = this.app.workspace.getActiveFile();
// if (!currentFile) {
// new Notice("No file is open/active. Please open a file and try again.")
// return;
// }
if (currentFile.extension !== 'md') {
new Notice("The current file is not a markdown file. Please open a markdown file and try again.")
return;
}
new Notice("Publishing note...");
await this.publisher.publishNote(currentFile);
new Notice(`✅ Successfully published note to your Flowershow site.`);
} catch (e) {
console.error(e)
new Notice("❌ Unable to publish note, something went wrong.")
}
}
async publishAllNotes() {
const statusBarItem = this.addStatusBarItem();
try {
new Notice('Processing files to publish...');
const { unpublishedNotes, changedNotes, deletedNotePaths } = await this.publishStatusManager.getPublishStatus();
const notesToPublish: TFile[] = changedNotes.concat(unpublishedNotes);
const notesToDelete: string[] = deletedNotePaths;
// TODO what about images to publish?
const filesRequiringActionCount = notesToPublish.length + notesToDelete.length;
const statusBar = new PublishStatusBar(statusBarItem, filesRequiringActionCount);
new Notice(`Publishing ${notesToPublish.length} notes and deleting ${notesToDelete.length} notes.`, 8000);
for (const file of notesToPublish) {
try {
statusBar.increment();
await this.publisher.publishNote(file);
} catch {
new Notice(`Unable to publish note ${file.path}, skipping it.`)
}
}
for (const path of notesToDelete) {
try {
// statusBar.increment();
await this.publisher.unpublishNote(path);
} catch {
new Notice(`Unable to delete note ${path}, skipping it.`)
}
}
statusBar.finish(8000);
} catch (e) {
statusBarItem.remove();
console.error(e)
new Notice("Unable to publish multiple notes, something went wrong.")
}
}
// async copyNoteUrlToClipboard() {
// try {
// const currentFile = this.app.workspace.getActiveFile();
// if (!currentFile) {
// new Notice("No file is open/active. Please open a file and try again.")
// return;
// }
// const fullUrl = this.siteManager.getNoteUrl(currentFile);
// await navigator.clipboard.writeText(fullUrl);
// new Notice(`Note URL copied to clipboard`);
// } catch (e) {
// console.log(e)
// new Notice("Unable to copy note URL to clipboard, something went wrong.")
// }
// }
// async addPublishFlag() {
// const engine = new ObsidianFrontMatterEngine(this.app.vault, this.app.metadataCache, this.app.workspace.getActiveFile());
// engine.set("dgpublish", true).apply();
// }
openPublishStatusModal() {
if (!this.publishStatusModal) {
const siteManager = new SiteManager(this.app.metadataCache, this.settings);
const publisher = new Publisher(this.app.vault, this.app.metadataCache, this.settings);
const publishStatusManager = new PublishStatusManager(siteManager, publisher);
this.publishStatusModal = new PublishStatusModal(this.app, publishStatusManager, publisher, this.settings);
}
this.publishStatusModal.open();
}
}
class FlowershowSettingTab extends PluginSettingTab {
plugin: Flowershow;
constructor(app: App, plugin: Flowershow) {
super(app, plugin);
this.plugin = plugin;
// if (!this.plugin.settings.noteSettingsIsInitialized) {
// const siteManager = new SiteManager(this.app.metadataCache, this.plugin.settings);
// siteManager.updateEnv();
// this.plugin.settings.noteSettingsIsInitialized = true;
// this.plugin.saveData(this.plugin.settings);
// }
}
async display(): Promise<void> {
const { containerEl } = this;
const settingView = new SettingView(this.app, containerEl, this.plugin.settings, async () => await this.plugin.saveData(this.plugin.settings));
const prModal = new Modal(this.app)
await settingView.initialize(prModal);
}
}