Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/markdown editor cleanup #3830

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scripts/babel/proptypes-from-ts-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ function resolveArrayTypeToPropTypes(node, state) {
* - Arrays
* - MouseEventHandler is interpretted as functions
* - ExclusiveUnion custom type
* - OneOf custom type
* - defined types/interfaces (found during initial program body parsing)
* Returns `null` for unresolvable types
* @param node
Expand Down Expand Up @@ -342,6 +343,13 @@ function resolveIdentifierToPropTypes(node, state) {
return propTypes;
}

if (identifier.name === 'OneOf') {
// the second type parameter is ignorable as it is a subset of the first,
// and the OneOf operation cannot be well-described by proptypes
const [sourceTypes] = node.typeParameters.params;
return getPropTypesForNode(sourceTypes, true, state);
}

// Lookup this identifier from types/interfaces defined in code
const identifierDefinition = typeDefinitions[identifier.name];

Expand Down
2 changes: 2 additions & 0 deletions src-docs/src/views/markdown_editor/mardown_editor_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const markdownEditorErrorsHtml = renderToHtml(MarkdownEditorErrors);

export const MarkdownEditorExample = {
title: 'Markdown editor',
beta: true,
isNew: true,
intro: (
<Fragment>
<EuiText>
Expand Down
2 changes: 2 additions & 0 deletions src-docs/src/views/markdown_editor/mardown_format_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const markdownFormatSinkHtml = renderToHtml(MarkdownFormatSink);

export const MarkdownFormatExample = {
title: 'Markdown format',
beta: true,
isNew: true,
intro: (
<Fragment>
<EuiText>
Expand Down
2 changes: 2 additions & 0 deletions src-docs/src/views/markdown_editor/markdown_plugin_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ const uiPluginConcepts = [

export const MarkdownPluginExample = {
title: 'Markdown plugins',
beta: true,
isNew: true,
intro: (
<Fragment>
<EuiText>
Expand Down
41 changes: 29 additions & 12 deletions src/components/markdown_editor/markdown_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@

import React, {
createElement,
FunctionComponent,
HTMLAttributes,
useEffect,
useImperativeHandle,
useMemo,
useState,
forwardRef,
useCallback,
useRef,
} from 'react';
Expand Down Expand Up @@ -60,32 +58,38 @@ import {

type CommonMarkdownEditorProps = HTMLAttributes<HTMLDivElement> &
CommonProps & {
/** A unique ID to attach to the textarea. If one isn't provided, a random one
/** aria-label OR aria-labelledby must be set */
'aria-label'?: string;

/** aria-label OR aria-labelledby must be set */
'aria-labelledby'?: string;

/** a unique ID to attach to the textarea. If one isn't provided, a random one
* will be generated */
editorId?: string;

/** A markdown content */
value: string;

/** Callback function when markdown content is modified */
/** callback function when markdown content is modified */
onChange: (value: string) => void;

/** The height of the content/preview area */
/** height of the content/preview area */
height?: number;

/** array of unified plugins to parse content into an AST */
/** plugins to identify new syntax and parse it into an AST node */
parsingPluginList?: PluggableList;

/** array of unified plugins to convert the AST into a ReactNode */
/** plugins to process the markdown AST nodes into a React nodes */
processingPluginList?: PluggableList;

/** array of toolbar plugins **/
/** defines UI for plugins' buttons in the toolbar as well as any modals or extra UI that provides content to the editor */
uiPlugins?: EuiMarkdownEditorUiPlugin[];

/** Errors to bubble up */
/** errors to bubble up */
errors?: EuiMarkdownParseError[];

/** callback triggered when parsing results are available **/
/** callback triggered when parsing results are available */
onParse?: (
error: EuiMarkdownParseError | null,
data: {
Expand All @@ -94,14 +98,26 @@ type CommonMarkdownEditorProps = HTMLAttributes<HTMLDivElement> &
}
) => void;

/** initial display mode for the editor */
initialViewMode?: MARKDOWN_MODE;

/** array defining any drag&drop handlers */
dropHandlers?: EuiMarkdownDropHandler[];
};
export type EuiMarkdownEditorProps = OneOf<
CommonMarkdownEditorProps,
'aria-label' | 'aria-labelledby'
>;

export const EuiMarkdownEditor: FunctionComponent<EuiMarkdownEditorProps> = forwardRef(
interface EuiMarkdownEditorRef {
textarea: HTMLTextAreaElement | null;
replaceNode: ContextShape['replaceNode'];
}

export const EuiMarkdownEditor = React.forwardRef<
EuiMarkdownEditorRef,
EuiMarkdownEditorProps
>(
(
{
className,
Expand All @@ -116,12 +132,13 @@ export const EuiMarkdownEditor: FunctionComponent<EuiMarkdownEditorProps> = forw
errors = [],
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledBy,
initialViewMode = MODE_EDITING,
dropHandlers = [],
...rest
},
ref
) => {
const [viewMode, setViewMode] = useState<MARKDOWN_MODE>(MODE_EDITING);
const [viewMode, setViewMode] = useState<MARKDOWN_MODE>(initialViewMode);
const editorId = useMemo(() => _editorId || htmlIdGenerator()(), [
_editorId,
]);
Expand Down
1 change: 1 addition & 0 deletions src/components/markdown_editor/markdown_editor_footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const EuiMarkdownEditorFooter: FunctionComponent<EuiMarkdownEditorFooterP
<EuiToolTip
content={`Supported files: ${dropHandlers
.map(({ supportedFiles }) => supportedFiles.join(', '))
.sort()
.join(', ')}`}>
<EuiButtonEmpty
className="euiMarkdownEditorFooter__uploadError"
Expand Down
4 changes: 2 additions & 2 deletions src/components/markdown_editor/markdown_modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

export const MODE_EDITING = 'editing';
export const MODE_VIEWING = 'viewing';
export const MODE_EDITING = 'editing' as const;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise TS see's the MARKDOWN_MODE union type as string 😞

export const MODE_VIEWING = 'viewing' as const;

export type MARKDOWN_MODE = typeof MODE_EDITING | typeof MODE_VIEWING;