diff --git a/src/app.tsx b/src/app.tsx index 4e9da2f..a9c030f 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -16,10 +16,12 @@ import { Version, asVersion } from './php-wasm/php'; import SelectPHP from './select'; import { Editor } from './editor'; import { SunIcon } from '@chakra-ui/icons'; +import {Format, SelectFormat} from "./format"; type UrlState = { v: Version; c: string; + f: Format; }; export default function App() { @@ -32,25 +34,30 @@ export default function App() { const currentVersion = asVersion(searchParams.get('v')) ?? '8.2'; + const currentFormat = searchParams.get('f') as Format ?? "html"; + function updateVersion(v: Version) { const currentState = history.state as UrlState | null; const code = lzstring.decompressFromEncodedURIComponent( currentState?.c ?? initCode ); + const format = currentState?.f ?? currentFormat; if (code == null) { return; } setSearchParams({ v: v, c: lzstring.compressToEncodedURIComponent(code), + f: format, }); - setHistory(code, v); + setHistory(code, v, format); } - function setHistory(code: string, version: Version) { + function setHistory(code: string, version: Version, format: Format) { const state: UrlState = { c: lzstring.compressToEncodedURIComponent(code), v: version, + f: format, }; const urlSearchParam = new URLSearchParams(state).toString(); // Only push to history. @@ -61,10 +68,28 @@ export default function App() { useEffect( function () { updateVersion(currentVersion); + updateFormat(currentFormat) }, - [currentVersion] + [currentVersion, currentFormat] ); + function updateFormat(format: Format) { + const currentState = history.state as UrlState | null; + const code = lzstring.decompressFromEncodedURIComponent( + currentState?.c ?? initCode + ); + const version = currentState?.v ?? currentVersion; + if (code == null) { + return; + } + setSearchParams({ + v: version, + c: lzstring.compressToEncodedURIComponent(code), + f: format, + }); + setHistory(code, version, format); + } + return (
@@ -119,13 +144,15 @@ export default function App() { onChange={updateVersion} version={currentVersion} /> +
diff --git a/src/editor.tsx b/src/editor.tsx index e992b64..67df6b3 100644 --- a/src/editor.tsx +++ b/src/editor.tsx @@ -10,6 +10,7 @@ import { Box, Center, Flex, Spinner } from '@chakra-ui/react'; import type { ReactElement } from 'react'; import * as React from 'react'; import MonacoEditor from '@monaco-editor/react'; +import {Format} from "./format"; function LoadSpinner() { return ( @@ -41,7 +42,7 @@ function PhpEditor() { ); } -function PhpPreview(params: { version: Version }) { +function PhpPreview(params: { version: Version, format: Format }) { const { sandpack } = useSandpack(); const { files, activeFile } = sandpack; const code = files[activeFile].code; @@ -50,6 +51,9 @@ function PhpPreview(params: { version: Version }) { if (loading) { return ; } + if (params.format === "console") { + return
{result}
; + } return