Skip to content

Commit

Permalink
feat(better functionallity): remove artifacts, keep renamed files & more
Browse files Browse the repository at this point in the history
  • Loading branch information
iOSonntag authored and iOSonntag committed Sep 24, 2023
1 parent 874083a commit 0102555
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 106 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ Badge](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fraw.githubuse
[![build](https://github.com/iOSonntag/obsidian-plugin-treefocus/actions/workflows/release.yml/badge.svg?branch=master)](https://github.com/iOSonntag/obsidian-plugin-treefocus/actions/workflows/release.yml)

### Ever wanted to (*dim*) a `File` or `Folder`?
> **For example:**
> *System* like Folder: `/_SYS/`
> (maybe for your template and attachment Folders)
*For rarely used or unimportant `Files` or `Folders`.*

**Examples:**
- `/_SYS/` (maybe for your template and attachment Folders)
- `/some_dir/not_in_use/` (storing some irrelevant data)

### Or maybe **HIGHLIGHT** some `Files` and `Folders`?

> **For example:**
> Root Folders like: `/_Backend/` or `/More/`
*For the good stuff.*
**Examples:**
- `/Main/`
- `/VeryImportantFolder_DoNotDelete_Plz/`

### Or any other imaginable item focus config for your file explorer?
### Or any other imaginable config for your file explorer items?

*🍀 🍀 🍀 Good Fortune has arrived! This plugin makes it happen! :D 🍀 🍀 🍀*

Expand Down Expand Up @@ -113,9 +117,11 @@ Please report any issues at: [TreeFocus - GitHub Repository](https://github.com/

Pull requests are **WELCOME** !

> If you have improvements or feel like you can solve a bug, please do not
> hesitate to submit a pull requests. Even if you think you might not be skilled
> enough. That is pure bullsh*t. We are all beginners - all the time :)
If you have improvements or feel like you can solve a bug, please do not
hesitate to submit a pull requests.

**Even if you think you might not be skilled
enough. That is pure bullsh*t. We are all beginners - all the time :)**

<br/>
<br/>
Expand All @@ -126,8 +132,6 @@ If you like this plugin and want to support it - submit a feature request, a
pull request or simply buy me
a little coffee :) - Thank You.

If you find this useful - feel free to buy me a coffee :)

