How to access plugin settings from within a State Field?

I’m working on a simple plugin that has a single setting and utilizes a state field. Both the state field and the setting are implemented pretty much as described in the Obsidian’s official devs tutorial, except that my setting is of boolean type. In the update() method of the state field I need to perform one action if mySetting is set to true and another action if it’s set to false, however I’m not sure how to access the setting’s value from within that method. Is it somewhere amidst the oldState or transaction’s attributes? this.plugin.settings.mySetting does not seem to work.

Create your state field inside a function that takes a plugin instance as a parameter.

function createStateField(plugin: YourPlugin) {
    return StateField.define<...>({
        // You can access plugin.settings here
    })
}

export default YourPlugin extends Plugin {
    settings: YourPluginSettings

    onload() {
        ...
        this.registerEditorExtension(createStateField(this))
        ...
    }
}
1 Like