blocktoolbar.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window, document, console:false, setTimeout */
  6. import BalloonEditor from '@ckeditor/ckeditor5-editor-balloon/src/ballooneditor';
  7. import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  8. import List from '@ckeditor/ckeditor5-list/src/list';
  9. import Image from '@ckeditor/ckeditor5-image/src/image';
  10. import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  13. import HeadingButtonsUI from '@ckeditor/ckeditor5-heading/src/headingbuttonsui';
  14. import ParagraphButtonUI from '@ckeditor/ckeditor5-paragraph/src/paragraphbuttonui';
  15. import BlockToolbar from '../../../src/toolbar/block/blocktoolbar';
  16. BalloonEditor
  17. .create( document.querySelector( '#editor' ), {
  18. plugins: [ Essentials, List, Paragraph, Heading, Image, ImageCaption, HeadingButtonsUI, ParagraphButtonUI, BlockToolbar ],
  19. blockToolbar: [ 'paragraph', 'heading1', 'heading2', 'heading3', 'bulletedList', 'numberedList' ]
  20. } )
  21. .then( editor => {
  22. window.editor = editor;
  23. const externalChanges = createExternalChangesSimulator( editor );
  24. document.querySelector( '.external-type' ).addEventListener( 'click', () => {
  25. externalChanges.wait( 4000 )
  26. .then( () => externalChanges.insertNewLine( [ 1 ] ) )
  27. .then( () => externalChanges.type( [ 1, 0 ], 'New line' ) )
  28. .then( () => externalChanges.insertNewLine( [ 2 ] ) )
  29. .then( () => externalChanges.type( [ 2, 0 ], 'New line' ) )
  30. .then( () => externalChanges.insertNewLine( [ 3 ] ) )
  31. .then( () => externalChanges.type( [ 3, 0 ], 'New line' ) );
  32. } );
  33. document.querySelector( '.external-delete' ).addEventListener( 'click', () => {
  34. externalChanges.wait( 4000 )
  35. .then( () => externalChanges.removeElement( [ 1 ] ) );
  36. } );
  37. } )
  38. .catch( err => {
  39. console.error( err.stack );
  40. } );
  41. // Move it to the test utils.
  42. // See https://github.com/ckeditor/ckeditor5-ui/issues/393.
  43. function createExternalChangesSimulator( editor ) {
  44. const { model } = editor;
  45. function wait( delay ) {
  46. return new Promise( resolve => setTimeout( () => resolve(), delay ) );
  47. }
  48. function insertNewLine( path ) {
  49. model.enqueueChange( 'transparent', writer => {
  50. writer.insertElement( 'paragraph', writer.createPositionFromPath( model.document.getRoot(), path ) );
  51. } );
  52. return Promise.resolve();
  53. }
  54. function type( path, text ) {
  55. return new Promise( resolve => {
  56. let position = model.createPositionFromPath( model.document.getRoot(), path );
  57. let index = 0;
  58. function typing() {
  59. wait( 40 ).then( () => {
  60. model.enqueueChange( 'transparent', writer => {
  61. writer.insertText( text[ index ], position );
  62. position = position.getShiftedBy( 1 );
  63. const nextLetter = text[ ++index ];
  64. if ( nextLetter ) {
  65. typing( nextLetter );
  66. } else {
  67. index = 0;
  68. resolve();
  69. }
  70. } );
  71. } );
  72. }
  73. typing();
  74. } );
  75. }
  76. function removeElement( path ) {
  77. model.enqueueChange( 'transparent', writer => {
  78. const start = writer.createPositionFromPath( model.document.getRoot(), path );
  79. writer.remove( writer.createRange( start, start.getShiftedBy( 1 ) ) );
  80. } );
  81. return Promise.resolve();
  82. }
  83. return { wait, insertNewLine, type, removeElement };
  84. }