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 (
{result}; + } return ; } @@ -76,7 +80,7 @@ function EditorLayout(params: { Editor: ReactElement; Preview: ReactElement }) { as={SandpackLayout} flexDirection={{ base: 'column', lg: 'row' }} height={{ base: '50%', lg: '100%' }} - width="100%" + width={{ base: '100%', lg: '50%' }} >