8
0

1.js 3.1 KB

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