Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add support for Markdown styles expansion on pressing return
  • Loading branch information
thibaudcolas committed Mar 4, 2019
commit 01f802fb23cbfb4db21bd64115eed24e6ec4b1aa
36 changes: 31 additions & 5 deletions lib/components/DraftailEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,8 @@ class DraftailEditor extends Component<Props, State> {

/* :: handleReturn: (e: SyntheticKeyboardEvent<>) => 'not-handled' | 'handled'; */
handleReturn(e: SyntheticKeyboardEvent<>) {
const { enableLineBreak } = this.props;
const { enableLineBreak, inlineStyles } = this.props;
const { editorState } = this.state;
const contentState = editorState.getCurrentContent();
let ret = NOT_HANDLED;

// alt + enter opens links and other entities with a `url` property.
Expand All @@ -504,7 +503,8 @@ class DraftailEditor extends Component<Props, State> {
const entityKey = DraftUtils.getSelectionEntity(editorState);

if (entityKey) {
const entityData = contentState.getEntity(entityKey).getData();
const content = editorState.getCurrentContent();
const entityData = content.getEntity(entityKey).getData();

if (entityData.url) {
window.open(entityData.url);
Expand All @@ -518,7 +518,34 @@ class DraftailEditor extends Component<Props, State> {

let newState = editorState;

newState = DraftUtils.handleNewLine(newState, e);
const selection = newState.getSelection();
// Check whether we should apply a Markdown styles shortcut.
if (selection.isCollapsed()) {
const block = DraftUtils.getSelectedBlock(editorState);
const newStyle = behavior.handleBeforeInputInlineStyle(
block.getText(),
inlineStyles,
);

if (newStyle) {
newState = DraftUtils.applyMarkdownStyle(newState, newStyle, "");
}

const newLineState = DraftUtils.handleNewLine(newState, e);

// Manually handle the return if there is a style to apply.
if (!newLineState && newStyle) {
const content = newState.getCurrentContent();
const newContent = Modifier.splitBlock(content, selection);
newState = EditorState.push(newState, newContent, "split-block");
// Do not propagate the style from the last block.
newState = RichUtils.toggleInlineStyle(newState, newStyle.type);
} else {
newState = newLineState;
}
} else {
newState = DraftUtils.handleNewLine(newState, e);
}

if (newState && newState !== editorState) {
ret = HANDLED;
Expand Down Expand Up @@ -601,7 +628,6 @@ class DraftailEditor extends Component<Props, State> {
);
}

// TODO Do it on return as well.
const newStyle = behavior.handleBeforeInputInlineStyle(
beforeInput,
inlineStyles,
Expand Down