copycut.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals console, window, document */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  8. import { stringify as stringifyView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  9. ClassicEditor
  10. .create( document.querySelector( '#editor' ), {
  11. plugins: [ ArticlePluginSet ],
  12. toolbar: [ 'headings', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'undo', 'redo' ]
  13. } )
  14. .then( editor => {
  15. window.editor = editor;
  16. const clipboard = editor.plugins.get( 'Clipboard' );
  17. editor.editing.view.on( 'paste', ( evt, data ) => {
  18. console.clear();
  19. onViewEvent( evt, data );
  20. } );
  21. editor.editing.view.on( 'paste', onViewEvent );
  22. editor.editing.view.on( 'copy', onViewEvent, { priority: 'lowest' } );
  23. editor.editing.view.on( 'cut', onViewEvent, { priority: 'lowest' } );
  24. clipboard.on( 'inputTransformation', onPipelineEvent );
  25. editor.editing.view.on( 'clipboardOutput', ( evt, data ) => {
  26. console.clear();
  27. onPipelineEvent( evt, data );
  28. } );
  29. function onViewEvent( evt, data ) {
  30. console.log( `----- ${ evt.name } -----` );
  31. console.log( 'text/html\n', data.dataTransfer.getData( 'text/html' ) );
  32. }
  33. function onPipelineEvent( evt, data ) {
  34. console.log( `----- ${ evt.name } -----` );
  35. console.log( 'stringify( data.content )\n', stringifyView( data.content ) );
  36. }
  37. } )
  38. .catch( err => {
  39. console.error( err.stack );
  40. } );
  41. document.getElementById( 'native' ).addEventListener( 'paste', onNativeEvent );
  42. document.getElementById( 'native' ).addEventListener( 'copy', onNativeEvent );
  43. document.getElementById( 'native' ).addEventListener( 'cut', onNativeEvent );
  44. function onNativeEvent( evt ) {
  45. console.clear();
  46. console.log( `----- native ${ evt.type } -----` );
  47. if ( evt.type == 'paste' ) {
  48. console.log( 'text/html\n', evt.clipboardData.getData( 'text/html' ) );
  49. }
  50. }