1.js 3.1 KB

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