1.js 3.2 KB

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