Skip to content

Commit adb82ca

Browse files
authored
Merge pull request #2 from GeekyAnts/feat/renderer
Feat/renderer
2 parents 593c2da + eb25e46 commit adb82ca

File tree

8 files changed

+92
-6
lines changed

8 files changed

+92
-6
lines changed

example/Plugins/ClickMePlugin.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1+
// @ts-ignore
2+
import React from 'react';
13
import { IPlugin } from '../../.';
24

35
class ClickMePlugin implements IPlugin {
4-
private pluginStore;
6+
public pluginStore;
57

68
init(pluginStore) {
79
console.log('Inside init');
810
this.pluginStore = pluginStore;
911
}
1012
activate() {
11-
console.log('Inside activate', this.pluginStore);
12-
1313
this.pluginStore.addFunction('sendAlert', () => {
1414
alert('Testing');
1515
});
16+
17+
this.pluginStore.executeFunction(
18+
'RendererPlugin.add',
19+
'top',
20+
<h1>asjdf</h1>
21+
);
1622
}
1723
deactivate() {
1824
//

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
};

example/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import 'react-app-polyfill/ie11';
22
import * as React from 'react';
33
import * as ReactDOM from 'react-dom';
4-
import { createPluginStore, PluginProvider } from '../.';
4+
import { createPluginStore, PluginProvider, RendererPlugin } from '../.';
55
import ClickMePlugin from './Plugins/ClickMePlugin';
66
import Test from './components/Test';
77

88
const App = () => {
99
const pluginStore = createPluginStore();
10+
pluginStore.install(new RendererPlugin());
1011
pluginStore.install(new ClickMePlugin());
1112

1213
pluginStore.addFunction('test', (a, b) => {

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/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { IPlugin } from './interfaces/IPlugin';
22
export { createPluginStore } from './hooks/createPluginStore';
33
export { PluginProvider } from './PluginProvider';
44
export { usePluginStore } from './hooks/usePluginStore';
5+
export { RendererPlugin } from './plugins/RendererPlugin';

src/interfaces/IPlugin.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { PluginStore } from '../PluginStore';
22

33
export interface IPlugin {
4+
pluginStore: PluginStore;
45
init(pluginStore: PluginStore): void;
56
activate(): void;
67
deactivate(): void;
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)