diff --git a/README.md b/README.md index 486c564..3c6f45b 100644 --- a/README.md +++ b/README.md @@ -72,20 +72,37 @@ const config: StorybookConfig = { Use the `Playground` component in [MDX format](https://storybook.js.org/docs/writing-docs/mdx). -```jsx +```mdx // MyComponent.stories.mdx +import { Playground } from 'storybook-addon-code-editor' + + +``` + +
+Example with a wrapping component and modified editor options + +```mdx import { Playground } from 'storybook-addon-code-editor'; - + ( +
{props.children}
+ )} + code="export default () =>

Hello

;" +/> ``` +
+
More advanced example -```jsx +```mdx // MyComponent.stories.mdx import { Playground } from 'storybook-addon-code-editor'; -import * as MyLibrary from './index'; +import \* as MyLibrary from './index'; import storyCode from './MyStory.source.tsx?raw'; // TypeScript might complain about not finding this import or @@ -106,10 +123,10 @@ import MyLibraryTypes from '../dist/types.d.ts?raw'; monaco.editor.setTheme('vs-dark'); monaco.languages.typescript.typescriptDefaults.addExtraLib( MyLibraryTypes, - 'file:///node_modules/my-library/index.d.ts' + 'file:///node_modules/my-library/index.d.ts', ); }} -/>; +/> ```
diff --git a/example/src/intro.mdx b/example/src/intro.mdx index ca5a924..df89819 100644 --- a/example/src/intro.mdx +++ b/example/src/intro.mdx @@ -99,6 +99,29 @@ ${code} --- +{(() => { + const code = `export default () => ( +

+ Wrapped in a custom component with editor options (no minimap) +

+);`; + return ( + (
{props.children}
)} + code={` +/\* MDX: +import { Playground } from 'storybook-addon-code-editor'; + +*/ +${code} + `.trim()} + /> + ); +})()} + +--- + {(() => { const code = ` /\* MDX: diff --git a/src/Editor/Editor.tsx b/src/Editor/Editor.tsx index 7e5091a..5ef9086 100644 --- a/src/Editor/Editor.tsx +++ b/src/Editor/Editor.tsx @@ -7,6 +7,8 @@ import { getMonacoSetup } from './setupMonaco'; let monacoPromise: Promise | undefined; +export type EditorOptions = Monaco.editor.IEditorOptions; + function loadMonacoEditor() { const monacoSetup = getMonacoSetup(); @@ -38,7 +40,12 @@ function loadMonacoEditor() { let fileCount = 1; -function createEditor(monaco: typeof Monaco, code: string, container: HTMLElement) { +function createEditor( + monaco: typeof Monaco, + code: string, + container: HTMLElement, + editorOptions?: EditorOptions, +) { const uri = monaco.Uri.parse(`file:///index${fileCount++}.tsx`); return monaco.editor.create(container, { @@ -47,6 +54,7 @@ function createEditor(monaco: typeof Monaco, code: string, container: HTMLElemen model: monaco.editor.createModel(code, 'typescript', uri), overflowWidgetsDomNode: getMonacoOverflowContainer('monacoOverflowContainer'), tabSize: 2, + ...editorOptions, }); } @@ -55,6 +63,7 @@ interface EditorProps { value: string; modifyEditor?: (monaco: typeof Monaco, editor: Monaco.editor.IStandaloneCodeEditor) => any; parentSize?: string; + editorOptions?: EditorOptions; } interface EditorState { @@ -76,7 +85,7 @@ export default function Editor(props: EditorProps) { Promise.all([containerPromise, loadMonacoEditor()]).then(([editorContainer, monaco]) => { stateRef.monaco = monaco; - stateRef.editor = createEditor(monaco, props.value, editorContainer); + stateRef.editor = createEditor(monaco, props.value, editorContainer, props.editorOptions); stateRef.editor.onDidChangeModelContent(() => { const currentValue = stateRef.editor?.getValue(); diff --git a/src/index.tsx b/src/index.tsx index d4781b3..2c72122 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; import { createStore } from './createStore'; -import Editor from './Editor/Editor'; +import Editor, { EditorOptions } from './Editor/Editor'; import ErrorBoundary from './ErrorBoundary'; import Preview from './Preview'; export { setupMonaco } from './Editor/setupMonaco'; @@ -9,6 +9,7 @@ export interface StoryState { code: string; availableImports?: Record>; modifyEditor?: React.ComponentProps['modifyEditor']; + editorOptions?: EditorOptions; } const store = createStore(); @@ -80,8 +81,14 @@ export function Playground({ code, height = '200px', id, + wrappingComponent: WrappingComponent, ...editorProps -}: Partial & { height?: string; id?: string }) { +}: Partial & { + height?: string; + id?: string; + wrappingComponent?: React.JSXElementConstructor<{ children: React.ReactNode }>; +}) { + console.log(editorProps); let initialCode = code ?? ''; if (id !== undefined) { savedCode[id] ??= initialCode; @@ -93,11 +100,15 @@ export function Playground({ ? currentCode : "import * as React from 'react';" + currentCode; + const preview = ( + + ); + return (
- + {WrappingComponent ? {preview} : preview}