inline-widget.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. class InlineWidget extends Plugin {
  21. constructor( editor ) {
  22. super( editor );
  23. editor.model.schema.register( 'placeholder', {
  24. allowWhere: '$text',
  25. isObject: true,
  26. allowAttributes: [ 'type' ]
  27. } );
  28. editor.conversion.for( 'editingDowncast' ).elementToElement( {
  29. model: 'placeholder',
  30. view: ( modelItem, viewWriter ) => {
  31. const widgetElement = createPlaceholderView( modelItem, viewWriter );
  32. return toWidget( widgetElement, viewWriter );
  33. }
  34. } );
  35. editor.conversion.for( 'dataDowncast' ).elementToElement( {
  36. model: 'placeholder',
  37. view: createPlaceholderView
  38. } );
  39. editor.conversion.for( 'upcast' ).elementToElement( {
  40. view: 'placeholder',
  41. model: ( viewElement, modelWriter ) => {
  42. let type = 'general';
  43. if ( viewElement.childCount ) {
  44. const text = viewElement.getChild( 0 );
  45. if ( text.is( 'text' ) ) {
  46. type = text.data;
  47. }
  48. }
  49. return modelWriter.createElement( 'placeholder', { type } );
  50. }
  51. } );
  52. function createPlaceholderView( modelItem, viewWriter ) {
  53. const widgetElement = viewWriter.createContainerElement( 'placeholder' );
  54. viewWriter.insert( viewWriter.createPositionAt( widgetElement, 0 ), viewWriter.createText( modelItem.getAttribute( 'type' ) ) );
  55. return widgetElement;
  56. }
  57. this._createToolbarButton();
  58. }
  59. _createToolbarButton() {
  60. const editor = this.editor;
  61. const t = editor.t;
  62. editor.ui.componentFactory.add( 'placeholder', locale => {
  63. const buttonView = new ButtonView( locale );
  64. buttonView.set( {
  65. label: t( 'Insert placeholder' ),
  66. tooltip: true,
  67. withText: true
  68. } );
  69. this.listenTo( buttonView, 'execute', () => {
  70. const model = editor.model;
  71. model.change( writer => {
  72. const placeholder = writer.createElement( 'placeholder', { type: 'placeholder' } );
  73. model.insertContent( placeholder );
  74. writer.setSelection( placeholder, 'on' );
  75. } );
  76. } );
  77. return buttonView;
  78. } );
  79. }
  80. }
  81. ClassicEditor
  82. .create( global.document.querySelector( '#editor' ), {
  83. plugins: [ Enter, Typing, Paragraph, Heading, Bold, Undo, Clipboard, Widget, InlineWidget ],
  84. toolbar: [ 'heading', '|', 'bold', '|', 'placeholder', '|', 'undo', 'redo' ]
  85. } )
  86. .then( editor => {
  87. editor.model.document.on( 'change', () => {
  88. printModelContents( editor );
  89. } );
  90. printModelContents( editor );
  91. } )
  92. .catch( err => {
  93. console.error( err.stack );
  94. } );
  95. const modelDiv = global.document.querySelector( '#model' );
  96. function printModelContents( editor ) {
  97. modelDiv.innerText = formatData( getData( editor.model ) );
  98. }
  99. function formatData( data ) {
  100. return data.replace( /<(paragraph)>/g, '\n<$1>' );
  101. }