Skip to content

New plugin api#1457

Merged
texodus merged 5 commits intomasterfrom
new-plugin-api
Jun 24, 2021
Merged

New plugin api#1457
texodus merged 5 commits intomasterfrom
new-plugin-api

Conversation

@texodus
Copy link
Member

@texodus texodus commented Jun 22, 2021

<perspective-viewer>'s current plugin API is implemented via a configuration object literal, registered via a global registration callback function registerPlugin(). Some deficiencies of this design include:

  • Plugin only has plugin_config attribute in public API.
  • Hard to style since it is encapsulated in the Shadow DOM, must frequently create new css var aliases.
  • Even for plugins that implement light DOM rendering like @finos/perspective-viewer-datagrid, developers must endure a complex setup of awaiting the plugin to render in order to customize it.
  • Plugin keys are not discoverable in the UI.

This PR refactors the plugin API to use Custom Elements, rather than configuration object literals, as the plugin template. Plugin Custom Elements reside as the <perspective-viewer>'s light DOM children, which makes it convenient for plugins to expose their own APIs. For example, The <perspective-viewer-debug> element, which just renders the View as a CSV non-virtually here, and has this basic framework:

class DebugPlugin extends HTMLElement {
     get name() {
          return 'Debug';
     }

     get custom_func(x) {
          console.log(`${x} called a custom API call!`);
     }

     static custom_all_funcs(y) {
          console.log(`${x} called a static custom API call!`);
     }

     async draw(view) {
          const csv = await view.to_csv();
          this.innerHTML = `<pre>${csv}</pre>`;
     }
}   

customElements.define('perspective-viewer-debug', DebugPlugin);
registerPlugin("perspective-viewer-debug");

which can be customized at runtime like so:

// Specific instance
const plugin = document.querySelectorAll("perspective-viewer-debug");
plugin.custom_func("Bob Developer");

// All instances
const plugin_class = customElements.get("perspective-viewer-debug");
plugin_class.custom_all_funcs("Alice Developer");

// Customize plugin
class CustomPlugin extends customElements.get("perspective-viewer-debug") {
     async draw(view) {
          const csv = await view.to_csv();
          this.innerHTML = `<title>Custom CSV!!!</title><pre>${csv}</pre>`;
     }
}

customElements.define('perspective-viewer-custom', CustomPlugin);
registerPlugin("perspective-viewer-custom");

Other effects of this PR include:

  • type and name have been replaced by just name; e.g., "x_bar" type is now "X Bar", the name plugin property. Docs, examples, python, etc have been updated to reflect this.

@texodus texodus marked this pull request as ready for review June 23, 2021 00:52
@texodus texodus merged commit a54d9ac into master Jun 24, 2021
@texodus texodus deleted the new-plugin-api branch June 24, 2021 01:39
@texodus texodus mentioned this pull request Jun 29, 2021
@texodus texodus added the enhancement Feature requests or improvements label Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Feature requests or improvements

Development

Successfully merging this pull request may close these issues.

1 participant