mention-custom-renderer.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global console, window, document */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import Mention from '../../src/mention';
  8. import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
  9. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  10. import Font from '@ckeditor/ckeditor5-font/src/font';
  11. ClassicEditor
  12. .create( document.querySelector( '#editor' ), {
  13. plugins: [ ArticlePluginSet, Underline, Font, Mention ],
  14. toolbar: [
  15. 'heading',
  16. '|', 'bulletedList', 'numberedList', 'blockQuote',
  17. '|', 'bold', 'italic', 'underline', 'link',
  18. '|', 'fontFamily', 'fontSize', 'fontColor', 'fontBackgroundColor',
  19. '|', 'insertTable',
  20. '|', 'undo', 'redo'
  21. ],
  22. table: {
  23. contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ],
  24. tableToolbar: [ 'bold', 'italic' ]
  25. },
  26. mention: {
  27. feeds: [
  28. {
  29. marker: '@',
  30. feed: getFeed,
  31. itemRenderer: item => {
  32. const span = document.createElement( 'span' );
  33. span.classList.add( 'custom-item' );
  34. span.id = `mention-list-item-id-${ item.itemId }`;
  35. span.innerHTML = `${ item.name } <span class="custom-item-username">${ item.id }</span>`;
  36. return span;
  37. }
  38. },
  39. {
  40. marker: '#',
  41. feed: [
  42. { id: '#1002', text: 'Some bug in editor' },
  43. { id: '#1003', text: 'Introduce this feature' },
  44. { id: '#1004', text: 'Missing docs' },
  45. { id: '#1005', text: 'Another bug' },
  46. { id: '#1006', text: 'More bugs' }
  47. ],
  48. itemRenderer: item => `Issue ${ item.id }: ${ item.text }`
  49. }
  50. ]
  51. }
  52. } )
  53. .then( editor => {
  54. window.editor = editor;
  55. } )
  56. .catch( err => {
  57. console.error( err.stack );
  58. } );
  59. function getFeed( feedText ) {
  60. return Promise.resolve( [
  61. { itemId: '1', name: 'Barney Stinson', id: '@swarley' },
  62. { itemId: '2', name: 'Lily Aldrin', id: '@lilypad' },
  63. { itemId: '3', name: 'Marshall Eriksen', id: '@marshmallow' },
  64. { itemId: '4', name: 'Robin Scherbatsky', id: '@rsparkles' },
  65. { itemId: '5', name: 'Ted Mosby', id: '@tdog' }
  66. ].filter( item => {
  67. const searchString = feedText.toLowerCase();
  68. return item.name.toLowerCase().includes( searchString ) || item.id.toLowerCase().includes( searchString );
  69. } ) );
  70. }