Skip to content

Commit eb25e46

Browse files
committed
Renderer added
1 parent ea7dad6 commit eb25e46

File tree

6 files changed

+80
-33
lines changed

6 files changed

+80
-33
lines changed

example/Plugins/ClickMePlugin.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react';
33
import { IPlugin } from '../../.';
44

55
class ClickMePlugin implements IPlugin {
6-
private pluginStore;
6+
public pluginStore;
77

88
init(pluginStore) {
99
console.log('Inside init');

example/components/Test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const Test = (props: any) => {
55
const pluginStore: any = usePluginStore();
66
console.log(pluginStore);
77
pluginStore.executeFunction('test', 1, 2);
8+
let Renderer = pluginStore.executeFunction(
9+
'RendererPlugin.getRendererComponent'
10+
);
811
return (
912
<>
1013
<h1>Working</h1>{' '}
@@ -15,6 +18,7 @@ const Test = (props: any) => {
1518
>
1619
Send Alert
1720
</button>
21+
<Renderer placement={'top'} />
1822
</>
1923
);
2024
};

src/PluginStore.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export class PluginStore {
1919
this.functionArray.set(key, fn);
2020
}
2121

22-
executeFunction(key: string, ...args: any) {
22+
executeFunction(key: string, ...args: any): any {
2323
let fn = this.functionArray.get(key);
24-
fn(...args);
24+
return fn(...args);
2525
}
2626
}

src/plugins/RendererPlugin.tsx

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
import { usePluginStore } from '../../../hooks/usePluginStore';
3+
4+
export const Renderer: React.SFC<{
5+
placement: string;
6+
}> = ({ placement }) => {
7+
const pluginStore = usePluginStore();
8+
9+
let components = pluginStore.executeFunction(
10+
'RendererPlugin.getComponentsInPosition',
11+
placement
12+
);
13+
14+
return (
15+
<React.Fragment>
16+
{components.map((component: any) => (
17+
<>{component}</>
18+
))}
19+
</React.Fragment>
20+
);
21+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { IPlugin } from '../../interfaces/IPlugin';
2+
import { PluginStore } from '../../PluginStore';
3+
import { Renderer } from './components/Renderer';
4+
5+
export class RendererPlugin implements IPlugin {
6+
public pluginStore: PluginStore = new PluginStore();
7+
private componentMap: Map<string, Array<any>> = new Map<string, Array<any>>();
8+
9+
init(pluginStore: PluginStore) {
10+
this.pluginStore = pluginStore;
11+
}
12+
13+
addToComponentMap(position: string, component: React.Component) {
14+
let array = this.componentMap.get(position);
15+
if (!array) {
16+
array = [component];
17+
}
18+
this.componentMap.set(position, array);
19+
}
20+
21+
getRendererComponent() {
22+
return Renderer;
23+
}
24+
25+
getComponentsInPosition(position: string) {
26+
let componentArray = this.componentMap.get(position);
27+
if (!componentArray) return [];
28+
29+
return componentArray;
30+
}
31+
32+
activate() {
33+
this.pluginStore.addFunction(
34+
'RendererPlugin.add',
35+
this.addToComponentMap.bind(this)
36+
);
37+
38+
this.pluginStore.addFunction(
39+
'RendererPlugin.getComponentsInPosition',
40+
this.getComponentsInPosition.bind(this)
41+
);
42+
43+
this.pluginStore.addFunction(
44+
'RendererPlugin.getRendererComponent',
45+
this.getRendererComponent.bind(this)
46+
);
47+
}
48+
49+
deactivate() {
50+
//
51+
}
52+
}

0 commit comments

Comments
 (0)