mention.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @license Copyright (c) 2003-2019, 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, viewWriter ) => {
  27. const widgetElement = createPlaceholderView( modelItem, viewWriter );
  28. return toWidget( widgetElement, viewWriter );
  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, modelWriter ) => {
  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 modelWriter.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, viewWriter ) {
  54. const widgetElement = viewWriter.createContainerElement( 'placeholder' );
  55. const viewText = viewWriter.createText( '{' + modelItem.getAttribute( 'type' ) + '}' );
  56. viewWriter.insert( viewWriter.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. ClassicEditor
  83. .create( global.document.querySelector( '#editor' ), {
  84. plugins: [ ArticlePluginSet, Underline, Font, Mention, InlineWidget ],
  85. toolbar: [
  86. 'heading',
  87. '|', 'bulletedList', 'numberedList', 'blockQuote',
  88. '|', 'bold', 'italic', 'underline', 'link',
  89. '|', 'fontFamily', 'fontSize', 'fontColor', 'fontBackgroundColor',
  90. '|', 'insertTable', 'placeholder',
  91. '|', 'undo', 'redo'
  92. ],
  93. image: {
  94. toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  95. },
  96. table: {
  97. contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ],
  98. tableToolbar: [ 'bold', 'italic' ]
  99. },
  100. mention: {
  101. feeds: [
  102. {
  103. marker: '@',
  104. feed: [ '@Barney', '@Lily', '@Marshall', '@Robin', '@Ted' ]
  105. },
  106. {
  107. marker: '#',
  108. feed: [
  109. '#a01', '#a02', '#a03', '#a04', '#a05', '#a06', '#a07', '#a08', '#a09', '#a10',
  110. '#a11', '#a12', '#a13', '#a14', '#a15', '#a16', '#a17', '#a18', '#a19', '#a20'
  111. ]
  112. }
  113. ]
  114. }
  115. } )
  116. .then( editor => {
  117. window.editor = editor;
  118. } )
  119. .catch( err => {
  120. console.error( err.stack );
  121. } );