mention-customization.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals ClassicEditor, console, window, document, setTimeout */
  6. import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config';
  7. ClassicEditor
  8. .create( document.querySelector( '#snippet-mention-customization' ), {
  9. cloudServices: CS_CONFIG,
  10. extraPlugins: [ MentionCustomization ],
  11. toolbar: {
  12. items: [
  13. 'heading', '|', 'bold', 'italic', '|', 'undo', 'redo'
  14. ],
  15. viewportTopOffset: window.getViewportTopOffsetConfig(),
  16. },
  17. mention: {
  18. feeds: [
  19. {
  20. marker: '@',
  21. feed: getFeedItems,
  22. itemRenderer: customItemRenderer,
  23. minimumCharacters: 1
  24. }
  25. ]
  26. }
  27. } )
  28. .then( editor => {
  29. window.editor = editor;
  30. } )
  31. .catch( err => {
  32. console.error( err.stack );
  33. } );
  34. function MentionCustomization( editor ) {
  35. // The upcast converter will convert <a class="mention" href="" data-user-id="">
  36. // elements to the model 'mention' attribute.
  37. editor.conversion.for( 'upcast' ).elementToAttribute( {
  38. view: {
  39. name: 'a',
  40. key: 'data-mention',
  41. classes: 'mention',
  42. attributes: {
  43. href: true,
  44. 'data-user-id': true
  45. }
  46. },
  47. model: {
  48. key: 'mention',
  49. value: viewItem => {
  50. // The mention feature expects that the mention attribute value
  51. // in the model is a plain object with set of additional attributes.
  52. // In order to create proper object use `toMentionAttribute` helper method:
  53. const mentionAttribute = editor.plugins.get( 'Mention' ).toMentionAttribute( {
  54. // Add any other properties that you need.
  55. link: viewItem.getAttribute( 'href' ),
  56. userId: viewItem.getAttribute( 'data-user-id' )
  57. } );
  58. return mentionAttribute;
  59. }
  60. },
  61. converterPriority: 'high'
  62. } );
  63. // Do not forget to define a downcast converter as well:
  64. editor.conversion.for( 'downcast' ).attributeToElement( {
  65. model: 'mention',
  66. view: ( modelAttributeValue, viewWriter ) => {
  67. // Do not convert empty attributes (lack of value means no mention).
  68. if ( !modelAttributeValue ) {
  69. return;
  70. }
  71. return viewWriter.createAttributeElement( 'a', {
  72. class: 'mention',
  73. 'data-mention': modelAttributeValue.id,
  74. 'data-user-id': modelAttributeValue.userId,
  75. 'href': modelAttributeValue.link
  76. } );
  77. },
  78. converterPriority: 'high'
  79. } );
  80. }
  81. const items = [
  82. { id: '@swarley', userId: '1', name: 'Barney Stinson', link: 'https://www.imdb.com/title/tt0460649/characters/nm0000439' },
  83. { id: '@lilypad', userId: '2', name: 'Lily Aldrin', link: 'https://www.imdb.com/title/tt0460649/characters/nm0004989' },
  84. { id: '@marshmallow', userId: '3', name: 'Marshall Eriksen', link: 'https://www.imdb.com/title/tt0460649/characters/nm0781981' },
  85. { id: '@rsparkles', userId: '4', name: 'Robin Scherbatsky', link: 'https://www.imdb.com/title/tt0460649/characters/nm1130627' },
  86. { id: '@tdog', userId: '5', name: 'Ted Mosby', link: 'https://www.imdb.com/title/tt0460649/characters/nm1102140' }
  87. ];
  88. function getFeedItems( queryText ) {
  89. // As an example of an asynchronous action, let's return a promise
  90. // that resolves after a 100ms timeout.
  91. // This can be a server request, or any sort of delayed action.
  92. return new Promise( resolve => {
  93. setTimeout( () => {
  94. resolve( items.filter( isItemMatching ) );
  95. }, 100 );
  96. } );
  97. // Filtering function - it uses `name` and `username` properties of an item to find a match.
  98. function isItemMatching( item ) {
  99. // Make search case-insensitive.
  100. const searchString = queryText.toLowerCase();
  101. // Include an item in the search results if name or username includes the current user input.
  102. return (
  103. item.name.toLowerCase().includes( searchString ) ||
  104. item.id.toLowerCase().includes( searchString )
  105. );
  106. }
  107. }
  108. function customItemRenderer( item ) {
  109. const itemElement = document.createElement( 'span' );
  110. itemElement.classList.add( 'custom-item' );
  111. itemElement.id = `mention-list-item-id-${ item.userid }`;
  112. itemElement.textContent = `${ item.name } `;
  113. const usernameElement = document.createElement( 'span' );
  114. usernameElement.classList.add( 'custom-item-username' );
  115. usernameElement.textContent = item.id;
  116. itemElement.appendChild( usernameElement );
  117. return itemElement;
  118. }