/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. * * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ /* * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import React, { useState, useMemo, Fragment, ReactChild, forwardRef, } from 'react'; import { OuiLoadingSpinner } from '../loading'; import { OuiButton, OuiButtonEmpty, OuiButtonIcon } from '../button'; import { OuiTitle } from '../title'; import { OuiModal, OuiModalBody, OuiModalFooter, OuiModalHeader, } from '../modal'; import { OuiI18n, useOuiI18n } from '../i18n'; import { OuiMarkdownDropHandler, OuiMarkdownEditorUiPlugin, OuiMarkdownParseError, } from './markdown_types'; import { OuiPopover, OuiPopoverTitle } from '../popover'; import { OuiText } from '../text'; import { OuiSpacer } from '../spacer'; // @ts-ignore a react svg import MarkdownLogo from './icons/markdown_logo'; import { OuiHorizontalRule } from '../horizontal_rule'; import { OuiToolTip } from '../tool_tip'; interface OuiMarkdownEditorFooterProps { uiPlugins: OuiMarkdownEditorUiPlugin[]; isUploadingFiles: boolean; openFiles: () => void; errors: OuiMarkdownParseError[]; hasUnacceptedItems: boolean; dropHandlers: OuiMarkdownDropHandler[]; } export const OuiMarkdownEditorFooter = forwardRef< HTMLDivElement, OuiMarkdownEditorFooterProps >((props, ref) => { const { uiPlugins, isUploadingFiles, openFiles, errors, hasUnacceptedItems, dropHandlers, } = props; const [isShowingHelp, setIsShowingHelp] = useState(false); const [isPopoverOpen, setIsPopoverOpen] = useState(false); const onButtonClick = () => setIsPopoverOpen((isPopoverOpen) => !isPopoverOpen); const closePopover = () => setIsPopoverOpen(false); let uploadButton; const supportedFileTypes = useMemo( () => dropHandlers .map(({ supportedFiles }) => supportedFiles.join(', ')) .sort() .join(', '), [dropHandlers] ); const ariaLabels = { uploadingFiles: useOuiI18n( 'ouiMarkdownEditorFooter.uploadingFiles', 'Click to upload files' ), openUploadModal: useOuiI18n( 'ouiMarkdownEditorFooter.openUploadModal', 'Open upload files modal' ), unsupportedFileType: useOuiI18n( 'ouiMarkdownEditorFooter.unsupportedFileType', 'File type not supported' ), supportedFileTypes: useOuiI18n( 'ouiMarkdownEditorFooter.supportedFileTypes', 'Supported files: {supportedFileTypes}', { supportedFileTypes } ), showSyntaxErrors: useOuiI18n( 'ouiMarkdownEditorFooter.showSyntaxErrors', 'Show errors' ), showMarkdownHelp: useOuiI18n( 'ouiMarkdownEditorFooter.showMarkdownHelp', 'Show markdown help' ), }; if (isUploadingFiles) { uploadButton = ( ); } else if (dropHandlers.length > 0 && hasUnacceptedItems) { uploadButton = ( {ariaLabels.unsupportedFileType} ); } else if (dropHandlers.length > 0) { uploadButton = ( ); } let errorsButton; if (errors && errors.length) { errorsButton = ( {errors.length} } isOpen={isPopoverOpen} closePopover={closePopover} panelPaddingSize="s" anchorPosition="upCenter">
{errors.map((message, idx) => ( {message.toString()} ))}
); } return (
{uploadButton} {errorsButton}
setIsShowingHelp(!isShowingHelp)} /> {isShowingHelp && ( setIsShowingHelp(false)}>

{([descriptionPrefix, descriptionSuffix]: ReactChild[]) => (

{descriptionPrefix}{' '} Github flavored markdown . {descriptionSuffix}

)}
{uiPlugins .filter(({ helpText }) => !!helpText) .map(({ name, helpText }) => (

{name}

{helpText}
))}
setIsShowingHelp(false)} fill>
)}
); }); OuiMarkdownEditorFooter.displayName = 'OuiMarkdownEditorFooter';