-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: all pass to lazy * chore: match route * chore: meta demos * chore: support getDemoById * chore: split code * chore: back of it * chore: getDemoById * chore: support demo sus * chore: all demos * chore: demo extract * chore: clean up * chore: clean up * chore: rm fallback
- Loading branch information
Showing
13 changed files
with
213 additions
and
108 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 |
---|---|---|
@@ -1,12 +1,2 @@ | ||
import * as demo from '../../docs/index.md?type=demo'; | ||
import * as demoIndex from '../../docs/index.md?type=demo-index'; | ||
import * as frontmatter from '../../docs/index.md?type=frontmatter'; | ||
import * as text from '../../docs/index.md?type=text'; | ||
|
||
console.log('frontmatter', frontmatter); | ||
console.log('demo', demo); | ||
console.log('demoIndex', demoIndex); | ||
console.log('text', text); | ||
|
||
// Customize Page for dumi test | ||
export default () => 'Customize Dumi Test Page'; |
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 @@ | ||
export default () => 'External Demo Block'; |
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 |
---|---|---|
|
@@ -3,3 +3,7 @@ | |
```jsx | ||
export default () => 'Hello Foo!'; | ||
``` | ||
|
||
你好,Foo! | ||
|
||
<code src="./demo/work.tsx">示例框</code> |
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 was deleted.
Oops, something went wrong.
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,27 @@ | ||
import Container from 'dumi/theme/builtins/Container'; | ||
import React, { type FC, type ReactNode } from 'react'; | ||
import { ErrorBoundary } from 'react-error-boundary'; | ||
|
||
const DemoErrorBoundary: FC<{ children: ReactNode }> = (props) => ( | ||
<ErrorBoundary | ||
fallbackRender={({ error }: any) => ( | ||
<Container type="error"> | ||
<p> | ||
<strong>{error.message || 'This demo has been crashed.'}</strong> | ||
</p> | ||
{error.stack && ( | ||
<p> | ||
<details open> | ||
<summary>Error stack</summary> | ||
<pre>{error.stack}</pre> | ||
</details> | ||
</p> | ||
)} | ||
</Container> | ||
)} | ||
> | ||
{props.children} | ||
</ErrorBoundary> | ||
); | ||
|
||
export default DemoErrorBoundary; |
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,66 @@ | ||
import { SP_ROUTE_PREFIX } from '@/constants'; | ||
import { useAppData, useDemoData, useSiteData } from 'dumi'; | ||
import React, { createElement, type FC } from 'react'; | ||
import type { IPreviewerProps } from '../types'; | ||
|
||
import Previewer from 'dumi/theme/builtins/Previewer'; | ||
import DemoErrorBoundary from './DemoErrorBoundary'; | ||
|
||
export interface IDumiDemoProps { | ||
demo: { | ||
id: string; | ||
inline?: boolean; | ||
}; | ||
previewerProps: Omit<IPreviewerProps, 'asset' | 'children'>; | ||
} | ||
|
||
const InternalDumiDemo = (props: IDumiDemoProps) => { | ||
const { historyType } = useSiteData(); | ||
const { basename } = useAppData(); | ||
const demoInfo = useDemoData(props.demo.id)!; | ||
|
||
// hide debug demo in production | ||
if (process.env.NODE_ENV === 'production' && props.previewerProps.debug) | ||
return null; | ||
|
||
const { component, asset } = demoInfo; | ||
|
||
const demoNode = ( | ||
<DemoErrorBoundary>{createElement(component)}</DemoErrorBoundary> | ||
); | ||
|
||
if (props.demo.inline) { | ||
return demoNode; | ||
} | ||
|
||
const isHashRoute = historyType === 'hash'; | ||
|
||
return ( | ||
<Previewer | ||
asset={asset} | ||
demoUrl={ | ||
// allow user override demoUrl by frontmatter | ||
props.previewerProps.demoUrl || | ||
// when use hash route, browser can automatically handle relative paths starting with # | ||
`${isHashRoute ? `#` : ''}${basename}${SP_ROUTE_PREFIX}demos/${ | ||
props.demo.id | ||
}` | ||
} | ||
{...props.previewerProps} | ||
> | ||
{props.previewerProps.iframe ? null : demoNode} | ||
</Previewer> | ||
); | ||
}; | ||
|
||
export const DumiDemo: FC<IDumiDemoProps> = React.memo( | ||
InternalDumiDemo, | ||
(prev, next) => { | ||
// compare length for performance | ||
return JSON.stringify(prev).length === JSON.stringify(next).length; | ||
}, | ||
); | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
InternalDumiDemo.displayName = 'DumiDemo'; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copy from React official demo. | ||
// This will be replace if React release new version of use hooks | ||
type ReactPromise<T> = Promise<T> & { | ||
status?: 'pending' | 'fulfilled' | 'rejected'; | ||
value?: T; | ||
reason?: any; | ||
}; | ||
|
||
export default function use<T>(promise: ReactPromise<T>): T { | ||
if (promise.status === 'fulfilled') { | ||
return promise.value!; | ||
} else if (promise.status === 'rejected') { | ||
throw promise.reason; | ||
} else if (promise.status === 'pending') { | ||
throw promise; | ||
} else { | ||
promise.status = 'pending'; | ||
promise.then( | ||
(result) => { | ||
promise.status = 'fulfilled'; | ||
promise.value = result; | ||
}, | ||
(reason) => { | ||
promise.status = 'rejected'; | ||
promise.reason = reason; | ||
}, | ||
); | ||
throw promise; | ||
} | ||
} |
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
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
Oops, something went wrong.