From b794a0cc70968bd2d5e310e73c328b19b76eb100 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 5 Dec 2017 23:41:01 -0800 Subject: [PATCH] Cleaner API for BlockToolbar cursor positioning --- app/components/Editor/changes.js | 1 + .../Editor/components/Toolbar/BlockToolbar.js | 30 +++++++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/app/components/Editor/changes.js b/app/components/Editor/changes.js index 9c44c28b6..01be8901d 100644 --- a/app/components/Editor/changes.js +++ b/app/components/Editor/changes.js @@ -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); diff --git a/app/components/Editor/components/Toolbar/BlockToolbar.js b/app/components/Editor/components/Toolbar/BlockToolbar.js index 355c81636..4d284cfc9 100644 --- a/app/components/Editor/components/Toolbar/BlockToolbar.js +++ b/app/components/Editor/components/Toolbar/BlockToolbar.js @@ -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',