Commit 7e301ed7 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Update shared components in `MarkdownEditor`

parent 9c550f78
import { import {
Icon, ButtonBase,
IconButton, IconButton,
LabeledButton, LinkBase,
Link, } from '@hypothesis/frontend-shared/lib/next';
} from '@hypothesis/frontend-shared'; import {
EditorLatexIcon,
EditorQuoteIcon,
EditorTextBoldIcon,
EditorTextItalicIcon,
HelpIcon,
ImageIcon,
LinkIcon,
ListOrderedIcon,
ListUnorderedIcon,
} from '@hypothesis/frontend-shared/lib/next';
import classnames from 'classnames'; import classnames from 'classnames';
import { useEffect, useMemo, useRef, useState } from 'preact/hooks'; import { useEffect, useMemo, useRef, useState } from 'preact/hooks';
...@@ -24,7 +35,7 @@ import MarkdownView from './MarkdownView'; ...@@ -24,7 +35,7 @@ import MarkdownView from './MarkdownView';
*/ */
/** /**
* @typedef {import("@hypothesis/frontend-shared/lib/components/Link").LinkProps} LinkProps * @typedef {import('@hypothesis/frontend-shared/lib/types').IconComponent} IconComponent
* @typedef {import('preact').JSX.HTMLAttributes<HTMLTextAreaElement>} TextAreaAttributes * @typedef {import('preact').JSX.HTMLAttributes<HTMLTextAreaElement>} TextAreaAttributes
* @typedef {import('../markdown-commands').EditorState} EditorState * @typedef {import('../markdown-commands').EditorState} EditorState
*/ */
...@@ -130,33 +141,10 @@ function handleToolbarCommand(command, inputEl) { ...@@ -130,33 +141,10 @@ function handleToolbarCommand(command, inputEl) {
} }
} }
/**
* Style a Link to look like an IconButton, including touch sizing
* affordances.
*
* @param {Omit<LinkProps, 'children'> & { icon: string }} props
*/
function IconLink({ classes, icon, linkRef, ...restProps }) {
return (
<Link
classes={classnames(
'flex justify-center items-center',
'text-grey-7 hover:!text-grey-7',
'touch:h-touch-minimum touch:w-touch-minimum',
classes
)}
linkRef={linkRef}
{...restProps}
>
<Icon classes="text-tiny touch:text-base" name={icon} />
</Link>
);
}
/** /**
* @typedef ToolbarButtonProps * @typedef ToolbarButtonProps
* @prop {boolean} [disabled] * @prop {boolean} [disabled]
* @prop {string} [iconName] * @prop {IconComponent} [icon]
* @prop {string} [label] * @prop {string} [label]
* @prop {(e: MouseEvent) => void} onClick * @prop {(e: MouseEvent) => void} onClick
* @prop {string} [shortcutKey] * @prop {string} [shortcutKey]
...@@ -166,7 +154,7 @@ function IconLink({ classes, icon, linkRef, ...restProps }) { ...@@ -166,7 +154,7 @@ function IconLink({ classes, icon, linkRef, ...restProps }) {
/** @param {ToolbarButtonProps} props */ /** @param {ToolbarButtonProps} props */
function ToolbarButton({ function ToolbarButton({
disabled = false, disabled = false,
iconName = '', icon,
label, label,
onClick, onClick,
shortcutKey, shortcutKey,
...@@ -181,24 +169,19 @@ function ToolbarButton({ ...@@ -181,24 +169,19 @@ function ToolbarButton({
const buttonProps = { const buttonProps = {
disabled, disabled,
icon: iconName, icon,
onClick, onClick,
title: tooltip, title: tooltip,
}; };
if (label) { if (label) {
return ( return (
<LabeledButton <ButtonBase
classes={classnames( classes="text-grey-7 hover:text-grey-9 p-1.5"
'font-normal bg-transparent',
// TODO: Refactor shared button styles to reduce specificity and make
// this !important rule unnecessary
'hover:!bg-transparent'
)}
{...buttonProps} {...buttonProps}
> >
{label} {label}
</LabeledButton> </ButtonBase>
); );
} }
return ( return (
...@@ -266,72 +249,75 @@ function Toolbar({ isPreviewing, onCommand, onTogglePreview }) { ...@@ -266,72 +249,75 @@ function Toolbar({ isPreviewing, onCommand, onTogglePreview }) {
> >
<ToolbarButton <ToolbarButton
disabled={isPreviewing} disabled={isPreviewing}
iconName="format-bold" icon={EditorTextBoldIcon}
onClick={() => onCommand('bold')} onClick={() => onCommand('bold')}
shortcutKey={SHORTCUT_KEYS.bold} shortcutKey={SHORTCUT_KEYS.bold}
title="Bold" title="Bold"
/> />
<ToolbarButton <ToolbarButton
disabled={isPreviewing} disabled={isPreviewing}
iconName="format-italic" icon={EditorTextItalicIcon}
onClick={() => onCommand('italic')} onClick={() => onCommand('italic')}
shortcutKey={SHORTCUT_KEYS.italic} shortcutKey={SHORTCUT_KEYS.italic}
title="Italic" title="Italic"
/> />
<ToolbarButton <ToolbarButton
disabled={isPreviewing} disabled={isPreviewing}
iconName="format-quote" icon={EditorQuoteIcon}
onClick={() => onCommand('quote')} onClick={() => onCommand('quote')}
shortcutKey={SHORTCUT_KEYS.quote} shortcutKey={SHORTCUT_KEYS.quote}
title="Quote" title="Quote"
/> />
<ToolbarButton <ToolbarButton
disabled={isPreviewing} disabled={isPreviewing}
iconName="link" icon={LinkIcon}
onClick={() => onCommand('link')} onClick={() => onCommand('link')}
shortcutKey={SHORTCUT_KEYS.link} shortcutKey={SHORTCUT_KEYS.link}
title="Insert link" title="Insert link"
/> />
<ToolbarButton <ToolbarButton
disabled={isPreviewing} disabled={isPreviewing}
iconName="image" icon={ImageIcon}
onClick={() => onCommand('image')} onClick={() => onCommand('image')}
shortcutKey={SHORTCUT_KEYS.image} shortcutKey={SHORTCUT_KEYS.image}
title="Insert image" title="Insert image"
/> />
<ToolbarButton <ToolbarButton
disabled={isPreviewing} disabled={isPreviewing}
iconName="format-functions" icon={EditorLatexIcon}
onClick={() => onCommand('math')} onClick={() => onCommand('math')}
title="Insert math (LaTeX is supported)" title="Insert math (LaTeX is supported)"
/> />
<ToolbarButton <ToolbarButton
disabled={isPreviewing} disabled={isPreviewing}
iconName="format-list-numbered" icon={ListOrderedIcon}
onClick={() => onCommand('numlist')} onClick={() => onCommand('numlist')}
shortcutKey={SHORTCUT_KEYS.numlist} shortcutKey={SHORTCUT_KEYS.numlist}
title="Numbered list" title="Numbered list"
/> />
<ToolbarButton <ToolbarButton
disabled={isPreviewing} disabled={isPreviewing}
iconName="format-list-unordered" icon={ListUnorderedIcon}
onClick={() => onCommand('list')} onClick={() => onCommand('list')}
shortcutKey={SHORTCUT_KEYS.list} shortcutKey={SHORTCUT_KEYS.list}
title="Bulleted list" title="Bulleted list"
/> />
<div className="grow flex justify-end"> <div className="grow flex justify-end">
<IconLink <LinkBase
classes={classnames( classes={classnames(
// Adjust padding to make element a little taller than wide 'flex justify-center items-center',
// This matches ToolbarButton styling 'text-grey-7 hover:!text-grey-7',
'touch:h-touch-minimum touch:w-touch-minimum',
'px-2 py-2.5' 'px-2 py-2.5'
)} )}
href="https://web.hypothes.is/help/formatting-annotations-with-markdown/" href="https://web.hypothes.is/help/formatting-annotations-with-markdown/"
icon="help"
target="_blank" target="_blank"
title="Formatting help" title="Formatting help"
aria-label="Formatting help" aria-label="Formatting help"
/> >
<HelpIcon className="w-2.5 h-2.5" />
</LinkBase>
<ToolbarButton <ToolbarButton
label={isPreviewing ? 'Write' : 'Preview'} label={isPreviewing ? 'Write' : 'Preview'}
onClick={onTogglePreview} onClick={onTogglePreview}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment