import{r as L,U as g,I as a,K as C,cw as R,cx as w,cy as N}from"./vendor-ttbOpx5u.js";import{e as j,bo as b}from"./index-C4h_t6yi.js";import{B as S,bN as E}from"./ControlType-D67jic7H.js";import{r as P}from"./mathjax-D8SISaSI.js";import"./helpers-BosuxZz1.js";import"./monaco-DaE0jI-i.js";function p(r,e,t){return(e=I(e))in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r}function I(r){var e=B(r,"string");return typeof e=="symbol"?e:e+""}function B(r,e){if(typeof r!="object"||!r)return r;var t=r[Symbol.toPrimitive];if(t!==void 0){var n=t.call(r,e);if(typeof n!="object")return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return(e==="string"?String:Number)(r)}class M extends L.PureComponent{constructor(e){super(e),p(this,"commands",void 0),p(this,"codeElements",void 0),p(this,"editorScrollView",void 0),this.handleRunSelected=this.handleRunSelected.bind(this),this.renderCodeBlock=this.renderCodeBlock.bind(this),this.renderLink=this.renderLink.bind(this),this.commands=new Map,this.codeElements=new Map,this.editorScrollView=g.createRef(),this.state={hasCode:!1,hasRunCode:!1,nextStartLine:null}}componentDidMount(){this.updateHasCode()}componentDidUpdate(){this.updateHasCode()}updateHasCode(){var{hasCode:e}=this.state;this.commands.size===0&&e?this.setState({hasCode:!1}):this.commands.size>0&&!e&&this.setState({hasCode:!0})}getNextStartLine(e){var t=[...this.commands.keys()],n=t.findIndex(s=>s===e)+1;return n>=t.length?null:t[n]}handleRunSelected(e){var t,n;e.preventDefault(),e.stopPropagation();var{nextStartLine:s}=this.state,i=[...this.commands.keys()],d=i.findIndex(x=>x===s||s==null);if(!(d<0)){var l=i[d],f=this.commands.get(l),u=this.getNextStartLine(l),m=(t=this.codeElements.get(l))===null||t===void 0?void 0:t.current,o=m==null||(n=m.parentElement)===null||n===void 0?void 0:n.nextElementSibling;if(o!=null){var c,{offsetTop:v}=o,k=v;(c=this.editorScrollView.current)===null||c===void 0||c.scroll({top:k,left:0})}this.runCode(f),this.setState({nextStartLine:u})}}runCode(e){var{onRunCode:t}=this.props;t(e),this.setState({hasRunCode:!0})}renderCodeBlock(e){var{children:t,className:n,inline:s,node:i}=e,{hasRunCode:d,nextStartLine:l}=this.state,{children:f,position:u}=i;j(u);var{start:m}=u,{line:o}=m,c=f[0].value,v=g.createRef(),k=this.commands.size===0,x=l===o||k&&l==null&&!d,y=n!==void 0&&n.startsWith("language-")?n.substring(9):"plaintext";return s!=null&&s?a.jsx("code",{className:n,children:t}):(this.commands.set(o,c),this.codeElements.set(o,v),a.jsxs("div",{className:C("markdown-notebook-code-block",{"is-selected":x}),ref:v,onClick:()=>{this.setState({nextStartLine:o})},role:"presentation",children:[a.jsx(S,{kind:"ghost",icon:b,className:"btn-play-block",onClick:h=>{h.stopPropagation(),h.preventDefault(),this.runCode(c),this.setState({nextStartLine:o})},tooltip:"Run code"}),a.jsx("code",{children:a.jsx(E,{language:y,children:g.Children.map(t,h=>typeof h=="string"?h.trim():h)})})]}))}renderLink(e){var{onLinkClick:t}=this.props,{href:n,children:s,target:i}=e;return a.jsx("a",{href:n,onClick:t,target:i,children:s})}render(){var{content:e,transformImageUri:t,transformLinkUri:n}=this.props,{hasCode:s,hasRunCode:i,nextStartLine:d}=this.state;return a.jsxs("div",{className:"markdown-notebook",children:[a.jsx("div",{className:"markdown-notebook-toolbar",children:a.jsx(S,{className:C("btn-play-selected-cell",{flashing:s&&!i}),kind:"ghost",icon:b,onClick:this.handleRunSelected,tooltip:"Run code and select next",disabled:i&&d==null||!s,children:"Run Selected Code"})}),a.jsx("div",{className:"markdown-notebook-content",ref:this.editorScrollView,children:a.jsx(R,{components:{code:this.renderCodeBlock,a:this.renderLink},linkTarget:"_blank",remarkPlugins:[w,N],rehypePlugins:[P],transformLinkUri:n,transformImageUri:t,includeElementIndex:!0,children:e})})]})}}p(M,"defaultProps",{onRunCode:()=>{}});export{M as MarkdownNotebook,M as default};
//# sourceMappingURL=MarkdownNotebook-Bcx0SeNz.js.map
