import{r as L,V as g,J as s,M as C,co as R,cp as w,cq as N}from"./vendor-3e5e4180.js";import{e as j,aW as S}from"./index-ac0f7fdf.js";import{B as b,bp as E}from"./ControlType-f915b24d.js";import{r as P}from"./mathjax-d42af1a5.js";import"./helpers-de833af9.js";import"./monaco-b5248072.js";function p(r,e,t){return e=M(e),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:String(e)}function B(r,e){if(typeof r!="object"||r===null)return r;var t=r[Symbol.toPrimitive];if(t!==void 0){var n=t.call(r,e||"default");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(a=>a===e)+1;return n>=t.length?null:t[n]}handleRunSelected(e){var t,n;e.preventDefault(),e.stopPropagation();var{nextStartLine:a}=this.state,i=[...this.commands.keys()],d=i.findIndex(x=>x===a||a==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:a,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 a!=null&&a?s.jsx("code",{className:n,children:t}):(this.commands.set(o,c),this.codeElements.set(o,v),s.jsxs("div",{className:C("markdown-notebook-code-block",{"is-selected":x}),ref:v,onClick:()=>{this.setState({nextStartLine:o})},role:"presentation",children:[s.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"}),s.jsx("code",{children:s.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:a,target:i}=e;return s.jsx("a",{href:n,onClick:t,target:i,children:a})}render(){var{content:e,transformImageUri:t,transformLinkUri:n}=this.props,{hasCode:a,hasRunCode:i,nextStartLine:d}=this.state;return s.jsxs("div",{className:"markdown-notebook",children:[s.jsx("div",{className:"markdown-notebook-toolbar",children:s.jsx(b,{className:C("btn-play-selected-cell",{flashing:a&&!i}),kind:"ghost",icon:S,onClick:this.handleRunSelected,tooltip:"Run code and select next",disabled:i&&d==null||!a,children:"Run Selected Code"})}),s.jsx("div",{className:"markdown-notebook-content",ref:this.editorScrollView,children:s.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(I,"defaultProps",{onRunCode:()=>{}});export{I as MarkdownNotebook,I as default};
//# sourceMappingURL=MarkdownNotebook-038bd5b5.js.map
