1.js 3.0 KB

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