Skip to content

Add example of passing settings to view plugin#45

Closed
Phrynobatrachus wants to merge 2 commits intoobsidianmd:mainfrom
Phrynobatrachus:patch-1
Closed

Add example of passing settings to view plugin#45
Phrynobatrachus wants to merge 2 commits intoobsidianmd:mainfrom
Phrynobatrachus:patch-1

Conversation

@Phrynobatrachus
Copy link

@Phrynobatrachus Phrynobatrachus commented Jun 20, 2023

marcusolsson/obsidian-plugin-docs#73

Duped here since the above repo is now archived. 🍻

@marcusolsson
Copy link
Contributor

It's not clear to me what this example demonstrates. Could you give some more context?

Also, I'd prefer to avoid using snippets from other plugins, and instead create a simpler code example that demonstrates the specific use case.

@Phrynobatrachus
Copy link
Author

Phrynobatrachus commented Jun 29, 2023

static fromClass<V extends PluginValue>(cls: {new (view: EditorView) → V}, spec⁠?: PluginSpec<V>) → ViewPlugin<V>

If you want to reference your main plugin's settings in a view plugin, how do you do that given the constructor signature fromClass expects? The answer is that the settings have to be in scope at the time the view plugin class is defined, and using a function that's called with those settings and defines the class inline is a way to do this.

The original PR has a couple discord links to occasions where this came up, perhaps the second link's example is a bit cleaner?

https://discord.com/channels/686053708261228577/840286264964022302/1088037085086236682

export function YOURVIEWPLUGIN(settings: YOURPLUGINSETTINGS) {
  const viewPlugin = ViewPlugin.fromClass(
    class {
      // use settings wherever needed
      constructor(view: EditorView) {
        // ...
      }

      update(update: ViewUpdate) {
         // ...
      }

      buildDecorations(view: EditorView): DecorationSet {
        // ...
      }
    },
    {
      decorations: // Decorations go here
    }
  );
  return viewPlugin;
}

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.

2 participants