Markdown shortcuts for checkbox lists

This commit is contained in:
Tom Moor
2017-09-25 20:55:13 -07:00
parent d84b2847f2
commit 799ba2f5e3
3 changed files with 26 additions and 19 deletions

View File

@ -22,28 +22,26 @@ export default class TodoItem extends Component {
const { children, checked, readOnly } = this.props; const { children, checked, readOnly } = this.props;
return ( return (
<StyledLi contentEditable={false}> <ListItem>
<input <Checkbox
type="checkbox" type="checkbox"
checked={checked} checked={checked}
onChange={this.handleChange} onChange={this.handleChange}
disabled={readOnly} disabled={readOnly}
/> />
{' '} {children}
<span contentEditable={!readOnly} suppressContentEditableWarning> </ListItem>
{children}
</span>
</StyledLi>
); );
} }
} }
const StyledLi = styled.li` const ListItem = styled.li`
input { padding-left: 1.4em;
margin-right: 0.25em; position: relative;
} `;
&:last-child:focus { const Checkbox = styled.input`
outline: none; position: absolute;
} left: 0;
top: 0.4em;
`; `;

View File

@ -40,17 +40,24 @@ export default function MarkdownShortcuts() {
onSpace(ev: SyntheticEvent, state: Object) { onSpace(ev: SyntheticEvent, state: Object) {
if (state.isExpanded) return; if (state.isExpanded) return;
const { startBlock, startOffset } = state; const { startBlock, startOffset } = state;
const chars = startBlock.text.slice(0, startOffset).replace(/\s*/g, ''); const chars = startBlock.text.slice(0, startOffset).trim();
const type = this.getType(chars); const type = this.getType(chars);
if (type) { if (type) {
if (type === 'list-item' && startBlock.type === 'list-item') return; if (type === 'list-item' && startBlock.type === 'list-item') return;
ev.preventDefault(); ev.preventDefault();
const transform = state.transform().setBlock(type); let checked;
if (chars === '[x]') checked = true;
if (chars === '[ ]') checked = false;
const transform = state
.transform()
.setBlock({ type, data: { checked } });
if (type === 'list-item') { if (type === 'list-item') {
if (chars === '1.') { if (checked !== undefined) {
transform.wrapBlock('todo-list');
} else if (chars === '1.') {
transform.wrapBlock('ordered-list'); transform.wrapBlock('ordered-list');
} else { } else {
transform.wrapBlock('bulleted-list'); transform.wrapBlock('bulleted-list');
@ -234,6 +241,8 @@ export default function MarkdownShortcuts() {
case '-': case '-':
case '+': case '+':
case '1.': case '1.':
case '[ ]':
case '[x]':
return 'list-item'; return 'list-item';
case '>': case '>':
return 'block-quote'; return 'block-quote';

View File

@ -19,7 +19,7 @@ import type { Props, Node, Transform } from './types';
const TodoList = styled.ul` const TodoList = styled.ul`
list-style: none; list-style: none;
padding-left: 0; padding: 0 !important;
ul { ul {
padding-left: 1em; padding-left: 1em;