mention-custom-view.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @license Copyright (c) 2003-2020, 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. }, {
  46. priority: 20,
  47. id: modelAttributeValue.uid
  48. } );
  49. },
  50. converterPriority: 'high'
  51. } );
  52. }
  53. }
  54. ClassicEditor
  55. .create( global.document.querySelector( '#editor' ), {
  56. plugins: [ ArticlePluginSet, Underline, Font, Mention, CustomMentionAttributeView ],
  57. toolbar: [
  58. 'heading',
  59. '|', 'bulletedList', 'numberedList', 'blockQuote',
  60. '|', 'bold', 'italic', 'underline', 'link',
  61. '|', 'fontFamily', 'fontSize', 'fontColor', 'fontBackgroundColor',
  62. '|', 'insertTable',
  63. '|', 'undo', 'redo'
  64. ],
  65. table: {
  66. contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ],
  67. tableToolbar: [ 'bold', 'italic' ]
  68. },
  69. mention: {
  70. feeds: [
  71. {
  72. marker: '@',
  73. feed: getFeed
  74. }
  75. ]
  76. }
  77. } )
  78. .then( editor => {
  79. window.editor = editor;
  80. } )
  81. .catch( err => {
  82. console.error( err.stack );
  83. } );
  84. function getFeed( feedText ) {
  85. return [
  86. { id: '@Barney Stinson', text: 'Barney Stinson', link: 'https://www.imdb.com/title/tt0460649/characters/nm0000439' },
  87. { id: '@Lily Aldrin', text: 'Lily Aldrin', link: 'https://www.imdb.com/title/tt0460649/characters/nm0004989' },
  88. { id: '@Marshall Eriksen', text: 'Marshall Eriksen', link: 'https://www.imdb.com/title/tt0460649/characters/nm0781981' },
  89. { id: '@Robin Scherbatsky', text: 'Robin Scherbatsky', link: 'https://www.imdb.com/title/tt0460649/characters/nm1130627' },
  90. { id: '@Ted Mosby', text: 'Ted Mosby', link: 'https://www.imdb.com/title/tt0460649/characters/nm1102140' }
  91. ].filter( item => {
  92. const searchString = feedText.toLowerCase();
  93. return item.id.toLowerCase().includes( searchString );
  94. } );
  95. }