This repository was archived by the owner on Sep 27, 2023. It is now read-only.
generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsettings-tab.ts
More file actions
198 lines (179 loc) · 6.85 KB
/
settings-tab.ts
File metadata and controls
198 lines (179 loc) · 6.85 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
import { EmbedSource } from "embeds";
import SimpleEmbedsPlugin from "main";
import { App, Platform, PluginSettingTab, Setting } from "obsidian";
import { PluginSettings } from "settings";
export class SimpleEmbedPluginSettingTab extends PluginSettingTab {
plugin: SimpleEmbedsPlugin;
private embedSettings: { [key: string]: Setting[] } = {};
constructor(app: App, plugin: SimpleEmbedsPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
let { containerEl } = this;
containerEl.empty();
containerEl.classList.add("simple-embeds-settings");
containerEl.createEl("h3", { text: "Available Embed Sources" });
containerEl.createEl(
"p",
{
cls: "setting-item-description",
},
(el) => {
el.innerHTML =
"Disable to prevent <em>all</em> links from source ever being turned into embeds. To disable an individual link, add <code>|noembed</code> to the link text. For example, <code>[Some description|noembed](https://twitter.com/user/status/123)</code>";
},
);
// Toggles to enabled/disabled embed sources.
this.plugin.embedSources.forEach((embedSource) => {
new Setting(containerEl).setName(embedSource.name).addToggle((toggle) => {
toggle
.setValue(this.plugin.settings[embedSource.enabledKey])
.onChange(async (enabled) => {
await this.saveSettings({ [embedSource.enabledKey]: enabled });
this.toggleAdditionalSettings(embedSource, enabled);
});
});
});
// Settings for generic link previews
containerEl.createEl("h3", { text: "Generic Link Previews" });
containerEl.createEl(
"p",
{
cls: "setting-item-description",
},
(el) => {
el.innerHTML =
"Desktop only";
},
);
new Setting(containerEl)
.setName("Show generic previews for links")
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.replaceGenericLinks)
.onChange(async (enabled) => {
await this.saveSettings({ replaceGenericLinks: enabled });
});
})
.setDisabled(Platform.isMobile);
new Setting(containerEl)
.setName("Use a cache for link preview metadata")
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.useCacheForGenericLinks)
.onChange(async (enabled) => {
await this.saveSettings({ useCacheForGenericLinks: enabled });
});
})
.setDisabled(Platform.isMobile);
new Setting(containerEl)
.setName("Clear link preview metadata cache")
.addButton((button) => {
button
.setButtonText("Clear")
.onClick(async () => {
await this.app.vault.adapter.write(this.plugin.genericPreviewCacheFile, "{}");
this.plugin.genericPreviewCache = {};
await this.plugin.saveSettings({});
});
})
.setDisabled(Platform.isMobile);
// Any additional settings for embed sources.
containerEl.createEl("h3", { text: "Appearance" });
this.plugin.embedSources.forEach((embedSource) => {
if (embedSource.createAdditionalSettings) {
containerEl.createEl("details", {}, (el) => {
const fragment = new DocumentFragment();
const summary = fragment.createEl("summary");
const title = fragment.createEl("h4", {
text: embedSource.name,
});
summary.appendChild(title);
el.appendChild(summary);
const settings = embedSource.createAdditionalSettings(
el,
this.plugin.settings,
this.saveSettings.bind(this),
);
this.embedSettings[embedSource.constructor.name] = settings;
const enabled = this.plugin.settings[embedSource.enabledKey];
this.toggleAdditionalSettings(embedSource, enabled);
});
}
});
containerEl.createEl("h3", { text: "Advanced Settings" });
new Setting(containerEl)
.setName("Show Embeds in Live Preview (beta)")
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.enableInLivePreview)
.onChange(async (value) => {
await this.saveSettings({ enableInLivePreview: value });
});
});
new Setting(containerEl).setName("Center embeds").addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.centerEmbeds)
.onChange(async (value) => {
await this.saveSettings({ centerEmbeds: value });
});
});
new Setting(containerEl)
.setName("Keep links in preview")
.setDesc(
"Insert embeds above the link, instead of replacing the link in the preview.",
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.keepLinksInPreview)
.onChange(async (value) => {
await this.saveSettings({ keepLinksInPreview: value });
placement.setDisabled(!this.plugin.settings.keepLinksInPreview);
});
});
const placement = new Setting(containerEl)
.setName("Place embeds")
.setDesc(
'When "Keep links in preview" is enabled, choose whether to place the embed above or below the link.',
)
.addDropdown((dropdown) => {
dropdown
.addOptions({ above: "Above link", below: "Below link" })
.setValue(this.plugin.settings.embedPlacement)
.onChange(async (value: "above" | "below") => {
await this.saveSettings({ embedPlacement: value });
});
})
.setDisabled(!this.plugin.settings.keepLinksInPreview);
const fragment = new DocumentFragment();
const div = fragment.createEl("div");
const span = fragment.createEl("span");
span.innerHTML =
"Instead of automatically embedding all matching links, you must add <code>|embed</code> to the link text of each link you would like to turn into an embed. For example, <code>[Some description|embed](https://twitter.com/user/status/123)</code>";
div.appendChild(span);
fragment.appendChild(div);
new Setting(containerEl)
.setName("Disable automatic embeds")
.setDesc(fragment)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.disableAutomaticEmbeds)
.onChange(async (value) => {
await this.saveSettings({ disableAutomaticEmbeds: value });
});
});
}
private async saveSettings(updates: Partial<PluginSettings>) {
return this.plugin.saveSettings(updates);
}
// Disable/enable all additional settings when embed is disabled/enabled.
private toggleAdditionalSettings(embedSource: EmbedSource, enabled: boolean) {
const additionalSettings = this.embedSettings[embedSource.constructor.name];
if (additionalSettings) {
additionalSettings.forEach((setting) => {
setting.setDisabled(!enabled);
});
}
}
}