Skip to content

Commit 07a262e

Browse files
committed
feat: add honkitSettings to options
1 parent 4869ed2 commit 07a262e

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

public/README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
</style>
1313

14-
This is example.
14+
This is combination with CodeBlock.
1515

1616
<!-- sandpack:{
1717
"files": {
@@ -31,7 +31,7 @@ This is example.
3131
document.querySelector("h1").style.color = "red";
3232
```
3333

34-
This is complex example.
34+
Open editor at first.
3535

3636
<!-- sandpack:{
3737
"files": {
@@ -51,8 +51,9 @@ This is complex example.
5151
"path": "example2/index.html"
5252
}
5353
},
54-
"entry": "/index.html"
54+
"entry": "/index.html",
55+
"honkitSettings": {
56+
"isOpen": true,
57+
"hideExitButton": true
58+
}
5559
} -->
56-
```js
57-
// Todo App
58-
```

src/hook-inline.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseComment } from "./parse-comment";
1+
import { parseCommentAsSandboxOptions } from "./parse-comment-as-sandbox-options";
22
import * as fs from "fs";
33
import path from "path";
44
import { SandboxOptions } from "./sandpack";
@@ -12,7 +12,7 @@ export const inlineFiles = (content: string, filePath: string) => {
1212
const baseDir = path.dirname(filePath);
1313
const commentPattern = /<!--\s?(sandpack:{[\s\S]*?})\s?-->/g;
1414
return content.replace(commentPattern, (_, match) => {
15-
const options = parseComment(match.trim());
15+
const options = parseCommentAsSandboxOptions(match.trim());
1616
const inlinedFiles = Object.fromEntries(
1717
Object.entries(options.files).map((entry) => {
1818
const fileName = entry[0];
@@ -37,7 +37,7 @@ export const inlineFiles = (content: string, filePath: string) => {
3737
})
3838
);
3939
const updatedOptions = {
40-
...inlinedFiles,
40+
...options,
4141
files: inlinedFiles
4242
};
4343
return `<!-- sandpack:${JSON.stringify(updatedOptions)} -->`;

src/parse-comment.ts renamed to src/parse-comment-as-sandbox-options.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import type { SandboxInfo } from "@codesandbox/sandpack-client";
2-
3-
export type { SandboxInfo };
4-
51
/**
62
* <!-- sandpack:{/ Sandpack Options /} -->
73
* @param {string} comment
84
*/
9-
export function parseComment(comment: string): SandboxInfo {
5+
import { SandboxOptions } from "./sandpack";
6+
7+
export function parseCommentAsSandboxOptions(comment: string): SandboxOptions {
108
const CONSOLE_METADATA = /sandpack:({[\s\S]+})$/;
119
const optionString = comment.match(CONSOLE_METADATA);
1210
if (!optionString) {

src/sandpack.tsx

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Sandpack, SandpackPredefinedTemplate, SandpackSetup } from "@codesandbox/sandpack-react";
22
import React from "react";
33
import { createRoot } from "react-dom/client";
4-
import { parseComment } from "./parse-comment";
4+
import { parseCommentAsSandboxOptions } from "./parse-comment-as-sandbox-options";
55
import { t } from "./localize";
66
import { 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() {
197210
window.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

Comments
 (0)