Skip to content

Commit

Permalink
patch: inject prefix into copy/fork source code
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Dec 13, 2022
1 parent 1573b9a commit 6b811cd
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cli/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ async function buildOnce(file: string, options: BuildOptions & CmdOptions) {
throw new CompilationError("compilation failed")
}

log(`binary: ${prettySize(binary.length)}`)
log(`bytecode: ${prettySize(binary.length)}`)
if (stats) {
const { sizes, functions } = dbg
log(
Expand Down
2 changes: 1 addition & 1 deletion cli/src/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function devtools(options: DevToolsOptions & CmdOptions) {
? () => {
const bytecode = readFileSync(bytecodeFile)
const dbg = debugFile ? readJSONSync(debugFile) : undefined
debug(`refresh bytecode ${prettySize(bytecode.length)}...`)
debug(`refresh bytecode (${prettySize(bytecode.length)}) with ${clients.length} clients...`)
const msg = JSON.stringify({
type: "bytecode",
channel: "devicescript",
Expand Down
1 change: 1 addition & 0 deletions website/language.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async function createConfig() {
label: "ts", // label for the language in markdown code blocks
highlight: "typescript", // syntax highlighting provided by prism for the language
showLineNumbers: false, // whether to show line numbers in all code block of this language
prefix: 'import * as ds from "@devicescript/core"',
buildConfig: {
version: "1",
langVersion,
Expand Down
20 changes: 6 additions & 14 deletions website/src/components/TutorialComponents/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ function CodeEditor(props: {
theme: PrismTheme
showLineNumbers: boolean
readonly: boolean
prefix?: string
sandbox?: {
files: Record<string, { content: string }>
main?: string
}
}) {
const { code, language, sandbox } = props
// const [disabled, setDisabled] = useState(props.disabled);
const [allowUndo, setAllowUndo] = useState(false)
const [tmpCode, setTmpCode] = useState("")
const [hasFocus, setHasFocus] = useState(false)

const { code, sandbox, prefix } = props
const prefixedCode =
!prefix || code.indexOf(prefix) > -1 ? code : prefix + "\n\n" + code
useEffect(() => {
if (props.onChange) {
props.onChange(code)
Expand All @@ -52,11 +50,6 @@ function CodeEditor(props: {
range.collapse(true)
selectObj.addRange(range)
}
setHasFocus(true)
}

const handleBlur = () => {
setHasFocus(false)
}

return (
Expand Down Expand Up @@ -111,7 +104,6 @@ function CodeEditor(props: {
}}
spellCheck="false"
onFocus={handleFocus}
onBlur={handleBlur}
>
<code
className={clsx(
Expand Down Expand Up @@ -148,14 +140,14 @@ function CodeEditor(props: {
<div className={codeBlockContentStyles.buttonGroup}>
<CopyButton
className={codeBlockContentStyles.codeButton}
code={code}
code={prefixedCode}
/>
{sandbox && (
<CodeSandboxButton
className={codeBlockContentStyles.codeButton}
files={{
...sandbox.files,
[sandbox.main]: { content: code },
[sandbox.main]: { content: prefixedCode },
}}
startFile={sandbox.main}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useState } from "react"
import clsx from "clsx"
import codeBlockContentStyles from "@docusaurus/theme-classic/src/theme/CodeBlock/Content/styles.module.css"

export default function CodeSandboxButton(props: {
className?: string
Expand Down
6 changes: 6 additions & 0 deletions website/src/components/TutorialComponents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface CodeBlockProps {
readonly: boolean
langVersion?: string
tool?: string
prefix?: string
sandbox?: {
files: Record<string, { content: string }>
main?: string
Expand Down Expand Up @@ -91,6 +92,7 @@ function CustomCodeEditor(props: {
onChange?: (code: string) => void
githubRepo: string | undefined
readonly: boolean
prefix?: string
sandbox?: {
files: Record<string, { content: string }>
main?: string
Expand All @@ -105,6 +107,7 @@ function CustomCodeEditor(props: {
onChange,
readonly,
sandbox,
prefix,
} = props
const prismTheme = usePrismTheme()
// console.log(prismTheme);
Expand Down Expand Up @@ -134,6 +137,7 @@ function CustomCodeEditor(props: {
readonly={readonly}
showLineNumbers={showLineNumbers}
sandbox={sandbox}
prefix={prefix}
/>
</Container>
)
Expand All @@ -154,6 +158,7 @@ export default function CustomCodeBlock(props: { input: CodeBlockProps }) {
showLineNumbers,
readonly,
sandbox,
prefix,
} = input
const [currCode, setCurrCode] = useState(code)
const [outputRendered, setOutputRendered] = useState(false)
Expand Down Expand Up @@ -236,6 +241,7 @@ export default function CustomCodeBlock(props: { input: CodeBlockProps }) {
githubRepo={githubRepo}
readonly={readonly}
sandbox={sandbox}
prefix={prefix}
/>
<>
<div className={styles.buttons}>
Expand Down
3 changes: 3 additions & 0 deletions website/src/remark/render-code-blocks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ export default function plugin() {
readonly: langConfig.readonly ?? true,
showLineNumbers: showLineNumbers,
sandbox: langConfig.sandbox,
prefix: langConfig.prefix,
label,
})
parent.children.splice(index, 1, {
type: "jsx",
Expand Down Expand Up @@ -258,6 +260,7 @@ export default function plugin() {
tool: buildConfig.npmPackage,
sandbox: langConfig.sandbox,
label,
prefix: langConfig.prefix,
})
parent.children.splice(index, 1, {
type: "jsx",
Expand Down

0 comments on commit 6b811cd

Please sign in to comment.