import{r as L,V as g,J as a,M as C,co as R,cp as w,cq as j}from"./vendor-DKP6ul09.js";import{e as N,aW as S}from"./index-B2hEC56I.js";import{B as b,bo as E}from"./ControlType-C3G8KL5r.js";import{r as P}from"./mathjax-CS2eaJoM.js";import"./helpers-BosuxZz1.js";import"./monaco-C4VaWq8m.js";function p(r,e,t){return(e=M(e))in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r}function M(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 I 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(i=>i===e)+1;return n>=t.length?null:t[n]}handleRunSelected(e){var t,n;e.preventDefault(),e.stopPropagation();var{nextStartLine:i}=this.state,s=[...this.commands.keys()],d=s.findIndex(x=>x===i||i==null);if(!(d<0)){var l=s[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:i,node:s}=e,{hasRunCode:d,nextStartLine:l}=this.state,{children:f,position:u}=s;N(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 i!=null&&i?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(b,{kind:"ghost",icon:S,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:i,target:s}=e;return a.jsx("a",{href:n,onClick:t,target:s,children:i})}render(){var{content:e,transformImageUri:t,transformLinkUri:n}=this.props,{hasCode:i,hasRunCode:s,nextStartLine:d}=this.state;return a.jsxs("div",{className:"markdown-notebook",children:[a.jsx("div",{className:"markdown-notebook-toolbar",children:a.jsx(b,{className:C("btn-play-selected-cell",{flashing:i&&!s}),kind:"ghost",icon:S,onClick:this.handleRunSelected,tooltip:"Run code and select next",disabled:s&&d==null||!i,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,j],rehypePlugins:[P],transformLinkUri:n,transformImageUri:t,includeElementIndex:!0,children:e})})]})}}p(I,"defaultProps",{onRunCode:()=>{}});export{I as MarkdownNotebook,I as default};
//# sourceMappingURL=MarkdownNotebook-BxXofSyL.js.map
