mention-custom-view.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 Link from '@ckeditor/ckeditor5-link/src/link';
  19. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  20. import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
  21. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  22. import MentionUI from '../../src/mentionui';
  23. import MentionEditing from '../../src/mentionediting';
  24. class CustomMentionAttributeView extends Plugin {
  25. init() {
  26. const editor = this.editor;
  27. editor.conversion.for( 'upcast' ).elementToAttribute( {
  28. view: {
  29. name: 'a',
  30. key: 'data-mention',
  31. classes: 'mention',
  32. attributes: {
  33. href: true
  34. }
  35. },
  36. model: {
  37. key: 'mention',
  38. value: viewItem => {
  39. const mentionValue = {
  40. name: viewItem.getAttribute( 'data-mention' ),
  41. link: viewItem.getAttribute( 'href' )
  42. };
  43. return mentionValue;
  44. }
  45. },
  46. converterPriority: 'high'
  47. } );
  48. editor.conversion.for( 'downcast' ).attributeToElement( {
  49. model: 'mention',
  50. view: ( modelAttributeValue, viewWriter ) => {
  51. if ( !modelAttributeValue ) {
  52. return;
  53. }
  54. return viewWriter.createAttributeElement( 'a', {
  55. class: 'mention',
  56. 'data-mention': modelAttributeValue.name,
  57. 'href': modelAttributeValue.link
  58. } );
  59. },
  60. converterPriority: 'high'
  61. } );
  62. }
  63. }
  64. ClassicEditor
  65. .create( global.document.querySelector( '#editor' ), {
  66. plugins: [ Enter, Typing, Paragraph, Link, Heading, Bold, Italic, Underline, Undo, Clipboard, Widget, ShiftEnter, Table,
  67. MentionEditing, CustomMentionAttributeView, MentionUI ],
  68. toolbar: [ 'heading', '|', 'bold', 'italic', 'underline', 'link', '|', 'insertTable', '|', 'undo', 'redo' ],
  69. mention: {
  70. feeds: [
  71. { feed: getFeed }
  72. ]
  73. }
  74. } )
  75. .then( editor => {
  76. window.editor = editor;
  77. } )
  78. .catch( err => {
  79. console.error( err.stack );
  80. } );
  81. function getFeed( feedText ) {
  82. return [
  83. { id: '1', name: 'Barney Stinson', link: 'https://www.imdb.com/title/tt0460649/characters/nm0000439' },
  84. { id: '2', name: 'Lily Aldrin', link: 'https://www.imdb.com/title/tt0460649/characters/nm0004989' },
  85. { id: '3', name: 'Marshall Eriksen', link: 'https://www.imdb.com/title/tt0460649/characters/nm0781981' },
  86. { id: '4', name: 'Robin Scherbatsky', link: 'https://www.imdb.com/title/tt0460649/characters/nm1130627' },
  87. { id: '5', name: 'Ted Mosby', link: 'https://www.imdb.com/title/tt0460649/characters/nm1102140' }
  88. ].filter( item => {
  89. const searchString = feedText.toLowerCase();
  90. return item.name.toLowerCase().includes( searchString );
  91. } );
  92. }