mention-custom-view.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 priorities from '@ckeditor/ckeditor5-utils/src/priorities';
  23. import MentionUI from '../../src/mentionui';
  24. // eslint-disable-next-line no-unused-vars
  25. import MentionEditing from '../../src/mentionediting';
  26. // eslint-disable-next-line no-unused-vars
  27. import MentionElementEditing from '../../src/mentioneditingelement';
  28. // eslint-disable-next-line no-unused-vars
  29. import MentionMarkerEditing from '../../src/mentioneditingmarker';
  30. const HIGHER_THEN_HIGHEST = priorities.highest + 50;
  31. // eslint-disable-next-line no-unused-vars
  32. class CustomMentionAttributeView extends Plugin {
  33. init() {
  34. const editor = this.editor;
  35. editor.conversion.for( 'upcast' ).elementToAttribute( {
  36. view: {
  37. name: 'a',
  38. key: 'data-mention',
  39. classes: 'mention',
  40. attributes: {
  41. href: true
  42. }
  43. },
  44. model: {
  45. key: 'mention',
  46. value: viewItem => {
  47. const mentionValue = {
  48. label: viewItem.getAttribute( 'data-mention' ),
  49. link: viewItem.getAttribute( 'href' )
  50. };
  51. return mentionValue;
  52. }
  53. },
  54. converterPriority: HIGHER_THEN_HIGHEST
  55. } );
  56. editor.conversion.for( 'downcast' ).attributeToElement( {
  57. model: 'mention',
  58. view: ( modelAttributeValue, viewWriter ) => {
  59. if ( !modelAttributeValue ) {
  60. return;
  61. }
  62. return viewWriter.createAttributeElement( 'a', {
  63. class: 'mention',
  64. 'data-mention': modelAttributeValue.label,
  65. 'href': modelAttributeValue.link
  66. } );
  67. },
  68. converterPriority: HIGHER_THEN_HIGHEST
  69. } );
  70. }
  71. }
  72. // eslint-disable-next-line no-unused-vars
  73. class CustomMentionElementView extends Plugin {
  74. init() {
  75. const editor = this.editor;
  76. editor.conversion.for( 'upcast' ).elementToElement( {
  77. view: {
  78. name: 'a',
  79. classes: 'mention',
  80. attributes: {
  81. href: true
  82. }
  83. },
  84. model: ( viewItem, modelWriter ) => {
  85. const item = {
  86. label: viewItem.getAttribute( 'data-mention' ),
  87. link: viewItem.getAttribute( 'href' )
  88. };
  89. const frag = modelWriter.createDocumentFragment();
  90. const mention = modelWriter.createElement( 'mention', { item } );
  91. modelWriter.insert( mention, frag, 0 );
  92. modelWriter.insertText( item.label, mention, 0 );
  93. return mention;
  94. },
  95. converterPriority: HIGHER_THEN_HIGHEST
  96. } );
  97. editor.conversion.for( 'downcast' ).elementToElement( {
  98. model: 'mentionElement',
  99. view: ( modelElement, viewWriter ) => {
  100. const item = modelElement.getAttribute( 'item' );
  101. const link = viewWriter.createContainerElement( 'a', {
  102. class: 'mention',
  103. 'data-mention': item.label,
  104. 'href': item.link
  105. } );
  106. return link;
  107. },
  108. converterPriority: HIGHER_THEN_HIGHEST
  109. } );
  110. }
  111. }
  112. ClassicEditor
  113. .create( global.document.querySelector( '#editor' ), {
  114. plugins: [ Enter, Typing, Paragraph, Link, Heading, Bold, Italic, Underline, Undo, Clipboard, Widget, ShiftEnter, Table,
  115. // 1. Using attributes
  116. // MentionEditing,
  117. // CustomMentionAttributeView,
  118. // 2. Using Element - buggy
  119. // MentionElementEditing,
  120. // CustomMentionElementView,
  121. MentionMarkerEditing,
  122. // Common: ui
  123. MentionUI
  124. ],
  125. toolbar: [ 'heading', '|', 'bold', 'italic', 'underline', 'link', '|', 'insertTable', '|', 'undo', 'redo' ],
  126. mention: [
  127. {
  128. feed: getFeed
  129. }
  130. ]
  131. } )
  132. .then( editor => {
  133. window.editor = editor;
  134. } )
  135. .catch( err => {
  136. console.error( err.stack );
  137. } );
  138. function getFeed( feedText ) {
  139. return Promise.resolve( [
  140. { id: '1', label: 'Barney Stinson', link: 'https://www.imdb.com/title/tt0460649/characters/nm0000439' },
  141. { id: '2', label: 'Lily Aldrin', link: 'https://www.imdb.com/title/tt0460649/characters/nm0004989' },
  142. { id: '3', label: 'Marshall Eriksen', link: 'https://www.imdb.com/title/tt0460649/characters/nm0781981' },
  143. { id: '4', label: 'Robin Scherbatsky', link: 'https://www.imdb.com/title/tt0460649/characters/nm1130627' },
  144. { id: '5', label: 'Ted Mosby', link: 'https://www.imdb.com/title/tt0460649/characters/nm1102140' }
  145. ].filter( item => {
  146. const searchString = feedText.toLowerCase();
  147. return item.label.toLowerCase().includes( searchString );
  148. } ) );
  149. }