8
0

mentioncommand.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. marker: '@',
  49. mention: '@John',
  50. range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
  51. } );
  52. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
  53. } );
  54. it( 'inserts mention object with data if mention was passed as object', () => {
  55. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  56. command.execute( {
  57. marker: '@',
  58. mention: { id: '@John', userId: '123456' },
  59. range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
  60. } );
  61. const mentionNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  62. assertMention( mentionNode, '@John' );
  63. expect( mentionNode.getAttribute( 'mention' ) ).to.have.property( 'userId', '123456' );
  64. } );
  65. it( 'inserts options.text as mention text', () => {
  66. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  67. command.execute( {
  68. marker: '@',
  69. mention: '@John',
  70. text: '@John Doe',
  71. range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
  72. } );
  73. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
  74. } );
  75. it( 'inserts mention attribute with passed marker for given range', () => {
  76. setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
  77. const end = model.createPositionAt( selection.focus );
  78. const start = end.getShiftedBy( -3 );
  79. command.execute( {
  80. marker: '#',
  81. mention: '#John',
  82. range: model.createRange( start, end ),
  83. } );
  84. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '#John' );
  85. } );
  86. it( 'inserts mention attribute at current selection if no range was passed', () => {
  87. setData( model, '<paragraph>foo []bar</paragraph>' );
  88. command.execute( {
  89. marker: '@',
  90. mention: '@John'
  91. } );
  92. assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
  93. } );
  94. it( 'should set also other styles in inserted text', () => {
  95. model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  96. setData( model, '<paragraph><$text bold="true">foo@John[]bar</$text></paragraph>' );
  97. command.execute( {
  98. marker: '@',
  99. mention: '@John',
  100. range: model.createRange( selection.focus.getShiftedBy( -5 ), selection.focus )
  101. } );
  102. const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
  103. assertMention( textNode, '@John' );
  104. expect( textNode.hasAttribute( 'bold' ) ).to.be.true;
  105. } );
  106. } );
  107. function assertMention( textNode, id ) {
  108. expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
  109. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_uid' );
  110. expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', textNode.data );
  111. expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', id );
  112. }
  113. } );