Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.

Commit 6b811cd

Browse files
committed
patch: inject prefix into copy/fork source code
1 parent 1573b9a commit 6b811cd

File tree

7 files changed

+18
-17
lines changed

7 files changed

+18
-17
lines changed

cli/src/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ async function buildOnce(file: string, options: BuildOptions & CmdOptions) {
152152
throw new CompilationError("compilation failed")
153153
}
154154

155-
log(`binary: ${prettySize(binary.length)}`)
155+
log(`bytecode: ${prettySize(binary.length)}`)
156156
if (stats) {
157157
const { sizes, functions } = dbg
158158
log(

cli/src/devtools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export async function devtools(options: DevToolsOptions & CmdOptions) {
7070
? () => {
7171
const bytecode = readFileSync(bytecodeFile)
7272
const dbg = debugFile ? readJSONSync(debugFile) : undefined
73-
debug(`refresh bytecode ${prettySize(bytecode.length)}...`)
73+
debug(`refresh bytecode (${prettySize(bytecode.length)}) with ${clients.length} clients...`)
7474
const msg = JSON.stringify({
7575
type: "bytecode",
7676
channel: "devicescript",

website/language.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ async function createConfig() {
1111
label: "ts", // label for the language in markdown code blocks
1212
highlight: "typescript", // syntax highlighting provided by prism for the language
1313
showLineNumbers: false, // whether to show line numbers in all code block of this language
14+
prefix: 'import * as ds from "@devicescript/core"',
1415
buildConfig: {
1516
version: "1",
1617
langVersion,

website/src/components/TutorialComponents/CodeBlock.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,15 @@ function CodeEditor(props: {
2626
theme: PrismTheme
2727
showLineNumbers: boolean
2828
readonly: boolean
29+
prefix?: string
2930
sandbox?: {
3031
files: Record<string, { content: string }>
3132
main?: string
3233
}
3334
}) {
34-
const { code, language, sandbox } = props
35-
// const [disabled, setDisabled] = useState(props.disabled);
36-
const [allowUndo, setAllowUndo] = useState(false)
37-
const [tmpCode, setTmpCode] = useState("")
38-
const [hasFocus, setHasFocus] = useState(false)
39-
35+
const { code, sandbox, prefix } = props
36+
const prefixedCode =
37+
!prefix || code.indexOf(prefix) > -1 ? code : prefix + "\n\n" + code
4038
useEffect(() => {
4139
if (props.onChange) {
4240
props.onChange(code)
@@ -52,11 +50,6 @@ function CodeEditor(props: {
5250
range.collapse(true)
5351
selectObj.addRange(range)
5452
}
55-
setHasFocus(true)
56-
}
57-
58-
const handleBlur = () => {
59-
setHasFocus(false)
6053
}
6154

6255
return (
@@ -111,7 +104,6 @@ function CodeEditor(props: {
111104
}}
112105
spellCheck="false"
113106
onFocus={handleFocus}
114-
onBlur={handleBlur}
115107
>
116108
<code
117109
className={clsx(
@@ -148,14 +140,14 @@ function CodeEditor(props: {
148140
<div className={codeBlockContentStyles.buttonGroup}>
149141
<CopyButton
150142
className={codeBlockContentStyles.codeButton}
151-
code={code}
143+
code={prefixedCode}
152144
/>
153145
{sandbox && (
154146
<CodeSandboxButton
155147
className={codeBlockContentStyles.codeButton}
156148
files={{
157149
...sandbox.files,
158-
[sandbox.main]: { content: code },
150+
[sandbox.main]: { content: prefixedCode },
159151
}}
160152
startFile={sandbox.main}
161153
/>

website/src/components/TutorialComponents/CodeSandboxButton.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { useState } from "react"
22
import clsx from "clsx"
3-
import codeBlockContentStyles from "@docusaurus/theme-classic/src/theme/CodeBlock/Content/styles.module.css"
43

54
export default function CodeSandboxButton(props: {
65
className?: string

website/src/components/TutorialComponents/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ interface CodeBlockProps {
2323
readonly: boolean
2424
langVersion?: string
2525
tool?: string
26+
prefix?: string
2627
sandbox?: {
2728
files: Record<string, { content: string }>
2829
main?: string
@@ -91,6 +92,7 @@ function CustomCodeEditor(props: {
9192
onChange?: (code: string) => void
9293
githubRepo: string | undefined
9394
readonly: boolean
95+
prefix?: string
9496
sandbox?: {
9597
files: Record<string, { content: string }>
9698
main?: string
@@ -105,6 +107,7 @@ function CustomCodeEditor(props: {
105107
onChange,
106108
readonly,
107109
sandbox,
110+
prefix,
108111
} = props
109112
const prismTheme = usePrismTheme()
110113
// console.log(prismTheme);
@@ -134,6 +137,7 @@ function CustomCodeEditor(props: {
134137
readonly={readonly}
135138
showLineNumbers={showLineNumbers}
136139
sandbox={sandbox}
140+
prefix={prefix}
137141
/>
138142
</Container>
139143
)
@@ -154,6 +158,7 @@ export default function CustomCodeBlock(props: { input: CodeBlockProps }) {
154158
showLineNumbers,
155159
readonly,
156160
sandbox,
161+
prefix,
157162
} = input
158163
const [currCode, setCurrCode] = useState(code)
159164
const [outputRendered, setOutputRendered] = useState(false)
@@ -236,6 +241,7 @@ export default function CustomCodeBlock(props: { input: CodeBlockProps }) {
236241
githubRepo={githubRepo}
237242
readonly={readonly}
238243
sandbox={sandbox}
244+
prefix={prefix}
239245
/>
240246
<>
241247
<div className={styles.buttons}>

website/src/remark/render-code-blocks.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ export default function plugin() {
226226
readonly: langConfig.readonly ?? true,
227227
showLineNumbers: showLineNumbers,
228228
sandbox: langConfig.sandbox,
229+
prefix: langConfig.prefix,
230+
label,
229231
})
230232
parent.children.splice(index, 1, {
231233
type: "jsx",
@@ -258,6 +260,7 @@ export default function plugin() {
258260
tool: buildConfig.npmPackage,
259261
sandbox: langConfig.sandbox,
260262
label,
263+
prefix: langConfig.prefix,
261264
})
262265
parent.children.splice(index, 1, {
263266
type: "jsx",

0 commit comments

Comments
 (0)