mentioncommand.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import MentionCommand from '../src/mentioncommand';
  8. describe( 'MentionCommand', () => {
  9. let editor, command, model, doc, selection;
  10. beforeEach( () => {
  11. return ModelTestEditor
  12. .create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. doc = model.document;
  17. selection = doc.selection;
  18. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  19. model.schema.register( 'x', { inheritAllFrom: '$block' } );
  20. model.schema.extend( '$text', { allowAttributes: [ 'mention' ] } );
  21. command = new MentionCommand( editor );
  22. } );
  23. } );
  24. afterEach( () => {
  25. command.destroy();
  26. return editor.destroy();
  27. } );
  28. describe( 'isEnabled', () => {
  29. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  30. setData( model, '<paragraph>f[]oo</paragraph>' );
  31. expect( command.isEnabled ).to.be.true;
  32. } );
  33. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  34. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  35. // Allow 'bold' on p>$text.
  36. if ( ctx.endsWith( 'x $text' ) && attributeName == 'mention' ) {
  37. return false;
  38. }
  39. } );
  40. setData( model, '<x>fo[]o</x>' );
  41. expect( command.isEnabled ).to.be.false;
  42. } );
  43. } );
  44. describe( 'execute()', () => {
  45. it( 'inserts mention object if mention was passed as string', () => {
  46. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  47. command.execute( {
  48. mention: '@John',
  49. range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
  50. } );
  51. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
  52. } );
  53. it( 'inserts mention object with data if mention was passed as object', () => {
  54. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  55. command.execute( {
  56. mention: { id: '@John', userId: '123456' },
  57. range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
  58. } );
  59. const mentionNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  60. assertMention( mentionNode, '@John' );
  61. expect( mentionNode.getAttribute( 'mention' ) ).to.have.property( 'userId', '123456' );
  62. } );
  63. it( 'inserts options.text as mention text', () => {
  64. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  65. command.execute( {
  66. mention: '@John',
  67. text: '@John Doe',
  68. range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
  69. } );
  70. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
  71. } );
  72. it( 'inserts mention attribute with passed marker for given range', () => {
  73. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  74. const end = model.createPositionAt( selection.focus );
  75. const start = end.getShiftedBy( -3 );
  76. command.execute( {
  77. mention: '#John',
  78. range: model.createRange( start, end ),
  79. } );
  80. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '#John' );
  81. } );
  82. it( 'inserts mention attribute at current selection if no range was passed', () => {
  83. setData( model, '<paragraph>foo []bar</paragraph>' );
  84. command.execute( {
  85. mention: '@John'
  86. } );
  87. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
  88. } );
  89. it( 'should set also other styles in inserted text', () => {
  90. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  91. setData( model, '<paragraph><$text bold="true">foo@John[]bar</$text></paragraph>' );
  92. command.execute( {
  93. mention: '@John',
  94. range: model.createRange( selection.focus.getShiftedBy( -5 ), selection.focus )
  95. } );
  96. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  97. assertMention( textNode, '@John' );
  98. expect( textNode.hasAttribute( 'bold' ) ).to.be.true;
  99. } );
  100. } );
  101. function assertMention( textNode, id ) {
  102. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  103. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_uid' );
  104. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', textNode.data );
  105. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', id );
  106. }
  107. } );