Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
emitter.listenTo( view.document, 'keydown', ( evt, data ) => {
// This implementation works only for collapsed selection.
if ( !modelSelection.isCollapsed ) {
return;
}
// When user tries to expand the selection or jump over the whole word or to the beginning/end then
// two-steps movement is not necessary.
if ( data.shiftKey || data.altKey || data.ctrlKey ) {
return;
}
const arrowRightPressed = data.keyCode == keyCodes.arrowright;
const arrowLeftPressed = data.keyCode == keyCodes.arrowleft;
// When neither left or right arrow has been pressed then do noting.
if ( !arrowRightPressed && !arrowLeftPressed ) {
return;
}
const position = modelSelection.getFirstPosition();
const contentDirection = locale.contentLanguageDirection;
let isMovementHandled;
if ( ( contentDirection === 'ltr' && arrowRightPressed ) || ( contentDirection === 'rtl' && arrowLeftPressed ) ) {
isMovementHandled = twoStepCaretHandler.handleForwardMovement( position, data );
} else {
isMovementHandled = twoStepCaretHandler.handleBackwardMovement( position, data );
}
'[]foo',
{ keyCode: keyCodes.arrowright, ctrlKey: true },
'[]foo'
);
test(
'should work correctly with modifier key: right arrow + alt',
'[]foo',
{ keyCode: keyCodes.arrowright, altKey: true },
'[]foo'
);
test(
'should work correctly with modifier key: right arrow + shift',
'[]foo',
{ keyCode: keyCodes.arrowright, shiftKey: true },
'[]foo'
);
test(
'should work correctly with modifier key: down arrow + ctrl',
'[]foo',
{ keyCode: keyCodes.arrowdown, ctrlKey: true },
'[]foo'
);
test(
'should work correctly with modifier key: down arrow + alt',
'[]foo',
{ keyCode: keyCodes.arrowdown, altKey: true },
'[]foo'
);
it( 'should listen with the high+1 priority on view.document#keydown', () => {
const highestPrioritySpy = sinon.spy();
const highPrioritySpy = sinon.spy();
const normalPrioritySpy = sinon.spy();
setData( model, '<$text c="true">foo[]<$text a="true" b="true">bar' );
emitter.listenTo( view.document, 'keydown', highestPrioritySpy, { priority: 'highest' } );
emitter.listenTo( view.document, 'keydown', highPrioritySpy, { priority: 'high' } );
emitter.listenTo( view.document, 'keydown', normalPrioritySpy, { priority: 'normal' } );
fireKeyDownEvent( {
keyCode: keyCodes.arrowright,
preventDefault: preventDefaultSpy
} );
sinon.assert.callOrder(
highestPrioritySpy,
preventDefaultSpy );
sinon.assert.notCalled( highPrioritySpy );
sinon.assert.notCalled( normalPrioritySpy );
} );
describe( 'arrows', () => {
test(
'should move selection forward from selected object - right arrow',
'[]foo',
keyCodes.arrowright,
'[]foo'
);
test(
'should move selection forward from selected object - down arrow',
'[]foo',
keyCodes.arrowdown,
'[]foo'
);
test(
'should move selection backward from selected object - left arrow',
'foo[]',
keyCodes.arrowleft,
'foo[]'
);
'[]',
keyCodes.arrowleft,
'[]'
);
test(
'should move selection to previous widget - up arrow',
'[]',
keyCodes.arrowup,
'[]'
);
test(
'should do nothing on non-collapsed selection next to object - right arrow',
'ba[r]',
keyCodes.arrowright,
'ba[r]'
);
test(
'should do nothing on non-collapsed selection next to object - down arrow',
'ba[r]',
keyCodes.arrowdown,
'ba[r]'
);
test(
'should do nothing on non-collapsed selection next to object - left arrow',
'[b]ar',
keyCodes.arrowleft,
'[b]ar'
);
it( 'should do nothing when another key is pressed', () => {
setData( view, 'foo[]bar' );
view.forceRender();
viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowright, domTarget: view.domRoots.get( 'main' ) } );
const domSelection = document.getSelection();
expect( isInlineFiller( domSelection.anchorNode ) ).to.be.true;
expect( domSelection.anchorOffset ).to.equal( INLINE_FILLER_LENGTH );
expect( domSelection.isCollapsed ).to.be.true;
} );
function renderAndFireKeydownEvent( options ) {
view.forceRender();
const eventData = Object.assign( { keyCode: keyCodes.arrowright, domTarget: view.domRoots.get( 'main' ) }, options );
viewDocument.fire( 'keydown', eventData );
}
function isArrowKeyCode( keyCode ) {
return keyCode == keyCodes.arrowright ||
keyCode == keyCodes.arrowleft ||
keyCode == keyCodes.arrowup ||
keyCode == keyCodes.arrowdown;
}