mention-custom-renderer.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. {
  28. feed: getFeed,
  29. itemRenderer: item => {
  30. const span = global.document.createElementNS( 'http://www.w3.org/1999/xhtml', 'span' );
  31. span.classList.add( 'custom-item' );
  32. span.id = `mention-list-item-id-${ item.id }`;
  33. span.innerHTML = `${ item.label } <span class="custom-item-username">@${ item.username }</span>`;
  34. return span;
  35. }
  36. }
  37. ]
  38. } )
  39. .then( editor => {
  40. window.editor = editor;
  41. } )
  42. .catch( err => {
  43. console.error( err.stack );
  44. } );
  45. function getFeed( feedText ) {
  46. return Promise.resolve( [
  47. { id: '1', label: 'Barney Stinson', username: 'swarley' },
  48. { id: '2', label: 'Lily Aldrin', username: 'lilypad' },
  49. { id: '3', label: 'Marshall Eriksen', username: 'marshmallow' },
  50. { id: '4', label: 'Robin Scherbatsky', username: 'rsparkles' },
  51. { id: '5', label: 'Ted Mosby', username: 'tdog' }
  52. ].filter( item => {
  53. const searchString = feedText.toLowerCase();
  54. return item.label.toLowerCase().includes( searchString ) || item.username.toLowerCase().includes( searchString );
  55. } ) );
  56. }