1.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals console:false, document, setTimeout */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
  7. import ArticlePresets from '@ckeditor/ckeditor5-presets/src/article';
  8. import ContextualToolbar from '../../../../src/toolbar/contextual/contextualtoolbar';
  9. import Element from '@ckeditor/ckeditor5-engine/src/model/element';
  10. import Text from '@ckeditor/ckeditor5-engine/src/model/text';
  11. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  12. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  13. // Editor for the external insert.
  14. ClassicEditor.create( document.querySelector( '#editor-insert' ), {
  15. plugins: [ ArticlePresets, ContextualToolbar ],
  16. toolbar: [ 'undo', 'redo' ],
  17. contextualToolbar: [ 'bold', 'italic' ]
  18. } )
  19. .then( editor => {
  20. const element = document.querySelector( '#button-insert' );
  21. element.addEventListener( 'click', () => {
  22. element.disabled = true;
  23. startExternalInsert( editor );
  24. } );
  25. } )
  26. .catch( err => console.error( err.stack ) );
  27. // Editor for the external delete.
  28. ClassicEditor.create( document.querySelector( '#editor-delete' ), {
  29. plugins: [ ArticlePresets, ContextualToolbar ],
  30. toolbar: [ 'undo', 'redo' ],
  31. contextualToolbar: [ 'bold', 'italic' ]
  32. } )
  33. .then( editor => {
  34. const element = document.querySelector( '#button-delete' );
  35. element.addEventListener( 'click', () => {
  36. element.disabled = true;
  37. startExternalDelete( editor );
  38. } );
  39. } )
  40. .catch( err => console.error( err.stack ) );
  41. function wait( delay ) {
  42. return new Promise( ( resolve ) => {
  43. setTimeout( () => resolve(), delay );
  44. } );
  45. }
  46. function startExternalInsert( editor ) {
  47. const document = editor.document;
  48. const bath = document.batch( 'transparent' );
  49. function type( path, text ) {
  50. return new Promise( ( resolve ) => {
  51. let position = new Position( document.getRoot(), path );
  52. let index = 0;
  53. function typing() {
  54. wait( 40 ).then( () => {
  55. document.enqueueChanges( () => {
  56. bath.insert( position, new Text( text[ index ] ) );
  57. position = position.getShiftedBy( 1 );
  58. let nextLetter = text[ ++index ];
  59. if ( nextLetter ) {
  60. typing( nextLetter );
  61. } else {
  62. index = 0;
  63. resolve();
  64. }
  65. } );
  66. } );
  67. }
  68. typing();
  69. } );
  70. }
  71. function insertNewLine( path ) {
  72. return wait( 200 ).then( () => {
  73. document.enqueueChanges( () => {
  74. bath.insert( new Position( document.getRoot(), path ), new Element( 'paragraph' ) );
  75. } );
  76. return Promise.resolve();
  77. } );
  78. }
  79. wait( 3000 )
  80. .then( () => type( [ 0, 36 ], `This specification defines the 5th major revision of the core language of the World Wide Web. ` ) )
  81. .then( () => insertNewLine( [ 0 ] ) )
  82. .then( () => type( [ 0, 0 ], 'a' ) )
  83. .then( () => insertNewLine( [ 1 ] ) )
  84. .then( () => type( [ 1, 0 ], 'b' ) )
  85. .then( () => insertNewLine( [ 2 ] ) )
  86. .then( () => type( [ 2, 0 ], 'c' ) )
  87. .then( () => insertNewLine( [ 0 ] ) )
  88. .then( () => type( [ 0, 0 ], 'DONE :)' ) );
  89. }
  90. function startExternalDelete( editor ) {
  91. const document = editor.document;
  92. const bath = document.batch( 'transparent' );
  93. wait( 3000 ).then( () => {
  94. document.enqueueChanges( () => {
  95. bath.remove( Range.createFromPositionAndShift( new Position( document.getRoot(), [ 1 ] ), 1 ) );
  96. } );
  97. } );
  98. }