Skip to content

Commit

Permalink
Merge pull request #13443 from ckeditor/ci/2675-slash-commands-rtl-fix
Browse files Browse the repository at this point in the history
Fix (mention): Mentions panel should be positioned left to the caret in editors with right–to–left UI language. Closes #13459.
  • Loading branch information
oleq authored Feb 15, 2023
2 parents 72edbbe + 7402b6a commit 45ac9e2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/ckeditor5-mention/src/mentionui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ export default class MentionUI extends Plugin {
const editing = editor.editing;
const domConverter = editing.view.domConverter;
const mapper = editing.mapper;
const uiLanguageDirection = editor.locale.uiLanguageDirection;

return {
target: () => {
Expand Down Expand Up @@ -573,15 +574,18 @@ export default class MentionUI extends Plugin {

return null;
},
positions: getBalloonPanelPositions( preferredPosition )
positions: getBalloonPanelPositions( preferredPosition, uiLanguageDirection )
};
}
}

/**
* Returns the balloon positions data callbacks.
*/
function getBalloonPanelPositions( preferredPosition: MentionsView['position'] ): PositionOptions['positions'] {
function getBalloonPanelPositions(
preferredPosition: MentionsView['position'],
uiLanguageDirection: string
): PositionOptions['positions'] {
const positions: Record<string, PositionOptions['positions'][0]> = {
// Positions the panel to the southeast of the caret rectangle.
'caret_se': ( targetRect: Rect ) => {
Expand Down Expand Up @@ -639,12 +643,17 @@ function getBalloonPanelPositions( preferredPosition: MentionsView['position'] )
];
}

// By default return all position callbacks.
return [
// By default, return all position callbacks ordered depending on the UI language direction.
return uiLanguageDirection !== 'rtl' ? [
positions.caret_se,
positions.caret_sw,
positions.caret_ne,
positions.caret_nw
] : [
positions.caret_sw,
positions.caret_se,
positions.caret_nw,
positions.caret_ne
];
}

Expand Down
65 changes: 65 additions & 0 deletions packages/ckeditor5-mention/tests/mentionui.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,71 @@ describe( 'MentionUI', () => {
expect( limiter() ).to.be.null;
} );
} );

describe( 'relation with the UI language direction of the editor', () => {
describe( 'for RTL languages', () => {
let contextualBaloonSpy;

beforeEach( async () => {
await editor.destroy();

return createClassicTestEditor( { ...staticConfig } )
.then( () => {
const contextualBalloon = editor.plugins.get( ContextualBalloon );
setData( model, '<paragraph>foo []</paragraph>' );
editor.locale.uiLanguageDirection = 'rtl';
contextualBaloonSpy = sinon.spy( contextualBalloon, 'add' );

model.change( writer => {
writer.insertText( '@', doc.selection.getFirstPosition() );
} );
} )
.then( waitForDebounce );
} );

it( 'should prefer the west position first (to the left of the caret)', () => {
const positionNames = contextualBaloonSpy.firstCall.firstArg.position.positions.map( ( { name } ) => name );

expect( positionNames ).to.have.ordered.members( [
'caret_sw',
'caret_se',
'caret_nw',
'caret_ne'
] );
} );
} );

describe( 'for ltr languages', () => {
let contextualBaloonSpy;

beforeEach( async () => {
await editor.destroy();

return createClassicTestEditor( { ...staticConfig } )
.then( () => {
const contextualBalloon = editor.plugins.get( ContextualBalloon );
setData( model, '<paragraph>foo []</paragraph>' );
contextualBaloonSpy = sinon.spy( contextualBalloon, 'add' );

model.change( writer => {
writer.insertText( '@', doc.selection.getFirstPosition() );
} );
} )
.then( waitForDebounce );
} );

it( 'should prefer the east position first (to the right of the caret)', () => {
const positionNames = contextualBaloonSpy.firstCall.firstArg.position.positions.map( ( { name } ) => name );

expect( positionNames ).to.have.ordered.members( [
'caret_se',
'caret_sw',
'caret_ne',
'caret_nw'
] );
} );
} );
} );
} );

describe( 'typing integration', () => {
Expand Down

0 comments on commit 45ac9e2

Please sign in to comment.