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