Skip to content

Commit

Permalink
fix: addResource -> addResources, removeResource -> removeResources, …
Browse files Browse the repository at this point in the history
…addResources now returns a deregister function
  • Loading branch information
valentine195 committed Jan 20, 2023
1 parent 0a2e0cf commit 1afdb45
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ interface Resource {
desc: string;
}

SettingsSearch.addResource(resource: Resource);
SettingsSearch.removeResource(resource: Resource);
/**
* Add an arbitrary number of resources to the settings search.
* Returns a function that can be used to remove the registered resources.
*/
SettingsSearch.addResources(...resources: Resource[]);

/**
* Remove an arbitrary number of resources from the settings search.
*/
SettingsSearch.removeResources(...resources: Resource[]);

```
68 changes: 41 additions & 27 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ declare module "obsidian" {
declare global {
interface Window {
SettingsSearch?: {
addResource: SettingsSearch["addResource"];
removeResource: SettingsSearch["removeResource"];
addResources: SettingsSearch["addResources"];
removeResources: SettingsSearch["removeResources"];
};
}
}
Expand Down Expand Up @@ -95,8 +95,8 @@ export default class SettingsSearch extends Plugin {

async onload() {
(window["SettingsSearch"] = {
addResource: this.addResource.bind(this),
removeResource: this.removeResource.bind(this)
addResources: this.addResources.bind(this),
removeResources: this.removeResources.bind(this)
}) && this.register(() => delete window["SettingsSearch"]);

this.app.workspace.onLayoutReady(async () => {
Expand Down Expand Up @@ -223,10 +223,15 @@ export default class SettingsSearch extends Plugin {
this.settingCache.delete(resource);
}
}
addResource(resource: Resource) {
resource.external = true;
this.resources.push(resource);
this.addResourceToCache(resource);
addResources(...resources: Resource[]) {
for (const resource of resources) {
resource.external = true;
if (this.resources.find((k) => this.equivalent(resource, k)))
continue;
this.resources.push(resource);
this.addResourceToCache(resource);
}
return () => this.removeResources(...resources);
}
equivalent(resource1: Resource, resource2: Resource) {
return (
Expand All @@ -237,18 +242,34 @@ export default class SettingsSearch extends Plugin {
resource1.external == resource2.external
);
}
removeResource(resource: Resource) {
if (!resource || !resource.text || !resource.name || !resource.tab) {
return new Error("A valid resource must be provided.");
removeResources(...resources: Resource[]) {
const removing = [];
const keys = [...this.settingCache.keys()];
for (const resource of resources) {
if (
!resource ||
!resource.text ||
!resource.name ||
!resource.tab
) {
continue;
}
resource.external = true;
this.resources = this.resources.filter(
(r) => !this.equivalent(resource, r)
);
removing.push(
...keys.filter(
(k) => k == resource || this.equivalent(resource, k)
)
);
}
resource.external = true;
this.resources = this.resources.filter(
(r) => !this.equivalent(resource, r)
);
const keys = [...this.settingCache.keys()].filter((k) =>
this.equivalent(resource, k)
);
this.removeResourcesFromCache(keys);
this.removeResourcesFromCache(removing);
}
removeTabResources(tab: string) {
const removing = this.resources.filter((t) => t.tab == tab);
this.resources = this.resources.filter((t) => t.tab != tab);
this.removeResourcesFromCache(removing);
}
async getTabResources(tab: SettingTab) {
await tab.display();
Expand Down Expand Up @@ -280,7 +301,6 @@ export default class SettingsSearch extends Plugin {
tab.containerEl.detach();
tab.hide();
}

patchSettings() {
const self = this;

Expand Down Expand Up @@ -314,13 +334,7 @@ export default class SettingsSearch extends Plugin {
removeSettingTab: function (next) {
return function (tab: SettingTab) {
if (this.isPluginSettingTab(tab)) {
const removing = self.resources.filter(
(t) => t.tab == tab.id
);
self.resources = self.resources.filter(
(t) => t.tab != tab.id
);
self.removeResourcesFromCache(removing);
self.removeTabResources(tab.id);
}
return next.call(this, tab);
};
Expand Down

0 comments on commit 1afdb45

Please sign in to comment.