<a href="https://www.buymeacoffee.com/iOSonntag" target="_blank"><img
src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A
Coffee" style="height: 60px !important;width: 217px !important;" ></a>
Expand Down
23 changes: 16 additions & 7 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { App, FileExplorer, Menu, PluginManifest, TAbstractFile, WorkspaceLeaf } from 'obsidian';
import { pluginMetaData } from 'src/_config/meta';
import { TreeFocus } from 'src/core/tree-focus';
import { ObsidianPlugin } from 'src/enhanced-obsidian-components/obsidian-plugin';
import { SOURCE_TYPE_FILE_EXPLORER_CONTEXT_MENU, VIEW_TYPE_FILE_EXPLORER } from 'src/enhanced-obsidian-components/known-type-keys';
Expand All @@ -11,7 +10,7 @@ export default class TreeFocusPlugin extends ObsidianPlugin {

constructor(app: App, manifest: PluginManifest)
{
super(app, manifest, pluginMetaData);
super(app, manifest);

this.treeFocus = new TreeFocus(app, this, manifest);
}
Expand All @@ -25,14 +24,14 @@ export default class TreeFocusPlugin extends ObsidianPlugin {
// Obsidian recommends wrapping every event listeners into
// this.registerEvent() - but this is an exception because it gets only
// called once.
this.app.workspace.onLayoutReady(() => this.treeFocus.requestRefresh());
this.app.workspace.onLayoutReady(() => this.treeFocus.requestInitialRefresh());

this.registerEvent(this.app.workspace.on('layout-change', () => this.onObsidianEvent('workspace.layout-change')));
this.registerEvent(this.app.workspace.on('file-menu', (menu, file, source, leaf) => this.onFileMenu(menu, file, source, leaf)));

this.registerEvent(this.app.vault.on('create', () => this.onObsidianEvent('vault.create')));
this.registerEvent(this.app.vault.on('delete', () => this.onObsidianEvent('vault.delete')));
this.registerEvent(this.app.vault.on('rename', () => this.onObsidianEvent('vault.rename')));
this.registerEvent(this.app.vault.on('rename', (file, oldPath) => this.onFileEventRename(file, oldPath)));
this.registerEvent(this.app.vault.on('delete', (file) => this.onFileEventDelete(file)));

this.addSettingTab(this.treeFocus.getSettingsView());

Expand All @@ -43,14 +42,24 @@ export default class TreeFocusPlugin extends ObsidianPlugin {
getFileExplorers(): FileExplorer[]
{
let list = this.app.workspace.getLeavesOfType(VIEW_TYPE_FILE_EXPLORER);

return list.map((leaf) => leaf.view as FileExplorer);
}

onFileEventRename(file: TAbstractFile, oldPath: string): void
{
Log.eventFired('vault.rename', 'file', file, 'oldPath', oldPath);
this.treeFocus.onFileMoved(file.path, oldPath);
}

onFileEventDelete(file: TAbstractFile): void
{
Log.eventFired('vault.delete', 'file', file);
this.treeFocus.onFileDeleted(file.path);
}

onObsidianEvent(name: string): void
{
Log.eventFired(name);

this.treeFocus.requestRefresh();
}

Expand Down
5 changes: 2 additions & 3 deletions src/_config/initial-settings.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { SettingsObject } from 'src/core/plugin-settings';
import { Rule } from 'src/types/rules';
import { MatchingRule } from 'src/types/matching-rules';

export const DEFAULT_RULES = (): Rule[] => [
export const DEFAULT_RULES = (): MatchingRule[] => [
{

matcher: {
method: 'STARTS_WITH',
value: '_',
Expand Down
2 changes: 1 addition & 1 deletion src/core/plugin.ts → src/core/plugin-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { App as ObsidianApp, Plugin as ObsidianPlugin, PluginManifest as Obsidia
import { ErrorHelper } from 'src/util/error-helper';


export abstract class P {
export abstract class Bundle {

private static _app?: WeakRef<ObsidianApp>;
private static _plugin?: WeakRef<ObsidianPlugin>;
Expand Down
40 changes: 16 additions & 24 deletions src/core/plugin-info.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { App, PluginManifest, apiVersion } from 'obsidian';
import { ErrorHelper } from '../util/error-helper';
import { PluginMeta } from 'src/types/plugin';



type HttpsUrl = `https://${string}`;
type RepositoryType = 'git' | 'hg' | 'svn';
type RepositoryProvider = 'GitHub' | 'GitLab' | 'Bitbucket' | 'Gitea' | 'Gogs' | 'Custom';
interface TreeFocusPluginManifest extends PluginManifest {
repository?: {
type?: RepositoryType;
provider?: RepositoryProvider;
providerUrl?: HttpsUrl;
repoUrl?: HttpsUrl;
issuesUrl?: HttpsUrl;
}
}
export abstract class PluginInfo {

private static _obsidianVersion: string;
private static _manifest: PluginManifest;
private static _meta: PluginMeta;
private static _manifest: TreeFocusPluginManifest;

static get obsidianVersion()
{
Expand All @@ -20,7 +28,7 @@ export abstract class PluginInfo {
return this._obsidianVersion;
};

static get manifest(): PluginManifest
static get manifest(): TreeFocusPluginManifest
{
if (!this._manifest)
{
Expand All @@ -30,16 +38,6 @@ export abstract class PluginInfo {
return this._manifest;
}

static get meta(): PluginMeta
{
if (!this._meta)
{
throw ErrorHelper.pluginBug('Trying to access meta before it was set.');
}

return this._meta;
}

static get humanIdentifier(): string
{
return `${this.manifest.name} (${this.manifest.id} @ v${this.manifest.version})`;
Expand All @@ -55,20 +53,14 @@ export abstract class PluginInfo {
return this.manifest.name;
}

static get debug(): boolean
{
return this.meta.debugMode;
}

static _internalInit(app: App, manifest: PluginManifest, meta: PluginMeta)
static _internalInit(app: App, manifest: PluginManifest)
{
if (this._manifest || this._obsidianVersion || this._meta)
if (this._manifest || this._obsidianVersion)
{
throw ErrorHelper.pluginBug('Trying to set plugin info twice.');
}

this._obsidianVersion = apiVersion;
this._manifest = manifest;
this._meta = meta;
}
}
52 changes: 48 additions & 4 deletions src/core/plugin-settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { PluginDataStore } from 'src/services/plugin-data-store';
import { RulesHelper } from 'src/util/rules-helper';
import { FilesFocusModeMap, Rule } from 'src/types/rules';
import { FilesFocusModeMap, MatchingRule } from 'src/types/matching-rules';
import { ItemFocusMode } from 'src/types/general';
import { DEFAULT_RULES } from 'src/_config/initial-settings';

Expand All @@ -10,7 +10,7 @@ export type TransformPresetKey = 'DEFAULT' | 'FANCY' | 'DELIGHT';

export type SettingsObject = {
transformPreset: TransformPresetKey;
rules: Rule[];
rules: MatchingRule[];
fileOverwrites: FilesFocusModeMap;
};

Expand All @@ -21,28 +21,69 @@ export type SettingsObjectKey = keyof SettingsObject;

export abstract class PluginSettings {


/**
* Returns the entire settings object.
**/
static getAll(): SettingsObject
{
return PluginDataStore.getAll() as SettingsObject;
}

/**
* Returns a setting value by key.
*/
static get<T extends SettingsObjectKey>(key: T): SettingsObject[T]
{
return PluginDataStore.get(key) as SettingsObject[T];
}

/**
* Sets a setting value by key.
*/
static async set<T extends SettingsObjectKey>(key: T, value: SettingsObject[T]): Promise<void>
{
await PluginDataStore.set(key, value);
}

static async resetRules(): Promise<void>
/**
* Returns the configured matching rules, sorted by priority.
*/
static getMatchingRules(): MatchingRule[]
{
return PluginSettings.get('rules');
}

/**
* Resets the matching rules to the default rules.
*/
static async resetMatchingRules(): Promise<void>
{
await PluginSettings.set('rules', DEFAULT_RULES());
}

/**
* Returns the explicit {@link ItemFocusMode} set for the given file path if any.
*/
static getExplicitMode(path: string): ItemFocusMode | undefined
{
const files = PluginSettings.get('fileOverwrites');

return files[path];
}

/**
* Returns all file paths that have an explicit {@link ItemFocusMode} set.
*/
static getExplicitModePaths(): string[]
{
const files = PluginSettings.get('fileOverwrites');

return Object.keys(files);
}

/**
* Sets the explicit {@link ItemFocusMode} for the given file path.
*/
static async setExplicitMode(path: string, mode: ItemFocusMode): Promise<void>
{
const files = PluginSettings.get('fileOverwrites');
Expand All @@ -52,6 +93,9 @@ export abstract class PluginSettings {
await PluginSettings.set('fileOverwrites', files);
}

/**
* Removes the explicit {@link ItemFocusMode} for the given file path.
*/
static async removeExplicitMode(path: string): Promise<void>
{
const files = PluginSettings.get('fileOverwrites');
Expand Down
Loading

0 comments on commit 0102555

Please sign in to comment.