-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
166 lines (140 loc) · 3.82 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
import { App, Editor, Notice, Plugin } from "obsidian";
import type {
MarkdownFileInfo,
MarkdownView,
PluginManifest,
TFile,
} from "obsidian";
import { api, type Api } from "./Api/Api";
import { HackMdSettingsTab } from "HackMdSettingsTab";
import { HackMdPaneItemView } from "HackMdPaneItemView";
import store from "store";
export const VIEW_ID = "hackmd-view";
export const VIEW_ICON = "messages-square";
const HACKMD_PROP_NAME = "hackMdPublishLink";
interface MyPluginSettings {
API_KEY: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
API_KEY: "",
};
// THE HACK MD PLUGIN
export default class HackMdPlugin extends Plugin {
constructor(app: App, manifest: PluginManifest) {
super(app, manifest);
}
settings: MyPluginSettings = {
API_KEY: "",
};
getHackMdPublishLink(file: TFile): string {
const fm = this.app.metadataCache.getFileCache(file)?.frontmatter;
if (fm && fm[HACKMD_PROP_NAME]) {
return fm[HACKMD_PROP_NAME];
}
return "";
}
async setHackMdPublishLink(file: TFile, link: string): Promise<void> {
await this.app.fileManager.processFrontMatter(
file,
(fm) => (fm[HACKMD_PROP_NAME] = link)
);
}
async onload() {
// Setup API client from access token in settings
const hackMdApi = await this.loadApiWithSettings();
// Register the view
this.registerAndActivePane(hackMdApi);
store.plugin.set(store.init(hackMdApi));
const file = this.app.workspace.getActiveFile();
if (file) {
const activeFilePublishUrl = this.getHackMdPublishLink(file);
store.plugin.update((state) => ({
...state,
activeFilePublishUrl,
}));
}
this.app.workspace.on("active-leaf-change", (leaf) => {
if (leaf?.view.getViewType() != "markdown") return;
const file = this.app.workspace.activeEditor?.file;
let fm = undefined;
if (file) {
fm = this.app.metadataCache.getFileCache(file)?.frontmatter;
}
if (fm && fm[HACKMD_PROP_NAME]) {
const activeFilePublishUrl = fm[HACKMD_PROP_NAME];
if (activeFilePublishUrl) {
store.plugin.update((state) => ({
...state,
activeFilePublishUrl,
}));
}
}
});
// Register commands
this.addCommand({
id: `hackmd-publish`,
name: "Publish",
editorCallback: async (
editor: Editor,
ctx: MarkdownView | MarkdownFileInfo
) => {
// Guards
if (!ctx.file) return;
// Check if note has already been published
const link = this.getHackMdPublishLink(ctx.file);
if (link) {
navigator.clipboard.writeText(link);
new Notice(
"Already published, link copied from file properties."
);
return;
}
const content = editor.getDoc().getValue();
const note = await hackMdApi.createNote(content);
if (note) {
this.setHackMdPublishLink(ctx.file, note.publishLink);
await navigator.clipboard.writeText(note.publishLink);
new Notice(note.publishLink);
}
},
});
this.addSettingTab(new HackMdSettingsTab(this.app, this));
}
async activateView() {
this.app.workspace.detachLeavesOfType(VIEW_ID);
await this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_ID,
active: true,
});
this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(VIEW_ID)[0]
);
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
await this.loadSettings();
const hackMdApi = api(this.settings.API_KEY);
store.plugin.update((state) => ({ ...state, hackMdApi }));
}
async loadApiWithSettings() {
await this.loadSettings();
this.saveSettings();
return api(this.settings.API_KEY);
}
registerAndActivePane(hackMdApi: Api) {
this.registerView(
VIEW_ID,
(leaf) => new HackMdPaneItemView(leaf, hackMdApi)
);
this.addRibbonIcon(VIEW_ICON, "HackMD", () => {
this.activateView();
});
}
}