1
1
import { Sandpack , SandpackPredefinedTemplate , SandpackSetup } from "@codesandbox/sandpack-react" ;
2
2
import React from "react" ;
3
3
import { createRoot } from "react-dom/client" ;
4
- import { parseComment } from "./parse-comment" ;
4
+ import { parseCommentAsSandboxOptions } from "./parse-comment-as-sandbox-options " ;
5
5
import { t } from "./localize" ;
6
6
import { Dependencies , SandpackBundlerFile } from "@codesandbox/sandpack-client/dist/types/types" ;
7
7
@@ -26,9 +26,14 @@ export type SandboxOptions = {
26
26
* What template we use, if not defined we infer the template from the dependencies or files.
27
27
*/
28
28
template ?: string ;
29
+ honkitSettings ?: {
30
+ isOpen : boolean ; // false by default
31
+ hideExitButton : boolean ; // false by default
32
+ hideRunButton : boolean ; // false by default
33
+ } ;
29
34
} ;
30
35
31
- export const attachToElement = ( element : HTMLElement , options : SandboxOptions , isOpen : boolean = false ) => {
36
+ export const attachToElement = ( element : HTMLElement | ChildNode , options : SandboxOptions ) => {
32
37
let currentRoot : ReturnType < typeof createRoot > | null ;
33
38
let containerElement : HTMLDivElement | null = null ;
34
39
const insert = ( node : HTMLElement ) => {
@@ -76,8 +81,12 @@ export const attachToElement = (element: HTMLElement, options: SandboxOptions, i
76
81
runButton . addEventListener ( "click" , ( ) => enter ( ) ) ;
77
82
const buttonContainer = document . createElement ( "div" ) ;
78
83
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
+ }
81
90
insert ( buttonContainer ) ;
82
91
83
92
const enter = ( ) => {
@@ -125,7 +134,7 @@ export const attachToElement = (element: HTMLElement, options: SandboxOptions, i
125
134
runButton . style . display = "" ;
126
135
exitButton . style . display = "none" ;
127
136
} ;
128
-
137
+ const isOpen = options . honkitSettings ?. isOpen ?? false ;
129
138
if ( isOpen ) {
130
139
enter ( ) ;
131
140
} else {
@@ -174,7 +183,7 @@ function updateCodeBlocs() {
174
183
. map ( ( commentNode ) => {
175
184
return {
176
185
commentNode,
177
- options : parseComment ( commentNode ?. textContent ?. trim ( ) ! )
186
+ options : parseCommentAsSandboxOptions ( commentNode ?. textContent ?. trim ( ) ! )
178
187
} ;
179
188
} )
180
189
. forEach ( ( { commentNode, options } ) => {
@@ -187,7 +196,11 @@ function updateCodeBlocs() {
187
196
const nextNextNode = nextNode && nextNode . nextElementSibling ;
188
197
const replaceNode = getCommentNextPreNode ( prevNode , nextNode , nextNextNode ) ;
189
198
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 ) ;
191
204
}
192
205
} ) ;
193
206
}
@@ -197,11 +210,3 @@ function updateCodeBlocs() {
197
210
window . gitbook . events . bind ( "page.change" , function ( ) {
198
211
updateCodeBlocs ( ) ;
199
212
} ) ;
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