-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP MarkdownToolbar extracted to seperate file
- Loading branch information
1 parent
8212ec3
commit 8dbd623
Showing
2 changed files
with
161 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import React from 'react' | ||
import IconButton from './theme/IconButton' | ||
import { | ||
HeadingIcon, | ||
BoldIcon, | ||
ItalicIcon, | ||
QuoteIcon, | ||
CodeIcon, | ||
LinkIcon, | ||
ListUnorderedIcon, | ||
ListOrderedIcon, | ||
TasklistIcon | ||
// TypographyIcon, | ||
// ChevronDownIcon, | ||
// ChevronUpIcon | ||
} from '@primer/octicons-react' | ||
import { ButtonToolbar, ButtonToolbarProps } from 'react-bootstrap' | ||
|
||
// TODO: Better name for utility | ||
const applyMarkdown = ( | ||
inputRef: React.RefObject<HTMLTextAreaElement>, | ||
left: string, | ||
right: string = '' | ||
): void => { | ||
// TODO: add expand single select to word functionality and support for one sided and multiline operations | ||
if (!inputRef.current) return | ||
const { value, selectionStart, selectionEnd } = inputRef.current | ||
|
||
const selection = value.slice(selectionStart, selectionEnd) | ||
|
||
const hasLeft = | ||
selectionStart >= left.length && | ||
value.slice(selectionStart - left.length, selectionStart) === left | ||
|
||
const hasRight = | ||
selectionEnd + right.length <= value.length && | ||
value.slice(selectionEnd, selectionEnd + right.length) === right | ||
|
||
inputRef.current.focus() | ||
|
||
if (hasLeft && hasRight) { | ||
// Modification detected around selection, remove it! | ||
|
||
// expand selection to include left and right | ||
inputRef.current.setSelectionRange( | ||
selectionStart - left.length, | ||
selectionEnd + right.length | ||
) | ||
// Replace with old selection | ||
document.execCommand('insertText', false, selection) | ||
|
||
// update selection with removed left | ||
inputRef.current.setSelectionRange( | ||
selectionStart - left.length, | ||
selectionEnd - left.length | ||
) | ||
} else { | ||
// No modification detected, apply modifiers | ||
document.execCommand('insertText', false, left + selection + right) | ||
|
||
/// update selection with added left | ||
inputRef.current.setSelectionRange( | ||
selectionStart + left.length, | ||
selectionEnd + left.length | ||
) | ||
} | ||
} | ||
|
||
// TODO: Add hotkey support! | ||
const buttons = [ | ||
{ | ||
tooltipTitle: 'Add header text', | ||
togglerParams: ['### ', ''], | ||
Icon: HeadingIcon | ||
}, | ||
{ | ||
tooltipTitle: 'Add bold text <ctrl+b>', | ||
togglerParams: ['**', '**'], | ||
Icon: BoldIcon | ||
}, | ||
{ | ||
tooltipTitle: 'Add italic text <ctrl+i>', | ||
togglerParams: ['_', '_'], | ||
Icon: ItalicIcon | ||
}, | ||
{ | ||
tooltipTitle: 'Insert a quote', | ||
togglerParams: ['\n> ', ''], | ||
Icon: QuoteIcon | ||
}, | ||
{ | ||
tooltipTitle: 'Insert code <ctrl+e>', | ||
togglerParams: ['`', '`'], | ||
Icon: CodeIcon | ||
}, | ||
{ | ||
tooltipTitle: 'Add a link <ctrl+k>', | ||
togglerParams: ['[', '](URL)'], | ||
Icon: LinkIcon | ||
}, | ||
{ | ||
tooltipTitle: 'Add a bulleted list', | ||
togglerParams: ['\n- ', ''], | ||
Icon: ListUnorderedIcon | ||
}, | ||
{ | ||
tooltipTitle: 'Add a numbered list', | ||
togglerParams: ['\n1. ', ''], | ||
Icon: ListOrderedIcon | ||
}, | ||
{ | ||
tooltipTitle: 'Add a task list', | ||
togglerParams: ['\n- [] ', ''], | ||
Icon: TasklistIcon | ||
} | ||
] | ||
|
||
type Props = { | ||
inputRef: React.RefObject<HTMLTextAreaElement> | ||
} | ||
|
||
const MarkdownToolbar: React.FC<Props & ButtonToolbarProps> = ({ | ||
inputRef, | ||
...buttonProps | ||
}) => { | ||
return ( | ||
<ButtonToolbar {...buttonProps}> | ||
{buttons.map(({ tooltipTitle, togglerParams, Icon }, i) => ( | ||
<IconButton | ||
key={i} | ||
className={i === 2 || i === 5 ? 'mr-3' : ''} | ||
tabIndex="-1" | ||
color="black" | ||
delay={{ show: 250 }} | ||
tooltipTitle={tooltipTitle} | ||
aria-label={tooltipTitle} | ||
placement="bottom-end" | ||
onClick={() => | ||
applyMarkdown(inputRef, ...(togglerParams as [string, string])) | ||
} | ||
icon={<Icon size={'small'} />} | ||
/> | ||
))} | ||
</ButtonToolbar> | ||
) | ||
} | ||
|
||
export default MarkdownToolbar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters