Skip to content

Comments

Add new APIs: isBuildingResourcesForTab & getShownResult#49

Open
RyotaUshio wants to merge 1 commit intojavalent:mainfrom
RyotaUshio:show-result
Open

Add new APIs: isBuildingResourcesForTab & getShownResult#49
RyotaUshio wants to merge 1 commit intojavalent:mainfrom
RyotaUshio:show-result

Conversation

@RyotaUshio
Copy link

@RyotaUshio RyotaUshio commented May 5, 2025

Hi, thank you for maintaining this project. I'd like to suggest some addition to the plugin API. Let me know if you have any suggestions. Also please feel free to reject this PR if it doesn't make sense to you.

Pull Request Description

Problem

I'm currently working on a plugin where its setting tab renders a tree view dynamically, which means the leaves (= each Setting) are not available when Settings Search builds resources.

I can work around this by either using the current API (window.SettingsSearch.addResources, etc.) or using a hack like this:

class MySettingTab extends PluginSettingTab {
  ...
  display() {
    this.treeView.render(); // the tree is collapsed by default

    // check if this display() call is triggered by Settings Search's initial resource building
    const stack = new Error().stack;
    if (typeof stack === 'string' && /\(plugin:settings-search:\d+:\d+\)/.test(stack) && stack.includes('.getTabResources')) {
        this.treeView.expandAll(); // render all leaf nodes so that Settings Search can grab them
    }
  }
}

Of course this method is too hacky and unreliable, but the biggest problem is that, when a user searches for a specific leaf setting and click the arrow button, the user cannot jump to the setting because the tree is collapsed in this time and the destination setting is not rendered yet.

Solution

This problem can be solved by providing new APIs: isBuildingResoucesForTab and getShownResult.

class MySettingTab extends PluginSettingTab {
  ...
  display() {
    this.treeView.render(); // the tree is collapsed by default

    // check if this display() call is triggered by Settings Search's initial resource building
    if (SettingsSearch.isBuildingResourcesForTab(this)) {
        this.treeView.expandAll(); // render all leaf nodes so that Settings Search can grab them
        return;
    }

    const shownResult = SettingsSearch.getShownResult();
    if (!shownResult) return;
    
    this.treeView.expandTo(shownResult.text); // Custom logic to expand the tree so that the destination setting is visible
  }
}

The current showResult method seems to include additional logic to handle Style Settings and Excalidraw (and other plugins with <detail>-based tree views), by providing the getShownResult API, any plugin will be able to be compatible with Settings Search without hard coding their own logics in Settings Search's code.

Changes Proposed

  • Added SettingsSearch.tabCached property that indicates which tab is being cached, and SettingsSearch.isBuildingResourcesForTab(tab: SettingTab): boolean method to check a given tab is being cached.
  • Added SettingsSearch.shownResult property that holds the resource passed to the showResult method, and SettingsSearch.getShownResult(): Resouce | null as a getter.
  • Exposed isBuildingResourcesForTab and getShownResult to the public plugin API.

Related Issues

Not exactly, but related: #21

Checklist

  • I have read the contribution guidelines and code of conduct.
  • I have tested the changes locally and they are working as expected.
  • I have added appropriate comments and documentation for the code changes.
    • I chose not to add comments to align the existing code style.
  • My code follows the coding style and standards of this project.
  • I have rebased my branch on the latest main (or master) branch.
  • All tests (if applicable) have passed successfully.
    • Test not found
  • I have run linters and fixed any issues.
  • I have checked for any potential security issues or vulnerabilities.

Screenshots (if applicable)

Not applicable

Additional Notes

Not applicable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant