externalchanges.js 3.2 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 ContextualToolbar from '../../../src/toolbar/contextual/contextualtoolbar';
  8. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  9. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  10. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  11. // Editor for the external insert.
  12. ClassicEditor
  13. .create( document.querySelector( '#editor-insert' ), {
  14. plugins: [ ArticlePluginSet, ContextualToolbar ],
  15. toolbar: [ 'bold', 'link' ],
  16. contextualToolbar: [ 'bold', '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: [ ArticlePluginSet, ContextualToolbar ],
  30. toolbar: [ 'bold', 'link' ],
  31. contextualToolbar: [ 'bold', 'link' ]
  32. } )
  33. .then( editor => {
  34. const element = document.querySelector( '#button-delete' );
  35. element.addEventListener( 'click', () => {
  36. element.disabled = true;
  37. startExternalDelete( editor );
  38. } );
  39. } )
  40. .catch( err => console.error( err.stack ) );
  41. function wait( delay ) {
  42. return new Promise( resolve => {
  43. setTimeout( () => resolve(), delay );
  44. } );
  45. }
  46. function startExternalInsert( editor ) {
  47. const model = editor.model;
  48. function type( path, text ) {
  49. return new Promise( resolve => {
  50. let position = new Position( model.document.getRoot(), path );
  51. let index = 0;
  52. function typing() {
  53. wait( 40 ).then( () => {
  54. model.enqueueChange( 'transparent', writer => {
  55. writer.insertText( text[ index ], position );
  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. model.enqueueChange( 'transparent', writer => {
  73. writer.insertElement( 'paragraph', new Position( model.document.getRoot(), path ) );
  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 model = editor.model;
  91. wait( 3000 ).then( () => {
  92. model.enqueueChange( 'transparent', writer => {
  93. writer.remove( Range.createFromPositionAndShift( new Position( model.document.getRoot(), [ 1 ] ), 1 ) );
  94. } );
  95. } );
  96. }