mention-custom-view.js 3.5 KB

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