Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for renderExtraFooter #55

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,19 @@ The source code is written in markdown, refer to [example.md](https://raw.github

### `<CodeView>`

| Name | Type | Default value | Description |
| -------------- | --------------------------------- | ----------------------- | ------------------------------------------------------------------------- |
| afterCompile | (code: string) => string | | Executed after compiling the code |
| beforeCompile | (code: string) => string | | Executed before compiling the code |
| children | any | | The code to be rendered is executed. Usually imported via markdown-loader |
| dependencies | object | | Dependent objects required by the executed code |
| editable | boolean | false | Renders a code editor that can modify the source code |
| editor | object | | Editor properties |
| onChange | (code?: string) => void | | Callback triggered after code change |
| renderToolbar | (buttons: ReactNode) => ReactNode | | Customize the rendering toolbar |
| sourceCode | string | | The code to be rendered is executed |
| theme | 'light' , 'dark' | 'light' | Code editor theme, applied to CodeMirror |
| compileOptions | object | defaultTransformOptions | https://github.com/alangpierce/sucrase#transforms |
| Name | Type | Default value | Description |
| ----------------- | --------------------------------- | ----------------------- | ------------------------------------------------------------------------- |
| afterCompile | (code: string) => string | | Executed after compiling the code |
| beforeCompile | (code: string) => string | | Executed before compiling the code |
| children | any | | The code to be rendered is executed. Usually imported via markdown-loader |
| compileOptions | object | defaultTransformOptions | https://github.com/alangpierce/sucrase#transforms |
| dependencies | object | | Dependent objects required by the executed code |
| editable | boolean | false | Renders a code editor that can modify the source code |
| editor | object | | Editor properties |
| onChange | (code?: string) => void | | Callback triggered after code change |
| onCloseEditor | () => void | | Callback triggered when the editor is closed |
| onOpenEditor | () => void | | Callback triggered when the editor is opened |
| renderExtraFooter | () => ReactNode | | Customize the rendering footer |
| renderToolbar | (buttons: ReactNode) => ReactNode | | Customize the rendering toolbar |
| sourceCode | string | | The code to be rendered is executed |
| theme | 'light' , 'dark' | 'light' | Code editor theme, applied to CodeMirror |
23 changes: 22 additions & 1 deletion __tests__/Renderer-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render } from '@testing-library/react';
import { render, screen, fireEvent } from '@testing-library/react';
import Renderer from '../src/Renderer';

it('refault is rendering', () => {
Expand All @@ -12,3 +12,24 @@ it('should render a test div', () => {

expect(container.querySelector('.rcv-render')).toContainHTML('<div>test</div>');
});

it('should render a test div with footer', () => {
render(<Renderer renderExtraFooter={() => <div>footer</div>} />);

expect(screen.getByText('footer')).toBeInTheDocument();
});

it('should call onOpenEditor and onCloseEditor callback', () => {
const onOpenEditor = jest.fn();
const onCloseEditor = jest.fn();

render(<Renderer onOpenEditor={onOpenEditor} onCloseEditor={onCloseEditor} />);

fireEvent.click(screen.getByRole('switch', { name: 'Show the full source' }));

expect(onOpenEditor).toHaveBeenCalled();

fireEvent.click(screen.getByRole('switch', { name: 'Show the full source' }));

expect(onCloseEditor).toHaveBeenCalled();
});
9 changes: 9 additions & 0 deletions docs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ const App = () => {
afterCompile={(code: string) => {
return code.replace(/import\ [\*\w\,\{\}\ ]+\ from\ ?[\."'@/\w-]+;/gi, '');
}}
onOpenEditor={() => {
console.log('open editor');
}}
onCloseEditor={() => {
console.log('close editor');
}}
renderExtraFooter={() => {
return <div>Footer</div>;
}}
>
{example}
</CodeView>
Expand Down
10 changes: 8 additions & 2 deletions src/CodeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const CodeView = React.forwardRef((props: CodeViewProps, ref: React.Ref<HTMLDivE
onChange,
beforeCompile,
afterCompile,
onOpenEditor,
onCloseEditor,
renderExtraFooter,
...rest
} = props;

Expand All @@ -41,13 +44,16 @@ const CodeView = React.forwardRef((props: CodeViewProps, ref: React.Ref<HTMLDivE
code={fragment.content}
editable={editable}
theme={theme}
editor={editor}
dependencies={dependencies}
transformOptions={transformOptions}
renderToolbar={renderToolbar}
afterCompile={afterCompile}
onChange={onChange}
onOpenEditor={onOpenEditor}
onCloseEditor={onCloseEditor}
beforeCompile={beforeCompile}
afterCompile={afterCompile}
editor={editor}
renderExtraFooter={renderExtraFooter}
/>
);
} else if (fragment.type === 'html') {
Expand Down
24 changes: 23 additions & 1 deletion src/Renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ export interface RendererProps extends Omit<React.HTMLAttributes<HTMLElement>, '
/** Customize the rendering toolbar */
renderToolbar?: (buttons: React.ReactNode) => React.ReactNode;

/** Customize the rendering footer */
renderExtraFooter?: () => React.ReactNode;

/**
* Callback triggered when the editor is opened
*/
onOpenEditor?: () => void;
/**
* Callback triggered when the editor is closed
*/
onCloseEditor?: () => void;

/** Callback triggered after code change */
onChange?: (code?: string) => void;

Expand All @@ -71,6 +83,9 @@ const Renderer = React.forwardRef((props: RendererProps, ref: React.Ref<HTMLDivE
code,
copyCodeButtonAs,
renderToolbar,
renderExtraFooter,
onOpenEditor,
onCloseEditor,
onChange,
beforeCompile,
afterCompile,
Expand All @@ -91,7 +106,13 @@ const Renderer = React.forwardRef((props: RendererProps, ref: React.Ref<HTMLDivE

const handleExpandEditor = useCallback(() => {
setEditable(!editable);
}, [editable]);

if (editable) {
onCloseEditor?.();
} else if (!editable) {
onOpenEditor?.();
}
}, [editable, onCloseEditor, onOpenEditor]);

const handleError = useCallback(error => {
setErrorMessage(error.message);
Expand Down Expand Up @@ -183,6 +204,7 @@ const Renderer = React.forwardRef((props: RendererProps, ref: React.Ref<HTMLDivE
code={code}
/>
)}
{renderExtraFooter?.()}
</div>
);
});
Expand Down
Loading