mirror of
https://github.com/nick-de-bruin/obsidian-extended-file-support.git
synced 2026-07-22 05:37:54 +00:00
158 lines
4.6 KiB
TypeScript
158 lines
4.6 KiB
TypeScript
import { App, Plugin, PluginSettingTab, Setting } from 'obsidian';
|
|
import { EXTENSION_REGISTRY } from 'src/extensionsRegistry';
|
|
import { DEFAULT_SETTINGS, ExtendedFileSupportSettings } from 'src/settings';
|
|
import { EmbedRegistry } from 'obsidian-typings';
|
|
|
|
export default class ExtendedFileSupport extends Plugin {
|
|
settings: ExtendedFileSupportSettings;
|
|
|
|
async onload() {
|
|
await this.loadSettings();
|
|
this.addSettingTab(new ExtendedFileSupportSettingTab(this.app, this));
|
|
|
|
const embedRegistry = this.app.embedRegistry as EmbedRegistry;
|
|
|
|
for (const extension of EXTENSION_REGISTRY) {
|
|
this.registerView(extension.view_type, (leaf) => new extension.view(leaf, this));
|
|
|
|
for (const extension_type of extension.types) {
|
|
// @ts-ignore
|
|
if (this.settings[extension_type]) {
|
|
this.registerExtensions([extension_type], extension.view_type);
|
|
|
|
embedRegistry.registerExtension(extension_type, (context, file, p) => {
|
|
return new extension.component(context.containerEl, this, file, context.linktext);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
onunload() {
|
|
const embedRegistry = this.app.embedRegistry as EmbedRegistry;
|
|
|
|
for (const extension of EXTENSION_REGISTRY) {
|
|
for (const extension_type of extension.types) {
|
|
// @ts-ignore
|
|
if (this.settings[extension_type]) {
|
|
this.app.viewRegistry.unregisterExtensions([extension_type]);
|
|
embedRegistry.unregisterExtension(extension_type);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async loadSettings() {
|
|
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
|
}
|
|
|
|
async saveSettings() {
|
|
await this.saveData(this.settings);
|
|
}
|
|
|
|
public toggleExtension(extension: string, enable: boolean): void {
|
|
const e = EXTENSION_REGISTRY.find(e => e.types.contains(extension));
|
|
const embedRegistry = this.app.embedRegistry as EmbedRegistry;
|
|
|
|
if (!e) return;
|
|
|
|
if (enable) {
|
|
this.registerExtensions(e.types, e.view_type);
|
|
|
|
embedRegistry.registerExtension(extension, (context, file, _) => {
|
|
return new e.component(context.containerEl, this, file, context.linktext);
|
|
});
|
|
} else {
|
|
this.app.viewRegistry.unregisterExtensions([extension]);
|
|
embedRegistry.unregisterExtension(extension);
|
|
}
|
|
}
|
|
}
|
|
|
|
class ExtendedFileSupportSettingTab extends PluginSettingTab {
|
|
plugin: ExtendedFileSupport;
|
|
|
|
constructor(app: App, plugin: ExtendedFileSupport) {
|
|
super(app, plugin);
|
|
this.plugin = plugin;
|
|
}
|
|
|
|
display(): void {
|
|
const {containerEl} = this;
|
|
|
|
containerEl.empty();
|
|
|
|
new Setting(containerEl)
|
|
.setName("Image formats")
|
|
.setHeading();
|
|
|
|
new Setting(containerEl)
|
|
.setName(".psd")
|
|
.setDesc("Photoshop documents. Generated by Adobe Photoshop or similar programs.")
|
|
.addToggle(toggle => toggle
|
|
.setValue(this.plugin.settings.psd)
|
|
.onChange(async (value) => {
|
|
this.plugin.settings.psd = value;
|
|
await this.plugin.saveSettings();
|
|
this.plugin.toggleExtension("psd", value);
|
|
}));
|
|
|
|
new Setting(containerEl)
|
|
.setName(".kra")
|
|
.setDesc("Files generated by Krita.")
|
|
.addToggle(toggle => toggle
|
|
.setValue(this.plugin.settings.kra)
|
|
.onChange(async (value) => {
|
|
this.plugin.settings.kra = value;
|
|
await this.plugin.saveSettings();
|
|
this.plugin.toggleExtension("kra", value);
|
|
}));
|
|
|
|
new Setting(containerEl)
|
|
.setName("3D formats")
|
|
.setHeading();
|
|
|
|
new Setting(containerEl)
|
|
.setName("Animate")
|
|
.setDesc("Animate 3d objects (Rotate the camera around them).")
|
|
.addToggle(toggle => toggle
|
|
.setValue(this.plugin.settings.animate_3d_objects)
|
|
.onChange(async (value) => {
|
|
this.plugin.settings.animate_3d_objects = value;
|
|
await this.plugin.saveSettings();
|
|
}));
|
|
|
|
new Setting(containerEl)
|
|
.setName(".obj")
|
|
.setDesc("Object file type for 3d models.")
|
|
.addToggle(toggle => toggle
|
|
.setValue(this.plugin.settings.obj)
|
|
.onChange(async (value) => {
|
|
this.plugin.settings.obj = value;
|
|
await this.plugin.saveSettings();
|
|
this.plugin.toggleExtension("obj", value);
|
|
}));
|
|
|
|
new Setting(containerEl)
|
|
.setName(".gltf")
|
|
.setDesc("glTF format. Only embedded glTF files are supported.")
|
|
.addToggle(toggle => toggle
|
|
.setValue(this.plugin.settings.gltf)
|
|
.onChange(async (value) => {
|
|
this.plugin.settings.gltf = value;
|
|
await this.plugin.saveSettings();
|
|
this.plugin.toggleExtension("gltf", value);
|
|
}));
|
|
|
|
new Setting(containerEl)
|
|
.setName(".glb")
|
|
.setDesc("Binary glTF format. Only self-contained glb files are supported.")
|
|
.addToggle(toggle => toggle
|
|
.setValue(this.plugin.settings.glb)
|
|
.onChange(async (value) => {
|
|
this.plugin.settings.glb = value;
|
|
await this.plugin.saveSettings();
|
|
this.plugin.toggleExtension("glb", value);
|
|
}));
|
|
}
|
|
}
|