mention.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 Mention from '../../src/mention';
  9. import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
  10. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  11. import Font from '@ckeditor/ckeditor5-font/src/font';
  12. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  13. import { toWidget, viewToModelPositionOutsideModelElement } from '@ckeditor/ckeditor5-widget/src/utils';
  14. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  15. class InlineWidget extends Plugin {
  16. constructor( editor ) {
  17. super( editor );
  18. editor.model.schema.register( 'placeholder', {
  19. allowWhere: '$text',
  20. isObject: true,
  21. isInline: true,
  22. allowAttributes: [ 'type' ]
  23. } );
  24. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  25. model: 'placeholder',
  26. view: ( modelItem, conversionApi ) => {
  27. const widgetElement = createPlaceholderView( modelItem, conversionApi );
  28. return toWidget( widgetElement, conversionApi.writer );
  29. }
  30. } );
  31. editor.conversion.for( 'dataDowncast' ).elementToElement( {
  32. model: 'placeholder',
  33. view: createPlaceholderView
  34. } );
  35. editor.conversion.for( 'upcast' ).elementToElement( {
  36. view: 'placeholder',
  37. model: ( viewElement, { writer } ) => {
  38. let type = 'general';
  39. if ( viewElement.childCount ) {
  40. const text = viewElement.getChild( 0 );
  41. if ( text.is( '$text' ) ) {
  42. type = text.data.slice( 1, -1 );
  43. }
  44. }
  45. return writer.createElement( 'placeholder', { type } );
  46. }
  47. } );
  48. editor.editing.mapper.on(
  49. 'viewToModelPosition',
  50. viewToModelPositionOutsideModelElement( editor.model, viewElement => viewElement.name == 'placeholder' )
  51. );
  52. this._createToolbarButton();
  53. function createPlaceholderView( modelItem, { writer } ) {
  54. const widgetElement = writer.createContainerElement( 'placeholder' );
  55. const viewText = writer.createText( '{' + modelItem.getAttribute( 'type' ) + '}' );
  56. writer.insert( writer.createPositionAt( widgetElement, 0 ), viewText );
  57. return widgetElement;
  58. }
  59. }
  60. _createToolbarButton() {
  61. const editor = this.editor;
  62. const t = editor.t;
  63. editor.ui.componentFactory.add( 'placeholder', locale => {
  64. const buttonView = new ButtonView( locale );
  65. buttonView.set( {
  66. label: t( 'Insert placeholder' ),
  67. tooltip: true,
  68. withText: true
  69. } );
  70. this.listenTo( buttonView, 'execute', () => {
  71. const model = editor.model;
  72. model.change( writer => {
  73. const placeholder = writer.createElement( 'placeholder', { type: 'placeholder' } );
  74. model.insertContent( placeholder );
  75. writer.setSelection( placeholder, 'on' );
  76. } );
  77. } );
  78. return buttonView;
  79. } );
  80. }
  81. }
  82. class MentionCommandSwitcher extends Plugin {
  83. init() {
  84. const editor = this.editor;
  85. const mentionCommand = editor.commands.get( 'mention' );
  86. editor.ui.componentFactory.add( 'toggleMentionCommand', locale => {
  87. const view = new ButtonView( locale );
  88. view.set( {
  89. label: 'Mentions',
  90. withText: true,
  91. isToggleable: true
  92. } );
  93. view.bind( 'isOn' ).to( mentionCommand, 'isEnabled' );
  94. this.listenTo( view, 'execute', () => {
  95. if ( mentionCommand.isEnabled ) {
  96. mentionCommand.forceDisabled( 'mentionCommandSwitcher' );
  97. } else {
  98. mentionCommand.clearForceDisabled( 'mentionCommandSwitcher' );
  99. }
  100. } );
  101. return view;
  102. } );
  103. }
  104. }
  105. ClassicEditor
  106. .create( global.document.querySelector( '#editor' ), {
  107. plugins: [ ArticlePluginSet, Underline, Font, Mention, InlineWidget, MentionCommandSwitcher ],
  108. toolbar: [
  109. 'heading',
  110. '|', 'bulletedList', 'numberedList', 'blockQuote',
  111. '|', 'bold', 'italic', 'underline', 'link',
  112. '|', 'fontFamily', 'fontSize', 'fontColor', 'fontBackgroundColor',
  113. '|', 'insertTable', 'placeholder', 'toggleMentionCommand',
  114. '|', 'undo', 'redo'
  115. ],
  116. image: {
  117. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  118. },
  119. table: {
  120. contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ],
  121. tableToolbar: [ 'bold', 'italic' ]
  122. },
  123. mention: {
  124. feeds: [
  125. {
  126. marker: '@',
  127. feed: [ '@Barney', '@Lily', '@Marshall', '@Robin', '@Ted' ]
  128. },
  129. {
  130. marker: '#',
  131. feed: [
  132. '#a01', '#a02', '#a03', '#a04', '#a05', '#a06', '#a07', '#a08', '#a09', '#a10',
  133. '#a11', '#a12', '#a13', '#a14', '#a15', '#a16', '#a17', '#a18', '#a19', '#a20'
  134. ]
  135. },
  136. {
  137. marker: ':',
  138. feed: [
  139. ':+1:', ':-1:', ':@(at-sign):', ':$(dollar-sign):', ':#(hash-sign):'
  140. ]
  141. }
  142. ]
  143. }
  144. } )
  145. .then( editor => {
  146. window.editor = editor;
  147. } )
  148. .catch( err => {
  149. console.error( err.stack );
  150. } );