uielement.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /* globals console, window, document */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  8. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  9. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  12. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  13. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  14. import Position from '../../../src/view/position';
  15. function createEndingUIElement( writer ) {
  16. const element = writer.createUIElement( 'span', null, function( domDocument ) {
  17. const root = this.toDomElement( domDocument );
  18. root.classList.add( 'ui-element' );
  19. root.innerHTML = 'END OF PARAGRAPH';
  20. return root;
  21. } );
  22. return element;
  23. }
  24. function createMiddleUIElement( writer ) {
  25. const element = writer.createUIElement( 'span', null, function( domDocument ) {
  26. const root = this.toDomElement( domDocument );
  27. root.classList.add( 'ui-element' );
  28. root.innerHTML = 'X';
  29. return root;
  30. } );
  31. return element;
  32. }
  33. class UIElementTestPlugin extends Plugin {
  34. init() {
  35. const editor = this.editor;
  36. const editing = editor.editing;
  37. // Add some UIElement to each paragraph.
  38. editing.downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => {
  39. const viewP = conversionApi.mapper.toViewElement( data.item );
  40. viewP._appendChild( createEndingUIElement( conversionApi.writer ) );
  41. }, { priority: 'lowest' } );
  42. }
  43. }
  44. ClassicEditor
  45. .create( document.querySelector( '#editor' ), {
  46. plugins: [ Enter, Typing, Paragraph, Undo, Bold, Italic, UIElementTestPlugin ],
  47. toolbar: [ 'undo', 'redo', 'bold', 'italic' ]
  48. } )
  49. .then( editor => {
  50. window.editor = editor;
  51. const view = editor.editing.view;
  52. // Add some UI elements.
  53. const viewRoot = editor.editing.view.document.getRoot();
  54. const viewText1 = viewRoot.getChild( 0 ).getChild( 0 );
  55. const viewText2 = viewRoot.getChild( 1 ).getChild( 0 );
  56. view.change( writer => {
  57. writer.insert( new Position( viewText1, 20 ), createMiddleUIElement( writer ) );
  58. writer.insert( new Position( viewText1, 20 ), createMiddleUIElement( writer ) );
  59. writer.insert( new Position( viewText2, 0 ), createMiddleUIElement( writer ) );
  60. writer.insert( new Position( viewText2, 6 ), createMiddleUIElement( writer ) );
  61. } );
  62. } )
  63. .catch( err => {
  64. console.error( err.stack );
  65. } );