import '@ungap/custom-elements'; import '@github/markdown-toolbar-element'; import { h, Component } from 'preact'; import { defineMessages, IntlShape } from 'react-intl'; // TODO: Use SVGR import { BoldIcon } from './markdown-toolbar-icons/bold-icon'; import { HeaderIcon } from './markdown-toolbar-icons/header-icon'; import { ItalicIcon } from './markdown-toolbar-icons/italic-icon'; import { QuoteIcon } from './markdown-toolbar-icons/quote-icon'; import { CodeIcon } from './markdown-toolbar-icons/code-icon'; import { LinkIcon } from './markdown-toolbar-icons/link-icon'; import { ImageIcon } from './markdown-toolbar-icons/image-icon'; import { UnorderedListIcon } from './markdown-toolbar-icons/unordered-list-icon'; import { OrderedListIcon } from './markdown-toolbar-icons/ordered-list-icon'; interface Props { intl: IntlShape; textareaId: string; uploadImages: (files: File[]) => Promise; allowUpload: boolean; } interface FileEventTarget extends EventTarget { readonly files: FileList | null; value: string | null; } interface FileInputEvent extends Event { readonly currentTarget: FileEventTarget | null; } const messages = defineMessages({ bold: { id: 'toolbar.bold', defaultMessage: 'Add bold text {shortcut}', }, header: { id: 'toolbar.header', defaultMessage: 'Add header text', }, italic: { id: 'toolbar.italic', defaultMessage: 'Add italic text {shortcut}', }, quote: { id: 'toolbar.quote', defaultMessage: 'Insert a quote', }, code: { id: 'toolbar.code', defaultMessage: 'Insert a code', }, link: { id: 'toolbar.link', defaultMessage: 'Add a link {shortcut}', }, unorderedList: { id: 'toolbar.unordered-list', defaultMessage: 'Add a bulleted list', }, orderedList: { id: 'toolbar.ordered-list', defaultMessage: 'Add a numbered list', }, attachImage: { id: 'toolbar.attach-image', defaultMessage: 'Attach the image, drag & drop or paste from clipboard', }, }); export class MarkdownToolbar extends Component { constructor(props: Props) { super(props); this.uploadImages = this.uploadImages.bind(this); } async uploadImages(e: Event) { const currentTarget = (e as FileInputEvent).currentTarget; if (!(this.props.allowUpload && currentTarget && currentTarget.files && currentTarget.files.length !== 0)) return; const files = Array.from(currentTarget.files); await this.props.uploadImages(files); currentTarget.value = null; } render(props: Props) { const intl = props.intl; const boldLabel = intl.formatMessage(messages.bold, { shortcut: '' }); const headerLabel = intl.formatMessage(messages.header); const italicLabel = intl.formatMessage(messages.italic, { shortcut: '' }); const quoteLabel = intl.formatMessage(messages.quote); const codeLabel = intl.formatMessage(messages.code); const linkLabel = intl.formatMessage(messages.link, { shortcut: '' }); const unorderedListLabel = intl.formatMessage(messages.unorderedList); const orderedListLabel = intl.formatMessage(messages.orderedList); const attachImageLabel = intl.formatMessage(messages.attachImage); return (
{this.props.allowUpload ? ( ) : null}
); } }