8
0

inline-widget.js 3.9 KB

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