Skip to content

Commit

Permalink
Eliminate global app and stop using node path
Browse files Browse the repository at this point in the history
  • Loading branch information
Acylation committed Apr 4, 2024
1 parent a4e00b0 commit 3c2c699
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 197 deletions.
21 changes: 14 additions & 7 deletions src/SettingTab.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import ClickClackPlugin, { Sounds } from './main';
import { PluginSettingTab, Setting, App, DropdownComponent } from 'obsidian';
import { getScheme, getInstalledSchemes, loadScheme } from './schemeHelpers';
import { defaultScheme } from './defaultSound';
import { checkOrDownload } from './fetchHelpers';
import { i18n } from './libs/i18n';
import { FetchHelper } from './fetchHelper';

export class ClickClackSettingTab extends PluginSettingTab {
plugin: ClickClackPlugin;
fetchHelper: FetchHelper;

constructor(app: App, plugin: ClickClackPlugin) {
super(app, plugin);
this.plugin = plugin;
this.fetchHelper = new FetchHelper(app);
}

async display(): Promise<void> {
Expand Down Expand Up @@ -61,20 +62,26 @@ export class ClickClackSettingTab extends PluginSettingTab {
.addOptions({
default: i18n.t('settings.scheme.default'),
})
.addOptions(await getInstalledSchemes())
.addOptions(
await this.plugin.schemeHelper.getInstalledSchemes()
)
.setValue(this.plugin.settings.activeScheme.id);
});
});

const dropdown = new DropdownComponent(schemeSetting.controlEl);
dropdown
.addOptions({ default: i18n.t('settings.scheme.default') })
.addOptions(await getInstalledSchemes())
.addOptions(await this.plugin.schemeHelper.getInstalledSchemes())
.setValue(this.plugin.settings.activeScheme.id)
.onChange(async (value) => {
const scheme = (await getScheme(value)) ?? defaultScheme;
const scheme =
(await this.plugin.schemeHelper.getScheme(value)) ??
defaultScheme;
this.plugin.settings.activeScheme = scheme;
this.plugin.sounds = await loadScheme(scheme);
this.plugin.sounds = await this.plugin.schemeHelper.loadScheme(
scheme
);
await this.plugin.saveSettings();
});

Expand All @@ -87,7 +94,7 @@ export class ClickClackSettingTab extends PluginSettingTab {
.setButtonText(i18n.t('settings.download.button'))
.setIcon('download')
.onClick(async () => {
await checkOrDownload();
await this.fetchHelper.checkOrDownload();
})
);
}
Expand Down
122 changes: 122 additions & 0 deletions src/fetchHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { normalizePath, requestUrl, Notice, App } from 'obsidian';
import { githubAsset } from 'typings/githubAsset';
import * as zip from '@zip.js/zip.js';

export class FetchHelper {
app: App;

constructor(app: App) {
this.app = app;
}

unzip = async (zipFile: string) => {
const blob = new Blob(
[await this.app.vault.adapter.readBinary(zipFile)],
{
type: 'octet/stream',
}
);
const entries = await new zip.ZipReader(
new zip.BlobReader(blob)
).getEntries();

const basePath = normalizePath(
[
this.app.vault.configDir,
'plugins',
'click-clack',
'resources',
].join('/')
);
this.checkOrCreateFolder(basePath);

const folders = entries.filter((e) => e.directory);
folders.forEach(async (e) =>
this.checkOrCreateFolder([basePath, e.filename].join('/'))
);
const files = entries.filter((e) => !e.directory);
files.forEach(async (e) => {
if (
await this.app.vault.adapter.exists(
[basePath, e.filename].join('/')
)
)
return;
if (!e || !e.getData) return;
await this.app.vault.adapter.writeBinary(
[basePath, e.filename].join('/'),
await (
await e.getData(new zip.BlobWriter('audio/wav'), {})
).arrayBuffer()
);
});
};

checkOrCreateFolder = async (name: string) => {
if (await this.app.vault.adapter.exists(name)) return;
await this.app.vault.adapter.mkdir(name);
};

checkOrDownload = async () => {
const resourcePath = normalizePath(
[
this.app.vault.configDir,
'plugins',
'click-clack',
'resources',
].join('/')
);
const zipPath = normalizePath(
[
this.app.vault.configDir,
'plugins',
'click-clack',
'resources.zip',
].join('/')
);

this.checkOrCreateFolder(resourcePath);
if (await this.app.vault.adapter.exists(zipPath)) {
this.unzip(zipPath);
} else {
await this.download('resources.zip', zipPath);
this.unzip(zipPath);
}
};

download = async (name: string, localPath: string) => {
new Notice(`Click Clack: Downloading ${name}!`, 5000);
try {
await this.fetchAsset(name, localPath);
new Notice(
`Click Clack: Resource ${name} successfully downloaded! ✔️`,
5000
);
} catch (error) {
new Notice(`Click Clack: Failed to fetch ${name}: ${error} ❌`);
throw Error(
`Failed to fetch resource ${name} from GitHub release.`
);
}
};

fetchAsset = async (target: string, localPath: string) => {
const assetInfo = await requestUrl(
`https://api.github.com/repos/acylation/obsidian-click-clack/releases/tags/${
this.app.plugins.getPlugin('click-clack')?.manifest.version ??
'0.1.0'
}`
).json;
const asset = assetInfo.assets.find(
(a: githubAsset) => a.name == target
);
if (asset === undefined)
throw Error('Could not find the online asset!');

const data = await requestUrl({
url: asset.url,
headers: { Accept: 'application/octet-stream' },
}).arrayBuffer;
await this.app.vault.adapter.writeBinary(localPath, data);
};
}
92 changes: 0 additions & 92 deletions src/fetchHelpers.ts

This file was deleted.

12 changes: 9 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Plugin, App, PluginManifest } from 'obsidian';
import { DEFAULT_MAP, keySoundMap } from './keySoundMap';
import { ClickClackSettings, DEFAULT_SETTINGS_V1 } from './settings';
import { ClickClackSettingTab } from './SettingTab';
import { loadScheme } from './schemeHelpers';

export interface Sounds {
key: Howl;
Expand All @@ -13,20 +12,25 @@ export interface Sounds {
delete: Howl;
}
import { defaultSounds } from './defaultSound';
import { SchemeHelper } from './schemeHelper';

export default class ClickClackPlugin extends Plugin {
settings: ClickClackSettings;
sounds: Sounds;
schemeHelper: SchemeHelper;

constructor(app: App, manifest: PluginManifest) {
super(app, manifest);
this.sounds = defaultSounds;
this.schemeHelper = new SchemeHelper(app);
}

async onload() {
await this.loadSettings();
this.addSettingTab(new ClickClackSettingTab(this.app, this));
this.sounds = await loadScheme(this.settings.activeScheme);
this.sounds = await this.schemeHelper.loadScheme(
this.settings.activeScheme
);
this.registerDomEvent(
this.app.workspace.containerEl,
'keydown',
Expand Down Expand Up @@ -85,7 +89,9 @@ export default class ClickClackPlugin extends Plugin {
}

async refreshSounds() {
this.sounds = await loadScheme(this.settings.activeScheme);
this.sounds = await this.schemeHelper.loadScheme(
this.settings.activeScheme
);
}

stopSounds() {
Expand Down
Loading

0 comments on commit 3c2c699

Please sign in to comment.