11import { Sandpack , SandpackPredefinedTemplate , SandpackSetup } from "@codesandbox/sandpack-react" ;
22import React from "react" ;
33import { createRoot } from "react-dom/client" ;
4- import { parseComment } from "./parse-comment" ;
4+ import { parseCommentAsSandboxOptions } from "./parse-comment-as-sandbox-options " ;
55import { t } from "./localize" ;
66import { Dependencies , SandpackBundlerFile } from "@codesandbox/sandpack-client/dist/types/types" ;
77
@@ -26,9 +26,14 @@ export type SandboxOptions = {
2626 * What template we use, if not defined we infer the template from the dependencies or files.
2727 */
2828 template ?: string ;
29+ honkitSettings ?: {
30+ isOpen : boolean ; // false by default
31+ hideExitButton : boolean ; // false by default
32+ hideRunButton : boolean ; // false by default
33+ } ;
2934} ;
3035
31- export const attachToElement = ( element : HTMLElement , options : SandboxOptions , isOpen : boolean = false ) => {
36+ export const attachToElement = ( element : HTMLElement | ChildNode , options : SandboxOptions ) => {
3237 let currentRoot : ReturnType < typeof createRoot > | null ;
3338 let containerElement : HTMLDivElement | null = null ;
3439 const insert = ( node : HTMLElement ) => {
@@ -76,8 +81,12 @@ export const attachToElement = (element: HTMLElement, options: SandboxOptions, i
7681 runButton . addEventListener ( "click" , ( ) => enter ( ) ) ;
7782 const buttonContainer = document . createElement ( "div" ) ;
7883 buttonContainer . className = "honkit-plugin-sandpack--buttonContainer" ;
79- buttonContainer . append ( runButton ) ;
80- buttonContainer . append ( exitButton ) ;
84+ if ( ! options . honkitSettings ?. hideRunButton ) {
85+ buttonContainer . append ( runButton ) ;
86+ }
87+ if ( ! options . honkitSettings ?. hideExitButton ) {
88+ buttonContainer . append ( exitButton ) ;
89+ }
8190 insert ( buttonContainer ) ;
8291
8392 const enter = ( ) => {
@@ -125,7 +134,7 @@ export const attachToElement = (element: HTMLElement, options: SandboxOptions, i
125134 runButton . style . display = "" ;
126135 exitButton . style . display = "none" ;
127136 } ;
128-
137+ const isOpen = options . honkitSettings ?. isOpen ?? false ;
129138 if ( isOpen ) {
130139 enter ( ) ;
131140 } else {
@@ -174,7 +183,7 @@ function updateCodeBlocs() {
174183 . map ( ( commentNode ) => {
175184 return {
176185 commentNode,
177- options : parseComment ( commentNode ?. textContent ?. trim ( ) ! )
186+ options : parseCommentAsSandboxOptions ( commentNode ?. textContent ?. trim ( ) ! )
178187 } ;
179188 } )
180189 . forEach ( ( { commentNode, options } ) => {
@@ -187,7 +196,11 @@ function updateCodeBlocs() {
187196 const nextNextNode = nextNode && nextNode . nextElementSibling ;
188197 const replaceNode = getCommentNextPreNode ( prevNode , nextNode , nextNextNode ) ;
189198 if ( replaceNode ) {
190- replaceCodeWithConsole ( replaceNode , options ) ;
199+ // append editor after pre/code
200+ attachToElement ( replaceNode , options ) ;
201+ } else {
202+ // replace comment with the editor
203+ attachToElement ( commentNode , options ) ;
191204 }
192205 } ) ;
193206 }
@@ -197,11 +210,3 @@ function updateCodeBlocs() {
197210window . gitbook . events . bind ( "page.change" , function ( ) {
198211 updateCodeBlocs ( ) ;
199212} ) ;
200-
201- function replaceCodeWithConsole ( codeBlock : Element , options : SandboxOptions ) {
202- const codes = codeBlock . getElementsByTagName ( "code" ) ;
203- if ( ! codes || codes . length === 0 ) {
204- return ;
205- }
206- attachToElement ( codeBlock as HTMLElement , options ) ;
207- }
0 commit comments