Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit d68b7d0

Browse files
authoredJun 14, 2018
Merge pull request #41 from ckeditor/t/ckeditor5/407
Feature: Creating a paragraph next to the selected widget is possible using the (<kbd>Shift</kbd>+)<kbd>Enter</kbd> key (see ckeditor/ckeditor5#407).
2 parents 543422b + 4dc91c4 commit d68b7d0

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
 

‎src/widget.js

+31
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ export default class Widget extends Plugin {
154154
wasHandled = this._handleArrowKeys( isForward );
155155
} else if ( isSelectAllKeyCode( domEventData ) ) {
156156
wasHandled = this._selectAllNestedEditableContent() || this._selectAllContent();
157+
} else if ( keyCode === keyCodes.enter ) {
158+
wasHandled = this._handleEnterKey( domEventData.shiftKey );
157159
}
158160

159161
if ( wasHandled ) {
@@ -207,6 +209,7 @@ export default class Widget extends Plugin {
207209
/**
208210
* Handles arrow keys.
209211
*
212+
* @private
210213
* @param {Boolean} isForward Set to true if arrow key should be handled in forward direction.
211214
* @returns {Boolean|undefined} Returns `true` if keys were handled correctly.
212215
*/
@@ -246,6 +249,34 @@ export default class Widget extends Plugin {
246249
}
247250
}
248251

252+
/**
253+
* Handles the enter key, giving users and access to positions in the editable directly before
254+
* (<kbd>Shift</kbd>+<kbd>Enter</kbd>) or after (<kbd>Enter</kbd>) the selected widget.
255+
* It improves the UX, mainly when the widget is the first or last child of the root editable
256+
* and there's no other way to type after or before it.
257+
*
258+
* @private
259+
* @param {Boolean} isBackwards Set to true if the new paragraph is to be inserted before
260+
* the selected widget (<kbd>Shift</kbd>+<kbd>Enter</kbd>).
261+
* @returns {Boolean|undefined} Returns `true` if keys were handled correctly.
262+
*/
263+
_handleEnterKey( isBackwards ) {
264+
const model = this.editor.model;
265+
const modelSelection = model.document.selection;
266+
const objectElement = modelSelection.getSelectedElement();
267+
268+
if ( objectElement && model.schema.isObject( objectElement ) ) {
269+
model.change( writer => {
270+
const paragraph = writer.createElement( 'paragraph' );
271+
272+
writer.insert( paragraph, objectElement, isBackwards ? 'before' : 'after' );
273+
writer.setSelection( paragraph, 'in' );
274+
} );
275+
276+
return true;
277+
}
278+
}
279+
249280
/**
250281
* Extends the {@link module:engine/model/selection~Selection document's selection} to span the entire
251282
* content of the nested editable if already anchored in one.

‎tests/widget.js

+44
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,50 @@ describe( 'Widget', () => {
681681
);
682682
} );
683683

684+
describe( 'enter', () => {
685+
test(
686+
'should insert a paragraph after the selected widget upon Enter',
687+
'[<widget></widget>]',
688+
keyCodes.enter,
689+
'<widget></widget><paragraph>[]</paragraph>'
690+
);
691+
692+
test(
693+
'should insert a paragraph before the selected widget upon Shift+Enter',
694+
'[<widget></widget>]',
695+
{ keyCode: keyCodes.enter, shiftKey: true },
696+
'<paragraph>[]</paragraph><widget></widget>'
697+
);
698+
699+
test(
700+
'should insert a paragraph when not a first-child of the root',
701+
'[<widget></widget>]<paragraph>foo</paragraph>',
702+
keyCodes.enter,
703+
'<widget></widget><paragraph>[]</paragraph><paragraph>foo</paragraph>'
704+
);
705+
706+
test(
707+
'should insert a paragraph when not a last-child of the root',
708+
'<paragraph>foo</paragraph>[<widget></widget>]',
709+
{ keyCode: keyCodes.enter, shiftKey: true },
710+
'<paragraph>foo</paragraph><paragraph>[]</paragraph><widget></widget>'
711+
);
712+
713+
test(
714+
'should insert a paragraph only when an entire widget is selected (#1)',
715+
'<widget><nested>[foo] bar</nested></widget>',
716+
keyCodes.enter,
717+
'<widget><nested>[] bar</nested></widget>'
718+
);
719+
720+
test(
721+
'should insert a paragraph only when an entire widget is selected (#2)',
722+
'<paragraph>f[oo</paragraph><widget></widget><paragraph>b]ar</paragraph>',
723+
keyCodes.enter,
724+
'<paragraph>f[]ar</paragraph>'
725+
);
726+
} );
727+
684728
function test( name, data, keyCodeOrMock, expected, expectedView ) {
685729
it( name, () => {
686730
const domEventDataMock = ( typeof keyCodeOrMock == 'object' ) ? keyCodeOrMock : {

0 commit comments

Comments
 (0)
This repository has been archived.