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