Last active
April 26, 2018 22:30
-
-
Save imeredith/04827cb3538854897d806c515f8c62f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Plugin } from './Plugin'; | |
export class AwesomePlugin implements Plugin { | |
render(element: HTMLDivElement): void { | |
console.log('rendered plugin'); | |
element.innerText ='XYZXYZ'; | |
} | |
extensionId: string = 'blah'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface Plugin { | |
render(element: HTMLDivElement): void | |
extensionId: string | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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