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 2 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
44 changes: 28 additions & 16 deletions blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ export default class Editable extends wp.element.Component {
event.preventDefault();
event.stopImmediatePropagation();
}

if ( event.keyCode === ENTER && event.shiftKey && this.props.inline ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we include some more inline documentation describing what these various inline conditions are achieving? Seems like they'll be difficult to revisit in the future otherwise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding some.

event.preventDefault();

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

onKeyUp( { keyCode } ) {
Expand All @@ -229,28 +237,32 @@ 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.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