copycut.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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, 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: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'undo', 'redo' ]
  13. } )
  14. .then( editor => {
  15. window.editor = editor;
  16. const viewDocument = editor.editing.view.document;
  17. const clipboard = editor.plugins.get( 'Clipboard' );
  18. viewDocument.on( 'paste', ( evt, data ) => {
  19. console.clear();
  20. onViewEvent( evt, data );
  21. } );
  22. viewDocument.on( 'paste', onViewEvent );
  23. viewDocument.on( 'copy', onViewEvent, { priority: 'lowest' } );
  24. viewDocument.on( 'cut', onViewEvent, { priority: 'lowest' } );
  25. clipboard.on( 'inputTransformation', onPipelineEvent );
  26. viewDocument.on( 'clipboardOutput', ( evt, data ) => {
  27. console.clear();
  28. onPipelineEvent( evt, data );
  29. } );
  30. function onViewEvent( evt, data ) {
  31. console.log( `----- ${ evt.name } -----` );
  32. console.log( 'text/html\n', data.dataTransfer.getData( 'text/html' ) );
  33. }
  34. function onPipelineEvent( evt, data ) {
  35. console.log( `----- ${ evt.name } -----` );
  36. console.log( 'stringify( data.content )\n', stringifyView( data.content ) );
  37. }
  38. } )
  39. .catch( err => {
  40. console.error( err.stack );
  41. } );
  42. document.getElementById( 'native' ).addEventListener( 'paste', onNativeEvent );
  43. document.getElementById( 'native' ).addEventListener( 'copy', onNativeEvent );
  44. document.getElementById( 'native' ).addEventListener( 'cut', onNativeEvent );
  45. function onNativeEvent( evt ) {
  46. console.clear();
  47. console.log( `----- native ${ evt.type } -----` );
  48. if ( evt.type == 'paste' ) {
  49. console.log( 'text/html\n', evt.clipboardData.getData( 'text/html' ) );
  50. }
  51. }