1.js 3.1 KB

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