copycut.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 ArticlePreset from '@ckeditor/ckeditor5-presets/src/article';
  8. import { stringify as stringifyView } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  9. ClassicEditor.create( document.querySelector( '#editor' ), {
  10. plugins: [ ArticlePreset ],
  11. toolbar: [ 'headings', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'undo', 'redo' ]
  12. } )
  13. .then( editor => {
  14. window.editor = editor;
  15. const clipboard = editor.plugins.get( 'Clipboard' );
  16. editor.editing.view.on( 'paste', ( evt, data ) => {
  17. console.clear();
  18. onViewEvent( evt, data );
  19. } );
  20. editor.editing.view.on( 'paste', onViewEvent );
  21. editor.editing.view.on( 'copy', onViewEvent, { priority: 'lowest' } );
  22. editor.editing.view.on( 'cut', onViewEvent, { priority: 'lowest' } );
  23. clipboard.on( 'inputTransformation', onPipelineEvent );
  24. editor.editing.view.on( 'clipboardOutput', ( evt, data ) => {
  25. console.clear();
  26. onPipelineEvent( evt, data );
  27. } );
  28. function onViewEvent( evt, data ) {
  29. console.log( `----- ${ evt.name } -----` );
  30. console.log( 'text/html\n', data.dataTransfer.getData( 'text/html' ) );
  31. }
  32. function onPipelineEvent( evt, data ) {
  33. console.log( `----- ${ evt.name } -----` );
  34. console.log( 'stringify( data.content )\n', stringifyView( data.content ) );
  35. }
  36. } )
  37. .catch( err => {
  38. console.error( err.stack );
  39. } );
  40. document.getElementById( 'native' ).addEventListener( 'paste', onNativeEvent );
  41. document.getElementById( 'native' ).addEventListener( 'copy', onNativeEvent );
  42. document.getElementById( 'native' ).addEventListener( 'cut', onNativeEvent );
  43. function onNativeEvent( evt ) {
  44. console.clear();
  45. console.log( `----- native ${ evt.type } -----` );
  46. if ( evt.type == 'paste' ) {
  47. console.log( 'text/html\n', evt.clipboardData.getData( 'text/html' ) );
  48. }
  49. }