/* eslint-disable no-unused-vars */ import React from 'react'; import {findDOMNode} from 'react-dom'; import {createRoot} from 'react-dom/client'; import {getTheme, render} from 'amis'; import {LazyComponent} from 'amis-core'; import {Overlay} from 'amis-core'; import {PopOver} from 'amis-core'; import classnames from 'classnames'; import {Link} from 'react-router-dom'; import Play from './Play'; class CodePreview extends React.Component { state = { PlayGround: null }; render() { const {container, setAsideFolded, setHeaderVisible, ...rest} = this.props; return ; } } function eachDom(dom, iterator) { if (!dom) { return; } iterator(dom); if (dom.children && dom.children.length) { [].slice.call(dom.children).forEach(dom => eachDom(dom, iterator)); } } class Preview extends React.Component { static displayName = 'MarkdownRenderer'; ref = null; roots = []; constructor(props) { super(props); this.divRef = this.divRef.bind(this); } componentDidMount() { this.renderSchema(); this.fixHtmlPreview(); if (location.hash && location.hash.length > 1) { // 禁用自动跳转 if (window.history && 'scrollRestoration' in window.history) { window.history.scrollRestoration = 'manual'; } const dom = document.querySelector( `[name="${location.hash.substring(1)}"]` ); dom && dom.scrollIntoView(); } } componentDidUpdate() { this.renderSchema(); this.fixHtmlPreview(); } componentWillUnmount() { // 立即 unmout 会报错 window.requestAnimationFrame(() => { this.roots.forEach(root => root.unmount()); }); } divRef(ref) { this.ref = ref; if (ref) { ref.innerHTML = this.props.doc.html; } } renderSchema() { const scripts = document.querySelectorAll('script[type="text/schema"]'); if (!scripts && !scripts.length) { return; } for (let i = 0, len = scripts.length; i < len; i++) { let script = scripts[i]; let props = {}; [].slice.apply(script.attributes).forEach(item => { props[item.name] = item.value; }); let dom = document.createElement('div'); let height = props.height ? parseInt(props.height, 10) : 200; if (this.props.viewMode === 'mobile') { // 移动端下高度不能太低 if (height < 500) { height = 500; } } dom.setAttribute('class', 'doc-play-ground'); // dom.setAttribute('style', `min-height: ${height}px;`); const origin = script.parentNode; origin.parentNode.replaceChild(dom, origin); const root = createRoot(dom); this.roots.push(root); root.render( findDOMNode(this)} component={CodePreview} code={script.innerText} scope={props.scope} // unMountOnHidden height={height} placeholder="加载中,请稍后。。。" /> ); } } fixHtmlPreview() { const htmlPreviews = document.querySelectorAll('.amis-doc>.preview'); if (!htmlPreviews && !htmlPreviews.length) { return; } const ns = getTheme(this.props.theme)?.classPrefix; [].slice.call(htmlPreviews).forEach(dom => { eachDom(dom, dom => { if (typeof dom.className !== 'string') { return; } dom.className = dom.className.replace( /(^|\s)([A-Z])/g, '$1' + ns + '$2' ); }); }); } render() { return (
Doc
); } } export default function (doc) { return class extends React.Component { popoverDom = null; originTitle = document.title; state = { headingPopover: false }; popoverRef = ref => { this.popoverDom = ref; }; renderHeading(children) { return children.map((child, idx) => (
{child.label} {child.children && child.children.length ? this.renderHeading(child.children) : null}
)); } handlePopOverClick = e => { this.setState({headingPopover: false}); e.stopPropagation(); // e.preventDefault(); }; renderHeadingPopover() { return this.state.headingPopover ? ( this.setState({headingPopover: false})} overlay onClick={this.handlePopOverClick} > {this.renderHeading(doc.toc.children)} ) : null; } componentDidMount() { if (doc.title) { document.title = doc.title; } } componentWillUnmount() { document.title = this.originTitle; } pathJoin(...parts) { const separator = '/'; const normalizedParts = parts .filter( part => part != null && (typeof part === 'string' || typeof part === 'number') ) .map((item, index, arr) => { let part = `${item}`; // 去除首个元素之外的"/"前缀 if (index > 0) { part = part.replace(/^[\/]+/, ''); } // 去除中间元素的"/"后缀,最后一个元素的多个"/"后缀改为1个 return index < arr.length - 1 ? part.replace(/[\/]+$/, '') : part.replace(/[\/]+$/, '/'); }); return normalizedParts.join(separator); } getDocEditLink() { const {ContextPath} = this.props; const basePath = 'https://github.com/baidu/amis/edit/master'; try { const [urlPath, locale, moduleName, relativePath] = location.pathname .replace(ContextPath, '') .match(/^\/(zh-CN)\/(docs|components|style|)(([\/]?[\w-]+)*)/); if (moduleName === 'docs') { return this.pathJoin( basePath, `/docs/${locale}/`, `${relativePath}.md` ); } else if ( moduleName === 'style' && !/style\/(index|css-vars|responsive-design|state)$/.test(urlPath) ) { const fileName = location.pathname.split('/')?.slice(-1)?.[0]; return this.pathJoin( basePath, `/packages/amis-ui/scss/helper`, relativePath.replace(fileName, `/_${fileName}.scss`) ); } else { return this.pathJoin( basePath, `/docs/${locale}/${moduleName}`, `/${relativePath}.md` ); } } catch (error) { return this.pathJoin(basePath, 'docs'); } } render() { const {prevDoc, nextDoc, ContextPath} = this.props; return ( <>
{doc.title ? (

{doc.title}

{doc?.toc.children?.length ? (
this.setState({ headingPopover: !this.state.headingPopover }) } className="Doc-headingPopBtn visible-xs" > {this.renderHeadingPopover()}
) : null}
) : null}
{prevDoc ? (
上一篇 - {prevDoc.group || '其他'}
{prevDoc.label}
) : null} {nextDoc ? (
下一篇 - {nextDoc.group || '其他'}
{nextDoc.label}
) : null}
{doc.toc && doc.toc.children && doc.toc.children.length > 0 ? (
{this.renderHeading(doc.toc.children)}
) : null} ); } }; }