Skip to content

Commit

Permalink
move code util to own module
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Jun 1, 2024
1 parent 4ad7076 commit 54ce61b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion website/docs/api/standard.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
sidebar_position: 1
---

import { code as ts } from "/src/components/CustomSandpack";
import ts from "/src/lib/code";
import { ConsoleSandbox } from "/src/components/ConsoleSandbox";

# Standard Methods
Expand Down
9 changes: 5 additions & 4 deletions website/docs/setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ A "history adapter" for managing undoable (and redoable) state changes with [imm

Also includes a Redux specific version, with additional utilities for use with Redux Toolkit.

## Redux Sandbox
## Sandboxes

import PatchesTabs from "/src/components/PatchesTabs";
import RTKCounter from "/src/components/RTKCounter";
import ZustandCounter from "/src/components/ZustandCounter";

### Redux Toolkit

<PatchesTabs>
<RTKCounter />
</PatchesTabs>

## Zustand Sandbox

import ZustandCounter from "/src/components/ZustandCounter";
### Zustand

<PatchesTabs>
<ZustandCounter />
Expand Down
10 changes: 4 additions & 6 deletions website/src/components/ConsoleSandbox.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { SandpackProps } from "@codesandbox/sandpack-react";
import {
CustomSandpack,
code as ts,
code as tsx,
code as css,
} from "./CustomSandpack";
import { CustomSandpack } from "./CustomSandpack";
import code from "../lib/code";

const { ts, tsx, css } = code;

export function ConsoleSandbox({
code,
Expand Down
7 changes: 0 additions & 7 deletions website/src/components/CustomSandpack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ import { Sandpack } from "@codesandbox/sandpack-react";
import { dracula, githubLight } from "@codesandbox/sandpack-themes";
import { useColorMode } from "@docusaurus/theme-common";

const removeLeadingTrailingNewlines: typeof String.raw = (str, ...args) =>
String.raw(str, ...args)
.replace(/^\n/, "")
.replace(/\n$/, "");

export const code = removeLeadingTrailingNewlines;

export function CustomSandpack(props: SandpackProps) {
const { colorMode } = useColorMode();
const theme = colorMode === "dark" ? dracula : githubLight;
Expand Down
5 changes: 4 additions & 1 deletion website/src/components/RTKCounter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { SandpackFiles } from "@codesandbox/sandpack-react";
import { CustomSandpack, code as ts } from "./CustomSandpack";
import { CustomSandpack } from "./CustomSandpack";
import { usePatches } from "./PatchesTabs";
import code from "../lib/code";

const { ts } = code;

export const defaultFiles = {
"/counterSlice.ts": ts`
Expand Down
5 changes: 4 additions & 1 deletion website/src/components/ZustandCounter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { SandpackFiles } from "@codesandbox/sandpack-react";
import { CustomSandpack, code as ts } from "./CustomSandpack";
import { CustomSandpack } from "./CustomSandpack";
import { usePatches } from "./PatchesTabs";
import code from "../lib/code";

const { ts } = code;

export const defaultFiles = {
"/counterStore.ts": ts`
Expand Down
17 changes: 17 additions & 0 deletions website/src/lib/code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const stripPatterns = [/^\n/, /\n$/];

const code: typeof String.raw = (template, ...subs) => {
let str = String.raw(template, ...subs);
for (const pattern of stripPatterns) {
str = str.replace(pattern, "");
}
return str;
};

export default new Proxy(code as typeof code & Record<string, typeof code>, {
get(target, p, receiver) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
if (Reflect.has(target, p)) { return Reflect.get(target, p, receiver); }
return code;
},
})

0 comments on commit 54ce61b

Please sign in to comment.