8
0

mention-custom-view.js 5.0 KB

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