@@ -154,6 +154,8 @@ export default class Widget extends Plugin {
154
154
wasHandled = this . _handleArrowKeys ( isForward ) ;
155
155
} else if ( isSelectAllKeyCode ( domEventData ) ) {
156
156
wasHandled = this . _selectAllNestedEditableContent ( ) || this . _selectAllContent ( ) ;
157
+ } else if ( keyCode === keyCodes . enter ) {
158
+ wasHandled = this . _handleEnterKey ( domEventData . shiftKey ) ;
157
159
}
158
160
159
161
if ( wasHandled ) {
@@ -207,6 +209,7 @@ export default class Widget extends Plugin {
207
209
/**
208
210
* Handles arrow keys.
209
211
*
212
+ * @private
210
213
* @param {Boolean } isForward Set to true if arrow key should be handled in forward direction.
211
214
* @returns {Boolean|undefined } Returns `true` if keys were handled correctly.
212
215
*/
@@ -246,6 +249,34 @@ export default class Widget extends Plugin {
246
249
}
247
250
}
248
251
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
+
249
280
/**
250
281
* Extends the {@link module:engine/model/selection~Selection document's selection} to span the entire
251
282
* content of the nested editable if already anchored in one.
0 commit comments