mention-custom-renderer.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  9. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  10. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  13. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  14. import Widget from '@ckeditor/ckeditor5-widget/src/widget';
  15. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  16. import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
  17. import Table from '@ckeditor/ckeditor5-table/src/table';
  18. import Mention from '../../src/mention';
  19. import Link from '@ckeditor/ckeditor5-link/src/link';
  20. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  21. import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
  22. ClassicEditor
  23. .create( global.document.querySelector( '#editor' ), {
  24. plugins: [ Enter, Typing, Paragraph, Heading, Link, Bold, Italic, Underline, Undo, Clipboard, Widget, ShiftEnter, Table, Mention ],
  25. toolbar: [ 'heading', '|', 'bold', 'italic', 'underline', 'link', '|', 'insertTable', '|', 'undo', 'redo' ],
  26. mention: {
  27. feeds: [
  28. {
  29. feed: getFeed,
  30. itemRenderer: item => {
  31. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  32. span.classList.add( 'custom-item' );
  33. span.id = `mention-list-item-id-${ item.id }`;
  34. span.innerHTML = `${ item.name } <span class="custom-item-username">@${ item.username }</span>`;
  35. return span;
  36. }
  37. }
  38. ]
  39. }
  40. } )
  41. .then( editor => {
  42. window.editor = editor;
  43. } )
  44. .catch( err => {
  45. console.error( err.stack );
  46. } );
  47. function getFeed( feedText ) {
  48. return Promise.resolve( [
  49. { id: '1', name: 'Barney Stinson', username: 'swarley' },
  50. { id: '2', name: 'Lily Aldrin', username: 'lilypad' },
  51. { id: '3', name: 'Marshall Eriksen', username: 'marshmallow' },
  52. { id: '4', name: 'Robin Scherbatsky', username: 'rsparkles' },
  53. { id: '5', name: 'Ted Mosby', username: 'tdog' }
  54. ].filter( item => {
  55. const searchString = feedText.toLowerCase();
  56. return item.name.toLowerCase().includes( searchString ) || item.username.toLowerCase().includes( searchString );
  57. } ) );
  58. }