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

Try splitting inline Editable #767

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
32 changes: 26 additions & 6 deletions blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { last, isEqual, capitalize, omitBy, forEach, merge } from 'lodash';
import { nodeListToReact } from 'dom-react';
import { Fill } from 'react-slot-fill';
import 'element-closest';
import tinymce from 'tinymce';

/**
* WordPress dependencies
Expand All @@ -19,9 +20,6 @@ import './style.scss';
import FormatToolbar from './format-toolbar';
import TinyMCE from './tinymce';

const KEYCODE_BACKSPACE = 8;
const KEYCODE_DELETE = 46;

const alignmentMap = {
alignleft: 'left',
alignright: 'right',
Expand Down Expand Up @@ -197,18 +195,40 @@ export default class Editable extends wp.element.Component {
}

onKeyDown( event ) {
const { BACKSPACE, DELETE, ENTER } = tinymce.util.VK;

if (
this.props.onMerge && (
( event.keyCode === KEYCODE_BACKSPACE && this.isStartOfEditor() ) ||
( event.keyCode === KEYCODE_DELETE && this.isEndOfEditor() )
( event.keyCode === BACKSPACE && this.isStartOfEditor() ) ||
( event.keyCode === DELETE && this.isEndOfEditor() )
)
) {
const forward = event.keyCode === KEYCODE_DELETE;
const forward = event.keyCode === DELETE;
this.onChange();
this.props.onMerge( forward );
event.preventDefault();
event.stopImmediatePropagation();
}

if ( this.props.inline && this.props.onSplit && event.keyCode === ENTER ) {
const isCollapsed = this.editor.selection.isCollapsed();
const node = this.editor.selection.getEnd();

if ( ! isCollapsed || node.nodeType !== 1 || node.nodeName !== 'BR' ) {
return;
}

event.preventDefault();
event.stopImmediatePropagation();

const childNodes = Array.from( this.editor.getBody().childNodes );
const splitIndex = childNodes.indexOf( node );
const before = nodeListToReact( childNodes.slice( 0, splitIndex ), createElement );
const after = nodeListToReact( childNodes.slice( splitIndex + 1 ), createElement );

this.setContent( before );
this.props.onSplit( before, after );
}
}

onNewBlock() {
Expand Down
8 changes: 7 additions & 1 deletion blocks/library/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ registerBlock( 'core/heading', {
};
},

edit( { attributes, setAttributes, focus, setFocus, mergeBlocks } ) {
edit( { attributes, setAttributes, focus, setFocus, mergeBlocks, insertBlockAfter } ) {
const { content, nodeName = 'H2' } = attributes;

return (
Expand All @@ -91,6 +91,12 @@ registerBlock( 'core/heading', {
onChange={ ( value ) => setAttributes( { content: value } ) }
onMerge={ mergeBlocks }
inline
onSplit={ ( before, after ) => {
setAttributes( { content: before } );
insertBlockAfter( createBlock( 'core/text', {
content: <p>{ after }</p>,
} ) );
} }
/>
);
},
Expand Down