/** @jsx h */ import { h, Component, RenderableProps } from 'preact'; import '@github/markdown-toolbar-element'; 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 { 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 boldLabel = 'Add bold text '; const headerLabel = 'Add header text'; const italicLabel = 'Add italic text '; const quoteLabel = 'Insert a quote'; const codeLabel = 'Insert a code'; const linkLabel = 'Add a link '; const unorderedListLabel = 'Add a bulleted list'; const orderedListLabel = 'Add a numbered list'; const attachImageLabel = 'Attach the image, drag & drop or paste from clipboard'; export default 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: RenderableProps) { return (
{this.props.allowUpload ? ( ) : null}
); } }