0
0
Fork 0

Primary code functionality

This commit is contained in:
Oliver Akins 2022-05-26 23:18:53 -06:00
parent 4a224bd6a8
commit 29d25dc6e7
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99
8 changed files with 282 additions and 76 deletions

View file

@ -1,112 +1,138 @@
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { ManageHiddenDirectories } from './settings/manageHiddenFolders';
import { VisibilityToggleCommand } from './commands/toggleVisibility';
import { VisibilityToggleSetting } from './settings/hiddenToggle';
import { App, Plugin, PluginSettingTab, TFolder } from 'obsidian';
import { ManageHiddenFiles } from './settings/manageHiddenFiles';
import { createStyleLine, findStyleSheet } from './utils';
interface FileHiderSettings {
ribbonIcon: boolean;
hidden: boolean;
hiddenFiles: string[];
hiddenFolders: string[];
};
const DEFAULT_SETTINGS: FileHiderSettings = {
ribbonIcon: false,
};
export default class FileHider extends Plugin {
settings: FileHiderSettings;
settings: FileHiderSettings = {
ribbonIcon: true,
hidden: true,
hiddenFiles: [],
hiddenFolders: [],
};
style: CSSStyleSheet|null = null;
hasRibbon: boolean = false;
async onload() {
await this.loadSettings();
// This creates an icon in the left ribbon.
const ribbonIconEl = this.addRibbonIcon('dice', 'Toggle Hiding', (evt: MouseEvent) => {
new Notice('This is a notice!');
});
// Perform additional things with the ribbon
ribbonIconEl.addClass('my-plugin-ribbon-class');
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
const statusBarItemEl = this.addStatusBarItem();
statusBarItemEl.setText('Status Bar Text');
// This adds a simple command that can be triggered anywhere
this.addCommand({
id: 'open-sample-modal-simple',
name: 'Open sample modal (simple)',
callback: () => {
new FilesModal(this.app).open();
}
});
// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: 'sample-editor-command',
name: 'Sample editor command',
editorCallback: (editor: Editor, view: MarkdownView) => {
console.log(editor.getSelection());
editor.replaceSelection('Sample Editor Command');
}
});
this.registerEvent(
this.app.workspace.on(`file-menu`, (menu, file) => {
if (!this.style) {
this.style = findStyleSheet();
};
if (file instanceof TFolder) {
menu.addItem((i) => {
i.setTitle(`Hide Folder`)
.setIcon(`minus-with-circle`)
.onClick(() => {
let rule = createStyleLine(`folder`, file.path);
this.style.insertRule(rule);
this.settings.hiddenFolders.push(file.path);
});
});
} else {
menu.addItem((i) => {
i.setTitle(`Hide File`)
.setIcon(`minus-with-circle`)
.onClick((e) => {
let rule = createStyleLine(`file`, file.path);
this.style.insertRule(rule);
this.settings.hiddenFiles.push(file.path);
});
});
};
})
);
new VisibilityToggleCommand(this);
this.initialLoadStyle();
this.addSettingTab(new FileHiderSettingsTab(this.app, this));
}
onunload() {
this.saveSettings();
};
}
/*
This is the method that handles re-hiding files when Obsidian starts, or
when the plugin is reloaded after being unloaded/disabled.
*/
async initialLoadStyle() {
console.log(`attempting to get the stylesheet`)
this.style = findStyleSheet();
if (this.style) {
for (var file of this.settings.hiddenFiles) {
let r = createStyleLine(`file`, file);
this.style.insertRule(r);
};
for (var folder of this.settings.hiddenFolders) {
let r = createStyleLine(`folder`, folder);
this.style.insertRule(r);
};
return
};
setTimeout(() => this.initialLoadStyle(), 1_000);
};
/*
Loads the config settings, with defaults created where needed.
*/
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
this.settings = Object.assign({}, this.settings, await this.loadData());
};
/* Saves the setting data */
async saveSettings() {
await this.saveData(this.settings);
};
// Remove/Add the ribbon icon if the user wants it
}
}
class FilesModal extends Modal {
constructor(app: App) {
super(app);
}
onOpen() {
const {contentEl} = this;
contentEl.setText('Woah!');
}
onClose() {
const {contentEl} = this;
contentEl.empty();
}
/*
Enables/Disables the file visibility based. (gets the stylesheet if needed)
*/
toggleVisibility() {
if (!this.style) {
this.style = findStyleSheet();
};
if (this.settings.hidden) {
this.style.disabled = true;
} else {
this.style.disabled = false;
};
this.settings.hidden = !this.settings.hidden;
};
};
class DirectoryModal extends Modal {
constructor(app: App) {
super(app);
}
onOpen() {
const {contentEl} = this;
contentEl.setText('Woah!');
}
onClose() {
const {contentEl} = this;
contentEl.empty();
}
}
/**
* All of the settings for the FileHider
*/
class FileHiderSettingsTab extends PluginSettingTab {
plugin: FileHider;
constructor(app: App, plugin: FileHider) {
super(app, plugin);
this.plugin = plugin;
}
};
display(): void {
const {containerEl} = this;
const { containerEl: container } = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'});
}
container.empty();
VisibilityToggleSetting.create(this.plugin, container);
ManageHiddenFiles.create(this.plugin, container);
ManageHiddenDirectories.create(this.plugin, container);
};
}