UNPKG

code-surfer

Version:
3 lines (2 loc) 4.91 kB
"use strict";var e,t=(e=require("react"))&&"object"==typeof e&&"default"in e?e.default:e,r=require("mdx-deck"),n=require("@code-surfer/standalone"),o=require("shell-quote"),i=require("use-spring"),s=require("@code-surfer/themes");function l(){return(l=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function u(e){return e&&e.props&&"pre"===e.props.mdxType}function a(e){if(!u(e))throw new Error("Invalid element inside <CodeSurfer/>. Make sure to add empty lines (no spaces) before and after each codeblock.");var t=e.props.children.props,r=t.className&&t.className.split(" ")[0];return l({code:t.children,lang:r&&r.substring("language-".length)},function(e){if(!e)return{};var t=o.parse(e),r={};return t.forEach(function(e){if(e.includes("=")){var t=e.split(/=(.*)/),n=t[1];r[t[0]]="true"===n||n}else"showNumbers"===e?r.showNumbers=!0:r.focus=e}),r}(t.metastring))}var c=function(e){var r,o;function i(t){var r;return(r=e.call(this,t)||this).state={},r}o=e,(r=i).prototype=Object.create(o.prototype),r.prototype.constructor=r,r.__proto__=o,i.getDerivedStateFromError=function(e){return{error:e}};var s=i.prototype;return s.componentDidCatch=function(e,t){},s.render=function(){return this.state.error?this.state.error.element?this.state.error.element:(console.error(this.state.error),t.createElement(n.UnknownError,{error:this.state.error})):this.props.children},i}(t.Component);function f(e){var t=function(e){var t=r.useSteps(e);return Infinity===t?0:t}(e-1);return i.useSpring(t,{decimals:3,stiffness:80,damping:48,mass:8})[0]}function p(e){var o=e.children,i=e.theme,s=r.useDeck(),l=t.useMemo(d(o),[s.index]),u=f(l.length);return t.createElement("div",{style:{width:"100vw",maxWidth:"100%",height:"100vh",display:"flex",flexDirection:"column",justifyContent:"center",alignItems:"center"},className:"cs-layout"},t.createElement(n.CodeSurfer,{steps:l,progress:u,theme:i}))}var d=function(e){return function(){var r=t.Children.map(e||[],function(e){return a(e)}).filter(function(e){return e});if(0===r.length)throw Error("No codeblocks found inside <CodeSurfer/>.");return r}};function m(e){var n=e.children,o=e.themes,i=void 0===o?[]:o,l=e.sizes,c=r.useDeck(),p=t.useMemo(function(){return function(e,n){void 0===n&&(n=[]);var o=[],i=t.Children.toArray(e);if(0===i.length)throw Error("No <Step/> found inside <CodeSurferColumns/>.");if(i.forEach(function(e,r){t.Children.toArray(e.props.children).forEach(function(e,t){if(!e||!e.props)throw Error("Invalid element inside <Step/>. Make sure to add empty lines (no spaces) before and after each element.");o[t]=o[t]||{steps:[],isCode:!0};var n=u(e)?a(e):{element:e};o[t].steps[r]=n,o[t].isCode=o[t].isCode&&u(e)})}),0===o.length)throw Error("<Step/> shouldn't be empty.");o.forEach(function(e,t){e.flex=n[t]||1});var s=i.map(function(e){return e.props.title}),l=i.map(function(e){return e.props.subtitle}),c=i.map(function(e){return t.Children.toArray(e.props.children).find(function(e){return e.props&&e.props.originalType===r.Notes})});return[o,s,l,c]}(n,l)},[c.index]),d=p[0],m=p[1],y=p[2];!function(e){var n=r.useDeck();t.useEffect(function(){if(n&&n.register&&void 0!==n.index){var t=function(e){var t=e.map(function(e){if(!e)return null;var t=e.props;return t.inline?{inline:!0,text:t.children}:t&&t.children});return t.length&&(t[t.length-1]=(t[t.length-1]||"")+"\n"),t}(e);n.register(n.index,{notes:t})}},[])}(p[3]);var E=f(d[0].steps.length),x=Math.round(E);return t.createElement("div",{style:{width:"100vw",maxWidth:"100%",height:"100vh",display:"flex",flexDirection:"row",justifyContent:"center",alignItems:"center",fontSize:"0.8em",position:"relative"},className:"cs-col-layout"},d.map(function(e,r){return t.createElement(h,{key:r,column:e,progress:E,theme:i[r]})}),t.createElement(s.StylesProvider,null,t.createElement(v,{text:m[x]}),t.createElement(g,{text:y[x]})))}function h(e){var r=e.column,o=e.progress;return t.createElement("div",{style:{flex:r.flex,overflow:"hidden",height:"100%"}},r.isCode?t.createElement(n.CodeSurfer,{steps:r.steps,progress:o,theme:e.theme}):t.createElement("div",{style:{display:"flex",justifyContent:"center",alignItems:"center",height:"100%"}},r.steps[Math.round(o)].element))}function v(e){var r=e.text;return r?t.createElement(s.Styled.Title,{className:"cs-title"},t.createElement("span",null,r)):null}function g(e){var r=e.text;return r?t.createElement(s.Styled.Subtitle,{className:"cs-subtitle",style:{margin:"0.3em 0"}},t.createElement("span",null,r)):null}exports.CodeSurfer=function(e){return t.createElement(c,null,t.createElement(p,Object.assign({},e)))},exports.CodeSurferColumns=function(e){return t.createElement(c,null,t.createElement(m,Object.assign({},e)))},exports.Step=function(){return null}; //# sourceMappingURL=code-surfer.cjs.production.min.js.map