Cleaner API for BlockToolbar cursor positioning

This commit is contained in:
Tom Moor
2017-12-05 23:41:01 -08:00
parent 5c38ff9e63
commit b794a0cc70
2 changed files with 19 additions and 12 deletions

View File

@@ -24,6 +24,7 @@ export function splitAndInsertBlock(change: Change, options: Options) {
.call(changes.unwrapList);
}
if (wrapper) change.collapseToStartOfNextBlock();
change.insertBlock(type);
if (wrapper) change.wrapBlock(wrapper);

View File

@@ -64,19 +64,22 @@ class BlockToolbar extends Component {
);
}
insertBlock = (options: Options) => {
insertBlock = (
options: Options,
cursorPosition: 'before' | 'on' | 'after' = 'on'
) => {
const { editor } = this.props;
editor.change(change => {
change.call(splitAndInsertBlock, options);
change
.collapseToEndOf(this.props.node)
.removeNodeByKey(this.props.node.key)
.collapseToEnd()
.call(splitAndInsertBlock, options);
editor.value.document.nodes.forEach(node => {
if (node.type === 'block-toolbar') {
change.removeNodeByKey(node.key, undefined, { normalize: false });
}
});
change.focus();
if (cursorPosition === 'before') change.collapseToStartOfPreviousBlock();
if (cursorPosition === 'after') change.collapseToStartOfNextBlock();
return change.focus();
});
};
@@ -89,9 +92,12 @@ class BlockToolbar extends Component {
case 'code':
return this.insertBlock({ type });
case 'horizontal-rule':
return this.insertBlock({
type: { type: 'horizontal-rule', isVoid: true },
});
return this.insertBlock(
{
type: { type: 'horizontal-rule', isVoid: true },
},
'after'
);
case 'bulleted-list':
return this.insertBlock({
type: 'list-item',