Skip to content

Instantly share code, notes, and snippets.

@imeredith
Last active April 26, 2018 22:30
Show Gist options
  • Save imeredith/04827cb3538854897d806c515f8c62f2 to your computer and use it in GitHub Desktop.
Save imeredith/04827cb3538854897d806c515f8c62f2 to your computer and use it in GitHub Desktop.
import { Plugin } from './Plugin';
export class AwesomePlugin implements Plugin {
render(element: HTMLDivElement): void {
console.log('rendered plugin');
element.innerText ='XYZXYZ';
}
extensionId: string = 'blah';
}
export interface Plugin {
render(element: HTMLDivElement): void
extensionId: string
}
import * as React from 'react';
import { PluginStore } from './PluginStore';
export interface Props<T> {
extensionId: string
props: T
}
export class PluginRenderer<T> extends React.Component<Props<T>> {
render() {
const pluginStore = PluginStore.getInstance();
return <div>
{pluginStore.getPlugins(this.props.extensionId).map(plugin => {
return <div ref={(element) => element && plugin.render(element, this.props.props)}></div>
})}
</div>
}
}
import { Plugin } from './Plugin'
declare global {
interface Window {
pluginStore: PluginStore;
}
}
export class PluginStore {
private plugins: Map<string, Plugin[]> = new Map<string, Plugin[]>();
register(plugin: Plugin): void {
const plugins = this.plugins.get(plugin.extensionId) || []
plugins.push(plugin);
this.plugins.set(plugin.extensionId, plugins);
}
getPlugins(extensionId: string): Plugin[] {
return this.plugins.get(extensionId) || [];
}
static getInstance() {
if(!window.pluginStore) {
window.pluginStore = new PluginStore;
}
return window.pluginStore;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment