-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
117 lines (106 loc) · 2.87 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
import {Editor, MarkdownView, Menu, Notice, Plugin, TAbstractFile, TFile} from 'obsidian'
import {PicklyPluginSettings, PicklyItem} from './src/types'
import {Publisher} from "./src/publisher"
import {PublishSuccessModal} from "./src/popups/published";
import {PublishingModal} from "./src/popups/piblushing";
export default class PicklyPageBlendPlugin extends Plugin {
private settings: PicklyPluginSettings
private publisher: Publisher
async onload() {
await this.loadSettings()
this.publisher = new Publisher(this.app, this.settings)
this.registerEvent(
this.app.workspace.on("file-menu", (menu: Menu, file: TAbstractFile) => {
this.addItem(menu, file)
})
)
this.registerEvent(
this.app.workspace.on("editor-menu", (menu: Menu, editor: Editor, view: MarkdownView) => {
this.addItem(menu, view.file)
})
)
this.addCommand({
id: "pickly-page-blend",
name: "Publish with PageBlend",
hotkeys: [
{
modifiers: ['Mod', 'Shift'],
key: 'p',
}
],
callback: async () => {
const file = this.app.workspace.activeEditor?.file
if (file) {
await this.publish(file)
} else {
new Notice("There is no file for publish")
}
},
})
this.registerEvent(this.app.vault.on('delete', file => {
this.settings.publishedFiles.delete(file.path)
this.saveSettings()
}))
this.registerEvent(this.app.vault.on('rename', (file, oldPath) => {
const oldItem = this.settings.publishedFiles.get(oldPath)
if (oldItem) {
this.settings.publishedFiles.set(file.path, {
id: oldItem.id
})
}
this.saveSettings()
}))
}
addItem(menu: Menu, file: TAbstractFile | null) {
if (file !== null && file instanceof TFile) {
menu.addItem((item) => {
item
.setTitle("Publish with PageBlend")
.setIcon('publish')
.onClick(async () => {
await this.publish(file)
})
})
}
}
async publish(file: TFile) {
const modal = new PublishingModal(this.app)
modal.open()
try {
const resp = await this.publisher.publish(
file,
item => modal.setProgress(item),
)
new PublishSuccessModal(this.app, resp).open()
} catch (e) {
new Notice("Something went wrong...")
console.error(e)
} finally {
modal.setCanClose(true)
modal.close()
await this.saveSettings()
}
}
async loadSettings() {
this.settings = {
publishedFiles: new Map<string, PicklyItem>()
}
const data = await this.loadData()
if (data.publishedFiles) {
Object.keys(data.publishedFiles).forEach(key => {
if (data.publishedFiles[key].id) {
this.settings.publishedFiles.set(key, {
id: data.publishedFiles[key].id
})
}
})
}
}
async saveSettings() {
const settings: { publishedFiles: { [key: string]: PicklyItem } } = {publishedFiles: {}}
this.settings.publishedFiles.forEach((val, key) => {
settings.publishedFiles[key] = val
})
await this.saveData(settings)
}
}