diff --git a/stories/typist-editor/decorators/typist-editor-decorator/typist-editor-decorator.tsx b/stories/typist-editor/decorators/typist-editor-decorator/typist-editor-decorator.tsx index 714a1a27..9eab77b8 100644 --- a/stories/typist-editor/decorators/typist-editor-decorator/typist-editor-decorator.tsx +++ b/stories/typist-editor/decorators/typist-editor-decorator/typist-editor-decorator.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useState } from 'react' +import { forwardRef, useCallback, useEffect, useState } from 'react' import { Box, Column, Columns } from '@doist/reactist' @@ -12,81 +12,95 @@ import type { PartialStoryFn } from '@storybook/csf' import type { ReactRenderer } from '@storybook/react' import type { CoreEditor, TypistEditorProps, TypistEditorRef, UpdateProps } from '../../../../src' -type TypistEditorArgs = Partial< +type TypistEditorPropsWithRef = Partial< TypistEditorProps & { ref: React.LegacyRef } > type TypistEditorDecoratorProps = { - Story: PartialStoryFn - args: TypistEditorArgs + Story: PartialStoryFn + args: TypistEditorProps withToolbar?: boolean renderBottomFunctions?: () => JSX.Element } -function TypistEditorDecorator({ - Story, - args, - withToolbar = false, - renderBottomFunctions, -}: TypistEditorDecoratorProps) { - const [typistEditor, setTypistEditor] = useState(null) - const [markdownOutput, setMarkdownOutput] = useState(args.content) +const TypistEditorDecorator = forwardRef( + function TypistEditorDecorator( + { Story, args, withToolbar = false, renderBottomFunctions }, + forwardedRef, + ) { + const [typistEditor, setTypistEditor] = useState(null) + const [markdownOutput, setMarkdownOutput] = useState(args.content) - const storyClassName = classNames('markdown-body', args.className) + const storyClassName = classNames('markdown-body', args.className) - const shouldRenderToolbar = typistEditor && withToolbar + const shouldRenderToolbar = typistEditor && withToolbar - const handleUpdate = useCallback((props: UpdateProps) => { - setMarkdownOutput(props.getMarkdown()) - }, []) + const handleUpdate = useCallback((props: UpdateProps) => { + setMarkdownOutput(props.getMarkdown()) + }, []) - useEffect( - function updateMarkdownOutputOnContentControlChange() { - setMarkdownOutput(args.content) - }, - [args.content], - ) + useEffect( + function updateMarkdownOutputOnContentControlChange() { + setMarkdownOutput(args.content) + }, + [args.content], + ) - return ( - - - -

Typist Editor

- {shouldRenderToolbar ? : null} - - { - setTypistEditor(instance?.getEditor() || null) - }, - }} - /> - -
- -

Markdown Output

- -
{markdownOutput}
+ return ( + + + +

Typist Editor

+ {shouldRenderToolbar ? : null} + + { + setTypistEditor(instance?.getEditor() || null) + + if (typeof forwardedRef === 'function') { + forwardedRef(instance) + } else if (forwardedRef) { + forwardedRef.current = instance + } + }, + }} + /> + +
+ +

Markdown Output

+ +
{markdownOutput}
+
+
+
+ {renderBottomFunctions ? ( + + {renderBottomFunctions()} -
-
- {renderBottomFunctions ? ( - - {renderBottomFunctions()} - - ) : null} -
- ) -} + ) : null} + + ) + }, +) export { TypistEditorDecorator } diff --git a/stories/typist-editor/plain-text-functions.stories.tsx b/stories/typist-editor/plain-text-functions.stories.tsx index dc7698a6..101cdd61 100644 --- a/stories/typist-editor/plain-text-functions.stories.tsx +++ b/stories/typist-editor/plain-text-functions.stories.tsx @@ -60,35 +60,32 @@ export const Commands: StoryObj = { .run() }, []) + function renderBottomFunctions() { + return ( + <> + + + + + + ) + } + return ( { - return ( - <> - - - - - - ) - }} + renderBottomFunctions={renderBottomFunctions} + ref={typistEditorRef} /> ) }, @@ -115,29 +112,32 @@ export const Helpers: StoryObj = { ) }, []) + function renderBottomFunctions() { + return ( + <> + + + + + ) + } + return ( { - return ( - <> - - - - - ) - }} + renderBottomFunctions={renderBottomFunctions} + ref={typistEditorRef} /> ) }, diff --git a/stories/typist-editor/rich-text-functions.stories.tsx b/stories/typist-editor/rich-text-functions.stories.tsx index bc2c21a3..76bc4646 100644 --- a/stories/typist-editor/rich-text-functions.stories.tsx +++ b/stories/typist-editor/rich-text-functions.stories.tsx @@ -60,35 +60,32 @@ export const Commands: StoryObj = { .run() }, []) + function renderBottomFunctions() { + return ( + <> + + + + + + ) + } + return ( { - return ( - <> - - - - - - ) - }} + renderBottomFunctions={renderBottomFunctions} + ref={typistEditorRef} /> ) }, @@ -115,29 +112,32 @@ export const Helpers: StoryObj = { ) }, []) + function renderBottomFunctions() { + return ( + <> + + + + + ) + } + return ( { - return ( - <> - - - - - ) - }} + renderBottomFunctions={renderBottomFunctions} + ref={typistEditorRef} /> ) }, diff --git a/stories/typist-editor/rich-text.stories.tsx b/stories/typist-editor/rich-text.stories.tsx index fb431404..9db021eb 100644 --- a/stories/typist-editor/rich-text.stories.tsx +++ b/stories/typist-editor/rich-text.stories.tsx @@ -139,9 +139,9 @@ export const Default: StoryObj = { args={{ ...context.args, extensions, - ref: typistEditorRef, }} withToolbar={true} + ref={typistEditorRef} /> ) },