Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editable: Fix splitting inline Editables using shift+enter #1299

Merged
merged 5 commits into from
Jun 20, 2017
Merged
Changes from all commits
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
47 changes: 31 additions & 16 deletions blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ export default class Editable extends wp.element.Component {
event.preventDefault();
event.stopImmediatePropagation();
}

// If we click shift+Enter on inline Editables, we avoid creating two contenteditables
// We also split the content and call the onSplit prop if provided.
if ( event.keyCode === ENTER && event.shiftKey && this.props.inline ) {
event.preventDefault();

if ( this.props.onSplit ) {
this.splitContent();
}
}
}

onKeyUp( { keyCode } ) {
Expand All @@ -229,28 +239,33 @@ export default class Editable extends wp.element.Component {
return;
}

const { dom } = this.editor;
const rootNode = this.editor.getBody();
const beforeRange = dom.createRng();
const afterRange = dom.createRng();
this.editor.dom.remove( prevNode );
this.editor.dom.remove( endNode );
this.splitContent();
}
}

dom.remove( prevNode );
splitContent() {
const { dom } = this.editor;
const rootNode = this.editor.getBody();
const beforeRange = dom.createRng();
const afterRange = dom.createRng();
const selectionRange = this.editor.selection.getRng();

beforeRange.setStart( rootNode, 0 );
beforeRange.setEnd( endNode.parentNode, dom.nodeIndex( endNode ) );
beforeRange.setStart( rootNode, 0 );
beforeRange.setEnd( selectionRange.startContainer, selectionRange.startOffset );

afterRange.setStart( endNode.parentNode, dom.nodeIndex( endNode ) + 1 );
afterRange.setEnd( rootNode, dom.nodeIndex( rootNode.lastChild ) + 1 );
afterRange.setStart( selectionRange.endContainer, selectionRange.endOffset );
afterRange.setEnd( rootNode, dom.nodeIndex( rootNode.lastChild ) + 1 );

const beforeFragment = beforeRange.extractContents();
const afterFragment = afterRange.extractContents();
const beforeFragment = beforeRange.extractContents();
const afterFragment = afterRange.extractContents();

const beforeElement = nodeListToReact( beforeFragment.childNodes, createElement );
const afterElement = nodeListToReact( afterFragment.childNodes, createElement );
const beforeElement = nodeListToReact( beforeFragment.childNodes, createElement );
const afterElement = nodeListToReact( afterFragment.childNodes, createElement );

this.setContent( beforeElement );
this.props.onSplit( beforeElement, afterElement );
}
this.setContent( beforeElement );
this.props.onSplit( beforeElement, afterElement );
}

onNewBlock() {
Expand Down