From 9e93a40a292271645a6e9561b59214ea1019f3ee Mon Sep 17 00:00:00 2001 From: Yossi Saadi Date: Tue, 2 Apr 2024 09:37:24 +0300 Subject: [PATCH] refactor(Editor): generalize editor component to be reusable --- src/components/Addons/Panel.tsx | 4 ++-- src/components/Editor/Editor.tsx | 38 +++++++++++++++++++++----------- src/types.ts | 3 ++- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/components/Addons/Panel.tsx b/src/components/Addons/Panel.tsx index 32b236b..009c2bc 100644 --- a/src/components/Addons/Panel.tsx +++ b/src/components/Addons/Panel.tsx @@ -70,11 +70,11 @@ const Panel: React.FC = ({ active }) => { diff --git a/src/components/Editor/Editor.tsx b/src/components/Editor/Editor.tsx index 021c2e5..1a1ca8b 100644 --- a/src/components/Editor/Editor.tsx +++ b/src/components/Editor/Editor.tsx @@ -1,18 +1,22 @@ import React, { forwardRef, lazy } from "react"; -import { Extension, ReactCodeMirrorRef } from "@uiw/react-codemirror"; -import { Code, EditorTheme, PlaygroundState } from "@/types"; +import { + Extension, + ReactCodeMirrorRef, + BasicSetupOptions, +} from "@uiw/react-codemirror"; import { Loader } from "@storybook/components"; const CodeMirror = lazy(() => import("@uiw/react-codemirror")); import "./Editor.module.css"; interface EditorProps { - loading: boolean; - type: PlaygroundState["selectedTab"]; - code: Code["jsx"] | Code["css"]; - theme?: EditorTheme; - fontSize: PlaygroundState["fontSize"]; - extensions: Extension[]; - onChange: (newVal: Code["jsx"] | Code["css"]) => void; + code: string; + onChange: (newVal: string) => void; + placeholder?: string; + loading?: boolean; + theme?: "light" | "dark" | Extension; + style?: React.CSSProperties; + extensions?: Extension[]; + setup?: BasicSetupOptions; } type EditorComponent = React.ForwardRefExoticComponent< @@ -21,24 +25,32 @@ type EditorComponent = React.ForwardRefExoticComponent< const Editor: EditorComponent = forwardRef( ( - { loading, type, code, theme = "light", fontSize, extensions, onChange }, + { + code, + onChange, + placeholder, + loading, + theme = "light", + style, + extensions, + setup, + }, ref ) => { - const placeholder = `Insert your ${type.toUpperCase()} code here`; - return ( <> {loading ? ( ) : ( )} diff --git a/src/types.ts b/src/types.ts index d379de9..7a2e738 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,5 @@ import React from "react"; +import { Extension } from "@uiw/react-codemirror"; export interface PlaygroundParameters { storyId: string; @@ -39,4 +40,4 @@ export type Code = { jsx: string; css: string }; export type Tab = "jsx" | "css"; -export type EditorTheme = "light" | "dark"; +export type EditorTheme = "light" | "dark" | Extension;