blocktoolbar.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  8. import HeadingButtonsUI from '@ckeditor/ckeditor5-heading/src/headingbuttonsui';
  9. import ParagraphButtonUI from '@ckeditor/ckeditor5-paragraph/src/paragraphbuttonui';
  10. import BalloonToolbar from '../../../src/toolbar/balloon/balloontoolbar';
  11. import BlockToolbar from '../../../src/toolbar/block/blocktoolbar';
  12. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  13. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  14. class CustomBlockToolbar extends BlockToolbar {
  15. init() {
  16. super.init();
  17. this.on( 'checkAllowed', ( evt, args ) => {
  18. const modelElement = args[ 0 ];
  19. if ( modelElement && modelElement.name === 'heading1' ) {
  20. evt.return = false;
  21. }
  22. } );
  23. }
  24. }
  25. BalloonEditor
  26. .create( document.querySelector( '#editor' ), {
  27. plugins: [ ArticlePluginSet, HeadingButtonsUI, ParagraphButtonUI, BalloonToolbar, CustomBlockToolbar ],
  28. balloonToolbar: [ 'bold', 'italic', 'link' ],
  29. blockToolbar: [ 'paragraph', 'heading1', 'heading2', 'heading3', 'bulletedList', 'numberedList', 'blockQuote' ]
  30. } )
  31. .then( editor => {
  32. window.editor = editor;
  33. const externalChanges = createExternalChangesSimulator( editor );
  34. document.querySelector( '.external-type' ).addEventListener( 'click', () => {
  35. externalChanges.wait( 4000 )
  36. .then( () => externalChanges.insertNewLine( [ 1 ] ) )
  37. .then( () => externalChanges.type( [ 1, 0 ], 'New line' ) )
  38. .then( () => externalChanges.insertNewLine( [ 2 ] ) )
  39. .then( () => externalChanges.type( [ 2, 0 ], 'New line' ) )
  40. .then( () => externalChanges.insertNewLine( [ 3 ] ) )
  41. .then( () => externalChanges.type( [ 3, 0 ], 'New line' ) );
  42. } );
  43. document.querySelector( '.external-delete' ).addEventListener( 'click', () => {
  44. externalChanges.wait( 4000 )
  45. .then( () => externalChanges.removeElement( [ 1 ] ) );
  46. } );
  47. } )
  48. .catch( err => {
  49. console.error( err.stack );
  50. } );
  51. // Move it to the test utils.
  52. // See https://github.com/ckeditor/ckeditor5-ui/issues/393.
  53. function createExternalChangesSimulator( editor ) {
  54. const { model } = editor;
  55. function wait( delay ) {
  56. return new Promise( resolve => setTimeout( () => resolve(), delay ) );
  57. }
  58. function insertNewLine( path ) {
  59. model.enqueueChange( 'transparent', writer => {
  60. writer.insertElement( 'paragraph', new Position( model.document.getRoot(), path ) );
  61. } );
  62. return Promise.resolve();
  63. }
  64. function type( path, text ) {
  65. return new Promise( resolve => {
  66. let position = new Position( model.document.getRoot(), path );
  67. let index = 0;
  68. function typing() {
  69. wait( 40 ).then( () => {
  70. model.enqueueChange( 'transparent', writer => {
  71. writer.insertText( text[ index ], position );
  72. position = position.getShiftedBy( 1 );
  73. const nextLetter = text[ ++index ];
  74. if ( nextLetter ) {
  75. typing( nextLetter );
  76. } else {
  77. index = 0;
  78. resolve();
  79. }
  80. } );
  81. } );
  82. }
  83. typing();
  84. } );
  85. }
  86. function removeElement( path ) {
  87. model.enqueueChange( 'transparent', writer => {
  88. writer.remove( Range.createFromPositionAndShift( new Position( model.document.getRoot(), path ), 1 ) );
  89. } );
  90. return Promise.resolve();
  91. }
  92. return { wait, insertNewLine, type, removeElement };
  93. }