inline-widget.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global console */
  6. import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  8. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  9. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  10. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  11. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  12. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  15. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  16. import Widget from '@ckeditor/ckeditor5-widget/src/widget';
  17. import { toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
  18. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  19. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  20. import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
  21. class InlineWidget extends Plugin {
  22. constructor( editor ) {
  23. super( editor );
  24. editor.model.schema.register( 'placeholder', {
  25. allowWhere: '$text',
  26. isObject: true,
  27. isInline: true,
  28. allowAttributes: [ 'type' ]
  29. } );
  30. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  31. model: 'placeholder',
  32. view: ( modelItem, viewWriter ) => {
  33. const widgetElement = createPlaceholderView( modelItem, viewWriter );
  34. return toWidget( widgetElement, viewWriter );
  35. }
  36. } );
  37. editor.conversion.for( 'dataDowncast' ).elementToElement( {
  38. model: 'placeholder',
  39. view: createPlaceholderView
  40. } );
  41. editor.conversion.for( 'upcast' ).elementToElement( {
  42. view: 'placeholder',
  43. model: ( viewElement, modelWriter ) => {
  44. let type = 'general';
  45. if ( viewElement.childCount ) {
  46. const text = viewElement.getChild( 0 );
  47. if ( text.is( 'text' ) ) {
  48. type = text.data;
  49. }
  50. }
  51. return modelWriter.createElement( 'placeholder', { type } );
  52. }
  53. } );
  54. this._createToolbarButton();
  55. function createPlaceholderView( modelItem, viewWriter ) {
  56. const widgetElement = viewWriter.createContainerElement( 'placeholder' );
  57. viewWriter.insert( viewWriter.createPositionAt( widgetElement, 0 ), viewWriter.createText( modelItem.getAttribute( 'type' ) ) );
  58. return widgetElement;
  59. }
  60. }
  61. _createToolbarButton() {
  62. const editor = this.editor;
  63. const t = editor.t;
  64. editor.ui.componentFactory.add( 'placeholder', locale => {
  65. const buttonView = new ButtonView( locale );
  66. buttonView.set( {
  67. label: t( 'Insert placeholder' ),
  68. tooltip: true,
  69. withText: true
  70. } );
  71. this.listenTo( buttonView, 'execute', () => {
  72. const model = editor.model;
  73. model.change( writer => {
  74. const placeholder = writer.createElement( 'placeholder', { type: 'placeholder' } );
  75. model.insertContent( placeholder );
  76. writer.setSelection( placeholder, 'on' );
  77. } );
  78. } );
  79. return buttonView;
  80. } );
  81. }
  82. }
  83. ClassicEditor
  84. .create( global.document.querySelector( '#editor' ), {
  85. plugins: [ Enter, Typing, Paragraph, Heading, Bold, Undo, Clipboard, Widget, ShiftEnter, InlineWidget ],
  86. toolbar: [ 'heading', '|', 'bold', '|', 'placeholder', '|', 'undo', 'redo' ]
  87. } )
  88. .then( editor => {
  89. editor.model.document.on( 'change', () => {
  90. printModelContents( editor );
  91. } );
  92. printModelContents( editor );
  93. } )
  94. .catch( err => {
  95. console.error( err.stack );
  96. } );
  97. const modelDiv = global.document.querySelector( '#model' );
  98. function printModelContents( editor ) {
  99. modelDiv.innerText = formatData( getData( editor.model ) );
  100. }
  101. function formatData( data ) {
  102. return data.replace( /<(paragraph)>/g, '\n<$1>' );
  103. }