Skip to content

Commit

Permalink
Exclude trailing block for type toggling if at zero offset
Browse files Browse the repository at this point in the history
Resolves facebookarchive#86.

In cases where the user triple-clicks on a block, then toggles the block type, it feels weird to have the trailing block also change block type.

The `SelectionState` resulting from the triple-click is correct, but to improve the interaction here, let's discard the trailing block for this modifier.

Test Plan:

View rich.html, add three lines of text.

- Triple-click first line, toggle H1. Verify that only the first line toggles.
- Repeat with second line, verify that only the second line toggles.
- Repeat with third line (no trailing block), verify that only the third line toggles.

Also select across multiple blocks by dragging, verify that toggling block types properly affects all visibly selected blocks.
  • Loading branch information
Isaac Salier-Hellendag committed Feb 27, 2016
1 parent 4c7fa43 commit 309bd97
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/model/modifier/RichTextEditorUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var DraftModifier = require('DraftModifier');
var EditorState = require('EditorState');

var adjustBlockDepthForContentState = require('adjustBlockDepthForContentState');
var nullthrows = require('nullthrows');

import type ContentState from 'ContentState';
import type {DraftBlockType} from 'DraftBlockType';
Expand Down Expand Up @@ -290,6 +291,23 @@ var RichTextEditorUtil = {
var startKey = selection.getStartKey();
var endKey = selection.getEndKey();
var content = editorState.getCurrentContent();
var target = selection;

// Triple-click can lead to a selection that includes offset 0 of the
// following block. The `SelectionState` for this case is accurate, but
// we should avoid toggling block type for the trailing block because it
// is a confusing interaction.
if (startKey !== endKey && selection.getEndOffset() === 0) {
var blockBefore = nullthrows(content.getBlockBefore(endKey));
endKey = blockBefore.getKey();
target = target.merge({
anchorKey: startKey,
anchorOffset: selection.getStartOffset(),
focusKey: endKey,
focusOffset: blockBefore.getLength(),
isBackward: false,
});
}

var hasMedia = content.getBlockMap()
.skipWhile((_, k) => k !== startKey)
Expand All @@ -306,7 +324,7 @@ var RichTextEditorUtil = {

return EditorState.push(
editorState,
DraftModifier.setBlockType(content, selection, typeToSet),
DraftModifier.setBlockType(content, target, typeToSet),
'change-block-type'
);
},
Expand Down

0 comments on commit 309bd97

Please sign in to comment.