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(marshaller): init feature #38

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 1 addition & 2 deletions playground/docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ const Docs: React.FC<PropTypes> = props => {
const diff7 = React.useMemo(() => differ.diff(before7, after7), [differ, before7, after7]);

const openInPlayground = (l: unknown, r: unknown) => {
updateInitialValues(JSON.stringify(l, null, 2), JSON.stringify(r, null, 2));
props.onSwitch();
updateInitialValues('playground', JSON.stringify(l, null, 2), JSON.stringify(r, null, 2));
};

const viewerCommonProps: Partial<ViewerProps> = {
Expand Down
4 changes: 3 additions & 1 deletion playground/generated-code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const GeneratedCode: React.FC<PropTypes> = ({ code }) => {
}, [code]);

return (
<pre className="language-tsx" dangerouslySetInnerHTML={{ __html: html }} />
<div className="generated-code">
<pre className="language-tsx" dangerouslySetInnerHTML={{ __html: html }} />
</div>
);
};

Expand Down
15 changes: 11 additions & 4 deletions playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ import ReactDOM from 'react-dom';

import Playground from './playground';
import Docs from './docs';
import MarshallerTest from './marshaller-test';

import '../src/viewer.less';
import './index.less';
import { updateInitialValues, useInitialValues } from './initial-values';

const Index: React.FC = () => {
const [usePlayground, setUsePlayground] = React.useState(true);
const { page } = useInitialValues();

return usePlayground
? <Playground onSwitch={() => setUsePlayground(false)} />
: <Docs onSwitch={() => setUsePlayground(true)} />;
switch (page) {
case 'marshaller-test':
return <MarshallerTest />;
case 'docs':
return <Docs onSwitch={() => updateInitialValues('playground', '', '')} />;
default:
return <Playground onSwitch={() => updateInitialValues('docs', '', '')} />;
}
};

ReactDOM.render(<React.StrictMode><Index /></React.StrictMode>, document.getElementById('root'));
13 changes: 9 additions & 4 deletions playground/initial-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const getValue = () => {
const hash = window.location.hash ? window.location.hash.slice(1) : '';
const query = new URLSearchParams(hash);
return {
page: query.get('page') || 'playground',
l: query.get('l') || '',
r: query.get('r') || '',
};
Expand All @@ -15,22 +16,26 @@ export const useInitialValues = () => {
React.useEffect(() => {
const hashChange = () => {
const newValue = getValue();
if (initialValues.l !== newValue.l || initialValues.r !== newValue.r) {
setInitialValues(newValue);
for (const key in newValue) {
if (initialValues[key] !== newValue[key]) {
setInitialValues(newValue);
break;
}
}
};
window.addEventListener('hashchange', hashChange);
return () => {
window.removeEventListener('hashchange', hashChange);
};
}, []);
}, [initialValues]);

return initialValues;
};

export const updateInitialValues = (l: string, r: string) => {
export const updateInitialValues = (page: string, l: string, r: string) => {
const hash = window.location.hash ? window.location.hash.slice(1) : '';
const query = new URLSearchParams(hash);
query.set('page', page);
query.set('l', l);
query.set('r', r);
window.location.hash = query.toString();
Expand Down
Loading