mention-custom-renderer.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global console, window */
  6. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  7. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  8. import Mention from '../../src/mention';
  9. import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
  10. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  11. import Font from '@ckeditor/ckeditor5-font/src/font';
  12. ClassicEditor
  13. .create( global.document.querySelector( '#editor' ), {
  14. plugins: [ ArticlePluginSet, Underline, Font, Mention ],
  15. toolbar: [
  16. 'heading',
  17. '|', 'bulletedList', 'numberedList', 'blockQuote',
  18. '|', 'bold', 'italic', 'underline', 'link',
  19. '|', 'fontFamily', 'fontSize', 'fontColor', 'fontBackgroundColor',
  20. '|', 'insertTable',
  21. '|', 'undo', 'redo'
  22. ],
  23. mention: {
  24. feeds: [
  25. {
  26. feed: getFeed,
  27. itemRenderer: item => {
  28. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  29. span.classList.add( 'custom-item' );
  30. span.id = `mention-list-item-id-${ item.id }`;
  31. span.innerHTML = `${ item.name } <span class="custom-item-username">@${ item.username }</span>`;
  32. return span;
  33. }
  34. }
  35. ]
  36. }
  37. } )
  38. .then( editor => {
  39. window.editor = editor;
  40. } )
  41. .catch( err => {
  42. console.error( err.stack );
  43. } );
  44. function getFeed( feedText ) {
  45. return Promise.resolve( [
  46. { id: '1', name: 'Barney Stinson', username: 'swarley' },
  47. { id: '2', name: 'Lily Aldrin', username: 'lilypad' },
  48. { id: '3', name: 'Marshall Eriksen', username: 'marshmallow' },
  49. { id: '4', name: 'Robin Scherbatsky', username: 'rsparkles' },
  50. { id: '5', name: 'Ted Mosby', username: 'tdog' }
  51. ].filter( item => {
  52. const searchString = feedText.toLowerCase();
  53. return item.name.toLowerCase().includes( searchString ) || item.username.toLowerCase().includes( searchString );
  54. } ) );
  55. }