mention-custom-view.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 => {
  28. return editor.plugins.get( 'Mention' ).toMentionAttribute( viewItem, {
  29. link: viewItem.getAttribute( 'href' )
  30. } );
  31. }
  32. },
  33. converterPriority: 'high'
  34. } );
  35. editor.conversion.for( 'downcast' ).attributeToElement( {
  36. model: 'mention',
  37. view: ( modelAttributeValue, viewWriter ) => {
  38. if ( !modelAttributeValue ) {
  39. return;
  40. }
  41. return viewWriter.createAttributeElement( 'a', {
  42. class: 'mention',
  43. 'data-mention': modelAttributeValue.id,
  44. 'href': modelAttributeValue.link
  45. }, { id: modelAttributeValue._uid } );
  46. },
  47. converterPriority: 'high'
  48. } );
  49. }
  50. }
  51. ClassicEditor
  52. .create( global.document.querySelector( '#editor' ), {
  53. plugins: [ ArticlePluginSet, Underline, Font, Mention, CustomMentionAttributeView ],
  54. toolbar: [
  55. 'heading',
  56. '|', 'bulletedList', 'numberedList', 'blockQuote',
  57. '|', 'bold', 'italic', 'underline', 'link',
  58. '|', 'fontFamily', 'fontSize', 'fontColor', 'fontBackgroundColor',
  59. '|', 'insertTable',
  60. '|', 'undo', 'redo'
  61. ],
  62. mention: {
  63. feeds: [
  64. {
  65. marker: '@',
  66. feed: getFeed
  67. }
  68. ]
  69. }
  70. } )
  71. .then( editor => {
  72. window.editor = editor;
  73. } )
  74. .catch( err => {
  75. console.error( err.stack );
  76. } );
  77. function getFeed( feedText ) {
  78. return [
  79. { id: '@Barney Stinson', text: 'Barney Stinson', link: 'https://www.imdb.com/title/tt0460649/characters/nm0000439' },
  80. { id: '@Lily Aldrin', text: 'Lily Aldrin', link: 'https://www.imdb.com/title/tt0460649/characters/nm0004989' },
  81. { id: '@Marshall Eriksen', text: 'Marshall Eriksen', link: 'https://www.imdb.com/title/tt0460649/characters/nm0781981' },
  82. { id: '@Robin Scherbatsky', text: 'Robin Scherbatsky', link: 'https://www.imdb.com/title/tt0460649/characters/nm1130627' },
  83. { id: '@Ted Mosby', text: 'Ted Mosby', link: 'https://www.imdb.com/title/tt0460649/characters/nm1102140' }
  84. ].filter( item => {
  85. const searchString = feedText.toLowerCase();
  86. return item.id.toLowerCase().includes( searchString );
  87. } );
  88. }