generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSettingTab.ts
270 lines (248 loc) · 8.49 KB
/
SettingTab.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import InsertUpsplashImagePlugin from "./main";
import { App, PluginSettingTab, Setting } from "obsidian";
import { imageSizesMapping, providerMapping } from "fetchers/constants";
export enum InsertMode {
remote = "remote",
local = "local",
}
export enum Orientation {
notSpecified = "not_specified",
landscape = "landscape",
portrait = "portrait",
squarish = "squarish",
}
export enum ImageProvider {
unsplash = "unsplash",
pixabay = "pixabay",
pexels = "pexels",
local = "local",
}
export enum ImageSize {
raw = "raw",
large = "large",
medium = "medium",
small = "small",
}
export interface PluginSettings {
insertMode: InsertMode;
orientation: Orientation;
insertSize: string;
imageSize: ImageSize;
frontmatter: {
key: string;
valueFormat: string;
appendReferral: boolean;
};
imageProvider: ImageProvider;
proxyServer: string;
pixabayApiKey: string;
insertBackLink: boolean;
pexelsApiKey: string;
useMarkdownLinks: boolean;
}
export const DEFAULT_SETTINGS = {
insertMode: InsertMode.remote,
orientation: Orientation.landscape,
insertSize: "",
imageSize: ImageSize.large,
frontmatter: {
key: "image",
valueFormat: "{image-url}",
appendReferral: false,
},
imageProvider: ImageProvider.unsplash,
proxyServer: "",
pixabayApiKey: "",
insertBackLink: false,
pexelsApiKey: "",
};
export class SettingTab extends PluginSettingTab {
plugin: InsertUpsplashImagePlugin;
constructor(app: App, plugin: InsertUpsplashImagePlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("Insert Mode")
.setDesc(
"Should the image be insert remotely (with Unsplash url) or locally (download into attachments folder).",
)
.addDropdown((dropdown) =>
dropdown
.addOption(InsertMode.remote, "Remote")
.addOption(InsertMode.local, "Local")
.setValue(this.plugin.settings.insertMode)
.onChange(async (value) => {
if (value === InsertMode.remote || value === InsertMode.local) {
this.plugin.settings.insertMode = value;
await this.plugin.saveSettings();
}
}),
);
new Setting(containerEl)
.setName("Orientation")
.setDesc("Search images with the specified orientation.")
.addDropdown((dropdown) =>
dropdown
.addOption(Orientation.notSpecified, "Not Specified")
.addOption(Orientation.landscape, "Landscape")
.addOption(Orientation.portrait, "Portrait")
.addOption(Orientation.squarish, "Squarish")
.setValue(this.plugin.settings.orientation)
.onChange(async (value) => {
if (
value === Orientation.notSpecified ||
value === Orientation.landscape ||
value === Orientation.portrait ||
value === Orientation.squarish
) {
this.plugin.settings.orientation = value;
await this.plugin.saveSettings();
}
}),
);
new Setting(containerEl)
.setName("Insert Size")
.setDesc(
'Set the size of the image when inserting. Format could be only the width "200" or the width and height "200x100".',
)
.addText((text) => {
text
.setValue(this.plugin.settings.insertSize)
.onChange(async (value) => {
this.plugin.settings.insertSize = value;
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Default Image Size")
.setDesc("Set the default preferred image size from image providers.")
.addDropdown((dropdown) => {
dropdown
.addOptions({
[ImageSize.raw]: imageSizesMapping[ImageSize.raw],
[ImageSize.large]: imageSizesMapping[ImageSize.large],
[ImageSize.medium]: imageSizesMapping[ImageSize.medium],
[ImageSize.small]: imageSizesMapping[ImageSize.small],
})
.setValue(this.plugin.settings.imageSize)
.onChange(async (value: ImageSize) => {
this.plugin.settings.imageSize = value;
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Insert backlink")
.setDesc(
"Insert a backlink (image HTML location on Provider website) in front of the reference text, eg. Backlink | Photo by ...",
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.insertBackLink)
.onChange(async (value: boolean) => {
this.plugin.settings.insertBackLink = value;
await this.plugin.saveSettings();
});
});
containerEl.createEl("h1", { text: "Frontmatter" });
new Setting(containerEl)
.setName("Insert to Frontmatter Key")
.setDesc("The key used when insert to frontmatter.")
.addText((text) => {
text
.setPlaceholder("image")
.setValue(this.plugin.settings.frontmatter.key)
.onChange(async (value) => {
this.plugin.settings.frontmatter.key = value;
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Insert to Frontmatter Value Format")
.setDesc(
"The value format used when insert to frontmatter. {image-url} will be replaced by the image url. Please visit https://github.com/cloudy9101/obsidian-image-inserter#frontmatter-value-format for more details.",
)
.addText((text) => {
text
.setPlaceholder("{image-url}")
.setValue(this.plugin.settings.frontmatter.valueFormat)
.onChange(async (value) => {
this.plugin.settings.frontmatter.valueFormat = value;
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Append image referral at end of the file.")
.setDesc("Will append image referral at end of the file if set to true")
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.frontmatter.appendReferral)
.onChange(async (value) => {
this.plugin.settings.frontmatter.appendReferral = value;
await this.plugin.saveSettings();
});
});
containerEl.createEl("h1", { text: "Image Provider" });
new Setting(containerEl)
.setName("Default Provider")
.addDropdown((dropdown) => {
dropdown
.addOptions({
[ImageProvider.unsplash]: providerMapping[ImageProvider.unsplash],
[ImageProvider.pixabay]: providerMapping[ImageProvider.pixabay],
[ImageProvider.pexels]: providerMapping[ImageProvider.pexels],
[ImageProvider.local]: providerMapping[ImageProvider.local],
})
.setValue(this.plugin.settings.imageProvider)
.onChange(async (value: ImageProvider) => {
this.plugin.settings.imageProvider = value;
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Unsplash Proxy Server")
.setDesc(
"Use a self host proxy server. Leave it empty if you don't want host proxy server by yourself.",
)
.addText((text) => {
text
.setPlaceholder("https://self-host-proxy.com/")
.setValue(this.plugin.settings.proxyServer)
.onChange(async (value) => {
this.plugin.settings.proxyServer = value;
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Pixabay API key")
.setDesc(
"API key can be found on https://pixabay.com/api/docs/ after logging in.",
)
.addText((text) => {
text
.setPlaceholder("Your API key")
.setValue(this.plugin.settings.pixabayApiKey)
.onChange(async (value) => {
this.plugin.settings.pixabayApiKey = value;
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Pexels API key")
.setDesc(
"API key can be created on https://www.pexels.com/api/new/ after logging in.",
)
.addText((text) => {
text
.setPlaceholder("Your API key")
.setValue(this.plugin.settings.pexelsApiKey)
.onChange(async (value) => {
this.plugin.settings.pexelsApiKey = value;
await this.plugin.saveSettings();
});
});
}
}