-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add event for data communicate (#62)
* feat: add context for data communication * chore: changeset * wip: event * feat: event for data communicate * chore: add changeset * chore: update changesets
- Loading branch information
Showing
9 changed files
with
202 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@antv/gpt-vis': minor | ||
--- | ||
|
||
feat: support event for data communicate |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,67 @@ | ||
import React, { memo } from 'react'; | ||
import EventEmitter from '@antv/event-emitter'; | ||
import React, { memo, useEffect, useMemo } from 'react'; | ||
import type { Options } from 'react-markdown'; | ||
import Markdown from 'react-markdown'; | ||
import rehypeRaw from 'rehype-raw'; | ||
import remarkGfm from 'remark-gfm'; | ||
import { GPTVisContext } from './hooks/useContext'; | ||
import { useEventPublish } from './hooks/useEvent'; | ||
|
||
export interface GPTVisLiteProps extends Options { | ||
/** 自定义 markdown components样式 */ | ||
/** | ||
* 自定义 markdown components | ||
*/ | ||
components?: | ||
| Options['components'] | ||
| { | ||
[key: string]: (props: any) => React.ReactNode; | ||
}; | ||
/** | ||
* 🧪 订阅组件事件,实验性属性 | ||
* 用于子组件与容器组件通信 | ||
*/ | ||
eventSubs?: Record<string, (data?: any) => void>; | ||
} | ||
|
||
const GPTVisLite: React.FC<GPTVisLiteProps> = ({ | ||
children, | ||
components, | ||
rehypePlugins, | ||
remarkPlugins, | ||
eventSubs, | ||
...rest | ||
}) => { | ||
const eventBus = useMemo(() => new EventEmitter(), []); | ||
const contextValue = useMemo(() => ({ eventBus }), [eventBus]); | ||
|
||
useEffect(() => { | ||
if (eventSubs) { | ||
const events = Object.keys(eventSubs); | ||
for (const eventName of events) { | ||
eventBus.on(eventName, eventSubs[eventName]); | ||
} | ||
return () => { | ||
for (const eventName of events) { | ||
eventBus.off(eventName, eventSubs[eventName]); | ||
} | ||
}; | ||
} | ||
}, [eventBus, eventSubs]); | ||
|
||
return ( | ||
<Markdown | ||
components={components} | ||
rehypePlugins={[rehypeRaw, ...(rehypePlugins ? rehypePlugins : [])]} | ||
remarkPlugins={[remarkGfm, ...(remarkPlugins ? remarkPlugins : [])]} | ||
{...rest} | ||
> | ||
{children} | ||
</Markdown> | ||
<GPTVisContext.Provider value={contextValue}> | ||
<Markdown | ||
components={components} | ||
rehypePlugins={[rehypeRaw, ...(rehypePlugins ? rehypePlugins : [])]} | ||
remarkPlugins={[remarkGfm, ...(remarkPlugins ? remarkPlugins : [])]} | ||
{...rest} | ||
> | ||
{children} | ||
</Markdown> | ||
</GPTVisContext.Provider> | ||
); | ||
}; | ||
|
||
export { useEventPublish }; | ||
|
||
export default memo(GPTVisLite); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { CodeBlockComponent } from '@antv/gpt-vis'; | ||
import { GPTVisLite, withChartCode } from '@antv/gpt-vis'; | ||
import React, { useCallback, useMemo, useState } from 'react'; | ||
|
||
export const MyContext = React.createContext(null as any); | ||
|
||
export function useMyContext() { | ||
const context = React.useContext(MyContext); | ||
if (context === undefined || Object.keys(context).length === 0) { | ||
throw new Error(`useMyContext must be used within a MyContext.Provider`); | ||
} | ||
|
||
return context; | ||
} | ||
|
||
/** | ||
* 自定义代码块渲染器 | ||
*/ | ||
const MyUIRenderer: CodeBlockComponent = ({ children }) => { | ||
const context = useMyContext(); | ||
console.log('context: ', context); | ||
return ( | ||
<div style={{ backgroundColor: '#f0f0f0', padding: '10px' }}> | ||
<p>{children}</p> | ||
<button onClick={context?.onClick} type="button"> | ||
click | ||
</button> | ||
</div> | ||
); | ||
}; | ||
const customRenderers = { 'my-ui': MyUIRenderer }; | ||
const components = { | ||
code: withChartCode({ | ||
languageRenderers: customRenderers, // register custom block renderer | ||
components: {}, | ||
}), | ||
}; | ||
|
||
const content = ` | ||
\`\`\`my-ui | ||
my ui data ... | ||
\`\`\` | ||
`; | ||
|
||
export default () => { | ||
const [count, setCount] = useState(0); | ||
const handleClick = useCallback(() => { | ||
console.log('handleClick'); | ||
setCount((pre) => pre + 1); | ||
// do something | ||
}, []); | ||
const context = useMemo(() => ({ count: count, onClick: handleClick }), [count]); | ||
|
||
return ( | ||
<> | ||
<p>count: {count}</p> | ||
<MyContext.Provider value={context}> | ||
<div> | ||
{/* other component ... */} | ||
<div> | ||
{/* other component ... */} | ||
<GPTVisLite components={components}>{content}</GPTVisLite> | ||
</div> | ||
</div> | ||
</MyContext.Provider> | ||
</> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { CodeBlockComponent } from '@antv/gpt-vis'; | ||
import { GPTVisLite, useEventPublish, withChartCode } from '@antv/gpt-vis'; | ||
import React, { useState } from 'react'; | ||
|
||
/** | ||
* 自定义代码块渲染器 | ||
*/ | ||
const MyUIRenderer: CodeBlockComponent = ({ children }) => { | ||
const eventPublish = useEventPublish(); | ||
return ( | ||
<div style={{ backgroundColor: '#f0f0f0', padding: '10px' }}> | ||
<p>{children}</p> | ||
<button | ||
onClick={() => { | ||
eventPublish('xxxclick', {}); | ||
}} | ||
type="button" | ||
> | ||
click | ||
</button> | ||
</div> | ||
); | ||
}; | ||
const customRenderers = { 'my-ui': MyUIRenderer }; | ||
const components = { | ||
code: withChartCode({ | ||
languageRenderers: customRenderers, | ||
components: {}, | ||
}), | ||
}; | ||
|
||
const content = ` | ||
\`\`\`my-ui | ||
my ui data ... | ||
\`\`\` | ||
`; | ||
export default () => { | ||
const [count, setCount] = useState(0); | ||
const onClick = (data: any) => { | ||
console.log('data: ', data); | ||
setCount((pre) => pre + 1); | ||
// do something | ||
}; | ||
|
||
return ( | ||
<> | ||
<p>count: {count}</p> | ||
<GPTVisLite eventSubs={{ xxxclick: onClick }} components={components}> | ||
{content} | ||
</GPTVisLite> | ||
</> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type EventEmitter from '@antv/event-emitter'; | ||
import React from 'react'; | ||
|
||
type GPTVisContextValue = { | ||
eventBus: EventEmitter; | ||
}; | ||
|
||
export const GPTVisContext = React.createContext<GPTVisContextValue>(null as any); | ||
|
||
export function useGPTVisContext<T = GPTVisContextValue>() { | ||
const context = React.useContext(GPTVisContext); | ||
if (context === undefined || Object.keys(context).length === 0) { | ||
throw new Error(`useGPTVisContext must be used within a GPTVisContext.Provider`); | ||
} | ||
|
||
return context as T; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { useGPTVisContext } from './useContext'; | ||
|
||
export const useEventPublish = () => { | ||
const { eventBus } = useGPTVisContext(); | ||
|
||
return eventBus.emit.bind(eventBus); | ||
}; |
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
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