mention-custom-renderer.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. mention: {
  23. feeds: [
  24. {
  25. marker: '@',
  26. feed: getFeed,
  27. itemRenderer: item => {
  28. const span = document.createElement( 'span' );
  29. span.classList.add( 'custom-item' );
  30. span.id = `mention-list-item-id-${ item.itemId }`;
  31. span.innerHTML = `${ item.name } <span class="custom-item-username">${ item.id }</span>`;
  32. return span;
  33. }
  34. },
  35. {
  36. marker: '#',
  37. feed: [
  38. { id: '#1002', text: 'Some bug in editor' },
  39. { id: '#1003', text: 'Introduce this feature' },
  40. { id: '#1004', text: 'Missing docs' },
  41. { id: '#1005', text: 'Another bug' },
  42. { id: '#1006', text: 'More bugs' }
  43. ],
  44. itemRenderer: item => `Issue ${ item.id }: ${ item.text }`
  45. }
  46. ]
  47. }
  48. } )
  49. .then( editor => {
  50. window.editor = editor;
  51. } )
  52. .catch( err => {
  53. console.error( err.stack );
  54. } );
  55. function getFeed( feedText ) {
  56. return Promise.resolve( [
  57. { itemId: '1', name: 'Barney Stinson', id: '@swarley' },
  58. { itemId: '2', name: 'Lily Aldrin', id: '@lilypad' },
  59. { itemId: '3', name: 'Marshall Eriksen', id: '@marshmallow' },
  60. { itemId: '4', name: 'Robin Scherbatsky', id: '@rsparkles' },
  61. { itemId: '5', name: 'Ted Mosby', id: '@tdog' }
  62. ].filter( item => {
  63. const searchString = feedText.toLowerCase();
  64. return item.name.toLowerCase().includes( searchString ) || item.id.toLowerCase().includes( searchString );
  65. } ) );
  66. }