Skip to content

Commit

Permalink
TMP.
Browse files Browse the repository at this point in the history
  • Loading branch information
oleq committed Jun 10, 2020
1 parent 9717d21 commit 35b6d08
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 16 deletions.
18 changes: 9 additions & 9 deletions packages/ckeditor5-table/src/tablekeyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class TableKeyboard extends Plugin {
// (like Widgets), which take over the keydown events with the "high" priority. Table navigation takes precedence
// over Widgets in that matter (widget arrow handler stops propagation of event if object element was selected
// but getNearestSelectionRange didn't returned any range).
this.listenTo( viewDocument, 'keydown', ( ...args ) => this._onKeydown( ...args ), { priority: priorities.get( 'high' ) + 1 } );
this.listenTo( viewDocument, 'keydown', ( ...args ) => this._onKeydown( ...args ), { priority: priorities.get( 'high' ) - 10 } );
}

/**
Expand Down Expand Up @@ -232,16 +232,16 @@ export default class TableKeyboard extends Plugin {

// If this is an object selected and it's not at the start or the end of cell content
// then let's allow widget handler to take care of it.
const objectElement = selection.getSelectedElement();
// const objectElement = selection.getSelectedElement();

if ( objectElement && model.schema.isObject( objectElement ) ) {
return false;
}
// if ( objectElement && model.schema.isObject( objectElement ) ) {
// return false;
// }

// If next to the selection there is an object then this is not the cell boundary (widget handler should handle this).
if ( this._isObjectElementNextToSelection( selection, isForward ) ) {
return false;
}
// // If next to the selection there is an object then this is not the cell boundary (widget handler should handle this).
// if ( this._isObjectElementNextToSelection( selection, isForward ) ) {
// return false;
// }

// If there isn't any $text position between cell edge and selection then we shall move the selection to next cell.
const textRange = this._findTextRangeFromSelection( cellRange, selection, isForward );
Expand Down
80 changes: 73 additions & 7 deletions packages/ckeditor5-widget/src/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import env from '@ckeditor/ckeditor5-utils/src/env';

import '../theme/widget.css';
import priorities from '@ckeditor/ckeditor5-utils/src/priorities';

/**
* The widget plugin. It enables base support for widgets.
Expand Down Expand Up @@ -100,7 +101,13 @@ export default class Widget extends Plugin {
this.listenTo( viewDocument, 'mousedown', ( ...args ) => this._onMousedown( ...args ) );

// Handle custom keydown behaviour.
this.listenTo( viewDocument, 'keydown', ( ...args ) => this._onKeydown( ...args ), { priority: 'high' } );
this.listenTo( viewDocument, 'keydown', ( ...args ) => {
this._onKeyDownSelectionChange( ...args );
}, { priority: 'high' } );

this.listenTo( viewDocument, 'keydown', ( ...args ) => {
this._onKeyDownPreventDefault( ...args );
}, { priority: priorities.get( 'high' ) - 20 } );

// Handle custom delete behaviour.
this.listenTo( viewDocument, 'delete', ( evt, data ) => {
Expand Down Expand Up @@ -171,19 +178,78 @@ export default class Widget extends Plugin {
* @param {module:utils/eventinfo~EventInfo} eventInfo
* @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
*/
_onKeydown( eventInfo, domEventData ) {
_onKeyDownSelectionChange( eventInfo, domEventData ) {
const keyCode = domEventData.keyCode;
let wasHandled = false;

// Checks if the keys were handled and then prevents the default event behaviour and stops
// the propagation.
if ( isArrowKeyCode( keyCode ) ) {
const isForward = isForwardArrowKeyCode( keyCode, this.editor.locale.contentLanguageDirection );
if ( !isArrowKeyCode( keyCode ) ) {
return;
}

const model = this.editor.model;
const schema = model.schema;
const modelSelection = model.document.selection;
const objectElement = modelSelection.getSelectedElement();
const isForward = isForwardArrowKeyCode( keyCode, this.editor.locale.contentLanguageDirection );

// If object element is selected.
if ( objectElement && schema.isObject( objectElement ) ) {
const position = isForward ? modelSelection.getLastPosition() : modelSelection.getFirstPosition();
const newRange = schema.getNearestSelectionRange( position, isForward ? 'forward' : 'backward' );

if ( newRange ) {
model.change( writer => {
writer.setSelection( newRange );
} );

wasHandled = this._handleArrowKeys( isForward );
domEventData.preventDefault();
eventInfo.stop();

return;
}
}

if ( wasHandled ) {
// If selection is next to object element.
// Return if not collapsed.
if ( !modelSelection.isCollapsed ) {
return;
}

const objectElement2 = this._getObjectElementNextToSelection( isForward );

if ( !!objectElement2 && schema.isObject( objectElement2 ) ) {
this._setSelectionOverElement( objectElement2 );

domEventData.preventDefault();
eventInfo.stop();
}
}

/**
* TODO
*
* @private
* @param {module:utils/eventinfo~EventInfo} eventInfo
* @param {module:engine/view/observer/domeventdata~DomEventData} domEventData
*/
_onKeyDownPreventDefault( eventInfo, domEventData ) {
const keyCode = domEventData.keyCode;

// Checks if the keys were handled and then prevents the default event behaviour and stops
// the propagation.
if ( !isArrowKeyCode( keyCode ) ) {
return;
}

const model = this.editor.model;
const schema = model.schema;
const modelDocument = model.document;
const modelSelection = modelDocument.selection;
const objectElement = modelSelection.getSelectedElement();

// If object element is selected.
if ( objectElement && schema.isObject( objectElement ) ) {
domEventData.preventDefault();
eventInfo.stop();
}
Expand Down

0 comments on commit 35b6d08

Please sign in to comment.